src/3rdparty/sqlite/sqlite3.c
changeset 0 1918ee327afb
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /******************************************************************************
       
     2 ** This file is an amalgamation of many separate C source files from SQLite
       
     3 ** version 3.6.19.  By combining all the individual C code files into this 
       
     4 ** single large file, the entire code can be compiled as a one 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% are 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 ** This amalgamation was generated on 2009-10-14 11:35:02 UTC.
       
    21 */
       
    22 #define SQLITE_CORE 1
       
    23 #define SQLITE_AMALGAMATION 1
       
    24 #ifndef SQLITE_PRIVATE
       
    25 # define SQLITE_PRIVATE static
       
    26 #endif
       
    27 #ifndef SQLITE_API
       
    28 # define SQLITE_API
       
    29 #endif
       
    30 /************** Begin file sqliteInt.h ***************************************/
       
    31 /*
       
    32 ** 2001 September 15
       
    33 **
       
    34 ** The author disclaims copyright to this source code.  In place of
       
    35 ** a legal notice, here is a blessing:
       
    36 **
       
    37 **    May you do good and not evil.
       
    38 **    May you find forgiveness for yourself and forgive others.
       
    39 **    May you share freely, never taking more than you give.
       
    40 **
       
    41 *************************************************************************
       
    42 ** Internal interface definitions for SQLite.
       
    43 **
       
    44 */
       
    45 #ifndef _SQLITEINT_H_
       
    46 #define _SQLITEINT_H_
       
    47 
       
    48 /*
       
    49 ** These #defines should enable >2GB file support on POSIX if the
       
    50 ** underlying operating system supports it.  If the OS lacks
       
    51 ** large file support, or if the OS is windows, these should be no-ops.
       
    52 **
       
    53 ** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
       
    54 ** system #includes.  Hence, this block of code must be the very first
       
    55 ** code in all source files.
       
    56 **
       
    57 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
       
    58 ** on the compiler command line.  This is necessary if you are compiling
       
    59 ** on a recent machine (ex: Red Hat 7.2) but you want your code to work
       
    60 ** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
       
    61 ** without this option, LFS is enable.  But LFS does not exist in the kernel
       
    62 ** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
       
    63 ** portability you should omit LFS.
       
    64 **
       
    65 ** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
       
    66 */
       
    67 #ifndef SQLITE_DISABLE_LFS
       
    68 # define _LARGE_FILE       1
       
    69 # ifndef _FILE_OFFSET_BITS
       
    70 #   define _FILE_OFFSET_BITS 64
       
    71 # endif
       
    72 # define _LARGEFILE_SOURCE 1
       
    73 #endif
       
    74 
       
    75 /*
       
    76 ** Include the configuration header output by 'configure' if we're using the
       
    77 ** autoconf-based build
       
    78 */
       
    79 #ifdef _HAVE_SQLITE_CONFIG_H
       
    80 #include "config.h"
       
    81 #endif
       
    82 
       
    83 /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
       
    84 /************** Begin file sqliteLimit.h *************************************/
       
    85 /*
       
    86 ** 2007 May 7
       
    87 **
       
    88 ** The author disclaims copyright to this source code.  In place of
       
    89 ** a legal notice, here is a blessing:
       
    90 **
       
    91 **    May you do good and not evil.
       
    92 **    May you find forgiveness for yourself and forgive others.
       
    93 **    May you share freely, never taking more than you give.
       
    94 **
       
    95 *************************************************************************
       
    96 ** 
       
    97 ** This file defines various limits of what SQLite can process.
       
    98 **
       
    99 ** @(#) $Id: sqliteLimit.h,v 1.10 2009/01/10 16:15:09 danielk1977 Exp $
       
   100 */
       
   101 
       
   102 /*
       
   103 ** The maximum length of a TEXT or BLOB in bytes.   This also
       
   104 ** limits the size of a row in a table or index.
       
   105 **
       
   106 ** The hard limit is the ability of a 32-bit signed integer
       
   107 ** to count the size: 2^31-1 or 2147483647.
       
   108 */
       
   109 #ifndef SQLITE_MAX_LENGTH
       
   110 # define SQLITE_MAX_LENGTH 1000000000
       
   111 #endif
       
   112 
       
   113 /*
       
   114 ** This is the maximum number of
       
   115 **
       
   116 **    * Columns in a table
       
   117 **    * Columns in an index
       
   118 **    * Columns in a view
       
   119 **    * Terms in the SET clause of an UPDATE statement
       
   120 **    * Terms in the result set of a SELECT statement
       
   121 **    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
       
   122 **    * Terms in the VALUES clause of an INSERT statement
       
   123 **
       
   124 ** The hard upper limit here is 32676.  Most database people will
       
   125 ** tell you that in a well-normalized database, you usually should
       
   126 ** not have more than a dozen or so columns in any table.  And if
       
   127 ** that is the case, there is no point in having more than a few
       
   128 ** dozen values in any of the other situations described above.
       
   129 */
       
   130 #ifndef SQLITE_MAX_COLUMN
       
   131 # define SQLITE_MAX_COLUMN 2000
       
   132 #endif
       
   133 
       
   134 /*
       
   135 ** The maximum length of a single SQL statement in bytes.
       
   136 **
       
   137 ** It used to be the case that setting this value to zero would
       
   138 ** turn the limit off.  That is no longer true.  It is not possible
       
   139 ** to turn this limit off.
       
   140 */
       
   141 #ifndef SQLITE_MAX_SQL_LENGTH
       
   142 # define SQLITE_MAX_SQL_LENGTH 1000000000
       
   143 #endif
       
   144 
       
   145 /*
       
   146 ** The maximum depth of an expression tree. This is limited to 
       
   147 ** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might 
       
   148 ** want to place more severe limits on the complexity of an 
       
   149 ** expression.
       
   150 **
       
   151 ** A value of 0 used to mean that the limit was not enforced.
       
   152 ** But that is no longer true.  The limit is now strictly enforced
       
   153 ** at all times.
       
   154 */
       
   155 #ifndef SQLITE_MAX_EXPR_DEPTH
       
   156 # define SQLITE_MAX_EXPR_DEPTH 1000
       
   157 #endif
       
   158 
       
   159 /*
       
   160 ** The maximum number of terms in a compound SELECT statement.
       
   161 ** The code generator for compound SELECT statements does one
       
   162 ** level of recursion for each term.  A stack overflow can result
       
   163 ** if the number of terms is too large.  In practice, most SQL
       
   164 ** never has more than 3 or 4 terms.  Use a value of 0 to disable
       
   165 ** any limit on the number of terms in a compount SELECT.
       
   166 */
       
   167 #ifndef SQLITE_MAX_COMPOUND_SELECT
       
   168 # define SQLITE_MAX_COMPOUND_SELECT 500
       
   169 #endif
       
   170 
       
   171 /*
       
   172 ** The maximum number of opcodes in a VDBE program.
       
   173 ** Not currently enforced.
       
   174 */
       
   175 #ifndef SQLITE_MAX_VDBE_OP
       
   176 # define SQLITE_MAX_VDBE_OP 25000
       
   177 #endif
       
   178 
       
   179 /*
       
   180 ** The maximum number of arguments to an SQL function.
       
   181 */
       
   182 #ifndef SQLITE_MAX_FUNCTION_ARG
       
   183 # define SQLITE_MAX_FUNCTION_ARG 127
       
   184 #endif
       
   185 
       
   186 /*
       
   187 ** The maximum number of in-memory pages to use for the main database
       
   188 ** table and for temporary tables.  The SQLITE_DEFAULT_CACHE_SIZE
       
   189 */
       
   190 #ifndef SQLITE_DEFAULT_CACHE_SIZE
       
   191 # define SQLITE_DEFAULT_CACHE_SIZE  2000
       
   192 #endif
       
   193 #ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
       
   194 # define SQLITE_DEFAULT_TEMP_CACHE_SIZE  500
       
   195 #endif
       
   196 
       
   197 /*
       
   198 ** The maximum number of attached databases.  This must be between 0
       
   199 ** and 30.  The upper bound on 30 is because a 32-bit integer bitmap
       
   200 ** is used internally to track attached databases.
       
   201 */
       
   202 #ifndef SQLITE_MAX_ATTACHED
       
   203 # define SQLITE_MAX_ATTACHED 10
       
   204 #endif
       
   205 
       
   206 
       
   207 /*
       
   208 ** The maximum value of a ?nnn wildcard that the parser will accept.
       
   209 */
       
   210 #ifndef SQLITE_MAX_VARIABLE_NUMBER
       
   211 # define SQLITE_MAX_VARIABLE_NUMBER 999
       
   212 #endif
       
   213 
       
   214 /* Maximum page size.  The upper bound on this value is 32768.  This a limit
       
   215 ** imposed by the necessity of storing the value in a 2-byte unsigned integer
       
   216 ** and the fact that the page size must be a power of 2.
       
   217 **
       
   218 ** If this limit is changed, then the compiled library is technically
       
   219 ** incompatible with an SQLite library compiled with a different limit. If
       
   220 ** a process operating on a database with a page-size of 65536 bytes 
       
   221 ** crashes, then an instance of SQLite compiled with the default page-size 
       
   222 ** limit will not be able to rollback the aborted transaction. This could
       
   223 ** lead to database corruption.
       
   224 */
       
   225 #ifndef SQLITE_MAX_PAGE_SIZE
       
   226 # define SQLITE_MAX_PAGE_SIZE 32768
       
   227 #endif
       
   228 
       
   229 
       
   230 /*
       
   231 ** The default size of a database page.
       
   232 */
       
   233 #ifndef SQLITE_DEFAULT_PAGE_SIZE
       
   234 # define SQLITE_DEFAULT_PAGE_SIZE 1024
       
   235 #endif
       
   236 #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
       
   237 # undef SQLITE_DEFAULT_PAGE_SIZE
       
   238 # define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
       
   239 #endif
       
   240 
       
   241 /*
       
   242 ** Ordinarily, if no value is explicitly provided, SQLite creates databases
       
   243 ** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
       
   244 ** device characteristics (sector-size and atomic write() support),
       
   245 ** SQLite may choose a larger value. This constant is the maximum value
       
   246 ** SQLite will choose on its own.
       
   247 */
       
   248 #ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
       
   249 # define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
       
   250 #endif
       
   251 #if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
       
   252 # undef SQLITE_MAX_DEFAULT_PAGE_SIZE
       
   253 # define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
       
   254 #endif
       
   255 
       
   256 
       
   257 /*
       
   258 ** Maximum number of pages in one database file.
       
   259 **
       
   260 ** This is really just the default value for the max_page_count pragma.
       
   261 ** This value can be lowered (or raised) at run-time using that the
       
   262 ** max_page_count macro.
       
   263 */
       
   264 #ifndef SQLITE_MAX_PAGE_COUNT
       
   265 # define SQLITE_MAX_PAGE_COUNT 1073741823
       
   266 #endif
       
   267 
       
   268 /*
       
   269 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
       
   270 ** operator.
       
   271 */
       
   272 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
       
   273 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
       
   274 #endif
       
   275 
       
   276 /*
       
   277 ** Maximum depth of recursion for triggers.
       
   278 **
       
   279 ** A value of 1 means that a trigger program will not be able to itself
       
   280 ** fire any triggers. A value of 0 means that no trigger programs at all 
       
   281 ** may be executed.
       
   282 */
       
   283 #ifndef SQLITE_MAX_TRIGGER_DEPTH
       
   284 #if defined(SQLITE_SMALL_STACK)
       
   285 # define SQLITE_MAX_TRIGGER_DEPTH 10
       
   286 #else
       
   287 # define SQLITE_MAX_TRIGGER_DEPTH 1000
       
   288 #endif
       
   289 #endif
       
   290 
       
   291 /************** End of sqliteLimit.h *****************************************/
       
   292 /************** Continuing where we left off in sqliteInt.h ******************/
       
   293 
       
   294 /* Disable nuisance warnings on Borland compilers */
       
   295 #if defined(__BORLANDC__)
       
   296 #pragma warn -rch /* unreachable code */
       
   297 #pragma warn -ccc /* Condition is always true or false */
       
   298 #pragma warn -aus /* Assigned value is never used */
       
   299 #pragma warn -csu /* Comparing signed and unsigned */
       
   300 #pragma warn -spa /* Suspicious pointer arithmetic */
       
   301 #endif
       
   302 
       
   303 /* Needed for various definitions... */
       
   304 #ifndef _GNU_SOURCE
       
   305 # define _GNU_SOURCE
       
   306 #endif
       
   307 
       
   308 /*
       
   309 ** Include standard header files as necessary
       
   310 */
       
   311 #ifdef HAVE_STDINT_H
       
   312 #include <stdint.h>
       
   313 #endif
       
   314 #ifdef HAVE_INTTYPES_H
       
   315 #include <inttypes.h>
       
   316 #endif
       
   317 
       
   318 #define SQLITE_INDEX_SAMPLES 10
       
   319 
       
   320 /*
       
   321 ** This macro is used to "hide" some ugliness in casting an int
       
   322 ** value to a ptr value under the MSVC 64-bit compiler.   Casting
       
   323 ** non 64-bit values to ptr types results in a "hard" error with 
       
   324 ** the MSVC 64-bit compiler which this attempts to avoid.  
       
   325 **
       
   326 ** A simple compiler pragma or casting sequence could not be found
       
   327 ** to correct this in all situations, so this macro was introduced.
       
   328 **
       
   329 ** It could be argued that the intptr_t type could be used in this
       
   330 ** case, but that type is not available on all compilers, or 
       
   331 ** requires the #include of specific headers which differs between
       
   332 ** platforms.
       
   333 **
       
   334 ** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
       
   335 ** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
       
   336 ** So we have to define the macros in different ways depending on the
       
   337 ** compiler.
       
   338 */
       
   339 #if defined(__GNUC__)
       
   340 # if defined(HAVE_STDINT_H)
       
   341 #   define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
       
   342 #   define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
       
   343 # else
       
   344 #   define SQLITE_INT_TO_PTR(X)  ((void*)(X))
       
   345 #   define SQLITE_PTR_TO_INT(X)  ((int)(X))
       
   346 # endif
       
   347 #else
       
   348 # define SQLITE_INT_TO_PTR(X)   ((void*)&((char*)0)[X])
       
   349 # define SQLITE_PTR_TO_INT(X)   ((int)(((char*)X)-(char*)0))
       
   350 #endif
       
   351 
       
   352 
       
   353 /*
       
   354 ** The SQLITE_THREADSAFE macro must be defined as either 0 or 1.
       
   355 ** Older versions of SQLite used an optional THREADSAFE macro.
       
   356 ** We support that for legacy
       
   357 */
       
   358 #if !defined(SQLITE_THREADSAFE)
       
   359 #if defined(THREADSAFE)
       
   360 # define SQLITE_THREADSAFE THREADSAFE
       
   361 #else
       
   362 # define SQLITE_THREADSAFE 1
       
   363 #endif
       
   364 #endif
       
   365 
       
   366 /*
       
   367 ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
       
   368 ** It determines whether or not the features related to 
       
   369 ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
       
   370 ** be overridden at runtime using the sqlite3_config() API.
       
   371 */
       
   372 #if !defined(SQLITE_DEFAULT_MEMSTATUS)
       
   373 # define SQLITE_DEFAULT_MEMSTATUS 1
       
   374 #endif
       
   375 
       
   376 /*
       
   377 ** Exactly one of the following macros must be defined in order to
       
   378 ** specify which memory allocation subsystem to use.
       
   379 **
       
   380 **     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
       
   381 **     SQLITE_MEMDEBUG               // Debugging version of system malloc()
       
   382 **     SQLITE_MEMORY_SIZE            // internal allocator #1
       
   383 **     SQLITE_MMAP_HEAP_SIZE         // internal mmap() allocator
       
   384 **     SQLITE_POW2_MEMORY_SIZE       // internal power-of-two allocator
       
   385 **
       
   386 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
       
   387 ** the default.
       
   388 */
       
   389 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)+\
       
   390     defined(SQLITE_MEMORY_SIZE)+defined(SQLITE_MMAP_HEAP_SIZE)+\
       
   391     defined(SQLITE_POW2_MEMORY_SIZE)>1
       
   392 # error "At most one of the following compile-time configuration options\
       
   393  is allows: SQLITE_SYSTEM_MALLOC, SQLITE_MEMDEBUG, SQLITE_MEMORY_SIZE,\
       
   394  SQLITE_MMAP_HEAP_SIZE, SQLITE_POW2_MEMORY_SIZE"
       
   395 #endif
       
   396 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)+\
       
   397     defined(SQLITE_MEMORY_SIZE)+defined(SQLITE_MMAP_HEAP_SIZE)+\
       
   398     defined(SQLITE_POW2_MEMORY_SIZE)==0
       
   399 # define SQLITE_SYSTEM_MALLOC 1
       
   400 #endif
       
   401 
       
   402 /*
       
   403 ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
       
   404 ** sizes of memory allocations below this value where possible.
       
   405 */
       
   406 #if !defined(SQLITE_MALLOC_SOFT_LIMIT)
       
   407 # define SQLITE_MALLOC_SOFT_LIMIT 1024
       
   408 #endif
       
   409 
       
   410 /*
       
   411 ** We need to define _XOPEN_SOURCE as follows in order to enable
       
   412 ** recursive mutexes on most Unix systems.  But Mac OS X is different.
       
   413 ** The _XOPEN_SOURCE define causes problems for Mac OS X we are told,
       
   414 ** so it is omitted there.  See ticket #2673.
       
   415 **
       
   416 ** Later we learn that _XOPEN_SOURCE is poorly or incorrectly
       
   417 ** implemented on some systems.  So we avoid defining it at all
       
   418 ** if it is already defined or if it is unneeded because we are
       
   419 ** not doing a threadsafe build.  Ticket #2681.
       
   420 **
       
   421 ** See also ticket #2741.
       
   422 */
       
   423 #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE && !defined(VXWORKS)
       
   424 #  define _XOPEN_SOURCE 500  /* Needed to enable pthread recursive mutexes */
       
   425 #endif
       
   426 
       
   427 /*
       
   428 ** The TCL headers are only needed when compiling the TCL bindings.
       
   429 */
       
   430 #if defined(SQLITE_TCL) || defined(TCLSH)
       
   431 # include <tcl.h>
       
   432 #endif
       
   433 
       
   434 /*
       
   435 ** Many people are failing to set -DNDEBUG=1 when compiling SQLite.
       
   436 ** Setting NDEBUG makes the code smaller and run faster.  So the following
       
   437 ** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1
       
   438 ** option is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
       
   439 ** feature.
       
   440 */
       
   441 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
       
   442 # define NDEBUG 1
       
   443 #endif
       
   444 
       
   445 /*
       
   446 ** The testcase() macro is used to aid in coverage testing.  When 
       
   447 ** doing coverage testing, the condition inside the argument to
       
   448 ** testcase() must be evaluated both true and false in order to
       
   449 ** get full branch coverage.  The testcase() macro is inserted
       
   450 ** to help ensure adequate test coverage in places where simple
       
   451 ** condition/decision coverage is inadequate.  For example, testcase()
       
   452 ** can be used to make sure boundary values are tested.  For
       
   453 ** bitmask tests, testcase() can be used to make sure each bit
       
   454 ** is significant and used at least once.  On switch statements
       
   455 ** where multiple cases go to the same block of code, testcase()
       
   456 ** can insure that all cases are evaluated.
       
   457 **
       
   458 */
       
   459 #ifdef SQLITE_COVERAGE_TEST
       
   460 SQLITE_PRIVATE   void sqlite3Coverage(int);
       
   461 # define testcase(X)  if( X ){ sqlite3Coverage(__LINE__); }
       
   462 #else
       
   463 # define testcase(X)
       
   464 #endif
       
   465 
       
   466 /*
       
   467 ** The TESTONLY macro is used to enclose variable declarations or
       
   468 ** other bits of code that are needed to support the arguments
       
   469 ** within testcase() and assert() macros.
       
   470 */
       
   471 #if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
       
   472 # define TESTONLY(X)  X
       
   473 #else
       
   474 # define TESTONLY(X)
       
   475 #endif
       
   476 
       
   477 /*
       
   478 ** Sometimes we need a small amount of code such as a variable initialization
       
   479 ** to setup for a later assert() statement.  We do not want this code to
       
   480 ** appear when assert() is disabled.  The following macro is therefore
       
   481 ** used to contain that setup code.  The "VVA" acronym stands for
       
   482 ** "Verification, Validation, and Accreditation".  In other words, the
       
   483 ** code within VVA_ONLY() will only run during verification processes.
       
   484 */
       
   485 #ifndef NDEBUG
       
   486 # define VVA_ONLY(X)  X
       
   487 #else
       
   488 # define VVA_ONLY(X)
       
   489 #endif
       
   490 
       
   491 /*
       
   492 ** The ALWAYS and NEVER macros surround boolean expressions which 
       
   493 ** are intended to always be true or false, respectively.  Such
       
   494 ** expressions could be omitted from the code completely.  But they
       
   495 ** are included in a few cases in order to enhance the resilience
       
   496 ** of SQLite to unexpected behavior - to make the code "self-healing"
       
   497 ** or "ductile" rather than being "brittle" and crashing at the first
       
   498 ** hint of unplanned behavior.
       
   499 **
       
   500 ** In other words, ALWAYS and NEVER are added for defensive code.
       
   501 **
       
   502 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
       
   503 ** be true and false so that the unreachable code then specify will
       
   504 ** not be counted as untested code.
       
   505 */
       
   506 #if defined(SQLITE_COVERAGE_TEST)
       
   507 # define ALWAYS(X)      (1)
       
   508 # define NEVER(X)       (0)
       
   509 #elif !defined(NDEBUG)
       
   510 # define ALWAYS(X)      ((X)?1:(assert(0),0))
       
   511 # define NEVER(X)       ((X)?(assert(0),1):0)
       
   512 #else
       
   513 # define ALWAYS(X)      (X)
       
   514 # define NEVER(X)       (X)
       
   515 #endif
       
   516 
       
   517 /*
       
   518 ** The macro unlikely() is a hint that surrounds a boolean
       
   519 ** expression that is usually false.  Macro likely() surrounds
       
   520 ** a boolean expression that is usually true.  GCC is able to
       
   521 ** use these hints to generate better code, sometimes.
       
   522 */
       
   523 #if defined(__GNUC__) && 0
       
   524 # define likely(X)    __builtin_expect((X),1)
       
   525 # define unlikely(X)  __builtin_expect((X),0)
       
   526 #else
       
   527 # define likely(X)    !!(X)
       
   528 # define unlikely(X)  !!(X)
       
   529 #endif
       
   530 
       
   531 /************** Include sqlite3.h in the middle of sqliteInt.h ***************/
       
   532 /************** Begin file sqlite3.h *****************************************/
       
   533 /*
       
   534 ** 2001 September 15
       
   535 **
       
   536 ** The author disclaims copyright to this source code.  In place of
       
   537 ** a legal notice, here is a blessing:
       
   538 **
       
   539 **    May you do good and not evil.
       
   540 **    May you find forgiveness for yourself and forgive others.
       
   541 **    May you share freely, never taking more than you give.
       
   542 **
       
   543 *************************************************************************
       
   544 ** This header file defines the interface that the SQLite library
       
   545 ** presents to client programs.  If a C-function, structure, datatype,
       
   546 ** or constant definition does not appear in this file, then it is
       
   547 ** not a published API of SQLite, is subject to change without
       
   548 ** notice, and should not be referenced by programs that use SQLite.
       
   549 **
       
   550 ** Some of the definitions that are in this file are marked as
       
   551 ** "experimental".  Experimental interfaces are normally new
       
   552 ** features recently added to SQLite.  We do not anticipate changes
       
   553 ** to experimental interfaces but reserve the right to make minor changes
       
   554 ** if experience from use "in the wild" suggest such changes are prudent.
       
   555 **
       
   556 ** The official C-language API documentation for SQLite is derived
       
   557 ** from comments in this file.  This file is the authoritative source
       
   558 ** on how SQLite interfaces are suppose to operate.
       
   559 **
       
   560 ** The name of this file under configuration management is "sqlite.h.in".
       
   561 ** The makefile makes some minor changes to this file (such as inserting
       
   562 ** the version number) and changes its name to "sqlite3.h" as
       
   563 ** part of the build process.
       
   564 */
       
   565 #ifndef _SQLITE3_H_
       
   566 #define _SQLITE3_H_
       
   567 
       
   568 #ifdef VXWORKS
       
   569 # define SQLITE_HOMEGROWN_RECURSIVE_MUTEX
       
   570 # define NO_GETTOD
       
   571 # include <ioLib.h>
       
   572 #endif
       
   573 
       
   574 #include <stdarg.h>     /* Needed for the definition of va_list */
       
   575 
       
   576 /*
       
   577 ** Make sure we can call this stuff from C++.
       
   578 */
       
   579 #if 0
       
   580 extern "C" {
       
   581 #endif
       
   582 
       
   583 
       
   584 /*
       
   585 ** Add the ability to override 'extern'
       
   586 */
       
   587 #ifndef SQLITE_EXTERN
       
   588 # define SQLITE_EXTERN extern
       
   589 #endif
       
   590 
       
   591 #ifndef SQLITE_API
       
   592 # define SQLITE_API
       
   593 #endif
       
   594 
       
   595 
       
   596 /*
       
   597 ** These no-op macros are used in front of interfaces to mark those
       
   598 ** interfaces as either deprecated or experimental.  New applications
       
   599 ** should not use deprecated interfaces - they are support for backwards
       
   600 ** compatibility only.  Application writers should be aware that
       
   601 ** experimental interfaces are subject to change in point releases.
       
   602 **
       
   603 ** These macros used to resolve to various kinds of compiler magic that
       
   604 ** would generate warning messages when they were used.  But that
       
   605 ** compiler magic ended up generating such a flurry of bug reports
       
   606 ** that we have taken it all out and gone back to using simple
       
   607 ** noop macros.
       
   608 */
       
   609 #define SQLITE_DEPRECATED
       
   610 #define SQLITE_EXPERIMENTAL
       
   611 
       
   612 /*
       
   613 ** Ensure these symbols were not defined by some previous header file.
       
   614 */
       
   615 #ifdef SQLITE_VERSION
       
   616 # undef SQLITE_VERSION
       
   617 #endif
       
   618 #ifdef SQLITE_VERSION_NUMBER
       
   619 # undef SQLITE_VERSION_NUMBER
       
   620 #endif
       
   621 
       
   622 /*
       
   623 ** CAPI3REF: Compile-Time Library Version Numbers {H10010} <S60100>
       
   624 **
       
   625 ** The SQLITE_VERSION and SQLITE_VERSION_NUMBER #defines in
       
   626 ** the sqlite3.h file specify the version of SQLite with which
       
   627 ** that header file is associated.
       
   628 **
       
   629 ** The "version" of SQLite is a string of the form "W.X.Y" or "W.X.Y.Z".
       
   630 ** The W value is major version number and is always 3 in SQLite3.
       
   631 ** The W value only changes when backwards compatibility is
       
   632 ** broken and we intend to never break backwards compatibility.
       
   633 ** The X value is the minor version number and only changes when
       
   634 ** there are major feature enhancements that are forwards compatible
       
   635 ** but not backwards compatible.
       
   636 ** The Y value is the release number and is incremented with
       
   637 ** each release but resets back to 0 whenever X is incremented.
       
   638 ** The Z value only appears on branch releases.
       
   639 **
       
   640 ** The SQLITE_VERSION_NUMBER is an integer that is computed as
       
   641 ** follows:
       
   642 **
       
   643 ** <blockquote><pre>
       
   644 ** SQLITE_VERSION_NUMBER = W*1000000 + X*1000 + Y
       
   645 ** </pre></blockquote>
       
   646 **
       
   647 ** Since version 3.6.18, SQLite source code has been stored in the
       
   648 ** <a href="http://www.fossil-scm.org/">fossil configuration management
       
   649 ** system</a>.  The SQLITE_SOURCE_ID
       
   650 ** macro is a string which identifies a particular check-in of SQLite
       
   651 ** within its configuration management system.  The string contains the
       
   652 ** date and time of the check-in (UTC) and an SHA1 hash of the entire
       
   653 ** source tree.
       
   654 **
       
   655 ** See also: [sqlite3_libversion()],
       
   656 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
       
   657 ** [sqlite_version()] and [sqlite_source_id()].
       
   658 **
       
   659 ** Requirements: [H10011] [H10014]
       
   660 */
       
   661 #define SQLITE_VERSION        "3.6.19"
       
   662 #define SQLITE_VERSION_NUMBER 3006019
       
   663 #define SQLITE_SOURCE_ID      "2009-10-14 11:33:55 c1d499afc50d54b376945b4efb65c56c787a073d"
       
   664 
       
   665 /*
       
   666 ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
       
   667 ** KEYWORDS: sqlite3_version
       
   668 **
       
   669 ** These interfaces provide the same information as the [SQLITE_VERSION],
       
   670 ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] #defines in the header,
       
   671 ** but are associated with the library instead of the header file.  Cautious
       
   672 ** programmers might include assert() statements in their application to
       
   673 ** verify that values returned by these interfaces match the macros in
       
   674 ** the header, and thus insure that the application is
       
   675 ** compiled with matching library and header files.
       
   676 **
       
   677 ** <blockquote><pre>
       
   678 ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
       
   679 ** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
       
   680 ** assert( strcmp(sqlite3_libversion,SQLITE_VERSION)==0 );
       
   681 ** </pre></blockquote>
       
   682 **
       
   683 ** The sqlite3_libversion() function returns the same information as is
       
   684 ** in the sqlite3_version[] string constant.  The function is provided
       
   685 ** for use in DLLs since DLL users usually do not have direct access to string
       
   686 ** constants within the DLL.  Similarly, the sqlite3_sourceid() function
       
   687 ** returns the same information as is in the [SQLITE_SOURCE_ID] #define of
       
   688 ** the header file.
       
   689 **
       
   690 ** See also: [sqlite_version()] and [sqlite_source_id()].
       
   691 **
       
   692 ** Requirements: [H10021] [H10022] [H10023]
       
   693 */
       
   694 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
       
   695 SQLITE_API const char *sqlite3_libversion(void);
       
   696 SQLITE_API const char *sqlite3_sourceid(void);
       
   697 SQLITE_API int sqlite3_libversion_number(void);
       
   698 
       
   699 /*
       
   700 ** CAPI3REF: Test To See If The Library Is Threadsafe {H10100} <S60100>
       
   701 **
       
   702 ** SQLite can be compiled with or without mutexes.  When
       
   703 ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
       
   704 ** are enabled and SQLite is threadsafe.  When the
       
   705 ** [SQLITE_THREADSAFE] macro is 0, 
       
   706 ** the mutexes are omitted.  Without the mutexes, it is not safe
       
   707 ** to use SQLite concurrently from more than one thread.
       
   708 **
       
   709 ** Enabling mutexes incurs a measurable performance penalty.
       
   710 ** So if speed is of utmost importance, it makes sense to disable
       
   711 ** the mutexes.  But for maximum safety, mutexes should be enabled.
       
   712 ** The default behavior is for mutexes to be enabled.
       
   713 **
       
   714 ** This interface can be used by an application to make sure that the
       
   715 ** version of SQLite that it is linking against was compiled with
       
   716 ** the desired setting of the [SQLITE_THREADSAFE] macro.
       
   717 **
       
   718 ** This interface only reports on the compile-time mutex setting
       
   719 ** of the [SQLITE_THREADSAFE] flag.  If SQLite is compiled with
       
   720 ** SQLITE_THREADSAFE=1 then mutexes are enabled by default but
       
   721 ** can be fully or partially disabled using a call to [sqlite3_config()]
       
   722 ** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
       
   723 ** or [SQLITE_CONFIG_MUTEX].  The return value of this function shows
       
   724 ** only the default compile-time setting, not any run-time changes
       
   725 ** to that setting.
       
   726 **
       
   727 ** See the [threading mode] documentation for additional information.
       
   728 **
       
   729 ** Requirements: [H10101] [H10102]
       
   730 */
       
   731 SQLITE_API int sqlite3_threadsafe(void);
       
   732 
       
   733 /*
       
   734 ** CAPI3REF: Database Connection Handle {H12000} <S40200>
       
   735 ** KEYWORDS: {database connection} {database connections}
       
   736 **
       
   737 ** Each open SQLite database is represented by a pointer to an instance of
       
   738 ** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
       
   739 ** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
       
   740 ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
       
   741 ** is its destructor.  There are many other interfaces (such as
       
   742 ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
       
   743 ** [sqlite3_busy_timeout()] to name but three) that are methods on an
       
   744 ** sqlite3 object.
       
   745 */
       
   746 typedef struct sqlite3 sqlite3;
       
   747 
       
   748 /*
       
   749 ** CAPI3REF: 64-Bit Integer Types {H10200} <S10110>
       
   750 ** KEYWORDS: sqlite_int64 sqlite_uint64
       
   751 **
       
   752 ** Because there is no cross-platform way to specify 64-bit integer types
       
   753 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
       
   754 **
       
   755 ** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
       
   756 ** The sqlite_int64 and sqlite_uint64 types are supported for backwards
       
   757 ** compatibility only.
       
   758 **
       
   759 ** Requirements: [H10201] [H10202]
       
   760 */
       
   761 #ifdef SQLITE_INT64_TYPE
       
   762   typedef SQLITE_INT64_TYPE sqlite_int64;
       
   763   typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
       
   764 #elif defined(_MSC_VER) || defined(__BORLANDC__)
       
   765   typedef __int64 sqlite_int64;
       
   766   typedef unsigned __int64 sqlite_uint64;
       
   767 #else
       
   768   typedef long long int sqlite_int64;
       
   769   typedef unsigned long long int sqlite_uint64;
       
   770 #endif
       
   771 typedef sqlite_int64 sqlite3_int64;
       
   772 typedef sqlite_uint64 sqlite3_uint64;
       
   773 
       
   774 /*
       
   775 ** If compiling for a processor that lacks floating point support,
       
   776 ** substitute integer for floating-point.
       
   777 */
       
   778 #ifdef SQLITE_OMIT_FLOATING_POINT
       
   779 # define double sqlite3_int64
       
   780 #endif
       
   781 
       
   782 /*
       
   783 ** CAPI3REF: Closing A Database Connection {H12010} <S30100><S40200>
       
   784 **
       
   785 ** This routine is the destructor for the [sqlite3] object.
       
   786 **
       
   787 ** Applications should [sqlite3_finalize | finalize] all [prepared statements]
       
   788 ** and [sqlite3_blob_close | close] all [BLOB handles] associated with
       
   789 ** the [sqlite3] object prior to attempting to close the object.
       
   790 ** The [sqlite3_next_stmt()] interface can be used to locate all
       
   791 ** [prepared statements] associated with a [database connection] if desired.
       
   792 ** Typical code might look like this:
       
   793 **
       
   794 ** <blockquote><pre>
       
   795 ** sqlite3_stmt *pStmt;
       
   796 ** while( (pStmt = sqlite3_next_stmt(db, 0))!=0 ){
       
   797 ** &nbsp;   sqlite3_finalize(pStmt);
       
   798 ** }
       
   799 ** </pre></blockquote>
       
   800 **
       
   801 ** If [sqlite3_close()] is invoked while a transaction is open,
       
   802 ** the transaction is automatically rolled back.
       
   803 **
       
   804 ** The C parameter to [sqlite3_close(C)] must be either a NULL
       
   805 ** pointer or an [sqlite3] object pointer obtained
       
   806 ** from [sqlite3_open()], [sqlite3_open16()], or
       
   807 ** [sqlite3_open_v2()], and not previously closed.
       
   808 **
       
   809 ** Requirements:
       
   810 ** [H12011] [H12012] [H12013] [H12014] [H12015] [H12019]
       
   811 */
       
   812 SQLITE_API int sqlite3_close(sqlite3 *);
       
   813 
       
   814 /*
       
   815 ** The type for a callback function.
       
   816 ** This is legacy and deprecated.  It is included for historical
       
   817 ** compatibility and is not documented.
       
   818 */
       
   819 typedef int (*sqlite3_callback)(void*,int,char**, char**);
       
   820 
       
   821 /*
       
   822 ** CAPI3REF: One-Step Query Execution Interface {H12100} <S10000>
       
   823 **
       
   824 ** The sqlite3_exec() interface is a convenient way of running one or more
       
   825 ** SQL statements without having to write a lot of C code.  The UTF-8 encoded
       
   826 ** SQL statements are passed in as the second parameter to sqlite3_exec().
       
   827 ** The statements are evaluated one by one until either an error or
       
   828 ** an interrupt is encountered, or until they are all done.  The 3rd parameter
       
   829 ** is an optional callback that is invoked once for each row of any query
       
   830 ** results produced by the SQL statements.  The 5th parameter tells where
       
   831 ** to write any error messages.
       
   832 **
       
   833 ** The error message passed back through the 5th parameter is held
       
   834 ** in memory obtained from [sqlite3_malloc()].  To avoid a memory leak,
       
   835 ** the calling application should call [sqlite3_free()] on any error
       
   836 ** message returned through the 5th parameter when it has finished using
       
   837 ** the error message.
       
   838 **
       
   839 ** If the SQL statement in the 2nd parameter is NULL or an empty string
       
   840 ** or a string containing only whitespace and comments, then no SQL
       
   841 ** statements are evaluated and the database is not changed.
       
   842 **
       
   843 ** The sqlite3_exec() interface is implemented in terms of
       
   844 ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()].
       
   845 ** The sqlite3_exec() routine does nothing to the database that cannot be done
       
   846 ** by [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()].
       
   847 **
       
   848 ** The first parameter to [sqlite3_exec()] must be an valid and open
       
   849 ** [database connection].
       
   850 **
       
   851 ** The database connection must not be closed while
       
   852 ** [sqlite3_exec()] is running.
       
   853 **
       
   854 ** The calling function should use [sqlite3_free()] to free
       
   855 ** the memory that *errmsg is left pointing at once the error
       
   856 ** message is no longer needed.
       
   857 **
       
   858 ** The SQL statement text in the 2nd parameter to [sqlite3_exec()]
       
   859 ** must remain unchanged while [sqlite3_exec()] is running.
       
   860 **
       
   861 ** Requirements:
       
   862 ** [H12101] [H12102] [H12104] [H12105] [H12107] [H12110] [H12113] [H12116]
       
   863 ** [H12119] [H12122] [H12125] [H12131] [H12134] [H12137] [H12138]
       
   864 */
       
   865 SQLITE_API int sqlite3_exec(
       
   866   sqlite3*,                                  /* An open database */
       
   867   const char *sql,                           /* SQL to be evaluated */
       
   868   int (*callback)(void*,int,char**,char**),  /* Callback function */
       
   869   void *,                                    /* 1st argument to callback */
       
   870   char **errmsg                              /* Error msg written here */
       
   871 );
       
   872 
       
   873 /*
       
   874 ** CAPI3REF: Result Codes {H10210} <S10700>
       
   875 ** KEYWORDS: SQLITE_OK {error code} {error codes}
       
   876 ** KEYWORDS: {result code} {result codes}
       
   877 **
       
   878 ** Many SQLite functions return an integer result code from the set shown
       
   879 ** here in order to indicates success or failure.
       
   880 **
       
   881 ** New error codes may be added in future versions of SQLite.
       
   882 **
       
   883 ** See also: [SQLITE_IOERR_READ | extended result codes]
       
   884 */
       
   885 #define SQLITE_OK           0   /* Successful result */
       
   886 /* beginning-of-error-codes */
       
   887 #define SQLITE_ERROR        1   /* SQL error or missing database */
       
   888 #define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
       
   889 #define SQLITE_PERM         3   /* Access permission denied */
       
   890 #define SQLITE_ABORT        4   /* Callback routine requested an abort */
       
   891 #define SQLITE_BUSY         5   /* The database file is locked */
       
   892 #define SQLITE_LOCKED       6   /* A table in the database is locked */
       
   893 #define SQLITE_NOMEM        7   /* A malloc() failed */
       
   894 #define SQLITE_READONLY     8   /* Attempt to write a readonly database */
       
   895 #define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
       
   896 #define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
       
   897 #define SQLITE_CORRUPT     11   /* The database disk image is malformed */
       
   898 #define SQLITE_NOTFOUND    12   /* NOT USED. Table or record not found */
       
   899 #define SQLITE_FULL        13   /* Insertion failed because database is full */
       
   900 #define SQLITE_CANTOPEN    14   /* Unable to open the database file */
       
   901 #define SQLITE_PROTOCOL    15   /* NOT USED. Database lock protocol error */
       
   902 #define SQLITE_EMPTY       16   /* Database is empty */
       
   903 #define SQLITE_SCHEMA      17   /* The database schema changed */
       
   904 #define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
       
   905 #define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
       
   906 #define SQLITE_MISMATCH    20   /* Data type mismatch */
       
   907 #define SQLITE_MISUSE      21   /* Library used incorrectly */
       
   908 #define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
       
   909 #define SQLITE_AUTH        23   /* Authorization denied */
       
   910 #define SQLITE_FORMAT      24   /* Auxiliary database format error */
       
   911 #define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
       
   912 #define SQLITE_NOTADB      26   /* File opened that is not a database file */
       
   913 #define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
       
   914 #define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
       
   915 /* end-of-error-codes */
       
   916 
       
   917 /*
       
   918 ** CAPI3REF: Extended Result Codes {H10220} <S10700>
       
   919 ** KEYWORDS: {extended error code} {extended error codes}
       
   920 ** KEYWORDS: {extended result code} {extended result codes}
       
   921 **
       
   922 ** In its default configuration, SQLite API routines return one of 26 integer
       
   923 ** [SQLITE_OK | result codes].  However, experience has shown that many of
       
   924 ** these result codes are too coarse-grained.  They do not provide as
       
   925 ** much information about problems as programmers might like.  In an effort to
       
   926 ** address this, newer versions of SQLite (version 3.3.8 and later) include
       
   927 ** support for additional result codes that provide more detailed information
       
   928 ** about errors. The extended result codes are enabled or disabled
       
   929 ** on a per database connection basis using the
       
   930 ** [sqlite3_extended_result_codes()] API.
       
   931 **
       
   932 ** Some of the available extended result codes are listed here.
       
   933 ** One may expect the number of extended result codes will be expand
       
   934 ** over time.  Software that uses extended result codes should expect
       
   935 ** to see new result codes in future releases of SQLite.
       
   936 **
       
   937 ** The SQLITE_OK result code will never be extended.  It will always
       
   938 ** be exactly zero.
       
   939 */
       
   940 #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
       
   941 #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
       
   942 #define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
       
   943 #define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
       
   944 #define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
       
   945 #define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
       
   946 #define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
       
   947 #define SQLITE_IOERR_UNLOCK            (SQLITE_IOERR | (8<<8))
       
   948 #define SQLITE_IOERR_RDLOCK            (SQLITE_IOERR | (9<<8))
       
   949 #define SQLITE_IOERR_DELETE            (SQLITE_IOERR | (10<<8))
       
   950 #define SQLITE_IOERR_BLOCKED           (SQLITE_IOERR | (11<<8))
       
   951 #define SQLITE_IOERR_NOMEM             (SQLITE_IOERR | (12<<8))
       
   952 #define SQLITE_IOERR_ACCESS            (SQLITE_IOERR | (13<<8))
       
   953 #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
       
   954 #define SQLITE_IOERR_LOCK              (SQLITE_IOERR | (15<<8))
       
   955 #define SQLITE_IOERR_CLOSE             (SQLITE_IOERR | (16<<8))
       
   956 #define SQLITE_IOERR_DIR_CLOSE         (SQLITE_IOERR | (17<<8))
       
   957 #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED | (1<<8) )
       
   958 
       
   959 /*
       
   960 ** CAPI3REF: Flags For File Open Operations {H10230} <H11120> <H12700>
       
   961 **
       
   962 ** These bit values are intended for use in the
       
   963 ** 3rd parameter to the [sqlite3_open_v2()] interface and
       
   964 ** in the 4th parameter to the xOpen method of the
       
   965 ** [sqlite3_vfs] object.
       
   966 */
       
   967 #define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
       
   968 #define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
       
   969 #define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
       
   970 #define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
       
   971 #define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
       
   972 #define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
       
   973 #define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
       
   974 #define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
       
   975 #define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
       
   976 #define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
       
   977 #define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
       
   978 #define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
       
   979 #define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
       
   980 #define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
       
   981 #define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
       
   982 #define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
       
   983 
       
   984 /*
       
   985 ** CAPI3REF: Device Characteristics {H10240} <H11120>
       
   986 **
       
   987 ** The xDeviceCapabilities method of the [sqlite3_io_methods]
       
   988 ** object returns an integer which is a vector of the these
       
   989 ** bit values expressing I/O characteristics of the mass storage
       
   990 ** device that holds the file that the [sqlite3_io_methods]
       
   991 ** refers to.
       
   992 **
       
   993 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
       
   994 ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
       
   995 ** mean that writes of blocks that are nnn bytes in size and
       
   996 ** are aligned to an address which is an integer multiple of
       
   997 ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
       
   998 ** that when data is appended to a file, the data is appended
       
   999 ** first then the size of the file is extended, never the other
       
  1000 ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
       
  1001 ** information is written to disk in the same order as calls
       
  1002 ** to xWrite().
       
  1003 */
       
  1004 #define SQLITE_IOCAP_ATOMIC          0x00000001
       
  1005 #define SQLITE_IOCAP_ATOMIC512       0x00000002
       
  1006 #define SQLITE_IOCAP_ATOMIC1K        0x00000004
       
  1007 #define SQLITE_IOCAP_ATOMIC2K        0x00000008
       
  1008 #define SQLITE_IOCAP_ATOMIC4K        0x00000010
       
  1009 #define SQLITE_IOCAP_ATOMIC8K        0x00000020
       
  1010 #define SQLITE_IOCAP_ATOMIC16K       0x00000040
       
  1011 #define SQLITE_IOCAP_ATOMIC32K       0x00000080
       
  1012 #define SQLITE_IOCAP_ATOMIC64K       0x00000100
       
  1013 #define SQLITE_IOCAP_SAFE_APPEND     0x00000200
       
  1014 #define SQLITE_IOCAP_SEQUENTIAL      0x00000400
       
  1015 
       
  1016 /*
       
  1017 ** CAPI3REF: File Locking Levels {H10250} <H11120> <H11310>
       
  1018 **
       
  1019 ** SQLite uses one of these integer values as the second
       
  1020 ** argument to calls it makes to the xLock() and xUnlock() methods
       
  1021 ** of an [sqlite3_io_methods] object.
       
  1022 */
       
  1023 #define SQLITE_LOCK_NONE          0
       
  1024 #define SQLITE_LOCK_SHARED        1
       
  1025 #define SQLITE_LOCK_RESERVED      2
       
  1026 #define SQLITE_LOCK_PENDING       3
       
  1027 #define SQLITE_LOCK_EXCLUSIVE     4
       
  1028 
       
  1029 /*
       
  1030 ** CAPI3REF: Synchronization Type Flags {H10260} <H11120>
       
  1031 **
       
  1032 ** When SQLite invokes the xSync() method of an
       
  1033 ** [sqlite3_io_methods] object it uses a combination of
       
  1034 ** these integer values as the second argument.
       
  1035 **
       
  1036 ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
       
  1037 ** sync operation only needs to flush data to mass storage.  Inode
       
  1038 ** information need not be flushed. If the lower four bits of the flag
       
  1039 ** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
       
  1040 ** If the lower four bits equal SQLITE_SYNC_FULL, that means
       
  1041 ** to use Mac OS X style fullsync instead of fsync().
       
  1042 */
       
  1043 #define SQLITE_SYNC_NORMAL        0x00002
       
  1044 #define SQLITE_SYNC_FULL          0x00003
       
  1045 #define SQLITE_SYNC_DATAONLY      0x00010
       
  1046 
       
  1047 /*
       
  1048 ** CAPI3REF: OS Interface Open File Handle {H11110} <S20110>
       
  1049 **
       
  1050 ** An [sqlite3_file] object represents an open file in the 
       
  1051 ** [sqlite3_vfs | OS interface layer].  Individual OS interface
       
  1052 ** implementations will
       
  1053 ** want to subclass this object by appending additional fields
       
  1054 ** for their own use.  The pMethods entry is a pointer to an
       
  1055 ** [sqlite3_io_methods] object that defines methods for performing
       
  1056 ** I/O operations on the open file.
       
  1057 */
       
  1058 typedef struct sqlite3_file sqlite3_file;
       
  1059 struct sqlite3_file {
       
  1060   const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
       
  1061 };
       
  1062 
       
  1063 /*
       
  1064 ** CAPI3REF: OS Interface File Virtual Methods Object {H11120} <S20110>
       
  1065 **
       
  1066 ** Every file opened by the [sqlite3_vfs] xOpen method populates an
       
  1067 ** [sqlite3_file] object (or, more commonly, a subclass of the
       
  1068 ** [sqlite3_file] object) with a pointer to an instance of this object.
       
  1069 ** This object defines the methods used to perform various operations
       
  1070 ** against the open file represented by the [sqlite3_file] object.
       
  1071 **
       
  1072 ** If the xOpen method sets the sqlite3_file.pMethods element 
       
  1073 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
       
  1074 ** may be invoked even if the xOpen reported that it failed.  The
       
  1075 ** only way to prevent a call to xClose following a failed xOpen
       
  1076 ** is for the xOpen to set the sqlite3_file.pMethods element to NULL.
       
  1077 **
       
  1078 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
       
  1079 ** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
       
  1080 ** The second choice is a Mac OS X style fullsync.  The [SQLITE_SYNC_DATAONLY]
       
  1081 ** flag may be ORed in to indicate that only the data of the file
       
  1082 ** and not its inode needs to be synced.
       
  1083 **
       
  1084 ** The integer values to xLock() and xUnlock() are one of
       
  1085 ** <ul>
       
  1086 ** <li> [SQLITE_LOCK_NONE],
       
  1087 ** <li> [SQLITE_LOCK_SHARED],
       
  1088 ** <li> [SQLITE_LOCK_RESERVED],
       
  1089 ** <li> [SQLITE_LOCK_PENDING], or
       
  1090 ** <li> [SQLITE_LOCK_EXCLUSIVE].
       
  1091 ** </ul>
       
  1092 ** xLock() increases the lock. xUnlock() decreases the lock.
       
  1093 ** The xCheckReservedLock() method checks whether any database connection,
       
  1094 ** either in this process or in some other process, is holding a RESERVED,
       
  1095 ** PENDING, or EXCLUSIVE lock on the file.  It returns true
       
  1096 ** if such a lock exists and false otherwise.
       
  1097 **
       
  1098 ** The xFileControl() method is a generic interface that allows custom
       
  1099 ** VFS implementations to directly control an open file using the
       
  1100 ** [sqlite3_file_control()] interface.  The second "op" argument is an
       
  1101 ** integer opcode.  The third argument is a generic pointer intended to
       
  1102 ** point to a structure that may contain arguments or space in which to
       
  1103 ** write return values.  Potential uses for xFileControl() might be
       
  1104 ** functions to enable blocking locks with timeouts, to change the
       
  1105 ** locking strategy (for example to use dot-file locks), to inquire
       
  1106 ** about the status of a lock, or to break stale locks.  The SQLite
       
  1107 ** core reserves all opcodes less than 100 for its own use.
       
  1108 ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
       
  1109 ** Applications that define a custom xFileControl method should use opcodes
       
  1110 ** greater than 100 to avoid conflicts.
       
  1111 **
       
  1112 ** The xSectorSize() method returns the sector size of the
       
  1113 ** device that underlies the file.  The sector size is the
       
  1114 ** minimum write that can be performed without disturbing
       
  1115 ** other bytes in the file.  The xDeviceCharacteristics()
       
  1116 ** method returns a bit vector describing behaviors of the
       
  1117 ** underlying device:
       
  1118 **
       
  1119 ** <ul>
       
  1120 ** <li> [SQLITE_IOCAP_ATOMIC]
       
  1121 ** <li> [SQLITE_IOCAP_ATOMIC512]
       
  1122 ** <li> [SQLITE_IOCAP_ATOMIC1K]
       
  1123 ** <li> [SQLITE_IOCAP_ATOMIC2K]
       
  1124 ** <li> [SQLITE_IOCAP_ATOMIC4K]
       
  1125 ** <li> [SQLITE_IOCAP_ATOMIC8K]
       
  1126 ** <li> [SQLITE_IOCAP_ATOMIC16K]
       
  1127 ** <li> [SQLITE_IOCAP_ATOMIC32K]
       
  1128 ** <li> [SQLITE_IOCAP_ATOMIC64K]
       
  1129 ** <li> [SQLITE_IOCAP_SAFE_APPEND]
       
  1130 ** <li> [SQLITE_IOCAP_SEQUENTIAL]
       
  1131 ** </ul>
       
  1132 **
       
  1133 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
       
  1134 ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
       
  1135 ** mean that writes of blocks that are nnn bytes in size and
       
  1136 ** are aligned to an address which is an integer multiple of
       
  1137 ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
       
  1138 ** that when data is appended to a file, the data is appended
       
  1139 ** first then the size of the file is extended, never the other
       
  1140 ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
       
  1141 ** information is written to disk in the same order as calls
       
  1142 ** to xWrite().
       
  1143 **
       
  1144 ** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
       
  1145 ** in the unread portions of the buffer with zeros.  A VFS that
       
  1146 ** fails to zero-fill short reads might seem to work.  However,
       
  1147 ** failure to zero-fill short reads will eventually lead to
       
  1148 ** database corruption.
       
  1149 */
       
  1150 typedef struct sqlite3_io_methods sqlite3_io_methods;
       
  1151 struct sqlite3_io_methods {
       
  1152   int iVersion;
       
  1153   int (*xClose)(sqlite3_file*);
       
  1154   int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
       
  1155   int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
       
  1156   int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
       
  1157   int (*xSync)(sqlite3_file*, int flags);
       
  1158   int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
       
  1159   int (*xLock)(sqlite3_file*, int);
       
  1160   int (*xUnlock)(sqlite3_file*, int);
       
  1161   int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
       
  1162   int (*xFileControl)(sqlite3_file*, int op, void *pArg);
       
  1163   int (*xSectorSize)(sqlite3_file*);
       
  1164   int (*xDeviceCharacteristics)(sqlite3_file*);
       
  1165   /* Additional methods may be added in future releases */
       
  1166 };
       
  1167 
       
  1168 /*
       
  1169 ** CAPI3REF: Standard File Control Opcodes {H11310} <S30800>
       
  1170 **
       
  1171 ** These integer constants are opcodes for the xFileControl method
       
  1172 ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
       
  1173 ** interface.
       
  1174 **
       
  1175 ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
       
  1176 ** opcode causes the xFileControl method to write the current state of
       
  1177 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
       
  1178 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
       
  1179 ** into an integer that the pArg argument points to. This capability
       
  1180 ** is used during testing and only needs to be supported when SQLITE_TEST
       
  1181 ** is defined.
       
  1182 */
       
  1183 #define SQLITE_FCNTL_LOCKSTATE        1
       
  1184 #define SQLITE_GET_LOCKPROXYFILE      2
       
  1185 #define SQLITE_SET_LOCKPROXYFILE      3
       
  1186 #define SQLITE_LAST_ERRNO             4
       
  1187 
       
  1188 /*
       
  1189 ** CAPI3REF: Mutex Handle {H17110} <S20130>
       
  1190 **
       
  1191 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
       
  1192 ** abstract type for a mutex object.  The SQLite core never looks
       
  1193 ** at the internal representation of an [sqlite3_mutex].  It only
       
  1194 ** deals with pointers to the [sqlite3_mutex] object.
       
  1195 **
       
  1196 ** Mutexes are created using [sqlite3_mutex_alloc()].
       
  1197 */
       
  1198 typedef struct sqlite3_mutex sqlite3_mutex;
       
  1199 
       
  1200 /*
       
  1201 ** CAPI3REF: OS Interface Object {H11140} <S20100>
       
  1202 **
       
  1203 ** An instance of the sqlite3_vfs object defines the interface between
       
  1204 ** the SQLite core and the underlying operating system.  The "vfs"
       
  1205 ** in the name of the object stands for "virtual file system".
       
  1206 **
       
  1207 ** The value of the iVersion field is initially 1 but may be larger in
       
  1208 ** future versions of SQLite.  Additional fields may be appended to this
       
  1209 ** object when the iVersion value is increased.  Note that the structure
       
  1210 ** of the sqlite3_vfs object changes in the transaction between
       
  1211 ** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
       
  1212 ** modified.
       
  1213 **
       
  1214 ** The szOsFile field is the size of the subclassed [sqlite3_file]
       
  1215 ** structure used by this VFS.  mxPathname is the maximum length of
       
  1216 ** a pathname in this VFS.
       
  1217 **
       
  1218 ** Registered sqlite3_vfs objects are kept on a linked list formed by
       
  1219 ** the pNext pointer.  The [sqlite3_vfs_register()]
       
  1220 ** and [sqlite3_vfs_unregister()] interfaces manage this list
       
  1221 ** in a thread-safe way.  The [sqlite3_vfs_find()] interface
       
  1222 ** searches the list.  Neither the application code nor the VFS
       
  1223 ** implementation should use the pNext pointer.
       
  1224 **
       
  1225 ** The pNext field is the only field in the sqlite3_vfs
       
  1226 ** structure that SQLite will ever modify.  SQLite will only access
       
  1227 ** or modify this field while holding a particular static mutex.
       
  1228 ** The application should never modify anything within the sqlite3_vfs
       
  1229 ** object once the object has been registered.
       
  1230 **
       
  1231 ** The zName field holds the name of the VFS module.  The name must
       
  1232 ** be unique across all VFS modules.
       
  1233 **
       
  1234 ** SQLite will guarantee that the zFilename parameter to xOpen
       
  1235 ** is either a NULL pointer or string obtained
       
  1236 ** from xFullPathname().  SQLite further guarantees that
       
  1237 ** the string will be valid and unchanged until xClose() is
       
  1238 ** called. Because of the previous sentence,
       
  1239 ** the [sqlite3_file] can safely store a pointer to the
       
  1240 ** filename if it needs to remember the filename for some reason.
       
  1241 ** If the zFilename parameter is xOpen is a NULL pointer then xOpen
       
  1242 ** must invent its own temporary name for the file.  Whenever the 
       
  1243 ** xFilename parameter is NULL it will also be the case that the
       
  1244 ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
       
  1245 **
       
  1246 ** The flags argument to xOpen() includes all bits set in
       
  1247 ** the flags argument to [sqlite3_open_v2()].  Or if [sqlite3_open()]
       
  1248 ** or [sqlite3_open16()] is used, then flags includes at least
       
  1249 ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. 
       
  1250 ** If xOpen() opens a file read-only then it sets *pOutFlags to
       
  1251 ** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be set.
       
  1252 **
       
  1253 ** SQLite will also add one of the following flags to the xOpen()
       
  1254 ** call, depending on the object being opened:
       
  1255 **
       
  1256 ** <ul>
       
  1257 ** <li>  [SQLITE_OPEN_MAIN_DB]
       
  1258 ** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
       
  1259 ** <li>  [SQLITE_OPEN_TEMP_DB]
       
  1260 ** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
       
  1261 ** <li>  [SQLITE_OPEN_TRANSIENT_DB]
       
  1262 ** <li>  [SQLITE_OPEN_SUBJOURNAL]
       
  1263 ** <li>  [SQLITE_OPEN_MASTER_JOURNAL]
       
  1264 ** </ul>
       
  1265 **
       
  1266 ** The file I/O implementation can use the object type flags to
       
  1267 ** change the way it deals with files.  For example, an application
       
  1268 ** that does not care about crash recovery or rollback might make
       
  1269 ** the open of a journal file a no-op.  Writes to this journal would
       
  1270 ** also be no-ops, and any attempt to read the journal would return
       
  1271 ** SQLITE_IOERR.  Or the implementation might recognize that a database
       
  1272 ** file will be doing page-aligned sector reads and writes in a random
       
  1273 ** order and set up its I/O subsystem accordingly.
       
  1274 **
       
  1275 ** SQLite might also add one of the following flags to the xOpen method:
       
  1276 **
       
  1277 ** <ul>
       
  1278 ** <li> [SQLITE_OPEN_DELETEONCLOSE]
       
  1279 ** <li> [SQLITE_OPEN_EXCLUSIVE]
       
  1280 ** </ul>
       
  1281 **
       
  1282 ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
       
  1283 ** deleted when it is closed.  The [SQLITE_OPEN_DELETEONCLOSE]
       
  1284 ** will be set for TEMP  databases, journals and for subjournals.
       
  1285 **
       
  1286 ** The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
       
  1287 ** with the [SQLITE_OPEN_CREATE] flag, which are both directly
       
  1288 ** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
       
  1289 ** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the 
       
  1290 ** SQLITE_OPEN_CREATE, is used to indicate that file should always
       
  1291 ** be created, and that it is an error if it already exists.
       
  1292 ** It is <i>not</i> used to indicate the file should be opened 
       
  1293 ** for exclusive access.
       
  1294 **
       
  1295 ** At least szOsFile bytes of memory are allocated by SQLite
       
  1296 ** to hold the  [sqlite3_file] structure passed as the third
       
  1297 ** argument to xOpen.  The xOpen method does not have to
       
  1298 ** allocate the structure; it should just fill it in.  Note that
       
  1299 ** the xOpen method must set the sqlite3_file.pMethods to either
       
  1300 ** a valid [sqlite3_io_methods] object or to NULL.  xOpen must do
       
  1301 ** this even if the open fails.  SQLite expects that the sqlite3_file.pMethods
       
  1302 ** element will be valid after xOpen returns regardless of the success
       
  1303 ** or failure of the xOpen call.
       
  1304 **
       
  1305 ** The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
       
  1306 ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
       
  1307 ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
       
  1308 ** to test whether a file is at least readable.   The file can be a
       
  1309 ** directory.
       
  1310 **
       
  1311 ** SQLite will always allocate at least mxPathname+1 bytes for the
       
  1312 ** output buffer xFullPathname.  The exact size of the output buffer
       
  1313 ** is also passed as a parameter to both  methods. If the output buffer
       
  1314 ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
       
  1315 ** handled as a fatal error by SQLite, vfs implementations should endeavor
       
  1316 ** to prevent this by setting mxPathname to a sufficiently large value.
       
  1317 **
       
  1318 ** The xRandomness(), xSleep(), and xCurrentTime() interfaces
       
  1319 ** are not strictly a part of the filesystem, but they are
       
  1320 ** included in the VFS structure for completeness.
       
  1321 ** The xRandomness() function attempts to return nBytes bytes
       
  1322 ** of good-quality randomness into zOut.  The return value is
       
  1323 ** the actual number of bytes of randomness obtained.
       
  1324 ** The xSleep() method causes the calling thread to sleep for at
       
  1325 ** least the number of microseconds given.  The xCurrentTime()
       
  1326 ** method returns a Julian Day Number for the current date and time.
       
  1327 **
       
  1328 */
       
  1329 typedef struct sqlite3_vfs sqlite3_vfs;
       
  1330 struct sqlite3_vfs {
       
  1331   int iVersion;            /* Structure version number */
       
  1332   int szOsFile;            /* Size of subclassed sqlite3_file */
       
  1333   int mxPathname;          /* Maximum file pathname length */
       
  1334   sqlite3_vfs *pNext;      /* Next registered VFS */
       
  1335   const char *zName;       /* Name of this virtual file system */
       
  1336   void *pAppData;          /* Pointer to application-specific data */
       
  1337   int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
       
  1338                int flags, int *pOutFlags);
       
  1339   int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
       
  1340   int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
       
  1341   int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
       
  1342   void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
       
  1343   void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
       
  1344   void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
       
  1345   void (*xDlClose)(sqlite3_vfs*, void*);
       
  1346   int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
       
  1347   int (*xSleep)(sqlite3_vfs*, int microseconds);
       
  1348   int (*xCurrentTime)(sqlite3_vfs*, double*);
       
  1349   int (*xGetLastError)(sqlite3_vfs*, int, char *);
       
  1350   /* New fields may be appended in figure versions.  The iVersion
       
  1351   ** value will increment whenever this happens. */
       
  1352 };
       
  1353 
       
  1354 /*
       
  1355 ** CAPI3REF: Flags for the xAccess VFS method {H11190} <H11140>
       
  1356 **
       
  1357 ** These integer constants can be used as the third parameter to
       
  1358 ** the xAccess method of an [sqlite3_vfs] object. {END}  They determine
       
  1359 ** what kind of permissions the xAccess method is looking for.
       
  1360 ** With SQLITE_ACCESS_EXISTS, the xAccess method
       
  1361 ** simply checks whether the file exists.
       
  1362 ** With SQLITE_ACCESS_READWRITE, the xAccess method
       
  1363 ** checks whether the file is both readable and writable.
       
  1364 ** With SQLITE_ACCESS_READ, the xAccess method
       
  1365 ** checks whether the file is readable.
       
  1366 */
       
  1367 #define SQLITE_ACCESS_EXISTS    0
       
  1368 #define SQLITE_ACCESS_READWRITE 1
       
  1369 #define SQLITE_ACCESS_READ      2
       
  1370 
       
  1371 /*
       
  1372 ** CAPI3REF: Initialize The SQLite Library {H10130} <S20000><S30100>
       
  1373 **
       
  1374 ** The sqlite3_initialize() routine initializes the
       
  1375 ** SQLite library.  The sqlite3_shutdown() routine
       
  1376 ** deallocates any resources that were allocated by sqlite3_initialize().
       
  1377 **
       
  1378 ** A call to sqlite3_initialize() is an "effective" call if it is
       
  1379 ** the first time sqlite3_initialize() is invoked during the lifetime of
       
  1380 ** the process, or if it is the first time sqlite3_initialize() is invoked
       
  1381 ** following a call to sqlite3_shutdown().  Only an effective call
       
  1382 ** of sqlite3_initialize() does any initialization.  All other calls
       
  1383 ** are harmless no-ops.
       
  1384 **
       
  1385 ** A call to sqlite3_shutdown() is an "effective" call if it is the first
       
  1386 ** call to sqlite3_shutdown() since the last sqlite3_initialize().  Only
       
  1387 ** an effective call to sqlite3_shutdown() does any deinitialization.
       
  1388 ** All other calls to sqlite3_shutdown() are harmless no-ops.
       
  1389 **
       
  1390 ** Among other things, sqlite3_initialize() shall invoke
       
  1391 ** sqlite3_os_init().  Similarly, sqlite3_shutdown()
       
  1392 ** shall invoke sqlite3_os_end().
       
  1393 **
       
  1394 ** The sqlite3_initialize() routine returns [SQLITE_OK] on success.
       
  1395 ** If for some reason, sqlite3_initialize() is unable to initialize
       
  1396 ** the library (perhaps it is unable to allocate a needed resource such
       
  1397 ** as a mutex) it returns an [error code] other than [SQLITE_OK].
       
  1398 **
       
  1399 ** The sqlite3_initialize() routine is called internally by many other
       
  1400 ** SQLite interfaces so that an application usually does not need to
       
  1401 ** invoke sqlite3_initialize() directly.  For example, [sqlite3_open()]
       
  1402 ** calls sqlite3_initialize() so the SQLite library will be automatically
       
  1403 ** initialized when [sqlite3_open()] is called if it has not be initialized
       
  1404 ** already.  However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
       
  1405 ** compile-time option, then the automatic calls to sqlite3_initialize()
       
  1406 ** are omitted and the application must call sqlite3_initialize() directly
       
  1407 ** prior to using any other SQLite interface.  For maximum portability,
       
  1408 ** it is recommended that applications always invoke sqlite3_initialize()
       
  1409 ** directly prior to using any other SQLite interface.  Future releases
       
  1410 ** of SQLite may require this.  In other words, the behavior exhibited
       
  1411 ** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
       
  1412 ** default behavior in some future release of SQLite.
       
  1413 **
       
  1414 ** The sqlite3_os_init() routine does operating-system specific
       
  1415 ** initialization of the SQLite library.  The sqlite3_os_end()
       
  1416 ** routine undoes the effect of sqlite3_os_init().  Typical tasks
       
  1417 ** performed by these routines include allocation or deallocation
       
  1418 ** of static resources, initialization of global variables,
       
  1419 ** setting up a default [sqlite3_vfs] module, or setting up
       
  1420 ** a default configuration using [sqlite3_config()].
       
  1421 **
       
  1422 ** The application should never invoke either sqlite3_os_init()
       
  1423 ** or sqlite3_os_end() directly.  The application should only invoke
       
  1424 ** sqlite3_initialize() and sqlite3_shutdown().  The sqlite3_os_init()
       
  1425 ** interface is called automatically by sqlite3_initialize() and
       
  1426 ** sqlite3_os_end() is called by sqlite3_shutdown().  Appropriate
       
  1427 ** implementations for sqlite3_os_init() and sqlite3_os_end()
       
  1428 ** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
       
  1429 ** When [custom builds | built for other platforms]
       
  1430 ** (using the [SQLITE_OS_OTHER=1] compile-time
       
  1431 ** option) the application must supply a suitable implementation for
       
  1432 ** sqlite3_os_init() and sqlite3_os_end().  An application-supplied
       
  1433 ** implementation of sqlite3_os_init() or sqlite3_os_end()
       
  1434 ** must return [SQLITE_OK] on success and some other [error code] upon
       
  1435 ** failure.
       
  1436 */
       
  1437 SQLITE_API int sqlite3_initialize(void);
       
  1438 SQLITE_API int sqlite3_shutdown(void);
       
  1439 SQLITE_API int sqlite3_os_init(void);
       
  1440 SQLITE_API int sqlite3_os_end(void);
       
  1441 
       
  1442 /*
       
  1443 ** CAPI3REF: Configuring The SQLite Library {H14100} <S20000><S30200>
       
  1444 ** EXPERIMENTAL
       
  1445 **
       
  1446 ** The sqlite3_config() interface is used to make global configuration
       
  1447 ** changes to SQLite in order to tune SQLite to the specific needs of
       
  1448 ** the application.  The default configuration is recommended for most
       
  1449 ** applications and so this routine is usually not necessary.  It is
       
  1450 ** provided to support rare applications with unusual needs.
       
  1451 **
       
  1452 ** The sqlite3_config() interface is not threadsafe.  The application
       
  1453 ** must insure that no other SQLite interfaces are invoked by other
       
  1454 ** threads while sqlite3_config() is running.  Furthermore, sqlite3_config()
       
  1455 ** may only be invoked prior to library initialization using
       
  1456 ** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
       
  1457 ** Note, however, that sqlite3_config() can be called as part of the
       
  1458 ** implementation of an application-defined [sqlite3_os_init()].
       
  1459 **
       
  1460 ** The first argument to sqlite3_config() is an integer
       
  1461 ** [SQLITE_CONFIG_SINGLETHREAD | configuration option] that determines
       
  1462 ** what property of SQLite is to be configured.  Subsequent arguments
       
  1463 ** vary depending on the [SQLITE_CONFIG_SINGLETHREAD | configuration option]
       
  1464 ** in the first argument.
       
  1465 **
       
  1466 ** When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
       
  1467 ** If the option is unknown or SQLite is unable to set the option
       
  1468 ** then this routine returns a non-zero [error code].
       
  1469 **
       
  1470 ** Requirements:
       
  1471 ** [H14103] [H14106] [H14120] [H14123] [H14126] [H14129] [H14132] [H14135]
       
  1472 ** [H14138] [H14141] [H14144] [H14147] [H14150] [H14153] [H14156] [H14159]
       
  1473 ** [H14162] [H14165] [H14168]
       
  1474 */
       
  1475 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_config(int, ...);
       
  1476 
       
  1477 /*
       
  1478 ** CAPI3REF: Configure database connections  {H14200} <S20000>
       
  1479 ** EXPERIMENTAL
       
  1480 **
       
  1481 ** The sqlite3_db_config() interface is used to make configuration
       
  1482 ** changes to a [database connection].  The interface is similar to
       
  1483 ** [sqlite3_config()] except that the changes apply to a single
       
  1484 ** [database connection] (specified in the first argument).  The
       
  1485 ** sqlite3_db_config() interface can only be used immediately after
       
  1486 ** the database connection is created using [sqlite3_open()],
       
  1487 ** [sqlite3_open16()], or [sqlite3_open_v2()].  
       
  1488 **
       
  1489 ** The second argument to sqlite3_db_config(D,V,...)  is the
       
  1490 ** configuration verb - an integer code that indicates what
       
  1491 ** aspect of the [database connection] is being configured.
       
  1492 ** The only choice for this value is [SQLITE_DBCONFIG_LOOKASIDE].
       
  1493 ** New verbs are likely to be added in future releases of SQLite.
       
  1494 ** Additional arguments depend on the verb.
       
  1495 **
       
  1496 ** Requirements:
       
  1497 ** [H14203] [H14206] [H14209] [H14212] [H14215]
       
  1498 */
       
  1499 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_db_config(sqlite3*, int op, ...);
       
  1500 
       
  1501 /*
       
  1502 ** CAPI3REF: Memory Allocation Routines {H10155} <S20120>
       
  1503 ** EXPERIMENTAL
       
  1504 **
       
  1505 ** An instance of this object defines the interface between SQLite
       
  1506 ** and low-level memory allocation routines.
       
  1507 **
       
  1508 ** This object is used in only one place in the SQLite interface.
       
  1509 ** A pointer to an instance of this object is the argument to
       
  1510 ** [sqlite3_config()] when the configuration option is
       
  1511 ** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].  
       
  1512 ** By creating an instance of this object
       
  1513 ** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
       
  1514 ** during configuration, an application can specify an alternative
       
  1515 ** memory allocation subsystem for SQLite to use for all of its
       
  1516 ** dynamic memory needs.
       
  1517 **
       
  1518 ** Note that SQLite comes with several [built-in memory allocators]
       
  1519 ** that are perfectly adequate for the overwhelming majority of applications
       
  1520 ** and that this object is only useful to a tiny minority of applications
       
  1521 ** with specialized memory allocation requirements.  This object is
       
  1522 ** also used during testing of SQLite in order to specify an alternative
       
  1523 ** memory allocator that simulates memory out-of-memory conditions in
       
  1524 ** order to verify that SQLite recovers gracefully from such
       
  1525 ** conditions.
       
  1526 **
       
  1527 ** The xMalloc and xFree methods must work like the
       
  1528 ** malloc() and free() functions from the standard C library.
       
  1529 ** The xRealloc method must work like realloc() from the standard C library
       
  1530 ** with the exception that if the second argument to xRealloc is zero,
       
  1531 ** xRealloc must be a no-op - it must not perform any allocation or
       
  1532 ** deallocation.  SQLite guaranteeds that the second argument to
       
  1533 ** xRealloc is always a value returned by a prior call to xRoundup.
       
  1534 ** And so in cases where xRoundup always returns a positive number,
       
  1535 ** xRealloc can perform exactly as the standard library realloc() and
       
  1536 ** still be in compliance with this specification.
       
  1537 **
       
  1538 ** xSize should return the allocated size of a memory allocation
       
  1539 ** previously obtained from xMalloc or xRealloc.  The allocated size
       
  1540 ** is always at least as big as the requested size but may be larger.
       
  1541 **
       
  1542 ** The xRoundup method returns what would be the allocated size of
       
  1543 ** a memory allocation given a particular requested size.  Most memory
       
  1544 ** allocators round up memory allocations at least to the next multiple
       
  1545 ** of 8.  Some allocators round up to a larger multiple or to a power of 2.
       
  1546 ** Every memory allocation request coming in through [sqlite3_malloc()]
       
  1547 ** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0, 
       
  1548 ** that causes the corresponding memory allocation to fail.
       
  1549 **
       
  1550 ** The xInit method initializes the memory allocator.  (For example,
       
  1551 ** it might allocate any require mutexes or initialize internal data
       
  1552 ** structures.  The xShutdown method is invoked (indirectly) by
       
  1553 ** [sqlite3_shutdown()] and should deallocate any resources acquired
       
  1554 ** by xInit.  The pAppData pointer is used as the only parameter to
       
  1555 ** xInit and xShutdown.
       
  1556 **
       
  1557 ** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
       
  1558 ** the xInit method, so the xInit method need not be threadsafe.  The
       
  1559 ** xShutdown method is only called from [sqlite3_shutdown()] so it does
       
  1560 ** not need to be threadsafe either.  For all other methods, SQLite
       
  1561 ** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
       
  1562 ** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
       
  1563 ** it is by default) and so the methods are automatically serialized.
       
  1564 ** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
       
  1565 ** methods must be threadsafe or else make their own arrangements for
       
  1566 ** serialization.
       
  1567 **
       
  1568 ** SQLite will never invoke xInit() more than once without an intervening
       
  1569 ** call to xShutdown().
       
  1570 */
       
  1571 typedef struct sqlite3_mem_methods sqlite3_mem_methods;
       
  1572 struct sqlite3_mem_methods {
       
  1573   void *(*xMalloc)(int);         /* Memory allocation function */
       
  1574   void (*xFree)(void*);          /* Free a prior allocation */
       
  1575   void *(*xRealloc)(void*,int);  /* Resize an allocation */
       
  1576   int (*xSize)(void*);           /* Return the size of an allocation */
       
  1577   int (*xRoundup)(int);          /* Round up request size to allocation size */
       
  1578   int (*xInit)(void*);           /* Initialize the memory allocator */
       
  1579   void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
       
  1580   void *pAppData;                /* Argument to xInit() and xShutdown() */
       
  1581 };
       
  1582 
       
  1583 /*
       
  1584 ** CAPI3REF: Configuration Options {H10160} <S20000>
       
  1585 ** EXPERIMENTAL
       
  1586 **
       
  1587 ** These constants are the available integer configuration options that
       
  1588 ** can be passed as the first argument to the [sqlite3_config()] interface.
       
  1589 **
       
  1590 ** New configuration options may be added in future releases of SQLite.
       
  1591 ** Existing configuration options might be discontinued.  Applications
       
  1592 ** should check the return code from [sqlite3_config()] to make sure that
       
  1593 ** the call worked.  The [sqlite3_config()] interface will return a
       
  1594 ** non-zero [error code] if a discontinued or unsupported configuration option
       
  1595 ** is invoked.
       
  1596 **
       
  1597 ** <dl>
       
  1598 ** <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
       
  1599 ** <dd>There are no arguments to this option.  This option disables
       
  1600 ** all mutexing and puts SQLite into a mode where it can only be used
       
  1601 ** by a single thread.</dd>
       
  1602 **
       
  1603 ** <dt>SQLITE_CONFIG_MULTITHREAD</dt>
       
  1604 ** <dd>There are no arguments to this option.  This option disables
       
  1605 ** mutexing on [database connection] and [prepared statement] objects.
       
  1606 ** The application is responsible for serializing access to
       
  1607 ** [database connections] and [prepared statements].  But other mutexes
       
  1608 ** are enabled so that SQLite will be safe to use in a multi-threaded
       
  1609 ** environment as long as no two threads attempt to use the same
       
  1610 ** [database connection] at the same time.  See the [threading mode]
       
  1611 ** documentation for additional information.</dd>
       
  1612 **
       
  1613 ** <dt>SQLITE_CONFIG_SERIALIZED</dt>
       
  1614 ** <dd>There are no arguments to this option.  This option enables
       
  1615 ** all mutexes including the recursive
       
  1616 ** mutexes on [database connection] and [prepared statement] objects.
       
  1617 ** In this mode (which is the default when SQLite is compiled with
       
  1618 ** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
       
  1619 ** to [database connections] and [prepared statements] so that the
       
  1620 ** application is free to use the same [database connection] or the
       
  1621 ** same [prepared statement] in different threads at the same time.
       
  1622 ** See the [threading mode] documentation for additional information.</dd>
       
  1623 **
       
  1624 ** <dt>SQLITE_CONFIG_MALLOC</dt>
       
  1625 ** <dd>This option takes a single argument which is a pointer to an
       
  1626 ** instance of the [sqlite3_mem_methods] structure.  The argument specifies
       
  1627 ** alternative low-level memory allocation routines to be used in place of
       
  1628 ** the memory allocation routines built into SQLite.</dd>
       
  1629 **
       
  1630 ** <dt>SQLITE_CONFIG_GETMALLOC</dt>
       
  1631 ** <dd>This option takes a single argument which is a pointer to an
       
  1632 ** instance of the [sqlite3_mem_methods] structure.  The [sqlite3_mem_methods]
       
  1633 ** structure is filled with the currently defined memory allocation routines.
       
  1634 ** This option can be used to overload the default memory allocation
       
  1635 ** routines with a wrapper that simulations memory allocation failure or
       
  1636 ** tracks memory usage, for example.</dd>
       
  1637 **
       
  1638 ** <dt>SQLITE_CONFIG_MEMSTATUS</dt>
       
  1639 ** <dd>This option takes single argument of type int, interpreted as a 
       
  1640 ** boolean, which enables or disables the collection of memory allocation 
       
  1641 ** statistics. When disabled, the following SQLite interfaces become 
       
  1642 ** non-operational:
       
  1643 **   <ul>
       
  1644 **   <li> [sqlite3_memory_used()]
       
  1645 **   <li> [sqlite3_memory_highwater()]
       
  1646 **   <li> [sqlite3_soft_heap_limit()]
       
  1647 **   <li> [sqlite3_status()]
       
  1648 **   </ul>
       
  1649 ** </dd>
       
  1650 **
       
  1651 ** <dt>SQLITE_CONFIG_SCRATCH</dt>
       
  1652 ** <dd>This option specifies a static memory buffer that SQLite can use for
       
  1653 ** scratch memory.  There are three arguments:  A pointer an 8-byte
       
  1654 ** aligned memory buffer from which the scrach allocations will be
       
  1655 ** drawn, the size of each scratch allocation (sz),
       
  1656 ** and the maximum number of scratch allocations (N).  The sz
       
  1657 ** argument must be a multiple of 16. The sz parameter should be a few bytes
       
  1658 ** larger than the actual scratch space required due to internal overhead.
       
  1659 ** The first argument should pointer to an 8-byte aligned buffer
       
  1660 ** of at least sz*N bytes of memory.
       
  1661 ** SQLite will use no more than one scratch buffer at once per thread, so
       
  1662 ** N should be set to the expected maximum number of threads.  The sz
       
  1663 ** parameter should be 6 times the size of the largest database page size.
       
  1664 ** Scratch buffers are used as part of the btree balance operation.  If
       
  1665 ** The btree balancer needs additional memory beyond what is provided by
       
  1666 ** scratch buffers or if no scratch buffer space is specified, then SQLite
       
  1667 ** goes to [sqlite3_malloc()] to obtain the memory it needs.</dd>
       
  1668 **
       
  1669 ** <dt>SQLITE_CONFIG_PAGECACHE</dt>
       
  1670 ** <dd>This option specifies a static memory buffer that SQLite can use for
       
  1671 ** the database page cache with the default page cache implemenation.  
       
  1672 ** This configuration should not be used if an application-define page
       
  1673 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
       
  1674 ** There are three arguments to this option: A pointer to 8-byte aligned
       
  1675 ** memory, the size of each page buffer (sz), and the number of pages (N).
       
  1676 ** The sz argument should be the size of the largest database page
       
  1677 ** (a power of two between 512 and 32768) plus a little extra for each
       
  1678 ** page header.  The page header size is 20 to 40 bytes depending on
       
  1679 ** the host architecture.  It is harmless, apart from the wasted memory,
       
  1680 ** to make sz a little too large.  The first
       
  1681 ** argument should point to an allocation of at least sz*N bytes of memory.
       
  1682 ** SQLite will use the memory provided by the first argument to satisfy its
       
  1683 ** memory needs for the first N pages that it adds to cache.  If additional
       
  1684 ** page cache memory is needed beyond what is provided by this option, then
       
  1685 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
       
  1686 ** The implementation might use one or more of the N buffers to hold 
       
  1687 ** memory accounting information. The pointer in the first argument must
       
  1688 ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
       
  1689 ** will be undefined.</dd>
       
  1690 **
       
  1691 ** <dt>SQLITE_CONFIG_HEAP</dt>
       
  1692 ** <dd>This option specifies a static memory buffer that SQLite will use
       
  1693 ** for all of its dynamic memory allocation needs beyond those provided
       
  1694 ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
       
  1695 ** There are three arguments: An 8-byte aligned pointer to the memory,
       
  1696 ** the number of bytes in the memory buffer, and the minimum allocation size.
       
  1697 ** If the first pointer (the memory pointer) is NULL, then SQLite reverts
       
  1698 ** to using its default memory allocator (the system malloc() implementation),
       
  1699 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  If the
       
  1700 ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
       
  1701 ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
       
  1702 ** allocator is engaged to handle all of SQLites memory allocation needs.
       
  1703 ** The first pointer (the memory pointer) must be aligned to an 8-byte
       
  1704 ** boundary or subsequent behavior of SQLite will be undefined.</dd>
       
  1705 **
       
  1706 ** <dt>SQLITE_CONFIG_MUTEX</dt>
       
  1707 ** <dd>This option takes a single argument which is a pointer to an
       
  1708 ** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
       
  1709 ** alternative low-level mutex routines to be used in place
       
  1710 ** the mutex routines built into SQLite.</dd>
       
  1711 **
       
  1712 ** <dt>SQLITE_CONFIG_GETMUTEX</dt>
       
  1713 ** <dd>This option takes a single argument which is a pointer to an
       
  1714 ** instance of the [sqlite3_mutex_methods] structure.  The
       
  1715 ** [sqlite3_mutex_methods]
       
  1716 ** structure is filled with the currently defined mutex routines.
       
  1717 ** This option can be used to overload the default mutex allocation
       
  1718 ** routines with a wrapper used to track mutex usage for performance
       
  1719 ** profiling or testing, for example.</dd>
       
  1720 **
       
  1721 ** <dt>SQLITE_CONFIG_LOOKASIDE</dt>
       
  1722 ** <dd>This option takes two arguments that determine the default
       
  1723 ** memory allocation lookaside optimization.  The first argument is the
       
  1724 ** size of each lookaside buffer slot and the second is the number of
       
  1725 ** slots allocated to each database connection.  This option sets the
       
  1726 ** <i>default</i> lookaside size.  The [SQLITE_DBCONFIG_LOOKASIDE]
       
  1727 ** verb to [sqlite3_db_config()] can be used to change the lookaside
       
  1728 ** configuration on individual connections.</dd>
       
  1729 **
       
  1730 ** <dt>SQLITE_CONFIG_PCACHE</dt>
       
  1731 ** <dd>This option takes a single argument which is a pointer to
       
  1732 ** an [sqlite3_pcache_methods] object.  This object specifies the interface
       
  1733 ** to a custom page cache implementation.  SQLite makes a copy of the
       
  1734 ** object and uses it for page cache memory allocations.</dd>
       
  1735 **
       
  1736 ** <dt>SQLITE_CONFIG_GETPCACHE</dt>
       
  1737 ** <dd>This option takes a single argument which is a pointer to an
       
  1738 ** [sqlite3_pcache_methods] object.  SQLite copies of the current
       
  1739 ** page cache implementation into that object.</dd>
       
  1740 **
       
  1741 ** </dl>
       
  1742 */
       
  1743 #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
       
  1744 #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
       
  1745 #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
       
  1746 #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
       
  1747 #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
       
  1748 #define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
       
  1749 #define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
       
  1750 #define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
       
  1751 #define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
       
  1752 #define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
       
  1753 #define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
       
  1754 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ 
       
  1755 #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
       
  1756 #define SQLITE_CONFIG_PCACHE       14  /* sqlite3_pcache_methods* */
       
  1757 #define SQLITE_CONFIG_GETPCACHE    15  /* sqlite3_pcache_methods* */
       
  1758 
       
  1759 /*
       
  1760 ** CAPI3REF: Configuration Options {H10170} <S20000>
       
  1761 ** EXPERIMENTAL
       
  1762 **
       
  1763 ** These constants are the available integer configuration options that
       
  1764 ** can be passed as the second argument to the [sqlite3_db_config()] interface.
       
  1765 **
       
  1766 ** New configuration options may be added in future releases of SQLite.
       
  1767 ** Existing configuration options might be discontinued.  Applications
       
  1768 ** should check the return code from [sqlite3_db_config()] to make sure that
       
  1769 ** the call worked.  The [sqlite3_db_config()] interface will return a
       
  1770 ** non-zero [error code] if a discontinued or unsupported configuration option
       
  1771 ** is invoked.
       
  1772 **
       
  1773 ** <dl>
       
  1774 ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
       
  1775 ** <dd>This option takes three additional arguments that determine the 
       
  1776 ** [lookaside memory allocator] configuration for the [database connection].
       
  1777 ** The first argument (the third parameter to [sqlite3_db_config()] is a
       
  1778 ** pointer to an memory buffer to use for lookaside memory.
       
  1779 ** The first argument may be NULL in which case SQLite will allocate the
       
  1780 ** lookaside buffer itself using [sqlite3_malloc()].  The second argument is the
       
  1781 ** size of each lookaside buffer slot and the third argument is the number of
       
  1782 ** slots.  The size of the buffer in the first argument must be greater than
       
  1783 ** or equal to the product of the second and third arguments.  The buffer
       
  1784 ** must be aligned to an 8-byte boundary.  If the second argument is not
       
  1785 ** a multiple of 8, it is internally rounded down to the next smaller
       
  1786 ** multiple of 8.  See also: [SQLITE_CONFIG_LOOKASIDE]</dd>
       
  1787 **
       
  1788 ** </dl>
       
  1789 */
       
  1790 #define SQLITE_DBCONFIG_LOOKASIDE    1001  /* void* int int */
       
  1791 
       
  1792 
       
  1793 /*
       
  1794 ** CAPI3REF: Enable Or Disable Extended Result Codes {H12200} <S10700>
       
  1795 **
       
  1796 ** The sqlite3_extended_result_codes() routine enables or disables the
       
  1797 ** [extended result codes] feature of SQLite. The extended result
       
  1798 ** codes are disabled by default for historical compatibility considerations.
       
  1799 **
       
  1800 ** Requirements:
       
  1801 ** [H12201] [H12202]
       
  1802 */
       
  1803 SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
       
  1804 
       
  1805 /*
       
  1806 ** CAPI3REF: Last Insert Rowid {H12220} <S10700>
       
  1807 **
       
  1808 ** Each entry in an SQLite table has a unique 64-bit signed
       
  1809 ** integer key called the [ROWID | "rowid"]. The rowid is always available
       
  1810 ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
       
  1811 ** names are not also used by explicitly declared columns. If
       
  1812 ** the table has a column of type [INTEGER PRIMARY KEY] then that column
       
  1813 ** is another alias for the rowid.
       
  1814 **
       
  1815 ** This routine returns the [rowid] of the most recent
       
  1816 ** successful [INSERT] into the database from the [database connection]
       
  1817 ** in the first argument.  If no successful [INSERT]s
       
  1818 ** have ever occurred on that database connection, zero is returned.
       
  1819 **
       
  1820 ** If an [INSERT] occurs within a trigger, then the [rowid] of the inserted
       
  1821 ** row is returned by this routine as long as the trigger is running.
       
  1822 ** But once the trigger terminates, the value returned by this routine
       
  1823 ** reverts to the last value inserted before the trigger fired.
       
  1824 **
       
  1825 ** An [INSERT] that fails due to a constraint violation is not a
       
  1826 ** successful [INSERT] and does not change the value returned by this
       
  1827 ** routine.  Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
       
  1828 ** and INSERT OR ABORT make no changes to the return value of this
       
  1829 ** routine when their insertion fails.  When INSERT OR REPLACE
       
  1830 ** encounters a constraint violation, it does not fail.  The
       
  1831 ** INSERT continues to completion after deleting rows that caused
       
  1832 ** the constraint problem so INSERT OR REPLACE will always change
       
  1833 ** the return value of this interface.
       
  1834 **
       
  1835 ** For the purposes of this routine, an [INSERT] is considered to
       
  1836 ** be successful even if it is subsequently rolled back.
       
  1837 **
       
  1838 ** Requirements:
       
  1839 ** [H12221] [H12223]
       
  1840 **
       
  1841 ** If a separate thread performs a new [INSERT] on the same
       
  1842 ** database connection while the [sqlite3_last_insert_rowid()]
       
  1843 ** function is running and thus changes the last insert [rowid],
       
  1844 ** then the value returned by [sqlite3_last_insert_rowid()] is
       
  1845 ** unpredictable and might not equal either the old or the new
       
  1846 ** last insert [rowid].
       
  1847 */
       
  1848 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
       
  1849 
       
  1850 /*
       
  1851 ** CAPI3REF: Count The Number Of Rows Modified {H12240} <S10600>
       
  1852 **
       
  1853 ** This function returns the number of database rows that were changed
       
  1854 ** or inserted or deleted by the most recently completed SQL statement
       
  1855 ** on the [database connection] specified by the first parameter.
       
  1856 ** Only changes that are directly specified by the [INSERT], [UPDATE],
       
  1857 ** or [DELETE] statement are counted.  Auxiliary changes caused by
       
  1858 ** triggers or [foreign key actions] are not counted. Use the
       
  1859 ** [sqlite3_total_changes()] function to find the total number of changes
       
  1860 ** including changes caused by triggers and foreign key actions.
       
  1861 **
       
  1862 ** Changes to a view that are simulated by an [INSTEAD OF trigger]
       
  1863 ** are not counted.  Only real table changes are counted.
       
  1864 **
       
  1865 ** A "row change" is a change to a single row of a single table
       
  1866 ** caused by an INSERT, DELETE, or UPDATE statement.  Rows that
       
  1867 ** are changed as side effects of [REPLACE] constraint resolution,
       
  1868 ** rollback, ABORT processing, [DROP TABLE], or by any other
       
  1869 ** mechanisms do not count as direct row changes.
       
  1870 **
       
  1871 ** A "trigger context" is a scope of execution that begins and
       
  1872 ** ends with the script of a [CREATE TRIGGER | trigger]. 
       
  1873 ** Most SQL statements are
       
  1874 ** evaluated outside of any trigger.  This is the "top level"
       
  1875 ** trigger context.  If a trigger fires from the top level, a
       
  1876 ** new trigger context is entered for the duration of that one
       
  1877 ** trigger.  Subtriggers create subcontexts for their duration.
       
  1878 **
       
  1879 ** Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
       
  1880 ** not create a new trigger context.
       
  1881 **
       
  1882 ** This function returns the number of direct row changes in the
       
  1883 ** most recent INSERT, UPDATE, or DELETE statement within the same
       
  1884 ** trigger context.
       
  1885 **
       
  1886 ** Thus, when called from the top level, this function returns the
       
  1887 ** number of changes in the most recent INSERT, UPDATE, or DELETE
       
  1888 ** that also occurred at the top level.  Within the body of a trigger,
       
  1889 ** the sqlite3_changes() interface can be called to find the number of
       
  1890 ** changes in the most recently completed INSERT, UPDATE, or DELETE
       
  1891 ** statement within the body of the same trigger.
       
  1892 ** However, the number returned does not include changes
       
  1893 ** caused by subtriggers since those have their own context.
       
  1894 **
       
  1895 ** See also the [sqlite3_total_changes()] interface and the
       
  1896 ** [count_changes pragma].
       
  1897 **
       
  1898 ** Requirements:
       
  1899 ** [H12241] [H12243]
       
  1900 **
       
  1901 ** If a separate thread makes changes on the same database connection
       
  1902 ** while [sqlite3_changes()] is running then the value returned
       
  1903 ** is unpredictable and not meaningful.
       
  1904 */
       
  1905 SQLITE_API int sqlite3_changes(sqlite3*);
       
  1906 
       
  1907 /*
       
  1908 ** CAPI3REF: Total Number Of Rows Modified {H12260} <S10600>
       
  1909 **
       
  1910 ** This function returns the number of row changes caused by [INSERT],
       
  1911 ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
       
  1912 ** The count includes all changes from all [CREATE TRIGGER | trigger] 
       
  1913 ** contexts and changes made by [foreign key actions]. However,
       
  1914 ** the count does not include changes used to implement [REPLACE] constraints,
       
  1915 ** do rollbacks or ABORT processing, or [DROP TABLE] processing.  The
       
  1916 ** count does not include rows of views that fire an [INSTEAD OF trigger],
       
  1917 ** though if the INSTEAD OF trigger makes changes of its own, those changes 
       
  1918 ** are counted.
       
  1919 ** The changes are counted as soon as the statement that makes them is
       
  1920 ** completed (when the statement handle is passed to [sqlite3_reset()] or
       
  1921 ** [sqlite3_finalize()]).
       
  1922 **
       
  1923 ** See also the [sqlite3_changes()] interface and the
       
  1924 ** [count_changes pragma].
       
  1925 **
       
  1926 ** Requirements:
       
  1927 ** [H12261] [H12263]
       
  1928 **
       
  1929 ** If a separate thread makes changes on the same database connection
       
  1930 ** while [sqlite3_total_changes()] is running then the value
       
  1931 ** returned is unpredictable and not meaningful.
       
  1932 */
       
  1933 SQLITE_API int sqlite3_total_changes(sqlite3*);
       
  1934 
       
  1935 /*
       
  1936 ** CAPI3REF: Interrupt A Long-Running Query {H12270} <S30500>
       
  1937 **
       
  1938 ** This function causes any pending database operation to abort and
       
  1939 ** return at its earliest opportunity. This routine is typically
       
  1940 ** called in response to a user action such as pressing "Cancel"
       
  1941 ** or Ctrl-C where the user wants a long query operation to halt
       
  1942 ** immediately.
       
  1943 **
       
  1944 ** It is safe to call this routine from a thread different from the
       
  1945 ** thread that is currently running the database operation.  But it
       
  1946 ** is not safe to call this routine with a [database connection] that
       
  1947 ** is closed or might close before sqlite3_interrupt() returns.
       
  1948 **
       
  1949 ** If an SQL operation is very nearly finished at the time when
       
  1950 ** sqlite3_interrupt() is called, then it might not have an opportunity
       
  1951 ** to be interrupted and might continue to completion.
       
  1952 **
       
  1953 ** An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
       
  1954 ** If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
       
  1955 ** that is inside an explicit transaction, then the entire transaction
       
  1956 ** will be rolled back automatically.
       
  1957 **
       
  1958 ** The sqlite3_interrupt(D) call is in effect until all currently running
       
  1959 ** SQL statements on [database connection] D complete.  Any new SQL statements
       
  1960 ** that are started after the sqlite3_interrupt() call and before the 
       
  1961 ** running statements reaches zero are interrupted as if they had been
       
  1962 ** running prior to the sqlite3_interrupt() call.  New SQL statements
       
  1963 ** that are started after the running statement count reaches zero are
       
  1964 ** not effected by the sqlite3_interrupt().
       
  1965 ** A call to sqlite3_interrupt(D) that occurs when there are no running
       
  1966 ** SQL statements is a no-op and has no effect on SQL statements
       
  1967 ** that are started after the sqlite3_interrupt() call returns.
       
  1968 **
       
  1969 ** Requirements:
       
  1970 ** [H12271] [H12272]
       
  1971 **
       
  1972 ** If the database connection closes while [sqlite3_interrupt()]
       
  1973 ** is running then bad things will likely happen.
       
  1974 */
       
  1975 SQLITE_API void sqlite3_interrupt(sqlite3*);
       
  1976 
       
  1977 /*
       
  1978 ** CAPI3REF: Determine If An SQL Statement Is Complete {H10510} <S70200>
       
  1979 **
       
  1980 ** These routines are useful during command-line input to determine if the
       
  1981 ** currently entered text seems to form a complete SQL statement or
       
  1982 ** if additional input is needed before sending the text into
       
  1983 ** SQLite for parsing.  These routines return 1 if the input string
       
  1984 ** appears to be a complete SQL statement.  A statement is judged to be
       
  1985 ** complete if it ends with a semicolon token and is not a prefix of a
       
  1986 ** well-formed CREATE TRIGGER statement.  Semicolons that are embedded within
       
  1987 ** string literals or quoted identifier names or comments are not
       
  1988 ** independent tokens (they are part of the token in which they are
       
  1989 ** embedded) and thus do not count as a statement terminator.  Whitespace
       
  1990 ** and comments that follow the final semicolon are ignored.
       
  1991 **
       
  1992 ** These routines return 0 if the statement is incomplete.  If a
       
  1993 ** memory allocation fails, then SQLITE_NOMEM is returned.
       
  1994 **
       
  1995 ** These routines do not parse the SQL statements thus
       
  1996 ** will not detect syntactically incorrect SQL.
       
  1997 **
       
  1998 ** If SQLite has not been initialized using [sqlite3_initialize()] prior 
       
  1999 ** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
       
  2000 ** automatically by sqlite3_complete16().  If that initialization fails,
       
  2001 ** then the return value from sqlite3_complete16() will be non-zero
       
  2002 ** regardless of whether or not the input SQL is complete.
       
  2003 **
       
  2004 ** Requirements: [H10511] [H10512]
       
  2005 **
       
  2006 ** The input to [sqlite3_complete()] must be a zero-terminated
       
  2007 ** UTF-8 string.
       
  2008 **
       
  2009 ** The input to [sqlite3_complete16()] must be a zero-terminated
       
  2010 ** UTF-16 string in native byte order.
       
  2011 */
       
  2012 SQLITE_API int sqlite3_complete(const char *sql);
       
  2013 SQLITE_API int sqlite3_complete16(const void *sql);
       
  2014 
       
  2015 /*
       
  2016 ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors {H12310} <S40400>
       
  2017 **
       
  2018 ** This routine sets a callback function that might be invoked whenever
       
  2019 ** an attempt is made to open a database table that another thread
       
  2020 ** or process has locked.
       
  2021 **
       
  2022 ** If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
       
  2023 ** is returned immediately upon encountering the lock. If the busy callback
       
  2024 ** is not NULL, then the callback will be invoked with two arguments.
       
  2025 **
       
  2026 ** The first argument to the handler is a copy of the void* pointer which
       
  2027 ** is the third argument to sqlite3_busy_handler().  The second argument to
       
  2028 ** the handler callback is the number of times that the busy handler has
       
  2029 ** been invoked for this locking event.  If the
       
  2030 ** busy callback returns 0, then no additional attempts are made to
       
  2031 ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
       
  2032 ** If the callback returns non-zero, then another attempt
       
  2033 ** is made to open the database for reading and the cycle repeats.
       
  2034 **
       
  2035 ** The presence of a busy handler does not guarantee that it will be invoked
       
  2036 ** when there is lock contention. If SQLite determines that invoking the busy
       
  2037 ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
       
  2038 ** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
       
  2039 ** Consider a scenario where one process is holding a read lock that
       
  2040 ** it is trying to promote to a reserved lock and
       
  2041 ** a second process is holding a reserved lock that it is trying
       
  2042 ** to promote to an exclusive lock.  The first process cannot proceed
       
  2043 ** because it is blocked by the second and the second process cannot
       
  2044 ** proceed because it is blocked by the first.  If both processes
       
  2045 ** invoke the busy handlers, neither will make any progress.  Therefore,
       
  2046 ** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
       
  2047 ** will induce the first process to release its read lock and allow
       
  2048 ** the second process to proceed.
       
  2049 **
       
  2050 ** The default busy callback is NULL.
       
  2051 **
       
  2052 ** The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
       
  2053 ** when SQLite is in the middle of a large transaction where all the
       
  2054 ** changes will not fit into the in-memory cache.  SQLite will
       
  2055 ** already hold a RESERVED lock on the database file, but it needs
       
  2056 ** to promote this lock to EXCLUSIVE so that it can spill cache
       
  2057 ** pages into the database file without harm to concurrent
       
  2058 ** readers.  If it is unable to promote the lock, then the in-memory
       
  2059 ** cache will be left in an inconsistent state and so the error
       
  2060 ** code is promoted from the relatively benign [SQLITE_BUSY] to
       
  2061 ** the more severe [SQLITE_IOERR_BLOCKED].  This error code promotion
       
  2062 ** forces an automatic rollback of the changes.  See the
       
  2063 ** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
       
  2064 ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
       
  2065 ** this is important.
       
  2066 **
       
  2067 ** There can only be a single busy handler defined for each
       
  2068 ** [database connection].  Setting a new busy handler clears any
       
  2069 ** previously set handler.  Note that calling [sqlite3_busy_timeout()]
       
  2070 ** will also set or clear the busy handler.
       
  2071 **
       
  2072 ** The busy callback should not take any actions which modify the
       
  2073 ** database connection that invoked the busy handler.  Any such actions
       
  2074 ** result in undefined behavior.
       
  2075 ** 
       
  2076 ** Requirements:
       
  2077 ** [H12311] [H12312] [H12314] [H12316] [H12318]
       
  2078 **
       
  2079 ** A busy handler must not close the database connection
       
  2080 ** or [prepared statement] that invoked the busy handler.
       
  2081 */
       
  2082 SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
       
  2083 
       
  2084 /*
       
  2085 ** CAPI3REF: Set A Busy Timeout {H12340} <S40410>
       
  2086 **
       
  2087 ** This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
       
  2088 ** for a specified amount of time when a table is locked.  The handler
       
  2089 ** will sleep multiple times until at least "ms" milliseconds of sleeping
       
  2090 ** have accumulated. {H12343} After "ms" milliseconds of sleeping,
       
  2091 ** the handler returns 0 which causes [sqlite3_step()] to return
       
  2092 ** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
       
  2093 **
       
  2094 ** Calling this routine with an argument less than or equal to zero
       
  2095 ** turns off all busy handlers.
       
  2096 **
       
  2097 ** There can only be a single busy handler for a particular
       
  2098 ** [database connection] any any given moment.  If another busy handler
       
  2099 ** was defined  (using [sqlite3_busy_handler()]) prior to calling
       
  2100 ** this routine, that other busy handler is cleared.
       
  2101 **
       
  2102 ** Requirements:
       
  2103 ** [H12341] [H12343] [H12344]
       
  2104 */
       
  2105 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
       
  2106 
       
  2107 /*
       
  2108 ** CAPI3REF: Convenience Routines For Running Queries {H12370} <S10000>
       
  2109 **
       
  2110 ** Definition: A <b>result table</b> is memory data structure created by the
       
  2111 ** [sqlite3_get_table()] interface.  A result table records the
       
  2112 ** complete query results from one or more queries.
       
  2113 **
       
  2114 ** The table conceptually has a number of rows and columns.  But
       
  2115 ** these numbers are not part of the result table itself.  These
       
  2116 ** numbers are obtained separately.  Let N be the number of rows
       
  2117 ** and M be the number of columns.
       
  2118 **
       
  2119 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
       
  2120 ** There are (N+1)*M elements in the array.  The first M pointers point
       
  2121 ** to zero-terminated strings that  contain the names of the columns.
       
  2122 ** The remaining entries all point to query results.  NULL values result
       
  2123 ** in NULL pointers.  All other values are in their UTF-8 zero-terminated
       
  2124 ** string representation as returned by [sqlite3_column_text()].
       
  2125 **
       
  2126 ** A result table might consist of one or more memory allocations.
       
  2127 ** It is not safe to pass a result table directly to [sqlite3_free()].
       
  2128 ** A result table should be deallocated using [sqlite3_free_table()].
       
  2129 **
       
  2130 ** As an example of the result table format, suppose a query result
       
  2131 ** is as follows:
       
  2132 **
       
  2133 ** <blockquote><pre>
       
  2134 **        Name        | Age
       
  2135 **        -----------------------
       
  2136 **        Alice       | 43
       
  2137 **        Bob         | 28
       
  2138 **        Cindy       | 21
       
  2139 ** </pre></blockquote>
       
  2140 **
       
  2141 ** There are two column (M==2) and three rows (N==3).  Thus the
       
  2142 ** result table has 8 entries.  Suppose the result table is stored
       
  2143 ** in an array names azResult.  Then azResult holds this content:
       
  2144 **
       
  2145 ** <blockquote><pre>
       
  2146 **        azResult&#91;0] = "Name";
       
  2147 **        azResult&#91;1] = "Age";
       
  2148 **        azResult&#91;2] = "Alice";
       
  2149 **        azResult&#91;3] = "43";
       
  2150 **        azResult&#91;4] = "Bob";
       
  2151 **        azResult&#91;5] = "28";
       
  2152 **        azResult&#91;6] = "Cindy";
       
  2153 **        azResult&#91;7] = "21";
       
  2154 ** </pre></blockquote>
       
  2155 **
       
  2156 ** The sqlite3_get_table() function evaluates one or more
       
  2157 ** semicolon-separated SQL statements in the zero-terminated UTF-8
       
  2158 ** string of its 2nd parameter.  It returns a result table to the
       
  2159 ** pointer given in its 3rd parameter.
       
  2160 **
       
  2161 ** After the calling function has finished using the result, it should
       
  2162 ** pass the pointer to the result table to sqlite3_free_table() in order to
       
  2163 ** release the memory that was malloced.  Because of the way the
       
  2164 ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
       
  2165 ** function must not try to call [sqlite3_free()] directly.  Only
       
  2166 ** [sqlite3_free_table()] is able to release the memory properly and safely.
       
  2167 **
       
  2168 ** The sqlite3_get_table() interface is implemented as a wrapper around
       
  2169 ** [sqlite3_exec()].  The sqlite3_get_table() routine does not have access
       
  2170 ** to any internal data structures of SQLite.  It uses only the public
       
  2171 ** interface defined here.  As a consequence, errors that occur in the
       
  2172 ** wrapper layer outside of the internal [sqlite3_exec()] call are not
       
  2173 ** reflected in subsequent calls to [sqlite3_errcode()] or [sqlite3_errmsg()].
       
  2174 **
       
  2175 ** Requirements:
       
  2176 ** [H12371] [H12373] [H12374] [H12376] [H12379] [H12382]
       
  2177 */
       
  2178 SQLITE_API int sqlite3_get_table(
       
  2179   sqlite3 *db,          /* An open database */
       
  2180   const char *zSql,     /* SQL to be evaluated */
       
  2181   char ***pazResult,    /* Results of the query */
       
  2182   int *pnRow,           /* Number of result rows written here */
       
  2183   int *pnColumn,        /* Number of result columns written here */
       
  2184   char **pzErrmsg       /* Error msg written here */
       
  2185 );
       
  2186 SQLITE_API void sqlite3_free_table(char **result);
       
  2187 
       
  2188 /*
       
  2189 ** CAPI3REF: Formatted String Printing Functions {H17400} <S70000><S20000>
       
  2190 **
       
  2191 ** These routines are work-alikes of the "printf()" family of functions
       
  2192 ** from the standard C library.
       
  2193 **
       
  2194 ** The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
       
  2195 ** results into memory obtained from [sqlite3_malloc()].
       
  2196 ** The strings returned by these two routines should be
       
  2197 ** released by [sqlite3_free()].  Both routines return a
       
  2198 ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
       
  2199 ** memory to hold the resulting string.
       
  2200 **
       
  2201 ** In sqlite3_snprintf() routine is similar to "snprintf()" from
       
  2202 ** the standard C library.  The result is written into the
       
  2203 ** buffer supplied as the second parameter whose size is given by
       
  2204 ** the first parameter. Note that the order of the
       
  2205 ** first two parameters is reversed from snprintf().  This is an
       
  2206 ** historical accident that cannot be fixed without breaking
       
  2207 ** backwards compatibility.  Note also that sqlite3_snprintf()
       
  2208 ** returns a pointer to its buffer instead of the number of
       
  2209 ** characters actually written into the buffer.  We admit that
       
  2210 ** the number of characters written would be a more useful return
       
  2211 ** value but we cannot change the implementation of sqlite3_snprintf()
       
  2212 ** now without breaking compatibility.
       
  2213 **
       
  2214 ** As long as the buffer size is greater than zero, sqlite3_snprintf()
       
  2215 ** guarantees that the buffer is always zero-terminated.  The first
       
  2216 ** parameter "n" is the total size of the buffer, including space for
       
  2217 ** the zero terminator.  So the longest string that can be completely
       
  2218 ** written will be n-1 characters.
       
  2219 **
       
  2220 ** These routines all implement some additional formatting
       
  2221 ** options that are useful for constructing SQL statements.
       
  2222 ** All of the usual printf() formatting options apply.  In addition, there
       
  2223 ** is are "%q", "%Q", and "%z" options.
       
  2224 **
       
  2225 ** The %q option works like %s in that it substitutes a null-terminated
       
  2226 ** string from the argument list.  But %q also doubles every '\'' character.
       
  2227 ** %q is designed for use inside a string literal.  By doubling each '\''
       
  2228 ** character it escapes that character and allows it to be inserted into
       
  2229 ** the string.
       
  2230 **
       
  2231 ** For example, assume the string variable zText contains text as follows:
       
  2232 **
       
  2233 ** <blockquote><pre>
       
  2234 **  char *zText = "It's a happy day!";
       
  2235 ** </pre></blockquote>
       
  2236 **
       
  2237 ** One can use this text in an SQL statement as follows:
       
  2238 **
       
  2239 ** <blockquote><pre>
       
  2240 **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
       
  2241 **  sqlite3_exec(db, zSQL, 0, 0, 0);
       
  2242 **  sqlite3_free(zSQL);
       
  2243 ** </pre></blockquote>
       
  2244 **
       
  2245 ** Because the %q format string is used, the '\'' character in zText
       
  2246 ** is escaped and the SQL generated is as follows:
       
  2247 **
       
  2248 ** <blockquote><pre>
       
  2249 **  INSERT INTO table1 VALUES('It''s a happy day!')
       
  2250 ** </pre></blockquote>
       
  2251 **
       
  2252 ** This is correct.  Had we used %s instead of %q, the generated SQL
       
  2253 ** would have looked like this:
       
  2254 **
       
  2255 ** <blockquote><pre>
       
  2256 **  INSERT INTO table1 VALUES('It's a happy day!');
       
  2257 ** </pre></blockquote>
       
  2258 **
       
  2259 ** This second example is an SQL syntax error.  As a general rule you should
       
  2260 ** always use %q instead of %s when inserting text into a string literal.
       
  2261 **
       
  2262 ** The %Q option works like %q except it also adds single quotes around
       
  2263 ** the outside of the total string.  Additionally, if the parameter in the
       
  2264 ** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
       
  2265 ** single quotes) in place of the %Q option.  So, for example, one could say:
       
  2266 **
       
  2267 ** <blockquote><pre>
       
  2268 **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
       
  2269 **  sqlite3_exec(db, zSQL, 0, 0, 0);
       
  2270 **  sqlite3_free(zSQL);
       
  2271 ** </pre></blockquote>
       
  2272 **
       
  2273 ** The code above will render a correct SQL statement in the zSQL
       
  2274 ** variable even if the zText variable is a NULL pointer.
       
  2275 **
       
  2276 ** The "%z" formatting option works exactly like "%s" with the
       
  2277 ** addition that after the string has been read and copied into
       
  2278 ** the result, [sqlite3_free()] is called on the input string. {END}
       
  2279 **
       
  2280 ** Requirements:
       
  2281 ** [H17403] [H17406] [H17407]
       
  2282 */
       
  2283 SQLITE_API char *sqlite3_mprintf(const char*,...);
       
  2284 SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
       
  2285 SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
       
  2286 
       
  2287 /*
       
  2288 ** CAPI3REF: Memory Allocation Subsystem {H17300} <S20000>
       
  2289 **
       
  2290 ** The SQLite core  uses these three routines for all of its own
       
  2291 ** internal memory allocation needs. "Core" in the previous sentence
       
  2292 ** does not include operating-system specific VFS implementation.  The
       
  2293 ** Windows VFS uses native malloc() and free() for some operations.
       
  2294 **
       
  2295 ** The sqlite3_malloc() routine returns a pointer to a block
       
  2296 ** of memory at least N bytes in length, where N is the parameter.
       
  2297 ** If sqlite3_malloc() is unable to obtain sufficient free
       
  2298 ** memory, it returns a NULL pointer.  If the parameter N to
       
  2299 ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
       
  2300 ** a NULL pointer.
       
  2301 **
       
  2302 ** Calling sqlite3_free() with a pointer previously returned
       
  2303 ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
       
  2304 ** that it might be reused.  The sqlite3_free() routine is
       
  2305 ** a no-op if is called with a NULL pointer.  Passing a NULL pointer
       
  2306 ** to sqlite3_free() is harmless.  After being freed, memory
       
  2307 ** should neither be read nor written.  Even reading previously freed
       
  2308 ** memory might result in a segmentation fault or other severe error.
       
  2309 ** Memory corruption, a segmentation fault, or other severe error
       
  2310 ** might result if sqlite3_free() is called with a non-NULL pointer that
       
  2311 ** was not obtained from sqlite3_malloc() or sqlite3_realloc().
       
  2312 **
       
  2313 ** The sqlite3_realloc() interface attempts to resize a
       
  2314 ** prior memory allocation to be at least N bytes, where N is the
       
  2315 ** second parameter.  The memory allocation to be resized is the first
       
  2316 ** parameter.  If the first parameter to sqlite3_realloc()
       
  2317 ** is a NULL pointer then its behavior is identical to calling
       
  2318 ** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
       
  2319 ** If the second parameter to sqlite3_realloc() is zero or
       
  2320 ** negative then the behavior is exactly the same as calling
       
  2321 ** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
       
  2322 ** sqlite3_realloc() returns a pointer to a memory allocation
       
  2323 ** of at least N bytes in size or NULL if sufficient memory is unavailable.
       
  2324 ** If M is the size of the prior allocation, then min(N,M) bytes
       
  2325 ** of the prior allocation are copied into the beginning of buffer returned
       
  2326 ** by sqlite3_realloc() and the prior allocation is freed.
       
  2327 ** If sqlite3_realloc() returns NULL, then the prior allocation
       
  2328 ** is not freed.
       
  2329 **
       
  2330 ** The memory returned by sqlite3_malloc() and sqlite3_realloc()
       
  2331 ** is always aligned to at least an 8 byte boundary. {END}
       
  2332 **
       
  2333 ** The default implementation of the memory allocation subsystem uses
       
  2334 ** the malloc(), realloc() and free() provided by the standard C library.
       
  2335 ** {H17382} However, if SQLite is compiled with the
       
  2336 ** SQLITE_MEMORY_SIZE=<i>NNN</i> C preprocessor macro (where <i>NNN</i>
       
  2337 ** is an integer), then SQLite create a static array of at least
       
  2338 ** <i>NNN</i> bytes in size and uses that array for all of its dynamic
       
  2339 ** memory allocation needs. {END}  Additional memory allocator options
       
  2340 ** may be added in future releases.
       
  2341 **
       
  2342 ** In SQLite version 3.5.0 and 3.5.1, it was possible to define
       
  2343 ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
       
  2344 ** implementation of these routines to be omitted.  That capability
       
  2345 ** is no longer provided.  Only built-in memory allocators can be used.
       
  2346 **
       
  2347 ** The Windows OS interface layer calls
       
  2348 ** the system malloc() and free() directly when converting
       
  2349 ** filenames between the UTF-8 encoding used by SQLite
       
  2350 ** and whatever filename encoding is used by the particular Windows
       
  2351 ** installation.  Memory allocation errors are detected, but
       
  2352 ** they are reported back as [SQLITE_CANTOPEN] or
       
  2353 ** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
       
  2354 **
       
  2355 ** Requirements:
       
  2356 ** [H17303] [H17304] [H17305] [H17306] [H17310] [H17312] [H17315] [H17318]
       
  2357 ** [H17321] [H17322] [H17323]
       
  2358 **
       
  2359 ** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
       
  2360 ** must be either NULL or else pointers obtained from a prior
       
  2361 ** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
       
  2362 ** not yet been released.
       
  2363 **
       
  2364 ** The application must not read or write any part of
       
  2365 ** a block of memory after it has been released using
       
  2366 ** [sqlite3_free()] or [sqlite3_realloc()].
       
  2367 */
       
  2368 SQLITE_API void *sqlite3_malloc(int);
       
  2369 SQLITE_API void *sqlite3_realloc(void*, int);
       
  2370 SQLITE_API void sqlite3_free(void*);
       
  2371 
       
  2372 /*
       
  2373 ** CAPI3REF: Memory Allocator Statistics {H17370} <S30210>
       
  2374 **
       
  2375 ** SQLite provides these two interfaces for reporting on the status
       
  2376 ** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
       
  2377 ** routines, which form the built-in memory allocation subsystem.
       
  2378 **
       
  2379 ** Requirements:
       
  2380 ** [H17371] [H17373] [H17374] [H17375]
       
  2381 */
       
  2382 SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
       
  2383 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
       
  2384 
       
  2385 /*
       
  2386 ** CAPI3REF: Pseudo-Random Number Generator {H17390} <S20000>
       
  2387 **
       
  2388 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
       
  2389 ** select random [ROWID | ROWIDs] when inserting new records into a table that
       
  2390 ** already uses the largest possible [ROWID].  The PRNG is also used for
       
  2391 ** the build-in random() and randomblob() SQL functions.  This interface allows
       
  2392 ** applications to access the same PRNG for other purposes.
       
  2393 **
       
  2394 ** A call to this routine stores N bytes of randomness into buffer P.
       
  2395 **
       
  2396 ** The first time this routine is invoked (either internally or by
       
  2397 ** the application) the PRNG is seeded using randomness obtained
       
  2398 ** from the xRandomness method of the default [sqlite3_vfs] object.
       
  2399 ** On all subsequent invocations, the pseudo-randomness is generated
       
  2400 ** internally and without recourse to the [sqlite3_vfs] xRandomness
       
  2401 ** method.
       
  2402 **
       
  2403 ** Requirements:
       
  2404 ** [H17392]
       
  2405 */
       
  2406 SQLITE_API void sqlite3_randomness(int N, void *P);
       
  2407 
       
  2408 /*
       
  2409 ** CAPI3REF: Compile-Time Authorization Callbacks {H12500} <S70100>
       
  2410 **
       
  2411 ** This routine registers a authorizer callback with a particular
       
  2412 ** [database connection], supplied in the first argument.
       
  2413 ** The authorizer callback is invoked as SQL statements are being compiled
       
  2414 ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
       
  2415 ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  At various
       
  2416 ** points during the compilation process, as logic is being created
       
  2417 ** to perform various actions, the authorizer callback is invoked to
       
  2418 ** see if those actions are allowed.  The authorizer callback should
       
  2419 ** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
       
  2420 ** specific action but allow the SQL statement to continue to be
       
  2421 ** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
       
  2422 ** rejected with an error.  If the authorizer callback returns
       
  2423 ** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
       
  2424 ** then the [sqlite3_prepare_v2()] or equivalent call that triggered
       
  2425 ** the authorizer will fail with an error message.
       
  2426 **
       
  2427 ** When the callback returns [SQLITE_OK], that means the operation
       
  2428 ** requested is ok.  When the callback returns [SQLITE_DENY], the
       
  2429 ** [sqlite3_prepare_v2()] or equivalent call that triggered the
       
  2430 ** authorizer will fail with an error message explaining that
       
  2431 ** access is denied. 
       
  2432 **
       
  2433 ** The first parameter to the authorizer callback is a copy of the third
       
  2434 ** parameter to the sqlite3_set_authorizer() interface. The second parameter
       
  2435 ** to the callback is an integer [SQLITE_COPY | action code] that specifies
       
  2436 ** the particular action to be authorized. The third through sixth parameters
       
  2437 ** to the callback are zero-terminated strings that contain additional
       
  2438 ** details about the action to be authorized.
       
  2439 **
       
  2440 ** If the action code is [SQLITE_READ]
       
  2441 ** and the callback returns [SQLITE_IGNORE] then the
       
  2442 ** [prepared statement] statement is constructed to substitute
       
  2443 ** a NULL value in place of the table column that would have
       
  2444 ** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
       
  2445 ** return can be used to deny an untrusted user access to individual
       
  2446 ** columns of a table.
       
  2447 ** If the action code is [SQLITE_DELETE] and the callback returns
       
  2448 ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
       
  2449 ** [truncate optimization] is disabled and all rows are deleted individually.
       
  2450 **
       
  2451 ** An authorizer is used when [sqlite3_prepare | preparing]
       
  2452 ** SQL statements from an untrusted source, to ensure that the SQL statements
       
  2453 ** do not try to access data they are not allowed to see, or that they do not
       
  2454 ** try to execute malicious statements that damage the database.  For
       
  2455 ** example, an application may allow a user to enter arbitrary
       
  2456 ** SQL queries for evaluation by a database.  But the application does
       
  2457 ** not want the user to be able to make arbitrary changes to the
       
  2458 ** database.  An authorizer could then be put in place while the
       
  2459 ** user-entered SQL is being [sqlite3_prepare | prepared] that
       
  2460 ** disallows everything except [SELECT] statements.
       
  2461 **
       
  2462 ** Applications that need to process SQL from untrusted sources
       
  2463 ** might also consider lowering resource limits using [sqlite3_limit()]
       
  2464 ** and limiting database size using the [max_page_count] [PRAGMA]
       
  2465 ** in addition to using an authorizer.
       
  2466 **
       
  2467 ** Only a single authorizer can be in place on a database connection
       
  2468 ** at a time.  Each call to sqlite3_set_authorizer overrides the
       
  2469 ** previous call.  Disable the authorizer by installing a NULL callback.
       
  2470 ** The authorizer is disabled by default.
       
  2471 **
       
  2472 ** The authorizer callback must not do anything that will modify
       
  2473 ** the database connection that invoked the authorizer callback.
       
  2474 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
       
  2475 ** database connections for the meaning of "modify" in this paragraph.
       
  2476 **
       
  2477 ** When [sqlite3_prepare_v2()] is used to prepare a statement, the
       
  2478 ** statement might be re-prepared during [sqlite3_step()] due to a 
       
  2479 ** schema change.  Hence, the application should ensure that the
       
  2480 ** correct authorizer callback remains in place during the [sqlite3_step()].
       
  2481 **
       
  2482 ** Note that the authorizer callback is invoked only during
       
  2483 ** [sqlite3_prepare()] or its variants.  Authorization is not
       
  2484 ** performed during statement evaluation in [sqlite3_step()], unless
       
  2485 ** as stated in the previous paragraph, sqlite3_step() invokes
       
  2486 ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
       
  2487 **
       
  2488 ** Requirements:
       
  2489 ** [H12501] [H12502] [H12503] [H12504] [H12505] [H12506] [H12507] [H12510]
       
  2490 ** [H12511] [H12512] [H12520] [H12521] [H12522]
       
  2491 */
       
  2492 SQLITE_API int sqlite3_set_authorizer(
       
  2493   sqlite3*,
       
  2494   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
       
  2495   void *pUserData
       
  2496 );
       
  2497 
       
  2498 /*
       
  2499 ** CAPI3REF: Authorizer Return Codes {H12590} <H12500>
       
  2500 **
       
  2501 ** The [sqlite3_set_authorizer | authorizer callback function] must
       
  2502 ** return either [SQLITE_OK] or one of these two constants in order
       
  2503 ** to signal SQLite whether or not the action is permitted.  See the
       
  2504 ** [sqlite3_set_authorizer | authorizer documentation] for additional
       
  2505 ** information.
       
  2506 */
       
  2507 #define SQLITE_DENY   1   /* Abort the SQL statement with an error */
       
  2508 #define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
       
  2509 
       
  2510 /*
       
  2511 ** CAPI3REF: Authorizer Action Codes {H12550} <H12500>
       
  2512 **
       
  2513 ** The [sqlite3_set_authorizer()] interface registers a callback function
       
  2514 ** that is invoked to authorize certain SQL statement actions.  The
       
  2515 ** second parameter to the callback is an integer code that specifies
       
  2516 ** what action is being authorized.  These are the integer action codes that
       
  2517 ** the authorizer callback may be passed.
       
  2518 **
       
  2519 ** These action code values signify what kind of operation is to be
       
  2520 ** authorized.  The 3rd and 4th parameters to the authorization
       
  2521 ** callback function will be parameters or NULL depending on which of these
       
  2522 ** codes is used as the second parameter.  The 5th parameter to the
       
  2523 ** authorizer callback is the name of the database ("main", "temp",
       
  2524 ** etc.) if applicable.  The 6th parameter to the authorizer callback
       
  2525 ** is the name of the inner-most trigger or view that is responsible for
       
  2526 ** the access attempt or NULL if this access attempt is directly from
       
  2527 ** top-level SQL code.
       
  2528 **
       
  2529 ** Requirements:
       
  2530 ** [H12551] [H12552] [H12553] [H12554]
       
  2531 */
       
  2532 /******************************************* 3rd ************ 4th ***********/
       
  2533 #define SQLITE_CREATE_INDEX          1   /* Index Name      Table Name      */
       
  2534 #define SQLITE_CREATE_TABLE          2   /* Table Name      NULL            */
       
  2535 #define SQLITE_CREATE_TEMP_INDEX     3   /* Index Name      Table Name      */
       
  2536 #define SQLITE_CREATE_TEMP_TABLE     4   /* Table Name      NULL            */
       
  2537 #define SQLITE_CREATE_TEMP_TRIGGER   5   /* Trigger Name    Table Name      */
       
  2538 #define SQLITE_CREATE_TEMP_VIEW      6   /* View Name       NULL            */
       
  2539 #define SQLITE_CREATE_TRIGGER        7   /* Trigger Name    Table Name      */
       
  2540 #define SQLITE_CREATE_VIEW           8   /* View Name       NULL            */
       
  2541 #define SQLITE_DELETE                9   /* Table Name      NULL            */
       
  2542 #define SQLITE_DROP_INDEX           10   /* Index Name      Table Name      */
       
  2543 #define SQLITE_DROP_TABLE           11   /* Table Name      NULL            */
       
  2544 #define SQLITE_DROP_TEMP_INDEX      12   /* Index Name      Table Name      */
       
  2545 #define SQLITE_DROP_TEMP_TABLE      13   /* Table Name      NULL            */
       
  2546 #define SQLITE_DROP_TEMP_TRIGGER    14   /* Trigger Name    Table Name      */
       
  2547 #define SQLITE_DROP_TEMP_VIEW       15   /* View Name       NULL            */
       
  2548 #define SQLITE_DROP_TRIGGER         16   /* Trigger Name    Table Name      */
       
  2549 #define SQLITE_DROP_VIEW            17   /* View Name       NULL            */
       
  2550 #define SQLITE_INSERT               18   /* Table Name      NULL            */
       
  2551 #define SQLITE_PRAGMA               19   /* Pragma Name     1st arg or NULL */
       
  2552 #define SQLITE_READ                 20   /* Table Name      Column Name     */
       
  2553 #define SQLITE_SELECT               21   /* NULL            NULL            */
       
  2554 #define SQLITE_TRANSACTION          22   /* Operation       NULL            */
       
  2555 #define SQLITE_UPDATE               23   /* Table Name      Column Name     */
       
  2556 #define SQLITE_ATTACH               24   /* Filename        NULL            */
       
  2557 #define SQLITE_DETACH               25   /* Database Name   NULL            */
       
  2558 #define SQLITE_ALTER_TABLE          26   /* Database Name   Table Name      */
       
  2559 #define SQLITE_REINDEX              27   /* Index Name      NULL            */
       
  2560 #define SQLITE_ANALYZE              28   /* Table Name      NULL            */
       
  2561 #define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
       
  2562 #define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
       
  2563 #define SQLITE_FUNCTION             31   /* NULL            Function Name   */
       
  2564 #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
       
  2565 #define SQLITE_COPY                  0   /* No longer used */
       
  2566 
       
  2567 /*
       
  2568 ** CAPI3REF: Tracing And Profiling Functions {H12280} <S60400>
       
  2569 ** EXPERIMENTAL
       
  2570 **
       
  2571 ** These routines register callback functions that can be used for
       
  2572 ** tracing and profiling the execution of SQL statements.
       
  2573 **
       
  2574 ** The callback function registered by sqlite3_trace() is invoked at
       
  2575 ** various times when an SQL statement is being run by [sqlite3_step()].
       
  2576 ** The callback returns a UTF-8 rendering of the SQL statement text
       
  2577 ** as the statement first begins executing.  Additional callbacks occur
       
  2578 ** as each triggered subprogram is entered.  The callbacks for triggers
       
  2579 ** contain a UTF-8 SQL comment that identifies the trigger.
       
  2580 **
       
  2581 ** The callback function registered by sqlite3_profile() is invoked
       
  2582 ** as each SQL statement finishes.  The profile callback contains
       
  2583 ** the original statement text and an estimate of wall-clock time
       
  2584 ** of how long that statement took to run.
       
  2585 **
       
  2586 ** Requirements:
       
  2587 ** [H12281] [H12282] [H12283] [H12284] [H12285] [H12287] [H12288] [H12289]
       
  2588 ** [H12290]
       
  2589 */
       
  2590 SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
       
  2591 SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
       
  2592    void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
       
  2593 
       
  2594 /*
       
  2595 ** CAPI3REF: Query Progress Callbacks {H12910} <S60400>
       
  2596 **
       
  2597 ** This routine configures a callback function - the
       
  2598 ** progress callback - that is invoked periodically during long
       
  2599 ** running calls to [sqlite3_exec()], [sqlite3_step()] and
       
  2600 ** [sqlite3_get_table()].  An example use for this
       
  2601 ** interface is to keep a GUI updated during a large query.
       
  2602 **
       
  2603 ** If the progress callback returns non-zero, the operation is
       
  2604 ** interrupted.  This feature can be used to implement a
       
  2605 ** "Cancel" button on a GUI progress dialog box.
       
  2606 **
       
  2607 ** The progress handler must not do anything that will modify
       
  2608 ** the database connection that invoked the progress handler.
       
  2609 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
       
  2610 ** database connections for the meaning of "modify" in this paragraph.
       
  2611 **
       
  2612 ** Requirements:
       
  2613 ** [H12911] [H12912] [H12913] [H12914] [H12915] [H12916] [H12917] [H12918]
       
  2614 **
       
  2615 */
       
  2616 SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
       
  2617 
       
  2618 /*
       
  2619 ** CAPI3REF: Opening A New Database Connection {H12700} <S40200>
       
  2620 **
       
  2621 ** These routines open an SQLite database file whose name is given by the
       
  2622 ** filename argument. The filename argument is interpreted as UTF-8 for
       
  2623 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
       
  2624 ** order for sqlite3_open16(). A [database connection] handle is usually
       
  2625 ** returned in *ppDb, even if an error occurs.  The only exception is that
       
  2626 ** if SQLite is unable to allocate memory to hold the [sqlite3] object,
       
  2627 ** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
       
  2628 ** object. If the database is opened (and/or created) successfully, then
       
  2629 ** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.  The
       
  2630 ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
       
  2631 ** an English language description of the error.
       
  2632 **
       
  2633 ** The default encoding for the database will be UTF-8 if
       
  2634 ** sqlite3_open() or sqlite3_open_v2() is called and
       
  2635 ** UTF-16 in the native byte order if sqlite3_open16() is used.
       
  2636 **
       
  2637 ** Whether or not an error occurs when it is opened, resources
       
  2638 ** associated with the [database connection] handle should be released by
       
  2639 ** passing it to [sqlite3_close()] when it is no longer required.
       
  2640 **
       
  2641 ** The sqlite3_open_v2() interface works like sqlite3_open()
       
  2642 ** except that it accepts two additional parameters for additional control
       
  2643 ** over the new database connection.  The flags parameter can take one of
       
  2644 ** the following three values, optionally combined with the 
       
  2645 ** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
       
  2646 ** and/or [SQLITE_OPEN_PRIVATECACHE] flags:
       
  2647 **
       
  2648 ** <dl>
       
  2649 ** <dt>[SQLITE_OPEN_READONLY]</dt>
       
  2650 ** <dd>The database is opened in read-only mode.  If the database does not
       
  2651 ** already exist, an error is returned.</dd>
       
  2652 **
       
  2653 ** <dt>[SQLITE_OPEN_READWRITE]</dt>
       
  2654 ** <dd>The database is opened for reading and writing if possible, or reading
       
  2655 ** only if the file is write protected by the operating system.  In either
       
  2656 ** case the database must already exist, otherwise an error is returned.</dd>
       
  2657 **
       
  2658 ** <dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
       
  2659 ** <dd>The database is opened for reading and writing, and is creates it if
       
  2660 ** it does not already exist. This is the behavior that is always used for
       
  2661 ** sqlite3_open() and sqlite3_open16().</dd>
       
  2662 ** </dl>
       
  2663 **
       
  2664 ** If the 3rd parameter to sqlite3_open_v2() is not one of the
       
  2665 ** combinations shown above or one of the combinations shown above combined
       
  2666 ** with the [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX],
       
  2667 ** [SQLITE_OPEN_SHAREDCACHE] and/or [SQLITE_OPEN_SHAREDCACHE] flags,
       
  2668 ** then the behavior is undefined.
       
  2669 **
       
  2670 ** If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
       
  2671 ** opens in the multi-thread [threading mode] as long as the single-thread
       
  2672 ** mode has not been set at compile-time or start-time.  If the
       
  2673 ** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
       
  2674 ** in the serialized [threading mode] unless single-thread was
       
  2675 ** previously selected at compile-time or start-time.
       
  2676 ** The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
       
  2677 ** eligible to use [shared cache mode], regardless of whether or not shared
       
  2678 ** cache is enabled using [sqlite3_enable_shared_cache()].  The
       
  2679 ** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
       
  2680 ** participate in [shared cache mode] even if it is enabled.
       
  2681 **
       
  2682 ** If the filename is ":memory:", then a private, temporary in-memory database
       
  2683 ** is created for the connection.  This in-memory database will vanish when
       
  2684 ** the database connection is closed.  Future versions of SQLite might
       
  2685 ** make use of additional special filenames that begin with the ":" character.
       
  2686 ** It is recommended that when a database filename actually does begin with
       
  2687 ** a ":" character you should prefix the filename with a pathname such as
       
  2688 ** "./" to avoid ambiguity.
       
  2689 **
       
  2690 ** If the filename is an empty string, then a private, temporary
       
  2691 ** on-disk database will be created.  This private database will be
       
  2692 ** automatically deleted as soon as the database connection is closed.
       
  2693 **
       
  2694 ** The fourth parameter to sqlite3_open_v2() is the name of the
       
  2695 ** [sqlite3_vfs] object that defines the operating system interface that
       
  2696 ** the new database connection should use.  If the fourth parameter is
       
  2697 ** a NULL pointer then the default [sqlite3_vfs] object is used.
       
  2698 **
       
  2699 ** <b>Note to Windows users:</b>  The encoding used for the filename argument
       
  2700 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
       
  2701 ** codepage is currently defined.  Filenames containing international
       
  2702 ** characters must be converted to UTF-8 prior to passing them into
       
  2703 ** sqlite3_open() or sqlite3_open_v2().
       
  2704 **
       
  2705 ** Requirements:
       
  2706 ** [H12701] [H12702] [H12703] [H12704] [H12706] [H12707] [H12709] [H12711]
       
  2707 ** [H12712] [H12713] [H12714] [H12717] [H12719] [H12721] [H12723]
       
  2708 */
       
  2709 SQLITE_API int sqlite3_open(
       
  2710   const char *filename,   /* Database filename (UTF-8) */
       
  2711   sqlite3 **ppDb          /* OUT: SQLite db handle */
       
  2712 );
       
  2713 SQLITE_API int sqlite3_open16(
       
  2714   const void *filename,   /* Database filename (UTF-16) */
       
  2715   sqlite3 **ppDb          /* OUT: SQLite db handle */
       
  2716 );
       
  2717 SQLITE_API int sqlite3_open_v2(
       
  2718   const char *filename,   /* Database filename (UTF-8) */
       
  2719   sqlite3 **ppDb,         /* OUT: SQLite db handle */
       
  2720   int flags,              /* Flags */
       
  2721   const char *zVfs        /* Name of VFS module to use */
       
  2722 );
       
  2723 
       
  2724 /*
       
  2725 ** CAPI3REF: Error Codes And Messages {H12800} <S60200>
       
  2726 **
       
  2727 ** The sqlite3_errcode() interface returns the numeric [result code] or
       
  2728 ** [extended result code] for the most recent failed sqlite3_* API call
       
  2729 ** associated with a [database connection]. If a prior API call failed
       
  2730 ** but the most recent API call succeeded, the return value from
       
  2731 ** sqlite3_errcode() is undefined.  The sqlite3_extended_errcode()
       
  2732 ** interface is the same except that it always returns the 
       
  2733 ** [extended result code] even when extended result codes are
       
  2734 ** disabled.
       
  2735 **
       
  2736 ** The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
       
  2737 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
       
  2738 ** Memory to hold the error message string is managed internally.
       
  2739 ** The application does not need to worry about freeing the result.
       
  2740 ** However, the error string might be overwritten or deallocated by
       
  2741 ** subsequent calls to other SQLite interface functions.
       
  2742 **
       
  2743 ** When the serialized [threading mode] is in use, it might be the
       
  2744 ** case that a second error occurs on a separate thread in between
       
  2745 ** the time of the first error and the call to these interfaces.
       
  2746 ** When that happens, the second error will be reported since these
       
  2747 ** interfaces always report the most recent result.  To avoid
       
  2748 ** this, each thread can obtain exclusive use of the [database connection] D
       
  2749 ** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
       
  2750 ** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
       
  2751 ** all calls to the interfaces listed here are completed.
       
  2752 **
       
  2753 ** If an interface fails with SQLITE_MISUSE, that means the interface
       
  2754 ** was invoked incorrectly by the application.  In that case, the
       
  2755 ** error code and message may or may not be set.
       
  2756 **
       
  2757 ** Requirements:
       
  2758 ** [H12801] [H12802] [H12803] [H12807] [H12808] [H12809]
       
  2759 */
       
  2760 SQLITE_API int sqlite3_errcode(sqlite3 *db);
       
  2761 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
       
  2762 SQLITE_API const char *sqlite3_errmsg(sqlite3*);
       
  2763 SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
       
  2764 
       
  2765 /*
       
  2766 ** CAPI3REF: SQL Statement Object {H13000} <H13010>
       
  2767 ** KEYWORDS: {prepared statement} {prepared statements}
       
  2768 **
       
  2769 ** An instance of this object represents a single SQL statement.
       
  2770 ** This object is variously known as a "prepared statement" or a
       
  2771 ** "compiled SQL statement" or simply as a "statement".
       
  2772 **
       
  2773 ** The life of a statement object goes something like this:
       
  2774 **
       
  2775 ** <ol>
       
  2776 ** <li> Create the object using [sqlite3_prepare_v2()] or a related
       
  2777 **      function.
       
  2778 ** <li> Bind values to [host parameters] using the sqlite3_bind_*()
       
  2779 **      interfaces.
       
  2780 ** <li> Run the SQL by calling [sqlite3_step()] one or more times.
       
  2781 ** <li> Reset the statement using [sqlite3_reset()] then go back
       
  2782 **      to step 2.  Do this zero or more times.
       
  2783 ** <li> Destroy the object using [sqlite3_finalize()].
       
  2784 ** </ol>
       
  2785 **
       
  2786 ** Refer to documentation on individual methods above for additional
       
  2787 ** information.
       
  2788 */
       
  2789 typedef struct sqlite3_stmt sqlite3_stmt;
       
  2790 
       
  2791 /*
       
  2792 ** CAPI3REF: Run-time Limits {H12760} <S20600>
       
  2793 **
       
  2794 ** This interface allows the size of various constructs to be limited
       
  2795 ** on a connection by connection basis.  The first parameter is the
       
  2796 ** [database connection] whose limit is to be set or queried.  The
       
  2797 ** second parameter is one of the [limit categories] that define a
       
  2798 ** class of constructs to be size limited.  The third parameter is the
       
  2799 ** new limit for that construct.  The function returns the old limit.
       
  2800 **
       
  2801 ** If the new limit is a negative number, the limit is unchanged.
       
  2802 ** For the limit category of SQLITE_LIMIT_XYZ there is a 
       
  2803 ** [limits | hard upper bound]
       
  2804 ** set by a compile-time C preprocessor macro named 
       
  2805 ** [limits | SQLITE_MAX_XYZ].
       
  2806 ** (The "_LIMIT_" in the name is changed to "_MAX_".)
       
  2807 ** Attempts to increase a limit above its hard upper bound are
       
  2808 ** silently truncated to the hard upper limit.
       
  2809 **
       
  2810 ** Run time limits are intended for use in applications that manage
       
  2811 ** both their own internal database and also databases that are controlled
       
  2812 ** by untrusted external sources.  An example application might be a
       
  2813 ** web browser that has its own databases for storing history and
       
  2814 ** separate databases controlled by JavaScript applications downloaded
       
  2815 ** off the Internet.  The internal databases can be given the
       
  2816 ** large, default limits.  Databases managed by external sources can
       
  2817 ** be given much smaller limits designed to prevent a denial of service
       
  2818 ** attack.  Developers might also want to use the [sqlite3_set_authorizer()]
       
  2819 ** interface to further control untrusted SQL.  The size of the database
       
  2820 ** created by an untrusted script can be contained using the
       
  2821 ** [max_page_count] [PRAGMA].
       
  2822 **
       
  2823 ** New run-time limit categories may be added in future releases.
       
  2824 **
       
  2825 ** Requirements:
       
  2826 ** [H12762] [H12766] [H12769]
       
  2827 */
       
  2828 SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
       
  2829 
       
  2830 /*
       
  2831 ** CAPI3REF: Run-Time Limit Categories {H12790} <H12760>
       
  2832 ** KEYWORDS: {limit category} {limit categories}
       
  2833 **
       
  2834 ** These constants define various performance limits
       
  2835 ** that can be lowered at run-time using [sqlite3_limit()].
       
  2836 ** The synopsis of the meanings of the various limits is shown below.
       
  2837 ** Additional information is available at [limits | Limits in SQLite].
       
  2838 **
       
  2839 ** <dl>
       
  2840 ** <dt>SQLITE_LIMIT_LENGTH</dt>
       
  2841 ** <dd>The maximum size of any string or BLOB or table row.<dd>
       
  2842 **
       
  2843 ** <dt>SQLITE_LIMIT_SQL_LENGTH</dt>
       
  2844 ** <dd>The maximum length of an SQL statement.</dd>
       
  2845 **
       
  2846 ** <dt>SQLITE_LIMIT_COLUMN</dt>
       
  2847 ** <dd>The maximum number of columns in a table definition or in the
       
  2848 ** result set of a [SELECT] or the maximum number of columns in an index
       
  2849 ** or in an ORDER BY or GROUP BY clause.</dd>
       
  2850 **
       
  2851 ** <dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
       
  2852 ** <dd>The maximum depth of the parse tree on any expression.</dd>
       
  2853 **
       
  2854 ** <dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
       
  2855 ** <dd>The maximum number of terms in a compound SELECT statement.</dd>
       
  2856 **
       
  2857 ** <dt>SQLITE_LIMIT_VDBE_OP</dt>
       
  2858 ** <dd>The maximum number of instructions in a virtual machine program
       
  2859 ** used to implement an SQL statement.</dd>
       
  2860 **
       
  2861 ** <dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
       
  2862 ** <dd>The maximum number of arguments on a function.</dd>
       
  2863 **
       
  2864 ** <dt>SQLITE_LIMIT_ATTACHED</dt>
       
  2865 ** <dd>The maximum number of [ATTACH | attached databases].</dd>
       
  2866 **
       
  2867 ** <dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
       
  2868 ** <dd>The maximum length of the pattern argument to the [LIKE] or
       
  2869 ** [GLOB] operators.</dd>
       
  2870 **
       
  2871 ** <dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
       
  2872 ** <dd>The maximum number of variables in an SQL statement that can
       
  2873 ** be bound.</dd>
       
  2874 **
       
  2875 ** <dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
       
  2876 ** <dd>The maximum depth of recursion for triggers.</dd>
       
  2877 ** </dl>
       
  2878 */
       
  2879 #define SQLITE_LIMIT_LENGTH                    0
       
  2880 #define SQLITE_LIMIT_SQL_LENGTH                1
       
  2881 #define SQLITE_LIMIT_COLUMN                    2
       
  2882 #define SQLITE_LIMIT_EXPR_DEPTH                3
       
  2883 #define SQLITE_LIMIT_COMPOUND_SELECT           4
       
  2884 #define SQLITE_LIMIT_VDBE_OP                   5
       
  2885 #define SQLITE_LIMIT_FUNCTION_ARG              6
       
  2886 #define SQLITE_LIMIT_ATTACHED                  7
       
  2887 #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
       
  2888 #define SQLITE_LIMIT_VARIABLE_NUMBER           9
       
  2889 #define SQLITE_LIMIT_TRIGGER_DEPTH            10
       
  2890 
       
  2891 /*
       
  2892 ** CAPI3REF: Compiling An SQL Statement {H13010} <S10000>
       
  2893 ** KEYWORDS: {SQL statement compiler}
       
  2894 **
       
  2895 ** To execute an SQL query, it must first be compiled into a byte-code
       
  2896 ** program using one of these routines.
       
  2897 **
       
  2898 ** The first argument, "db", is a [database connection] obtained from a
       
  2899 ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
       
  2900 ** [sqlite3_open16()].  The database connection must not have been closed.
       
  2901 **
       
  2902 ** The second argument, "zSql", is the statement to be compiled, encoded
       
  2903 ** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
       
  2904 ** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
       
  2905 ** use UTF-16.
       
  2906 **
       
  2907 ** If the nByte argument is less than zero, then zSql is read up to the
       
  2908 ** first zero terminator. If nByte is non-negative, then it is the maximum
       
  2909 ** number of  bytes read from zSql.  When nByte is non-negative, the
       
  2910 ** zSql string ends at either the first '\000' or '\u0000' character or
       
  2911 ** the nByte-th byte, whichever comes first. If the caller knows
       
  2912 ** that the supplied string is nul-terminated, then there is a small
       
  2913 ** performance advantage to be gained by passing an nByte parameter that
       
  2914 ** is equal to the number of bytes in the input string <i>including</i>
       
  2915 ** the nul-terminator bytes.
       
  2916 **
       
  2917 ** If pzTail is not NULL then *pzTail is made to point to the first byte
       
  2918 ** past the end of the first SQL statement in zSql.  These routines only
       
  2919 ** compile the first statement in zSql, so *pzTail is left pointing to
       
  2920 ** what remains uncompiled.
       
  2921 **
       
  2922 ** *ppStmt is left pointing to a compiled [prepared statement] that can be
       
  2923 ** executed using [sqlite3_step()].  If there is an error, *ppStmt is set
       
  2924 ** to NULL.  If the input text contains no SQL (if the input is an empty
       
  2925 ** string or a comment) then *ppStmt is set to NULL.
       
  2926 ** The calling procedure is responsible for deleting the compiled
       
  2927 ** SQL statement using [sqlite3_finalize()] after it has finished with it.
       
  2928 ** ppStmt may not be NULL.
       
  2929 **
       
  2930 ** On success, [SQLITE_OK] is returned, otherwise an [error code] is returned.
       
  2931 **
       
  2932 ** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
       
  2933 ** recommended for all new programs. The two older interfaces are retained
       
  2934 ** for backwards compatibility, but their use is discouraged.
       
  2935 ** In the "v2" interfaces, the prepared statement
       
  2936 ** that is returned (the [sqlite3_stmt] object) contains a copy of the
       
  2937 ** original SQL text. This causes the [sqlite3_step()] interface to
       
  2938 ** behave a differently in two ways:
       
  2939 **
       
  2940 ** <ol>
       
  2941 ** <li>
       
  2942 ** If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
       
  2943 ** always used to do, [sqlite3_step()] will automatically recompile the SQL
       
  2944 ** statement and try to run it again.  If the schema has changed in
       
  2945 ** a way that makes the statement no longer valid, [sqlite3_step()] will still
       
  2946 ** return [SQLITE_SCHEMA].  But unlike the legacy behavior, [SQLITE_SCHEMA] is
       
  2947 ** now a fatal error.  Calling [sqlite3_prepare_v2()] again will not make the
       
  2948 ** error go away.  Note: use [sqlite3_errmsg()] to find the text
       
  2949 ** of the parsing error that results in an [SQLITE_SCHEMA] return.
       
  2950 ** </li>
       
  2951 **
       
  2952 ** <li>
       
  2953 ** When an error occurs, [sqlite3_step()] will return one of the detailed
       
  2954 ** [error codes] or [extended error codes].  The legacy behavior was that
       
  2955 ** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
       
  2956 ** and you would have to make a second call to [sqlite3_reset()] in order
       
  2957 ** to find the underlying cause of the problem. With the "v2" prepare
       
  2958 ** interfaces, the underlying reason for the error is returned immediately.
       
  2959 ** </li>
       
  2960 ** </ol>
       
  2961 **
       
  2962 ** Requirements:
       
  2963 ** [H13011] [H13012] [H13013] [H13014] [H13015] [H13016] [H13019] [H13021]
       
  2964 **
       
  2965 */
       
  2966 SQLITE_API int sqlite3_prepare(
       
  2967   sqlite3 *db,            /* Database handle */
       
  2968   const char *zSql,       /* SQL statement, UTF-8 encoded */
       
  2969   int nByte,              /* Maximum length of zSql in bytes. */
       
  2970   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
       
  2971   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
       
  2972 );
       
  2973 SQLITE_API int sqlite3_prepare_v2(
       
  2974   sqlite3 *db,            /* Database handle */
       
  2975   const char *zSql,       /* SQL statement, UTF-8 encoded */
       
  2976   int nByte,              /* Maximum length of zSql in bytes. */
       
  2977   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
       
  2978   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
       
  2979 );
       
  2980 SQLITE_API int sqlite3_prepare16(
       
  2981   sqlite3 *db,            /* Database handle */
       
  2982   const void *zSql,       /* SQL statement, UTF-16 encoded */
       
  2983   int nByte,              /* Maximum length of zSql in bytes. */
       
  2984   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
       
  2985   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
       
  2986 );
       
  2987 SQLITE_API int sqlite3_prepare16_v2(
       
  2988   sqlite3 *db,            /* Database handle */
       
  2989   const void *zSql,       /* SQL statement, UTF-16 encoded */
       
  2990   int nByte,              /* Maximum length of zSql in bytes. */
       
  2991   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
       
  2992   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
       
  2993 );
       
  2994 
       
  2995 /*
       
  2996 ** CAPI3REF: Retrieving Statement SQL {H13100} <H13000>
       
  2997 **
       
  2998 ** This interface can be used to retrieve a saved copy of the original
       
  2999 ** SQL text used to create a [prepared statement] if that statement was
       
  3000 ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
       
  3001 **
       
  3002 ** Requirements:
       
  3003 ** [H13101] [H13102] [H13103]
       
  3004 */
       
  3005 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
       
  3006 
       
  3007 /*
       
  3008 ** CAPI3REF: Dynamically Typed Value Object {H15000} <S20200>
       
  3009 ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
       
  3010 **
       
  3011 ** SQLite uses the sqlite3_value object to represent all values
       
  3012 ** that can be stored in a database table. SQLite uses dynamic typing
       
  3013 ** for the values it stores. Values stored in sqlite3_value objects
       
  3014 ** can be integers, floating point values, strings, BLOBs, or NULL.
       
  3015 **
       
  3016 ** An sqlite3_value object may be either "protected" or "unprotected".
       
  3017 ** Some interfaces require a protected sqlite3_value.  Other interfaces
       
  3018 ** will accept either a protected or an unprotected sqlite3_value.
       
  3019 ** Every interface that accepts sqlite3_value arguments specifies
       
  3020 ** whether or not it requires a protected sqlite3_value.
       
  3021 **
       
  3022 ** The terms "protected" and "unprotected" refer to whether or not
       
  3023 ** a mutex is held.  A internal mutex is held for a protected
       
  3024 ** sqlite3_value object but no mutex is held for an unprotected
       
  3025 ** sqlite3_value object.  If SQLite is compiled to be single-threaded
       
  3026 ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
       
  3027 ** or if SQLite is run in one of reduced mutex modes 
       
  3028 ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
       
  3029 ** then there is no distinction between protected and unprotected
       
  3030 ** sqlite3_value objects and they can be used interchangeably.  However,
       
  3031 ** for maximum code portability it is recommended that applications
       
  3032 ** still make the distinction between between protected and unprotected
       
  3033 ** sqlite3_value objects even when not strictly required.
       
  3034 **
       
  3035 ** The sqlite3_value objects that are passed as parameters into the
       
  3036 ** implementation of [application-defined SQL functions] are protected.
       
  3037 ** The sqlite3_value object returned by
       
  3038 ** [sqlite3_column_value()] is unprotected.
       
  3039 ** Unprotected sqlite3_value objects may only be used with
       
  3040 ** [sqlite3_result_value()] and [sqlite3_bind_value()].
       
  3041 ** The [sqlite3_value_blob | sqlite3_value_type()] family of
       
  3042 ** interfaces require protected sqlite3_value objects.
       
  3043 */
       
  3044 typedef struct Mem sqlite3_value;
       
  3045 
       
  3046 /*
       
  3047 ** CAPI3REF: SQL Function Context Object {H16001} <S20200>
       
  3048 **
       
  3049 ** The context in which an SQL function executes is stored in an
       
  3050 ** sqlite3_context object.  A pointer to an sqlite3_context object
       
  3051 ** is always first parameter to [application-defined SQL functions].
       
  3052 ** The application-defined SQL function implementation will pass this
       
  3053 ** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
       
  3054 ** [sqlite3_aggregate_context()], [sqlite3_user_data()],
       
  3055 ** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
       
  3056 ** and/or [sqlite3_set_auxdata()].
       
  3057 */
       
  3058 typedef struct sqlite3_context sqlite3_context;
       
  3059 
       
  3060 /*
       
  3061 ** CAPI3REF: Binding Values To Prepared Statements {H13500} <S70300>
       
  3062 ** KEYWORDS: {host parameter} {host parameters} {host parameter name}
       
  3063 ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
       
  3064 **
       
  3065 ** In the SQL strings input to [sqlite3_prepare_v2()] and its variants,
       
  3066 ** literals may be replaced by a [parameter] that matches one of following
       
  3067 ** templates:
       
  3068 **
       
  3069 ** <ul>
       
  3070 ** <li>  ?
       
  3071 ** <li>  ?NNN
       
  3072 ** <li>  :VVV
       
  3073 ** <li>  @VVV
       
  3074 ** <li>  $VVV
       
  3075 ** </ul>
       
  3076 **
       
  3077 ** In the templates above, NNN represents an integer literal,
       
  3078 ** and VVV represents an alphanumeric identifer.  The values of these
       
  3079 ** parameters (also called "host parameter names" or "SQL parameters")
       
  3080 ** can be set using the sqlite3_bind_*() routines defined here.
       
  3081 **
       
  3082 ** The first argument to the sqlite3_bind_*() routines is always
       
  3083 ** a pointer to the [sqlite3_stmt] object returned from
       
  3084 ** [sqlite3_prepare_v2()] or its variants.
       
  3085 **
       
  3086 ** The second argument is the index of the SQL parameter to be set.
       
  3087 ** The leftmost SQL parameter has an index of 1.  When the same named
       
  3088 ** SQL parameter is used more than once, second and subsequent
       
  3089 ** occurrences have the same index as the first occurrence.
       
  3090 ** The index for named parameters can be looked up using the
       
  3091 ** [sqlite3_bind_parameter_index()] API if desired.  The index
       
  3092 ** for "?NNN" parameters is the value of NNN.
       
  3093 ** The NNN value must be between 1 and the [sqlite3_limit()]
       
  3094 ** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
       
  3095 **
       
  3096 ** The third argument is the value to bind to the parameter.
       
  3097 **
       
  3098 ** In those routines that have a fourth argument, its value is the
       
  3099 ** number of bytes in the parameter.  To be clear: the value is the
       
  3100 ** number of <u>bytes</u> in the value, not the number of characters.
       
  3101 ** If the fourth parameter is negative, the length of the string is
       
  3102 ** the number of bytes up to the first zero terminator.
       
  3103 **
       
  3104 ** The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
       
  3105 ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
       
  3106 ** string after SQLite has finished with it. If the fifth argument is
       
  3107 ** the special value [SQLITE_STATIC], then SQLite assumes that the
       
  3108 ** information is in static, unmanaged space and does not need to be freed.
       
  3109 ** If the fifth argument has the value [SQLITE_TRANSIENT], then
       
  3110 ** SQLite makes its own private copy of the data immediately, before
       
  3111 ** the sqlite3_bind_*() routine returns.
       
  3112 **
       
  3113 ** The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
       
  3114 ** is filled with zeroes.  A zeroblob uses a fixed amount of memory
       
  3115 ** (just an integer to hold its size) while it is being processed.
       
  3116 ** Zeroblobs are intended to serve as placeholders for BLOBs whose
       
  3117 ** content is later written using
       
  3118 ** [sqlite3_blob_open | incremental BLOB I/O] routines.
       
  3119 ** A negative value for the zeroblob results in a zero-length BLOB.
       
  3120 **
       
  3121 ** The sqlite3_bind_*() routines must be called after
       
  3122 ** [sqlite3_prepare_v2()] (and its variants) or [sqlite3_reset()] and
       
  3123 ** before [sqlite3_step()].
       
  3124 ** Bindings are not cleared by the [sqlite3_reset()] routine.
       
  3125 ** Unbound parameters are interpreted as NULL.
       
  3126 **
       
  3127 ** These routines return [SQLITE_OK] on success or an error code if
       
  3128 ** anything goes wrong.  [SQLITE_RANGE] is returned if the parameter
       
  3129 ** index is out of range.  [SQLITE_NOMEM] is returned if malloc() fails.
       
  3130 ** [SQLITE_MISUSE] might be returned if these routines are called on a
       
  3131 ** virtual machine that is the wrong state or which has already been finalized.
       
  3132 ** Detection of misuse is unreliable.  Applications should not depend
       
  3133 ** on SQLITE_MISUSE returns.  SQLITE_MISUSE is intended to indicate a
       
  3134 ** a logic error in the application.  Future versions of SQLite might
       
  3135 ** panic rather than return SQLITE_MISUSE.
       
  3136 **
       
  3137 ** See also: [sqlite3_bind_parameter_count()],
       
  3138 ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
       
  3139 **
       
  3140 ** Requirements:
       
  3141 ** [H13506] [H13509] [H13512] [H13515] [H13518] [H13521] [H13524] [H13527]
       
  3142 ** [H13530] [H13533] [H13536] [H13539] [H13542] [H13545] [H13548] [H13551]
       
  3143 **
       
  3144 */
       
  3145 SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
       
  3146 SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
       
  3147 SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
       
  3148 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
       
  3149 SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
       
  3150 SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
       
  3151 SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
       
  3152 SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
       
  3153 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
       
  3154 
       
  3155 /*
       
  3156 ** CAPI3REF: Number Of SQL Parameters {H13600} <S70300>
       
  3157 **
       
  3158 ** This routine can be used to find the number of [SQL parameters]
       
  3159 ** in a [prepared statement].  SQL parameters are tokens of the
       
  3160 ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
       
  3161 ** placeholders for values that are [sqlite3_bind_blob | bound]
       
  3162 ** to the parameters at a later time.
       
  3163 **
       
  3164 ** This routine actually returns the index of the largest (rightmost)
       
  3165 ** parameter. For all forms except ?NNN, this will correspond to the
       
  3166 ** number of unique parameters.  If parameters of the ?NNN are used,
       
  3167 ** there may be gaps in the list.
       
  3168 **
       
  3169 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
       
  3170 ** [sqlite3_bind_parameter_name()], and
       
  3171 ** [sqlite3_bind_parameter_index()].
       
  3172 **
       
  3173 ** Requirements:
       
  3174 ** [H13601]
       
  3175 */
       
  3176 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
       
  3177 
       
  3178 /*
       
  3179 ** CAPI3REF: Name Of A Host Parameter {H13620} <S70300>
       
  3180 **
       
  3181 ** This routine returns a pointer to the name of the n-th
       
  3182 ** [SQL parameter] in a [prepared statement].
       
  3183 ** SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
       
  3184 ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
       
  3185 ** respectively.
       
  3186 ** In other words, the initial ":" or "$" or "@" or "?"
       
  3187 ** is included as part of the name.
       
  3188 ** Parameters of the form "?" without a following integer have no name
       
  3189 ** and are also referred to as "anonymous parameters".
       
  3190 **
       
  3191 ** The first host parameter has an index of 1, not 0.
       
  3192 **
       
  3193 ** If the value n is out of range or if the n-th parameter is
       
  3194 ** nameless, then NULL is returned.  The returned string is
       
  3195 ** always in UTF-8 encoding even if the named parameter was
       
  3196 ** originally specified as UTF-16 in [sqlite3_prepare16()] or
       
  3197 ** [sqlite3_prepare16_v2()].
       
  3198 **
       
  3199 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
       
  3200 ** [sqlite3_bind_parameter_count()], and
       
  3201 ** [sqlite3_bind_parameter_index()].
       
  3202 **
       
  3203 ** Requirements:
       
  3204 ** [H13621]
       
  3205 */
       
  3206 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
       
  3207 
       
  3208 /*
       
  3209 ** CAPI3REF: Index Of A Parameter With A Given Name {H13640} <S70300>
       
  3210 **
       
  3211 ** Return the index of an SQL parameter given its name.  The
       
  3212 ** index value returned is suitable for use as the second
       
  3213 ** parameter to [sqlite3_bind_blob|sqlite3_bind()].  A zero
       
  3214 ** is returned if no matching parameter is found.  The parameter
       
  3215 ** name must be given in UTF-8 even if the original statement
       
  3216 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
       
  3217 **
       
  3218 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
       
  3219 ** [sqlite3_bind_parameter_count()], and
       
  3220 ** [sqlite3_bind_parameter_index()].
       
  3221 **
       
  3222 ** Requirements:
       
  3223 ** [H13641]
       
  3224 */
       
  3225 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
       
  3226 
       
  3227 /*
       
  3228 ** CAPI3REF: Reset All Bindings On A Prepared Statement {H13660} <S70300>
       
  3229 **
       
  3230 ** Contrary to the intuition of many, [sqlite3_reset()] does not reset
       
  3231 ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
       
  3232 ** Use this routine to reset all host parameters to NULL.
       
  3233 **
       
  3234 ** Requirements:
       
  3235 ** [H13661]
       
  3236 */
       
  3237 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
       
  3238 
       
  3239 /*
       
  3240 ** CAPI3REF: Number Of Columns In A Result Set {H13710} <S10700>
       
  3241 **
       
  3242 ** Return the number of columns in the result set returned by the
       
  3243 ** [prepared statement]. This routine returns 0 if pStmt is an SQL
       
  3244 ** statement that does not return data (for example an [UPDATE]).
       
  3245 **
       
  3246 ** Requirements:
       
  3247 ** [H13711]
       
  3248 */
       
  3249 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
       
  3250 
       
  3251 /*
       
  3252 ** CAPI3REF: Column Names In A Result Set {H13720} <S10700>
       
  3253 **
       
  3254 ** These routines return the name assigned to a particular column
       
  3255 ** in the result set of a [SELECT] statement.  The sqlite3_column_name()
       
  3256 ** interface returns a pointer to a zero-terminated UTF-8 string
       
  3257 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
       
  3258 ** UTF-16 string.  The first parameter is the [prepared statement]
       
  3259 ** that implements the [SELECT] statement. The second parameter is the
       
  3260 ** column number.  The leftmost column is number 0.
       
  3261 **
       
  3262 ** The returned string pointer is valid until either the [prepared statement]
       
  3263 ** is destroyed by [sqlite3_finalize()] or until the next call to
       
  3264 ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
       
  3265 **
       
  3266 ** If sqlite3_malloc() fails during the processing of either routine
       
  3267 ** (for example during a conversion from UTF-8 to UTF-16) then a
       
  3268 ** NULL pointer is returned.
       
  3269 **
       
  3270 ** The name of a result column is the value of the "AS" clause for
       
  3271 ** that column, if there is an AS clause.  If there is no AS clause
       
  3272 ** then the name of the column is unspecified and may change from
       
  3273 ** one release of SQLite to the next.
       
  3274 **
       
  3275 ** Requirements:
       
  3276 ** [H13721] [H13723] [H13724] [H13725] [H13726] [H13727]
       
  3277 */
       
  3278 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
       
  3279 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
       
  3280 
       
  3281 /*
       
  3282 ** CAPI3REF: Source Of Data In A Query Result {H13740} <S10700>
       
  3283 **
       
  3284 ** These routines provide a means to determine what column of what
       
  3285 ** table in which database a result of a [SELECT] statement comes from.
       
  3286 ** The name of the database or table or column can be returned as
       
  3287 ** either a UTF-8 or UTF-16 string.  The _database_ routines return
       
  3288 ** the database name, the _table_ routines return the table name, and
       
  3289 ** the origin_ routines return the column name.
       
  3290 ** The returned string is valid until the [prepared statement] is destroyed
       
  3291 ** using [sqlite3_finalize()] or until the same information is requested
       
  3292 ** again in a different encoding.
       
  3293 **
       
  3294 ** The names returned are the original un-aliased names of the
       
  3295 ** database, table, and column.
       
  3296 **
       
  3297 ** The first argument to the following calls is a [prepared statement].
       
  3298 ** These functions return information about the Nth column returned by
       
  3299 ** the statement, where N is the second function argument.
       
  3300 **
       
  3301 ** If the Nth column returned by the statement is an expression or
       
  3302 ** subquery and is not a column value, then all of these functions return
       
  3303 ** NULL.  These routine might also return NULL if a memory allocation error
       
  3304 ** occurs.  Otherwise, they return the name of the attached database, table
       
  3305 ** and column that query result column was extracted from.
       
  3306 **
       
  3307 ** As with all other SQLite APIs, those postfixed with "16" return
       
  3308 ** UTF-16 encoded strings, the other functions return UTF-8. {END}
       
  3309 **
       
  3310 ** These APIs are only available if the library was compiled with the
       
  3311 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
       
  3312 **
       
  3313 ** {A13751}
       
  3314 ** If two or more threads call one or more of these routines against the same
       
  3315 ** prepared statement and column at the same time then the results are
       
  3316 ** undefined.
       
  3317 **
       
  3318 ** Requirements:
       
  3319 ** [H13741] [H13742] [H13743] [H13744] [H13745] [H13746] [H13748]
       
  3320 **
       
  3321 ** If two or more threads call one or more
       
  3322 ** [sqlite3_column_database_name | column metadata interfaces]
       
  3323 ** for the same [prepared statement] and result column
       
  3324 ** at the same time then the results are undefined.
       
  3325 */
       
  3326 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
       
  3327 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
       
  3328 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
       
  3329 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
       
  3330 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
       
  3331 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
       
  3332 
       
  3333 /*
       
  3334 ** CAPI3REF: Declared Datatype Of A Query Result {H13760} <S10700>
       
  3335 **
       
  3336 ** The first parameter is a [prepared statement].
       
  3337 ** If this statement is a [SELECT] statement and the Nth column of the
       
  3338 ** returned result set of that [SELECT] is a table column (not an
       
  3339 ** expression or subquery) then the declared type of the table
       
  3340 ** column is returned.  If the Nth column of the result set is an
       
  3341 ** expression or subquery, then a NULL pointer is returned.
       
  3342 ** The returned string is always UTF-8 encoded. {END}
       
  3343 **
       
  3344 ** For example, given the database schema:
       
  3345 **
       
  3346 ** CREATE TABLE t1(c1 VARIANT);
       
  3347 **
       
  3348 ** and the following statement to be compiled:
       
  3349 **
       
  3350 ** SELECT c1 + 1, c1 FROM t1;
       
  3351 **
       
  3352 ** this routine would return the string "VARIANT" for the second result
       
  3353 ** column (i==1), and a NULL pointer for the first result column (i==0).
       
  3354 **
       
  3355 ** SQLite uses dynamic run-time typing.  So just because a column
       
  3356 ** is declared to contain a particular type does not mean that the
       
  3357 ** data stored in that column is of the declared type.  SQLite is
       
  3358 ** strongly typed, but the typing is dynamic not static.  Type
       
  3359 ** is associated with individual values, not with the containers
       
  3360 ** used to hold those values.
       
  3361 **
       
  3362 ** Requirements:
       
  3363 ** [H13761] [H13762] [H13763]
       
  3364 */
       
  3365 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
       
  3366 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
       
  3367 
       
  3368 /*
       
  3369 ** CAPI3REF: Evaluate An SQL Statement {H13200} <S10000>
       
  3370 **
       
  3371 ** After a [prepared statement] has been prepared using either
       
  3372 ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
       
  3373 ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
       
  3374 ** must be called one or more times to evaluate the statement.
       
  3375 **
       
  3376 ** The details of the behavior of the sqlite3_step() interface depend
       
  3377 ** on whether the statement was prepared using the newer "v2" interface
       
  3378 ** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
       
  3379 ** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
       
  3380 ** new "v2" interface is recommended for new applications but the legacy
       
  3381 ** interface will continue to be supported.
       
  3382 **
       
  3383 ** In the legacy interface, the return value will be either [SQLITE_BUSY],
       
  3384 ** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
       
  3385 ** With the "v2" interface, any of the other [result codes] or
       
  3386 ** [extended result codes] might be returned as well.
       
  3387 **
       
  3388 ** [SQLITE_BUSY] means that the database engine was unable to acquire the
       
  3389 ** database locks it needs to do its job.  If the statement is a [COMMIT]
       
  3390 ** or occurs outside of an explicit transaction, then you can retry the
       
  3391 ** statement.  If the statement is not a [COMMIT] and occurs within a
       
  3392 ** explicit transaction then you should rollback the transaction before
       
  3393 ** continuing.
       
  3394 **
       
  3395 ** [SQLITE_DONE] means that the statement has finished executing
       
  3396 ** successfully.  sqlite3_step() should not be called again on this virtual
       
  3397 ** machine without first calling [sqlite3_reset()] to reset the virtual
       
  3398 ** machine back to its initial state.
       
  3399 **
       
  3400 ** If the SQL statement being executed returns any data, then [SQLITE_ROW]
       
  3401 ** is returned each time a new row of data is ready for processing by the
       
  3402 ** caller. The values may be accessed using the [column access functions].
       
  3403 ** sqlite3_step() is called again to retrieve the next row of data.
       
  3404 **
       
  3405 ** [SQLITE_ERROR] means that a run-time error (such as a constraint
       
  3406 ** violation) has occurred.  sqlite3_step() should not be called again on
       
  3407 ** the VM. More information may be found by calling [sqlite3_errmsg()].
       
  3408 ** With the legacy interface, a more specific error code (for example,
       
  3409 ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
       
  3410 ** can be obtained by calling [sqlite3_reset()] on the
       
  3411 ** [prepared statement].  In the "v2" interface,
       
  3412 ** the more specific error code is returned directly by sqlite3_step().
       
  3413 **
       
  3414 ** [SQLITE_MISUSE] means that the this routine was called inappropriately.
       
  3415 ** Perhaps it was called on a [prepared statement] that has
       
  3416 ** already been [sqlite3_finalize | finalized] or on one that had
       
  3417 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
       
  3418 ** be the case that the same database connection is being used by two or
       
  3419 ** more threads at the same moment in time.
       
  3420 **
       
  3421 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
       
  3422 ** API always returns a generic error code, [SQLITE_ERROR], following any
       
  3423 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
       
  3424 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
       
  3425 ** specific [error codes] that better describes the error.
       
  3426 ** We admit that this is a goofy design.  The problem has been fixed
       
  3427 ** with the "v2" interface.  If you prepare all of your SQL statements
       
  3428 ** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
       
  3429 ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
       
  3430 ** then the more specific [error codes] are returned directly
       
  3431 ** by sqlite3_step().  The use of the "v2" interface is recommended.
       
  3432 **
       
  3433 ** Requirements:
       
  3434 ** [H13202] [H15304] [H15306] [H15308] [H15310]
       
  3435 */
       
  3436 SQLITE_API int sqlite3_step(sqlite3_stmt*);
       
  3437 
       
  3438 /*
       
  3439 ** CAPI3REF: Number of columns in a result set {H13770} <S10700>
       
  3440 **
       
  3441 ** Returns the number of values in the current row of the result set.
       
  3442 **
       
  3443 ** Requirements:
       
  3444 ** [H13771] [H13772]
       
  3445 */
       
  3446 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
       
  3447 
       
  3448 /*
       
  3449 ** CAPI3REF: Fundamental Datatypes {H10265} <S10110><S10120>
       
  3450 ** KEYWORDS: SQLITE_TEXT
       
  3451 **
       
  3452 ** {H10266} Every value in SQLite has one of five fundamental datatypes:
       
  3453 **
       
  3454 ** <ul>
       
  3455 ** <li> 64-bit signed integer
       
  3456 ** <li> 64-bit IEEE floating point number
       
  3457 ** <li> string
       
  3458 ** <li> BLOB
       
  3459 ** <li> NULL
       
  3460 ** </ul> {END}
       
  3461 **
       
  3462 ** These constants are codes for each of those types.
       
  3463 **
       
  3464 ** Note that the SQLITE_TEXT constant was also used in SQLite version 2
       
  3465 ** for a completely different meaning.  Software that links against both
       
  3466 ** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
       
  3467 ** SQLITE_TEXT.
       
  3468 */
       
  3469 #define SQLITE_INTEGER  1
       
  3470 #define SQLITE_FLOAT    2
       
  3471 #define SQLITE_BLOB     4
       
  3472 #define SQLITE_NULL     5
       
  3473 #ifdef SQLITE_TEXT
       
  3474 # undef SQLITE_TEXT
       
  3475 #else
       
  3476 # define SQLITE_TEXT     3
       
  3477 #endif
       
  3478 #define SQLITE3_TEXT     3
       
  3479 
       
  3480 /*
       
  3481 ** CAPI3REF: Result Values From A Query {H13800} <S10700>
       
  3482 ** KEYWORDS: {column access functions}
       
  3483 **
       
  3484 ** These routines form the "result set query" interface.
       
  3485 **
       
  3486 ** These routines return information about a single column of the current
       
  3487 ** result row of a query.  In every case the first argument is a pointer
       
  3488 ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
       
  3489 ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
       
  3490 ** and the second argument is the index of the column for which information
       
  3491 ** should be returned.  The leftmost column of the result set has the index 0.
       
  3492 **
       
  3493 ** If the SQL statement does not currently point to a valid row, or if the
       
  3494 ** column index is out of range, the result is undefined.
       
  3495 ** These routines may only be called when the most recent call to
       
  3496 ** [sqlite3_step()] has returned [SQLITE_ROW] and neither
       
  3497 ** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
       
  3498 ** If any of these routines are called after [sqlite3_reset()] or
       
  3499 ** [sqlite3_finalize()] or after [sqlite3_step()] has returned
       
  3500 ** something other than [SQLITE_ROW], the results are undefined.
       
  3501 ** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
       
  3502 ** are called from a different thread while any of these routines
       
  3503 ** are pending, then the results are undefined.
       
  3504 **
       
  3505 ** The sqlite3_column_type() routine returns the
       
  3506 ** [SQLITE_INTEGER | datatype code] for the initial data type
       
  3507 ** of the result column.  The returned value is one of [SQLITE_INTEGER],
       
  3508 ** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
       
  3509 ** returned by sqlite3_column_type() is only meaningful if no type
       
  3510 ** conversions have occurred as described below.  After a type conversion,
       
  3511 ** the value returned by sqlite3_column_type() is undefined.  Future
       
  3512 ** versions of SQLite may change the behavior of sqlite3_column_type()
       
  3513 ** following a type conversion.
       
  3514 **
       
  3515 ** If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
       
  3516 ** routine returns the number of bytes in that BLOB or string.
       
  3517 ** If the result is a UTF-16 string, then sqlite3_column_bytes() converts
       
  3518 ** the string to UTF-8 and then returns the number of bytes.
       
  3519 ** If the result is a numeric value then sqlite3_column_bytes() uses
       
  3520 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
       
  3521 ** the number of bytes in that string.
       
  3522 ** The value returned does not include the zero terminator at the end
       
  3523 ** of the string.  For clarity: the value returned is the number of
       
  3524 ** bytes in the string, not the number of characters.
       
  3525 **
       
  3526 ** Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
       
  3527 ** even empty strings, are always zero terminated.  The return
       
  3528 ** value from sqlite3_column_blob() for a zero-length BLOB is an arbitrary
       
  3529 ** pointer, possibly even a NULL pointer.
       
  3530 **
       
  3531 ** The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes()
       
  3532 ** but leaves the result in UTF-16 in native byte order instead of UTF-8.
       
  3533 ** The zero terminator is not included in this count.
       
  3534 **
       
  3535 ** The object returned by [sqlite3_column_value()] is an
       
  3536 ** [unprotected sqlite3_value] object.  An unprotected sqlite3_value object
       
  3537 ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
       
  3538 ** If the [unprotected sqlite3_value] object returned by
       
  3539 ** [sqlite3_column_value()] is used in any other way, including calls
       
  3540 ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
       
  3541 ** or [sqlite3_value_bytes()], then the behavior is undefined.
       
  3542 **
       
  3543 ** These routines attempt to convert the value where appropriate.  For
       
  3544 ** example, if the internal representation is FLOAT and a text result
       
  3545 ** is requested, [sqlite3_snprintf()] is used internally to perform the
       
  3546 ** conversion automatically.  The following table details the conversions
       
  3547 ** that are applied:
       
  3548 **
       
  3549 ** <blockquote>
       
  3550 ** <table border="1">
       
  3551 ** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
       
  3552 **
       
  3553 ** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
       
  3554 ** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
       
  3555 ** <tr><td>  NULL    <td>   TEXT    <td> Result is NULL pointer
       
  3556 ** <tr><td>  NULL    <td>   BLOB    <td> Result is NULL pointer
       
  3557 ** <tr><td> INTEGER  <td>  FLOAT    <td> Convert from integer to float
       
  3558 ** <tr><td> INTEGER  <td>   TEXT    <td> ASCII rendering of the integer
       
  3559 ** <tr><td> INTEGER  <td>   BLOB    <td> Same as INTEGER->TEXT
       
  3560 ** <tr><td>  FLOAT   <td> INTEGER   <td> Convert from float to integer
       
  3561 ** <tr><td>  FLOAT   <td>   TEXT    <td> ASCII rendering of the float
       
  3562 ** <tr><td>  FLOAT   <td>   BLOB    <td> Same as FLOAT->TEXT
       
  3563 ** <tr><td>  TEXT    <td> INTEGER   <td> Use atoi()
       
  3564 ** <tr><td>  TEXT    <td>  FLOAT    <td> Use atof()
       
  3565 ** <tr><td>  TEXT    <td>   BLOB    <td> No change
       
  3566 ** <tr><td>  BLOB    <td> INTEGER   <td> Convert to TEXT then use atoi()
       
  3567 ** <tr><td>  BLOB    <td>  FLOAT    <td> Convert to TEXT then use atof()
       
  3568 ** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
       
  3569 ** </table>
       
  3570 ** </blockquote>
       
  3571 **
       
  3572 ** The table above makes reference to standard C library functions atoi()
       
  3573 ** and atof().  SQLite does not really use these functions.  It has its
       
  3574 ** own equivalent internal routines.  The atoi() and atof() names are
       
  3575 ** used in the table for brevity and because they are familiar to most
       
  3576 ** C programmers.
       
  3577 **
       
  3578 ** Note that when type conversions occur, pointers returned by prior
       
  3579 ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
       
  3580 ** sqlite3_column_text16() may be invalidated.
       
  3581 ** Type conversions and pointer invalidations might occur
       
  3582 ** in the following cases:
       
  3583 **
       
  3584 ** <ul>
       
  3585 ** <li> The initial content is a BLOB and sqlite3_column_text() or
       
  3586 **      sqlite3_column_text16() is called.  A zero-terminator might
       
  3587 **      need to be added to the string.</li>
       
  3588 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
       
  3589 **      sqlite3_column_text16() is called.  The content must be converted
       
  3590 **      to UTF-16.</li>
       
  3591 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
       
  3592 **      sqlite3_column_text() is called.  The content must be converted
       
  3593 **      to UTF-8.</li>
       
  3594 ** </ul>
       
  3595 **
       
  3596 ** Conversions between UTF-16be and UTF-16le are always done in place and do
       
  3597 ** not invalidate a prior pointer, though of course the content of the buffer
       
  3598 ** that the prior pointer points to will have been modified.  Other kinds
       
  3599 ** of conversion are done in place when it is possible, but sometimes they
       
  3600 ** are not possible and in those cases prior pointers are invalidated.
       
  3601 **
       
  3602 ** The safest and easiest to remember policy is to invoke these routines
       
  3603 ** in one of the following ways:
       
  3604 **
       
  3605 ** <ul>
       
  3606 **  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
       
  3607 **  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
       
  3608 **  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
       
  3609 ** </ul>
       
  3610 **
       
  3611 ** In other words, you should call sqlite3_column_text(),
       
  3612 ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
       
  3613 ** into the desired format, then invoke sqlite3_column_bytes() or
       
  3614 ** sqlite3_column_bytes16() to find the size of the result.  Do not mix calls
       
  3615 ** to sqlite3_column_text() or sqlite3_column_blob() with calls to
       
  3616 ** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
       
  3617 ** with calls to sqlite3_column_bytes().
       
  3618 **
       
  3619 ** The pointers returned are valid until a type conversion occurs as
       
  3620 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
       
  3621 ** [sqlite3_finalize()] is called.  The memory space used to hold strings
       
  3622 ** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
       
  3623 ** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
       
  3624 ** [sqlite3_free()].
       
  3625 **
       
  3626 ** If a memory allocation error occurs during the evaluation of any
       
  3627 ** of these routines, a default value is returned.  The default value
       
  3628 ** is either the integer 0, the floating point number 0.0, or a NULL
       
  3629 ** pointer.  Subsequent calls to [sqlite3_errcode()] will return
       
  3630 ** [SQLITE_NOMEM].
       
  3631 **
       
  3632 ** Requirements:
       
  3633 ** [H13803] [H13806] [H13809] [H13812] [H13815] [H13818] [H13821] [H13824]
       
  3634 ** [H13827] [H13830]
       
  3635 */
       
  3636 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
       
  3637 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
       
  3638 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
       
  3639 SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
       
  3640 SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
       
  3641 SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
       
  3642 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
       
  3643 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
       
  3644 SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
       
  3645 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
       
  3646 
       
  3647 /*
       
  3648 ** CAPI3REF: Destroy A Prepared Statement Object {H13300} <S70300><S30100>
       
  3649 **
       
  3650 ** The sqlite3_finalize() function is called to delete a [prepared statement].
       
  3651 ** If the statement was executed successfully or not executed at all, then
       
  3652 ** SQLITE_OK is returned. If execution of the statement failed then an
       
  3653 ** [error code] or [extended error code] is returned.
       
  3654 **
       
  3655 ** This routine can be called at any point during the execution of the
       
  3656 ** [prepared statement].  If the virtual machine has not
       
  3657 ** completed execution when this routine is called, that is like
       
  3658 ** encountering an error or an [sqlite3_interrupt | interrupt].
       
  3659 ** Incomplete updates may be rolled back and transactions canceled,
       
  3660 ** depending on the circumstances, and the
       
  3661 ** [error code] returned will be [SQLITE_ABORT].
       
  3662 **
       
  3663 ** Requirements:
       
  3664 ** [H11302] [H11304]
       
  3665 */
       
  3666 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
       
  3667 
       
  3668 /*
       
  3669 ** CAPI3REF: Reset A Prepared Statement Object {H13330} <S70300>
       
  3670 **
       
  3671 ** The sqlite3_reset() function is called to reset a [prepared statement]
       
  3672 ** object back to its initial state, ready to be re-executed.
       
  3673 ** Any SQL statement variables that had values bound to them using
       
  3674 ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
       
  3675 ** Use [sqlite3_clear_bindings()] to reset the bindings.
       
  3676 **
       
  3677 ** {H11332} The [sqlite3_reset(S)] interface resets the [prepared statement] S
       
  3678 **          back to the beginning of its program.
       
  3679 **
       
  3680 ** {H11334} If the most recent call to [sqlite3_step(S)] for the
       
  3681 **          [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
       
  3682 **          or if [sqlite3_step(S)] has never before been called on S,
       
  3683 **          then [sqlite3_reset(S)] returns [SQLITE_OK].
       
  3684 **
       
  3685 ** {H11336} If the most recent call to [sqlite3_step(S)] for the
       
  3686 **          [prepared statement] S indicated an error, then
       
  3687 **          [sqlite3_reset(S)] returns an appropriate [error code].
       
  3688 **
       
  3689 ** {H11338} The [sqlite3_reset(S)] interface does not change the values
       
  3690 **          of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
       
  3691 */
       
  3692 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
       
  3693 
       
  3694 /*
       
  3695 ** CAPI3REF: Create Or Redefine SQL Functions {H16100} <S20200>
       
  3696 ** KEYWORDS: {function creation routines}
       
  3697 ** KEYWORDS: {application-defined SQL function}
       
  3698 ** KEYWORDS: {application-defined SQL functions}
       
  3699 **
       
  3700 ** These two functions (collectively known as "function creation routines")
       
  3701 ** are used to add SQL functions or aggregates or to redefine the behavior
       
  3702 ** of existing SQL functions or aggregates.  The only difference between the
       
  3703 ** two is that the second parameter, the name of the (scalar) function or
       
  3704 ** aggregate, is encoded in UTF-8 for sqlite3_create_function() and UTF-16
       
  3705 ** for sqlite3_create_function16().
       
  3706 **
       
  3707 ** The first parameter is the [database connection] to which the SQL
       
  3708 ** function is to be added.  If a single program uses more than one database
       
  3709 ** connection internally, then SQL functions must be added individually to
       
  3710 ** each database connection.
       
  3711 **
       
  3712 ** The second parameter is the name of the SQL function to be created or
       
  3713 ** redefined.  The length of the name is limited to 255 bytes, exclusive of
       
  3714 ** the zero-terminator.  Note that the name length limit is in bytes, not
       
  3715 ** characters.  Any attempt to create a function with a longer name
       
  3716 ** will result in [SQLITE_ERROR] being returned.
       
  3717 **
       
  3718 ** The third parameter (nArg)
       
  3719 ** is the number of arguments that the SQL function or
       
  3720 ** aggregate takes. If this parameter is -1, then the SQL function or
       
  3721 ** aggregate may take any number of arguments between 0 and the limit
       
  3722 ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
       
  3723 ** parameter is less than -1 or greater than 127 then the behavior is
       
  3724 ** undefined.
       
  3725 **
       
  3726 ** The fourth parameter, eTextRep, specifies what
       
  3727 ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
       
  3728 ** its parameters.  Any SQL function implementation should be able to work
       
  3729 ** work with UTF-8, UTF-16le, or UTF-16be.  But some implementations may be
       
  3730 ** more efficient with one encoding than another.  An application may
       
  3731 ** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
       
  3732 ** times with the same function but with different values of eTextRep.
       
  3733 ** When multiple implementations of the same function are available, SQLite
       
  3734 ** will pick the one that involves the least amount of data conversion.
       
  3735 ** If there is only a single implementation which does not care what text
       
  3736 ** encoding is used, then the fourth argument should be [SQLITE_ANY].
       
  3737 **
       
  3738 ** The fifth parameter is an arbitrary pointer.  The implementation of the
       
  3739 ** function can gain access to this pointer using [sqlite3_user_data()].
       
  3740 **
       
  3741 ** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are
       
  3742 ** pointers to C-language functions that implement the SQL function or
       
  3743 ** aggregate. A scalar SQL function requires an implementation of the xFunc
       
  3744 ** callback only, NULL pointers should be passed as the xStep and xFinal
       
  3745 ** parameters. An aggregate SQL function requires an implementation of xStep
       
  3746 ** and xFinal and NULL should be passed for xFunc. To delete an existing
       
  3747 ** SQL function or aggregate, pass NULL for all three function callbacks.
       
  3748 **
       
  3749 ** It is permitted to register multiple implementations of the same
       
  3750 ** functions with the same name but with either differing numbers of
       
  3751 ** arguments or differing preferred text encodings.  SQLite will use
       
  3752 ** the implementation that most closely matches the way in which the
       
  3753 ** SQL function is used.  A function implementation with a non-negative
       
  3754 ** nArg parameter is a better match than a function implementation with
       
  3755 ** a negative nArg.  A function where the preferred text encoding
       
  3756 ** matches the database encoding is a better
       
  3757 ** match than a function where the encoding is different.  
       
  3758 ** A function where the encoding difference is between UTF16le and UTF16be
       
  3759 ** is a closer match than a function where the encoding difference is
       
  3760 ** between UTF8 and UTF16.
       
  3761 **
       
  3762 ** Built-in functions may be overloaded by new application-defined functions.
       
  3763 ** The first application-defined function with a given name overrides all
       
  3764 ** built-in functions in the same [database connection] with the same name.
       
  3765 ** Subsequent application-defined functions of the same name only override 
       
  3766 ** prior application-defined functions that are an exact match for the
       
  3767 ** number of parameters and preferred encoding.
       
  3768 **
       
  3769 ** An application-defined function is permitted to call other
       
  3770 ** SQLite interfaces.  However, such calls must not
       
  3771 ** close the database connection nor finalize or reset the prepared
       
  3772 ** statement in which the function is running.
       
  3773 **
       
  3774 ** Requirements:
       
  3775 ** [H16103] [H16106] [H16109] [H16112] [H16118] [H16121] [H16127]
       
  3776 ** [H16130] [H16133] [H16136] [H16139] [H16142]
       
  3777 */
       
  3778 SQLITE_API int sqlite3_create_function(
       
  3779   sqlite3 *db,
       
  3780   const char *zFunctionName,
       
  3781   int nArg,
       
  3782   int eTextRep,
       
  3783   void *pApp,
       
  3784   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
       
  3785   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
       
  3786   void (*xFinal)(sqlite3_context*)
       
  3787 );
       
  3788 SQLITE_API int sqlite3_create_function16(
       
  3789   sqlite3 *db,
       
  3790   const void *zFunctionName,
       
  3791   int nArg,
       
  3792   int eTextRep,
       
  3793   void *pApp,
       
  3794   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
       
  3795   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
       
  3796   void (*xFinal)(sqlite3_context*)
       
  3797 );
       
  3798 
       
  3799 /*
       
  3800 ** CAPI3REF: Text Encodings {H10267} <S50200> <H16100>
       
  3801 **
       
  3802 ** These constant define integer codes that represent the various
       
  3803 ** text encodings supported by SQLite.
       
  3804 */
       
  3805 #define SQLITE_UTF8           1
       
  3806 #define SQLITE_UTF16LE        2
       
  3807 #define SQLITE_UTF16BE        3
       
  3808 #define SQLITE_UTF16          4    /* Use native byte order */
       
  3809 #define SQLITE_ANY            5    /* sqlite3_create_function only */
       
  3810 #define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
       
  3811 
       
  3812 /*
       
  3813 ** CAPI3REF: Deprecated Functions
       
  3814 ** DEPRECATED
       
  3815 **
       
  3816 ** These functions are [deprecated].  In order to maintain
       
  3817 ** backwards compatibility with older code, these functions continue 
       
  3818 ** to be supported.  However, new applications should avoid
       
  3819 ** the use of these functions.  To help encourage people to avoid
       
  3820 ** using these functions, we are not going to tell you what they do.
       
  3821 */
       
  3822 #ifndef SQLITE_OMIT_DEPRECATED
       
  3823 SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
       
  3824 SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
       
  3825 SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
       
  3826 SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
       
  3827 SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
       
  3828 SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64);
       
  3829 #endif
       
  3830 
       
  3831 /*
       
  3832 ** CAPI3REF: Obtaining SQL Function Parameter Values {H15100} <S20200>
       
  3833 **
       
  3834 ** The C-language implementation of SQL functions and aggregates uses
       
  3835 ** this set of interface routines to access the parameter values on
       
  3836 ** the function or aggregate.
       
  3837 **
       
  3838 ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
       
  3839 ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
       
  3840 ** define callbacks that implement the SQL functions and aggregates.
       
  3841 ** The 4th parameter to these callbacks is an array of pointers to
       
  3842 ** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
       
  3843 ** each parameter to the SQL function.  These routines are used to
       
  3844 ** extract values from the [sqlite3_value] objects.
       
  3845 **
       
  3846 ** These routines work only with [protected sqlite3_value] objects.
       
  3847 ** Any attempt to use these routines on an [unprotected sqlite3_value]
       
  3848 ** object results in undefined behavior.
       
  3849 **
       
  3850 ** These routines work just like the corresponding [column access functions]
       
  3851 ** except that  these routines take a single [protected sqlite3_value] object
       
  3852 ** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
       
  3853 **
       
  3854 ** The sqlite3_value_text16() interface extracts a UTF-16 string
       
  3855 ** in the native byte-order of the host machine.  The
       
  3856 ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
       
  3857 ** extract UTF-16 strings as big-endian and little-endian respectively.
       
  3858 **
       
  3859 ** The sqlite3_value_numeric_type() interface attempts to apply
       
  3860 ** numeric affinity to the value.  This means that an attempt is
       
  3861 ** made to convert the value to an integer or floating point.  If
       
  3862 ** such a conversion is possible without loss of information (in other
       
  3863 ** words, if the value is a string that looks like a number)
       
  3864 ** then the conversion is performed.  Otherwise no conversion occurs.
       
  3865 ** The [SQLITE_INTEGER | datatype] after conversion is returned.
       
  3866 **
       
  3867 ** Please pay particular attention to the fact that the pointer returned
       
  3868 ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
       
  3869 ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
       
  3870 ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
       
  3871 ** or [sqlite3_value_text16()].
       
  3872 **
       
  3873 ** These routines must be called from the same thread as
       
  3874 ** the SQL function that supplied the [sqlite3_value*] parameters.
       
  3875 **
       
  3876 ** Requirements:
       
  3877 ** [H15103] [H15106] [H15109] [H15112] [H15115] [H15118] [H15121] [H15124]
       
  3878 ** [H15127] [H15130] [H15133] [H15136]
       
  3879 */
       
  3880 SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
       
  3881 SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
       
  3882 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
       
  3883 SQLITE_API double sqlite3_value_double(sqlite3_value*);
       
  3884 SQLITE_API int sqlite3_value_int(sqlite3_value*);
       
  3885 SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
       
  3886 SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
       
  3887 SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
       
  3888 SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
       
  3889 SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
       
  3890 SQLITE_API int sqlite3_value_type(sqlite3_value*);
       
  3891 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
       
  3892 
       
  3893 /*
       
  3894 ** CAPI3REF: Obtain Aggregate Function Context {H16210} <S20200>
       
  3895 **
       
  3896 ** The implementation of aggregate SQL functions use this routine to allocate
       
  3897 ** a structure for storing their state.
       
  3898 **
       
  3899 ** The first time the sqlite3_aggregate_context() routine is called for a
       
  3900 ** particular aggregate, SQLite allocates nBytes of memory, zeroes out that
       
  3901 ** memory, and returns a pointer to it. On second and subsequent calls to
       
  3902 ** sqlite3_aggregate_context() for the same aggregate function index,
       
  3903 ** the same buffer is returned. The implementation of the aggregate can use
       
  3904 ** the returned buffer to accumulate data.
       
  3905 **
       
  3906 ** SQLite automatically frees the allocated buffer when the aggregate
       
  3907 ** query concludes.
       
  3908 **
       
  3909 ** The first parameter should be a copy of the
       
  3910 ** [sqlite3_context | SQL function context] that is the first parameter
       
  3911 ** to the callback routine that implements the aggregate function.
       
  3912 **
       
  3913 ** This routine must be called from the same thread in which
       
  3914 ** the aggregate SQL function is running.
       
  3915 **
       
  3916 ** Requirements:
       
  3917 ** [H16211] [H16213] [H16215] [H16217]
       
  3918 */
       
  3919 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
       
  3920 
       
  3921 /*
       
  3922 ** CAPI3REF: User Data For Functions {H16240} <S20200>
       
  3923 **
       
  3924 ** The sqlite3_user_data() interface returns a copy of
       
  3925 ** the pointer that was the pUserData parameter (the 5th parameter)
       
  3926 ** of the [sqlite3_create_function()]
       
  3927 ** and [sqlite3_create_function16()] routines that originally
       
  3928 ** registered the application defined function. {END}
       
  3929 **
       
  3930 ** This routine must be called from the same thread in which
       
  3931 ** the application-defined function is running.
       
  3932 **
       
  3933 ** Requirements:
       
  3934 ** [H16243]
       
  3935 */
       
  3936 SQLITE_API void *sqlite3_user_data(sqlite3_context*);
       
  3937 
       
  3938 /*
       
  3939 ** CAPI3REF: Database Connection For Functions {H16250} <S60600><S20200>
       
  3940 **
       
  3941 ** The sqlite3_context_db_handle() interface returns a copy of
       
  3942 ** the pointer to the [database connection] (the 1st parameter)
       
  3943 ** of the [sqlite3_create_function()]
       
  3944 ** and [sqlite3_create_function16()] routines that originally
       
  3945 ** registered the application defined function.
       
  3946 **
       
  3947 ** Requirements:
       
  3948 ** [H16253]
       
  3949 */
       
  3950 SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
       
  3951 
       
  3952 /*
       
  3953 ** CAPI3REF: Function Auxiliary Data {H16270} <S20200>
       
  3954 **
       
  3955 ** The following two functions may be used by scalar SQL functions to
       
  3956 ** associate metadata with argument values. If the same value is passed to
       
  3957 ** multiple invocations of the same SQL function during query execution, under
       
  3958 ** some circumstances the associated metadata may be preserved. This may
       
  3959 ** be used, for example, to add a regular-expression matching scalar
       
  3960 ** function. The compiled version of the regular expression is stored as
       
  3961 ** metadata associated with the SQL value passed as the regular expression
       
  3962 ** pattern.  The compiled regular expression can be reused on multiple
       
  3963 ** invocations of the same function so that the original pattern string
       
  3964 ** does not need to be recompiled on each invocation.
       
  3965 **
       
  3966 ** The sqlite3_get_auxdata() interface returns a pointer to the metadata
       
  3967 ** associated by the sqlite3_set_auxdata() function with the Nth argument
       
  3968 ** value to the application-defined function. If no metadata has been ever
       
  3969 ** been set for the Nth argument of the function, or if the corresponding
       
  3970 ** function parameter has changed since the meta-data was set,
       
  3971 ** then sqlite3_get_auxdata() returns a NULL pointer.
       
  3972 **
       
  3973 ** The sqlite3_set_auxdata() interface saves the metadata
       
  3974 ** pointed to by its 3rd parameter as the metadata for the N-th
       
  3975 ** argument of the application-defined function.  Subsequent
       
  3976 ** calls to sqlite3_get_auxdata() might return this data, if it has
       
  3977 ** not been destroyed.
       
  3978 ** If it is not NULL, SQLite will invoke the destructor
       
  3979 ** function given by the 4th parameter to sqlite3_set_auxdata() on
       
  3980 ** the metadata when the corresponding function parameter changes
       
  3981 ** or when the SQL statement completes, whichever comes first.
       
  3982 **
       
  3983 ** SQLite is free to call the destructor and drop metadata on any
       
  3984 ** parameter of any function at any time.  The only guarantee is that
       
  3985 ** the destructor will be called before the metadata is dropped.
       
  3986 **
       
  3987 ** In practice, metadata is preserved between function calls for
       
  3988 ** expressions that are constant at compile time. This includes literal
       
  3989 ** values and SQL variables.
       
  3990 **
       
  3991 ** These routines must be called from the same thread in which
       
  3992 ** the SQL function is running.
       
  3993 **
       
  3994 ** Requirements:
       
  3995 ** [H16272] [H16274] [H16276] [H16277] [H16278] [H16279]
       
  3996 */
       
  3997 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
       
  3998 SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
       
  3999 
       
  4000 
       
  4001 /*
       
  4002 ** CAPI3REF: Constants Defining Special Destructor Behavior {H10280} <S30100>
       
  4003 **
       
  4004 ** These are special values for the destructor that is passed in as the
       
  4005 ** final argument to routines like [sqlite3_result_blob()].  If the destructor
       
  4006 ** argument is SQLITE_STATIC, it means that the content pointer is constant
       
  4007 ** and will never change.  It does not need to be destroyed.  The
       
  4008 ** SQLITE_TRANSIENT value means that the content will likely change in
       
  4009 ** the near future and that SQLite should make its own private copy of
       
  4010 ** the content before returning.
       
  4011 **
       
  4012 ** The typedef is necessary to work around problems in certain
       
  4013 ** C++ compilers.  See ticket #2191.
       
  4014 */
       
  4015 typedef void (*sqlite3_destructor_type)(void*);
       
  4016 #define SQLITE_STATIC      ((sqlite3_destructor_type)0)
       
  4017 #define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
       
  4018 
       
  4019 /*
       
  4020 ** CAPI3REF: Setting The Result Of An SQL Function {H16400} <S20200>
       
  4021 **
       
  4022 ** These routines are used by the xFunc or xFinal callbacks that
       
  4023 ** implement SQL functions and aggregates.  See
       
  4024 ** [sqlite3_create_function()] and [sqlite3_create_function16()]
       
  4025 ** for additional information.
       
  4026 **
       
  4027 ** These functions work very much like the [parameter binding] family of
       
  4028 ** functions used to bind values to host parameters in prepared statements.
       
  4029 ** Refer to the [SQL parameter] documentation for additional information.
       
  4030 **
       
  4031 ** The sqlite3_result_blob() interface sets the result from
       
  4032 ** an application-defined function to be the BLOB whose content is pointed
       
  4033 ** to by the second parameter and which is N bytes long where N is the
       
  4034 ** third parameter.
       
  4035 **
       
  4036 ** The sqlite3_result_zeroblob() interfaces set the result of
       
  4037 ** the application-defined function to be a BLOB containing all zero
       
  4038 ** bytes and N bytes in size, where N is the value of the 2nd parameter.
       
  4039 **
       
  4040 ** The sqlite3_result_double() interface sets the result from
       
  4041 ** an application-defined function to be a floating point value specified
       
  4042 ** by its 2nd argument.
       
  4043 **
       
  4044 ** The sqlite3_result_error() and sqlite3_result_error16() functions
       
  4045 ** cause the implemented SQL function to throw an exception.
       
  4046 ** SQLite uses the string pointed to by the
       
  4047 ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
       
  4048 ** as the text of an error message.  SQLite interprets the error
       
  4049 ** message string from sqlite3_result_error() as UTF-8. SQLite
       
  4050 ** interprets the string from sqlite3_result_error16() as UTF-16 in native
       
  4051 ** byte order.  If the third parameter to sqlite3_result_error()
       
  4052 ** or sqlite3_result_error16() is negative then SQLite takes as the error
       
  4053 ** message all text up through the first zero character.
       
  4054 ** If the third parameter to sqlite3_result_error() or
       
  4055 ** sqlite3_result_error16() is non-negative then SQLite takes that many
       
  4056 ** bytes (not characters) from the 2nd parameter as the error message.
       
  4057 ** The sqlite3_result_error() and sqlite3_result_error16()
       
  4058 ** routines make a private copy of the error message text before
       
  4059 ** they return.  Hence, the calling function can deallocate or
       
  4060 ** modify the text after they return without harm.
       
  4061 ** The sqlite3_result_error_code() function changes the error code
       
  4062 ** returned by SQLite as a result of an error in a function.  By default,
       
  4063 ** the error code is SQLITE_ERROR.  A subsequent call to sqlite3_result_error()
       
  4064 ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
       
  4065 **
       
  4066 ** The sqlite3_result_toobig() interface causes SQLite to throw an error
       
  4067 ** indicating that a string or BLOB is to long to represent.
       
  4068 **
       
  4069 ** The sqlite3_result_nomem() interface causes SQLite to throw an error
       
  4070 ** indicating that a memory allocation failed.
       
  4071 **
       
  4072 ** The sqlite3_result_int() interface sets the return value
       
  4073 ** of the application-defined function to be the 32-bit signed integer
       
  4074 ** value given in the 2nd argument.
       
  4075 ** The sqlite3_result_int64() interface sets the return value
       
  4076 ** of the application-defined function to be the 64-bit signed integer
       
  4077 ** value given in the 2nd argument.
       
  4078 **
       
  4079 ** The sqlite3_result_null() interface sets the return value
       
  4080 ** of the application-defined function to be NULL.
       
  4081 **
       
  4082 ** The sqlite3_result_text(), sqlite3_result_text16(),
       
  4083 ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
       
  4084 ** set the return value of the application-defined function to be
       
  4085 ** a text string which is represented as UTF-8, UTF-16 native byte order,
       
  4086 ** UTF-16 little endian, or UTF-16 big endian, respectively.
       
  4087 ** SQLite takes the text result from the application from
       
  4088 ** the 2nd parameter of the sqlite3_result_text* interfaces.
       
  4089 ** If the 3rd parameter to the sqlite3_result_text* interfaces
       
  4090 ** is negative, then SQLite takes result text from the 2nd parameter
       
  4091 ** through the first zero character.
       
  4092 ** If the 3rd parameter to the sqlite3_result_text* interfaces
       
  4093 ** is non-negative, then as many bytes (not characters) of the text
       
  4094 ** pointed to by the 2nd parameter are taken as the application-defined
       
  4095 ** function result.
       
  4096 ** If the 4th parameter to the sqlite3_result_text* interfaces
       
  4097 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
       
  4098 ** function as the destructor on the text or BLOB result when it has
       
  4099 ** finished using that result.
       
  4100 ** If the 4th parameter to the sqlite3_result_text* interfaces or to
       
  4101 ** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
       
  4102 ** assumes that the text or BLOB result is in constant space and does not
       
  4103 ** copy the content of the parameter nor call a destructor on the content
       
  4104 ** when it has finished using that result.
       
  4105 ** If the 4th parameter to the sqlite3_result_text* interfaces
       
  4106 ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
       
  4107 ** then SQLite makes a copy of the result into space obtained from
       
  4108 ** from [sqlite3_malloc()] before it returns.
       
  4109 **
       
  4110 ** The sqlite3_result_value() interface sets the result of
       
  4111 ** the application-defined function to be a copy the
       
  4112 ** [unprotected sqlite3_value] object specified by the 2nd parameter.  The
       
  4113 ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
       
  4114 ** so that the [sqlite3_value] specified in the parameter may change or
       
  4115 ** be deallocated after sqlite3_result_value() returns without harm.
       
  4116 ** A [protected sqlite3_value] object may always be used where an
       
  4117 ** [unprotected sqlite3_value] object is required, so either
       
  4118 ** kind of [sqlite3_value] object can be used with this interface.
       
  4119 **
       
  4120 ** If these routines are called from within the different thread
       
  4121 ** than the one containing the application-defined function that received
       
  4122 ** the [sqlite3_context] pointer, the results are undefined.
       
  4123 **
       
  4124 ** Requirements:
       
  4125 ** [H16403] [H16406] [H16409] [H16412] [H16415] [H16418] [H16421] [H16424]
       
  4126 ** [H16427] [H16430] [H16433] [H16436] [H16439] [H16442] [H16445] [H16448]
       
  4127 ** [H16451] [H16454] [H16457] [H16460] [H16463]
       
  4128 */
       
  4129 SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
       
  4130 SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
       
  4131 SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
       
  4132 SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
       
  4133 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
       
  4134 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
       
  4135 SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
       
  4136 SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
       
  4137 SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
       
  4138 SQLITE_API void sqlite3_result_null(sqlite3_context*);
       
  4139 SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
       
  4140 SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
       
  4141 SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
       
  4142 SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
       
  4143 SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
       
  4144 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
       
  4145 
       
  4146 /*
       
  4147 ** CAPI3REF: Define New Collating Sequences {H16600} <S20300>
       
  4148 **
       
  4149 ** These functions are used to add new collation sequences to the
       
  4150 ** [database connection] specified as the first argument.
       
  4151 **
       
  4152 ** The name of the new collation sequence is specified as a UTF-8 string
       
  4153 ** for sqlite3_create_collation() and sqlite3_create_collation_v2()
       
  4154 ** and a UTF-16 string for sqlite3_create_collation16(). In all cases
       
  4155 ** the name is passed as the second function argument.
       
  4156 **
       
  4157 ** The third argument may be one of the constants [SQLITE_UTF8],
       
  4158 ** [SQLITE_UTF16LE], or [SQLITE_UTF16BE], indicating that the user-supplied
       
  4159 ** routine expects to be passed pointers to strings encoded using UTF-8,
       
  4160 ** UTF-16 little-endian, or UTF-16 big-endian, respectively. The
       
  4161 ** third argument might also be [SQLITE_UTF16] to indicate that the routine
       
  4162 ** expects pointers to be UTF-16 strings in the native byte order, or the
       
  4163 ** argument can be [SQLITE_UTF16_ALIGNED] if the
       
  4164 ** the routine expects pointers to 16-bit word aligned strings
       
  4165 ** of UTF-16 in the native byte order.
       
  4166 **
       
  4167 ** A pointer to the user supplied routine must be passed as the fifth
       
  4168 ** argument.  If it is NULL, this is the same as deleting the collation
       
  4169 ** sequence (so that SQLite cannot call it anymore).
       
  4170 ** Each time the application supplied function is invoked, it is passed
       
  4171 ** as its first parameter a copy of the void* passed as the fourth argument
       
  4172 ** to sqlite3_create_collation() or sqlite3_create_collation16().
       
  4173 **
       
  4174 ** The remaining arguments to the application-supplied routine are two strings,
       
  4175 ** each represented by a (length, data) pair and encoded in the encoding
       
  4176 ** that was passed as the third argument when the collation sequence was
       
  4177 ** registered. {END}  The application defined collation routine should
       
  4178 ** return negative, zero or positive if the first string is less than,
       
  4179 ** equal to, or greater than the second string. i.e. (STRING1 - STRING2).
       
  4180 **
       
  4181 ** The sqlite3_create_collation_v2() works like sqlite3_create_collation()
       
  4182 ** except that it takes an extra argument which is a destructor for
       
  4183 ** the collation.  The destructor is called when the collation is
       
  4184 ** destroyed and is passed a copy of the fourth parameter void* pointer
       
  4185 ** of the sqlite3_create_collation_v2().
       
  4186 ** Collations are destroyed when they are overridden by later calls to the
       
  4187 ** collation creation functions or when the [database connection] is closed
       
  4188 ** using [sqlite3_close()].
       
  4189 **
       
  4190 ** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
       
  4191 **
       
  4192 ** Requirements:
       
  4193 ** [H16603] [H16604] [H16606] [H16609] [H16612] [H16615] [H16618] [H16621]
       
  4194 ** [H16624] [H16627] [H16630]
       
  4195 */
       
  4196 SQLITE_API int sqlite3_create_collation(
       
  4197   sqlite3*, 
       
  4198   const char *zName, 
       
  4199   int eTextRep, 
       
  4200   void*,
       
  4201   int(*xCompare)(void*,int,const void*,int,const void*)
       
  4202 );
       
  4203 SQLITE_API int sqlite3_create_collation_v2(
       
  4204   sqlite3*, 
       
  4205   const char *zName, 
       
  4206   int eTextRep, 
       
  4207   void*,
       
  4208   int(*xCompare)(void*,int,const void*,int,const void*),
       
  4209   void(*xDestroy)(void*)
       
  4210 );
       
  4211 SQLITE_API int sqlite3_create_collation16(
       
  4212   sqlite3*, 
       
  4213   const void *zName,
       
  4214   int eTextRep, 
       
  4215   void*,
       
  4216   int(*xCompare)(void*,int,const void*,int,const void*)
       
  4217 );
       
  4218 
       
  4219 /*
       
  4220 ** CAPI3REF: Collation Needed Callbacks {H16700} <S20300>
       
  4221 **
       
  4222 ** To avoid having to register all collation sequences before a database
       
  4223 ** can be used, a single callback function may be registered with the
       
  4224 ** [database connection] to be called whenever an undefined collation
       
  4225 ** sequence is required.
       
  4226 **
       
  4227 ** If the function is registered using the sqlite3_collation_needed() API,
       
  4228 ** then it is passed the names of undefined collation sequences as strings
       
  4229 ** encoded in UTF-8. {H16703} If sqlite3_collation_needed16() is used,
       
  4230 ** the names are passed as UTF-16 in machine native byte order.
       
  4231 ** A call to either function replaces any existing callback.
       
  4232 **
       
  4233 ** When the callback is invoked, the first argument passed is a copy
       
  4234 ** of the second argument to sqlite3_collation_needed() or
       
  4235 ** sqlite3_collation_needed16().  The second argument is the database
       
  4236 ** connection.  The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
       
  4237 ** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
       
  4238 ** sequence function required.  The fourth parameter is the name of the
       
  4239 ** required collation sequence.
       
  4240 **
       
  4241 ** The callback function should register the desired collation using
       
  4242 ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
       
  4243 ** [sqlite3_create_collation_v2()].
       
  4244 **
       
  4245 ** Requirements:
       
  4246 ** [H16702] [H16704] [H16706]
       
  4247 */
       
  4248 SQLITE_API int sqlite3_collation_needed(
       
  4249   sqlite3*, 
       
  4250   void*, 
       
  4251   void(*)(void*,sqlite3*,int eTextRep,const char*)
       
  4252 );
       
  4253 SQLITE_API int sqlite3_collation_needed16(
       
  4254   sqlite3*, 
       
  4255   void*,
       
  4256   void(*)(void*,sqlite3*,int eTextRep,const void*)
       
  4257 );
       
  4258 
       
  4259 /*
       
  4260 ** Specify the key for an encrypted database.  This routine should be
       
  4261 ** called right after sqlite3_open().
       
  4262 **
       
  4263 ** The code to implement this API is not available in the public release
       
  4264 ** of SQLite.
       
  4265 */
       
  4266 SQLITE_API int sqlite3_key(
       
  4267   sqlite3 *db,                   /* Database to be rekeyed */
       
  4268   const void *pKey, int nKey     /* The key */
       
  4269 );
       
  4270 
       
  4271 /*
       
  4272 ** Change the key on an open database.  If the current database is not
       
  4273 ** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
       
  4274 ** database is decrypted.
       
  4275 **
       
  4276 ** The code to implement this API is not available in the public release
       
  4277 ** of SQLite.
       
  4278 */
       
  4279 SQLITE_API int sqlite3_rekey(
       
  4280   sqlite3 *db,                   /* Database to be rekeyed */
       
  4281   const void *pKey, int nKey     /* The new key */
       
  4282 );
       
  4283 
       
  4284 /*
       
  4285 ** CAPI3REF: Suspend Execution For A Short Time {H10530} <S40410>
       
  4286 **
       
  4287 ** The sqlite3_sleep() function causes the current thread to suspend execution
       
  4288 ** for at least a number of milliseconds specified in its parameter.
       
  4289 **
       
  4290 ** If the operating system does not support sleep requests with
       
  4291 ** millisecond time resolution, then the time will be rounded up to
       
  4292 ** the nearest second. The number of milliseconds of sleep actually
       
  4293 ** requested from the operating system is returned.
       
  4294 **
       
  4295 ** SQLite implements this interface by calling the xSleep()
       
  4296 ** method of the default [sqlite3_vfs] object.
       
  4297 **
       
  4298 ** Requirements: [H10533] [H10536]
       
  4299 */
       
  4300 SQLITE_API int sqlite3_sleep(int);
       
  4301 
       
  4302 /*
       
  4303 ** CAPI3REF: Name Of The Folder Holding Temporary Files {H10310} <S20000>
       
  4304 **
       
  4305 ** If this global variable is made to point to a string which is
       
  4306 ** the name of a folder (a.k.a. directory), then all temporary files
       
  4307 ** created by SQLite will be placed in that directory.  If this variable
       
  4308 ** is a NULL pointer, then SQLite performs a search for an appropriate
       
  4309 ** temporary file directory.
       
  4310 **
       
  4311 ** It is not safe to read or modify this variable in more than one
       
  4312 ** thread at a time.  It is not safe to read or modify this variable
       
  4313 ** if a [database connection] is being used at the same time in a separate
       
  4314 ** thread.
       
  4315 ** It is intended that this variable be set once
       
  4316 ** as part of process initialization and before any SQLite interface
       
  4317 ** routines have been called and that this variable remain unchanged
       
  4318 ** thereafter.
       
  4319 **
       
  4320 ** The [temp_store_directory pragma] may modify this variable and cause
       
  4321 ** it to point to memory obtained from [sqlite3_malloc].  Furthermore,
       
  4322 ** the [temp_store_directory pragma] always assumes that any string
       
  4323 ** that this variable points to is held in memory obtained from 
       
  4324 ** [sqlite3_malloc] and the pragma may attempt to free that memory
       
  4325 ** using [sqlite3_free].
       
  4326 ** Hence, if this variable is modified directly, either it should be
       
  4327 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
       
  4328 ** or else the use of the [temp_store_directory pragma] should be avoided.
       
  4329 */
       
  4330 SQLITE_API char *sqlite3_temp_directory;
       
  4331 
       
  4332 /*
       
  4333 ** CAPI3REF: Test For Auto-Commit Mode {H12930} <S60200>
       
  4334 ** KEYWORDS: {autocommit mode}
       
  4335 **
       
  4336 ** The sqlite3_get_autocommit() interface returns non-zero or
       
  4337 ** zero if the given database connection is or is not in autocommit mode,
       
  4338 ** respectively.  Autocommit mode is on by default.
       
  4339 ** Autocommit mode is disabled by a [BEGIN] statement.
       
  4340 ** Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
       
  4341 **
       
  4342 ** If certain kinds of errors occur on a statement within a multi-statement
       
  4343 ** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
       
  4344 ** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
       
  4345 ** transaction might be rolled back automatically.  The only way to
       
  4346 ** find out whether SQLite automatically rolled back the transaction after
       
  4347 ** an error is to use this function.
       
  4348 **
       
  4349 ** If another thread changes the autocommit status of the database
       
  4350 ** connection while this routine is running, then the return value
       
  4351 ** is undefined.
       
  4352 **
       
  4353 ** Requirements: [H12931] [H12932] [H12933] [H12934]
       
  4354 */
       
  4355 SQLITE_API int sqlite3_get_autocommit(sqlite3*);
       
  4356 
       
  4357 /*
       
  4358 ** CAPI3REF: Find The Database Handle Of A Prepared Statement {H13120} <S60600>
       
  4359 **
       
  4360 ** The sqlite3_db_handle interface returns the [database connection] handle
       
  4361 ** to which a [prepared statement] belongs.  The [database connection]
       
  4362 ** returned by sqlite3_db_handle is the same [database connection] that was the first argument
       
  4363 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
       
  4364 ** create the statement in the first place.
       
  4365 **
       
  4366 ** Requirements: [H13123]
       
  4367 */
       
  4368 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
       
  4369 
       
  4370 /*
       
  4371 ** CAPI3REF: Find the next prepared statement {H13140} <S60600>
       
  4372 **
       
  4373 ** This interface returns a pointer to the next [prepared statement] after
       
  4374 ** pStmt associated with the [database connection] pDb.  If pStmt is NULL
       
  4375 ** then this interface returns a pointer to the first prepared statement
       
  4376 ** associated with the database connection pDb.  If no prepared statement
       
  4377 ** satisfies the conditions of this routine, it returns NULL.
       
  4378 **
       
  4379 ** The [database connection] pointer D in a call to
       
  4380 ** [sqlite3_next_stmt(D,S)] must refer to an open database
       
  4381 ** connection and in particular must not be a NULL pointer.
       
  4382 **
       
  4383 ** Requirements: [H13143] [H13146] [H13149] [H13152]
       
  4384 */
       
  4385 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
       
  4386 
       
  4387 /*
       
  4388 ** CAPI3REF: Commit And Rollback Notification Callbacks {H12950} <S60400>
       
  4389 **
       
  4390 ** The sqlite3_commit_hook() interface registers a callback
       
  4391 ** function to be invoked whenever a transaction is [COMMIT | committed].
       
  4392 ** Any callback set by a previous call to sqlite3_commit_hook()
       
  4393 ** for the same database connection is overridden.
       
  4394 ** The sqlite3_rollback_hook() interface registers a callback
       
  4395 ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
       
  4396 ** Any callback set by a previous call to sqlite3_commit_hook()
       
  4397 ** for the same database connection is overridden.
       
  4398 ** The pArg argument is passed through to the callback.
       
  4399 ** If the callback on a commit hook function returns non-zero,
       
  4400 ** then the commit is converted into a rollback.
       
  4401 **
       
  4402 ** If another function was previously registered, its
       
  4403 ** pArg value is returned.  Otherwise NULL is returned.
       
  4404 **
       
  4405 ** The callback implementation must not do anything that will modify
       
  4406 ** the database connection that invoked the callback.  Any actions
       
  4407 ** to modify the database connection must be deferred until after the
       
  4408 ** completion of the [sqlite3_step()] call that triggered the commit
       
  4409 ** or rollback hook in the first place.
       
  4410 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
       
  4411 ** database connections for the meaning of "modify" in this paragraph.
       
  4412 **
       
  4413 ** Registering a NULL function disables the callback.
       
  4414 **
       
  4415 ** When the commit hook callback routine returns zero, the [COMMIT]
       
  4416 ** operation is allowed to continue normally.  If the commit hook
       
  4417 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
       
  4418 ** The rollback hook is invoked on a rollback that results from a commit
       
  4419 ** hook returning non-zero, just as it would be with any other rollback.
       
  4420 **
       
  4421 ** For the purposes of this API, a transaction is said to have been
       
  4422 ** rolled back if an explicit "ROLLBACK" statement is executed, or
       
  4423 ** an error or constraint causes an implicit rollback to occur.
       
  4424 ** The rollback callback is not invoked if a transaction is
       
  4425 ** automatically rolled back because the database connection is closed.
       
  4426 ** The rollback callback is not invoked if a transaction is
       
  4427 ** rolled back because a commit callback returned non-zero.
       
  4428 ** <todo> Check on this </todo>
       
  4429 **
       
  4430 ** See also the [sqlite3_update_hook()] interface.
       
  4431 **
       
  4432 ** Requirements:
       
  4433 ** [H12951] [H12952] [H12953] [H12954] [H12955]
       
  4434 ** [H12961] [H12962] [H12963] [H12964]
       
  4435 */
       
  4436 SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
       
  4437 SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
       
  4438 
       
  4439 /*
       
  4440 ** CAPI3REF: Data Change Notification Callbacks {H12970} <S60400>
       
  4441 **
       
  4442 ** The sqlite3_update_hook() interface registers a callback function
       
  4443 ** with the [database connection] identified by the first argument
       
  4444 ** to be invoked whenever a row is updated, inserted or deleted.
       
  4445 ** Any callback set by a previous call to this function
       
  4446 ** for the same database connection is overridden.
       
  4447 **
       
  4448 ** The second argument is a pointer to the function to invoke when a
       
  4449 ** row is updated, inserted or deleted.
       
  4450 ** The first argument to the callback is a copy of the third argument
       
  4451 ** to sqlite3_update_hook().
       
  4452 ** The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
       
  4453 ** or [SQLITE_UPDATE], depending on the operation that caused the callback
       
  4454 ** to be invoked.
       
  4455 ** The third and fourth arguments to the callback contain pointers to the
       
  4456 ** database and table name containing the affected row.
       
  4457 ** The final callback parameter is the [rowid] of the row.
       
  4458 ** In the case of an update, this is the [rowid] after the update takes place.
       
  4459 **
       
  4460 ** The update hook is not invoked when internal system tables are
       
  4461 ** modified (i.e. sqlite_master and sqlite_sequence).
       
  4462 **
       
  4463 ** In the current implementation, the update hook
       
  4464 ** is not invoked when duplication rows are deleted because of an
       
  4465 ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  Nor is the update hook
       
  4466 ** invoked when rows are deleted using the [truncate optimization].
       
  4467 ** The exceptions defined in this paragraph might change in a future
       
  4468 ** release of SQLite.
       
  4469 **
       
  4470 ** The update hook implementation must not do anything that will modify
       
  4471 ** the database connection that invoked the update hook.  Any actions
       
  4472 ** to modify the database connection must be deferred until after the
       
  4473 ** completion of the [sqlite3_step()] call that triggered the update hook.
       
  4474 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
       
  4475 ** database connections for the meaning of "modify" in this paragraph.
       
  4476 **
       
  4477 ** If another function was previously registered, its pArg value
       
  4478 ** is returned.  Otherwise NULL is returned.
       
  4479 **
       
  4480 ** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
       
  4481 ** interfaces.
       
  4482 **
       
  4483 ** Requirements:
       
  4484 ** [H12971] [H12973] [H12975] [H12977] [H12979] [H12981] [H12983] [H12986]
       
  4485 */
       
  4486 SQLITE_API void *sqlite3_update_hook(
       
  4487   sqlite3*, 
       
  4488   void(*)(void *,int ,char const *,char const *,sqlite3_int64),
       
  4489   void*
       
  4490 );
       
  4491 
       
  4492 /*
       
  4493 ** CAPI3REF: Enable Or Disable Shared Pager Cache {H10330} <S30900>
       
  4494 ** KEYWORDS: {shared cache}
       
  4495 **
       
  4496 ** This routine enables or disables the sharing of the database cache
       
  4497 ** and schema data structures between [database connection | connections]
       
  4498 ** to the same database. Sharing is enabled if the argument is true
       
  4499 ** and disabled if the argument is false.
       
  4500 **
       
  4501 ** Cache sharing is enabled and disabled for an entire process.
       
  4502 ** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
       
  4503 ** sharing was enabled or disabled for each thread separately.
       
  4504 **
       
  4505 ** The cache sharing mode set by this interface effects all subsequent
       
  4506 ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
       
  4507 ** Existing database connections continue use the sharing mode
       
  4508 ** that was in effect at the time they were opened.
       
  4509 **
       
  4510 ** Virtual tables cannot be used with a shared cache.  When shared
       
  4511 ** cache is enabled, the [sqlite3_create_module()] API used to register
       
  4512 ** virtual tables will always return an error.
       
  4513 **
       
  4514 ** This routine returns [SQLITE_OK] if shared cache was enabled or disabled
       
  4515 ** successfully.  An [error code] is returned otherwise.
       
  4516 **
       
  4517 ** Shared cache is disabled by default. But this might change in
       
  4518 ** future releases of SQLite.  Applications that care about shared
       
  4519 ** cache setting should set it explicitly.
       
  4520 **
       
  4521 ** See Also:  [SQLite Shared-Cache Mode]
       
  4522 **
       
  4523 ** Requirements: [H10331] [H10336] [H10337] [H10339]
       
  4524 */
       
  4525 SQLITE_API int sqlite3_enable_shared_cache(int);
       
  4526 
       
  4527 /*
       
  4528 ** CAPI3REF: Attempt To Free Heap Memory {H17340} <S30220>
       
  4529 **
       
  4530 ** The sqlite3_release_memory() interface attempts to free N bytes
       
  4531 ** of heap memory by deallocating non-essential memory allocations
       
  4532 ** held by the database library. {END}  Memory used to cache database
       
  4533 ** pages to improve performance is an example of non-essential memory.
       
  4534 ** sqlite3_release_memory() returns the number of bytes actually freed,
       
  4535 ** which might be more or less than the amount requested.
       
  4536 **
       
  4537 ** Requirements: [H17341] [H17342]
       
  4538 */
       
  4539 SQLITE_API int sqlite3_release_memory(int);
       
  4540 
       
  4541 /*
       
  4542 ** CAPI3REF: Impose A Limit On Heap Size {H17350} <S30220>
       
  4543 **
       
  4544 ** The sqlite3_soft_heap_limit() interface places a "soft" limit
       
  4545 ** on the amount of heap memory that may be allocated by SQLite.
       
  4546 ** If an internal allocation is requested that would exceed the
       
  4547 ** soft heap limit, [sqlite3_release_memory()] is invoked one or
       
  4548 ** more times to free up some space before the allocation is performed.
       
  4549 **
       
  4550 ** The limit is called "soft", because if [sqlite3_release_memory()]
       
  4551 ** cannot free sufficient memory to prevent the limit from being exceeded,
       
  4552 ** the memory is allocated anyway and the current operation proceeds.
       
  4553 **
       
  4554 ** A negative or zero value for N means that there is no soft heap limit and
       
  4555 ** [sqlite3_release_memory()] will only be called when memory is exhausted.
       
  4556 ** The default value for the soft heap limit is zero.
       
  4557 **
       
  4558 ** SQLite makes a best effort to honor the soft heap limit.
       
  4559 ** But if the soft heap limit cannot be honored, execution will
       
  4560 ** continue without error or notification.  This is why the limit is
       
  4561 ** called a "soft" limit.  It is advisory only.
       
  4562 **
       
  4563 ** Prior to SQLite version 3.5.0, this routine only constrained the memory
       
  4564 ** allocated by a single thread - the same thread in which this routine
       
  4565 ** runs.  Beginning with SQLite version 3.5.0, the soft heap limit is
       
  4566 ** applied to all threads. The value specified for the soft heap limit
       
  4567 ** is an upper bound on the total memory allocation for all threads. In
       
  4568 ** version 3.5.0 there is no mechanism for limiting the heap usage for
       
  4569 ** individual threads.
       
  4570 **
       
  4571 ** Requirements:
       
  4572 ** [H16351] [H16352] [H16353] [H16354] [H16355] [H16358]
       
  4573 */
       
  4574 SQLITE_API void sqlite3_soft_heap_limit(int);
       
  4575 
       
  4576 /*
       
  4577 ** CAPI3REF: Extract Metadata About A Column Of A Table {H12850} <S60300>
       
  4578 **
       
  4579 ** This routine returns metadata about a specific column of a specific
       
  4580 ** database table accessible using the [database connection] handle
       
  4581 ** passed as the first function argument.
       
  4582 **
       
  4583 ** The column is identified by the second, third and fourth parameters to
       
  4584 ** this function. The second parameter is either the name of the database
       
  4585 ** (i.e. "main", "temp" or an attached database) containing the specified
       
  4586 ** table or NULL. If it is NULL, then all attached databases are searched
       
  4587 ** for the table using the same algorithm used by the database engine to
       
  4588 ** resolve unqualified table references.
       
  4589 **
       
  4590 ** The third and fourth parameters to this function are the table and column
       
  4591 ** name of the desired column, respectively. Neither of these parameters
       
  4592 ** may be NULL.
       
  4593 **
       
  4594 ** Metadata is returned by writing to the memory locations passed as the 5th
       
  4595 ** and subsequent parameters to this function. Any of these arguments may be
       
  4596 ** NULL, in which case the corresponding element of metadata is omitted.
       
  4597 **
       
  4598 ** <blockquote>
       
  4599 ** <table border="1">
       
  4600 ** <tr><th> Parameter <th> Output<br>Type <th>  Description
       
  4601 **
       
  4602 ** <tr><td> 5th <td> const char* <td> Data type
       
  4603 ** <tr><td> 6th <td> const char* <td> Name of default collation sequence
       
  4604 ** <tr><td> 7th <td> int         <td> True if column has a NOT NULL constraint
       
  4605 ** <tr><td> 8th <td> int         <td> True if column is part of the PRIMARY KEY
       
  4606 ** <tr><td> 9th <td> int         <td> True if column is [AUTOINCREMENT]
       
  4607 ** </table>
       
  4608 ** </blockquote>
       
  4609 **
       
  4610 ** The memory pointed to by the character pointers returned for the
       
  4611 ** declaration type and collation sequence is valid only until the next
       
  4612 ** call to any SQLite API function.
       
  4613 **
       
  4614 ** If the specified table is actually a view, an [error code] is returned.
       
  4615 **
       
  4616 ** If the specified column is "rowid", "oid" or "_rowid_" and an
       
  4617 ** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
       
  4618 ** parameters are set for the explicitly declared column. If there is no
       
  4619 ** explicitly declared [INTEGER PRIMARY KEY] column, then the output
       
  4620 ** parameters are set as follows:
       
  4621 **
       
  4622 ** <pre>
       
  4623 **     data type: "INTEGER"
       
  4624 **     collation sequence: "BINARY"
       
  4625 **     not null: 0
       
  4626 **     primary key: 1
       
  4627 **     auto increment: 0
       
  4628 ** </pre>
       
  4629 **
       
  4630 ** This function may load one or more schemas from database files. If an
       
  4631 ** error occurs during this process, or if the requested table or column
       
  4632 ** cannot be found, an [error code] is returned and an error message left
       
  4633 ** in the [database connection] (to be retrieved using sqlite3_errmsg()).
       
  4634 **
       
  4635 ** This API is only available if the library was compiled with the
       
  4636 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
       
  4637 */
       
  4638 SQLITE_API int sqlite3_table_column_metadata(
       
  4639   sqlite3 *db,                /* Connection handle */
       
  4640   const char *zDbName,        /* Database name or NULL */
       
  4641   const char *zTableName,     /* Table name */
       
  4642   const char *zColumnName,    /* Column name */
       
  4643   char const **pzDataType,    /* OUTPUT: Declared data type */
       
  4644   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
       
  4645   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
       
  4646   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
       
  4647   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
       
  4648 );
       
  4649 
       
  4650 /*
       
  4651 ** CAPI3REF: Load An Extension {H12600} <S20500>
       
  4652 **
       
  4653 ** This interface loads an SQLite extension library from the named file.
       
  4654 **
       
  4655 ** {H12601} The sqlite3_load_extension() interface attempts to load an
       
  4656 **          SQLite extension library contained in the file zFile.
       
  4657 **
       
  4658 ** {H12602} The entry point is zProc.
       
  4659 **
       
  4660 ** {H12603} zProc may be 0, in which case the name of the entry point
       
  4661 **          defaults to "sqlite3_extension_init".
       
  4662 **
       
  4663 ** {H12604} The sqlite3_load_extension() interface shall return
       
  4664 **          [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
       
  4665 **
       
  4666 ** {H12605} If an error occurs and pzErrMsg is not 0, then the
       
  4667 **          [sqlite3_load_extension()] interface shall attempt to
       
  4668 **          fill *pzErrMsg with error message text stored in memory
       
  4669 **          obtained from [sqlite3_malloc()]. {END}  The calling function
       
  4670 **          should free this memory by calling [sqlite3_free()].
       
  4671 **
       
  4672 ** {H12606} Extension loading must be enabled using
       
  4673 **          [sqlite3_enable_load_extension()] prior to calling this API,
       
  4674 **          otherwise an error will be returned.
       
  4675 */
       
  4676 SQLITE_API int sqlite3_load_extension(
       
  4677   sqlite3 *db,          /* Load the extension into this database connection */
       
  4678   const char *zFile,    /* Name of the shared library containing extension */
       
  4679   const char *zProc,    /* Entry point.  Derived from zFile if 0 */
       
  4680   char **pzErrMsg       /* Put error message here if not 0 */
       
  4681 );
       
  4682 
       
  4683 /*
       
  4684 ** CAPI3REF: Enable Or Disable Extension Loading {H12620} <S20500>
       
  4685 **
       
  4686 ** So as not to open security holes in older applications that are
       
  4687 ** unprepared to deal with extension loading, and as a means of disabling
       
  4688 ** extension loading while evaluating user-entered SQL, the following API
       
  4689 ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
       
  4690 **
       
  4691 ** Extension loading is off by default. See ticket #1863.
       
  4692 **
       
  4693 ** {H12621} Call the sqlite3_enable_load_extension() routine with onoff==1
       
  4694 **          to turn extension loading on and call it with onoff==0 to turn
       
  4695 **          it back off again.
       
  4696 **
       
  4697 ** {H12622} Extension loading is off by default.
       
  4698 */
       
  4699 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
       
  4700 
       
  4701 /*
       
  4702 ** CAPI3REF: Automatically Load An Extensions {H12640} <S20500>
       
  4703 **
       
  4704 ** This API can be invoked at program startup in order to register
       
  4705 ** one or more statically linked extensions that will be available
       
  4706 ** to all new [database connections]. {END}
       
  4707 **
       
  4708 ** This routine stores a pointer to the extension in an array that is
       
  4709 ** obtained from [sqlite3_malloc()].  If you run a memory leak checker
       
  4710 ** on your program and it reports a leak because of this array, invoke
       
  4711 ** [sqlite3_reset_auto_extension()] prior to shutdown to free the memory.
       
  4712 **
       
  4713 ** {H12641} This function registers an extension entry point that is
       
  4714 **          automatically invoked whenever a new [database connection]
       
  4715 **          is opened using [sqlite3_open()], [sqlite3_open16()],
       
  4716 **          or [sqlite3_open_v2()].
       
  4717 **
       
  4718 ** {H12642} Duplicate extensions are detected so calling this routine
       
  4719 **          multiple times with the same extension is harmless.
       
  4720 **
       
  4721 ** {H12643} This routine stores a pointer to the extension in an array
       
  4722 **          that is obtained from [sqlite3_malloc()].
       
  4723 **
       
  4724 ** {H12644} Automatic extensions apply across all threads.
       
  4725 */
       
  4726 SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void));
       
  4727 
       
  4728 /*
       
  4729 ** CAPI3REF: Reset Automatic Extension Loading {H12660} <S20500>
       
  4730 **
       
  4731 ** This function disables all previously registered automatic
       
  4732 ** extensions. {END}  It undoes the effect of all prior
       
  4733 ** [sqlite3_auto_extension()] calls.
       
  4734 **
       
  4735 ** {H12661} This function disables all previously registered
       
  4736 **          automatic extensions.
       
  4737 **
       
  4738 ** {H12662} This function disables automatic extensions in all threads.
       
  4739 */
       
  4740 SQLITE_API void sqlite3_reset_auto_extension(void);
       
  4741 
       
  4742 /*
       
  4743 ****** EXPERIMENTAL - subject to change without notice **************
       
  4744 **
       
  4745 ** The interface to the virtual-table mechanism is currently considered
       
  4746 ** to be experimental.  The interface might change in incompatible ways.
       
  4747 ** If this is a problem for you, do not use the interface at this time.
       
  4748 **
       
  4749 ** When the virtual-table mechanism stabilizes, we will declare the
       
  4750 ** interface fixed, support it indefinitely, and remove this comment.
       
  4751 */
       
  4752 
       
  4753 /*
       
  4754 ** Structures used by the virtual table interface
       
  4755 */
       
  4756 typedef struct sqlite3_vtab sqlite3_vtab;
       
  4757 typedef struct sqlite3_index_info sqlite3_index_info;
       
  4758 typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
       
  4759 typedef struct sqlite3_module sqlite3_module;
       
  4760 
       
  4761 /*
       
  4762 ** CAPI3REF: Virtual Table Object {H18000} <S20400>
       
  4763 ** KEYWORDS: sqlite3_module {virtual table module}
       
  4764 ** EXPERIMENTAL
       
  4765 **
       
  4766 ** This structure, sometimes called a a "virtual table module", 
       
  4767 ** defines the implementation of a [virtual tables].  
       
  4768 ** This structure consists mostly of methods for the module.
       
  4769 **
       
  4770 ** A virtual table module is created by filling in a persistent
       
  4771 ** instance of this structure and passing a pointer to that instance
       
  4772 ** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
       
  4773 ** The registration remains valid until it is replaced by a different
       
  4774 ** module or until the [database connection] closes.  The content
       
  4775 ** of this structure must not change while it is registered with
       
  4776 ** any database connection.
       
  4777 */
       
  4778 struct sqlite3_module {
       
  4779   int iVersion;
       
  4780   int (*xCreate)(sqlite3*, void *pAux,
       
  4781                int argc, const char *const*argv,
       
  4782                sqlite3_vtab **ppVTab, char**);
       
  4783   int (*xConnect)(sqlite3*, void *pAux,
       
  4784                int argc, const char *const*argv,
       
  4785                sqlite3_vtab **ppVTab, char**);
       
  4786   int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
       
  4787   int (*xDisconnect)(sqlite3_vtab *pVTab);
       
  4788   int (*xDestroy)(sqlite3_vtab *pVTab);
       
  4789   int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
       
  4790   int (*xClose)(sqlite3_vtab_cursor*);
       
  4791   int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
       
  4792                 int argc, sqlite3_value **argv);
       
  4793   int (*xNext)(sqlite3_vtab_cursor*);
       
  4794   int (*xEof)(sqlite3_vtab_cursor*);
       
  4795   int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
       
  4796   int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
       
  4797   int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
       
  4798   int (*xBegin)(sqlite3_vtab *pVTab);
       
  4799   int (*xSync)(sqlite3_vtab *pVTab);
       
  4800   int (*xCommit)(sqlite3_vtab *pVTab);
       
  4801   int (*xRollback)(sqlite3_vtab *pVTab);
       
  4802   int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
       
  4803                        void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
       
  4804                        void **ppArg);
       
  4805   int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
       
  4806 };
       
  4807 
       
  4808 /*
       
  4809 ** CAPI3REF: Virtual Table Indexing Information {H18100} <S20400>
       
  4810 ** KEYWORDS: sqlite3_index_info
       
  4811 ** EXPERIMENTAL
       
  4812 **
       
  4813 ** The sqlite3_index_info structure and its substructures is used to
       
  4814 ** pass information into and receive the reply from the [xBestIndex]
       
  4815 ** method of a [virtual table module].  The fields under **Inputs** are the
       
  4816 ** inputs to xBestIndex and are read-only.  xBestIndex inserts its
       
  4817 ** results into the **Outputs** fields.
       
  4818 **
       
  4819 ** The aConstraint[] array records WHERE clause constraints of the form:
       
  4820 **
       
  4821 ** <pre>column OP expr</pre>
       
  4822 **
       
  4823 ** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.  The particular operator is
       
  4824 ** stored in aConstraint[].op.  The index of the column is stored in
       
  4825 ** aConstraint[].iColumn.  aConstraint[].usable is TRUE if the
       
  4826 ** expr on the right-hand side can be evaluated (and thus the constraint
       
  4827 ** is usable) and false if it cannot.
       
  4828 **
       
  4829 ** The optimizer automatically inverts terms of the form "expr OP column"
       
  4830 ** and makes other simplifications to the WHERE clause in an attempt to
       
  4831 ** get as many WHERE clause terms into the form shown above as possible.
       
  4832 ** The aConstraint[] array only reports WHERE clause terms in the correct
       
  4833 ** form that refer to the particular virtual table being queried.
       
  4834 **
       
  4835 ** Information about the ORDER BY clause is stored in aOrderBy[].
       
  4836 ** Each term of aOrderBy records a column of the ORDER BY clause.
       
  4837 **
       
  4838 ** The [xBestIndex] method must fill aConstraintUsage[] with information
       
  4839 ** about what parameters to pass to xFilter.  If argvIndex>0 then
       
  4840 ** the right-hand side of the corresponding aConstraint[] is evaluated
       
  4841 ** and becomes the argvIndex-th entry in argv.  If aConstraintUsage[].omit
       
  4842 ** is true, then the constraint is assumed to be fully handled by the
       
  4843 ** virtual table and is not checked again by SQLite.
       
  4844 **
       
  4845 ** The idxNum and idxPtr values are recorded and passed into the
       
  4846 ** [xFilter] method.
       
  4847 ** [sqlite3_free()] is used to free idxPtr if and only iff
       
  4848 ** needToFreeIdxPtr is true.
       
  4849 **
       
  4850 ** The orderByConsumed means that output from [xFilter]/[xNext] will occur in
       
  4851 ** the correct order to satisfy the ORDER BY clause so that no separate
       
  4852 ** sorting step is required.
       
  4853 **
       
  4854 ** The estimatedCost value is an estimate of the cost of doing the
       
  4855 ** particular lookup.  A full scan of a table with N entries should have
       
  4856 ** a cost of N.  A binary search of a table of N entries should have a
       
  4857 ** cost of approximately log(N).
       
  4858 */
       
  4859 struct sqlite3_index_info {
       
  4860   /* Inputs */
       
  4861   int nConstraint;           /* Number of entries in aConstraint */
       
  4862   struct sqlite3_index_constraint {
       
  4863      int iColumn;              /* Column on left-hand side of constraint */
       
  4864      unsigned char op;         /* Constraint operator */
       
  4865      unsigned char usable;     /* True if this constraint is usable */
       
  4866      int iTermOffset;          /* Used internally - xBestIndex should ignore */
       
  4867   } *aConstraint;            /* Table of WHERE clause constraints */
       
  4868   int nOrderBy;              /* Number of terms in the ORDER BY clause */
       
  4869   struct sqlite3_index_orderby {
       
  4870      int iColumn;              /* Column number */
       
  4871      unsigned char desc;       /* True for DESC.  False for ASC. */
       
  4872   } *aOrderBy;               /* The ORDER BY clause */
       
  4873   /* Outputs */
       
  4874   struct sqlite3_index_constraint_usage {
       
  4875     int argvIndex;           /* if >0, constraint is part of argv to xFilter */
       
  4876     unsigned char omit;      /* Do not code a test for this constraint */
       
  4877   } *aConstraintUsage;
       
  4878   int idxNum;                /* Number used to identify the index */
       
  4879   char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
       
  4880   int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
       
  4881   int orderByConsumed;       /* True if output is already ordered */
       
  4882   double estimatedCost;      /* Estimated cost of using this index */
       
  4883 };
       
  4884 #define SQLITE_INDEX_CONSTRAINT_EQ    2
       
  4885 #define SQLITE_INDEX_CONSTRAINT_GT    4
       
  4886 #define SQLITE_INDEX_CONSTRAINT_LE    8
       
  4887 #define SQLITE_INDEX_CONSTRAINT_LT    16
       
  4888 #define SQLITE_INDEX_CONSTRAINT_GE    32
       
  4889 #define SQLITE_INDEX_CONSTRAINT_MATCH 64
       
  4890 
       
  4891 /*
       
  4892 ** CAPI3REF: Register A Virtual Table Implementation {H18200} <S20400>
       
  4893 ** EXPERIMENTAL
       
  4894 **
       
  4895 ** This routine is used to register a new [virtual table module] name.
       
  4896 ** Module names must be registered before
       
  4897 ** creating a new [virtual table] using the module, or before using a
       
  4898 ** preexisting [virtual table] for the module.
       
  4899 **
       
  4900 ** The module name is registered on the [database connection] specified
       
  4901 ** by the first parameter.  The name of the module is given by the 
       
  4902 ** second parameter.  The third parameter is a pointer to
       
  4903 ** the implementation of the [virtual table module].   The fourth
       
  4904 ** parameter is an arbitrary client data pointer that is passed through
       
  4905 ** into the [xCreate] and [xConnect] methods of the virtual table module
       
  4906 ** when a new virtual table is be being created or reinitialized.
       
  4907 **
       
  4908 ** This interface has exactly the same effect as calling
       
  4909 ** [sqlite3_create_module_v2()] with a NULL client data destructor.
       
  4910 */
       
  4911 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_create_module(
       
  4912   sqlite3 *db,               /* SQLite connection to register module with */
       
  4913   const char *zName,         /* Name of the module */
       
  4914   const sqlite3_module *p,   /* Methods for the module */
       
  4915   void *pClientData          /* Client data for xCreate/xConnect */
       
  4916 );
       
  4917 
       
  4918 /*
       
  4919 ** CAPI3REF: Register A Virtual Table Implementation {H18210} <S20400>
       
  4920 ** EXPERIMENTAL
       
  4921 **
       
  4922 ** This routine is identical to the [sqlite3_create_module()] method,
       
  4923 ** except that it has an extra parameter to specify 
       
  4924 ** a destructor function for the client data pointer.  SQLite will
       
  4925 ** invoke the destructor function (if it is not NULL) when SQLite
       
  4926 ** no longer needs the pClientData pointer.  
       
  4927 */
       
  4928 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_create_module_v2(
       
  4929   sqlite3 *db,               /* SQLite connection to register module with */
       
  4930   const char *zName,         /* Name of the module */
       
  4931   const sqlite3_module *p,   /* Methods for the module */
       
  4932   void *pClientData,         /* Client data for xCreate/xConnect */
       
  4933   void(*xDestroy)(void*)     /* Module destructor function */
       
  4934 );
       
  4935 
       
  4936 /*
       
  4937 ** CAPI3REF: Virtual Table Instance Object {H18010} <S20400>
       
  4938 ** KEYWORDS: sqlite3_vtab
       
  4939 ** EXPERIMENTAL
       
  4940 **
       
  4941 ** Every [virtual table module] implementation uses a subclass
       
  4942 ** of the following structure to describe a particular instance
       
  4943 ** of the [virtual table].  Each subclass will
       
  4944 ** be tailored to the specific needs of the module implementation.
       
  4945 ** The purpose of this superclass is to define certain fields that are
       
  4946 ** common to all module implementations.
       
  4947 **
       
  4948 ** Virtual tables methods can set an error message by assigning a
       
  4949 ** string obtained from [sqlite3_mprintf()] to zErrMsg.  The method should
       
  4950 ** take care that any prior string is freed by a call to [sqlite3_free()]
       
  4951 ** prior to assigning a new string to zErrMsg.  After the error message
       
  4952 ** is delivered up to the client application, the string will be automatically
       
  4953 ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
       
  4954 */
       
  4955 struct sqlite3_vtab {
       
  4956   const sqlite3_module *pModule;  /* The module for this virtual table */
       
  4957   int nRef;                       /* NO LONGER USED */
       
  4958   char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
       
  4959   /* Virtual table implementations will typically add additional fields */
       
  4960 };
       
  4961 
       
  4962 /*
       
  4963 ** CAPI3REF: Virtual Table Cursor Object  {H18020} <S20400>
       
  4964 ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
       
  4965 ** EXPERIMENTAL
       
  4966 **
       
  4967 ** Every [virtual table module] implementation uses a subclass of the
       
  4968 ** following structure to describe cursors that point into the
       
  4969 ** [virtual table] and are used
       
  4970 ** to loop through the virtual table.  Cursors are created using the
       
  4971 ** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
       
  4972 ** by the [sqlite3_module.xClose | xClose] method.  Cussors are used
       
  4973 ** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
       
  4974 ** of the module.  Each module implementation will define
       
  4975 ** the content of a cursor structure to suit its own needs.
       
  4976 **
       
  4977 ** This superclass exists in order to define fields of the cursor that
       
  4978 ** are common to all implementations.
       
  4979 */
       
  4980 struct sqlite3_vtab_cursor {
       
  4981   sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
       
  4982   /* Virtual table implementations will typically add additional fields */
       
  4983 };
       
  4984 
       
  4985 /*
       
  4986 ** CAPI3REF: Declare The Schema Of A Virtual Table {H18280} <S20400>
       
  4987 ** EXPERIMENTAL
       
  4988 **
       
  4989 ** The [xCreate] and [xConnect] methods of a
       
  4990 ** [virtual table module] call this interface
       
  4991 ** to declare the format (the names and datatypes of the columns) of
       
  4992 ** the virtual tables they implement.
       
  4993 */
       
  4994 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
       
  4995 
       
  4996 /*
       
  4997 ** CAPI3REF: Overload A Function For A Virtual Table {H18300} <S20400>
       
  4998 ** EXPERIMENTAL
       
  4999 **
       
  5000 ** Virtual tables can provide alternative implementations of functions
       
  5001 ** using the [xFindFunction] method of the [virtual table module].  
       
  5002 ** But global versions of those functions
       
  5003 ** must exist in order to be overloaded.
       
  5004 **
       
  5005 ** This API makes sure a global version of a function with a particular
       
  5006 ** name and number of parameters exists.  If no such function exists
       
  5007 ** before this API is called, a new function is created.  The implementation
       
  5008 ** of the new function always causes an exception to be thrown.  So
       
  5009 ** the new function is not good for anything by itself.  Its only
       
  5010 ** purpose is to be a placeholder function that can be overloaded
       
  5011 ** by a [virtual table].
       
  5012 */
       
  5013 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
       
  5014 
       
  5015 /*
       
  5016 ** The interface to the virtual-table mechanism defined above (back up
       
  5017 ** to a comment remarkably similar to this one) is currently considered
       
  5018 ** to be experimental.  The interface might change in incompatible ways.
       
  5019 ** If this is a problem for you, do not use the interface at this time.
       
  5020 **
       
  5021 ** When the virtual-table mechanism stabilizes, we will declare the
       
  5022 ** interface fixed, support it indefinitely, and remove this comment.
       
  5023 **
       
  5024 ****** EXPERIMENTAL - subject to change without notice **************
       
  5025 */
       
  5026 
       
  5027 /*
       
  5028 ** CAPI3REF: A Handle To An Open BLOB {H17800} <S30230>
       
  5029 ** KEYWORDS: {BLOB handle} {BLOB handles}
       
  5030 **
       
  5031 ** An instance of this object represents an open BLOB on which
       
  5032 ** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
       
  5033 ** Objects of this type are created by [sqlite3_blob_open()]
       
  5034 ** and destroyed by [sqlite3_blob_close()].
       
  5035 ** The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
       
  5036 ** can be used to read or write small subsections of the BLOB.
       
  5037 ** The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
       
  5038 */
       
  5039 typedef struct sqlite3_blob sqlite3_blob;
       
  5040 
       
  5041 /*
       
  5042 ** CAPI3REF: Open A BLOB For Incremental I/O {H17810} <S30230>
       
  5043 **
       
  5044 ** This interfaces opens a [BLOB handle | handle] to the BLOB located
       
  5045 ** in row iRow, column zColumn, table zTable in database zDb;
       
  5046 ** in other words, the same BLOB that would be selected by:
       
  5047 **
       
  5048 ** <pre>
       
  5049 **     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
       
  5050 ** </pre> {END}
       
  5051 **
       
  5052 ** If the flags parameter is non-zero, then the BLOB is opened for read
       
  5053 ** and write access. If it is zero, the BLOB is opened for read access.
       
  5054 ** It is not possible to open a column that is part of an index or primary 
       
  5055 ** key for writing. ^If [foreign key constraints] are enabled, it is 
       
  5056 ** not possible to open a column that is part of a [child key] for writing.
       
  5057 **
       
  5058 ** Note that the database name is not the filename that contains
       
  5059 ** the database but rather the symbolic name of the database that
       
  5060 ** is assigned when the database is connected using [ATTACH].
       
  5061 ** For the main database file, the database name is "main".
       
  5062 ** For TEMP tables, the database name is "temp".
       
  5063 **
       
  5064 ** On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
       
  5065 ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
       
  5066 ** to be a null pointer.
       
  5067 ** This function sets the [database connection] error code and message
       
  5068 ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
       
  5069 ** functions.  Note that the *ppBlob variable is always initialized in a
       
  5070 ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
       
  5071 ** regardless of the success or failure of this routine.
       
  5072 **
       
  5073 ** If the row that a BLOB handle points to is modified by an
       
  5074 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
       
  5075 ** then the BLOB handle is marked as "expired".
       
  5076 ** This is true if any column of the row is changed, even a column
       
  5077 ** other than the one the BLOB handle is open on.
       
  5078 ** Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
       
  5079 ** a expired BLOB handle fail with an return code of [SQLITE_ABORT].
       
  5080 ** Changes written into a BLOB prior to the BLOB expiring are not
       
  5081 ** rollback by the expiration of the BLOB.  Such changes will eventually
       
  5082 ** commit if the transaction continues to completion.
       
  5083 **
       
  5084 ** Use the [sqlite3_blob_bytes()] interface to determine the size of
       
  5085 ** the opened blob.  The size of a blob may not be changed by this
       
  5086 ** interface.  Use the [UPDATE] SQL command to change the size of a
       
  5087 ** blob.
       
  5088 **
       
  5089 ** The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
       
  5090 ** and the built-in [zeroblob] SQL function can be used, if desired,
       
  5091 ** to create an empty, zero-filled blob in which to read or write using
       
  5092 ** this interface.
       
  5093 **
       
  5094 ** To avoid a resource leak, every open [BLOB handle] should eventually
       
  5095 ** be released by a call to [sqlite3_blob_close()].
       
  5096 **
       
  5097 ** Requirements:
       
  5098 ** [H17813] [H17814] [H17816] [H17819] [H17821] [H17824]
       
  5099 */
       
  5100 SQLITE_API int sqlite3_blob_open(
       
  5101   sqlite3*,
       
  5102   const char *zDb,
       
  5103   const char *zTable,
       
  5104   const char *zColumn,
       
  5105   sqlite3_int64 iRow,
       
  5106   int flags,
       
  5107   sqlite3_blob **ppBlob
       
  5108 );
       
  5109 
       
  5110 /*
       
  5111 ** CAPI3REF: Close A BLOB Handle {H17830} <S30230>
       
  5112 **
       
  5113 ** Closes an open [BLOB handle].
       
  5114 **
       
  5115 ** Closing a BLOB shall cause the current transaction to commit
       
  5116 ** if there are no other BLOBs, no pending prepared statements, and the
       
  5117 ** database connection is in [autocommit mode].
       
  5118 ** If any writes were made to the BLOB, they might be held in cache
       
  5119 ** until the close operation if they will fit.
       
  5120 **
       
  5121 ** Closing the BLOB often forces the changes
       
  5122 ** out to disk and so if any I/O errors occur, they will likely occur
       
  5123 ** at the time when the BLOB is closed.  Any errors that occur during
       
  5124 ** closing are reported as a non-zero return value.
       
  5125 **
       
  5126 ** The BLOB is closed unconditionally.  Even if this routine returns
       
  5127 ** an error code, the BLOB is still closed.
       
  5128 **
       
  5129 ** Calling this routine with a null pointer (which as would be returned
       
  5130 ** by failed call to [sqlite3_blob_open()]) is a harmless no-op.
       
  5131 **
       
  5132 ** Requirements:
       
  5133 ** [H17833] [H17836] [H17839]
       
  5134 */
       
  5135 SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
       
  5136 
       
  5137 /*
       
  5138 ** CAPI3REF: Return The Size Of An Open BLOB {H17840} <S30230>
       
  5139 **
       
  5140 ** Returns the size in bytes of the BLOB accessible via the 
       
  5141 ** successfully opened [BLOB handle] in its only argument.  The
       
  5142 ** incremental blob I/O routines can only read or overwriting existing
       
  5143 ** blob content; they cannot change the size of a blob.
       
  5144 **
       
  5145 ** This routine only works on a [BLOB handle] which has been created
       
  5146 ** by a prior successful call to [sqlite3_blob_open()] and which has not
       
  5147 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
       
  5148 ** to this routine results in undefined and probably undesirable behavior.
       
  5149 **
       
  5150 ** Requirements:
       
  5151 ** [H17843]
       
  5152 */
       
  5153 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
       
  5154 
       
  5155 /*
       
  5156 ** CAPI3REF: Read Data From A BLOB Incrementally {H17850} <S30230>
       
  5157 **
       
  5158 ** This function is used to read data from an open [BLOB handle] into a
       
  5159 ** caller-supplied buffer. N bytes of data are copied into buffer Z
       
  5160 ** from the open BLOB, starting at offset iOffset.
       
  5161 **
       
  5162 ** If offset iOffset is less than N bytes from the end of the BLOB,
       
  5163 ** [SQLITE_ERROR] is returned and no data is read.  If N or iOffset is
       
  5164 ** less than zero, [SQLITE_ERROR] is returned and no data is read.
       
  5165 ** The size of the blob (and hence the maximum value of N+iOffset)
       
  5166 ** can be determined using the [sqlite3_blob_bytes()] interface.
       
  5167 **
       
  5168 ** An attempt to read from an expired [BLOB handle] fails with an
       
  5169 ** error code of [SQLITE_ABORT].
       
  5170 **
       
  5171 ** On success, SQLITE_OK is returned.
       
  5172 ** Otherwise, an [error code] or an [extended error code] is returned.
       
  5173 **
       
  5174 ** This routine only works on a [BLOB handle] which has been created
       
  5175 ** by a prior successful call to [sqlite3_blob_open()] and which has not
       
  5176 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
       
  5177 ** to this routine results in undefined and probably undesirable behavior.
       
  5178 **
       
  5179 ** See also: [sqlite3_blob_write()].
       
  5180 **
       
  5181 ** Requirements:
       
  5182 ** [H17853] [H17856] [H17859] [H17862] [H17863] [H17865] [H17868]
       
  5183 */
       
  5184 SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
       
  5185 
       
  5186 /*
       
  5187 ** CAPI3REF: Write Data Into A BLOB Incrementally {H17870} <S30230>
       
  5188 **
       
  5189 ** This function is used to write data into an open [BLOB handle] from a
       
  5190 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
       
  5191 ** into the open BLOB, starting at offset iOffset.
       
  5192 **
       
  5193 ** If the [BLOB handle] passed as the first argument was not opened for
       
  5194 ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
       
  5195 ** this function returns [SQLITE_READONLY].
       
  5196 **
       
  5197 ** This function may only modify the contents of the BLOB; it is
       
  5198 ** not possible to increase the size of a BLOB using this API.
       
  5199 ** If offset iOffset is less than N bytes from the end of the BLOB,
       
  5200 ** [SQLITE_ERROR] is returned and no data is written.  If N is
       
  5201 ** less than zero [SQLITE_ERROR] is returned and no data is written.
       
  5202 ** The size of the BLOB (and hence the maximum value of N+iOffset)
       
  5203 ** can be determined using the [sqlite3_blob_bytes()] interface.
       
  5204 **
       
  5205 ** An attempt to write to an expired [BLOB handle] fails with an
       
  5206 ** error code of [SQLITE_ABORT].  Writes to the BLOB that occurred
       
  5207 ** before the [BLOB handle] expired are not rolled back by the
       
  5208 ** expiration of the handle, though of course those changes might
       
  5209 ** have been overwritten by the statement that expired the BLOB handle
       
  5210 ** or by other independent statements.
       
  5211 **
       
  5212 ** On success, SQLITE_OK is returned.
       
  5213 ** Otherwise, an  [error code] or an [extended error code] is returned.
       
  5214 **
       
  5215 ** This routine only works on a [BLOB handle] which has been created
       
  5216 ** by a prior successful call to [sqlite3_blob_open()] and which has not
       
  5217 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
       
  5218 ** to this routine results in undefined and probably undesirable behavior.
       
  5219 **
       
  5220 ** See also: [sqlite3_blob_read()].
       
  5221 **
       
  5222 ** Requirements:
       
  5223 ** [H17873] [H17874] [H17875] [H17876] [H17877] [H17879] [H17882] [H17885]
       
  5224 ** [H17888]
       
  5225 */
       
  5226 SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
       
  5227 
       
  5228 /*
       
  5229 ** CAPI3REF: Virtual File System Objects {H11200} <S20100>
       
  5230 **
       
  5231 ** A virtual filesystem (VFS) is an [sqlite3_vfs] object
       
  5232 ** that SQLite uses to interact
       
  5233 ** with the underlying operating system.  Most SQLite builds come with a
       
  5234 ** single default VFS that is appropriate for the host computer.
       
  5235 ** New VFSes can be registered and existing VFSes can be unregistered.
       
  5236 ** The following interfaces are provided.
       
  5237 **
       
  5238 ** The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
       
  5239 ** Names are case sensitive.
       
  5240 ** Names are zero-terminated UTF-8 strings.
       
  5241 ** If there is no match, a NULL pointer is returned.
       
  5242 ** If zVfsName is NULL then the default VFS is returned.
       
  5243 **
       
  5244 ** New VFSes are registered with sqlite3_vfs_register().
       
  5245 ** Each new VFS becomes the default VFS if the makeDflt flag is set.
       
  5246 ** The same VFS can be registered multiple times without injury.
       
  5247 ** To make an existing VFS into the default VFS, register it again
       
  5248 ** with the makeDflt flag set.  If two different VFSes with the
       
  5249 ** same name are registered, the behavior is undefined.  If a
       
  5250 ** VFS is registered with a name that is NULL or an empty string,
       
  5251 ** then the behavior is undefined.
       
  5252 **
       
  5253 ** Unregister a VFS with the sqlite3_vfs_unregister() interface.
       
  5254 ** If the default VFS is unregistered, another VFS is chosen as
       
  5255 ** the default.  The choice for the new VFS is arbitrary.
       
  5256 **
       
  5257 ** Requirements:
       
  5258 ** [H11203] [H11206] [H11209] [H11212] [H11215] [H11218]
       
  5259 */
       
  5260 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
       
  5261 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
       
  5262 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
       
  5263 
       
  5264 /*
       
  5265 ** CAPI3REF: Mutexes {H17000} <S20000>
       
  5266 **
       
  5267 ** The SQLite core uses these routines for thread
       
  5268 ** synchronization. Though they are intended for internal
       
  5269 ** use by SQLite, code that links against SQLite is
       
  5270 ** permitted to use any of these routines.
       
  5271 **
       
  5272 ** The SQLite source code contains multiple implementations
       
  5273 ** of these mutex routines.  An appropriate implementation
       
  5274 ** is selected automatically at compile-time.  The following
       
  5275 ** implementations are available in the SQLite core:
       
  5276 **
       
  5277 ** <ul>
       
  5278 ** <li>   SQLITE_MUTEX_OS2
       
  5279 ** <li>   SQLITE_MUTEX_PTHREAD
       
  5280 ** <li>   SQLITE_MUTEX_W32
       
  5281 ** <li>   SQLITE_MUTEX_NOOP
       
  5282 ** </ul>
       
  5283 **
       
  5284 ** The SQLITE_MUTEX_NOOP implementation is a set of routines
       
  5285 ** that does no real locking and is appropriate for use in
       
  5286 ** a single-threaded application.  The SQLITE_MUTEX_OS2,
       
  5287 ** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations
       
  5288 ** are appropriate for use on OS/2, Unix, and Windows.
       
  5289 **
       
  5290 ** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
       
  5291 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
       
  5292 ** implementation is included with the library. In this case the
       
  5293 ** application must supply a custom mutex implementation using the
       
  5294 ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
       
  5295 ** before calling sqlite3_initialize() or any other public sqlite3_
       
  5296 ** function that calls sqlite3_initialize().
       
  5297 **
       
  5298 ** {H17011} The sqlite3_mutex_alloc() routine allocates a new
       
  5299 ** mutex and returns a pointer to it. {H17012} If it returns NULL
       
  5300 ** that means that a mutex could not be allocated. {H17013} SQLite
       
  5301 ** will unwind its stack and return an error. {H17014} The argument
       
  5302 ** to sqlite3_mutex_alloc() is one of these integer constants:
       
  5303 **
       
  5304 ** <ul>
       
  5305 ** <li>  SQLITE_MUTEX_FAST
       
  5306 ** <li>  SQLITE_MUTEX_RECURSIVE
       
  5307 ** <li>  SQLITE_MUTEX_STATIC_MASTER
       
  5308 ** <li>  SQLITE_MUTEX_STATIC_MEM
       
  5309 ** <li>  SQLITE_MUTEX_STATIC_MEM2
       
  5310 ** <li>  SQLITE_MUTEX_STATIC_PRNG
       
  5311 ** <li>  SQLITE_MUTEX_STATIC_LRU
       
  5312 ** <li>  SQLITE_MUTEX_STATIC_LRU2
       
  5313 ** </ul>
       
  5314 **
       
  5315 ** {H17015} The first two constants cause sqlite3_mutex_alloc() to create
       
  5316 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
       
  5317 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. {END}
       
  5318 ** The mutex implementation does not need to make a distinction
       
  5319 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
       
  5320 ** not want to.  {H17016} But SQLite will only request a recursive mutex in
       
  5321 ** cases where it really needs one.  {END} If a faster non-recursive mutex
       
  5322 ** implementation is available on the host platform, the mutex subsystem
       
  5323 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
       
  5324 **
       
  5325 ** {H17017} The other allowed parameters to sqlite3_mutex_alloc() each return
       
  5326 ** a pointer to a static preexisting mutex. {END}  Six static mutexes are
       
  5327 ** used by the current version of SQLite.  Future versions of SQLite
       
  5328 ** may add additional static mutexes.  Static mutexes are for internal
       
  5329 ** use by SQLite only.  Applications that use SQLite mutexes should
       
  5330 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
       
  5331 ** SQLITE_MUTEX_RECURSIVE.
       
  5332 **
       
  5333 ** {H17018} Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
       
  5334 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
       
  5335 ** returns a different mutex on every call.  {H17034} But for the static
       
  5336 ** mutex types, the same mutex is returned on every call that has
       
  5337 ** the same type number.
       
  5338 **
       
  5339 ** {H17019} The sqlite3_mutex_free() routine deallocates a previously
       
  5340 ** allocated dynamic mutex. {H17020} SQLite is careful to deallocate every
       
  5341 ** dynamic mutex that it allocates. {A17021} The dynamic mutexes must not be in
       
  5342 ** use when they are deallocated. {A17022} Attempting to deallocate a static
       
  5343 ** mutex results in undefined behavior. {H17023} SQLite never deallocates
       
  5344 ** a static mutex. {END}
       
  5345 **
       
  5346 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
       
  5347 ** to enter a mutex. {H17024} If another thread is already within the mutex,
       
  5348 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
       
  5349 ** SQLITE_BUSY. {H17025}  The sqlite3_mutex_try() interface returns [SQLITE_OK]
       
  5350 ** upon successful entry.  {H17026} Mutexes created using
       
  5351 ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
       
  5352 ** {H17027} In such cases the,
       
  5353 ** mutex must be exited an equal number of times before another thread
       
  5354 ** can enter.  {A17028} If the same thread tries to enter any other
       
  5355 ** kind of mutex more than once, the behavior is undefined.
       
  5356 ** {H17029} SQLite will never exhibit
       
  5357 ** such behavior in its own use of mutexes.
       
  5358 **
       
  5359 ** Some systems (for example, Windows 95) do not support the operation
       
  5360 ** implemented by sqlite3_mutex_try().  On those systems, sqlite3_mutex_try()
       
  5361 ** will always return SQLITE_BUSY.  {H17030} The SQLite core only ever uses
       
  5362 ** sqlite3_mutex_try() as an optimization so this is acceptable behavior.
       
  5363 **
       
  5364 ** {H17031} The sqlite3_mutex_leave() routine exits a mutex that was
       
  5365 ** previously entered by the same thread.  {A17032} The behavior
       
  5366 ** is undefined if the mutex is not currently entered by the
       
  5367 ** calling thread or is not currently allocated.  {H17033} SQLite will
       
  5368 ** never do either. {END}
       
  5369 **
       
  5370 ** If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
       
  5371 ** sqlite3_mutex_leave() is a NULL pointer, then all three routines
       
  5372 ** behave as no-ops.
       
  5373 **
       
  5374 ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
       
  5375 */
       
  5376 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
       
  5377 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
       
  5378 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
       
  5379 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
       
  5380 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
       
  5381 
       
  5382 /*
       
  5383 ** CAPI3REF: Mutex Methods Object {H17120} <S20130>
       
  5384 ** EXPERIMENTAL
       
  5385 **
       
  5386 ** An instance of this structure defines the low-level routines
       
  5387 ** used to allocate and use mutexes.
       
  5388 **
       
  5389 ** Usually, the default mutex implementations provided by SQLite are
       
  5390 ** sufficient, however the user has the option of substituting a custom
       
  5391 ** implementation for specialized deployments or systems for which SQLite
       
  5392 ** does not provide a suitable implementation. In this case, the user
       
  5393 ** creates and populates an instance of this structure to pass
       
  5394 ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
       
  5395 ** Additionally, an instance of this structure can be used as an
       
  5396 ** output variable when querying the system for the current mutex
       
  5397 ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
       
  5398 **
       
  5399 ** The xMutexInit method defined by this structure is invoked as
       
  5400 ** part of system initialization by the sqlite3_initialize() function.
       
  5401 ** {H17001} The xMutexInit routine shall be called by SQLite once for each
       
  5402 ** effective call to [sqlite3_initialize()].
       
  5403 **
       
  5404 ** The xMutexEnd method defined by this structure is invoked as
       
  5405 ** part of system shutdown by the sqlite3_shutdown() function. The
       
  5406 ** implementation of this method is expected to release all outstanding
       
  5407 ** resources obtained by the mutex methods implementation, especially
       
  5408 ** those obtained by the xMutexInit method. {H17003} The xMutexEnd()
       
  5409 ** interface shall be invoked once for each call to [sqlite3_shutdown()].
       
  5410 **
       
  5411 ** The remaining seven methods defined by this structure (xMutexAlloc,
       
  5412 ** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
       
  5413 ** xMutexNotheld) implement the following interfaces (respectively):
       
  5414 **
       
  5415 ** <ul>
       
  5416 **   <li>  [sqlite3_mutex_alloc()] </li>
       
  5417 **   <li>  [sqlite3_mutex_free()] </li>
       
  5418 **   <li>  [sqlite3_mutex_enter()] </li>
       
  5419 **   <li>  [sqlite3_mutex_try()] </li>
       
  5420 **   <li>  [sqlite3_mutex_leave()] </li>
       
  5421 **   <li>  [sqlite3_mutex_held()] </li>
       
  5422 **   <li>  [sqlite3_mutex_notheld()] </li>
       
  5423 ** </ul>
       
  5424 **
       
  5425 ** The only difference is that the public sqlite3_XXX functions enumerated
       
  5426 ** above silently ignore any invocations that pass a NULL pointer instead
       
  5427 ** of a valid mutex handle. The implementations of the methods defined
       
  5428 ** by this structure are not required to handle this case, the results
       
  5429 ** of passing a NULL pointer instead of a valid mutex handle are undefined
       
  5430 ** (i.e. it is acceptable to provide an implementation that segfaults if
       
  5431 ** it is passed a NULL pointer).
       
  5432 **
       
  5433 ** The xMutexInit() method must be threadsafe.  It must be harmless to
       
  5434 ** invoke xMutexInit() mutiple times within the same process and without
       
  5435 ** intervening calls to xMutexEnd().  Second and subsequent calls to
       
  5436 ** xMutexInit() must be no-ops.
       
  5437 **
       
  5438 ** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
       
  5439 ** and its associates).  Similarly, xMutexAlloc() must not use SQLite memory
       
  5440 ** allocation for a static mutex.  However xMutexAlloc() may use SQLite
       
  5441 ** memory allocation for a fast or recursive mutex.
       
  5442 **
       
  5443 ** SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
       
  5444 ** called, but only if the prior call to xMutexInit returned SQLITE_OK.
       
  5445 ** If xMutexInit fails in any way, it is expected to clean up after itself
       
  5446 ** prior to returning.
       
  5447 */
       
  5448 typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
       
  5449 struct sqlite3_mutex_methods {
       
  5450   int (*xMutexInit)(void);
       
  5451   int (*xMutexEnd)(void);
       
  5452   sqlite3_mutex *(*xMutexAlloc)(int);
       
  5453   void (*xMutexFree)(sqlite3_mutex *);
       
  5454   void (*xMutexEnter)(sqlite3_mutex *);
       
  5455   int (*xMutexTry)(sqlite3_mutex *);
       
  5456   void (*xMutexLeave)(sqlite3_mutex *);
       
  5457   int (*xMutexHeld)(sqlite3_mutex *);
       
  5458   int (*xMutexNotheld)(sqlite3_mutex *);
       
  5459 };
       
  5460 
       
  5461 /*
       
  5462 ** CAPI3REF: Mutex Verification Routines {H17080} <S20130> <S30800>
       
  5463 **
       
  5464 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
       
  5465 ** are intended for use inside assert() statements. {H17081} The SQLite core
       
  5466 ** never uses these routines except inside an assert() and applications
       
  5467 ** are advised to follow the lead of the core.  {H17082} The core only
       
  5468 ** provides implementations for these routines when it is compiled
       
  5469 ** with the SQLITE_DEBUG flag.  {A17087} External mutex implementations
       
  5470 ** are only required to provide these routines if SQLITE_DEBUG is
       
  5471 ** defined and if NDEBUG is not defined.
       
  5472 **
       
  5473 ** {H17083} These routines should return true if the mutex in their argument
       
  5474 ** is held or not held, respectively, by the calling thread.
       
  5475 **
       
  5476 ** {X17084} The implementation is not required to provided versions of these
       
  5477 ** routines that actually work. If the implementation does not provide working
       
  5478 ** versions of these routines, it should at least provide stubs that always
       
  5479 ** return true so that one does not get spurious assertion failures.
       
  5480 **
       
  5481 ** {H17085} If the argument to sqlite3_mutex_held() is a NULL pointer then
       
  5482 ** the routine should return 1.  {END} This seems counter-intuitive since
       
  5483 ** clearly the mutex cannot be held if it does not exist.  But the
       
  5484 ** the reason the mutex does not exist is because the build is not
       
  5485 ** using mutexes.  And we do not want the assert() containing the
       
  5486 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
       
  5487 ** the appropriate thing to do.  {H17086} The sqlite3_mutex_notheld()
       
  5488 ** interface should also return 1 when given a NULL pointer.
       
  5489 */
       
  5490 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
       
  5491 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
       
  5492 
       
  5493 /*
       
  5494 ** CAPI3REF: Mutex Types {H17001} <H17000>
       
  5495 **
       
  5496 ** The [sqlite3_mutex_alloc()] interface takes a single argument
       
  5497 ** which is one of these integer constants.
       
  5498 **
       
  5499 ** The set of static mutexes may change from one SQLite release to the
       
  5500 ** next.  Applications that override the built-in mutex logic must be
       
  5501 ** prepared to accommodate additional static mutexes.
       
  5502 */
       
  5503 #define SQLITE_MUTEX_FAST             0
       
  5504 #define SQLITE_MUTEX_RECURSIVE        1
       
  5505 #define SQLITE_MUTEX_STATIC_MASTER    2
       
  5506 #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
       
  5507 #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
       
  5508 #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
       
  5509 #define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
       
  5510 #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
       
  5511 #define SQLITE_MUTEX_STATIC_LRU2      7  /* lru page list */
       
  5512 
       
  5513 /*
       
  5514 ** CAPI3REF: Retrieve the mutex for a database connection {H17002} <H17000>
       
  5515 **
       
  5516 ** This interface returns a pointer the [sqlite3_mutex] object that 
       
  5517 ** serializes access to the [database connection] given in the argument
       
  5518 ** when the [threading mode] is Serialized.
       
  5519 ** If the [threading mode] is Single-thread or Multi-thread then this
       
  5520 ** routine returns a NULL pointer.
       
  5521 */
       
  5522 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
       
  5523 
       
  5524 /*
       
  5525 ** CAPI3REF: Low-Level Control Of Database Files {H11300} <S30800>
       
  5526 **
       
  5527 ** {H11301} The [sqlite3_file_control()] interface makes a direct call to the
       
  5528 ** xFileControl method for the [sqlite3_io_methods] object associated
       
  5529 ** with a particular database identified by the second argument. {H11302} The
       
  5530 ** name of the database is the name assigned to the database by the
       
  5531 ** <a href="lang_attach.html">ATTACH</a> SQL command that opened the
       
  5532 ** database. {H11303} To control the main database file, use the name "main"
       
  5533 ** or a NULL pointer. {H11304} The third and fourth parameters to this routine
       
  5534 ** are passed directly through to the second and third parameters of
       
  5535 ** the xFileControl method.  {H11305} The return value of the xFileControl
       
  5536 ** method becomes the return value of this routine.
       
  5537 **
       
  5538 ** {H11306} If the second parameter (zDbName) does not match the name of any
       
  5539 ** open database file, then SQLITE_ERROR is returned. {H11307} This error
       
  5540 ** code is not remembered and will not be recalled by [sqlite3_errcode()]
       
  5541 ** or [sqlite3_errmsg()]. {A11308} The underlying xFileControl method might
       
  5542 ** also return SQLITE_ERROR.  {A11309} There is no way to distinguish between
       
  5543 ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
       
  5544 ** xFileControl method. {END}
       
  5545 **
       
  5546 ** See also: [SQLITE_FCNTL_LOCKSTATE]
       
  5547 */
       
  5548 SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
       
  5549 
       
  5550 /*
       
  5551 ** CAPI3REF: Testing Interface {H11400} <S30800>
       
  5552 **
       
  5553 ** The sqlite3_test_control() interface is used to read out internal
       
  5554 ** state of SQLite and to inject faults into SQLite for testing
       
  5555 ** purposes.  The first parameter is an operation code that determines
       
  5556 ** the number, meaning, and operation of all subsequent parameters.
       
  5557 **
       
  5558 ** This interface is not for use by applications.  It exists solely
       
  5559 ** for verifying the correct operation of the SQLite library.  Depending
       
  5560 ** on how the SQLite library is compiled, this interface might not exist.
       
  5561 **
       
  5562 ** The details of the operation codes, their meanings, the parameters
       
  5563 ** they take, and what they do are all subject to change without notice.
       
  5564 ** Unlike most of the SQLite API, this function is not guaranteed to
       
  5565 ** operate consistently from one release to the next.
       
  5566 */
       
  5567 SQLITE_API int sqlite3_test_control(int op, ...);
       
  5568 
       
  5569 /*
       
  5570 ** CAPI3REF: Testing Interface Operation Codes {H11410} <H11400>
       
  5571 **
       
  5572 ** These constants are the valid operation code parameters used
       
  5573 ** as the first argument to [sqlite3_test_control()].
       
  5574 **
       
  5575 ** These parameters and their meanings are subject to change
       
  5576 ** without notice.  These values are for testing purposes only.
       
  5577 ** Applications should not use any of these parameters or the
       
  5578 ** [sqlite3_test_control()] interface.
       
  5579 */
       
  5580 #define SQLITE_TESTCTRL_PRNG_SAVE                5
       
  5581 #define SQLITE_TESTCTRL_PRNG_RESTORE             6
       
  5582 #define SQLITE_TESTCTRL_PRNG_RESET               7
       
  5583 #define SQLITE_TESTCTRL_BITVEC_TEST              8
       
  5584 #define SQLITE_TESTCTRL_FAULT_INSTALL            9
       
  5585 #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
       
  5586 #define SQLITE_TESTCTRL_PENDING_BYTE            11
       
  5587 #define SQLITE_TESTCTRL_ASSERT                  12
       
  5588 #define SQLITE_TESTCTRL_ALWAYS                  13
       
  5589 #define SQLITE_TESTCTRL_RESERVE                 14
       
  5590 
       
  5591 /*
       
  5592 ** CAPI3REF: SQLite Runtime Status {H17200} <S60200>
       
  5593 ** EXPERIMENTAL
       
  5594 **
       
  5595 ** This interface is used to retrieve runtime status information
       
  5596 ** about the preformance of SQLite, and optionally to reset various
       
  5597 ** highwater marks.  The first argument is an integer code for
       
  5598 ** the specific parameter to measure.  Recognized integer codes
       
  5599 ** are of the form [SQLITE_STATUS_MEMORY_USED | SQLITE_STATUS_...].
       
  5600 ** The current value of the parameter is returned into *pCurrent.
       
  5601 ** The highest recorded value is returned in *pHighwater.  If the
       
  5602 ** resetFlag is true, then the highest record value is reset after
       
  5603 ** *pHighwater is written. Some parameters do not record the highest
       
  5604 ** value.  For those parameters
       
  5605 ** nothing is written into *pHighwater and the resetFlag is ignored.
       
  5606 ** Other parameters record only the highwater mark and not the current
       
  5607 ** value.  For these latter parameters nothing is written into *pCurrent.
       
  5608 **
       
  5609 ** This routine returns SQLITE_OK on success and a non-zero
       
  5610 ** [error code] on failure.
       
  5611 **
       
  5612 ** This routine is threadsafe but is not atomic.  This routine can be
       
  5613 ** called while other threads are running the same or different SQLite
       
  5614 ** interfaces.  However the values returned in *pCurrent and
       
  5615 ** *pHighwater reflect the status of SQLite at different points in time
       
  5616 ** and it is possible that another thread might change the parameter
       
  5617 ** in between the times when *pCurrent and *pHighwater are written.
       
  5618 **
       
  5619 ** See also: [sqlite3_db_status()]
       
  5620 */
       
  5621 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
       
  5622 
       
  5623 
       
  5624 /*
       
  5625 ** CAPI3REF: Status Parameters {H17250} <H17200>
       
  5626 ** EXPERIMENTAL
       
  5627 **
       
  5628 ** These integer constants designate various run-time status parameters
       
  5629 ** that can be returned by [sqlite3_status()].
       
  5630 **
       
  5631 ** <dl>
       
  5632 ** <dt>SQLITE_STATUS_MEMORY_USED</dt>
       
  5633 ** <dd>This parameter is the current amount of memory checked out
       
  5634 ** using [sqlite3_malloc()], either directly or indirectly.  The
       
  5635 ** figure includes calls made to [sqlite3_malloc()] by the application
       
  5636 ** and internal memory usage by the SQLite library.  Scratch memory
       
  5637 ** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
       
  5638 ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
       
  5639 ** this parameter.  The amount returned is the sum of the allocation
       
  5640 ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>
       
  5641 **
       
  5642 ** <dt>SQLITE_STATUS_MALLOC_SIZE</dt>
       
  5643 ** <dd>This parameter records the largest memory allocation request
       
  5644 ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
       
  5645 ** internal equivalents).  Only the value returned in the
       
  5646 ** *pHighwater parameter to [sqlite3_status()] is of interest.  
       
  5647 ** The value written into the *pCurrent parameter is undefined.</dd>
       
  5648 **
       
  5649 ** <dt>SQLITE_STATUS_PAGECACHE_USED</dt>
       
  5650 ** <dd>This parameter returns the number of pages used out of the
       
  5651 ** [pagecache memory allocator] that was configured using 
       
  5652 ** [SQLITE_CONFIG_PAGECACHE].  The
       
  5653 ** value returned is in pages, not in bytes.</dd>
       
  5654 **
       
  5655 ** <dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
       
  5656 ** <dd>This parameter returns the number of bytes of page cache
       
  5657 ** allocation which could not be statisfied by the [SQLITE_CONFIG_PAGECACHE]
       
  5658 ** buffer and where forced to overflow to [sqlite3_malloc()].  The
       
  5659 ** returned value includes allocations that overflowed because they
       
  5660 ** where too large (they were larger than the "sz" parameter to
       
  5661 ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
       
  5662 ** no space was left in the page cache.</dd>
       
  5663 **
       
  5664 ** <dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
       
  5665 ** <dd>This parameter records the largest memory allocation request
       
  5666 ** handed to [pagecache memory allocator].  Only the value returned in the
       
  5667 ** *pHighwater parameter to [sqlite3_status()] is of interest.  
       
  5668 ** The value written into the *pCurrent parameter is undefined.</dd>
       
  5669 **
       
  5670 ** <dt>SQLITE_STATUS_SCRATCH_USED</dt>
       
  5671 ** <dd>This parameter returns the number of allocations used out of the
       
  5672 ** [scratch memory allocator] configured using
       
  5673 ** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
       
  5674 ** in bytes.  Since a single thread may only have one scratch allocation
       
  5675 ** outstanding at time, this parameter also reports the number of threads
       
  5676 ** using scratch memory at the same time.</dd>
       
  5677 **
       
  5678 ** <dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
       
  5679 ** <dd>This parameter returns the number of bytes of scratch memory
       
  5680 ** allocation which could not be statisfied by the [SQLITE_CONFIG_SCRATCH]
       
  5681 ** buffer and where forced to overflow to [sqlite3_malloc()].  The values
       
  5682 ** returned include overflows because the requested allocation was too
       
  5683 ** larger (that is, because the requested allocation was larger than the
       
  5684 ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
       
  5685 ** slots were available.
       
  5686 ** </dd>
       
  5687 **
       
  5688 ** <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
       
  5689 ** <dd>This parameter records the largest memory allocation request
       
  5690 ** handed to [scratch memory allocator].  Only the value returned in the
       
  5691 ** *pHighwater parameter to [sqlite3_status()] is of interest.  
       
  5692 ** The value written into the *pCurrent parameter is undefined.</dd>
       
  5693 **
       
  5694 ** <dt>SQLITE_STATUS_PARSER_STACK</dt>
       
  5695 ** <dd>This parameter records the deepest parser stack.  It is only
       
  5696 ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>
       
  5697 ** </dl>
       
  5698 **
       
  5699 ** New status parameters may be added from time to time.
       
  5700 */
       
  5701 #define SQLITE_STATUS_MEMORY_USED          0
       
  5702 #define SQLITE_STATUS_PAGECACHE_USED       1
       
  5703 #define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
       
  5704 #define SQLITE_STATUS_SCRATCH_USED         3
       
  5705 #define SQLITE_STATUS_SCRATCH_OVERFLOW     4
       
  5706 #define SQLITE_STATUS_MALLOC_SIZE          5
       
  5707 #define SQLITE_STATUS_PARSER_STACK         6
       
  5708 #define SQLITE_STATUS_PAGECACHE_SIZE       7
       
  5709 #define SQLITE_STATUS_SCRATCH_SIZE         8
       
  5710 
       
  5711 /*
       
  5712 ** CAPI3REF: Database Connection Status {H17500} <S60200>
       
  5713 ** EXPERIMENTAL
       
  5714 **
       
  5715 ** This interface is used to retrieve runtime status information 
       
  5716 ** about a single [database connection].  The first argument is the
       
  5717 ** database connection object to be interrogated.  The second argument
       
  5718 ** is the parameter to interrogate.  Currently, the only allowed value
       
  5719 ** for the second parameter is [SQLITE_DBSTATUS_LOOKASIDE_USED].
       
  5720 ** Additional options will likely appear in future releases of SQLite.
       
  5721 **
       
  5722 ** The current value of the requested parameter is written into *pCur
       
  5723 ** and the highest instantaneous value is written into *pHiwtr.  If
       
  5724 ** the resetFlg is true, then the highest instantaneous value is
       
  5725 ** reset back down to the current value.
       
  5726 **
       
  5727 ** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
       
  5728 */
       
  5729 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
       
  5730 
       
  5731 /*
       
  5732 ** CAPI3REF: Status Parameters for database connections {H17520} <H17500>
       
  5733 ** EXPERIMENTAL
       
  5734 **
       
  5735 ** These constants are the available integer "verbs" that can be passed as
       
  5736 ** the second argument to the [sqlite3_db_status()] interface.
       
  5737 **
       
  5738 ** New verbs may be added in future releases of SQLite. Existing verbs
       
  5739 ** might be discontinued. Applications should check the return code from
       
  5740 ** [sqlite3_db_status()] to make sure that the call worked.
       
  5741 ** The [sqlite3_db_status()] interface will return a non-zero error code
       
  5742 ** if a discontinued or unsupported verb is invoked.
       
  5743 **
       
  5744 ** <dl>
       
  5745 ** <dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
       
  5746 ** <dd>This parameter returns the number of lookaside memory slots currently
       
  5747 ** checked out.</dd>
       
  5748 ** </dl>
       
  5749 */
       
  5750 #define SQLITE_DBSTATUS_LOOKASIDE_USED     0
       
  5751 
       
  5752 
       
  5753 /*
       
  5754 ** CAPI3REF: Prepared Statement Status {H17550} <S60200>
       
  5755 ** EXPERIMENTAL
       
  5756 **
       
  5757 ** Each prepared statement maintains various
       
  5758 ** [SQLITE_STMTSTATUS_SORT | counters] that measure the number
       
  5759 ** of times it has performed specific operations.  These counters can
       
  5760 ** be used to monitor the performance characteristics of the prepared
       
  5761 ** statements.  For example, if the number of table steps greatly exceeds
       
  5762 ** the number of table searches or result rows, that would tend to indicate
       
  5763 ** that the prepared statement is using a full table scan rather than
       
  5764 ** an index.  
       
  5765 **
       
  5766 ** This interface is used to retrieve and reset counter values from
       
  5767 ** a [prepared statement].  The first argument is the prepared statement
       
  5768 ** object to be interrogated.  The second argument
       
  5769 ** is an integer code for a specific [SQLITE_STMTSTATUS_SORT | counter]
       
  5770 ** to be interrogated. 
       
  5771 ** The current value of the requested counter is returned.
       
  5772 ** If the resetFlg is true, then the counter is reset to zero after this
       
  5773 ** interface call returns.
       
  5774 **
       
  5775 ** See also: [sqlite3_status()] and [sqlite3_db_status()].
       
  5776 */
       
  5777 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
       
  5778 
       
  5779 /*
       
  5780 ** CAPI3REF: Status Parameters for prepared statements {H17570} <H17550>
       
  5781 ** EXPERIMENTAL
       
  5782 **
       
  5783 ** These preprocessor macros define integer codes that name counter
       
  5784 ** values associated with the [sqlite3_stmt_status()] interface.
       
  5785 ** The meanings of the various counters are as follows:
       
  5786 **
       
  5787 ** <dl>
       
  5788 ** <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
       
  5789 ** <dd>This is the number of times that SQLite has stepped forward in
       
  5790 ** a table as part of a full table scan.  Large numbers for this counter
       
  5791 ** may indicate opportunities for performance improvement through 
       
  5792 ** careful use of indices.</dd>
       
  5793 **
       
  5794 ** <dt>SQLITE_STMTSTATUS_SORT</dt>
       
  5795 ** <dd>This is the number of sort operations that have occurred.
       
  5796 ** A non-zero value in this counter may indicate an opportunity to
       
  5797 ** improvement performance through careful use of indices.</dd>
       
  5798 **
       
  5799 ** </dl>
       
  5800 */
       
  5801 #define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
       
  5802 #define SQLITE_STMTSTATUS_SORT              2
       
  5803 
       
  5804 /*
       
  5805 ** CAPI3REF: Custom Page Cache Object
       
  5806 ** EXPERIMENTAL
       
  5807 **
       
  5808 ** The sqlite3_pcache type is opaque.  It is implemented by
       
  5809 ** the pluggable module.  The SQLite core has no knowledge of
       
  5810 ** its size or internal structure and never deals with the
       
  5811 ** sqlite3_pcache object except by holding and passing pointers
       
  5812 ** to the object.
       
  5813 **
       
  5814 ** See [sqlite3_pcache_methods] for additional information.
       
  5815 */
       
  5816 typedef struct sqlite3_pcache sqlite3_pcache;
       
  5817 
       
  5818 /*
       
  5819 ** CAPI3REF: Application Defined Page Cache.
       
  5820 ** KEYWORDS: {page cache}
       
  5821 ** EXPERIMENTAL
       
  5822 **
       
  5823 ** The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can
       
  5824 ** register an alternative page cache implementation by passing in an 
       
  5825 ** instance of the sqlite3_pcache_methods structure. The majority of the 
       
  5826 ** heap memory used by SQLite is used by the page cache to cache data read 
       
  5827 ** from, or ready to be written to, the database file. By implementing a 
       
  5828 ** custom page cache using this API, an application can control more 
       
  5829 ** precisely the amount of memory consumed by SQLite, the way in which 
       
  5830 ** that memory is allocated and released, and the policies used to 
       
  5831 ** determine exactly which parts of a database file are cached and for 
       
  5832 ** how long.
       
  5833 **
       
  5834 ** The contents of the sqlite3_pcache_methods structure are copied to an
       
  5835 ** internal buffer by SQLite within the call to [sqlite3_config].  Hence
       
  5836 ** the application may discard the parameter after the call to
       
  5837 ** [sqlite3_config()] returns.
       
  5838 **
       
  5839 ** The xInit() method is called once for each call to [sqlite3_initialize()]
       
  5840 ** (usually only once during the lifetime of the process). It is passed
       
  5841 ** a copy of the sqlite3_pcache_methods.pArg value. It can be used to set
       
  5842 ** up global structures and mutexes required by the custom page cache 
       
  5843 ** implementation. 
       
  5844 **
       
  5845 ** The xShutdown() method is called from within [sqlite3_shutdown()], 
       
  5846 ** if the application invokes this API. It can be used to clean up 
       
  5847 ** any outstanding resources before process shutdown, if required.
       
  5848 **
       
  5849 ** SQLite holds a [SQLITE_MUTEX_RECURSIVE] mutex when it invokes
       
  5850 ** the xInit method, so the xInit method need not be threadsafe.  The
       
  5851 ** xShutdown method is only called from [sqlite3_shutdown()] so it does
       
  5852 ** not need to be threadsafe either.  All other methods must be threadsafe
       
  5853 ** in multithreaded applications.
       
  5854 **
       
  5855 ** SQLite will never invoke xInit() more than once without an intervening
       
  5856 ** call to xShutdown().
       
  5857 **
       
  5858 ** The xCreate() method is used to construct a new cache instance.  SQLite
       
  5859 ** will typically create one cache instance for each open database file,
       
  5860 ** though this is not guaranteed. The
       
  5861 ** first parameter, szPage, is the size in bytes of the pages that must
       
  5862 ** be allocated by the cache.  szPage will not be a power of two.  szPage
       
  5863 ** will the page size of the database file that is to be cached plus an
       
  5864 ** increment (here called "R") of about 100 or 200.  SQLite will use the
       
  5865 ** extra R bytes on each page to store metadata about the underlying
       
  5866 ** database page on disk.  The value of R depends
       
  5867 ** on the SQLite version, the target platform, and how SQLite was compiled.
       
  5868 ** R is constant for a particular build of SQLite.  The second argument to
       
  5869 ** xCreate(), bPurgeable, is true if the cache being created will
       
  5870 ** be used to cache database pages of a file stored on disk, or
       
  5871 ** false if it is used for an in-memory database. The cache implementation
       
  5872 ** does not have to do anything special based with the value of bPurgeable;
       
  5873 ** it is purely advisory.  On a cache where bPurgeable is false, SQLite will
       
  5874 ** never invoke xUnpin() except to deliberately delete a page.
       
  5875 ** In other words, a cache created with bPurgeable set to false will
       
  5876 ** never contain any unpinned pages.
       
  5877 **
       
  5878 ** The xCachesize() method may be called at any time by SQLite to set the
       
  5879 ** suggested maximum cache-size (number of pages stored by) the cache
       
  5880 ** instance passed as the first argument. This is the value configured using
       
  5881 ** the SQLite "[PRAGMA cache_size]" command. As with the bPurgeable parameter,
       
  5882 ** the implementation is not required to do anything with this
       
  5883 ** value; it is advisory only.
       
  5884 **
       
  5885 ** The xPagecount() method should return the number of pages currently
       
  5886 ** stored in the cache.
       
  5887 ** 
       
  5888 ** The xFetch() method is used to fetch a page and return a pointer to it. 
       
  5889 ** A 'page', in this context, is a buffer of szPage bytes aligned at an
       
  5890 ** 8-byte boundary. The page to be fetched is determined by the key. The
       
  5891 ** mimimum key value is 1. After it has been retrieved using xFetch, the page 
       
  5892 ** is considered to be "pinned".
       
  5893 **
       
  5894 ** If the requested page is already in the page cache, then the page cache
       
  5895 ** implementation must return a pointer to the page buffer with its content
       
  5896 ** intact.  If the requested page is not already in the cache, then the
       
  5897 ** behavior of the cache implementation is determined by the value of the
       
  5898 ** createFlag parameter passed to xFetch, according to the following table:
       
  5899 **
       
  5900 ** <table border=1 width=85% align=center>
       
  5901 ** <tr><th> createFlag <th> Behaviour when page is not already in cache
       
  5902 ** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
       
  5903 ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
       
  5904 **                 Otherwise return NULL.
       
  5905 ** <tr><td> 2 <td> Make every effort to allocate a new page.  Only return
       
  5906 **                 NULL if allocating a new page is effectively impossible.
       
  5907 ** </table>
       
  5908 **
       
  5909 ** SQLite will normally invoke xFetch() with a createFlag of 0 or 1.  If
       
  5910 ** a call to xFetch() with createFlag==1 returns NULL, then SQLite will
       
  5911 ** attempt to unpin one or more cache pages by spilling the content of
       
  5912 ** pinned pages to disk and synching the operating system disk cache. After
       
  5913 ** attempting to unpin pages, the xFetch() method will be invoked again with
       
  5914 ** a createFlag of 2.
       
  5915 **
       
  5916 ** xUnpin() is called by SQLite with a pointer to a currently pinned page
       
  5917 ** as its second argument. If the third parameter, discard, is non-zero,
       
  5918 ** then the page should be evicted from the cache. In this case SQLite 
       
  5919 ** assumes that the next time the page is retrieved from the cache using
       
  5920 ** the xFetch() method, it will be zeroed. If the discard parameter is
       
  5921 ** zero, then the page is considered to be unpinned. The cache implementation
       
  5922 ** may choose to evict unpinned pages at any time.
       
  5923 **
       
  5924 ** The cache is not required to perform any reference counting. A single 
       
  5925 ** call to xUnpin() unpins the page regardless of the number of prior calls 
       
  5926 ** to xFetch().
       
  5927 **
       
  5928 ** The xRekey() method is used to change the key value associated with the
       
  5929 ** page passed as the second argument from oldKey to newKey. If the cache
       
  5930 ** previously contains an entry associated with newKey, it should be
       
  5931 ** discarded. Any prior cache entry associated with newKey is guaranteed not
       
  5932 ** to be pinned.
       
  5933 **
       
  5934 ** When SQLite calls the xTruncate() method, the cache must discard all
       
  5935 ** existing cache entries with page numbers (keys) greater than or equal
       
  5936 ** to the value of the iLimit parameter passed to xTruncate(). If any
       
  5937 ** of these pages are pinned, they are implicitly unpinned, meaning that
       
  5938 ** they can be safely discarded.
       
  5939 **
       
  5940 ** The xDestroy() method is used to delete a cache allocated by xCreate().
       
  5941 ** All resources associated with the specified cache should be freed. After
       
  5942 ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
       
  5943 ** handle invalid, and will not use it with any other sqlite3_pcache_methods
       
  5944 ** functions.
       
  5945 */
       
  5946 typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
       
  5947 struct sqlite3_pcache_methods {
       
  5948   void *pArg;
       
  5949   int (*xInit)(void*);
       
  5950   void (*xShutdown)(void*);
       
  5951   sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
       
  5952   void (*xCachesize)(sqlite3_pcache*, int nCachesize);
       
  5953   int (*xPagecount)(sqlite3_pcache*);
       
  5954   void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
       
  5955   void (*xUnpin)(sqlite3_pcache*, void*, int discard);
       
  5956   void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
       
  5957   void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
       
  5958   void (*xDestroy)(sqlite3_pcache*);
       
  5959 };
       
  5960 
       
  5961 /*
       
  5962 ** CAPI3REF: Online Backup Object
       
  5963 ** EXPERIMENTAL
       
  5964 **
       
  5965 ** The sqlite3_backup object records state information about an ongoing
       
  5966 ** online backup operation.  The sqlite3_backup object is created by
       
  5967 ** a call to [sqlite3_backup_init()] and is destroyed by a call to
       
  5968 ** [sqlite3_backup_finish()].
       
  5969 **
       
  5970 ** See Also: [Using the SQLite Online Backup API]
       
  5971 */
       
  5972 typedef struct sqlite3_backup sqlite3_backup;
       
  5973 
       
  5974 /*
       
  5975 ** CAPI3REF: Online Backup API.
       
  5976 ** EXPERIMENTAL
       
  5977 **
       
  5978 ** This API is used to overwrite the contents of one database with that
       
  5979 ** of another. It is useful either for creating backups of databases or
       
  5980 ** for copying in-memory databases to or from persistent files. 
       
  5981 **
       
  5982 ** See Also: [Using the SQLite Online Backup API]
       
  5983 **
       
  5984 ** Exclusive access is required to the destination database for the 
       
  5985 ** duration of the operation. However the source database is only
       
  5986 ** read-locked while it is actually being read, it is not locked
       
  5987 ** continuously for the entire operation. Thus, the backup may be
       
  5988 ** performed on a live database without preventing other users from
       
  5989 ** writing to the database for an extended period of time.
       
  5990 ** 
       
  5991 ** To perform a backup operation: 
       
  5992 **   <ol>
       
  5993 **     <li><b>sqlite3_backup_init()</b> is called once to initialize the
       
  5994 **         backup, 
       
  5995 **     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer 
       
  5996 **         the data between the two databases, and finally
       
  5997 **     <li><b>sqlite3_backup_finish()</b> is called to release all resources 
       
  5998 **         associated with the backup operation. 
       
  5999 **   </ol>
       
  6000 ** There should be exactly one call to sqlite3_backup_finish() for each
       
  6001 ** successful call to sqlite3_backup_init().
       
  6002 **
       
  6003 ** <b>sqlite3_backup_init()</b>
       
  6004 **
       
  6005 ** The first two arguments passed to [sqlite3_backup_init()] are the database
       
  6006 ** handle associated with the destination database and the database name 
       
  6007 ** used to attach the destination database to the handle. The database name
       
  6008 ** is "main" for the main database, "temp" for the temporary database, or
       
  6009 ** the name specified as part of the [ATTACH] statement if the destination is
       
  6010 ** an attached database. The third and fourth arguments passed to 
       
  6011 ** sqlite3_backup_init() identify the [database connection]
       
  6012 ** and database name used
       
  6013 ** to access the source database. The values passed for the source and 
       
  6014 ** destination [database connection] parameters must not be the same.
       
  6015 **
       
  6016 ** If an error occurs within sqlite3_backup_init(), then NULL is returned
       
  6017 ** and an error code and error message written into the [database connection] 
       
  6018 ** passed as the first argument. They may be retrieved using the
       
  6019 ** [sqlite3_errcode()], [sqlite3_errmsg()], and [sqlite3_errmsg16()] functions.
       
  6020 ** Otherwise, if successful, a pointer to an [sqlite3_backup] object is
       
  6021 ** returned. This pointer may be used with the sqlite3_backup_step() and
       
  6022 ** sqlite3_backup_finish() functions to perform the specified backup 
       
  6023 ** operation.
       
  6024 **
       
  6025 ** <b>sqlite3_backup_step()</b>
       
  6026 **
       
  6027 ** Function [sqlite3_backup_step()] is used to copy up to nPage pages between 
       
  6028 ** the source and destination databases, where nPage is the value of the 
       
  6029 ** second parameter passed to sqlite3_backup_step(). If nPage is a negative
       
  6030 ** value, all remaining source pages are copied. If the required pages are 
       
  6031 ** succesfully copied, but there are still more pages to copy before the 
       
  6032 ** backup is complete, it returns [SQLITE_OK]. If no error occured and there 
       
  6033 ** are no more pages to copy, then [SQLITE_DONE] is returned. If an error 
       
  6034 ** occurs, then an SQLite error code is returned. As well as [SQLITE_OK] and
       
  6035 ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
       
  6036 ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
       
  6037 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
       
  6038 **
       
  6039 ** As well as the case where the destination database file was opened for
       
  6040 ** read-only access, sqlite3_backup_step() may return [SQLITE_READONLY] if
       
  6041 ** the destination is an in-memory database with a different page size
       
  6042 ** from the source database.
       
  6043 **
       
  6044 ** If sqlite3_backup_step() cannot obtain a required file-system lock, then
       
  6045 ** the [sqlite3_busy_handler | busy-handler function]
       
  6046 ** is invoked (if one is specified). If the 
       
  6047 ** busy-handler returns non-zero before the lock is available, then 
       
  6048 ** [SQLITE_BUSY] is returned to the caller. In this case the call to
       
  6049 ** sqlite3_backup_step() can be retried later. If the source
       
  6050 ** [database connection]
       
  6051 ** is being used to write to the source database when sqlite3_backup_step()
       
  6052 ** is called, then [SQLITE_LOCKED] is returned immediately. Again, in this
       
  6053 ** case the call to sqlite3_backup_step() can be retried later on. If
       
  6054 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
       
  6055 ** [SQLITE_READONLY] is returned, then 
       
  6056 ** there is no point in retrying the call to sqlite3_backup_step(). These 
       
  6057 ** errors are considered fatal. At this point the application must accept 
       
  6058 ** that the backup operation has failed and pass the backup operation handle 
       
  6059 ** to the sqlite3_backup_finish() to release associated resources.
       
  6060 **
       
  6061 ** Following the first call to sqlite3_backup_step(), an exclusive lock is
       
  6062 ** obtained on the destination file. It is not released until either 
       
  6063 ** sqlite3_backup_finish() is called or the backup operation is complete 
       
  6064 ** and sqlite3_backup_step() returns [SQLITE_DONE]. Additionally, each time 
       
  6065 ** a call to sqlite3_backup_step() is made a [shared lock] is obtained on
       
  6066 ** the source database file. This lock is released before the
       
  6067 ** sqlite3_backup_step() call returns. Because the source database is not
       
  6068 ** locked between calls to sqlite3_backup_step(), it may be modified mid-way
       
  6069 ** through the backup procedure. If the source database is modified by an
       
  6070 ** external process or via a database connection other than the one being
       
  6071 ** used by the backup operation, then the backup will be transparently
       
  6072 ** restarted by the next call to sqlite3_backup_step(). If the source 
       
  6073 ** database is modified by the using the same database connection as is used
       
  6074 ** by the backup operation, then the backup database is transparently 
       
  6075 ** updated at the same time.
       
  6076 **
       
  6077 ** <b>sqlite3_backup_finish()</b>
       
  6078 **
       
  6079 ** Once sqlite3_backup_step() has returned [SQLITE_DONE], or when the 
       
  6080 ** application wishes to abandon the backup operation, the [sqlite3_backup]
       
  6081 ** object should be passed to sqlite3_backup_finish(). This releases all
       
  6082 ** resources associated with the backup operation. If sqlite3_backup_step()
       
  6083 ** has not yet returned [SQLITE_DONE], then any active write-transaction on the
       
  6084 ** destination database is rolled back. The [sqlite3_backup] object is invalid
       
  6085 ** and may not be used following a call to sqlite3_backup_finish().
       
  6086 **
       
  6087 ** The value returned by sqlite3_backup_finish is [SQLITE_OK] if no error
       
  6088 ** occurred, regardless or whether or not sqlite3_backup_step() was called
       
  6089 ** a sufficient number of times to complete the backup operation. Or, if
       
  6090 ** an out-of-memory condition or IO error occured during a call to
       
  6091 ** sqlite3_backup_step() then [SQLITE_NOMEM] or an
       
  6092 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] error code
       
  6093 ** is returned. In this case the error code and an error message are
       
  6094 ** written to the destination [database connection].
       
  6095 **
       
  6096 ** A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step() is
       
  6097 ** not a permanent error and does not affect the return value of
       
  6098 ** sqlite3_backup_finish().
       
  6099 **
       
  6100 ** <b>sqlite3_backup_remaining(), sqlite3_backup_pagecount()</b>
       
  6101 **
       
  6102 ** Each call to sqlite3_backup_step() sets two values stored internally
       
  6103 ** by an [sqlite3_backup] object. The number of pages still to be backed
       
  6104 ** up, which may be queried by sqlite3_backup_remaining(), and the total
       
  6105 ** number of pages in the source database file, which may be queried by
       
  6106 ** sqlite3_backup_pagecount().
       
  6107 **
       
  6108 ** The values returned by these functions are only updated by
       
  6109 ** sqlite3_backup_step(). If the source database is modified during a backup
       
  6110 ** operation, then the values are not updated to account for any extra
       
  6111 ** pages that need to be updated or the size of the source database file
       
  6112 ** changing.
       
  6113 **
       
  6114 ** <b>Concurrent Usage of Database Handles</b>
       
  6115 **
       
  6116 ** The source [database connection] may be used by the application for other
       
  6117 ** purposes while a backup operation is underway or being initialized.
       
  6118 ** If SQLite is compiled and configured to support threadsafe database
       
  6119 ** connections, then the source database connection may be used concurrently
       
  6120 ** from within other threads.
       
  6121 **
       
  6122 ** However, the application must guarantee that the destination database
       
  6123 ** connection handle is not passed to any other API (by any thread) after 
       
  6124 ** sqlite3_backup_init() is called and before the corresponding call to
       
  6125 ** sqlite3_backup_finish(). Unfortunately SQLite does not currently check
       
  6126 ** for this, if the application does use the destination [database connection]
       
  6127 ** for some other purpose during a backup operation, things may appear to
       
  6128 ** work correctly but in fact be subtly malfunctioning.  Use of the
       
  6129 ** destination database connection while a backup is in progress might
       
  6130 ** also cause a mutex deadlock.
       
  6131 **
       
  6132 ** Furthermore, if running in [shared cache mode], the application must
       
  6133 ** guarantee that the shared cache used by the destination database
       
  6134 ** is not accessed while the backup is running. In practice this means
       
  6135 ** that the application must guarantee that the file-system file being 
       
  6136 ** backed up to is not accessed by any connection within the process,
       
  6137 ** not just the specific connection that was passed to sqlite3_backup_init().
       
  6138 **
       
  6139 ** The [sqlite3_backup] object itself is partially threadsafe. Multiple 
       
  6140 ** threads may safely make multiple concurrent calls to sqlite3_backup_step().
       
  6141 ** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
       
  6142 ** APIs are not strictly speaking threadsafe. If they are invoked at the
       
  6143 ** same time as another thread is invoking sqlite3_backup_step() it is
       
  6144 ** possible that they return invalid values.
       
  6145 */
       
  6146 SQLITE_API sqlite3_backup *sqlite3_backup_init(
       
  6147   sqlite3 *pDest,                        /* Destination database handle */
       
  6148   const char *zDestName,                 /* Destination database name */
       
  6149   sqlite3 *pSource,                      /* Source database handle */
       
  6150   const char *zSourceName                /* Source database name */
       
  6151 );
       
  6152 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
       
  6153 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
       
  6154 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
       
  6155 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
       
  6156 
       
  6157 /*
       
  6158 ** CAPI3REF: Unlock Notification
       
  6159 ** EXPERIMENTAL
       
  6160 **
       
  6161 ** When running in shared-cache mode, a database operation may fail with
       
  6162 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
       
  6163 ** individual tables within the shared-cache cannot be obtained. See
       
  6164 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking. 
       
  6165 ** This API may be used to register a callback that SQLite will invoke 
       
  6166 ** when the connection currently holding the required lock relinquishes it.
       
  6167 ** This API is only available if the library was compiled with the
       
  6168 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
       
  6169 **
       
  6170 ** See Also: [Using the SQLite Unlock Notification Feature].
       
  6171 **
       
  6172 ** Shared-cache locks are released when a database connection concludes
       
  6173 ** its current transaction, either by committing it or rolling it back. 
       
  6174 **
       
  6175 ** When a connection (known as the blocked connection) fails to obtain a
       
  6176 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
       
  6177 ** identity of the database connection (the blocking connection) that
       
  6178 ** has locked the required resource is stored internally. After an 
       
  6179 ** application receives an SQLITE_LOCKED error, it may call the
       
  6180 ** sqlite3_unlock_notify() method with the blocked connection handle as 
       
  6181 ** the first argument to register for a callback that will be invoked
       
  6182 ** when the blocking connections current transaction is concluded. The
       
  6183 ** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
       
  6184 ** call that concludes the blocking connections transaction.
       
  6185 **
       
  6186 ** If sqlite3_unlock_notify() is called in a multi-threaded application,
       
  6187 ** there is a chance that the blocking connection will have already
       
  6188 ** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
       
  6189 ** If this happens, then the specified callback is invoked immediately,
       
  6190 ** from within the call to sqlite3_unlock_notify().
       
  6191 **
       
  6192 ** If the blocked connection is attempting to obtain a write-lock on a
       
  6193 ** shared-cache table, and more than one other connection currently holds
       
  6194 ** a read-lock on the same table, then SQLite arbitrarily selects one of 
       
  6195 ** the other connections to use as the blocking connection.
       
  6196 **
       
  6197 ** There may be at most one unlock-notify callback registered by a 
       
  6198 ** blocked connection. If sqlite3_unlock_notify() is called when the
       
  6199 ** blocked connection already has a registered unlock-notify callback,
       
  6200 ** then the new callback replaces the old. If sqlite3_unlock_notify() is
       
  6201 ** called with a NULL pointer as its second argument, then any existing
       
  6202 ** unlock-notify callback is cancelled. The blocked connections 
       
  6203 ** unlock-notify callback may also be canceled by closing the blocked
       
  6204 ** connection using [sqlite3_close()].
       
  6205 **
       
  6206 ** The unlock-notify callback is not reentrant. If an application invokes
       
  6207 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
       
  6208 ** crash or deadlock may be the result.
       
  6209 **
       
  6210 ** Unless deadlock is detected (see below), sqlite3_unlock_notify() always
       
  6211 ** returns SQLITE_OK.
       
  6212 **
       
  6213 ** <b>Callback Invocation Details</b>
       
  6214 **
       
  6215 ** When an unlock-notify callback is registered, the application provides a 
       
  6216 ** single void* pointer that is passed to the callback when it is invoked.
       
  6217 ** However, the signature of the callback function allows SQLite to pass
       
  6218 ** it an array of void* context pointers. The first argument passed to
       
  6219 ** an unlock-notify callback is a pointer to an array of void* pointers,
       
  6220 ** and the second is the number of entries in the array.
       
  6221 **
       
  6222 ** When a blocking connections transaction is concluded, there may be
       
  6223 ** more than one blocked connection that has registered for an unlock-notify
       
  6224 ** callback. If two or more such blocked connections have specified the
       
  6225 ** same callback function, then instead of invoking the callback function
       
  6226 ** multiple times, it is invoked once with the set of void* context pointers
       
  6227 ** specified by the blocked connections bundled together into an array.
       
  6228 ** This gives the application an opportunity to prioritize any actions 
       
  6229 ** related to the set of unblocked database connections.
       
  6230 **
       
  6231 ** <b>Deadlock Detection</b>
       
  6232 **
       
  6233 ** Assuming that after registering for an unlock-notify callback a 
       
  6234 ** database waits for the callback to be issued before taking any further
       
  6235 ** action (a reasonable assumption), then using this API may cause the
       
  6236 ** application to deadlock. For example, if connection X is waiting for
       
  6237 ** connection Y's transaction to be concluded, and similarly connection
       
  6238 ** Y is waiting on connection X's transaction, then neither connection
       
  6239 ** will proceed and the system may remain deadlocked indefinitely.
       
  6240 **
       
  6241 ** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
       
  6242 ** detection. If a given call to sqlite3_unlock_notify() would put the
       
  6243 ** system in a deadlocked state, then SQLITE_LOCKED is returned and no
       
  6244 ** unlock-notify callback is registered. The system is said to be in
       
  6245 ** a deadlocked state if connection A has registered for an unlock-notify
       
  6246 ** callback on the conclusion of connection B's transaction, and connection
       
  6247 ** B has itself registered for an unlock-notify callback when connection
       
  6248 ** A's transaction is concluded. Indirect deadlock is also detected, so
       
  6249 ** the system is also considered to be deadlocked if connection B has
       
  6250 ** registered for an unlock-notify callback on the conclusion of connection
       
  6251 ** C's transaction, where connection C is waiting on connection A. Any
       
  6252 ** number of levels of indirection are allowed.
       
  6253 **
       
  6254 ** <b>The "DROP TABLE" Exception</b>
       
  6255 **
       
  6256 ** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost 
       
  6257 ** always appropriate to call sqlite3_unlock_notify(). There is however,
       
  6258 ** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
       
  6259 ** SQLite checks if there are any currently executing SELECT statements
       
  6260 ** that belong to the same connection. If there are, SQLITE_LOCKED is
       
  6261 ** returned. In this case there is no "blocking connection", so invoking
       
  6262 ** sqlite3_unlock_notify() results in the unlock-notify callback being
       
  6263 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
       
  6264 ** or "DROP INDEX" query, an infinite loop might be the result.
       
  6265 **
       
  6266 ** One way around this problem is to check the extended error code returned
       
  6267 ** by an sqlite3_step() call. If there is a blocking connection, then the
       
  6268 ** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
       
  6269 ** the special "DROP TABLE/INDEX" case, the extended error code is just 
       
  6270 ** SQLITE_LOCKED.
       
  6271 */
       
  6272 SQLITE_API int sqlite3_unlock_notify(
       
  6273   sqlite3 *pBlocked,                          /* Waiting connection */
       
  6274   void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
       
  6275   void *pNotifyArg                            /* Argument to pass to xNotify */
       
  6276 );
       
  6277 
       
  6278 
       
  6279 /*
       
  6280 ** CAPI3REF: String Comparison
       
  6281 ** EXPERIMENTAL
       
  6282 **
       
  6283 ** The [sqlite3_strnicmp()] API allows applications and extensions to
       
  6284 ** compare the contents of two buffers containing UTF-8 strings in a
       
  6285 ** case-indendent fashion, using the same definition of case independence 
       
  6286 ** that SQLite uses internally when comparing identifiers.
       
  6287 */
       
  6288 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
       
  6289 
       
  6290 /*
       
  6291 ** Undo the hack that converts floating point types to integer for
       
  6292 ** builds on processors without floating point support.
       
  6293 */
       
  6294 #ifdef SQLITE_OMIT_FLOATING_POINT
       
  6295 # undef double
       
  6296 #endif
       
  6297 
       
  6298 #if 0
       
  6299 }  /* End of the 'extern "C"' block */
       
  6300 #endif
       
  6301 #endif
       
  6302 
       
  6303 
       
  6304 /************** End of sqlite3.h *********************************************/
       
  6305 /************** Continuing where we left off in sqliteInt.h ******************/
       
  6306 /************** Include hash.h in the middle of sqliteInt.h ******************/
       
  6307 /************** Begin file hash.h ********************************************/
       
  6308 /*
       
  6309 ** 2001 September 22
       
  6310 **
       
  6311 ** The author disclaims copyright to this source code.  In place of
       
  6312 ** a legal notice, here is a blessing:
       
  6313 **
       
  6314 **    May you do good and not evil.
       
  6315 **    May you find forgiveness for yourself and forgive others.
       
  6316 **    May you share freely, never taking more than you give.
       
  6317 **
       
  6318 *************************************************************************
       
  6319 ** This is the header file for the generic hash-table implemenation
       
  6320 ** used in SQLite.
       
  6321 **
       
  6322 ** $Id: hash.h,v 1.15 2009/05/02 13:29:38 drh Exp $
       
  6323 */
       
  6324 #ifndef _SQLITE_HASH_H_
       
  6325 #define _SQLITE_HASH_H_
       
  6326 
       
  6327 /* Forward declarations of structures. */
       
  6328 typedef struct Hash Hash;
       
  6329 typedef struct HashElem HashElem;
       
  6330 
       
  6331 /* A complete hash table is an instance of the following structure.
       
  6332 ** The internals of this structure are intended to be opaque -- client
       
  6333 ** code should not attempt to access or modify the fields of this structure
       
  6334 ** directly.  Change this structure only by using the routines below.
       
  6335 ** However, some of the "procedures" and "functions" for modifying and
       
  6336 ** accessing this structure are really macros, so we can't really make
       
  6337 ** this structure opaque.
       
  6338 **
       
  6339 ** All elements of the hash table are on a single doubly-linked list.
       
  6340 ** Hash.first points to the head of this list.
       
  6341 **
       
  6342 ** There are Hash.htsize buckets.  Each bucket points to a spot in
       
  6343 ** the global doubly-linked list.  The contents of the bucket are the
       
  6344 ** element pointed to plus the next _ht.count-1 elements in the list.
       
  6345 **
       
  6346 ** Hash.htsize and Hash.ht may be zero.  In that case lookup is done
       
  6347 ** by a linear search of the global list.  For small tables, the 
       
  6348 ** Hash.ht table is never allocated because if there are few elements
       
  6349 ** in the table, it is faster to do a linear search than to manage
       
  6350 ** the hash table.
       
  6351 */
       
  6352 struct Hash {
       
  6353   unsigned int htsize;      /* Number of buckets in the hash table */
       
  6354   unsigned int count;       /* Number of entries in this table */
       
  6355   HashElem *first;          /* The first element of the array */
       
  6356   struct _ht {              /* the hash table */
       
  6357     int count;                 /* Number of entries with this hash */
       
  6358     HashElem *chain;           /* Pointer to first entry with this hash */
       
  6359   } *ht;
       
  6360 };
       
  6361 
       
  6362 /* Each element in the hash table is an instance of the following 
       
  6363 ** structure.  All elements are stored on a single doubly-linked list.
       
  6364 **
       
  6365 ** Again, this structure is intended to be opaque, but it can't really
       
  6366 ** be opaque because it is used by macros.
       
  6367 */
       
  6368 struct HashElem {
       
  6369   HashElem *next, *prev;       /* Next and previous elements in the table */
       
  6370   void *data;                  /* Data associated with this element */
       
  6371   const char *pKey; int nKey;  /* Key associated with this element */
       
  6372 };
       
  6373 
       
  6374 /*
       
  6375 ** Access routines.  To delete, insert a NULL pointer.
       
  6376 */
       
  6377 SQLITE_PRIVATE void sqlite3HashInit(Hash*);
       
  6378 SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
       
  6379 SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
       
  6380 SQLITE_PRIVATE void sqlite3HashClear(Hash*);
       
  6381 
       
  6382 /*
       
  6383 ** Macros for looping over all elements of a hash table.  The idiom is
       
  6384 ** like this:
       
  6385 **
       
  6386 **   Hash h;
       
  6387 **   HashElem *p;
       
  6388 **   ...
       
  6389 **   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
       
  6390 **     SomeStructure *pData = sqliteHashData(p);
       
  6391 **     // do something with pData
       
  6392 **   }
       
  6393 */
       
  6394 #define sqliteHashFirst(H)  ((H)->first)
       
  6395 #define sqliteHashNext(E)   ((E)->next)
       
  6396 #define sqliteHashData(E)   ((E)->data)
       
  6397 /* #define sqliteHashKey(E)    ((E)->pKey) // NOT USED */
       
  6398 /* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
       
  6399 
       
  6400 /*
       
  6401 ** Number of entries in a hash table
       
  6402 */
       
  6403 /* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
       
  6404 
       
  6405 #endif /* _SQLITE_HASH_H_ */
       
  6406 
       
  6407 /************** End of hash.h ************************************************/
       
  6408 /************** Continuing where we left off in sqliteInt.h ******************/
       
  6409 /************** Include parse.h in the middle of sqliteInt.h *****************/
       
  6410 /************** Begin file parse.h *******************************************/
       
  6411 #define TK_SEMI                            1
       
  6412 #define TK_EXPLAIN                         2
       
  6413 #define TK_QUERY                           3
       
  6414 #define TK_PLAN                            4
       
  6415 #define TK_BEGIN                           5
       
  6416 #define TK_TRANSACTION                     6
       
  6417 #define TK_DEFERRED                        7
       
  6418 #define TK_IMMEDIATE                       8
       
  6419 #define TK_EXCLUSIVE                       9
       
  6420 #define TK_COMMIT                         10
       
  6421 #define TK_END                            11
       
  6422 #define TK_ROLLBACK                       12
       
  6423 #define TK_SAVEPOINT                      13
       
  6424 #define TK_RELEASE                        14
       
  6425 #define TK_TO                             15
       
  6426 #define TK_TABLE                          16
       
  6427 #define TK_CREATE                         17
       
  6428 #define TK_IF                             18
       
  6429 #define TK_NOT                            19
       
  6430 #define TK_EXISTS                         20
       
  6431 #define TK_TEMP                           21
       
  6432 #define TK_LP                             22
       
  6433 #define TK_RP                             23
       
  6434 #define TK_AS                             24
       
  6435 #define TK_COMMA                          25
       
  6436 #define TK_ID                             26
       
  6437 #define TK_INDEXED                        27
       
  6438 #define TK_ABORT                          28
       
  6439 #define TK_ACTION                         29
       
  6440 #define TK_AFTER                          30
       
  6441 #define TK_ANALYZE                        31
       
  6442 #define TK_ASC                            32
       
  6443 #define TK_ATTACH                         33
       
  6444 #define TK_BEFORE                         34
       
  6445 #define TK_BY                             35
       
  6446 #define TK_CASCADE                        36
       
  6447 #define TK_CAST                           37
       
  6448 #define TK_COLUMNKW                       38
       
  6449 #define TK_CONFLICT                       39
       
  6450 #define TK_DATABASE                       40
       
  6451 #define TK_DESC                           41
       
  6452 #define TK_DETACH                         42
       
  6453 #define TK_EACH                           43
       
  6454 #define TK_FAIL                           44
       
  6455 #define TK_FOR                            45
       
  6456 #define TK_IGNORE                         46
       
  6457 #define TK_INITIALLY                      47
       
  6458 #define TK_INSTEAD                        48
       
  6459 #define TK_LIKE_KW                        49
       
  6460 #define TK_MATCH                          50
       
  6461 #define TK_NO                             51
       
  6462 #define TK_KEY                            52
       
  6463 #define TK_OF                             53
       
  6464 #define TK_OFFSET                         54
       
  6465 #define TK_PRAGMA                         55
       
  6466 #define TK_RAISE                          56
       
  6467 #define TK_REPLACE                        57
       
  6468 #define TK_RESTRICT                       58
       
  6469 #define TK_ROW                            59
       
  6470 #define TK_TRIGGER                        60
       
  6471 #define TK_VACUUM                         61
       
  6472 #define TK_VIEW                           62
       
  6473 #define TK_VIRTUAL                        63
       
  6474 #define TK_REINDEX                        64
       
  6475 #define TK_RENAME                         65
       
  6476 #define TK_CTIME_KW                       66
       
  6477 #define TK_ANY                            67
       
  6478 #define TK_OR                             68
       
  6479 #define TK_AND                            69
       
  6480 #define TK_IS                             70
       
  6481 #define TK_BETWEEN                        71
       
  6482 #define TK_IN                             72
       
  6483 #define TK_ISNULL                         73
       
  6484 #define TK_NOTNULL                        74
       
  6485 #define TK_NE                             75
       
  6486 #define TK_EQ                             76
       
  6487 #define TK_GT                             77
       
  6488 #define TK_LE                             78
       
  6489 #define TK_LT                             79
       
  6490 #define TK_GE                             80
       
  6491 #define TK_ESCAPE                         81
       
  6492 #define TK_BITAND                         82
       
  6493 #define TK_BITOR                          83
       
  6494 #define TK_LSHIFT                         84
       
  6495 #define TK_RSHIFT                         85
       
  6496 #define TK_PLUS                           86
       
  6497 #define TK_MINUS                          87
       
  6498 #define TK_STAR                           88
       
  6499 #define TK_SLASH                          89
       
  6500 #define TK_REM                            90
       
  6501 #define TK_CONCAT                         91
       
  6502 #define TK_COLLATE                        92
       
  6503 #define TK_BITNOT                         93
       
  6504 #define TK_STRING                         94
       
  6505 #define TK_JOIN_KW                        95
       
  6506 #define TK_CONSTRAINT                     96
       
  6507 #define TK_DEFAULT                        97
       
  6508 #define TK_NULL                           98
       
  6509 #define TK_PRIMARY                        99
       
  6510 #define TK_UNIQUE                         100
       
  6511 #define TK_CHECK                          101
       
  6512 #define TK_REFERENCES                     102
       
  6513 #define TK_AUTOINCR                       103
       
  6514 #define TK_ON                             104
       
  6515 #define TK_DELETE                         105
       
  6516 #define TK_UPDATE                         106
       
  6517 #define TK_SET                            107
       
  6518 #define TK_DEFERRABLE                     108
       
  6519 #define TK_FOREIGN                        109
       
  6520 #define TK_DROP                           110
       
  6521 #define TK_UNION                          111
       
  6522 #define TK_ALL                            112
       
  6523 #define TK_EXCEPT                         113
       
  6524 #define TK_INTERSECT                      114
       
  6525 #define TK_SELECT                         115
       
  6526 #define TK_DISTINCT                       116
       
  6527 #define TK_DOT                            117
       
  6528 #define TK_FROM                           118
       
  6529 #define TK_JOIN                           119
       
  6530 #define TK_USING                          120
       
  6531 #define TK_ORDER                          121
       
  6532 #define TK_GROUP                          122
       
  6533 #define TK_HAVING                         123
       
  6534 #define TK_LIMIT                          124
       
  6535 #define TK_WHERE                          125
       
  6536 #define TK_INTO                           126
       
  6537 #define TK_VALUES                         127
       
  6538 #define TK_INSERT                         128
       
  6539 #define TK_INTEGER                        129
       
  6540 #define TK_FLOAT                          130
       
  6541 #define TK_BLOB                           131
       
  6542 #define TK_REGISTER                       132
       
  6543 #define TK_VARIABLE                       133
       
  6544 #define TK_CASE                           134
       
  6545 #define TK_WHEN                           135
       
  6546 #define TK_THEN                           136
       
  6547 #define TK_ELSE                           137
       
  6548 #define TK_INDEX                          138
       
  6549 #define TK_ALTER                          139
       
  6550 #define TK_ADD                            140
       
  6551 #define TK_TO_TEXT                        141
       
  6552 #define TK_TO_BLOB                        142
       
  6553 #define TK_TO_NUMERIC                     143
       
  6554 #define TK_TO_INT                         144
       
  6555 #define TK_TO_REAL                        145
       
  6556 #define TK_ISNOT                          146
       
  6557 #define TK_END_OF_FILE                    147
       
  6558 #define TK_ILLEGAL                        148
       
  6559 #define TK_SPACE                          149
       
  6560 #define TK_UNCLOSED_STRING                150
       
  6561 #define TK_FUNCTION                       151
       
  6562 #define TK_COLUMN                         152
       
  6563 #define TK_AGG_FUNCTION                   153
       
  6564 #define TK_AGG_COLUMN                     154
       
  6565 #define TK_CONST_FUNC                     155
       
  6566 #define TK_UMINUS                         156
       
  6567 #define TK_UPLUS                          157
       
  6568 
       
  6569 /************** End of parse.h ***********************************************/
       
  6570 /************** Continuing where we left off in sqliteInt.h ******************/
       
  6571 #include <stdio.h>
       
  6572 #include <stdlib.h>
       
  6573 #include <string.h>
       
  6574 #include <assert.h>
       
  6575 #include <stddef.h>
       
  6576 
       
  6577 /*
       
  6578 ** If compiling for a processor that lacks floating point support,
       
  6579 ** substitute integer for floating-point
       
  6580 */
       
  6581 #ifdef SQLITE_OMIT_FLOATING_POINT
       
  6582 # define double sqlite_int64
       
  6583 # define LONGDOUBLE_TYPE sqlite_int64
       
  6584 # ifndef SQLITE_BIG_DBL
       
  6585 #   define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
       
  6586 # endif
       
  6587 # define SQLITE_OMIT_DATETIME_FUNCS 1
       
  6588 # define SQLITE_OMIT_TRACE 1
       
  6589 # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
       
  6590 # undef SQLITE_HAVE_ISNAN
       
  6591 #endif
       
  6592 #ifndef SQLITE_BIG_DBL
       
  6593 # define SQLITE_BIG_DBL (1e99)
       
  6594 #endif
       
  6595 
       
  6596 /*
       
  6597 ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
       
  6598 ** afterward. Having this macro allows us to cause the C compiler 
       
  6599 ** to omit code used by TEMP tables without messy #ifndef statements.
       
  6600 */
       
  6601 #ifdef SQLITE_OMIT_TEMPDB
       
  6602 #define OMIT_TEMPDB 1
       
  6603 #else
       
  6604 #define OMIT_TEMPDB 0
       
  6605 #endif
       
  6606 
       
  6607 /*
       
  6608 ** If the following macro is set to 1, then NULL values are considered
       
  6609 ** distinct when determining whether or not two entries are the same
       
  6610 ** in a UNIQUE index.  This is the way PostgreSQL, Oracle, DB2, MySQL,
       
  6611 ** OCELOT, and Firebird all work.  The SQL92 spec explicitly says this
       
  6612 ** is the way things are suppose to work.
       
  6613 **
       
  6614 ** If the following macro is set to 0, the NULLs are indistinct for
       
  6615 ** a UNIQUE index.  In this mode, you can only have a single NULL entry
       
  6616 ** for a column declared UNIQUE.  This is the way Informix and SQL Server
       
  6617 ** work.
       
  6618 */
       
  6619 #define NULL_DISTINCT_FOR_UNIQUE 1
       
  6620 
       
  6621 /*
       
  6622 ** The "file format" number is an integer that is incremented whenever
       
  6623 ** the VDBE-level file format changes.  The following macros define the
       
  6624 ** the default file format for new databases and the maximum file format
       
  6625 ** that the library can read.
       
  6626 */
       
  6627 #define SQLITE_MAX_FILE_FORMAT 4
       
  6628 #ifndef SQLITE_DEFAULT_FILE_FORMAT
       
  6629 # define SQLITE_DEFAULT_FILE_FORMAT 1
       
  6630 #endif
       
  6631 
       
  6632 #ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
       
  6633 # define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
       
  6634 #endif
       
  6635 
       
  6636 /*
       
  6637 ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
       
  6638 ** on the command-line
       
  6639 */
       
  6640 #ifndef SQLITE_TEMP_STORE
       
  6641 # define SQLITE_TEMP_STORE 1
       
  6642 #endif
       
  6643 
       
  6644 /*
       
  6645 ** GCC does not define the offsetof() macro so we'll have to do it
       
  6646 ** ourselves.
       
  6647 */
       
  6648 #ifndef offsetof
       
  6649 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
       
  6650 #endif
       
  6651 
       
  6652 /*
       
  6653 ** Check to see if this machine uses EBCDIC.  (Yes, believe it or
       
  6654 ** not, there are still machines out there that use EBCDIC.)
       
  6655 */
       
  6656 #if 'A' == '\301'
       
  6657 # define SQLITE_EBCDIC 1
       
  6658 #else
       
  6659 # define SQLITE_ASCII 1
       
  6660 #endif
       
  6661 
       
  6662 /*
       
  6663 ** Integers of known sizes.  These typedefs might change for architectures
       
  6664 ** where the sizes very.  Preprocessor macros are available so that the
       
  6665 ** types can be conveniently redefined at compile-type.  Like this:
       
  6666 **
       
  6667 **         cc '-DUINTPTR_TYPE=long long int' ...
       
  6668 */
       
  6669 #ifndef UINT32_TYPE
       
  6670 # ifdef HAVE_UINT32_T
       
  6671 #  define UINT32_TYPE uint32_t
       
  6672 # else
       
  6673 #  define UINT32_TYPE unsigned int
       
  6674 # endif
       
  6675 #endif
       
  6676 #ifndef UINT16_TYPE
       
  6677 # ifdef HAVE_UINT16_T
       
  6678 #  define UINT16_TYPE uint16_t
       
  6679 # else
       
  6680 #  define UINT16_TYPE unsigned short int
       
  6681 # endif
       
  6682 #endif
       
  6683 #ifndef INT16_TYPE
       
  6684 # ifdef HAVE_INT16_T
       
  6685 #  define INT16_TYPE int16_t
       
  6686 # else
       
  6687 #  define INT16_TYPE short int
       
  6688 # endif
       
  6689 #endif
       
  6690 #ifndef UINT8_TYPE
       
  6691 # ifdef HAVE_UINT8_T
       
  6692 #  define UINT8_TYPE uint8_t
       
  6693 # else
       
  6694 #  define UINT8_TYPE unsigned char
       
  6695 # endif
       
  6696 #endif
       
  6697 #ifndef INT8_TYPE
       
  6698 # ifdef HAVE_INT8_T
       
  6699 #  define INT8_TYPE int8_t
       
  6700 # else
       
  6701 #  define INT8_TYPE signed char
       
  6702 # endif
       
  6703 #endif
       
  6704 #ifndef LONGDOUBLE_TYPE
       
  6705 # define LONGDOUBLE_TYPE long double
       
  6706 #endif
       
  6707 typedef sqlite_int64 i64;          /* 8-byte signed integer */
       
  6708 typedef sqlite_uint64 u64;         /* 8-byte unsigned integer */
       
  6709 typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
       
  6710 typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
       
  6711 typedef INT16_TYPE i16;            /* 2-byte signed integer */
       
  6712 typedef UINT8_TYPE u8;             /* 1-byte unsigned integer */
       
  6713 typedef INT8_TYPE i8;              /* 1-byte signed integer */
       
  6714 
       
  6715 /*
       
  6716 ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
       
  6717 ** that can be stored in a u32 without loss of data.  The value
       
  6718 ** is 0x00000000ffffffff.  But because of quirks of some compilers, we
       
  6719 ** have to specify the value in the less intuitive manner shown:
       
  6720 */
       
  6721 #define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
       
  6722 
       
  6723 /*
       
  6724 ** Macros to determine whether the machine is big or little endian,
       
  6725 ** evaluated at runtime.
       
  6726 */
       
  6727 #ifdef SQLITE_AMALGAMATION
       
  6728 SQLITE_PRIVATE const int sqlite3one = 1;
       
  6729 #else
       
  6730 SQLITE_PRIVATE const int sqlite3one;
       
  6731 #endif
       
  6732 #if defined(i386) || defined(__i386__) || defined(_M_IX86)\
       
  6733                              || defined(__x86_64) || defined(__x86_64__)
       
  6734 # define SQLITE_BIGENDIAN    0
       
  6735 # define SQLITE_LITTLEENDIAN 1
       
  6736 # define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
       
  6737 #else
       
  6738 # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
       
  6739 # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
       
  6740 # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
       
  6741 #endif
       
  6742 
       
  6743 /*
       
  6744 ** Constants for the largest and smallest possible 64-bit signed integers.
       
  6745 ** These macros are designed to work correctly on both 32-bit and 64-bit
       
  6746 ** compilers.
       
  6747 */
       
  6748 #define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
       
  6749 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
       
  6750 
       
  6751 /* 
       
  6752 ** Round up a number to the next larger multiple of 8.  This is used
       
  6753 ** to force 8-byte alignment on 64-bit architectures.
       
  6754 */
       
  6755 #define ROUND8(x)     (((x)+7)&~7)
       
  6756 
       
  6757 /*
       
  6758 ** Round down to the nearest multiple of 8
       
  6759 */
       
  6760 #define ROUNDDOWN8(x) ((x)&~7)
       
  6761 
       
  6762 /*
       
  6763 ** Assert that the pointer X is aligned to an 8-byte boundary.
       
  6764 */
       
  6765 #define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
       
  6766 
       
  6767 
       
  6768 /*
       
  6769 ** An instance of the following structure is used to store the busy-handler
       
  6770 ** callback for a given sqlite handle. 
       
  6771 **
       
  6772 ** The sqlite.busyHandler member of the sqlite struct contains the busy
       
  6773 ** callback for the database handle. Each pager opened via the sqlite
       
  6774 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
       
  6775 ** callback is currently invoked only from within pager.c.
       
  6776 */
       
  6777 typedef struct BusyHandler BusyHandler;
       
  6778 struct BusyHandler {
       
  6779   int (*xFunc)(void *,int);  /* The busy callback */
       
  6780   void *pArg;                /* First arg to busy callback */
       
  6781   int nBusy;                 /* Incremented with each busy call */
       
  6782 };
       
  6783 
       
  6784 /*
       
  6785 ** Name of the master database table.  The master database table
       
  6786 ** is a special table that holds the names and attributes of all
       
  6787 ** user tables and indices.
       
  6788 */
       
  6789 #define MASTER_NAME       "sqlite_master"
       
  6790 #define TEMP_MASTER_NAME  "sqlite_temp_master"
       
  6791 
       
  6792 /*
       
  6793 ** The root-page of the master database table.
       
  6794 */
       
  6795 #define MASTER_ROOT       1
       
  6796 
       
  6797 /*
       
  6798 ** The name of the schema table.
       
  6799 */
       
  6800 #define SCHEMA_TABLE(x)  ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
       
  6801 
       
  6802 /*
       
  6803 ** A convenience macro that returns the number of elements in
       
  6804 ** an array.
       
  6805 */
       
  6806 #define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
       
  6807 
       
  6808 /*
       
  6809 ** The following value as a destructor means to use sqlite3DbFree().
       
  6810 ** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT.
       
  6811 */
       
  6812 #define SQLITE_DYNAMIC   ((sqlite3_destructor_type)sqlite3DbFree)
       
  6813 
       
  6814 /*
       
  6815 ** When SQLITE_OMIT_WSD is defined, it means that the target platform does
       
  6816 ** not support Writable Static Data (WSD) such as global and static variables.
       
  6817 ** All variables must either be on the stack or dynamically allocated from
       
  6818 ** the heap.  When WSD is unsupported, the variable declarations scattered
       
  6819 ** throughout the SQLite code must become constants instead.  The SQLITE_WSD
       
  6820 ** macro is used for this purpose.  And instead of referencing the variable
       
  6821 ** directly, we use its constant as a key to lookup the run-time allocated
       
  6822 ** buffer that holds real variable.  The constant is also the initializer
       
  6823 ** for the run-time allocated buffer.
       
  6824 **
       
  6825 ** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
       
  6826 ** macros become no-ops and have zero performance impact.
       
  6827 */
       
  6828 #ifdef SQLITE_OMIT_WSD
       
  6829   #define SQLITE_WSD const
       
  6830   #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
       
  6831   #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
       
  6832 SQLITE_API   int sqlite3_wsd_init(int N, int J);
       
  6833 SQLITE_API   void *sqlite3_wsd_find(void *K, int L);
       
  6834 #else
       
  6835   #define SQLITE_WSD 
       
  6836   #define GLOBAL(t,v) v
       
  6837   #define sqlite3GlobalConfig sqlite3Config
       
  6838 #endif
       
  6839 
       
  6840 /*
       
  6841 ** The following macros are used to suppress compiler warnings and to
       
  6842 ** make it clear to human readers when a function parameter is deliberately 
       
  6843 ** left unused within the body of a function. This usually happens when
       
  6844 ** a function is called via a function pointer. For example the 
       
  6845 ** implementation of an SQL aggregate step callback may not use the
       
  6846 ** parameter indicating the number of arguments passed to the aggregate,
       
  6847 ** if it knows that this is enforced elsewhere.
       
  6848 **
       
  6849 ** When a function parameter is not used at all within the body of a function,
       
  6850 ** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
       
  6851 ** However, these macros may also be used to suppress warnings related to
       
  6852 ** parameters that may or may not be used depending on compilation options.
       
  6853 ** For example those parameters only used in assert() statements. In these
       
  6854 ** cases the parameters are named as per the usual conventions.
       
  6855 */
       
  6856 #define UNUSED_PARAMETER(x) (void)(x)
       
  6857 #define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
       
  6858 
       
  6859 /*
       
  6860 ** Forward references to structures
       
  6861 */
       
  6862 typedef struct AggInfo AggInfo;
       
  6863 typedef struct AuthContext AuthContext;
       
  6864 typedef struct AutoincInfo AutoincInfo;
       
  6865 typedef struct Bitvec Bitvec;
       
  6866 typedef struct RowSet RowSet;
       
  6867 typedef struct CollSeq CollSeq;
       
  6868 typedef struct Column Column;
       
  6869 typedef struct Db Db;
       
  6870 typedef struct Schema Schema;
       
  6871 typedef struct Expr Expr;
       
  6872 typedef struct ExprList ExprList;
       
  6873 typedef struct ExprSpan ExprSpan;
       
  6874 typedef struct FKey FKey;
       
  6875 typedef struct FuncDef FuncDef;
       
  6876 typedef struct FuncDefHash FuncDefHash;
       
  6877 typedef struct IdList IdList;
       
  6878 typedef struct Index Index;
       
  6879 typedef struct IndexSample IndexSample;
       
  6880 typedef struct KeyClass KeyClass;
       
  6881 typedef struct KeyInfo KeyInfo;
       
  6882 typedef struct Lookaside Lookaside;
       
  6883 typedef struct LookasideSlot LookasideSlot;
       
  6884 typedef struct Module Module;
       
  6885 typedef struct NameContext NameContext;
       
  6886 typedef struct Parse Parse;
       
  6887 typedef struct Savepoint Savepoint;
       
  6888 typedef struct Select Select;
       
  6889 typedef struct SrcList SrcList;
       
  6890 typedef struct StrAccum StrAccum;
       
  6891 typedef struct Table Table;
       
  6892 typedef struct TableLock TableLock;
       
  6893 typedef struct Token Token;
       
  6894 typedef struct TriggerPrg TriggerPrg;
       
  6895 typedef struct TriggerStep TriggerStep;
       
  6896 typedef struct Trigger Trigger;
       
  6897 typedef struct UnpackedRecord UnpackedRecord;
       
  6898 typedef struct VTable VTable;
       
  6899 typedef struct Walker Walker;
       
  6900 typedef struct WherePlan WherePlan;
       
  6901 typedef struct WhereInfo WhereInfo;
       
  6902 typedef struct WhereLevel WhereLevel;
       
  6903 
       
  6904 /*
       
  6905 ** Defer sourcing vdbe.h and btree.h until after the "u8" and 
       
  6906 ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
       
  6907 ** pointer types (i.e. FuncDef) defined above.
       
  6908 */
       
  6909 /************** Include btree.h in the middle of sqliteInt.h *****************/
       
  6910 /************** Begin file btree.h *******************************************/
       
  6911 /*
       
  6912 ** 2001 September 15
       
  6913 **
       
  6914 ** The author disclaims copyright to this source code.  In place of
       
  6915 ** a legal notice, here is a blessing:
       
  6916 **
       
  6917 **    May you do good and not evil.
       
  6918 **    May you find forgiveness for yourself and forgive others.
       
  6919 **    May you share freely, never taking more than you give.
       
  6920 **
       
  6921 *************************************************************************
       
  6922 ** This header file defines the interface that the sqlite B-Tree file
       
  6923 ** subsystem.  See comments in the source code for a detailed description
       
  6924 ** of what each interface routine does.
       
  6925 **
       
  6926 ** @(#) $Id: btree.h,v 1.120 2009/07/22 00:35:24 drh Exp $
       
  6927 */
       
  6928 #ifndef _BTREE_H_
       
  6929 #define _BTREE_H_
       
  6930 
       
  6931 /* TODO: This definition is just included so other modules compile. It
       
  6932 ** needs to be revisited.
       
  6933 */
       
  6934 #define SQLITE_N_BTREE_META 10
       
  6935 
       
  6936 /*
       
  6937 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
       
  6938 ** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
       
  6939 */
       
  6940 #ifndef SQLITE_DEFAULT_AUTOVACUUM
       
  6941   #define SQLITE_DEFAULT_AUTOVACUUM 0
       
  6942 #endif
       
  6943 
       
  6944 #define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
       
  6945 #define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
       
  6946 #define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
       
  6947 
       
  6948 /*
       
  6949 ** Forward declarations of structure
       
  6950 */
       
  6951 typedef struct Btree Btree;
       
  6952 typedef struct BtCursor BtCursor;
       
  6953 typedef struct BtShared BtShared;
       
  6954 typedef struct BtreeMutexArray BtreeMutexArray;
       
  6955 
       
  6956 /*
       
  6957 ** This structure records all of the Btrees that need to hold
       
  6958 ** a mutex before we enter sqlite3VdbeExec().  The Btrees are
       
  6959 ** are placed in aBtree[] in order of aBtree[]->pBt.  That way,
       
  6960 ** we can always lock and unlock them all quickly.
       
  6961 */
       
  6962 struct BtreeMutexArray {
       
  6963   int nMutex;
       
  6964   Btree *aBtree[SQLITE_MAX_ATTACHED+1];
       
  6965 };
       
  6966 
       
  6967 
       
  6968 SQLITE_PRIVATE int sqlite3BtreeOpen(
       
  6969   const char *zFilename,   /* Name of database file to open */
       
  6970   sqlite3 *db,             /* Associated database connection */
       
  6971   Btree **ppBtree,         /* Return open Btree* here */
       
  6972   int flags,               /* Flags */
       
  6973   int vfsFlags             /* Flags passed through to VFS open */
       
  6974 );
       
  6975 
       
  6976 /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
       
  6977 ** following values.
       
  6978 **
       
  6979 ** NOTE:  These values must match the corresponding PAGER_ values in
       
  6980 ** pager.h.
       
  6981 */
       
  6982 #define BTREE_OMIT_JOURNAL  1  /* Do not use journal.  No argument */
       
  6983 #define BTREE_NO_READLOCK   2  /* Omit readlocks on readonly files */
       
  6984 #define BTREE_MEMORY        4  /* In-memory DB.  No argument */
       
  6985 #define BTREE_READONLY      8  /* Open the database in read-only mode */
       
  6986 #define BTREE_READWRITE    16  /* Open for both reading and writing */
       
  6987 #define BTREE_CREATE       32  /* Create the database if it does not exist */
       
  6988 
       
  6989 SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
       
  6990 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
       
  6991 SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int);
       
  6992 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
       
  6993 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
       
  6994 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
       
  6995 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
       
  6996 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
       
  6997 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
       
  6998 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
       
  6999 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
       
  7000 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
       
  7001 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*);
       
  7002 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
       
  7003 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
       
  7004 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
       
  7005 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
       
  7006 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
       
  7007 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
       
  7008 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
       
  7009 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
       
  7010 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
       
  7011 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
       
  7012 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
       
  7013 
       
  7014 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
       
  7015 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
       
  7016 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
       
  7017 
       
  7018 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
       
  7019 
       
  7020 /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
       
  7021 ** of the following flags:
       
  7022 */
       
  7023 #define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
       
  7024 #define BTREE_ZERODATA   2    /* Table has keys only - no data */
       
  7025 #define BTREE_LEAFDATA   4    /* Data stored in leaves only.  Implies INTKEY */
       
  7026 
       
  7027 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
       
  7028 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
       
  7029 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
       
  7030 
       
  7031 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
       
  7032 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
       
  7033 
       
  7034 /*
       
  7035 ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
       
  7036 ** should be one of the following values. The integer values are assigned 
       
  7037 ** to constants so that the offset of the corresponding field in an
       
  7038 ** SQLite database header may be found using the following formula:
       
  7039 **
       
  7040 **   offset = 36 + (idx * 4)
       
  7041 **
       
  7042 ** For example, the free-page-count field is located at byte offset 36 of
       
  7043 ** the database file header. The incr-vacuum-flag field is located at
       
  7044 ** byte offset 64 (== 36+4*7).
       
  7045 */
       
  7046 #define BTREE_FREE_PAGE_COUNT     0
       
  7047 #define BTREE_SCHEMA_VERSION      1
       
  7048 #define BTREE_FILE_FORMAT         2
       
  7049 #define BTREE_DEFAULT_CACHE_SIZE  3
       
  7050 #define BTREE_LARGEST_ROOT_PAGE   4
       
  7051 #define BTREE_TEXT_ENCODING       5
       
  7052 #define BTREE_USER_VERSION        6
       
  7053 #define BTREE_INCR_VACUUM         7
       
  7054 
       
  7055 SQLITE_PRIVATE int sqlite3BtreeCursor(
       
  7056   Btree*,                              /* BTree containing table to open */
       
  7057   int iTable,                          /* Index of root page */
       
  7058   int wrFlag,                          /* 1 for writing.  0 for read-only */
       
  7059   struct KeyInfo*,                     /* First argument to compare function */
       
  7060   BtCursor *pCursor                    /* Space to write cursor structure */
       
  7061 );
       
  7062 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
       
  7063 
       
  7064 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
       
  7065 SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
       
  7066   BtCursor*,
       
  7067   UnpackedRecord *pUnKey,
       
  7068   i64 intKey,
       
  7069   int bias,
       
  7070   int *pRes
       
  7071 );
       
  7072 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
       
  7073 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
       
  7074 SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
       
  7075                                   const void *pData, int nData,
       
  7076                                   int nZero, int bias, int seekResult);
       
  7077 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
       
  7078 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
       
  7079 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
       
  7080 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
       
  7081 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
       
  7082 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
       
  7083 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
       
  7084 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
       
  7085 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
       
  7086 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
       
  7087 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
       
  7088 SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
       
  7089 SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
       
  7090 
       
  7091 SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
       
  7092 SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
       
  7093 
       
  7094 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
       
  7095 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
       
  7096 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
       
  7097 
       
  7098 #ifndef NDEBUG
       
  7099 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
       
  7100 #endif
       
  7101 
       
  7102 #ifndef SQLITE_OMIT_BTREECOUNT
       
  7103 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
       
  7104 #endif
       
  7105 
       
  7106 #ifdef SQLITE_TEST
       
  7107 SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
       
  7108 SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
       
  7109 #endif
       
  7110 
       
  7111 /*
       
  7112 ** If we are not using shared cache, then there is no need to
       
  7113 ** use mutexes to access the BtShared structures.  So make the
       
  7114 ** Enter and Leave procedures no-ops.
       
  7115 */
       
  7116 #ifndef SQLITE_OMIT_SHARED_CACHE
       
  7117 SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
       
  7118 SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
       
  7119 #else
       
  7120 # define sqlite3BtreeEnter(X) 
       
  7121 # define sqlite3BtreeEnterAll(X)
       
  7122 #endif
       
  7123 
       
  7124 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
       
  7125 SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
       
  7126 SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
       
  7127 SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
       
  7128 SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
       
  7129 SQLITE_PRIVATE   void sqlite3BtreeMutexArrayEnter(BtreeMutexArray*);
       
  7130 SQLITE_PRIVATE   void sqlite3BtreeMutexArrayLeave(BtreeMutexArray*);
       
  7131 SQLITE_PRIVATE   void sqlite3BtreeMutexArrayInsert(BtreeMutexArray*, Btree*);
       
  7132 #ifndef NDEBUG
       
  7133   /* These routines are used inside assert() statements only. */
       
  7134 SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
       
  7135 SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
       
  7136 #endif
       
  7137 #else
       
  7138 
       
  7139 # define sqlite3BtreeLeave(X)
       
  7140 # define sqlite3BtreeEnterCursor(X)
       
  7141 # define sqlite3BtreeLeaveCursor(X)
       
  7142 # define sqlite3BtreeLeaveAll(X)
       
  7143 # define sqlite3BtreeMutexArrayEnter(X)
       
  7144 # define sqlite3BtreeMutexArrayLeave(X)
       
  7145 # define sqlite3BtreeMutexArrayInsert(X,Y)
       
  7146 
       
  7147 # define sqlite3BtreeHoldsMutex(X) 1
       
  7148 # define sqlite3BtreeHoldsAllMutexes(X) 1
       
  7149 #endif
       
  7150 
       
  7151 
       
  7152 #endif /* _BTREE_H_ */
       
  7153 
       
  7154 /************** End of btree.h ***********************************************/
       
  7155 /************** Continuing where we left off in sqliteInt.h ******************/
       
  7156 /************** Include vdbe.h in the middle of sqliteInt.h ******************/
       
  7157 /************** Begin file vdbe.h ********************************************/
       
  7158 /*
       
  7159 ** 2001 September 15
       
  7160 **
       
  7161 ** The author disclaims copyright to this source code.  In place of
       
  7162 ** a legal notice, here is a blessing:
       
  7163 **
       
  7164 **    May you do good and not evil.
       
  7165 **    May you find forgiveness for yourself and forgive others.
       
  7166 **    May you share freely, never taking more than you give.
       
  7167 **
       
  7168 *************************************************************************
       
  7169 ** Header file for the Virtual DataBase Engine (VDBE)
       
  7170 **
       
  7171 ** This header defines the interface to the virtual database engine
       
  7172 ** or VDBE.  The VDBE implements an abstract machine that runs a
       
  7173 ** simple program to access and modify the underlying database.
       
  7174 **
       
  7175 ** $Id: vdbe.h,v 1.142 2009/07/24 17:58:53 danielk1977 Exp $
       
  7176 */
       
  7177 #ifndef _SQLITE_VDBE_H_
       
  7178 #define _SQLITE_VDBE_H_
       
  7179 
       
  7180 /*
       
  7181 ** A single VDBE is an opaque structure named "Vdbe".  Only routines
       
  7182 ** in the source file sqliteVdbe.c are allowed to see the insides
       
  7183 ** of this structure.
       
  7184 */
       
  7185 typedef struct Vdbe Vdbe;
       
  7186 
       
  7187 /*
       
  7188 ** The names of the following types declared in vdbeInt.h are required
       
  7189 ** for the VdbeOp definition.
       
  7190 */
       
  7191 typedef struct VdbeFunc VdbeFunc;
       
  7192 typedef struct Mem Mem;
       
  7193 typedef struct SubProgram SubProgram;
       
  7194 
       
  7195 /*
       
  7196 ** A single instruction of the virtual machine has an opcode
       
  7197 ** and as many as three operands.  The instruction is recorded
       
  7198 ** as an instance of the following structure:
       
  7199 */
       
  7200 struct VdbeOp {
       
  7201   u8 opcode;          /* What operation to perform */
       
  7202   signed char p4type; /* One of the P4_xxx constants for p4 */
       
  7203   u8 opflags;         /* Not currently used */
       
  7204   u8 p5;              /* Fifth parameter is an unsigned character */
       
  7205   int p1;             /* First operand */
       
  7206   int p2;             /* Second parameter (often the jump destination) */
       
  7207   int p3;             /* The third parameter */
       
  7208   union {             /* fourth parameter */
       
  7209     int i;                 /* Integer value if p4type==P4_INT32 */
       
  7210     void *p;               /* Generic pointer */
       
  7211     char *z;               /* Pointer to data for string (char array) types */
       
  7212     i64 *pI64;             /* Used when p4type is P4_INT64 */
       
  7213     double *pReal;         /* Used when p4type is P4_REAL */
       
  7214     FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
       
  7215     VdbeFunc *pVdbeFunc;   /* Used when p4type is P4_VDBEFUNC */
       
  7216     CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
       
  7217     Mem *pMem;             /* Used when p4type is P4_MEM */
       
  7218     VTable *pVtab;         /* Used when p4type is P4_VTAB */
       
  7219     KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
       
  7220     int *ai;               /* Used when p4type is P4_INTARRAY */
       
  7221     SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
       
  7222   } p4;
       
  7223 #ifdef SQLITE_DEBUG
       
  7224   char *zComment;          /* Comment to improve readability */
       
  7225 #endif
       
  7226 #ifdef VDBE_PROFILE
       
  7227   int cnt;                 /* Number of times this instruction was executed */
       
  7228   u64 cycles;              /* Total time spent executing this instruction */
       
  7229 #endif
       
  7230 };
       
  7231 typedef struct VdbeOp VdbeOp;
       
  7232 
       
  7233 
       
  7234 /*
       
  7235 ** A sub-routine used to implement a trigger program.
       
  7236 */
       
  7237 struct SubProgram {
       
  7238   VdbeOp *aOp;                  /* Array of opcodes for sub-program */
       
  7239   int nOp;                      /* Elements in aOp[] */
       
  7240   int nMem;                     /* Number of memory cells required */
       
  7241   int nCsr;                     /* Number of cursors required */
       
  7242   int nRef;                     /* Number of pointers to this structure */
       
  7243   void *token;                  /* id that may be used to recursive triggers */
       
  7244 };
       
  7245 
       
  7246 /*
       
  7247 ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
       
  7248 ** it takes up less space.
       
  7249 */
       
  7250 struct VdbeOpList {
       
  7251   u8 opcode;          /* What operation to perform */
       
  7252   signed char p1;     /* First operand */
       
  7253   signed char p2;     /* Second parameter (often the jump destination) */
       
  7254   signed char p3;     /* Third parameter */
       
  7255 };
       
  7256 typedef struct VdbeOpList VdbeOpList;
       
  7257 
       
  7258 /*
       
  7259 ** Allowed values of VdbeOp.p4type
       
  7260 */
       
  7261 #define P4_NOTUSED    0   /* The P4 parameter is not used */
       
  7262 #define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
       
  7263 #define P4_STATIC   (-2)  /* Pointer to a static string */
       
  7264 #define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
       
  7265 #define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
       
  7266 #define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
       
  7267 #define P4_VDBEFUNC (-7)  /* P4 is a pointer to a VdbeFunc structure */
       
  7268 #define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
       
  7269 #define P4_TRANSIENT (-9) /* P4 is a pointer to a transient string */
       
  7270 #define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
       
  7271 #define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
       
  7272 #define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
       
  7273 #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
       
  7274 #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
       
  7275 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
       
  7276 #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
       
  7277 
       
  7278 /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
       
  7279 ** is made.  That copy is freed when the Vdbe is finalized.  But if the
       
  7280 ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used.  It still
       
  7281 ** gets freed when the Vdbe is finalized so it still should be obtained
       
  7282 ** from a single sqliteMalloc().  But no copy is made and the calling
       
  7283 ** function should *not* try to free the KeyInfo.
       
  7284 */
       
  7285 #define P4_KEYINFO_HANDOFF (-16)
       
  7286 #define P4_KEYINFO_STATIC  (-17)
       
  7287 
       
  7288 /*
       
  7289 ** The Vdbe.aColName array contains 5n Mem structures, where n is the 
       
  7290 ** number of columns of data returned by the statement.
       
  7291 */
       
  7292 #define COLNAME_NAME     0
       
  7293 #define COLNAME_DECLTYPE 1
       
  7294 #define COLNAME_DATABASE 2
       
  7295 #define COLNAME_TABLE    3
       
  7296 #define COLNAME_COLUMN   4
       
  7297 #ifdef SQLITE_ENABLE_COLUMN_METADATA
       
  7298 # define COLNAME_N        5      /* Number of COLNAME_xxx symbols */
       
  7299 #else
       
  7300 # ifdef SQLITE_OMIT_DECLTYPE
       
  7301 #   define COLNAME_N      1      /* Store only the name */
       
  7302 # else
       
  7303 #   define COLNAME_N      2      /* Store the name and decltype */
       
  7304 # endif
       
  7305 #endif
       
  7306 
       
  7307 /*
       
  7308 ** The following macro converts a relative address in the p2 field
       
  7309 ** of a VdbeOp structure into a negative number so that 
       
  7310 ** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
       
  7311 ** the macro again restores the address.
       
  7312 */
       
  7313 #define ADDR(X)  (-1-(X))
       
  7314 
       
  7315 /*
       
  7316 ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
       
  7317 ** header file that defines a number for each opcode used by the VDBE.
       
  7318 */
       
  7319 /************** Include opcodes.h in the middle of vdbe.h ********************/
       
  7320 /************** Begin file opcodes.h *****************************************/
       
  7321 /* Automatically generated.  Do not edit */
       
  7322 /* See the mkopcodeh.awk script for details */
       
  7323 #define OP_VNext                                1
       
  7324 #define OP_Affinity                             2
       
  7325 #define OP_Column                               3
       
  7326 #define OP_SetCookie                            4
       
  7327 #define OP_Seek                                 5
       
  7328 #define OP_Real                               130   /* same as TK_FLOAT    */
       
  7329 #define OP_Sequence                             6
       
  7330 #define OP_Savepoint                            7
       
  7331 #define OP_Ge                                  80   /* same as TK_GE       */
       
  7332 #define OP_RowKey                               8
       
  7333 #define OP_SCopy                                9
       
  7334 #define OP_Eq                                  76   /* same as TK_EQ       */
       
  7335 #define OP_OpenWrite                           10
       
  7336 #define OP_NotNull                             74   /* same as TK_NOTNULL  */
       
  7337 #define OP_If                                  11
       
  7338 #define OP_ToInt                              144   /* same as TK_TO_INT   */
       
  7339 #define OP_String8                             94   /* same as TK_STRING   */
       
  7340 #define OP_CollSeq                             12
       
  7341 #define OP_OpenRead                            13
       
  7342 #define OP_Expire                              14
       
  7343 #define OP_AutoCommit                          15
       
  7344 #define OP_Gt                                  77   /* same as TK_GT       */
       
  7345 #define OP_Pagecount                           16
       
  7346 #define OP_IntegrityCk                         17
       
  7347 #define OP_Sort                                18
       
  7348 #define OP_Copy                                20
       
  7349 #define OP_Trace                               21
       
  7350 #define OP_Function                            22
       
  7351 #define OP_IfNeg                               23
       
  7352 #define OP_And                                 69   /* same as TK_AND      */
       
  7353 #define OP_Subtract                            87   /* same as TK_MINUS    */
       
  7354 #define OP_Noop                                24
       
  7355 #define OP_Program                             25
       
  7356 #define OP_Return                              26
       
  7357 #define OP_Remainder                           90   /* same as TK_REM      */
       
  7358 #define OP_NewRowid                            27
       
  7359 #define OP_Multiply                            88   /* same as TK_STAR     */
       
  7360 #define OP_FkCounter                           28
       
  7361 #define OP_Variable                            29
       
  7362 #define OP_String                              30
       
  7363 #define OP_RealAffinity                        31
       
  7364 #define OP_VRename                             32
       
  7365 #define OP_ParseSchema                         33
       
  7366 #define OP_VOpen                               34
       
  7367 #define OP_Close                               35
       
  7368 #define OP_CreateIndex                         36
       
  7369 #define OP_IsUnique                            37
       
  7370 #define OP_NotFound                            38
       
  7371 #define OP_Int64                               39
       
  7372 #define OP_MustBeInt                           40
       
  7373 #define OP_Halt                                41
       
  7374 #define OP_Rowid                               42
       
  7375 #define OP_IdxLT                               43
       
  7376 #define OP_AddImm                              44
       
  7377 #define OP_RowData                             45
       
  7378 #define OP_MemMax                              46
       
  7379 #define OP_Or                                  68   /* same as TK_OR       */
       
  7380 #define OP_NotExists                           47
       
  7381 #define OP_Gosub                               48
       
  7382 #define OP_Divide                              89   /* same as TK_SLASH    */
       
  7383 #define OP_Integer                             49
       
  7384 #define OP_ToNumeric                          143   /* same as TK_TO_NUMERIC*/
       
  7385 #define OP_Prev                                50
       
  7386 #define OP_RowSetRead                          51
       
  7387 #define OP_Concat                              91   /* same as TK_CONCAT   */
       
  7388 #define OP_RowSetAdd                           52
       
  7389 #define OP_BitAnd                              82   /* same as TK_BITAND   */
       
  7390 #define OP_VColumn                             53
       
  7391 #define OP_CreateTable                         54
       
  7392 #define OP_Last                                55
       
  7393 #define OP_SeekLe                              56
       
  7394 #define OP_IsNull                              73   /* same as TK_ISNULL   */
       
  7395 #define OP_IncrVacuum                          57
       
  7396 #define OP_IdxRowid                            58
       
  7397 #define OP_ShiftRight                          85   /* same as TK_RSHIFT   */
       
  7398 #define OP_ResetCount                          59
       
  7399 #define OP_Yield                               60
       
  7400 #define OP_DropTrigger                         61
       
  7401 #define OP_DropIndex                           62
       
  7402 #define OP_Param                               63
       
  7403 #define OP_IdxGE                               64
       
  7404 #define OP_IdxDelete                           65
       
  7405 #define OP_Vacuum                              66
       
  7406 #define OP_IfNot                               67
       
  7407 #define OP_DropTable                           70
       
  7408 #define OP_SeekLt                              71
       
  7409 #define OP_MakeRecord                          72
       
  7410 #define OP_ToBlob                             142   /* same as TK_TO_BLOB  */
       
  7411 #define OP_ResultRow                           81
       
  7412 #define OP_Delete                              92
       
  7413 #define OP_AggFinal                            95
       
  7414 #define OP_Compare                             96
       
  7415 #define OP_ShiftLeft                           84   /* same as TK_LSHIFT   */
       
  7416 #define OP_Goto                                97
       
  7417 #define OP_TableLock                           98
       
  7418 #define OP_Clear                               99
       
  7419 #define OP_Le                                  78   /* same as TK_LE       */
       
  7420 #define OP_VerifyCookie                       100
       
  7421 #define OP_AggStep                            101
       
  7422 #define OP_ToText                             141   /* same as TK_TO_TEXT  */
       
  7423 #define OP_Not                                 19   /* same as TK_NOT      */
       
  7424 #define OP_ToReal                             145   /* same as TK_TO_REAL  */
       
  7425 #define OP_Transaction                        102
       
  7426 #define OP_VFilter                            103
       
  7427 #define OP_Ne                                  75   /* same as TK_NE       */
       
  7428 #define OP_VDestroy                           104
       
  7429 #define OP_BitOr                               83   /* same as TK_BITOR    */
       
  7430 #define OP_Next                               105
       
  7431 #define OP_Count                              106
       
  7432 #define OP_IdxInsert                          107
       
  7433 #define OP_Lt                                  79   /* same as TK_LT       */
       
  7434 #define OP_FkIfZero                           108
       
  7435 #define OP_SeekGe                             109
       
  7436 #define OP_Insert                             110
       
  7437 #define OP_Destroy                            111
       
  7438 #define OP_ReadCookie                         112
       
  7439 #define OP_RowSetTest                         113
       
  7440 #define OP_LoadAnalysis                       114
       
  7441 #define OP_Explain                            115
       
  7442 #define OP_HaltIfNull                         116
       
  7443 #define OP_OpenPseudo                         117
       
  7444 #define OP_OpenEphemeral                      118
       
  7445 #define OP_Null                               119
       
  7446 #define OP_Move                               120
       
  7447 #define OP_Blob                               121
       
  7448 #define OP_Add                                 86   /* same as TK_PLUS     */
       
  7449 #define OP_Rewind                             122
       
  7450 #define OP_SeekGt                             123
       
  7451 #define OP_VBegin                             124
       
  7452 #define OP_VUpdate                            125
       
  7453 #define OP_IfZero                             126
       
  7454 #define OP_BitNot                              93   /* same as TK_BITNOT   */
       
  7455 #define OP_VCreate                            127
       
  7456 #define OP_Found                              128
       
  7457 #define OP_IfPos                              129
       
  7458 #define OP_NullRow                            131
       
  7459 #define OP_Jump                               132
       
  7460 #define OP_Permutation                        133
       
  7461 
       
  7462 /* The following opcode values are never used */
       
  7463 #define OP_NotUsed_134                        134
       
  7464 #define OP_NotUsed_135                        135
       
  7465 #define OP_NotUsed_136                        136
       
  7466 #define OP_NotUsed_137                        137
       
  7467 #define OP_NotUsed_138                        138
       
  7468 #define OP_NotUsed_139                        139
       
  7469 #define OP_NotUsed_140                        140
       
  7470 
       
  7471 
       
  7472 /* Properties such as "out2" or "jump" that are specified in
       
  7473 ** comments following the "case" for each opcode in the vdbe.c
       
  7474 ** are encoded into bitvectors as follows:
       
  7475 */
       
  7476 #define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
       
  7477 #define OPFLG_OUT2_PRERELEASE 0x0002  /* out2-prerelease: */
       
  7478 #define OPFLG_IN1             0x0004  /* in1:   P1 is an input */
       
  7479 #define OPFLG_IN2             0x0008  /* in2:   P2 is an input */
       
  7480 #define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
       
  7481 #define OPFLG_OUT3            0x0020  /* out3:  P3 is an output */
       
  7482 #define OPFLG_INITIALIZER {\
       
  7483 /*   0 */ 0x00, 0x01, 0x00, 0x00, 0x10, 0x08, 0x02, 0x00,\
       
  7484 /*   8 */ 0x00, 0x04, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00,\
       
  7485 /*  16 */ 0x02, 0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x05,\
       
  7486 /*  24 */ 0x00, 0x01, 0x04, 0x02, 0x00, 0x00, 0x02, 0x04,\
       
  7487 /*  32 */ 0x00, 0x00, 0x00, 0x00, 0x02, 0x11, 0x11, 0x02,\
       
  7488 /*  40 */ 0x05, 0x00, 0x02, 0x11, 0x04, 0x00, 0x08, 0x11,\
       
  7489 /*  48 */ 0x01, 0x02, 0x01, 0x21, 0x08, 0x00, 0x02, 0x01,\
       
  7490 /*  56 */ 0x11, 0x01, 0x02, 0x00, 0x04, 0x00, 0x00, 0x02,\
       
  7491 /*  64 */ 0x11, 0x00, 0x00, 0x05, 0x2c, 0x2c, 0x00, 0x11,\
       
  7492 /*  72 */ 0x00, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
       
  7493 /*  80 */ 0x15, 0x00, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c,\
       
  7494 /*  88 */ 0x2c, 0x2c, 0x2c, 0x2c, 0x00, 0x04, 0x02, 0x00,\
       
  7495 /*  96 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,\
       
  7496 /* 104 */ 0x00, 0x01, 0x02, 0x08, 0x01, 0x11, 0x00, 0x02,\
       
  7497 /* 112 */ 0x02, 0x15, 0x00, 0x00, 0x10, 0x00, 0x00, 0x02,\
       
  7498 /* 120 */ 0x00, 0x02, 0x01, 0x11, 0x00, 0x00, 0x05, 0x00,\
       
  7499 /* 128 */ 0x11, 0x05, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,\
       
  7500 /* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
       
  7501 /* 144 */ 0x04, 0x04,}
       
  7502 
       
  7503 /************** End of opcodes.h *********************************************/
       
  7504 /************** Continuing where we left off in vdbe.h ***********************/
       
  7505 
       
  7506 /*
       
  7507 ** Prototypes for the VDBE interface.  See comments on the implementation
       
  7508 ** for a description of what each of these routines does.
       
  7509 */
       
  7510 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*);
       
  7511 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
       
  7512 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
       
  7513 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
       
  7514 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
       
  7515 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
       
  7516 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
       
  7517 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
       
  7518 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
       
  7519 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
       
  7520 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
       
  7521 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
       
  7522 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
       
  7523 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
       
  7524 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
       
  7525 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
       
  7526 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
       
  7527 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
       
  7528 SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,int,int,int,int,int,int);
       
  7529 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
       
  7530 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
       
  7531 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
       
  7532 #ifdef SQLITE_DEBUG
       
  7533 SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
       
  7534 SQLITE_PRIVATE   void sqlite3VdbeTrace(Vdbe*,FILE*);
       
  7535 #endif
       
  7536 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
       
  7537 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
       
  7538 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
       
  7539 SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
       
  7540 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
       
  7541 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
       
  7542 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
       
  7543 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
       
  7544 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
       
  7545 SQLITE_PRIVATE void sqlite3VdbeProgramDelete(sqlite3 *, SubProgram *, int);
       
  7546 
       
  7547 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
       
  7548 SQLITE_PRIVATE int sqlite3VdbeReleaseMemory(int);
       
  7549 #endif
       
  7550 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,char*,int);
       
  7551 SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord*);
       
  7552 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
       
  7553 
       
  7554 
       
  7555 #ifndef NDEBUG
       
  7556 SQLITE_PRIVATE   void sqlite3VdbeComment(Vdbe*, const char*, ...);
       
  7557 # define VdbeComment(X)  sqlite3VdbeComment X
       
  7558 SQLITE_PRIVATE   void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
       
  7559 # define VdbeNoopComment(X)  sqlite3VdbeNoopComment X
       
  7560 #else
       
  7561 # define VdbeComment(X)
       
  7562 # define VdbeNoopComment(X)
       
  7563 #endif
       
  7564 
       
  7565 #endif
       
  7566 
       
  7567 /************** End of vdbe.h ************************************************/
       
  7568 /************** Continuing where we left off in sqliteInt.h ******************/
       
  7569 /************** Include pager.h in the middle of sqliteInt.h *****************/
       
  7570 /************** Begin file pager.h *******************************************/
       
  7571 /*
       
  7572 ** 2001 September 15
       
  7573 **
       
  7574 ** The author disclaims copyright to this source code.  In place of
       
  7575 ** a legal notice, here is a blessing:
       
  7576 **
       
  7577 **    May you do good and not evil.
       
  7578 **    May you find forgiveness for yourself and forgive others.
       
  7579 **    May you share freely, never taking more than you give.
       
  7580 **
       
  7581 *************************************************************************
       
  7582 ** This header file defines the interface that the sqlite page cache
       
  7583 ** subsystem.  The page cache subsystem reads and writes a file a page
       
  7584 ** at a time and provides a journal for rollback.
       
  7585 **
       
  7586 ** @(#) $Id: pager.h,v 1.104 2009/07/24 19:01:19 drh Exp $
       
  7587 */
       
  7588 
       
  7589 #ifndef _PAGER_H_
       
  7590 #define _PAGER_H_
       
  7591 
       
  7592 /*
       
  7593 ** Default maximum size for persistent journal files. A negative 
       
  7594 ** value means no limit. This value may be overridden using the 
       
  7595 ** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
       
  7596 */
       
  7597 #ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
       
  7598   #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
       
  7599 #endif
       
  7600 
       
  7601 /*
       
  7602 ** The type used to represent a page number.  The first page in a file
       
  7603 ** is called page 1.  0 is used to represent "not a page".
       
  7604 */
       
  7605 typedef u32 Pgno;
       
  7606 
       
  7607 /*
       
  7608 ** Each open file is managed by a separate instance of the "Pager" structure.
       
  7609 */
       
  7610 typedef struct Pager Pager;
       
  7611 
       
  7612 /*
       
  7613 ** Handle type for pages.
       
  7614 */
       
  7615 typedef struct PgHdr DbPage;
       
  7616 
       
  7617 /*
       
  7618 ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
       
  7619 ** reserved for working around a windows/posix incompatibility). It is
       
  7620 ** used in the journal to signify that the remainder of the journal file 
       
  7621 ** is devoted to storing a master journal name - there are no more pages to
       
  7622 ** roll back. See comments for function writeMasterJournal() in pager.c 
       
  7623 ** for details.
       
  7624 */
       
  7625 #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
       
  7626 
       
  7627 /*
       
  7628 ** Allowed values for the flags parameter to sqlite3PagerOpen().
       
  7629 **
       
  7630 ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
       
  7631 */
       
  7632 #define PAGER_OMIT_JOURNAL  0x0001    /* Do not use a rollback journal */
       
  7633 #define PAGER_NO_READLOCK   0x0002    /* Omit readlocks on readonly files */
       
  7634 
       
  7635 /*
       
  7636 ** Valid values for the second argument to sqlite3PagerLockingMode().
       
  7637 */
       
  7638 #define PAGER_LOCKINGMODE_QUERY      -1
       
  7639 #define PAGER_LOCKINGMODE_NORMAL      0
       
  7640 #define PAGER_LOCKINGMODE_EXCLUSIVE   1
       
  7641 
       
  7642 /*
       
  7643 ** Valid values for the second argument to sqlite3PagerJournalMode().
       
  7644 */
       
  7645 #define PAGER_JOURNALMODE_QUERY      -1
       
  7646 #define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
       
  7647 #define PAGER_JOURNALMODE_PERSIST     1   /* Commit by zeroing journal header */
       
  7648 #define PAGER_JOURNALMODE_OFF         2   /* Journal omitted.  */
       
  7649 #define PAGER_JOURNALMODE_TRUNCATE    3   /* Commit by truncating journal */
       
  7650 #define PAGER_JOURNALMODE_MEMORY      4   /* In-memory journal file */
       
  7651 
       
  7652 /*
       
  7653 ** The remainder of this file contains the declarations of the functions
       
  7654 ** that make up the Pager sub-system API. See source code comments for 
       
  7655 ** a detailed description of each routine.
       
  7656 */
       
  7657 
       
  7658 /* Open and close a Pager connection. */ 
       
  7659 SQLITE_PRIVATE int sqlite3PagerOpen(
       
  7660   sqlite3_vfs*,
       
  7661   Pager **ppPager,
       
  7662   const char*,
       
  7663   int,
       
  7664   int,
       
  7665   int,
       
  7666   void(*)(DbPage*)
       
  7667 );
       
  7668 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
       
  7669 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
       
  7670 
       
  7671 /* Functions used to configure a Pager object. */
       
  7672 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
       
  7673 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*, int);
       
  7674 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
       
  7675 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
       
  7676 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int);
       
  7677 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
       
  7678 SQLITE_PRIVATE int sqlite3PagerJournalMode(Pager *, int);
       
  7679 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
       
  7680 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
       
  7681 
       
  7682 /* Functions used to obtain and release page references. */ 
       
  7683 SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
       
  7684 #define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
       
  7685 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
       
  7686 SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
       
  7687 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
       
  7688 
       
  7689 /* Operations on page references. */
       
  7690 SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
       
  7691 SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
       
  7692 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
       
  7693 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
       
  7694 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *); 
       
  7695 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *); 
       
  7696 
       
  7697 /* Functions used to manage pager transactions and savepoints. */
       
  7698 SQLITE_PRIVATE int sqlite3PagerPagecount(Pager*, int*);
       
  7699 SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
       
  7700 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
       
  7701 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
       
  7702 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
       
  7703 SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
       
  7704 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
       
  7705 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
       
  7706 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
       
  7707 
       
  7708 /* Functions used to query pager state and configuration. */
       
  7709 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
       
  7710 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
       
  7711 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*);
       
  7712 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
       
  7713 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
       
  7714 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
       
  7715 SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
       
  7716 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
       
  7717 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
       
  7718 
       
  7719 /* Functions used to truncate the database file. */
       
  7720 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
       
  7721 
       
  7722 /* Functions to support testing and debugging. */
       
  7723 #if !defined(NDEBUG) || defined(SQLITE_TEST)
       
  7724 SQLITE_PRIVATE   Pgno sqlite3PagerPagenumber(DbPage*);
       
  7725 SQLITE_PRIVATE   int sqlite3PagerIswriteable(DbPage*);
       
  7726 #endif
       
  7727 #ifdef SQLITE_TEST
       
  7728 SQLITE_PRIVATE   int *sqlite3PagerStats(Pager*);
       
  7729 SQLITE_PRIVATE   void sqlite3PagerRefdump(Pager*);
       
  7730   void disable_simulated_io_errors(void);
       
  7731   void enable_simulated_io_errors(void);
       
  7732 #else
       
  7733 # define disable_simulated_io_errors()
       
  7734 # define enable_simulated_io_errors()
       
  7735 #endif
       
  7736 
       
  7737 #endif /* _PAGER_H_ */
       
  7738 
       
  7739 /************** End of pager.h ***********************************************/
       
  7740 /************** Continuing where we left off in sqliteInt.h ******************/
       
  7741 /************** Include pcache.h in the middle of sqliteInt.h ****************/
       
  7742 /************** Begin file pcache.h ******************************************/
       
  7743 /*
       
  7744 ** 2008 August 05
       
  7745 **
       
  7746 ** The author disclaims copyright to this source code.  In place of
       
  7747 ** a legal notice, here is a blessing:
       
  7748 **
       
  7749 **    May you do good and not evil.
       
  7750 **    May you find forgiveness for yourself and forgive others.
       
  7751 **    May you share freely, never taking more than you give.
       
  7752 **
       
  7753 *************************************************************************
       
  7754 ** This header file defines the interface that the sqlite page cache
       
  7755 ** subsystem. 
       
  7756 **
       
  7757 ** @(#) $Id: pcache.h,v 1.20 2009/07/25 11:46:49 danielk1977 Exp $
       
  7758 */
       
  7759 
       
  7760 #ifndef _PCACHE_H_
       
  7761 
       
  7762 typedef struct PgHdr PgHdr;
       
  7763 typedef struct PCache PCache;
       
  7764 
       
  7765 /*
       
  7766 ** Every page in the cache is controlled by an instance of the following
       
  7767 ** structure.
       
  7768 */
       
  7769 struct PgHdr {
       
  7770   void *pData;                   /* Content of this page */
       
  7771   void *pExtra;                  /* Extra content */
       
  7772   PgHdr *pDirty;                 /* Transient list of dirty pages */
       
  7773   Pgno pgno;                     /* Page number for this page */
       
  7774   Pager *pPager;                 /* The pager this page is part of */
       
  7775 #ifdef SQLITE_CHECK_PAGES
       
  7776   u32 pageHash;                  /* Hash of page content */
       
  7777 #endif
       
  7778   u16 flags;                     /* PGHDR flags defined below */
       
  7779 
       
  7780   /**********************************************************************
       
  7781   ** Elements above are public.  All that follows is private to pcache.c
       
  7782   ** and should not be accessed by other modules.
       
  7783   */
       
  7784   i16 nRef;                      /* Number of users of this page */
       
  7785   PCache *pCache;                /* Cache that owns this page */
       
  7786 
       
  7787   PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
       
  7788   PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
       
  7789 };
       
  7790 
       
  7791 /* Bit values for PgHdr.flags */
       
  7792 #define PGHDR_DIRTY             0x002  /* Page has changed */
       
  7793 #define PGHDR_NEED_SYNC         0x004  /* Fsync the rollback journal before
       
  7794                                        ** writing this page to the database */
       
  7795 #define PGHDR_NEED_READ         0x008  /* Content is unread */
       
  7796 #define PGHDR_REUSE_UNLIKELY    0x010  /* A hint that reuse is unlikely */
       
  7797 #define PGHDR_DONT_WRITE        0x020  /* Do not write content to disk */
       
  7798 
       
  7799 /* Initialize and shutdown the page cache subsystem */
       
  7800 SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
       
  7801 SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
       
  7802 
       
  7803 /* Page cache buffer management:
       
  7804 ** These routines implement SQLITE_CONFIG_PAGECACHE.
       
  7805 */
       
  7806 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
       
  7807 
       
  7808 /* Create a new pager cache.
       
  7809 ** Under memory stress, invoke xStress to try to make pages clean.
       
  7810 ** Only clean and unpinned pages can be reclaimed.
       
  7811 */
       
  7812 SQLITE_PRIVATE void sqlite3PcacheOpen(
       
  7813   int szPage,                    /* Size of every page */
       
  7814   int szExtra,                   /* Extra space associated with each page */
       
  7815   int bPurgeable,                /* True if pages are on backing store */
       
  7816   int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
       
  7817   void *pStress,                 /* Argument to xStress */
       
  7818   PCache *pToInit                /* Preallocated space for the PCache */
       
  7819 );
       
  7820 
       
  7821 /* Modify the page-size after the cache has been created. */
       
  7822 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
       
  7823 
       
  7824 /* Return the size in bytes of a PCache object.  Used to preallocate
       
  7825 ** storage space.
       
  7826 */
       
  7827 SQLITE_PRIVATE int sqlite3PcacheSize(void);
       
  7828 
       
  7829 /* One release per successful fetch.  Page is pinned until released.
       
  7830 ** Reference counted. 
       
  7831 */
       
  7832 SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
       
  7833 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
       
  7834 
       
  7835 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
       
  7836 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
       
  7837 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
       
  7838 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
       
  7839 
       
  7840 /* Change a page number.  Used by incr-vacuum. */
       
  7841 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
       
  7842 
       
  7843 /* Remove all pages with pgno>x.  Reset the cache if x==0 */
       
  7844 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
       
  7845 
       
  7846 /* Get a list of all dirty pages in the cache, sorted by page number */
       
  7847 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
       
  7848 
       
  7849 /* Reset and close the cache object */
       
  7850 SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
       
  7851 
       
  7852 /* Clear flags from pages of the page cache */
       
  7853 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
       
  7854 
       
  7855 /* Discard the contents of the cache */
       
  7856 SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
       
  7857 
       
  7858 /* Return the total number of outstanding page references */
       
  7859 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
       
  7860 
       
  7861 /* Increment the reference count of an existing page */
       
  7862 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
       
  7863 
       
  7864 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
       
  7865 
       
  7866 /* Return the total number of pages stored in the cache */
       
  7867 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
       
  7868 
       
  7869 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
       
  7870 /* Iterate through all dirty pages currently stored in the cache. This
       
  7871 ** interface is only available if SQLITE_CHECK_PAGES is defined when the 
       
  7872 ** library is built.
       
  7873 */
       
  7874 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
       
  7875 #endif
       
  7876 
       
  7877 /* Set and get the suggested cache-size for the specified pager-cache.
       
  7878 **
       
  7879 ** If no global maximum is configured, then the system attempts to limit
       
  7880 ** the total number of pages cached by purgeable pager-caches to the sum
       
  7881 ** of the suggested cache-sizes.
       
  7882 */
       
  7883 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
       
  7884 #ifdef SQLITE_TEST
       
  7885 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
       
  7886 #endif
       
  7887 
       
  7888 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
       
  7889 /* Try to return memory used by the pcache module to the main memory heap */
       
  7890 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
       
  7891 #endif
       
  7892 
       
  7893 #ifdef SQLITE_TEST
       
  7894 SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
       
  7895 #endif
       
  7896 
       
  7897 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
       
  7898 
       
  7899 #endif /* _PCACHE_H_ */
       
  7900 
       
  7901 /************** End of pcache.h **********************************************/
       
  7902 /************** Continuing where we left off in sqliteInt.h ******************/
       
  7903 
       
  7904 /************** Include os.h in the middle of sqliteInt.h ********************/
       
  7905 /************** Begin file os.h **********************************************/
       
  7906 /*
       
  7907 ** 2001 September 16
       
  7908 **
       
  7909 ** The author disclaims copyright to this source code.  In place of
       
  7910 ** a legal notice, here is a blessing:
       
  7911 **
       
  7912 **    May you do good and not evil.
       
  7913 **    May you find forgiveness for yourself and forgive others.
       
  7914 **    May you share freely, never taking more than you give.
       
  7915 **
       
  7916 ******************************************************************************
       
  7917 **
       
  7918 ** This header file (together with is companion C source-code file
       
  7919 ** "os.c") attempt to abstract the underlying operating system so that
       
  7920 ** the SQLite library will work on both POSIX and windows systems.
       
  7921 **
       
  7922 ** This header file is #include-ed by sqliteInt.h and thus ends up
       
  7923 ** being included by every source file.
       
  7924 **
       
  7925 ** $Id: os.h,v 1.108 2009/02/05 16:31:46 drh Exp $
       
  7926 */
       
  7927 #ifndef _SQLITE_OS_H_
       
  7928 #define _SQLITE_OS_H_
       
  7929 
       
  7930 /*
       
  7931 ** Figure out if we are dealing with Unix, Windows, or some other
       
  7932 ** operating system.  After the following block of preprocess macros,
       
  7933 ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER 
       
  7934 ** will defined to either 1 or 0.  One of the four will be 1.  The other 
       
  7935 ** three will be 0.
       
  7936 */
       
  7937 #if defined(SQLITE_OS_OTHER)
       
  7938 # if SQLITE_OS_OTHER==1
       
  7939 #   undef SQLITE_OS_UNIX
       
  7940 #   define SQLITE_OS_UNIX 0
       
  7941 #   undef SQLITE_OS_WIN
       
  7942 #   define SQLITE_OS_WIN 0
       
  7943 #   undef SQLITE_OS_OS2
       
  7944 #   define SQLITE_OS_OS2 0
       
  7945 # else
       
  7946 #   undef SQLITE_OS_OTHER
       
  7947 # endif
       
  7948 #endif
       
  7949 #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
       
  7950 # define SQLITE_OS_OTHER 0
       
  7951 # ifndef SQLITE_OS_WIN
       
  7952 #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
       
  7953 #     define SQLITE_OS_WIN 1
       
  7954 #     define SQLITE_OS_UNIX 0
       
  7955 #     define SQLITE_OS_OS2 0
       
  7956 #   elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
       
  7957 #     define SQLITE_OS_WIN 0
       
  7958 #     define SQLITE_OS_UNIX 0
       
  7959 #     define SQLITE_OS_OS2 1
       
  7960 #   else
       
  7961 #     define SQLITE_OS_WIN 0
       
  7962 #     define SQLITE_OS_UNIX 1
       
  7963 #     define SQLITE_OS_OS2 0
       
  7964 #  endif
       
  7965 # else
       
  7966 #  define SQLITE_OS_UNIX 0
       
  7967 #  define SQLITE_OS_OS2 0
       
  7968 # endif
       
  7969 #else
       
  7970 # ifndef SQLITE_OS_WIN
       
  7971 #  define SQLITE_OS_WIN 0
       
  7972 # endif
       
  7973 #endif
       
  7974 
       
  7975 /*
       
  7976 ** Determine if we are dealing with WindowsCE - which has a much
       
  7977 ** reduced API.
       
  7978 */
       
  7979 #if defined(_WIN32_WCE)
       
  7980 # define SQLITE_OS_WINCE 1
       
  7981 #else
       
  7982 # define SQLITE_OS_WINCE 0
       
  7983 #endif
       
  7984 
       
  7985 
       
  7986 /*
       
  7987 ** Define the maximum size of a temporary filename
       
  7988 */
       
  7989 #if SQLITE_OS_WIN
       
  7990 # include <windows.h>
       
  7991 # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
       
  7992 #elif SQLITE_OS_OS2
       
  7993 # if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
       
  7994 #  include <os2safe.h> /* has to be included before os2.h for linking to work */
       
  7995 # endif
       
  7996 # define INCL_DOSDATETIME
       
  7997 # define INCL_DOSFILEMGR
       
  7998 # define INCL_DOSERRORS
       
  7999 # define INCL_DOSMISC
       
  8000 # define INCL_DOSPROCESS
       
  8001 # define INCL_DOSMODULEMGR
       
  8002 # define INCL_DOSSEMAPHORES
       
  8003 # include <os2.h>
       
  8004 # include <uconv.h>
       
  8005 # define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
       
  8006 #else
       
  8007 # define SQLITE_TEMPNAME_SIZE 200
       
  8008 #endif
       
  8009 
       
  8010 /* If the SET_FULLSYNC macro is not defined above, then make it
       
  8011 ** a no-op
       
  8012 */
       
  8013 #ifndef SET_FULLSYNC
       
  8014 # define SET_FULLSYNC(x,y)
       
  8015 #endif
       
  8016 
       
  8017 /*
       
  8018 ** The default size of a disk sector
       
  8019 */
       
  8020 #ifndef SQLITE_DEFAULT_SECTOR_SIZE
       
  8021 # define SQLITE_DEFAULT_SECTOR_SIZE 512
       
  8022 #endif
       
  8023 
       
  8024 /*
       
  8025 ** Temporary files are named starting with this prefix followed by 16 random
       
  8026 ** alphanumeric characters, and no file extension. They are stored in the
       
  8027 ** OS's standard temporary file directory, and are deleted prior to exit.
       
  8028 ** If sqlite is being embedded in another program, you may wish to change the
       
  8029 ** prefix to reflect your program's name, so that if your program exits
       
  8030 ** prematurely, old temporary files can be easily identified. This can be done
       
  8031 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
       
  8032 **
       
  8033 ** 2006-10-31:  The default prefix used to be "sqlite_".  But then
       
  8034 ** Mcafee started using SQLite in their anti-virus product and it
       
  8035 ** started putting files with the "sqlite" name in the c:/temp folder.
       
  8036 ** This annoyed many windows users.  Those users would then do a 
       
  8037 ** Google search for "sqlite", find the telephone numbers of the
       
  8038 ** developers and call to wake them up at night and complain.
       
  8039 ** For this reason, the default name prefix is changed to be "sqlite" 
       
  8040 ** spelled backwards.  So the temp files are still identified, but
       
  8041 ** anybody smart enough to figure out the code is also likely smart
       
  8042 ** enough to know that calling the developer will not help get rid
       
  8043 ** of the file.
       
  8044 */
       
  8045 #ifndef SQLITE_TEMP_FILE_PREFIX
       
  8046 # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
       
  8047 #endif
       
  8048 
       
  8049 /*
       
  8050 ** The following values may be passed as the second argument to
       
  8051 ** sqlite3OsLock(). The various locks exhibit the following semantics:
       
  8052 **
       
  8053 ** SHARED:    Any number of processes may hold a SHARED lock simultaneously.
       
  8054 ** RESERVED:  A single process may hold a RESERVED lock on a file at
       
  8055 **            any time. Other processes may hold and obtain new SHARED locks.
       
  8056 ** PENDING:   A single process may hold a PENDING lock on a file at
       
  8057 **            any one time. Existing SHARED locks may persist, but no new
       
  8058 **            SHARED locks may be obtained by other processes.
       
  8059 ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
       
  8060 **
       
  8061 ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
       
  8062 ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
       
  8063 ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
       
  8064 ** sqlite3OsLock().
       
  8065 */
       
  8066 #define NO_LOCK         0
       
  8067 #define SHARED_LOCK     1
       
  8068 #define RESERVED_LOCK   2
       
  8069 #define PENDING_LOCK    3
       
  8070 #define EXCLUSIVE_LOCK  4
       
  8071 
       
  8072 /*
       
  8073 ** File Locking Notes:  (Mostly about windows but also some info for Unix)
       
  8074 **
       
  8075 ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
       
  8076 ** those functions are not available.  So we use only LockFile() and
       
  8077 ** UnlockFile().
       
  8078 **
       
  8079 ** LockFile() prevents not just writing but also reading by other processes.
       
  8080 ** A SHARED_LOCK is obtained by locking a single randomly-chosen 
       
  8081 ** byte out of a specific range of bytes. The lock byte is obtained at 
       
  8082 ** random so two separate readers can probably access the file at the 
       
  8083 ** same time, unless they are unlucky and choose the same lock byte.
       
  8084 ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
       
  8085 ** There can only be one writer.  A RESERVED_LOCK is obtained by locking
       
  8086 ** a single byte of the file that is designated as the reserved lock byte.
       
  8087 ** A PENDING_LOCK is obtained by locking a designated byte different from
       
  8088 ** the RESERVED_LOCK byte.
       
  8089 **
       
  8090 ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
       
  8091 ** which means we can use reader/writer locks.  When reader/writer locks
       
  8092 ** are used, the lock is placed on the same range of bytes that is used
       
  8093 ** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
       
  8094 ** will support two or more Win95 readers or two or more WinNT readers.
       
  8095 ** But a single Win95 reader will lock out all WinNT readers and a single
       
  8096 ** WinNT reader will lock out all other Win95 readers.
       
  8097 **
       
  8098 ** The following #defines specify the range of bytes used for locking.
       
  8099 ** SHARED_SIZE is the number of bytes available in the pool from which
       
  8100 ** a random byte is selected for a shared lock.  The pool of bytes for
       
  8101 ** shared locks begins at SHARED_FIRST. 
       
  8102 **
       
  8103 ** The same locking strategy and
       
  8104 ** byte ranges are used for Unix.  This leaves open the possiblity of having
       
  8105 ** clients on win95, winNT, and unix all talking to the same shared file
       
  8106 ** and all locking correctly.  To do so would require that samba (or whatever
       
  8107 ** tool is being used for file sharing) implements locks correctly between
       
  8108 ** windows and unix.  I'm guessing that isn't likely to happen, but by
       
  8109 ** using the same locking range we are at least open to the possibility.
       
  8110 **
       
  8111 ** Locking in windows is manditory.  For this reason, we cannot store
       
  8112 ** actual data in the bytes used for locking.  The pager never allocates
       
  8113 ** the pages involved in locking therefore.  SHARED_SIZE is selected so
       
  8114 ** that all locks will fit on a single page even at the minimum page size.
       
  8115 ** PENDING_BYTE defines the beginning of the locks.  By default PENDING_BYTE
       
  8116 ** is set high so that we don't have to allocate an unused page except
       
  8117 ** for very large databases.  But one should test the page skipping logic 
       
  8118 ** by setting PENDING_BYTE low and running the entire regression suite.
       
  8119 **
       
  8120 ** Changing the value of PENDING_BYTE results in a subtly incompatible
       
  8121 ** file format.  Depending on how it is changed, you might not notice
       
  8122 ** the incompatibility right away, even running a full regression test.
       
  8123 ** The default location of PENDING_BYTE is the first byte past the
       
  8124 ** 1GB boundary.
       
  8125 **
       
  8126 */
       
  8127 #define PENDING_BYTE      sqlite3PendingByte
       
  8128 #define RESERVED_BYTE     (PENDING_BYTE+1)
       
  8129 #define SHARED_FIRST      (PENDING_BYTE+2)
       
  8130 #define SHARED_SIZE       510
       
  8131 
       
  8132 /*
       
  8133 ** Wrapper around OS specific sqlite3_os_init() function.
       
  8134 */
       
  8135 SQLITE_PRIVATE int sqlite3OsInit(void);
       
  8136 
       
  8137 /* 
       
  8138 ** Functions for accessing sqlite3_file methods 
       
  8139 */
       
  8140 SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
       
  8141 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
       
  8142 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
       
  8143 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
       
  8144 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
       
  8145 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
       
  8146 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
       
  8147 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
       
  8148 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
       
  8149 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
       
  8150 #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
       
  8151 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
       
  8152 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
       
  8153 
       
  8154 /* 
       
  8155 ** Functions for accessing sqlite3_vfs methods 
       
  8156 */
       
  8157 SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
       
  8158 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
       
  8159 SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
       
  8160 SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
       
  8161 #ifndef SQLITE_OMIT_LOAD_EXTENSION
       
  8162 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
       
  8163 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
       
  8164 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
       
  8165 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
       
  8166 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
       
  8167 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
       
  8168 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
       
  8169 SQLITE_PRIVATE int sqlite3OsCurrentTime(sqlite3_vfs *, double*);
       
  8170 
       
  8171 /*
       
  8172 ** Convenience functions for opening and closing files using 
       
  8173 ** sqlite3_malloc() to obtain space for the file-handle structure.
       
  8174 */
       
  8175 SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
       
  8176 SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
       
  8177 
       
  8178 #endif /* _SQLITE_OS_H_ */
       
  8179 
       
  8180 /************** End of os.h **************************************************/
       
  8181 /************** Continuing where we left off in sqliteInt.h ******************/
       
  8182 /************** Include mutex.h in the middle of sqliteInt.h *****************/
       
  8183 /************** Begin file mutex.h *******************************************/
       
  8184 /*
       
  8185 ** 2007 August 28
       
  8186 **
       
  8187 ** The author disclaims copyright to this source code.  In place of
       
  8188 ** a legal notice, here is a blessing:
       
  8189 **
       
  8190 **    May you do good and not evil.
       
  8191 **    May you find forgiveness for yourself and forgive others.
       
  8192 **    May you share freely, never taking more than you give.
       
  8193 **
       
  8194 *************************************************************************
       
  8195 **
       
  8196 ** This file contains the common header for all mutex implementations.
       
  8197 ** The sqliteInt.h header #includes this file so that it is available
       
  8198 ** to all source files.  We break it out in an effort to keep the code
       
  8199 ** better organized.
       
  8200 **
       
  8201 ** NOTE:  source files should *not* #include this header file directly.
       
  8202 ** Source files should #include the sqliteInt.h file and let that file
       
  8203 ** include this one indirectly.
       
  8204 **
       
  8205 ** $Id: mutex.h,v 1.9 2008/10/07 15:25:48 drh Exp $
       
  8206 */
       
  8207 
       
  8208 
       
  8209 /*
       
  8210 ** Figure out what version of the code to use.  The choices are
       
  8211 **
       
  8212 **   SQLITE_MUTEX_OMIT         No mutex logic.  Not even stubs.  The
       
  8213 **                             mutexes implemention cannot be overridden
       
  8214 **                             at start-time.
       
  8215 **
       
  8216 **   SQLITE_MUTEX_NOOP         For single-threaded applications.  No
       
  8217 **                             mutual exclusion is provided.  But this
       
  8218 **                             implementation can be overridden at
       
  8219 **                             start-time.
       
  8220 **
       
  8221 **   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
       
  8222 **
       
  8223 **   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
       
  8224 **
       
  8225 **   SQLITE_MUTEX_OS2          For multi-threaded applications on OS/2.
       
  8226 */
       
  8227 #if !SQLITE_THREADSAFE
       
  8228 # define SQLITE_MUTEX_OMIT
       
  8229 #endif
       
  8230 #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
       
  8231 #  if SQLITE_OS_UNIX
       
  8232 #    define SQLITE_MUTEX_PTHREADS
       
  8233 #  elif SQLITE_OS_WIN
       
  8234 #    define SQLITE_MUTEX_W32
       
  8235 #  elif SQLITE_OS_OS2
       
  8236 #    define SQLITE_MUTEX_OS2
       
  8237 #  else
       
  8238 #    define SQLITE_MUTEX_NOOP
       
  8239 #  endif
       
  8240 #endif
       
  8241 
       
  8242 #ifdef SQLITE_MUTEX_OMIT
       
  8243 /*
       
  8244 ** If this is a no-op implementation, implement everything as macros.
       
  8245 */
       
  8246 #define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
       
  8247 #define sqlite3_mutex_free(X)
       
  8248 #define sqlite3_mutex_enter(X)
       
  8249 #define sqlite3_mutex_try(X)      SQLITE_OK
       
  8250 #define sqlite3_mutex_leave(X)
       
  8251 #define sqlite3_mutex_held(X)     1
       
  8252 #define sqlite3_mutex_notheld(X)  1
       
  8253 #define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
       
  8254 #define sqlite3MutexInit()        SQLITE_OK
       
  8255 #define sqlite3MutexEnd()
       
  8256 #endif /* defined(SQLITE_MUTEX_OMIT) */
       
  8257 
       
  8258 /************** End of mutex.h ***********************************************/
       
  8259 /************** Continuing where we left off in sqliteInt.h ******************/
       
  8260 
       
  8261 
       
  8262 /*
       
  8263 ** Each database file to be accessed by the system is an instance
       
  8264 ** of the following structure.  There are normally two of these structures
       
  8265 ** in the sqlite.aDb[] array.  aDb[0] is the main database file and
       
  8266 ** aDb[1] is the database file used to hold temporary tables.  Additional
       
  8267 ** databases may be attached.
       
  8268 */
       
  8269 struct Db {
       
  8270   char *zName;         /* Name of this database */
       
  8271   Btree *pBt;          /* The B*Tree structure for this database file */
       
  8272   u8 inTrans;          /* 0: not writable.  1: Transaction.  2: Checkpoint */
       
  8273   u8 safety_level;     /* How aggressive at syncing data to disk */
       
  8274   Schema *pSchema;     /* Pointer to database schema (possibly shared) */
       
  8275 };
       
  8276 
       
  8277 /*
       
  8278 ** An instance of the following structure stores a database schema.
       
  8279 **
       
  8280 ** If there are no virtual tables configured in this schema, the
       
  8281 ** Schema.db variable is set to NULL. After the first virtual table
       
  8282 ** has been added, it is set to point to the database connection 
       
  8283 ** used to create the connection. Once a virtual table has been
       
  8284 ** added to the Schema structure and the Schema.db variable populated, 
       
  8285 ** only that database connection may use the Schema to prepare 
       
  8286 ** statements.
       
  8287 */
       
  8288 struct Schema {
       
  8289   int schema_cookie;   /* Database schema version number for this file */
       
  8290   Hash tblHash;        /* All tables indexed by name */
       
  8291   Hash idxHash;        /* All (named) indices indexed by name */
       
  8292   Hash trigHash;       /* All triggers indexed by name */
       
  8293   Hash fkeyHash;       /* All foreign keys by referenced table name */
       
  8294   Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
       
  8295   u8 file_format;      /* Schema format version for this file */
       
  8296   u8 enc;              /* Text encoding used by this database */
       
  8297   u16 flags;           /* Flags associated with this schema */
       
  8298   int cache_size;      /* Number of pages to use in the cache */
       
  8299 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
  8300   sqlite3 *db;         /* "Owner" connection. See comment above */
       
  8301 #endif
       
  8302 };
       
  8303 
       
  8304 /*
       
  8305 ** These macros can be used to test, set, or clear bits in the 
       
  8306 ** Db.flags field.
       
  8307 */
       
  8308 #define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
       
  8309 #define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
       
  8310 #define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
       
  8311 #define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
       
  8312 
       
  8313 /*
       
  8314 ** Allowed values for the DB.flags field.
       
  8315 **
       
  8316 ** The DB_SchemaLoaded flag is set after the database schema has been
       
  8317 ** read into internal hash tables.
       
  8318 **
       
  8319 ** DB_UnresetViews means that one or more views have column names that
       
  8320 ** have been filled out.  If the schema changes, these column names might
       
  8321 ** changes and so the view will need to be reset.
       
  8322 */
       
  8323 #define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
       
  8324 #define DB_UnresetViews    0x0002  /* Some views have defined column names */
       
  8325 #define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
       
  8326 
       
  8327 /*
       
  8328 ** The number of different kinds of things that can be limited
       
  8329 ** using the sqlite3_limit() interface.
       
  8330 */
       
  8331 #define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1)
       
  8332 
       
  8333 /*
       
  8334 ** Lookaside malloc is a set of fixed-size buffers that can be used
       
  8335 ** to satisfy small transient memory allocation requests for objects
       
  8336 ** associated with a particular database connection.  The use of
       
  8337 ** lookaside malloc provides a significant performance enhancement
       
  8338 ** (approx 10%) by avoiding numerous malloc/free requests while parsing
       
  8339 ** SQL statements.
       
  8340 **
       
  8341 ** The Lookaside structure holds configuration information about the
       
  8342 ** lookaside malloc subsystem.  Each available memory allocation in
       
  8343 ** the lookaside subsystem is stored on a linked list of LookasideSlot
       
  8344 ** objects.
       
  8345 **
       
  8346 ** Lookaside allocations are only allowed for objects that are associated
       
  8347 ** with a particular database connection.  Hence, schema information cannot
       
  8348 ** be stored in lookaside because in shared cache mode the schema information
       
  8349 ** is shared by multiple database connections.  Therefore, while parsing
       
  8350 ** schema information, the Lookaside.bEnabled flag is cleared so that
       
  8351 ** lookaside allocations are not used to construct the schema objects.
       
  8352 */
       
  8353 struct Lookaside {
       
  8354   u16 sz;                 /* Size of each buffer in bytes */
       
  8355   u8 bEnabled;            /* False to disable new lookaside allocations */
       
  8356   u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
       
  8357   int nOut;               /* Number of buffers currently checked out */
       
  8358   int mxOut;              /* Highwater mark for nOut */
       
  8359   LookasideSlot *pFree;   /* List of available buffers */
       
  8360   void *pStart;           /* First byte of available memory space */
       
  8361   void *pEnd;             /* First byte past end of available space */
       
  8362 };
       
  8363 struct LookasideSlot {
       
  8364   LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
       
  8365 };
       
  8366 
       
  8367 /*
       
  8368 ** A hash table for function definitions.
       
  8369 **
       
  8370 ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
       
  8371 ** Collisions are on the FuncDef.pHash chain.
       
  8372 */
       
  8373 struct FuncDefHash {
       
  8374   FuncDef *a[23];       /* Hash table for functions */
       
  8375 };
       
  8376 
       
  8377 /*
       
  8378 ** Each database is an instance of the following structure.
       
  8379 **
       
  8380 ** The sqlite.lastRowid records the last insert rowid generated by an
       
  8381 ** insert statement.  Inserts on views do not affect its value.  Each
       
  8382 ** trigger has its own context, so that lastRowid can be updated inside
       
  8383 ** triggers as usual.  The previous value will be restored once the trigger
       
  8384 ** exits.  Upon entering a before or instead of trigger, lastRowid is no
       
  8385 ** longer (since after version 2.8.12) reset to -1.
       
  8386 **
       
  8387 ** The sqlite.nChange does not count changes within triggers and keeps no
       
  8388 ** context.  It is reset at start of sqlite3_exec.
       
  8389 ** The sqlite.lsChange represents the number of changes made by the last
       
  8390 ** insert, update, or delete statement.  It remains constant throughout the
       
  8391 ** length of a statement and is then updated by OP_SetCounts.  It keeps a
       
  8392 ** context stack just like lastRowid so that the count of changes
       
  8393 ** within a trigger is not seen outside the trigger.  Changes to views do not
       
  8394 ** affect the value of lsChange.
       
  8395 ** The sqlite.csChange keeps track of the number of current changes (since
       
  8396 ** the last statement) and is used to update sqlite_lsChange.
       
  8397 **
       
  8398 ** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16
       
  8399 ** store the most recent error code and, if applicable, string. The
       
  8400 ** internal function sqlite3Error() is used to set these variables
       
  8401 ** consistently.
       
  8402 */
       
  8403 struct sqlite3 {
       
  8404   sqlite3_vfs *pVfs;            /* OS Interface */
       
  8405   int nDb;                      /* Number of backends currently in use */
       
  8406   Db *aDb;                      /* All backends */
       
  8407   int flags;                    /* Miscellaneous flags. See below */
       
  8408   int openFlags;                /* Flags passed to sqlite3_vfs.xOpen() */
       
  8409   int errCode;                  /* Most recent error code (SQLITE_*) */
       
  8410   int errMask;                  /* & result codes with this before returning */
       
  8411   u8 autoCommit;                /* The auto-commit flag. */
       
  8412   u8 temp_store;                /* 1: file 2: memory 0: default */
       
  8413   u8 mallocFailed;              /* True if we have seen a malloc failure */
       
  8414   u8 dfltLockMode;              /* Default locking-mode for attached dbs */
       
  8415   u8 dfltJournalMode;           /* Default journal mode for attached dbs */
       
  8416   signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
       
  8417   int nextPagesize;             /* Pagesize after VACUUM if >0 */
       
  8418   int nTable;                   /* Number of tables in the database */
       
  8419   CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
       
  8420   i64 lastRowid;                /* ROWID of most recent insert (see above) */
       
  8421   u32 magic;                    /* Magic number for detect library misuse */
       
  8422   int nChange;                  /* Value returned by sqlite3_changes() */
       
  8423   int nTotalChange;             /* Value returned by sqlite3_total_changes() */
       
  8424   sqlite3_mutex *mutex;         /* Connection mutex */
       
  8425   int aLimit[SQLITE_N_LIMIT];   /* Limits */
       
  8426   struct sqlite3InitInfo {      /* Information used during initialization */
       
  8427     int iDb;                    /* When back is being initialized */
       
  8428     int newTnum;                /* Rootpage of table being initialized */
       
  8429     u8 busy;                    /* TRUE if currently initializing */
       
  8430     u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
       
  8431   } init;
       
  8432   int nExtension;               /* Number of loaded extensions */
       
  8433   void **aExtension;            /* Array of shared library handles */
       
  8434   struct Vdbe *pVdbe;           /* List of active virtual machines */
       
  8435   int activeVdbeCnt;            /* Number of VDBEs currently executing */
       
  8436   int writeVdbeCnt;             /* Number of active VDBEs that are writing */
       
  8437   void (*xTrace)(void*,const char*);        /* Trace function */
       
  8438   void *pTraceArg;                          /* Argument to the trace function */
       
  8439   void (*xProfile)(void*,const char*,u64);  /* Profiling function */
       
  8440   void *pProfileArg;                        /* Argument to profile function */
       
  8441   void *pCommitArg;                 /* Argument to xCommitCallback() */   
       
  8442   int (*xCommitCallback)(void*);    /* Invoked at every commit. */
       
  8443   void *pRollbackArg;               /* Argument to xRollbackCallback() */   
       
  8444   void (*xRollbackCallback)(void*); /* Invoked at every commit. */
       
  8445   void *pUpdateArg;
       
  8446   void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
       
  8447   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
       
  8448   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
       
  8449   void *pCollNeededArg;
       
  8450   sqlite3_value *pErr;          /* Most recent error message */
       
  8451   char *zErrMsg;                /* Most recent error message (UTF-8 encoded) */
       
  8452   char *zErrMsg16;              /* Most recent error message (UTF-16 encoded) */
       
  8453   union {
       
  8454     volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
       
  8455     double notUsed1;            /* Spacer */
       
  8456   } u1;
       
  8457   Lookaside lookaside;          /* Lookaside malloc configuration */
       
  8458 #ifndef SQLITE_OMIT_AUTHORIZATION
       
  8459   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
       
  8460                                 /* Access authorization function */
       
  8461   void *pAuthArg;               /* 1st argument to the access auth function */
       
  8462 #endif
       
  8463 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
       
  8464   int (*xProgress)(void *);     /* The progress callback */
       
  8465   void *pProgressArg;           /* Argument to the progress callback */
       
  8466   int nProgressOps;             /* Number of opcodes for progress callback */
       
  8467 #endif
       
  8468 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
  8469   Hash aModule;                 /* populated by sqlite3_create_module() */
       
  8470   Table *pVTab;                 /* vtab with active Connect/Create method */
       
  8471   VTable **aVTrans;             /* Virtual tables with open transactions */
       
  8472   int nVTrans;                  /* Allocated size of aVTrans */
       
  8473   VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
       
  8474 #endif
       
  8475   FuncDefHash aFunc;            /* Hash table of connection functions */
       
  8476   Hash aCollSeq;                /* All collating sequences */
       
  8477   BusyHandler busyHandler;      /* Busy callback */
       
  8478   int busyTimeout;              /* Busy handler timeout, in msec */
       
  8479   Db aDbStatic[2];              /* Static space for the 2 default backends */
       
  8480   Savepoint *pSavepoint;        /* List of active savepoints */
       
  8481   int nSavepoint;               /* Number of non-transaction savepoints */
       
  8482   int nStatement;               /* Number of nested statement-transactions  */
       
  8483   u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
       
  8484   i64 nDeferredCons;            /* Net deferred constraints this transaction. */
       
  8485 
       
  8486 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
       
  8487   /* The following variables are all protected by the STATIC_MASTER 
       
  8488   ** mutex, not by sqlite3.mutex. They are used by code in notify.c. 
       
  8489   **
       
  8490   ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
       
  8491   ** unlock so that it can proceed.
       
  8492   **
       
  8493   ** When X.pBlockingConnection==Y, that means that something that X tried
       
  8494   ** tried to do recently failed with an SQLITE_LOCKED error due to locks
       
  8495   ** held by Y.
       
  8496   */
       
  8497   sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
       
  8498   sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */
       
  8499   void *pUnlockArg;                     /* Argument to xUnlockNotify */
       
  8500   void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */
       
  8501   sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */
       
  8502 #endif
       
  8503 };
       
  8504 
       
  8505 /*
       
  8506 ** A macro to discover the encoding of a database.
       
  8507 */
       
  8508 #define ENC(db) ((db)->aDb[0].pSchema->enc)
       
  8509 
       
  8510 /*
       
  8511 ** Possible values for the sqlite.flags and or Db.flags fields.
       
  8512 **
       
  8513 ** On sqlite.flags, the SQLITE_InTrans value means that we have
       
  8514 ** executed a BEGIN.  On Db.flags, SQLITE_InTrans means a statement
       
  8515 ** transaction is active on that particular database file.
       
  8516 */
       
  8517 #define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
       
  8518 #define SQLITE_InTrans        0x00000008  /* True if in a transaction */
       
  8519 #define SQLITE_InternChanges  0x00000010  /* Uncommitted Hash table changes */
       
  8520 #define SQLITE_FullColNames   0x00000020  /* Show full column names on SELECT */
       
  8521 #define SQLITE_ShortColNames  0x00000040  /* Show short columns names */
       
  8522 #define SQLITE_CountRows      0x00000080  /* Count rows changed by INSERT, */
       
  8523                                           /*   DELETE, or UPDATE and return */
       
  8524                                           /*   the count using a callback. */
       
  8525 #define SQLITE_NullCallback   0x00000100  /* Invoke the callback once if the */
       
  8526                                           /*   result set is empty */
       
  8527 #define SQLITE_SqlTrace       0x00000200  /* Debug print SQL as it executes */
       
  8528 #define SQLITE_VdbeListing    0x00000400  /* Debug listings of VDBE programs */
       
  8529 #define SQLITE_WriteSchema    0x00000800  /* OK to update SQLITE_MASTER */
       
  8530 #define SQLITE_NoReadlock     0x00001000  /* Readlocks are omitted when 
       
  8531                                           ** accessing read-only databases */
       
  8532 #define SQLITE_IgnoreChecks   0x00002000  /* Do not enforce check constraints */
       
  8533 #define SQLITE_ReadUncommitted 0x00004000 /* For shared-cache mode */
       
  8534 #define SQLITE_LegacyFileFmt  0x00008000  /* Create new databases in format 1 */
       
  8535 #define SQLITE_FullFSync      0x00010000  /* Use full fsync on the backend */
       
  8536 #define SQLITE_LoadExtension  0x00020000  /* Enable load_extension */
       
  8537 
       
  8538 #define SQLITE_RecoveryMode   0x00040000  /* Ignore schema errors */
       
  8539 #define SQLITE_ReverseOrder   0x00100000  /* Reverse unordered SELECTs */
       
  8540 #define SQLITE_RecTriggers    0x00200000  /* Enable recursive triggers */
       
  8541 #define SQLITE_ForeignKeys    0x00400000  /* Enforce foreign key constraints  */
       
  8542 
       
  8543 /*
       
  8544 ** Possible values for the sqlite.magic field.
       
  8545 ** The numbers are obtained at random and have no special meaning, other
       
  8546 ** than being distinct from one another.
       
  8547 */
       
  8548 #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
       
  8549 #define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
       
  8550 #define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
       
  8551 #define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
       
  8552 #define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
       
  8553 
       
  8554 /*
       
  8555 ** Each SQL function is defined by an instance of the following
       
  8556 ** structure.  A pointer to this structure is stored in the sqlite.aFunc
       
  8557 ** hash table.  When multiple functions have the same name, the hash table
       
  8558 ** points to a linked list of these structures.
       
  8559 */
       
  8560 struct FuncDef {
       
  8561   i16 nArg;            /* Number of arguments.  -1 means unlimited */
       
  8562   u8 iPrefEnc;         /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */
       
  8563   u8 flags;            /* Some combination of SQLITE_FUNC_* */
       
  8564   void *pUserData;     /* User data parameter */
       
  8565   FuncDef *pNext;      /* Next function with same name */
       
  8566   void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
       
  8567   void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
       
  8568   void (*xFinalize)(sqlite3_context*);                /* Aggregate finalizer */
       
  8569   char *zName;         /* SQL name of the function. */
       
  8570   FuncDef *pHash;      /* Next with a different name but the same hash */
       
  8571 };
       
  8572 
       
  8573 /*
       
  8574 ** Possible values for FuncDef.flags
       
  8575 */
       
  8576 #define SQLITE_FUNC_LIKE     0x01 /* Candidate for the LIKE optimization */
       
  8577 #define SQLITE_FUNC_CASE     0x02 /* Case-sensitive LIKE-type function */
       
  8578 #define SQLITE_FUNC_EPHEM    0x04 /* Ephemeral.  Delete with VDBE */
       
  8579 #define SQLITE_FUNC_NEEDCOLL 0x08 /* sqlite3GetFuncCollSeq() might be called */
       
  8580 #define SQLITE_FUNC_PRIVATE  0x10 /* Allowed for internal use only */
       
  8581 #define SQLITE_FUNC_COUNT    0x20 /* Built-in count(*) aggregate */
       
  8582 
       
  8583 /*
       
  8584 ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
       
  8585 ** used to create the initializers for the FuncDef structures.
       
  8586 **
       
  8587 **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
       
  8588 **     Used to create a scalar function definition of a function zName 
       
  8589 **     implemented by C function xFunc that accepts nArg arguments. The
       
  8590 **     value passed as iArg is cast to a (void*) and made available
       
  8591 **     as the user-data (sqlite3_user_data()) for the function. If 
       
  8592 **     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
       
  8593 **
       
  8594 **   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
       
  8595 **     Used to create an aggregate function definition implemented by
       
  8596 **     the C functions xStep and xFinal. The first four parameters
       
  8597 **     are interpreted in the same way as the first 4 parameters to
       
  8598 **     FUNCTION().
       
  8599 **
       
  8600 **   LIKEFUNC(zName, nArg, pArg, flags)
       
  8601 **     Used to create a scalar function definition of a function zName 
       
  8602 **     that accepts nArg arguments and is implemented by a call to C 
       
  8603 **     function likeFunc. Argument pArg is cast to a (void *) and made
       
  8604 **     available as the function user-data (sqlite3_user_data()). The
       
  8605 **     FuncDef.flags variable is set to the value passed as the flags
       
  8606 **     parameter.
       
  8607 */
       
  8608 #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
       
  8609   {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
       
  8610    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0}
       
  8611 #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
       
  8612   {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
       
  8613    pArg, 0, xFunc, 0, 0, #zName, 0}
       
  8614 #define LIKEFUNC(zName, nArg, arg, flags) \
       
  8615   {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0}
       
  8616 #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
       
  8617   {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \
       
  8618    SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0}
       
  8619 
       
  8620 /*
       
  8621 ** All current savepoints are stored in a linked list starting at
       
  8622 ** sqlite3.pSavepoint. The first element in the list is the most recently
       
  8623 ** opened savepoint. Savepoints are added to the list by the vdbe
       
  8624 ** OP_Savepoint instruction.
       
  8625 */
       
  8626 struct Savepoint {
       
  8627   char *zName;                        /* Savepoint name (nul-terminated) */
       
  8628   i64 nDeferredCons;                  /* Number of deferred fk violations */
       
  8629   Savepoint *pNext;                   /* Parent savepoint (if any) */
       
  8630 };
       
  8631 
       
  8632 /*
       
  8633 ** The following are used as the second parameter to sqlite3Savepoint(),
       
  8634 ** and as the P1 argument to the OP_Savepoint instruction.
       
  8635 */
       
  8636 #define SAVEPOINT_BEGIN      0
       
  8637 #define SAVEPOINT_RELEASE    1
       
  8638 #define SAVEPOINT_ROLLBACK   2
       
  8639 
       
  8640 
       
  8641 /*
       
  8642 ** Each SQLite module (virtual table definition) is defined by an
       
  8643 ** instance of the following structure, stored in the sqlite3.aModule
       
  8644 ** hash table.
       
  8645 */
       
  8646 struct Module {
       
  8647   const sqlite3_module *pModule;       /* Callback pointers */
       
  8648   const char *zName;                   /* Name passed to create_module() */
       
  8649   void *pAux;                          /* pAux passed to create_module() */
       
  8650   void (*xDestroy)(void *);            /* Module destructor function */
       
  8651 };
       
  8652 
       
  8653 /*
       
  8654 ** information about each column of an SQL table is held in an instance
       
  8655 ** of this structure.
       
  8656 */
       
  8657 struct Column {
       
  8658   char *zName;     /* Name of this column */
       
  8659   Expr *pDflt;     /* Default value of this column */
       
  8660   char *zDflt;     /* Original text of the default value */
       
  8661   char *zType;     /* Data type for this column */
       
  8662   char *zColl;     /* Collating sequence.  If NULL, use the default */
       
  8663   u8 notNull;      /* True if there is a NOT NULL constraint */
       
  8664   u8 isPrimKey;    /* True if this column is part of the PRIMARY KEY */
       
  8665   char affinity;   /* One of the SQLITE_AFF_... values */
       
  8666 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
  8667   u8 isHidden;     /* True if this column is 'hidden' */
       
  8668 #endif
       
  8669 };
       
  8670 
       
  8671 /*
       
  8672 ** A "Collating Sequence" is defined by an instance of the following
       
  8673 ** structure. Conceptually, a collating sequence consists of a name and
       
  8674 ** a comparison routine that defines the order of that sequence.
       
  8675 **
       
  8676 ** There may two separate implementations of the collation function, one
       
  8677 ** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
       
  8678 ** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine
       
  8679 ** native byte order. When a collation sequence is invoked, SQLite selects
       
  8680 ** the version that will require the least expensive encoding
       
  8681 ** translations, if any.
       
  8682 **
       
  8683 ** The CollSeq.pUser member variable is an extra parameter that passed in
       
  8684 ** as the first argument to the UTF-8 comparison function, xCmp.
       
  8685 ** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function,
       
  8686 ** xCmp16.
       
  8687 **
       
  8688 ** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
       
  8689 ** collating sequence is undefined.  Indices built on an undefined
       
  8690 ** collating sequence may not be read or written.
       
  8691 */
       
  8692 struct CollSeq {
       
  8693   char *zName;          /* Name of the collating sequence, UTF-8 encoded */
       
  8694   u8 enc;               /* Text encoding handled by xCmp() */
       
  8695   u8 type;              /* One of the SQLITE_COLL_... values below */
       
  8696   void *pUser;          /* First argument to xCmp() */
       
  8697   int (*xCmp)(void*,int, const void*, int, const void*);
       
  8698   void (*xDel)(void*);  /* Destructor for pUser */
       
  8699 };
       
  8700 
       
  8701 /*
       
  8702 ** Allowed values of CollSeq.type:
       
  8703 */
       
  8704 #define SQLITE_COLL_BINARY  1  /* The default memcmp() collating sequence */
       
  8705 #define SQLITE_COLL_NOCASE  2  /* The built-in NOCASE collating sequence */
       
  8706 #define SQLITE_COLL_REVERSE 3  /* The built-in REVERSE collating sequence */
       
  8707 #define SQLITE_COLL_USER    0  /* Any other user-defined collating sequence */
       
  8708 
       
  8709 /*
       
  8710 ** A sort order can be either ASC or DESC.
       
  8711 */
       
  8712 #define SQLITE_SO_ASC       0  /* Sort in ascending order */
       
  8713 #define SQLITE_SO_DESC      1  /* Sort in ascending order */
       
  8714 
       
  8715 /*
       
  8716 ** Column affinity types.
       
  8717 **
       
  8718 ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
       
  8719 ** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
       
  8720 ** the speed a little by numbering the values consecutively.  
       
  8721 **
       
  8722 ** But rather than start with 0 or 1, we begin with 'a'.  That way,
       
  8723 ** when multiple affinity types are concatenated into a string and
       
  8724 ** used as the P4 operand, they will be more readable.
       
  8725 **
       
  8726 ** Note also that the numeric types are grouped together so that testing
       
  8727 ** for a numeric type is a single comparison.
       
  8728 */
       
  8729 #define SQLITE_AFF_TEXT     'a'
       
  8730 #define SQLITE_AFF_NONE     'b'
       
  8731 #define SQLITE_AFF_NUMERIC  'c'
       
  8732 #define SQLITE_AFF_INTEGER  'd'
       
  8733 #define SQLITE_AFF_REAL     'e'
       
  8734 
       
  8735 #define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
       
  8736 
       
  8737 /*
       
  8738 ** The SQLITE_AFF_MASK values masks off the significant bits of an
       
  8739 ** affinity value. 
       
  8740 */
       
  8741 #define SQLITE_AFF_MASK     0x67
       
  8742 
       
  8743 /*
       
  8744 ** Additional bit values that can be ORed with an affinity without
       
  8745 ** changing the affinity.
       
  8746 */
       
  8747 #define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
       
  8748 #define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
       
  8749 #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
       
  8750 
       
  8751 /*
       
  8752 ** An object of this type is created for each virtual table present in
       
  8753 ** the database schema. 
       
  8754 **
       
  8755 ** If the database schema is shared, then there is one instance of this
       
  8756 ** structure for each database connection (sqlite3*) that uses the shared
       
  8757 ** schema. This is because each database connection requires its own unique
       
  8758 ** instance of the sqlite3_vtab* handle used to access the virtual table 
       
  8759 ** implementation. sqlite3_vtab* handles can not be shared between 
       
  8760 ** database connections, even when the rest of the in-memory database 
       
  8761 ** schema is shared, as the implementation often stores the database
       
  8762 ** connection handle passed to it via the xConnect() or xCreate() method
       
  8763 ** during initialization internally. This database connection handle may
       
  8764 ** then used by the virtual table implementation to access real tables 
       
  8765 ** within the database. So that they appear as part of the callers 
       
  8766 ** transaction, these accesses need to be made via the same database 
       
  8767 ** connection as that used to execute SQL operations on the virtual table.
       
  8768 **
       
  8769 ** All VTable objects that correspond to a single table in a shared
       
  8770 ** database schema are initially stored in a linked-list pointed to by
       
  8771 ** the Table.pVTable member variable of the corresponding Table object.
       
  8772 ** When an sqlite3_prepare() operation is required to access the virtual
       
  8773 ** table, it searches the list for the VTable that corresponds to the
       
  8774 ** database connection doing the preparing so as to use the correct
       
  8775 ** sqlite3_vtab* handle in the compiled query.
       
  8776 **
       
  8777 ** When an in-memory Table object is deleted (for example when the
       
  8778 ** schema is being reloaded for some reason), the VTable objects are not 
       
  8779 ** deleted and the sqlite3_vtab* handles are not xDisconnect()ed 
       
  8780 ** immediately. Instead, they are moved from the Table.pVTable list to
       
  8781 ** another linked list headed by the sqlite3.pDisconnect member of the
       
  8782 ** corresponding sqlite3 structure. They are then deleted/xDisconnected 
       
  8783 ** next time a statement is prepared using said sqlite3*. This is done
       
  8784 ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
       
  8785 ** Refer to comments above function sqlite3VtabUnlockList() for an
       
  8786 ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
       
  8787 ** list without holding the corresponding sqlite3.mutex mutex.
       
  8788 **
       
  8789 ** The memory for objects of this type is always allocated by 
       
  8790 ** sqlite3DbMalloc(), using the connection handle stored in VTable.db as 
       
  8791 ** the first argument.
       
  8792 */
       
  8793 struct VTable {
       
  8794   sqlite3 *db;              /* Database connection associated with this table */
       
  8795   Module *pMod;             /* Pointer to module implementation */
       
  8796   sqlite3_vtab *pVtab;      /* Pointer to vtab instance */
       
  8797   int nRef;                 /* Number of pointers to this structure */
       
  8798   VTable *pNext;            /* Next in linked list (see above) */
       
  8799 };
       
  8800 
       
  8801 /*
       
  8802 ** Each SQL table is represented in memory by an instance of the
       
  8803 ** following structure.
       
  8804 **
       
  8805 ** Table.zName is the name of the table.  The case of the original
       
  8806 ** CREATE TABLE statement is stored, but case is not significant for
       
  8807 ** comparisons.
       
  8808 **
       
  8809 ** Table.nCol is the number of columns in this table.  Table.aCol is a
       
  8810 ** pointer to an array of Column structures, one for each column.
       
  8811 **
       
  8812 ** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
       
  8813 ** the column that is that key.   Otherwise Table.iPKey is negative.  Note
       
  8814 ** that the datatype of the PRIMARY KEY must be INTEGER for this field to
       
  8815 ** be set.  An INTEGER PRIMARY KEY is used as the rowid for each row of
       
  8816 ** the table.  If a table has no INTEGER PRIMARY KEY, then a random rowid
       
  8817 ** is generated for each row of the table.  TF_HasPrimaryKey is set if
       
  8818 ** the table has any PRIMARY KEY, INTEGER or otherwise.
       
  8819 **
       
  8820 ** Table.tnum is the page number for the root BTree page of the table in the
       
  8821 ** database file.  If Table.iDb is the index of the database table backend
       
  8822 ** in sqlite.aDb[].  0 is for the main database and 1 is for the file that
       
  8823 ** holds temporary tables and indices.  If TF_Ephemeral is set
       
  8824 ** then the table is stored in a file that is automatically deleted
       
  8825 ** when the VDBE cursor to the table is closed.  In this case Table.tnum 
       
  8826 ** refers VDBE cursor number that holds the table open, not to the root
       
  8827 ** page number.  Transient tables are used to hold the results of a
       
  8828 ** sub-query that appears instead of a real table name in the FROM clause 
       
  8829 ** of a SELECT statement.
       
  8830 */
       
  8831 struct Table {
       
  8832   sqlite3 *dbMem;      /* DB connection used for lookaside allocations. */
       
  8833   char *zName;         /* Name of the table or view */
       
  8834   int iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
       
  8835   int nCol;            /* Number of columns in this table */
       
  8836   Column *aCol;        /* Information about each column */
       
  8837   Index *pIndex;       /* List of SQL indexes on this table. */
       
  8838   int tnum;            /* Root BTree node for this table (see note above) */
       
  8839   Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
       
  8840   u16 nRef;            /* Number of pointers to this Table */
       
  8841   u8 tabFlags;         /* Mask of TF_* values */
       
  8842   u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
       
  8843   FKey *pFKey;         /* Linked list of all foreign keys in this table */
       
  8844   char *zColAff;       /* String defining the affinity of each column */
       
  8845 #ifndef SQLITE_OMIT_CHECK
       
  8846   Expr *pCheck;        /* The AND of all CHECK constraints */
       
  8847 #endif
       
  8848 #ifndef SQLITE_OMIT_ALTERTABLE
       
  8849   int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
       
  8850 #endif
       
  8851 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
  8852   VTable *pVTable;     /* List of VTable objects. */
       
  8853   int nModuleArg;      /* Number of arguments to the module */
       
  8854   char **azModuleArg;  /* Text of all module args. [0] is module name */
       
  8855 #endif
       
  8856   Trigger *pTrigger;   /* List of triggers stored in pSchema */
       
  8857   Schema *pSchema;     /* Schema that contains this table */
       
  8858   Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
       
  8859 };
       
  8860 
       
  8861 /*
       
  8862 ** Allowed values for Tabe.tabFlags.
       
  8863 */
       
  8864 #define TF_Readonly        0x01    /* Read-only system table */
       
  8865 #define TF_Ephemeral       0x02    /* An ephemeral table */
       
  8866 #define TF_HasPrimaryKey   0x04    /* Table has a primary key */
       
  8867 #define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
       
  8868 #define TF_Virtual         0x10    /* Is a virtual table */
       
  8869 #define TF_NeedMetadata    0x20    /* aCol[].zType and aCol[].pColl missing */
       
  8870 
       
  8871 
       
  8872 
       
  8873 /*
       
  8874 ** Test to see whether or not a table is a virtual table.  This is
       
  8875 ** done as a macro so that it will be optimized out when virtual
       
  8876 ** table support is omitted from the build.
       
  8877 */
       
  8878 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
  8879 #  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
       
  8880 #  define IsHiddenColumn(X) ((X)->isHidden)
       
  8881 #else
       
  8882 #  define IsVirtual(X)      0
       
  8883 #  define IsHiddenColumn(X) 0
       
  8884 #endif
       
  8885 
       
  8886 /*
       
  8887 ** Each foreign key constraint is an instance of the following structure.
       
  8888 **
       
  8889 ** A foreign key is associated with two tables.  The "from" table is
       
  8890 ** the table that contains the REFERENCES clause that creates the foreign
       
  8891 ** key.  The "to" table is the table that is named in the REFERENCES clause.
       
  8892 ** Consider this example:
       
  8893 **
       
  8894 **     CREATE TABLE ex1(
       
  8895 **       a INTEGER PRIMARY KEY,
       
  8896 **       b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
       
  8897 **     );
       
  8898 **
       
  8899 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
       
  8900 **
       
  8901 ** Each REFERENCES clause generates an instance of the following structure
       
  8902 ** which is attached to the from-table.  The to-table need not exist when
       
  8903 ** the from-table is created.  The existence of the to-table is not checked.
       
  8904 */
       
  8905 struct FKey {
       
  8906   Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
       
  8907   FKey *pNextFrom;  /* Next foreign key in pFrom */
       
  8908   char *zTo;        /* Name of table that the key points to (aka: Parent) */
       
  8909   FKey *pNextTo;    /* Next foreign key on table named zTo */
       
  8910   FKey *pPrevTo;    /* Previous foreign key on table named zTo */
       
  8911   int nCol;         /* Number of columns in this key */
       
  8912   /* EV: R-30323-21917 */
       
  8913   u8 isDeferred;    /* True if constraint checking is deferred till COMMIT */
       
  8914   u8 aAction[2];          /* ON DELETE and ON UPDATE actions, respectively */
       
  8915   Trigger *apTrigger[2];  /* Triggers for aAction[] actions */
       
  8916   struct sColMap {  /* Mapping of columns in pFrom to columns in zTo */
       
  8917     int iFrom;         /* Index of column in pFrom */
       
  8918     char *zCol;        /* Name of column in zTo.  If 0 use PRIMARY KEY */
       
  8919   } aCol[1];        /* One entry for each of nCol column s */
       
  8920 };
       
  8921 
       
  8922 /*
       
  8923 ** SQLite supports many different ways to resolve a constraint
       
  8924 ** error.  ROLLBACK processing means that a constraint violation
       
  8925 ** causes the operation in process to fail and for the current transaction
       
  8926 ** to be rolled back.  ABORT processing means the operation in process
       
  8927 ** fails and any prior changes from that one operation are backed out,
       
  8928 ** but the transaction is not rolled back.  FAIL processing means that
       
  8929 ** the operation in progress stops and returns an error code.  But prior
       
  8930 ** changes due to the same operation are not backed out and no rollback
       
  8931 ** occurs.  IGNORE means that the particular row that caused the constraint
       
  8932 ** error is not inserted or updated.  Processing continues and no error
       
  8933 ** is returned.  REPLACE means that preexisting database rows that caused
       
  8934 ** a UNIQUE constraint violation are removed so that the new insert or
       
  8935 ** update can proceed.  Processing continues and no error is reported.
       
  8936 **
       
  8937 ** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
       
  8938 ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
       
  8939 ** same as ROLLBACK for DEFERRED keys.  SETNULL means that the foreign
       
  8940 ** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
       
  8941 ** referenced table row is propagated into the row that holds the
       
  8942 ** foreign key.
       
  8943 ** 
       
  8944 ** The following symbolic values are used to record which type
       
  8945 ** of action to take.
       
  8946 */
       
  8947 #define OE_None     0   /* There is no constraint to check */
       
  8948 #define OE_Rollback 1   /* Fail the operation and rollback the transaction */
       
  8949 #define OE_Abort    2   /* Back out changes but do no rollback transaction */
       
  8950 #define OE_Fail     3   /* Stop the operation but leave all prior changes */
       
  8951 #define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
       
  8952 #define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
       
  8953 
       
  8954 #define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
       
  8955 #define OE_SetNull  7   /* Set the foreign key value to NULL */
       
  8956 #define OE_SetDflt  8   /* Set the foreign key value to its default */
       
  8957 #define OE_Cascade  9   /* Cascade the changes */
       
  8958 
       
  8959 #define OE_Default  99  /* Do whatever the default action is */
       
  8960 
       
  8961 
       
  8962 /*
       
  8963 ** An instance of the following structure is passed as the first
       
  8964 ** argument to sqlite3VdbeKeyCompare and is used to control the 
       
  8965 ** comparison of the two index keys.
       
  8966 */
       
  8967 struct KeyInfo {
       
  8968   sqlite3 *db;        /* The database connection */
       
  8969   u8 enc;             /* Text encoding - one of the TEXT_Utf* values */
       
  8970   u16 nField;         /* Number of entries in aColl[] */
       
  8971   u8 *aSortOrder;     /* If defined an aSortOrder[i] is true, sort DESC */
       
  8972   CollSeq *aColl[1];  /* Collating sequence for each term of the key */
       
  8973 };
       
  8974 
       
  8975 /*
       
  8976 ** An instance of the following structure holds information about a
       
  8977 ** single index record that has already been parsed out into individual
       
  8978 ** values.
       
  8979 **
       
  8980 ** A record is an object that contains one or more fields of data.
       
  8981 ** Records are used to store the content of a table row and to store
       
  8982 ** the key of an index.  A blob encoding of a record is created by
       
  8983 ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
       
  8984 ** OP_Column opcode.
       
  8985 **
       
  8986 ** This structure holds a record that has already been disassembled
       
  8987 ** into its constituent fields.
       
  8988 */
       
  8989 struct UnpackedRecord {
       
  8990   KeyInfo *pKeyInfo;  /* Collation and sort-order information */
       
  8991   u16 nField;         /* Number of entries in apMem[] */
       
  8992   u16 flags;          /* Boolean settings.  UNPACKED_... below */
       
  8993   i64 rowid;          /* Used by UNPACKED_PREFIX_SEARCH */
       
  8994   Mem *aMem;          /* Values */
       
  8995 };
       
  8996 
       
  8997 /*
       
  8998 ** Allowed values of UnpackedRecord.flags
       
  8999 */
       
  9000 #define UNPACKED_NEED_FREE     0x0001  /* Memory is from sqlite3Malloc() */
       
  9001 #define UNPACKED_NEED_DESTROY  0x0002  /* apMem[]s should all be destroyed */
       
  9002 #define UNPACKED_IGNORE_ROWID  0x0004  /* Ignore trailing rowid on key1 */
       
  9003 #define UNPACKED_INCRKEY       0x0008  /* Make this key an epsilon larger */
       
  9004 #define UNPACKED_PREFIX_MATCH  0x0010  /* A prefix match is considered OK */
       
  9005 #define UNPACKED_PREFIX_SEARCH 0x0020  /* A prefix match is considered OK */
       
  9006 
       
  9007 /*
       
  9008 ** Each SQL index is represented in memory by an
       
  9009 ** instance of the following structure.
       
  9010 **
       
  9011 ** The columns of the table that are to be indexed are described
       
  9012 ** by the aiColumn[] field of this structure.  For example, suppose
       
  9013 ** we have the following table and index:
       
  9014 **
       
  9015 **     CREATE TABLE Ex1(c1 int, c2 int, c3 text);
       
  9016 **     CREATE INDEX Ex2 ON Ex1(c3,c1);
       
  9017 **
       
  9018 ** In the Table structure describing Ex1, nCol==3 because there are
       
  9019 ** three columns in the table.  In the Index structure describing
       
  9020 ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
       
  9021 ** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the 
       
  9022 ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
       
  9023 ** The second column to be indexed (c1) has an index of 0 in
       
  9024 ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
       
  9025 **
       
  9026 ** The Index.onError field determines whether or not the indexed columns
       
  9027 ** must be unique and what to do if they are not.  When Index.onError=OE_None,
       
  9028 ** it means this is not a unique index.  Otherwise it is a unique index
       
  9029 ** and the value of Index.onError indicate the which conflict resolution 
       
  9030 ** algorithm to employ whenever an attempt is made to insert a non-unique
       
  9031 ** element.
       
  9032 */
       
  9033 struct Index {
       
  9034   char *zName;     /* Name of this index */
       
  9035   int nColumn;     /* Number of columns in the table used by this index */
       
  9036   int *aiColumn;   /* Which columns are used by this index.  1st is 0 */
       
  9037   unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
       
  9038   Table *pTable;   /* The SQL table being indexed */
       
  9039   int tnum;        /* Page containing root of this index in database file */
       
  9040   u8 onError;      /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
       
  9041   u8 autoIndex;    /* True if is automatically created (ex: by UNIQUE) */
       
  9042   char *zColAff;   /* String defining the affinity of each column */
       
  9043   Index *pNext;    /* The next index associated with the same table */
       
  9044   Schema *pSchema; /* Schema containing this index */
       
  9045   u8 *aSortOrder;  /* Array of size Index.nColumn. True==DESC, False==ASC */
       
  9046   char **azColl;   /* Array of collation sequence names for index */
       
  9047   IndexSample *aSample;    /* Array of SQLITE_INDEX_SAMPLES samples */
       
  9048 };
       
  9049 
       
  9050 /*
       
  9051 ** Each sample stored in the sqlite_stat2 table is represented in memory 
       
  9052 ** using a structure of this type.
       
  9053 */
       
  9054 struct IndexSample {
       
  9055   union {
       
  9056     char *z;        /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
       
  9057     double r;       /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
       
  9058   } u;
       
  9059   u8 eType;         /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
       
  9060   u8 nByte;         /* Size in byte of text or blob. */
       
  9061 };
       
  9062 
       
  9063 /*
       
  9064 ** Each token coming out of the lexer is an instance of
       
  9065 ** this structure.  Tokens are also used as part of an expression.
       
  9066 **
       
  9067 ** Note if Token.z==0 then Token.dyn and Token.n are undefined and
       
  9068 ** may contain random values.  Do not make any assumptions about Token.dyn
       
  9069 ** and Token.n when Token.z==0.
       
  9070 */
       
  9071 struct Token {
       
  9072   const char *z;     /* Text of the token.  Not NULL-terminated! */
       
  9073   unsigned int n;    /* Number of characters in this token */
       
  9074 };
       
  9075 
       
  9076 /*
       
  9077 ** An instance of this structure contains information needed to generate
       
  9078 ** code for a SELECT that contains aggregate functions.
       
  9079 **
       
  9080 ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
       
  9081 ** pointer to this structure.  The Expr.iColumn field is the index in
       
  9082 ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
       
  9083 ** code for that node.
       
  9084 **
       
  9085 ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
       
  9086 ** original Select structure that describes the SELECT statement.  These
       
  9087 ** fields do not need to be freed when deallocating the AggInfo structure.
       
  9088 */
       
  9089 struct AggInfo {
       
  9090   u8 directMode;          /* Direct rendering mode means take data directly
       
  9091                           ** from source tables rather than from accumulators */
       
  9092   u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
       
  9093                           ** than the source table */
       
  9094   int sortingIdx;         /* Cursor number of the sorting index */
       
  9095   ExprList *pGroupBy;     /* The group by clause */
       
  9096   int nSortingColumn;     /* Number of columns in the sorting index */
       
  9097   struct AggInfo_col {    /* For each column used in source tables */
       
  9098     Table *pTab;             /* Source table */
       
  9099     int iTable;              /* Cursor number of the source table */
       
  9100     int iColumn;             /* Column number within the source table */
       
  9101     int iSorterColumn;       /* Column number in the sorting index */
       
  9102     int iMem;                /* Memory location that acts as accumulator */
       
  9103     Expr *pExpr;             /* The original expression */
       
  9104   } *aCol;
       
  9105   int nColumn;            /* Number of used entries in aCol[] */
       
  9106   int nColumnAlloc;       /* Number of slots allocated for aCol[] */
       
  9107   int nAccumulator;       /* Number of columns that show through to the output.
       
  9108                           ** Additional columns are used only as parameters to
       
  9109                           ** aggregate functions */
       
  9110   struct AggInfo_func {   /* For each aggregate function */
       
  9111     Expr *pExpr;             /* Expression encoding the function */
       
  9112     FuncDef *pFunc;          /* The aggregate function implementation */
       
  9113     int iMem;                /* Memory location that acts as accumulator */
       
  9114     int iDistinct;           /* Ephemeral table used to enforce DISTINCT */
       
  9115   } *aFunc;
       
  9116   int nFunc;              /* Number of entries in aFunc[] */
       
  9117   int nFuncAlloc;         /* Number of slots allocated for aFunc[] */
       
  9118 };
       
  9119 
       
  9120 /*
       
  9121 ** Each node of an expression in the parse tree is an instance
       
  9122 ** of this structure.
       
  9123 **
       
  9124 ** Expr.op is the opcode. The integer parser token codes are reused
       
  9125 ** as opcodes here. For example, the parser defines TK_GE to be an integer
       
  9126 ** code representing the ">=" operator. This same integer code is reused
       
  9127 ** to represent the greater-than-or-equal-to operator in the expression
       
  9128 ** tree.
       
  9129 **
       
  9130 ** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, 
       
  9131 ** or TK_STRING), then Expr.token contains the text of the SQL literal. If
       
  9132 ** the expression is a variable (TK_VARIABLE), then Expr.token contains the 
       
  9133 ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
       
  9134 ** then Expr.token contains the name of the function.
       
  9135 **
       
  9136 ** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
       
  9137 ** binary operator. Either or both may be NULL.
       
  9138 **
       
  9139 ** Expr.x.pList is a list of arguments if the expression is an SQL function,
       
  9140 ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
       
  9141 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
       
  9142 ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
       
  9143 ** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is 
       
  9144 ** valid.
       
  9145 **
       
  9146 ** An expression of the form ID or ID.ID refers to a column in a table.
       
  9147 ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
       
  9148 ** the integer cursor number of a VDBE cursor pointing to that table and
       
  9149 ** Expr.iColumn is the column number for the specific column.  If the
       
  9150 ** expression is used as a result in an aggregate SELECT, then the
       
  9151 ** value is also stored in the Expr.iAgg column in the aggregate so that
       
  9152 ** it can be accessed after all aggregates are computed.
       
  9153 **
       
  9154 ** If the expression is an unbound variable marker (a question mark 
       
  9155 ** character '?' in the original SQL) then the Expr.iTable holds the index 
       
  9156 ** number for that variable.
       
  9157 **
       
  9158 ** If the expression is a subquery then Expr.iColumn holds an integer
       
  9159 ** register number containing the result of the subquery.  If the
       
  9160 ** subquery gives a constant result, then iTable is -1.  If the subquery
       
  9161 ** gives a different answer at different times during statement processing
       
  9162 ** then iTable is the address of a subroutine that computes the subquery.
       
  9163 **
       
  9164 ** If the Expr is of type OP_Column, and the table it is selecting from
       
  9165 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
       
  9166 ** corresponding table definition.
       
  9167 **
       
  9168 ** ALLOCATION NOTES:
       
  9169 **
       
  9170 ** Expr objects can use a lot of memory space in database schema.  To
       
  9171 ** help reduce memory requirements, sometimes an Expr object will be
       
  9172 ** truncated.  And to reduce the number of memory allocations, sometimes
       
  9173 ** two or more Expr objects will be stored in a single memory allocation,
       
  9174 ** together with Expr.zToken strings.
       
  9175 **
       
  9176 ** If the EP_Reduced and EP_TokenOnly flags are set when
       
  9177 ** an Expr object is truncated.  When EP_Reduced is set, then all
       
  9178 ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
       
  9179 ** are contained within the same memory allocation.  Note, however, that
       
  9180 ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
       
  9181 ** allocated, regardless of whether or not EP_Reduced is set.
       
  9182 */
       
  9183 struct Expr {
       
  9184   u8 op;                 /* Operation performed by this node */
       
  9185   char affinity;         /* The affinity of the column or 0 if not a column */
       
  9186   u16 flags;             /* Various flags.  EP_* See below */
       
  9187   union {
       
  9188     char *zToken;          /* Token value. Zero terminated and dequoted */
       
  9189     int iValue;            /* Integer value if EP_IntValue */
       
  9190   } u;
       
  9191 
       
  9192   /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
       
  9193   ** space is allocated for the fields below this point. An attempt to
       
  9194   ** access them will result in a segfault or malfunction. 
       
  9195   *********************************************************************/
       
  9196 
       
  9197   Expr *pLeft;           /* Left subnode */
       
  9198   Expr *pRight;          /* Right subnode */
       
  9199   union {
       
  9200     ExprList *pList;     /* Function arguments or in "<expr> IN (<expr-list)" */
       
  9201     Select *pSelect;     /* Used for sub-selects and "<expr> IN (<select>)" */
       
  9202   } x;
       
  9203   CollSeq *pColl;        /* The collation type of the column or 0 */
       
  9204 
       
  9205   /* If the EP_Reduced flag is set in the Expr.flags mask, then no
       
  9206   ** space is allocated for the fields below this point. An attempt to
       
  9207   ** access them will result in a segfault or malfunction.
       
  9208   *********************************************************************/
       
  9209 
       
  9210   int iTable;            /* TK_COLUMN: cursor number of table holding column
       
  9211                          ** TK_REGISTER: register number
       
  9212                          ** TK_TRIGGER: 1 -> new, 0 -> old */
       
  9213   i16 iColumn;           /* TK_COLUMN: column index.  -1 for rowid */
       
  9214   i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
       
  9215   i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
       
  9216   u8 flags2;             /* Second set of flags.  EP2_... */
       
  9217   u8 op2;                /* If a TK_REGISTER, the original value of Expr.op */
       
  9218   AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
       
  9219   Table *pTab;           /* Table for TK_COLUMN expressions. */
       
  9220 #if SQLITE_MAX_EXPR_DEPTH>0
       
  9221   int nHeight;           /* Height of the tree headed by this node */
       
  9222 #endif
       
  9223 };
       
  9224 
       
  9225 /*
       
  9226 ** The following are the meanings of bits in the Expr.flags field.
       
  9227 */
       
  9228 #define EP_FromJoin   0x0001  /* Originated in ON or USING clause of a join */
       
  9229 #define EP_Agg        0x0002  /* Contains one or more aggregate functions */
       
  9230 #define EP_Resolved   0x0004  /* IDs have been resolved to COLUMNs */
       
  9231 #define EP_Error      0x0008  /* Expression contains one or more errors */
       
  9232 #define EP_Distinct   0x0010  /* Aggregate function with DISTINCT keyword */
       
  9233 #define EP_VarSelect  0x0020  /* pSelect is correlated, not constant */
       
  9234 #define EP_DblQuoted  0x0040  /* token.z was originally in "..." */
       
  9235 #define EP_InfixFunc  0x0080  /* True for an infix function: LIKE, GLOB, etc */
       
  9236 #define EP_ExpCollate 0x0100  /* Collating sequence specified explicitly */
       
  9237 #define EP_AnyAff     0x0200  /* Can take a cached column of any affinity */
       
  9238 #define EP_FixedDest  0x0400  /* Result needed in a specific register */
       
  9239 #define EP_IntValue   0x0800  /* Integer value contained in u.iValue */
       
  9240 #define EP_xIsSelect  0x1000  /* x.pSelect is valid (otherwise x.pList is) */
       
  9241 
       
  9242 #define EP_Reduced    0x2000  /* Expr struct is EXPR_REDUCEDSIZE bytes only */
       
  9243 #define EP_TokenOnly  0x4000  /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
       
  9244 #define EP_Static     0x8000  /* Held in memory not obtained from malloc() */
       
  9245 
       
  9246 /*
       
  9247 ** The following are the meanings of bits in the Expr.flags2 field.
       
  9248 */
       
  9249 #define EP2_MallocedToken  0x0001  /* Need to sqlite3DbFree() Expr.zToken */
       
  9250 #define EP2_Irreducible    0x0002  /* Cannot EXPRDUP_REDUCE this Expr */
       
  9251 
       
  9252 /*
       
  9253 ** The pseudo-routine sqlite3ExprSetIrreducible sets the EP2_Irreducible
       
  9254 ** flag on an expression structure.  This flag is used for VV&A only.  The
       
  9255 ** routine is implemented as a macro that only works when in debugging mode,
       
  9256 ** so as not to burden production code.
       
  9257 */
       
  9258 #ifdef SQLITE_DEBUG
       
  9259 # define ExprSetIrreducible(X)  (X)->flags2 |= EP2_Irreducible
       
  9260 #else
       
  9261 # define ExprSetIrreducible(X)
       
  9262 #endif
       
  9263 
       
  9264 /*
       
  9265 ** These macros can be used to test, set, or clear bits in the 
       
  9266 ** Expr.flags field.
       
  9267 */
       
  9268 #define ExprHasProperty(E,P)     (((E)->flags&(P))==(P))
       
  9269 #define ExprHasAnyProperty(E,P)  (((E)->flags&(P))!=0)
       
  9270 #define ExprSetProperty(E,P)     (E)->flags|=(P)
       
  9271 #define ExprClearProperty(E,P)   (E)->flags&=~(P)
       
  9272 
       
  9273 /*
       
  9274 ** Macros to determine the number of bytes required by a normal Expr 
       
  9275 ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags 
       
  9276 ** and an Expr struct with the EP_TokenOnly flag set.
       
  9277 */
       
  9278 #define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
       
  9279 #define EXPR_REDUCEDSIZE        offsetof(Expr,iTable)  /* Common features */
       
  9280 #define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
       
  9281 
       
  9282 /*
       
  9283 ** Flags passed to the sqlite3ExprDup() function. See the header comment 
       
  9284 ** above sqlite3ExprDup() for details.
       
  9285 */
       
  9286 #define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
       
  9287 
       
  9288 /*
       
  9289 ** A list of expressions.  Each expression may optionally have a
       
  9290 ** name.  An expr/name combination can be used in several ways, such
       
  9291 ** as the list of "expr AS ID" fields following a "SELECT" or in the
       
  9292 ** list of "ID = expr" items in an UPDATE.  A list of expressions can
       
  9293 ** also be used as the argument to a function, in which case the a.zName
       
  9294 ** field is not used.
       
  9295 */
       
  9296 struct ExprList {
       
  9297   int nExpr;             /* Number of expressions on the list */
       
  9298   int nAlloc;            /* Number of entries allocated below */
       
  9299   int iECursor;          /* VDBE Cursor associated with this ExprList */
       
  9300   struct ExprList_item {
       
  9301     Expr *pExpr;           /* The list of expressions */
       
  9302     char *zName;           /* Token associated with this expression */
       
  9303     char *zSpan;           /* Original text of the expression */
       
  9304     u8 sortOrder;          /* 1 for DESC or 0 for ASC */
       
  9305     u8 done;               /* A flag to indicate when processing is finished */
       
  9306     u16 iCol;              /* For ORDER BY, column number in result set */
       
  9307     u16 iAlias;            /* Index into Parse.aAlias[] for zName */
       
  9308   } *a;                  /* One entry for each expression */
       
  9309 };
       
  9310 
       
  9311 /*
       
  9312 ** An instance of this structure is used by the parser to record both
       
  9313 ** the parse tree for an expression and the span of input text for an
       
  9314 ** expression.
       
  9315 */
       
  9316 struct ExprSpan {
       
  9317   Expr *pExpr;          /* The expression parse tree */
       
  9318   const char *zStart;   /* First character of input text */
       
  9319   const char *zEnd;     /* One character past the end of input text */
       
  9320 };
       
  9321 
       
  9322 /*
       
  9323 ** An instance of this structure can hold a simple list of identifiers,
       
  9324 ** such as the list "a,b,c" in the following statements:
       
  9325 **
       
  9326 **      INSERT INTO t(a,b,c) VALUES ...;
       
  9327 **      CREATE INDEX idx ON t(a,b,c);
       
  9328 **      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
       
  9329 **
       
  9330 ** The IdList.a.idx field is used when the IdList represents the list of
       
  9331 ** column names after a table name in an INSERT statement.  In the statement
       
  9332 **
       
  9333 **     INSERT INTO t(a,b,c) ...
       
  9334 **
       
  9335 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
       
  9336 */
       
  9337 struct IdList {
       
  9338   struct IdList_item {
       
  9339     char *zName;      /* Name of the identifier */
       
  9340     int idx;          /* Index in some Table.aCol[] of a column named zName */
       
  9341   } *a;
       
  9342   int nId;         /* Number of identifiers on the list */
       
  9343   int nAlloc;      /* Number of entries allocated for a[] below */
       
  9344 };
       
  9345 
       
  9346 /*
       
  9347 ** The bitmask datatype defined below is used for various optimizations.
       
  9348 **
       
  9349 ** Changing this from a 64-bit to a 32-bit type limits the number of
       
  9350 ** tables in a join to 32 instead of 64.  But it also reduces the size
       
  9351 ** of the library by 738 bytes on ix86.
       
  9352 */
       
  9353 typedef u64 Bitmask;
       
  9354 
       
  9355 /*
       
  9356 ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
       
  9357 */
       
  9358 #define BMS  ((int)(sizeof(Bitmask)*8))
       
  9359 
       
  9360 /*
       
  9361 ** The following structure describes the FROM clause of a SELECT statement.
       
  9362 ** Each table or subquery in the FROM clause is a separate element of
       
  9363 ** the SrcList.a[] array.
       
  9364 **
       
  9365 ** With the addition of multiple database support, the following structure
       
  9366 ** can also be used to describe a particular table such as the table that
       
  9367 ** is modified by an INSERT, DELETE, or UPDATE statement.  In standard SQL,
       
  9368 ** such a table must be a simple name: ID.  But in SQLite, the table can
       
  9369 ** now be identified by a database name, a dot, then the table name: ID.ID.
       
  9370 **
       
  9371 ** The jointype starts out showing the join type between the current table
       
  9372 ** and the next table on the list.  The parser builds the list this way.
       
  9373 ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
       
  9374 ** jointype expresses the join between the table and the previous table.
       
  9375 */
       
  9376 struct SrcList {
       
  9377   i16 nSrc;        /* Number of tables or subqueries in the FROM clause */
       
  9378   i16 nAlloc;      /* Number of entries allocated in a[] below */
       
  9379   struct SrcList_item {
       
  9380     char *zDatabase;  /* Name of database holding this table */
       
  9381     char *zName;      /* Name of the table */
       
  9382     char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
       
  9383     Table *pTab;      /* An SQL table corresponding to zName */
       
  9384     Select *pSelect;  /* A SELECT statement used in place of a table name */
       
  9385     u8 isPopulated;   /* Temporary table associated with SELECT is populated */
       
  9386     u8 jointype;      /* Type of join between this able and the previous */
       
  9387     u8 notIndexed;    /* True if there is a NOT INDEXED clause */
       
  9388     int iCursor;      /* The VDBE cursor number used to access this table */
       
  9389     Expr *pOn;        /* The ON clause of a join */
       
  9390     IdList *pUsing;   /* The USING clause of a join */
       
  9391     Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
       
  9392     char *zIndex;     /* Identifier from "INDEXED BY <zIndex>" clause */
       
  9393     Index *pIndex;    /* Index structure corresponding to zIndex, if any */
       
  9394   } a[1];             /* One entry for each identifier on the list */
       
  9395 };
       
  9396 
       
  9397 /*
       
  9398 ** Permitted values of the SrcList.a.jointype field
       
  9399 */
       
  9400 #define JT_INNER     0x0001    /* Any kind of inner or cross join */
       
  9401 #define JT_CROSS     0x0002    /* Explicit use of the CROSS keyword */
       
  9402 #define JT_NATURAL   0x0004    /* True for a "natural" join */
       
  9403 #define JT_LEFT      0x0008    /* Left outer join */
       
  9404 #define JT_RIGHT     0x0010    /* Right outer join */
       
  9405 #define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
       
  9406 #define JT_ERROR     0x0040    /* unknown or unsupported join type */
       
  9407 
       
  9408 
       
  9409 /*
       
  9410 ** A WherePlan object holds information that describes a lookup
       
  9411 ** strategy.
       
  9412 **
       
  9413 ** This object is intended to be opaque outside of the where.c module.
       
  9414 ** It is included here only so that that compiler will know how big it
       
  9415 ** is.  None of the fields in this object should be used outside of
       
  9416 ** the where.c module.
       
  9417 **
       
  9418 ** Within the union, pIdx is only used when wsFlags&WHERE_INDEXED is true.
       
  9419 ** pTerm is only used when wsFlags&WHERE_MULTI_OR is true.  And pVtabIdx
       
  9420 ** is only used when wsFlags&WHERE_VIRTUALTABLE is true.  It is never the
       
  9421 ** case that more than one of these conditions is true.
       
  9422 */
       
  9423 struct WherePlan {
       
  9424   u32 wsFlags;                   /* WHERE_* flags that describe the strategy */
       
  9425   u32 nEq;                       /* Number of == constraints */
       
  9426   union {
       
  9427     Index *pIdx;                   /* Index when WHERE_INDEXED is true */
       
  9428     struct WhereTerm *pTerm;       /* WHERE clause term for OR-search */
       
  9429     sqlite3_index_info *pVtabIdx;  /* Virtual table index to use */
       
  9430   } u;
       
  9431 };
       
  9432 
       
  9433 /*
       
  9434 ** For each nested loop in a WHERE clause implementation, the WhereInfo
       
  9435 ** structure contains a single instance of this structure.  This structure
       
  9436 ** is intended to be private the the where.c module and should not be
       
  9437 ** access or modified by other modules.
       
  9438 **
       
  9439 ** The pIdxInfo field is used to help pick the best index on a
       
  9440 ** virtual table.  The pIdxInfo pointer contains indexing
       
  9441 ** information for the i-th table in the FROM clause before reordering.
       
  9442 ** All the pIdxInfo pointers are freed by whereInfoFree() in where.c.
       
  9443 ** All other information in the i-th WhereLevel object for the i-th table
       
  9444 ** after FROM clause ordering.
       
  9445 */
       
  9446 struct WhereLevel {
       
  9447   WherePlan plan;       /* query plan for this element of the FROM clause */
       
  9448   int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
       
  9449   int iTabCur;          /* The VDBE cursor used to access the table */
       
  9450   int iIdxCur;          /* The VDBE cursor used to access pIdx */
       
  9451   int addrBrk;          /* Jump here to break out of the loop */
       
  9452   int addrNxt;          /* Jump here to start the next IN combination */
       
  9453   int addrCont;         /* Jump here to continue with the next loop cycle */
       
  9454   int addrFirst;        /* First instruction of interior of the loop */
       
  9455   u8 iFrom;             /* Which entry in the FROM clause */
       
  9456   u8 op, p5;            /* Opcode and P5 of the opcode that ends the loop */
       
  9457   int p1, p2;           /* Operands of the opcode used to ends the loop */
       
  9458   union {               /* Information that depends on plan.wsFlags */
       
  9459     struct {
       
  9460       int nIn;              /* Number of entries in aInLoop[] */
       
  9461       struct InLoop {
       
  9462         int iCur;              /* The VDBE cursor used by this IN operator */
       
  9463         int addrInTop;         /* Top of the IN loop */
       
  9464       } *aInLoop;           /* Information about each nested IN operator */
       
  9465     } in;                 /* Used when plan.wsFlags&WHERE_IN_ABLE */
       
  9466   } u;
       
  9467 
       
  9468   /* The following field is really not part of the current level.  But
       
  9469   ** we need a place to cache virtual table index information for each
       
  9470   ** virtual table in the FROM clause and the WhereLevel structure is
       
  9471   ** a convenient place since there is one WhereLevel for each FROM clause
       
  9472   ** element.
       
  9473   */
       
  9474   sqlite3_index_info *pIdxInfo;  /* Index info for n-th source table */
       
  9475 };
       
  9476 
       
  9477 /*
       
  9478 ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
       
  9479 ** and the WhereInfo.wctrlFlags member.
       
  9480 */
       
  9481 #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
       
  9482 #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
       
  9483 #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
       
  9484 #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
       
  9485 #define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
       
  9486 #define WHERE_OMIT_OPEN        0x0010 /* Table cursor are already open */
       
  9487 #define WHERE_OMIT_CLOSE       0x0020 /* Omit close of table & index cursors */
       
  9488 #define WHERE_FORCE_TABLE      0x0040 /* Do not use an index-only search */
       
  9489 
       
  9490 /*
       
  9491 ** The WHERE clause processing routine has two halves.  The
       
  9492 ** first part does the start of the WHERE loop and the second
       
  9493 ** half does the tail of the WHERE loop.  An instance of
       
  9494 ** this structure is returned by the first half and passed
       
  9495 ** into the second half to give some continuity.
       
  9496 */
       
  9497 struct WhereInfo {
       
  9498   Parse *pParse;       /* Parsing and code generating context */
       
  9499   u16 wctrlFlags;      /* Flags originally passed to sqlite3WhereBegin() */
       
  9500   u8 okOnePass;        /* Ok to use one-pass algorithm for UPDATE or DELETE */
       
  9501   SrcList *pTabList;             /* List of tables in the join */
       
  9502   int iTop;                      /* The very beginning of the WHERE loop */
       
  9503   int iContinue;                 /* Jump here to continue with next record */
       
  9504   int iBreak;                    /* Jump here to break out of the loop */
       
  9505   int nLevel;                    /* Number of nested loop */
       
  9506   struct WhereClause *pWC;       /* Decomposition of the WHERE clause */
       
  9507   WhereLevel a[1];               /* Information about each nest loop in WHERE */
       
  9508 };
       
  9509 
       
  9510 /*
       
  9511 ** A NameContext defines a context in which to resolve table and column
       
  9512 ** names.  The context consists of a list of tables (the pSrcList) field and
       
  9513 ** a list of named expression (pEList).  The named expression list may
       
  9514 ** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
       
  9515 ** to the table being operated on by INSERT, UPDATE, or DELETE.  The
       
  9516 ** pEList corresponds to the result set of a SELECT and is NULL for
       
  9517 ** other statements.
       
  9518 **
       
  9519 ** NameContexts can be nested.  When resolving names, the inner-most 
       
  9520 ** context is searched first.  If no match is found, the next outer
       
  9521 ** context is checked.  If there is still no match, the next context
       
  9522 ** is checked.  This process continues until either a match is found
       
  9523 ** or all contexts are check.  When a match is found, the nRef member of
       
  9524 ** the context containing the match is incremented. 
       
  9525 **
       
  9526 ** Each subquery gets a new NameContext.  The pNext field points to the
       
  9527 ** NameContext in the parent query.  Thus the process of scanning the
       
  9528 ** NameContext list corresponds to searching through successively outer
       
  9529 ** subqueries looking for a match.
       
  9530 */
       
  9531 struct NameContext {
       
  9532   Parse *pParse;       /* The parser */
       
  9533   SrcList *pSrcList;   /* One or more tables used to resolve names */
       
  9534   ExprList *pEList;    /* Optional list of named expressions */
       
  9535   int nRef;            /* Number of names resolved by this context */
       
  9536   int nErr;            /* Number of errors encountered while resolving names */
       
  9537   u8 allowAgg;         /* Aggregate functions allowed here */
       
  9538   u8 hasAgg;           /* True if aggregates are seen */
       
  9539   u8 isCheck;          /* True if resolving names in a CHECK constraint */
       
  9540   int nDepth;          /* Depth of subquery recursion. 1 for no recursion */
       
  9541   AggInfo *pAggInfo;   /* Information about aggregates at this level */
       
  9542   NameContext *pNext;  /* Next outer name context.  NULL for outermost */
       
  9543 };
       
  9544 
       
  9545 /*
       
  9546 ** An instance of the following structure contains all information
       
  9547 ** needed to generate code for a single SELECT statement.
       
  9548 **
       
  9549 ** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
       
  9550 ** If there is a LIMIT clause, the parser sets nLimit to the value of the
       
  9551 ** limit and nOffset to the value of the offset (or 0 if there is not
       
  9552 ** offset).  But later on, nLimit and nOffset become the memory locations
       
  9553 ** in the VDBE that record the limit and offset counters.
       
  9554 **
       
  9555 ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
       
  9556 ** These addresses must be stored so that we can go back and fill in
       
  9557 ** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
       
  9558 ** the number of columns in P2 can be computed at the same time
       
  9559 ** as the OP_OpenEphm instruction is coded because not
       
  9560 ** enough information about the compound query is known at that point.
       
  9561 ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
       
  9562 ** for the result set.  The KeyInfo for addrOpenTran[2] contains collating
       
  9563 ** sequences for the ORDER BY clause.
       
  9564 */
       
  9565 struct Select {
       
  9566   ExprList *pEList;      /* The fields of the result */
       
  9567   u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
       
  9568   char affinity;         /* MakeRecord with this affinity for SRT_Set */
       
  9569   u16 selFlags;          /* Various SF_* values */
       
  9570   SrcList *pSrc;         /* The FROM clause */
       
  9571   Expr *pWhere;          /* The WHERE clause */
       
  9572   ExprList *pGroupBy;    /* The GROUP BY clause */
       
  9573   Expr *pHaving;         /* The HAVING clause */
       
  9574   ExprList *pOrderBy;    /* The ORDER BY clause */
       
  9575   Select *pPrior;        /* Prior select in a compound select statement */
       
  9576   Select *pNext;         /* Next select to the left in a compound */
       
  9577   Select *pRightmost;    /* Right-most select in a compound select statement */
       
  9578   Expr *pLimit;          /* LIMIT expression. NULL means not used. */
       
  9579   Expr *pOffset;         /* OFFSET expression. NULL means not used. */
       
  9580   int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
       
  9581   int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
       
  9582 };
       
  9583 
       
  9584 /*
       
  9585 ** Allowed values for Select.selFlags.  The "SF" prefix stands for
       
  9586 ** "Select Flag".
       
  9587 */
       
  9588 #define SF_Distinct        0x0001  /* Output should be DISTINCT */
       
  9589 #define SF_Resolved        0x0002  /* Identifiers have been resolved */
       
  9590 #define SF_Aggregate       0x0004  /* Contains aggregate functions */
       
  9591 #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
       
  9592 #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
       
  9593 #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
       
  9594 
       
  9595 
       
  9596 /*
       
  9597 ** The results of a select can be distributed in several ways.  The
       
  9598 ** "SRT" prefix means "SELECT Result Type".
       
  9599 */
       
  9600 #define SRT_Union        1  /* Store result as keys in an index */
       
  9601 #define SRT_Except       2  /* Remove result from a UNION index */
       
  9602 #define SRT_Exists       3  /* Store 1 if the result is not empty */
       
  9603 #define SRT_Discard      4  /* Do not save the results anywhere */
       
  9604 
       
  9605 /* The ORDER BY clause is ignored for all of the above */
       
  9606 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
       
  9607 
       
  9608 #define SRT_Output       5  /* Output each row of result */
       
  9609 #define SRT_Mem          6  /* Store result in a memory cell */
       
  9610 #define SRT_Set          7  /* Store results as keys in an index */
       
  9611 #define SRT_Table        8  /* Store result as data with an automatic rowid */
       
  9612 #define SRT_EphemTab     9  /* Create transient tab and store like SRT_Table */
       
  9613 #define SRT_Coroutine   10  /* Generate a single row of result */
       
  9614 
       
  9615 /*
       
  9616 ** A structure used to customize the behavior of sqlite3Select(). See
       
  9617 ** comments above sqlite3Select() for details.
       
  9618 */
       
  9619 typedef struct SelectDest SelectDest;
       
  9620 struct SelectDest {
       
  9621   u8 eDest;         /* How to dispose of the results */
       
  9622   u8 affinity;      /* Affinity used when eDest==SRT_Set */
       
  9623   int iParm;        /* A parameter used by the eDest disposal method */
       
  9624   int iMem;         /* Base register where results are written */
       
  9625   int nMem;         /* Number of registers allocated */
       
  9626 };
       
  9627 
       
  9628 /*
       
  9629 ** During code generation of statements that do inserts into AUTOINCREMENT 
       
  9630 ** tables, the following information is attached to the Table.u.autoInc.p
       
  9631 ** pointer of each autoincrement table to record some side information that
       
  9632 ** the code generator needs.  We have to keep per-table autoincrement
       
  9633 ** information in case inserts are down within triggers.  Triggers do not
       
  9634 ** normally coordinate their activities, but we do need to coordinate the
       
  9635 ** loading and saving of autoincrement information.
       
  9636 */
       
  9637 struct AutoincInfo {
       
  9638   AutoincInfo *pNext;   /* Next info block in a list of them all */
       
  9639   Table *pTab;          /* Table this info block refers to */
       
  9640   int iDb;              /* Index in sqlite3.aDb[] of database holding pTab */
       
  9641   int regCtr;           /* Memory register holding the rowid counter */
       
  9642 };
       
  9643 
       
  9644 /*
       
  9645 ** Size of the column cache
       
  9646 */
       
  9647 #ifndef SQLITE_N_COLCACHE
       
  9648 # define SQLITE_N_COLCACHE 10
       
  9649 #endif
       
  9650 
       
  9651 /*
       
  9652 ** At least one instance of the following structure is created for each 
       
  9653 ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
       
  9654 ** statement. All such objects are stored in the linked list headed at
       
  9655 ** Parse.pTriggerPrg and deleted once statement compilation has been
       
  9656 ** completed.
       
  9657 **
       
  9658 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
       
  9659 ** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
       
  9660 ** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
       
  9661 ** The Parse.pTriggerPrg list never contains two entries with the same
       
  9662 ** values for both pTrigger and orconf.
       
  9663 **
       
  9664 ** The TriggerPrg.oldmask variable is set to a mask of old.* columns
       
  9665 ** accessed (or set to 0 for triggers fired as a result of INSERT 
       
  9666 ** statements).
       
  9667 */
       
  9668 struct TriggerPrg {
       
  9669   Trigger *pTrigger;      /* Trigger this program was coded from */
       
  9670   int orconf;             /* Default ON CONFLICT policy */
       
  9671   SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
       
  9672   u32 oldmask;            /* Mask of old.* columns accessed */
       
  9673   TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
       
  9674 };
       
  9675 
       
  9676 /*
       
  9677 ** An SQL parser context.  A copy of this structure is passed through
       
  9678 ** the parser and down into all the parser action routine in order to
       
  9679 ** carry around information that is global to the entire parse.
       
  9680 **
       
  9681 ** The structure is divided into two parts.  When the parser and code
       
  9682 ** generate call themselves recursively, the first part of the structure
       
  9683 ** is constant but the second part is reset at the beginning and end of
       
  9684 ** each recursion.
       
  9685 **
       
  9686 ** The nTableLock and aTableLock variables are only used if the shared-cache 
       
  9687 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
       
  9688 ** used to store the set of table-locks required by the statement being
       
  9689 ** compiled. Function sqlite3TableLock() is used to add entries to the
       
  9690 ** list.
       
  9691 */
       
  9692 struct Parse {
       
  9693   sqlite3 *db;         /* The main database structure */
       
  9694   int rc;              /* Return code from execution */
       
  9695   char *zErrMsg;       /* An error message */
       
  9696   Vdbe *pVdbe;         /* An engine for executing database bytecode */
       
  9697   u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
       
  9698   u8 nameClash;        /* A permanent table name clashes with temp table name */
       
  9699   u8 checkSchema;      /* Causes schema cookie check after an error */
       
  9700   u8 nested;           /* Number of nested calls to the parser/code generator */
       
  9701   u8 parseError;       /* True after a parsing error.  Ticket #1794 */
       
  9702   u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
       
  9703   u8 nTempInUse;       /* Number of aTempReg[] currently checked out */
       
  9704   int aTempReg[8];     /* Holding area for temporary registers */
       
  9705   int nRangeReg;       /* Size of the temporary register block */
       
  9706   int iRangeReg;       /* First register in temporary register block */
       
  9707   int nErr;            /* Number of errors seen */
       
  9708   int nTab;            /* Number of previously allocated VDBE cursors */
       
  9709   int nMem;            /* Number of memory cells used so far */
       
  9710   int nSet;            /* Number of sets used so far */
       
  9711   int ckBase;          /* Base register of data during check constraints */
       
  9712   int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
       
  9713   int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
       
  9714   u8 nColCache;        /* Number of entries in the column cache */
       
  9715   u8 iColCache;        /* Next entry of the cache to replace */
       
  9716   struct yColCache {
       
  9717     int iTable;           /* Table cursor number */
       
  9718     int iColumn;          /* Table column number */
       
  9719     u8 affChange;         /* True if this register has had an affinity change */
       
  9720     u8 tempReg;           /* iReg is a temp register that needs to be freed */
       
  9721     int iLevel;           /* Nesting level */
       
  9722     int iReg;             /* Reg with value of this column. 0 means none. */
       
  9723     int lru;              /* Least recently used entry has the smallest value */
       
  9724   } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
       
  9725   u32 writeMask;       /* Start a write transaction on these databases */
       
  9726   u32 cookieMask;      /* Bitmask of schema verified databases */
       
  9727   u8 isMultiWrite;     /* True if statement may affect/insert multiple rows */
       
  9728   u8 mayAbort;         /* True if statement may throw an ABORT exception */
       
  9729   int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
       
  9730   int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
       
  9731 #ifndef SQLITE_OMIT_SHARED_CACHE
       
  9732   int nTableLock;        /* Number of locks in aTableLock */
       
  9733   TableLock *aTableLock; /* Required table locks for shared-cache mode */
       
  9734 #endif
       
  9735   int regRowid;        /* Register holding rowid of CREATE TABLE entry */
       
  9736   int regRoot;         /* Register holding root page number for new objects */
       
  9737   AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
       
  9738   int nMaxArg;         /* Max args passed to user function by sub-program */
       
  9739 
       
  9740   /* Information used while coding trigger programs. */
       
  9741   Parse *pToplevel;    /* Parse structure for main program (or NULL) */
       
  9742   Table *pTriggerTab;  /* Table triggers are being coded for */
       
  9743   u32 oldmask;         /* Mask of old.* columns referenced */
       
  9744   u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
       
  9745   u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
       
  9746   u8 disableTriggers;  /* True to disable triggers */
       
  9747 
       
  9748   /* Above is constant between recursions.  Below is reset before and after
       
  9749   ** each recursion */
       
  9750 
       
  9751   int nVar;            /* Number of '?' variables seen in the SQL so far */
       
  9752   int nVarExpr;        /* Number of used slots in apVarExpr[] */
       
  9753   int nVarExprAlloc;   /* Number of allocated slots in apVarExpr[] */
       
  9754   Expr **apVarExpr;    /* Pointers to :aaa and $aaaa wildcard expressions */
       
  9755   int nAlias;          /* Number of aliased result set columns */
       
  9756   int nAliasAlloc;     /* Number of allocated slots for aAlias[] */
       
  9757   int *aAlias;         /* Register used to hold aliased result */
       
  9758   u8 explain;          /* True if the EXPLAIN flag is found on the query */
       
  9759   Token sNameToken;    /* Token with unqualified schema object name */
       
  9760   Token sLastToken;    /* The last token parsed */
       
  9761   const char *zTail;   /* All SQL text past the last semicolon parsed */
       
  9762   Table *pNewTable;    /* A table being constructed by CREATE TABLE */
       
  9763   Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
       
  9764   const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
       
  9765 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
  9766   Token sArg;                /* Complete text of a module argument */
       
  9767   u8 declareVtab;            /* True if inside sqlite3_declare_vtab() */
       
  9768   int nVtabLock;             /* Number of virtual tables to lock */
       
  9769   Table **apVtabLock;        /* Pointer to virtual tables needing locking */
       
  9770 #endif
       
  9771   int nHeight;            /* Expression tree height of current sub-select */
       
  9772   Table *pZombieTab;      /* List of Table objects to delete after code gen */
       
  9773   TriggerPrg *pTriggerPrg;    /* Linked list of coded triggers */
       
  9774 };
       
  9775 
       
  9776 #ifdef SQLITE_OMIT_VIRTUALTABLE
       
  9777   #define IN_DECLARE_VTAB 0
       
  9778 #else
       
  9779   #define IN_DECLARE_VTAB (pParse->declareVtab)
       
  9780 #endif
       
  9781 
       
  9782 /*
       
  9783 ** An instance of the following structure can be declared on a stack and used
       
  9784 ** to save the Parse.zAuthContext value so that it can be restored later.
       
  9785 */
       
  9786 struct AuthContext {
       
  9787   const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
       
  9788   Parse *pParse;              /* The Parse structure */
       
  9789 };
       
  9790 
       
  9791 /*
       
  9792 ** Bitfield flags for P5 value in OP_Insert and OP_Delete
       
  9793 */
       
  9794 #define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
       
  9795 #define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
       
  9796 #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
       
  9797 #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
       
  9798 #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
       
  9799 #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
       
  9800 
       
  9801 /*
       
  9802  * Each trigger present in the database schema is stored as an instance of
       
  9803  * struct Trigger. 
       
  9804  *
       
  9805  * Pointers to instances of struct Trigger are stored in two ways.
       
  9806  * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 
       
  9807  *    database). This allows Trigger structures to be retrieved by name.
       
  9808  * 2. All triggers associated with a single table form a linked list, using the
       
  9809  *    pNext member of struct Trigger. A pointer to the first element of the
       
  9810  *    linked list is stored as the "pTrigger" member of the associated
       
  9811  *    struct Table.
       
  9812  *
       
  9813  * The "step_list" member points to the first element of a linked list
       
  9814  * containing the SQL statements specified as the trigger program.
       
  9815  */
       
  9816 struct Trigger {
       
  9817   char *zName;            /* The name of the trigger                        */
       
  9818   char *table;            /* The table or view to which the trigger applies */
       
  9819   u8 op;                  /* One of TK_DELETE, TK_UPDATE, TK_INSERT         */
       
  9820   u8 tr_tm;               /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
       
  9821   Expr *pWhen;            /* The WHEN clause of the expression (may be NULL) */
       
  9822   IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
       
  9823                              the <column-list> is stored here */
       
  9824   Schema *pSchema;        /* Schema containing the trigger */
       
  9825   Schema *pTabSchema;     /* Schema containing the table */
       
  9826   TriggerStep *step_list; /* Link list of trigger program steps             */
       
  9827   Trigger *pNext;         /* Next trigger associated with the table */
       
  9828 };
       
  9829 
       
  9830 /*
       
  9831 ** A trigger is either a BEFORE or an AFTER trigger.  The following constants
       
  9832 ** determine which. 
       
  9833 **
       
  9834 ** If there are multiple triggers, you might of some BEFORE and some AFTER.
       
  9835 ** In that cases, the constants below can be ORed together.
       
  9836 */
       
  9837 #define TRIGGER_BEFORE  1
       
  9838 #define TRIGGER_AFTER   2
       
  9839 
       
  9840 /*
       
  9841  * An instance of struct TriggerStep is used to store a single SQL statement
       
  9842  * that is a part of a trigger-program. 
       
  9843  *
       
  9844  * Instances of struct TriggerStep are stored in a singly linked list (linked
       
  9845  * using the "pNext" member) referenced by the "step_list" member of the 
       
  9846  * associated struct Trigger instance. The first element of the linked list is
       
  9847  * the first step of the trigger-program.
       
  9848  * 
       
  9849  * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
       
  9850  * "SELECT" statement. The meanings of the other members is determined by the 
       
  9851  * value of "op" as follows:
       
  9852  *
       
  9853  * (op == TK_INSERT)
       
  9854  * orconf    -> stores the ON CONFLICT algorithm
       
  9855  * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
       
  9856  *              this stores a pointer to the SELECT statement. Otherwise NULL.
       
  9857  * target    -> A token holding the quoted name of the table to insert into.
       
  9858  * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
       
  9859  *              this stores values to be inserted. Otherwise NULL.
       
  9860  * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ... 
       
  9861  *              statement, then this stores the column-names to be
       
  9862  *              inserted into.
       
  9863  *
       
  9864  * (op == TK_DELETE)
       
  9865  * target    -> A token holding the quoted name of the table to delete from.
       
  9866  * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
       
  9867  *              Otherwise NULL.
       
  9868  * 
       
  9869  * (op == TK_UPDATE)
       
  9870  * target    -> A token holding the quoted name of the table to update rows of.
       
  9871  * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
       
  9872  *              Otherwise NULL.
       
  9873  * pExprList -> A list of the columns to update and the expressions to update
       
  9874  *              them to. See sqlite3Update() documentation of "pChanges"
       
  9875  *              argument.
       
  9876  * 
       
  9877  */
       
  9878 struct TriggerStep {
       
  9879   u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
       
  9880   u8 orconf;           /* OE_Rollback etc. */
       
  9881   Trigger *pTrig;      /* The trigger that this step is a part of */
       
  9882   Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
       
  9883   Token target;        /* Target table for DELETE, UPDATE, INSERT */
       
  9884   Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
       
  9885   ExprList *pExprList; /* SET clause for UPDATE.  VALUES clause for INSERT */
       
  9886   IdList *pIdList;     /* Column names for INSERT */
       
  9887   TriggerStep *pNext;  /* Next in the link-list */
       
  9888   TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
       
  9889 };
       
  9890 
       
  9891 /*
       
  9892 ** The following structure contains information used by the sqliteFix...
       
  9893 ** routines as they walk the parse tree to make database references
       
  9894 ** explicit.  
       
  9895 */
       
  9896 typedef struct DbFixer DbFixer;
       
  9897 struct DbFixer {
       
  9898   Parse *pParse;      /* The parsing context.  Error messages written here */
       
  9899   const char *zDb;    /* Make sure all objects are contained in this database */
       
  9900   const char *zType;  /* Type of the container - used for error messages */
       
  9901   const Token *pName; /* Name of the container - used for error messages */
       
  9902 };
       
  9903 
       
  9904 /*
       
  9905 ** An objected used to accumulate the text of a string where we
       
  9906 ** do not necessarily know how big the string will be in the end.
       
  9907 */
       
  9908 struct StrAccum {
       
  9909   sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
       
  9910   char *zBase;         /* A base allocation.  Not from malloc. */
       
  9911   char *zText;         /* The string collected so far */
       
  9912   int  nChar;          /* Length of the string so far */
       
  9913   int  nAlloc;         /* Amount of space allocated in zText */
       
  9914   int  mxAlloc;        /* Maximum allowed string length */
       
  9915   u8   mallocFailed;   /* Becomes true if any memory allocation fails */
       
  9916   u8   useMalloc;      /* True if zText is enlargeable using realloc */
       
  9917   u8   tooBig;         /* Becomes true if string size exceeds limits */
       
  9918 };
       
  9919 
       
  9920 /*
       
  9921 ** A pointer to this structure is used to communicate information
       
  9922 ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
       
  9923 */
       
  9924 typedef struct {
       
  9925   sqlite3 *db;        /* The database being initialized */
       
  9926   int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
       
  9927   char **pzErrMsg;    /* Error message stored here */
       
  9928   int rc;             /* Result code stored here */
       
  9929 } InitData;
       
  9930 
       
  9931 /*
       
  9932 ** Structure containing global configuration data for the SQLite library.
       
  9933 **
       
  9934 ** This structure also contains some state information.
       
  9935 */
       
  9936 struct Sqlite3Config {
       
  9937   int bMemstat;                     /* True to enable memory status */
       
  9938   int bCoreMutex;                   /* True to enable core mutexing */
       
  9939   int bFullMutex;                   /* True to enable full mutexing */
       
  9940   int mxStrlen;                     /* Maximum string length */
       
  9941   int szLookaside;                  /* Default lookaside buffer size */
       
  9942   int nLookaside;                   /* Default lookaside buffer count */
       
  9943   sqlite3_mem_methods m;            /* Low-level memory allocation interface */
       
  9944   sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
       
  9945   sqlite3_pcache_methods pcache;    /* Low-level page-cache interface */
       
  9946   void *pHeap;                      /* Heap storage space */
       
  9947   int nHeap;                        /* Size of pHeap[] */
       
  9948   int mnReq, mxReq;                 /* Min and max heap requests sizes */
       
  9949   void *pScratch;                   /* Scratch memory */
       
  9950   int szScratch;                    /* Size of each scratch buffer */
       
  9951   int nScratch;                     /* Number of scratch buffers */
       
  9952   void *pPage;                      /* Page cache memory */
       
  9953   int szPage;                       /* Size of each page in pPage[] */
       
  9954   int nPage;                        /* Number of pages in pPage[] */
       
  9955   int mxParserStack;                /* maximum depth of the parser stack */
       
  9956   int sharedCacheEnabled;           /* true if shared-cache mode enabled */
       
  9957   /* The above might be initialized to non-zero.  The following need to always
       
  9958   ** initially be zero, however. */
       
  9959   int isInit;                       /* True after initialization has finished */
       
  9960   int inProgress;                   /* True while initialization in progress */
       
  9961   int isMutexInit;                  /* True after mutexes are initialized */
       
  9962   int isMallocInit;                 /* True after malloc is initialized */
       
  9963   int isPCacheInit;                 /* True after malloc is initialized */
       
  9964   sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
       
  9965   int nRefInitMutex;                /* Number of users of pInitMutex */
       
  9966 };
       
  9967 
       
  9968 /*
       
  9969 ** Context pointer passed down through the tree-walk.
       
  9970 */
       
  9971 struct Walker {
       
  9972   int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
       
  9973   int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
       
  9974   Parse *pParse;                            /* Parser context.  */
       
  9975   union {                                   /* Extra data for callback */
       
  9976     NameContext *pNC;                          /* Naming context */
       
  9977     int i;                                     /* Integer value */
       
  9978   } u;
       
  9979 };
       
  9980 
       
  9981 /* Forward declarations */
       
  9982 SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
       
  9983 SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
       
  9984 SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
       
  9985 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
       
  9986 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
       
  9987 
       
  9988 /*
       
  9989 ** Return code from the parse-tree walking primitives and their
       
  9990 ** callbacks.
       
  9991 */
       
  9992 #define WRC_Continue    0   /* Continue down into children */
       
  9993 #define WRC_Prune       1   /* Omit children but continue walking siblings */
       
  9994 #define WRC_Abort       2   /* Abandon the tree walk */
       
  9995 
       
  9996 /*
       
  9997 ** Assuming zIn points to the first byte of a UTF-8 character,
       
  9998 ** advance zIn to point to the first byte of the next UTF-8 character.
       
  9999 */
       
 10000 #define SQLITE_SKIP_UTF8(zIn) {                        \
       
 10001   if( (*(zIn++))>=0xc0 ){                              \
       
 10002     while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
       
 10003   }                                                    \
       
 10004 }
       
 10005 
       
 10006 /*
       
 10007 ** The SQLITE_CORRUPT_BKPT macro can be either a constant (for production
       
 10008 ** builds) or a function call (for debugging).  If it is a function call,
       
 10009 ** it allows the operator to set a breakpoint at the spot where database
       
 10010 ** corruption is first detected.
       
 10011 */
       
 10012 #ifdef SQLITE_DEBUG
       
 10013 SQLITE_PRIVATE   int sqlite3Corrupt(void);
       
 10014 # define SQLITE_CORRUPT_BKPT sqlite3Corrupt()
       
 10015 #else
       
 10016 # define SQLITE_CORRUPT_BKPT SQLITE_CORRUPT
       
 10017 #endif
       
 10018 
       
 10019 /*
       
 10020 ** The ctype.h header is needed for non-ASCII systems.  It is also
       
 10021 ** needed by FTS3 when FTS3 is included in the amalgamation.
       
 10022 */
       
 10023 #if !defined(SQLITE_ASCII) || \
       
 10024     (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
       
 10025 # include <ctype.h>
       
 10026 #endif
       
 10027 
       
 10028 /*
       
 10029 ** The following macros mimic the standard library functions toupper(),
       
 10030 ** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
       
 10031 ** sqlite versions only work for ASCII characters, regardless of locale.
       
 10032 */
       
 10033 #ifdef SQLITE_ASCII
       
 10034 # define sqlite3Toupper(x)  ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
       
 10035 # define sqlite3Isspace(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
       
 10036 # define sqlite3Isalnum(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
       
 10037 # define sqlite3Isalpha(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
       
 10038 # define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
       
 10039 # define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
       
 10040 # define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
       
 10041 #else
       
 10042 # define sqlite3Toupper(x)   toupper((unsigned char)(x))
       
 10043 # define sqlite3Isspace(x)   isspace((unsigned char)(x))
       
 10044 # define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
       
 10045 # define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
       
 10046 # define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
       
 10047 # define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
       
 10048 # define sqlite3Tolower(x)   tolower((unsigned char)(x))
       
 10049 #endif
       
 10050 
       
 10051 /*
       
 10052 ** Internal function prototypes
       
 10053 */
       
 10054 SQLITE_PRIVATE int sqlite3StrICmp(const char *, const char *);
       
 10055 SQLITE_PRIVATE int sqlite3IsNumber(const char*, int*, u8);
       
 10056 SQLITE_PRIVATE int sqlite3Strlen30(const char*);
       
 10057 #define sqlite3StrNICmp sqlite3_strnicmp
       
 10058 
       
 10059 SQLITE_PRIVATE int sqlite3MallocInit(void);
       
 10060 SQLITE_PRIVATE void sqlite3MallocEnd(void);
       
 10061 SQLITE_PRIVATE void *sqlite3Malloc(int);
       
 10062 SQLITE_PRIVATE void *sqlite3MallocZero(int);
       
 10063 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, int);
       
 10064 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, int);
       
 10065 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
       
 10066 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int);
       
 10067 SQLITE_PRIVATE void *sqlite3Realloc(void*, int);
       
 10068 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
       
 10069 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int);
       
 10070 SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
       
 10071 SQLITE_PRIVATE int sqlite3MallocSize(void*);
       
 10072 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
       
 10073 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
       
 10074 SQLITE_PRIVATE void sqlite3ScratchFree(void*);
       
 10075 SQLITE_PRIVATE void *sqlite3PageMalloc(int);
       
 10076 SQLITE_PRIVATE void sqlite3PageFree(void*);
       
 10077 SQLITE_PRIVATE void sqlite3MemSetDefault(void);
       
 10078 SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
       
 10079 SQLITE_PRIVATE int sqlite3MemoryAlarm(void (*)(void*, sqlite3_int64, int), void*, sqlite3_int64);
       
 10080 
       
 10081 /*
       
 10082 ** On systems with ample stack space and that support alloca(), make
       
 10083 ** use of alloca() to obtain space for large automatic objects.  By default,
       
 10084 ** obtain space from malloc().
       
 10085 **
       
 10086 ** The alloca() routine never returns NULL.  This will cause code paths
       
 10087 ** that deal with sqlite3StackAlloc() failures to be unreachable.
       
 10088 */
       
 10089 #ifdef SQLITE_USE_ALLOCA
       
 10090 # define sqlite3StackAllocRaw(D,N)   alloca(N)
       
 10091 # define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
       
 10092 # define sqlite3StackFree(D,P)       
       
 10093 #else
       
 10094 # define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
       
 10095 # define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
       
 10096 # define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
       
 10097 #endif
       
 10098 
       
 10099 #ifdef SQLITE_ENABLE_MEMSYS3
       
 10100 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
       
 10101 #endif
       
 10102 #ifdef SQLITE_ENABLE_MEMSYS5
       
 10103 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
       
 10104 #endif
       
 10105 
       
 10106 
       
 10107 #ifndef SQLITE_MUTEX_OMIT
       
 10108 SQLITE_PRIVATE   sqlite3_mutex_methods *sqlite3DefaultMutex(void);
       
 10109 SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
       
 10110 SQLITE_PRIVATE   int sqlite3MutexInit(void);
       
 10111 SQLITE_PRIVATE   int sqlite3MutexEnd(void);
       
 10112 #endif
       
 10113 
       
 10114 SQLITE_PRIVATE int sqlite3StatusValue(int);
       
 10115 SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
       
 10116 SQLITE_PRIVATE void sqlite3StatusSet(int, int);
       
 10117 
       
 10118 SQLITE_PRIVATE int sqlite3IsNaN(double);
       
 10119 
       
 10120 SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, int, const char*, va_list);
       
 10121 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
       
 10122 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
       
 10123 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
       
 10124 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
       
 10125 SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
       
 10126 #endif
       
 10127 #if defined(SQLITE_TEST)
       
 10128 SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
       
 10129 #endif
       
 10130 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
       
 10131 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
       
 10132 SQLITE_PRIVATE void sqlite3ErrorClear(Parse*);
       
 10133 SQLITE_PRIVATE int sqlite3Dequote(char*);
       
 10134 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
       
 10135 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
       
 10136 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
       
 10137 SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
       
 10138 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
       
 10139 SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
       
 10140 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
       
 10141 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
       
 10142 SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
       
 10143 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
       
 10144 SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
       
 10145 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
       
 10146 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
       
 10147 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
       
 10148 SQLITE_PRIVATE void sqlite3ExprClear(sqlite3*, Expr*);
       
 10149 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
       
 10150 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
       
 10151 SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
       
 10152 SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
       
 10153 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
       
 10154 SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
       
 10155 SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
       
 10156 SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
       
 10157 SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3*, int);
       
 10158 SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
       
 10159 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
       
 10160 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
       
 10161 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
       
 10162 SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
       
 10163 SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
       
 10164 SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
       
 10165 SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
       
 10166 SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
       
 10167 SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
       
 10168 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
       
 10169 SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
       
 10170 SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,Select*);
       
 10171 
       
 10172 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
       
 10173 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
       
 10174 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
       
 10175 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
       
 10176 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
       
 10177 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
       
 10178 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
       
 10179 
       
 10180 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
       
 10181 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
       
 10182 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
       
 10183 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
       
 10184 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
       
 10185 
       
 10186 SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
       
 10187 
       
 10188 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
       
 10189 SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
       
 10190 #else
       
 10191 # define sqlite3ViewGetColumnNames(A,B) 0
       
 10192 #endif
       
 10193 
       
 10194 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
       
 10195 SQLITE_PRIVATE void sqlite3DeleteTable(Table*);
       
 10196 #ifndef SQLITE_OMIT_AUTOINCREMENT
       
 10197 SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
       
 10198 SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
       
 10199 #else
       
 10200 # define sqlite3AutoincrementBegin(X)
       
 10201 # define sqlite3AutoincrementEnd(X)
       
 10202 #endif
       
 10203 SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
       
 10204 SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
       
 10205 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
       
 10206 SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
       
 10207 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
       
 10208 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
       
 10209 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
       
 10210                                       Token*, Select*, Expr*, IdList*);
       
 10211 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
       
 10212 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
       
 10213 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
       
 10214 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
       
 10215 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
       
 10216 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
       
 10217 SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
       
 10218                         Token*, int, int);
       
 10219 SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
       
 10220 SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
       
 10221 SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
       
 10222                          Expr*,ExprList*,int,Expr*,Expr*);
       
 10223 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
       
 10224 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
       
 10225 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
       
 10226 SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
       
 10227 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
       
 10228 SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
       
 10229 #endif
       
 10230 SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
       
 10231 SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
       
 10232 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**, u16);
       
 10233 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
       
 10234 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, int);
       
 10235 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
       
 10236 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
       
 10237 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
       
 10238 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
       
 10239 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
       
 10240 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int);
       
 10241 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
       
 10242 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
       
 10243 SQLITE_PRIVATE void sqlite3ExprHardCopy(Parse*,int,int);
       
 10244 SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
       
 10245 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
       
 10246 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
       
 10247 SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
       
 10248 SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*);
       
 10249 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int);
       
 10250 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
       
 10251 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
       
 10252 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
       
 10253 SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
       
 10254 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
       
 10255 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
       
 10256 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
       
 10257 SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
       
 10258 SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
       
 10259 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
       
 10260 SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
       
 10261 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
       
 10262 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
       
 10263 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
       
 10264 SQLITE_PRIVATE Expr *sqlite3CreateIdExpr(Parse *, const char*);
       
 10265 SQLITE_PRIVATE void sqlite3PrngSaveState(void);
       
 10266 SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
       
 10267 SQLITE_PRIVATE void sqlite3PrngResetState(void);
       
 10268 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*);
       
 10269 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
       
 10270 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
       
 10271 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
       
 10272 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
       
 10273 SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
       
 10274 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
       
 10275 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
       
 10276 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
       
 10277 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
       
 10278 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
       
 10279 SQLITE_PRIVATE int sqlite3IsRowid(const char*);
       
 10280 SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int, Trigger *, int);
       
 10281 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
       
 10282 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
       
 10283 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
       
 10284                                      int*,int,int,int,int,int*);
       
 10285 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
       
 10286 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
       
 10287 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
       
 10288 SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
       
 10289 SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
       
 10290 SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, char*, int);
       
 10291 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
       
 10292 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
       
 10293 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
       
 10294 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
       
 10295 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
       
 10296 SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
       
 10297 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
       
 10298 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
       
 10299 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
       
 10300 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
       
 10301 #ifdef SQLITE_DEBUG
       
 10302 SQLITE_PRIVATE   int sqlite3SafetyOn(sqlite3*);
       
 10303 SQLITE_PRIVATE   int sqlite3SafetyOff(sqlite3*);
       
 10304 #else
       
 10305 # define sqlite3SafetyOn(A) 0
       
 10306 # define sqlite3SafetyOff(A) 0
       
 10307 #endif
       
 10308 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
       
 10309 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
       
 10310 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
       
 10311 
       
 10312 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
       
 10313 SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
       
 10314 #endif
       
 10315 
       
 10316 #ifndef SQLITE_OMIT_TRIGGER
       
 10317 SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
       
 10318                            Expr*,int, int);
       
 10319 SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
       
 10320 SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
       
 10321 SQLITE_PRIVATE   void sqlite3DropTriggerPtr(Parse*, Trigger*);
       
 10322 SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
       
 10323 SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
       
 10324 SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
       
 10325                             int, int, int);
       
 10326 SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
       
 10327   void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
       
 10328 SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
       
 10329 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
       
 10330 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
       
 10331                                         ExprList*,Select*,u8);
       
 10332 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
       
 10333 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
       
 10334 SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
       
 10335 SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
       
 10336 SQLITE_PRIVATE   u32 sqlite3TriggerOldmask(Parse*,Trigger*,ExprList*,Table*,int);
       
 10337 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
       
 10338 #else
       
 10339 # define sqlite3TriggersExist(B,C,D,E,F) 0
       
 10340 # define sqlite3DeleteTrigger(A,B)
       
 10341 # define sqlite3DropTriggerPtr(A,B)
       
 10342 # define sqlite3UnlinkAndDeleteTrigger(A,B,C)
       
 10343 # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
       
 10344 # define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
       
 10345 # define sqlite3TriggerList(X, Y) 0
       
 10346 # define sqlite3ParseToplevel(p) p
       
 10347 # define sqlite3TriggerOldmask(A,B,C,D,E) 0
       
 10348 #endif
       
 10349 
       
 10350 SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
       
 10351 SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
       
 10352 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
       
 10353 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 10354 SQLITE_PRIVATE   void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
       
 10355 SQLITE_PRIVATE   int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
       
 10356 SQLITE_PRIVATE   void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
       
 10357 SQLITE_PRIVATE   void sqlite3AuthContextPop(AuthContext*);
       
 10358 SQLITE_PRIVATE   int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
       
 10359 #else
       
 10360 # define sqlite3AuthRead(a,b,c,d)
       
 10361 # define sqlite3AuthCheck(a,b,c,d,e)    SQLITE_OK
       
 10362 # define sqlite3AuthContextPush(a,b,c)
       
 10363 # define sqlite3AuthContextPop(a)  ((void)(a))
       
 10364 #endif
       
 10365 SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
       
 10366 SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
       
 10367 SQLITE_PRIVATE int sqlite3BtreeFactory(const sqlite3 *db, const char *zFilename,
       
 10368                        int omitJournal, int nCache, int flags, Btree **ppBtree);
       
 10369 SQLITE_PRIVATE int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
       
 10370 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
       
 10371 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
       
 10372 SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
       
 10373 SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
       
 10374 SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
       
 10375 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*);
       
 10376 SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
       
 10377 SQLITE_PRIVATE int sqlite3FitsIn64Bits(const char *, int);
       
 10378 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
       
 10379 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
       
 10380 SQLITE_PRIVATE int sqlite3Utf8Read(const u8*, const u8**);
       
 10381 
       
 10382 /*
       
 10383 ** Routines to read and write variable-length integers.  These used to
       
 10384 ** be defined locally, but now we use the varint routines in the util.c
       
 10385 ** file.  Code should use the MACRO forms below, as the Varint32 versions
       
 10386 ** are coded to assume the single byte case is already handled (which 
       
 10387 ** the MACRO form does).
       
 10388 */
       
 10389 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
       
 10390 SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char*, u32);
       
 10391 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
       
 10392 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
       
 10393 SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
       
 10394 
       
 10395 /*
       
 10396 ** The header of a record consists of a sequence variable-length integers.
       
 10397 ** These integers are almost always small and are encoded as a single byte.
       
 10398 ** The following macros take advantage this fact to provide a fast encode
       
 10399 ** and decode of the integers in a record header.  It is faster for the common
       
 10400 ** case where the integer is a single byte.  It is a little slower when the
       
 10401 ** integer is two or more bytes.  But overall it is faster.
       
 10402 **
       
 10403 ** The following expressions are equivalent:
       
 10404 **
       
 10405 **     x = sqlite3GetVarint32( A, &B );
       
 10406 **     x = sqlite3PutVarint32( A, B );
       
 10407 **
       
 10408 **     x = getVarint32( A, B );
       
 10409 **     x = putVarint32( A, B );
       
 10410 **
       
 10411 */
       
 10412 #define getVarint32(A,B)  (u8)((*(A)<(u8)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), (u32 *)&(B)))
       
 10413 #define putVarint32(A,B)  (u8)(((u32)(B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
       
 10414 #define getVarint    sqlite3GetVarint
       
 10415 #define putVarint    sqlite3PutVarint
       
 10416 
       
 10417 
       
 10418 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
       
 10419 SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *, Table *);
       
 10420 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
       
 10421 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
       
 10422 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
       
 10423 SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*);
       
 10424 SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
       
 10425 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
       
 10426 SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
       
 10427 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
       
 10428 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
       
 10429 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
       
 10430 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
       
 10431 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
       
 10432 SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Parse *pParse, Expr *, Token *);
       
 10433 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
       
 10434 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
       
 10435 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
       
 10436 
       
 10437 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
       
 10438 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
       
 10439 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
       
 10440                         void(*)(void*));
       
 10441 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
       
 10442 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
       
 10443 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int);
       
 10444 #ifdef SQLITE_ENABLE_STAT2
       
 10445 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
       
 10446 #endif
       
 10447 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
       
 10448 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
       
 10449 #ifndef SQLITE_AMALGAMATION
       
 10450 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
       
 10451 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
       
 10452 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
       
 10453 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
       
 10454 SQLITE_PRIVATE int sqlite3PendingByte;
       
 10455 #endif
       
 10456 SQLITE_PRIVATE void sqlite3RootPageMoved(Db*, int, int);
       
 10457 SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
       
 10458 SQLITE_PRIVATE void sqlite3AlterFunctions(sqlite3*);
       
 10459 SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
       
 10460 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
       
 10461 SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
       
 10462 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
       
 10463 SQLITE_PRIVATE void sqlite3CodeSubselect(Parse *, Expr *, int, int);
       
 10464 SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
       
 10465 SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
       
 10466 SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
       
 10467 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
       
 10468 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
       
 10469 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
       
 10470 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
       
 10471 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(sqlite3*, u8, CollSeq *, const char*);
       
 10472 SQLITE_PRIVATE char sqlite3AffinityType(const char*);
       
 10473 SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
       
 10474 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
       
 10475 SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
       
 10476 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
       
 10477 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
       
 10478 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(Index*);
       
 10479 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
       
 10480 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
       
 10481 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
       
 10482 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
       
 10483 SQLITE_PRIVATE void sqlite3SchemaFree(void *);
       
 10484 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
       
 10485 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
       
 10486 SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
       
 10487 SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, 
       
 10488   void (*)(sqlite3_context*,int,sqlite3_value **),
       
 10489   void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*));
       
 10490 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
       
 10491 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
       
 10492 
       
 10493 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
       
 10494 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
       
 10495 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
       
 10496 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
       
 10497 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
       
 10498 
       
 10499 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
       
 10500 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
       
 10501 
       
 10502 /*
       
 10503 ** The interface to the LEMON-generated parser
       
 10504 */
       
 10505 SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
       
 10506 SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
       
 10507 SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
       
 10508 #ifdef YYTRACKMAXSTACKDEPTH
       
 10509 SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
       
 10510 #endif
       
 10511 
       
 10512 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
       
 10513 #ifndef SQLITE_OMIT_LOAD_EXTENSION
       
 10514 SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
       
 10515 #else
       
 10516 # define sqlite3CloseExtensions(X)
       
 10517 #endif
       
 10518 
       
 10519 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 10520 SQLITE_PRIVATE   void sqlite3TableLock(Parse *, int, int, u8, const char *);
       
 10521 #else
       
 10522   #define sqlite3TableLock(v,w,x,y,z)
       
 10523 #endif
       
 10524 
       
 10525 #ifdef SQLITE_TEST
       
 10526 SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
       
 10527 #endif
       
 10528 
       
 10529 #ifdef SQLITE_OMIT_VIRTUALTABLE
       
 10530 #  define sqlite3VtabClear(Y)
       
 10531 #  define sqlite3VtabSync(X,Y) SQLITE_OK
       
 10532 #  define sqlite3VtabRollback(X)
       
 10533 #  define sqlite3VtabCommit(X)
       
 10534 #  define sqlite3VtabInSync(db) 0
       
 10535 #  define sqlite3VtabLock(X) 
       
 10536 #  define sqlite3VtabUnlock(X)
       
 10537 #  define sqlite3VtabUnlockList(X)
       
 10538 #else
       
 10539 SQLITE_PRIVATE    void sqlite3VtabClear(Table*);
       
 10540 SQLITE_PRIVATE    int sqlite3VtabSync(sqlite3 *db, char **);
       
 10541 SQLITE_PRIVATE    int sqlite3VtabRollback(sqlite3 *db);
       
 10542 SQLITE_PRIVATE    int sqlite3VtabCommit(sqlite3 *db);
       
 10543 SQLITE_PRIVATE    void sqlite3VtabLock(VTable *);
       
 10544 SQLITE_PRIVATE    void sqlite3VtabUnlock(VTable *);
       
 10545 SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
       
 10546 #  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
       
 10547 #endif
       
 10548 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
       
 10549 SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
       
 10550 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
       
 10551 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
       
 10552 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
       
 10553 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
       
 10554 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
       
 10555 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
       
 10556 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
       
 10557 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
       
 10558 SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
       
 10559 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
       
 10560 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
       
 10561 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
       
 10562 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
       
 10563 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
       
 10564 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
       
 10565 
       
 10566 /* Declarations for functions in fkey.c. All of these are replaced by
       
 10567 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
       
 10568 ** key functionality is available. If OMIT_TRIGGER is defined but
       
 10569 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
       
 10570 ** this case foreign keys are parsed, but no other functionality is 
       
 10571 ** provided (enforcement of FK constraints requires the triggers sub-system).
       
 10572 */
       
 10573 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
       
 10574 SQLITE_PRIVATE   void sqlite3FkCheck(Parse*, Table*, int, int);
       
 10575 SQLITE_PRIVATE   void sqlite3FkDropTable(Parse*, SrcList *, Table*);
       
 10576 SQLITE_PRIVATE   void sqlite3FkActions(Parse*, Table*, ExprList*, int);
       
 10577 SQLITE_PRIVATE   int sqlite3FkRequired(Parse*, Table*, int*, int);
       
 10578 SQLITE_PRIVATE   u32 sqlite3FkOldmask(Parse*, Table*);
       
 10579 SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
       
 10580 #else
       
 10581   #define sqlite3FkActions(a,b,c,d)
       
 10582   #define sqlite3FkCheck(a,b,c,d)
       
 10583   #define sqlite3FkDropTable(a,b,c)
       
 10584   #define sqlite3FkOldmask(a,b)      0
       
 10585   #define sqlite3FkRequired(a,b,c,d) 0
       
 10586 #endif
       
 10587 #ifndef SQLITE_OMIT_FOREIGN_KEY
       
 10588 SQLITE_PRIVATE   void sqlite3FkDelete(Table*);
       
 10589 #else
       
 10590   #define sqlite3FkDelete(a)
       
 10591 #endif
       
 10592 
       
 10593 
       
 10594 /*
       
 10595 ** Available fault injectors.  Should be numbered beginning with 0.
       
 10596 */
       
 10597 #define SQLITE_FAULTINJECTOR_MALLOC     0
       
 10598 #define SQLITE_FAULTINJECTOR_COUNT      1
       
 10599 
       
 10600 /*
       
 10601 ** The interface to the code in fault.c used for identifying "benign"
       
 10602 ** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
       
 10603 ** is not defined.
       
 10604 */
       
 10605 #ifndef SQLITE_OMIT_BUILTIN_TEST
       
 10606 SQLITE_PRIVATE   void sqlite3BeginBenignMalloc(void);
       
 10607 SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
       
 10608 #else
       
 10609   #define sqlite3BeginBenignMalloc()
       
 10610   #define sqlite3EndBenignMalloc()
       
 10611 #endif
       
 10612 
       
 10613 #define IN_INDEX_ROWID           1
       
 10614 #define IN_INDEX_EPH             2
       
 10615 #define IN_INDEX_INDEX           3
       
 10616 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
       
 10617 
       
 10618 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
       
 10619 SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
       
 10620 SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
       
 10621 SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
       
 10622 #else
       
 10623   #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
       
 10624 #endif
       
 10625 
       
 10626 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
       
 10627 SQLITE_PRIVATE int sqlite3MemJournalSize(void);
       
 10628 SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
       
 10629 
       
 10630 #if SQLITE_MAX_EXPR_DEPTH>0
       
 10631 SQLITE_PRIVATE   void sqlite3ExprSetHeight(Parse *pParse, Expr *p);
       
 10632 SQLITE_PRIVATE   int sqlite3SelectExprHeight(Select *);
       
 10633 SQLITE_PRIVATE   int sqlite3ExprCheckHeight(Parse*, int);
       
 10634 #else
       
 10635   #define sqlite3ExprSetHeight(x,y)
       
 10636   #define sqlite3SelectExprHeight(x) 0
       
 10637   #define sqlite3ExprCheckHeight(x,y)
       
 10638 #endif
       
 10639 
       
 10640 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
       
 10641 SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
       
 10642 
       
 10643 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
       
 10644 SQLITE_PRIVATE   void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
       
 10645 SQLITE_PRIVATE   void sqlite3ConnectionUnlocked(sqlite3 *db);
       
 10646 SQLITE_PRIVATE   void sqlite3ConnectionClosed(sqlite3 *db);
       
 10647 #else
       
 10648   #define sqlite3ConnectionBlocked(x,y)
       
 10649   #define sqlite3ConnectionUnlocked(x)
       
 10650   #define sqlite3ConnectionClosed(x)
       
 10651 #endif
       
 10652 
       
 10653 #ifdef SQLITE_DEBUG
       
 10654 SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
       
 10655 #endif
       
 10656 
       
 10657 /*
       
 10658 ** If the SQLITE_ENABLE IOTRACE exists then the global variable
       
 10659 ** sqlite3IoTrace is a pointer to a printf-like routine used to
       
 10660 ** print I/O tracing messages. 
       
 10661 */
       
 10662 #ifdef SQLITE_ENABLE_IOTRACE
       
 10663 # define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
       
 10664 SQLITE_PRIVATE   void sqlite3VdbeIOTraceSql(Vdbe*);
       
 10665 SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*,...);
       
 10666 #else
       
 10667 # define IOTRACE(A)
       
 10668 # define sqlite3VdbeIOTraceSql(X)
       
 10669 #endif
       
 10670 
       
 10671 #endif
       
 10672 
       
 10673 /************** End of sqliteInt.h *******************************************/
       
 10674 /************** Begin file global.c ******************************************/
       
 10675 /*
       
 10676 ** 2008 June 13
       
 10677 **
       
 10678 ** The author disclaims copyright to this source code.  In place of
       
 10679 ** a legal notice, here is a blessing:
       
 10680 **
       
 10681 **    May you do good and not evil.
       
 10682 **    May you find forgiveness for yourself and forgive others.
       
 10683 **    May you share freely, never taking more than you give.
       
 10684 **
       
 10685 *************************************************************************
       
 10686 **
       
 10687 ** This file contains definitions of global variables and contants.
       
 10688 */
       
 10689 
       
 10690 
       
 10691 /* An array to map all upper-case characters into their corresponding
       
 10692 ** lower-case character. 
       
 10693 **
       
 10694 ** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
       
 10695 ** handle case conversions for the UTF character set since the tables
       
 10696 ** involved are nearly as big or bigger than SQLite itself.
       
 10697 */
       
 10698 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
       
 10699 #ifdef SQLITE_ASCII
       
 10700       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       
 10701      18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
       
 10702      36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
       
 10703      54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
       
 10704     104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
       
 10705     122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
       
 10706     108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
       
 10707     126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
       
 10708     144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
       
 10709     162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
       
 10710     180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
       
 10711     198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
       
 10712     216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
       
 10713     234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
       
 10714     252,253,254,255
       
 10715 #endif
       
 10716 #ifdef SQLITE_EBCDIC
       
 10717       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
       
 10718      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
       
 10719      32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
       
 10720      48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
       
 10721      64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
       
 10722      80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
       
 10723      96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
       
 10724     112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
       
 10725     128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
       
 10726     144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
       
 10727     160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
       
 10728     176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
       
 10729     192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
       
 10730     208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
       
 10731     224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
       
 10732     239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
       
 10733 #endif
       
 10734 };
       
 10735 
       
 10736 /*
       
 10737 ** The following 256 byte lookup table is used to support SQLites built-in
       
 10738 ** equivalents to the following standard library functions:
       
 10739 **
       
 10740 **   isspace()                        0x01
       
 10741 **   isalpha()                        0x02
       
 10742 **   isdigit()                        0x04
       
 10743 **   isalnum()                        0x06
       
 10744 **   isxdigit()                       0x08
       
 10745 **   toupper()                        0x20
       
 10746 **
       
 10747 ** Bit 0x20 is set if the mapped character requires translation to upper
       
 10748 ** case. i.e. if the character is a lower-case ASCII character.
       
 10749 ** If x is a lower-case ASCII character, then its upper-case equivalent
       
 10750 ** is (x - 0x20). Therefore toupper() can be implemented as:
       
 10751 **
       
 10752 **   (x & ~(map[x]&0x20))
       
 10753 **
       
 10754 ** Standard function tolower() is implemented using the sqlite3UpperToLower[]
       
 10755 ** array. tolower() is used more often than toupper() by SQLite.
       
 10756 **
       
 10757 ** SQLite's versions are identical to the standard versions assuming a
       
 10758 ** locale of "C". They are implemented as macros in sqliteInt.h.
       
 10759 */
       
 10760 #ifdef SQLITE_ASCII
       
 10761 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
       
 10762   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
       
 10763   0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
       
 10764   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
       
 10765   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
       
 10766   0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
       
 10767   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
       
 10768   0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
       
 10769   0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
       
 10770 
       
 10771   0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
       
 10772   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
       
 10773   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
       
 10774   0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 58..5f    XYZ[\]^_ */
       
 10775   0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
       
 10776   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
       
 10777   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
       
 10778   0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
       
 10779 
       
 10780   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 80..87    ........ */
       
 10781   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 88..8f    ........ */
       
 10782   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 90..97    ........ */
       
 10783   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 98..9f    ........ */
       
 10784   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* a0..a7    ........ */
       
 10785   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* a8..af    ........ */
       
 10786   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* b0..b7    ........ */
       
 10787   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* b8..bf    ........ */
       
 10788 
       
 10789   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* c0..c7    ........ */
       
 10790   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* c8..cf    ........ */
       
 10791   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* d0..d7    ........ */
       
 10792   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* d8..df    ........ */
       
 10793   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* e0..e7    ........ */
       
 10794   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* e8..ef    ........ */
       
 10795   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* f0..f7    ........ */
       
 10796   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00   /* f8..ff    ........ */
       
 10797 };
       
 10798 #endif
       
 10799 
       
 10800 
       
 10801 
       
 10802 /*
       
 10803 ** The following singleton contains the global configuration for
       
 10804 ** the SQLite library.
       
 10805 */
       
 10806 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
       
 10807    SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
       
 10808    1,                         /* bCoreMutex */
       
 10809    SQLITE_THREADSAFE==1,      /* bFullMutex */
       
 10810    0x7ffffffe,                /* mxStrlen */
       
 10811    100,                       /* szLookaside */
       
 10812    500,                       /* nLookaside */
       
 10813    {0,0,0,0,0,0,0,0},         /* m */
       
 10814    {0,0,0,0,0,0,0,0,0},       /* mutex */
       
 10815    {0,0,0,0,0,0,0,0,0,0,0},   /* pcache */
       
 10816    (void*)0,                  /* pHeap */
       
 10817    0,                         /* nHeap */
       
 10818    0, 0,                      /* mnHeap, mxHeap */
       
 10819    (void*)0,                  /* pScratch */
       
 10820    0,                         /* szScratch */
       
 10821    0,                         /* nScratch */
       
 10822    (void*)0,                  /* pPage */
       
 10823    0,                         /* szPage */
       
 10824    0,                         /* nPage */
       
 10825    0,                         /* mxParserStack */
       
 10826    0,                         /* sharedCacheEnabled */
       
 10827    /* All the rest should always be initialized to zero */
       
 10828    0,                         /* isInit */
       
 10829    0,                         /* inProgress */
       
 10830    0,                         /* isMutexInit */
       
 10831    0,                         /* isMallocInit */
       
 10832    0,                         /* isPCacheInit */
       
 10833    0,                         /* pInitMutex */
       
 10834    0,                         /* nRefInitMutex */
       
 10835 };
       
 10836 
       
 10837 
       
 10838 /*
       
 10839 ** Hash table for global functions - functions common to all
       
 10840 ** database connections.  After initialization, this table is
       
 10841 ** read-only.
       
 10842 */
       
 10843 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
       
 10844 
       
 10845 /*
       
 10846 ** The value of the "pending" byte must be 0x40000000 (1 byte past the
       
 10847 ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
       
 10848 ** the database page that contains the pending byte.  It never attempts
       
 10849 ** to read or write that page.  The pending byte page is set assign
       
 10850 ** for use by the VFS layers as space for managing file locks.
       
 10851 **
       
 10852 ** During testing, it is often desirable to move the pending byte to
       
 10853 ** a different position in the file.  This allows code that has to
       
 10854 ** deal with the pending byte to run on files that are much smaller
       
 10855 ** than 1 GiB.  The sqlite3_test_control() interface can be used to
       
 10856 ** move the pending byte.
       
 10857 **
       
 10858 ** IMPORTANT:  Changing the pending byte to any value other than
       
 10859 ** 0x40000000 results in an incompatible database file format!
       
 10860 ** Changing the pending byte during operating results in undefined
       
 10861 ** and dileterious behavior.
       
 10862 */
       
 10863 SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
       
 10864 
       
 10865 /************** End of global.c **********************************************/
       
 10866 /************** Begin file status.c ******************************************/
       
 10867 /*
       
 10868 ** 2008 June 18
       
 10869 **
       
 10870 ** The author disclaims copyright to this source code.  In place of
       
 10871 ** a legal notice, here is a blessing:
       
 10872 **
       
 10873 **    May you do good and not evil.
       
 10874 **    May you find forgiveness for yourself and forgive others.
       
 10875 **    May you share freely, never taking more than you give.
       
 10876 **
       
 10877 *************************************************************************
       
 10878 **
       
 10879 ** This module implements the sqlite3_status() interface and related
       
 10880 ** functionality.
       
 10881 **
       
 10882 ** $Id: status.c,v 1.9 2008/09/02 00:52:52 drh Exp $
       
 10883 */
       
 10884 
       
 10885 /*
       
 10886 ** Variables in which to record status information.
       
 10887 */
       
 10888 typedef struct sqlite3StatType sqlite3StatType;
       
 10889 static SQLITE_WSD struct sqlite3StatType {
       
 10890   int nowValue[9];         /* Current value */
       
 10891   int mxValue[9];          /* Maximum value */
       
 10892 } sqlite3Stat = { {0,}, {0,} };
       
 10893 
       
 10894 
       
 10895 /* The "wsdStat" macro will resolve to the status information
       
 10896 ** state vector.  If writable static data is unsupported on the target,
       
 10897 ** we have to locate the state vector at run-time.  In the more common
       
 10898 ** case where writable static data is supported, wsdStat can refer directly
       
 10899 ** to the "sqlite3Stat" state vector declared above.
       
 10900 */
       
 10901 #ifdef SQLITE_OMIT_WSD
       
 10902 # define wsdStatInit  sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
       
 10903 # define wsdStat x[0]
       
 10904 #else
       
 10905 # define wsdStatInit
       
 10906 # define wsdStat sqlite3Stat
       
 10907 #endif
       
 10908 
       
 10909 /*
       
 10910 ** Return the current value of a status parameter.
       
 10911 */
       
 10912 SQLITE_PRIVATE int sqlite3StatusValue(int op){
       
 10913   wsdStatInit;
       
 10914   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
       
 10915   return wsdStat.nowValue[op];
       
 10916 }
       
 10917 
       
 10918 /*
       
 10919 ** Add N to the value of a status record.  It is assumed that the
       
 10920 ** caller holds appropriate locks.
       
 10921 */
       
 10922 SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
       
 10923   wsdStatInit;
       
 10924   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
       
 10925   wsdStat.nowValue[op] += N;
       
 10926   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
       
 10927     wsdStat.mxValue[op] = wsdStat.nowValue[op];
       
 10928   }
       
 10929 }
       
 10930 
       
 10931 /*
       
 10932 ** Set the value of a status to X.
       
 10933 */
       
 10934 SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
       
 10935   wsdStatInit;
       
 10936   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
       
 10937   wsdStat.nowValue[op] = X;
       
 10938   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
       
 10939     wsdStat.mxValue[op] = wsdStat.nowValue[op];
       
 10940   }
       
 10941 }
       
 10942 
       
 10943 /*
       
 10944 ** Query status information.
       
 10945 **
       
 10946 ** This implementation assumes that reading or writing an aligned
       
 10947 ** 32-bit integer is an atomic operation.  If that assumption is not true,
       
 10948 ** then this routine is not threadsafe.
       
 10949 */
       
 10950 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
       
 10951   wsdStatInit;
       
 10952   if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
       
 10953     return SQLITE_MISUSE;
       
 10954   }
       
 10955   *pCurrent = wsdStat.nowValue[op];
       
 10956   *pHighwater = wsdStat.mxValue[op];
       
 10957   if( resetFlag ){
       
 10958     wsdStat.mxValue[op] = wsdStat.nowValue[op];
       
 10959   }
       
 10960   return SQLITE_OK;
       
 10961 }
       
 10962 
       
 10963 /*
       
 10964 ** Query status information for a single database connection
       
 10965 */
       
 10966 SQLITE_API int sqlite3_db_status(
       
 10967   sqlite3 *db,          /* The database connection whose status is desired */
       
 10968   int op,               /* Status verb */
       
 10969   int *pCurrent,        /* Write current value here */
       
 10970   int *pHighwater,      /* Write high-water mark here */
       
 10971   int resetFlag         /* Reset high-water mark if true */
       
 10972 ){
       
 10973   switch( op ){
       
 10974     case SQLITE_DBSTATUS_LOOKASIDE_USED: {
       
 10975       *pCurrent = db->lookaside.nOut;
       
 10976       *pHighwater = db->lookaside.mxOut;
       
 10977       if( resetFlag ){
       
 10978         db->lookaside.mxOut = db->lookaside.nOut;
       
 10979       }
       
 10980       break;
       
 10981     }
       
 10982     default: {
       
 10983       return SQLITE_ERROR;
       
 10984     }
       
 10985   }
       
 10986   return SQLITE_OK;
       
 10987 }
       
 10988 
       
 10989 /************** End of status.c **********************************************/
       
 10990 /************** Begin file date.c ********************************************/
       
 10991 /*
       
 10992 ** 2003 October 31
       
 10993 **
       
 10994 ** The author disclaims copyright to this source code.  In place of
       
 10995 ** a legal notice, here is a blessing:
       
 10996 **
       
 10997 **    May you do good and not evil.
       
 10998 **    May you find forgiveness for yourself and forgive others.
       
 10999 **    May you share freely, never taking more than you give.
       
 11000 **
       
 11001 *************************************************************************
       
 11002 ** This file contains the C functions that implement date and time
       
 11003 ** functions for SQLite.  
       
 11004 **
       
 11005 ** There is only one exported symbol in this file - the function
       
 11006 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
       
 11007 ** All other code has file scope.
       
 11008 **
       
 11009 ** $Id: date.c,v 1.107 2009/05/03 20:23:53 drh Exp $
       
 11010 **
       
 11011 ** SQLite processes all times and dates as Julian Day numbers.  The
       
 11012 ** dates and times are stored as the number of days since noon
       
 11013 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
       
 11014 ** calendar system. 
       
 11015 **
       
 11016 ** 1970-01-01 00:00:00 is JD 2440587.5
       
 11017 ** 2000-01-01 00:00:00 is JD 2451544.5
       
 11018 **
       
 11019 ** This implemention requires years to be expressed as a 4-digit number
       
 11020 ** which means that only dates between 0000-01-01 and 9999-12-31 can
       
 11021 ** be represented, even though julian day numbers allow a much wider
       
 11022 ** range of dates.
       
 11023 **
       
 11024 ** The Gregorian calendar system is used for all dates and times,
       
 11025 ** even those that predate the Gregorian calendar.  Historians usually
       
 11026 ** use the Julian calendar for dates prior to 1582-10-15 and for some
       
 11027 ** dates afterwards, depending on locale.  Beware of this difference.
       
 11028 **
       
 11029 ** The conversion algorithms are implemented based on descriptions
       
 11030 ** in the following text:
       
 11031 **
       
 11032 **      Jean Meeus
       
 11033 **      Astronomical Algorithms, 2nd Edition, 1998
       
 11034 **      ISBM 0-943396-61-1
       
 11035 **      Willmann-Bell, Inc
       
 11036 **      Richmond, Virginia (USA)
       
 11037 */
       
 11038 #include <time.h>
       
 11039 
       
 11040 #ifndef SQLITE_OMIT_DATETIME_FUNCS
       
 11041 
       
 11042 /*
       
 11043 ** On recent Windows platforms, the localtime_s() function is available
       
 11044 ** as part of the "Secure CRT". It is essentially equivalent to 
       
 11045 ** localtime_r() available under most POSIX platforms, except that the 
       
 11046 ** order of the parameters is reversed.
       
 11047 **
       
 11048 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
       
 11049 **
       
 11050 ** If the user has not indicated to use localtime_r() or localtime_s()
       
 11051 ** already, check for an MSVC build environment that provides 
       
 11052 ** localtime_s().
       
 11053 */
       
 11054 #if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
       
 11055      defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
       
 11056 #define HAVE_LOCALTIME_S 1
       
 11057 #endif
       
 11058 
       
 11059 /*
       
 11060 ** A structure for holding a single date and time.
       
 11061 */
       
 11062 typedef struct DateTime DateTime;
       
 11063 struct DateTime {
       
 11064   sqlite3_int64 iJD; /* The julian day number times 86400000 */
       
 11065   int Y, M, D;       /* Year, month, and day */
       
 11066   int h, m;          /* Hour and minutes */
       
 11067   int tz;            /* Timezone offset in minutes */
       
 11068   double s;          /* Seconds */
       
 11069   char validYMD;     /* True (1) if Y,M,D are valid */
       
 11070   char validHMS;     /* True (1) if h,m,s are valid */
       
 11071   char validJD;      /* True (1) if iJD is valid */
       
 11072   char validTZ;      /* True (1) if tz is valid */
       
 11073 };
       
 11074 
       
 11075 
       
 11076 /*
       
 11077 ** Convert zDate into one or more integers.  Additional arguments
       
 11078 ** come in groups of 5 as follows:
       
 11079 **
       
 11080 **       N       number of digits in the integer
       
 11081 **       min     minimum allowed value of the integer
       
 11082 **       max     maximum allowed value of the integer
       
 11083 **       nextC   first character after the integer
       
 11084 **       pVal    where to write the integers value.
       
 11085 **
       
 11086 ** Conversions continue until one with nextC==0 is encountered.
       
 11087 ** The function returns the number of successful conversions.
       
 11088 */
       
 11089 static int getDigits(const char *zDate, ...){
       
 11090   va_list ap;
       
 11091   int val;
       
 11092   int N;
       
 11093   int min;
       
 11094   int max;
       
 11095   int nextC;
       
 11096   int *pVal;
       
 11097   int cnt = 0;
       
 11098   va_start(ap, zDate);
       
 11099   do{
       
 11100     N = va_arg(ap, int);
       
 11101     min = va_arg(ap, int);
       
 11102     max = va_arg(ap, int);
       
 11103     nextC = va_arg(ap, int);
       
 11104     pVal = va_arg(ap, int*);
       
 11105     val = 0;
       
 11106     while( N-- ){
       
 11107       if( !sqlite3Isdigit(*zDate) ){
       
 11108         goto end_getDigits;
       
 11109       }
       
 11110       val = val*10 + *zDate - '0';
       
 11111       zDate++;
       
 11112     }
       
 11113     if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
       
 11114       goto end_getDigits;
       
 11115     }
       
 11116     *pVal = val;
       
 11117     zDate++;
       
 11118     cnt++;
       
 11119   }while( nextC );
       
 11120 end_getDigits:
       
 11121   va_end(ap);
       
 11122   return cnt;
       
 11123 }
       
 11124 
       
 11125 /*
       
 11126 ** Read text from z[] and convert into a floating point number.  Return
       
 11127 ** the number of digits converted.
       
 11128 */
       
 11129 #define getValue sqlite3AtoF
       
 11130 
       
 11131 /*
       
 11132 ** Parse a timezone extension on the end of a date-time.
       
 11133 ** The extension is of the form:
       
 11134 **
       
 11135 **        (+/-)HH:MM
       
 11136 **
       
 11137 ** Or the "zulu" notation:
       
 11138 **
       
 11139 **        Z
       
 11140 **
       
 11141 ** If the parse is successful, write the number of minutes
       
 11142 ** of change in p->tz and return 0.  If a parser error occurs,
       
 11143 ** return non-zero.
       
 11144 **
       
 11145 ** A missing specifier is not considered an error.
       
 11146 */
       
 11147 static int parseTimezone(const char *zDate, DateTime *p){
       
 11148   int sgn = 0;
       
 11149   int nHr, nMn;
       
 11150   int c;
       
 11151   while( sqlite3Isspace(*zDate) ){ zDate++; }
       
 11152   p->tz = 0;
       
 11153   c = *zDate;
       
 11154   if( c=='-' ){
       
 11155     sgn = -1;
       
 11156   }else if( c=='+' ){
       
 11157     sgn = +1;
       
 11158   }else if( c=='Z' || c=='z' ){
       
 11159     zDate++;
       
 11160     goto zulu_time;
       
 11161   }else{
       
 11162     return c!=0;
       
 11163   }
       
 11164   zDate++;
       
 11165   if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
       
 11166     return 1;
       
 11167   }
       
 11168   zDate += 5;
       
 11169   p->tz = sgn*(nMn + nHr*60);
       
 11170 zulu_time:
       
 11171   while( sqlite3Isspace(*zDate) ){ zDate++; }
       
 11172   return *zDate!=0;
       
 11173 }
       
 11174 
       
 11175 /*
       
 11176 ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
       
 11177 ** The HH, MM, and SS must each be exactly 2 digits.  The
       
 11178 ** fractional seconds FFFF can be one or more digits.
       
 11179 **
       
 11180 ** Return 1 if there is a parsing error and 0 on success.
       
 11181 */
       
 11182 static int parseHhMmSs(const char *zDate, DateTime *p){
       
 11183   int h, m, s;
       
 11184   double ms = 0.0;
       
 11185   if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
       
 11186     return 1;
       
 11187   }
       
 11188   zDate += 5;
       
 11189   if( *zDate==':' ){
       
 11190     zDate++;
       
 11191     if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
       
 11192       return 1;
       
 11193     }
       
 11194     zDate += 2;
       
 11195     if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
       
 11196       double rScale = 1.0;
       
 11197       zDate++;
       
 11198       while( sqlite3Isdigit(*zDate) ){
       
 11199         ms = ms*10.0 + *zDate - '0';
       
 11200         rScale *= 10.0;
       
 11201         zDate++;
       
 11202       }
       
 11203       ms /= rScale;
       
 11204     }
       
 11205   }else{
       
 11206     s = 0;
       
 11207   }
       
 11208   p->validJD = 0;
       
 11209   p->validHMS = 1;
       
 11210   p->h = h;
       
 11211   p->m = m;
       
 11212   p->s = s + ms;
       
 11213   if( parseTimezone(zDate, p) ) return 1;
       
 11214   p->validTZ = (p->tz!=0)?1:0;
       
 11215   return 0;
       
 11216 }
       
 11217 
       
 11218 /*
       
 11219 ** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
       
 11220 ** that the YYYY-MM-DD is according to the Gregorian calendar.
       
 11221 **
       
 11222 ** Reference:  Meeus page 61
       
 11223 */
       
 11224 static void computeJD(DateTime *p){
       
 11225   int Y, M, D, A, B, X1, X2;
       
 11226 
       
 11227   if( p->validJD ) return;
       
 11228   if( p->validYMD ){
       
 11229     Y = p->Y;
       
 11230     M = p->M;
       
 11231     D = p->D;
       
 11232   }else{
       
 11233     Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
       
 11234     M = 1;
       
 11235     D = 1;
       
 11236   }
       
 11237   if( M<=2 ){
       
 11238     Y--;
       
 11239     M += 12;
       
 11240   }
       
 11241   A = Y/100;
       
 11242   B = 2 - A + (A/4);
       
 11243   X1 = 36525*(Y+4716)/100;
       
 11244   X2 = 306001*(M+1)/10000;
       
 11245   p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
       
 11246   p->validJD = 1;
       
 11247   if( p->validHMS ){
       
 11248     p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
       
 11249     if( p->validTZ ){
       
 11250       p->iJD -= p->tz*60000;
       
 11251       p->validYMD = 0;
       
 11252       p->validHMS = 0;
       
 11253       p->validTZ = 0;
       
 11254     }
       
 11255   }
       
 11256 }
       
 11257 
       
 11258 /*
       
 11259 ** Parse dates of the form
       
 11260 **
       
 11261 **     YYYY-MM-DD HH:MM:SS.FFF
       
 11262 **     YYYY-MM-DD HH:MM:SS
       
 11263 **     YYYY-MM-DD HH:MM
       
 11264 **     YYYY-MM-DD
       
 11265 **
       
 11266 ** Write the result into the DateTime structure and return 0
       
 11267 ** on success and 1 if the input string is not a well-formed
       
 11268 ** date.
       
 11269 */
       
 11270 static int parseYyyyMmDd(const char *zDate, DateTime *p){
       
 11271   int Y, M, D, neg;
       
 11272 
       
 11273   if( zDate[0]=='-' ){
       
 11274     zDate++;
       
 11275     neg = 1;
       
 11276   }else{
       
 11277     neg = 0;
       
 11278   }
       
 11279   if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
       
 11280     return 1;
       
 11281   }
       
 11282   zDate += 10;
       
 11283   while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
       
 11284   if( parseHhMmSs(zDate, p)==0 ){
       
 11285     /* We got the time */
       
 11286   }else if( *zDate==0 ){
       
 11287     p->validHMS = 0;
       
 11288   }else{
       
 11289     return 1;
       
 11290   }
       
 11291   p->validJD = 0;
       
 11292   p->validYMD = 1;
       
 11293   p->Y = neg ? -Y : Y;
       
 11294   p->M = M;
       
 11295   p->D = D;
       
 11296   if( p->validTZ ){
       
 11297     computeJD(p);
       
 11298   }
       
 11299   return 0;
       
 11300 }
       
 11301 
       
 11302 /*
       
 11303 ** Set the time to the current time reported by the VFS
       
 11304 */
       
 11305 static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
       
 11306   double r;
       
 11307   sqlite3 *db = sqlite3_context_db_handle(context);
       
 11308   sqlite3OsCurrentTime(db->pVfs, &r);
       
 11309   p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
       
 11310   p->validJD = 1;
       
 11311 }
       
 11312 
       
 11313 /*
       
 11314 ** Attempt to parse the given string into a Julian Day Number.  Return
       
 11315 ** the number of errors.
       
 11316 **
       
 11317 ** The following are acceptable forms for the input string:
       
 11318 **
       
 11319 **      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
       
 11320 **      DDDD.DD 
       
 11321 **      now
       
 11322 **
       
 11323 ** In the first form, the +/-HH:MM is always optional.  The fractional
       
 11324 ** seconds extension (the ".FFF") is optional.  The seconds portion
       
 11325 ** (":SS.FFF") is option.  The year and date can be omitted as long
       
 11326 ** as there is a time string.  The time string can be omitted as long
       
 11327 ** as there is a year and date.
       
 11328 */
       
 11329 static int parseDateOrTime(
       
 11330   sqlite3_context *context, 
       
 11331   const char *zDate, 
       
 11332   DateTime *p
       
 11333 ){
       
 11334   int isRealNum;    /* Return from sqlite3IsNumber().  Not used */
       
 11335   if( parseYyyyMmDd(zDate,p)==0 ){
       
 11336     return 0;
       
 11337   }else if( parseHhMmSs(zDate, p)==0 ){
       
 11338     return 0;
       
 11339   }else if( sqlite3StrICmp(zDate,"now")==0){
       
 11340     setDateTimeToCurrent(context, p);
       
 11341     return 0;
       
 11342   }else if( sqlite3IsNumber(zDate, &isRealNum, SQLITE_UTF8) ){
       
 11343     double r;
       
 11344     getValue(zDate, &r);
       
 11345     p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
       
 11346     p->validJD = 1;
       
 11347     return 0;
       
 11348   }
       
 11349   return 1;
       
 11350 }
       
 11351 
       
 11352 /*
       
 11353 ** Compute the Year, Month, and Day from the julian day number.
       
 11354 */
       
 11355 static void computeYMD(DateTime *p){
       
 11356   int Z, A, B, C, D, E, X1;
       
 11357   if( p->validYMD ) return;
       
 11358   if( !p->validJD ){
       
 11359     p->Y = 2000;
       
 11360     p->M = 1;
       
 11361     p->D = 1;
       
 11362   }else{
       
 11363     Z = (int)((p->iJD + 43200000)/86400000);
       
 11364     A = (int)((Z - 1867216.25)/36524.25);
       
 11365     A = Z + 1 + A - (A/4);
       
 11366     B = A + 1524;
       
 11367     C = (int)((B - 122.1)/365.25);
       
 11368     D = (36525*C)/100;
       
 11369     E = (int)((B-D)/30.6001);
       
 11370     X1 = (int)(30.6001*E);
       
 11371     p->D = B - D - X1;
       
 11372     p->M = E<14 ? E-1 : E-13;
       
 11373     p->Y = p->M>2 ? C - 4716 : C - 4715;
       
 11374   }
       
 11375   p->validYMD = 1;
       
 11376 }
       
 11377 
       
 11378 /*
       
 11379 ** Compute the Hour, Minute, and Seconds from the julian day number.
       
 11380 */
       
 11381 static void computeHMS(DateTime *p){
       
 11382   int s;
       
 11383   if( p->validHMS ) return;
       
 11384   computeJD(p);
       
 11385   s = (int)((p->iJD + 43200000) % 86400000);
       
 11386   p->s = s/1000.0;
       
 11387   s = (int)p->s;
       
 11388   p->s -= s;
       
 11389   p->h = s/3600;
       
 11390   s -= p->h*3600;
       
 11391   p->m = s/60;
       
 11392   p->s += s - p->m*60;
       
 11393   p->validHMS = 1;
       
 11394 }
       
 11395 
       
 11396 /*
       
 11397 ** Compute both YMD and HMS
       
 11398 */
       
 11399 static void computeYMD_HMS(DateTime *p){
       
 11400   computeYMD(p);
       
 11401   computeHMS(p);
       
 11402 }
       
 11403 
       
 11404 /*
       
 11405 ** Clear the YMD and HMS and the TZ
       
 11406 */
       
 11407 static void clearYMD_HMS_TZ(DateTime *p){
       
 11408   p->validYMD = 0;
       
 11409   p->validHMS = 0;
       
 11410   p->validTZ = 0;
       
 11411 }
       
 11412 
       
 11413 #ifndef SQLITE_OMIT_LOCALTIME
       
 11414 /*
       
 11415 ** Windows CE does not declare the localtime
       
 11416 ** function as it is not defined anywhere.
       
 11417 ** Anyway we need the forward-declaration to be
       
 11418 ** able to define it later on.
       
 11419 */
       
 11420 #if defined(_WIN32_WCE) && (_WIN32_WCE >= 0x600)
       
 11421 struct tm *__cdecl localtime(const time_t *t);
       
 11422 #endif
       
 11423 
       
 11424 /*
       
 11425 ** Compute the difference (in milliseconds)
       
 11426 ** between localtime and UTC (a.k.a. GMT)
       
 11427 
       
 11428 ** for the time value p where p is in UTC.
       
 11429 */
       
 11430 static sqlite3_int64 localtimeOffset(DateTime *p){
       
 11431   DateTime x, y;
       
 11432   time_t t;
       
 11433   x = *p;
       
 11434   computeYMD_HMS(&x);
       
 11435   if( x.Y<1971 || x.Y>=2038 ){
       
 11436     x.Y = 2000;
       
 11437     x.M = 1;
       
 11438     x.D = 1;
       
 11439     x.h = 0;
       
 11440     x.m = 0;
       
 11441     x.s = 0.0;
       
 11442   } else {
       
 11443     int s = (int)(x.s + 0.5);
       
 11444     x.s = s;
       
 11445   }
       
 11446   x.tz = 0;
       
 11447   x.validJD = 0;
       
 11448   computeJD(&x);
       
 11449   t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
       
 11450 #ifdef HAVE_LOCALTIME_R
       
 11451   {
       
 11452     struct tm sLocal;
       
 11453     localtime_r(&t, &sLocal);
       
 11454     y.Y = sLocal.tm_year + 1900;
       
 11455     y.M = sLocal.tm_mon + 1;
       
 11456     y.D = sLocal.tm_mday;
       
 11457     y.h = sLocal.tm_hour;
       
 11458     y.m = sLocal.tm_min;
       
 11459     y.s = sLocal.tm_sec;
       
 11460   }
       
 11461 #elif defined(HAVE_LOCALTIME_S) && HAVE_LOCALTIME_S
       
 11462   {
       
 11463     struct tm sLocal;
       
 11464     localtime_s(&sLocal, &t);
       
 11465     y.Y = sLocal.tm_year + 1900;
       
 11466     y.M = sLocal.tm_mon + 1;
       
 11467     y.D = sLocal.tm_mday;
       
 11468     y.h = sLocal.tm_hour;
       
 11469     y.m = sLocal.tm_min;
       
 11470     y.s = sLocal.tm_sec;
       
 11471   }
       
 11472 #else
       
 11473   {
       
 11474     struct tm *pTm;
       
 11475     sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
       
 11476     pTm = localtime(&t);
       
 11477     y.Y = pTm->tm_year + 1900;
       
 11478     y.M = pTm->tm_mon + 1;
       
 11479     y.D = pTm->tm_mday;
       
 11480     y.h = pTm->tm_hour;
       
 11481     y.m = pTm->tm_min;
       
 11482     y.s = pTm->tm_sec;
       
 11483     sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
       
 11484   }
       
 11485 #endif
       
 11486   y.validYMD = 1;
       
 11487   y.validHMS = 1;
       
 11488   y.validJD = 0;
       
 11489   y.validTZ = 0;
       
 11490   computeJD(&y);
       
 11491   return y.iJD - x.iJD;
       
 11492 }
       
 11493 #endif /* SQLITE_OMIT_LOCALTIME */
       
 11494 
       
 11495 /*
       
 11496 ** Process a modifier to a date-time stamp.  The modifiers are
       
 11497 ** as follows:
       
 11498 **
       
 11499 **     NNN days
       
 11500 **     NNN hours
       
 11501 **     NNN minutes
       
 11502 **     NNN.NNNN seconds
       
 11503 **     NNN months
       
 11504 **     NNN years
       
 11505 **     start of month
       
 11506 **     start of year
       
 11507 **     start of week
       
 11508 **     start of day
       
 11509 **     weekday N
       
 11510 **     unixepoch
       
 11511 **     localtime
       
 11512 **     utc
       
 11513 **
       
 11514 ** Return 0 on success and 1 if there is any kind of error.
       
 11515 */
       
 11516 static int parseModifier(const char *zMod, DateTime *p){
       
 11517   int rc = 1;
       
 11518   int n;
       
 11519   double r;
       
 11520   char *z, zBuf[30];
       
 11521   z = zBuf;
       
 11522   for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
       
 11523     z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
       
 11524   }
       
 11525   z[n] = 0;
       
 11526   switch( z[0] ){
       
 11527 #ifndef SQLITE_OMIT_LOCALTIME
       
 11528     case 'l': {
       
 11529       /*    localtime
       
 11530       **
       
 11531       ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
       
 11532       ** show local time.
       
 11533       */
       
 11534       if( strcmp(z, "localtime")==0 ){
       
 11535         computeJD(p);
       
 11536         p->iJD += localtimeOffset(p);
       
 11537         clearYMD_HMS_TZ(p);
       
 11538         rc = 0;
       
 11539       }
       
 11540       break;
       
 11541     }
       
 11542 #endif
       
 11543     case 'u': {
       
 11544       /*
       
 11545       **    unixepoch
       
 11546       **
       
 11547       ** Treat the current value of p->iJD as the number of
       
 11548       ** seconds since 1970.  Convert to a real julian day number.
       
 11549       */
       
 11550       if( strcmp(z, "unixepoch")==0 && p->validJD ){
       
 11551         p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
       
 11552         clearYMD_HMS_TZ(p);
       
 11553         rc = 0;
       
 11554       }
       
 11555 #ifndef SQLITE_OMIT_LOCALTIME
       
 11556       else if( strcmp(z, "utc")==0 ){
       
 11557         sqlite3_int64 c1;
       
 11558         computeJD(p);
       
 11559         c1 = localtimeOffset(p);
       
 11560         p->iJD -= c1;
       
 11561         clearYMD_HMS_TZ(p);
       
 11562         p->iJD += c1 - localtimeOffset(p);
       
 11563         rc = 0;
       
 11564       }
       
 11565 #endif
       
 11566       break;
       
 11567     }
       
 11568     case 'w': {
       
 11569       /*
       
 11570       **    weekday N
       
 11571       **
       
 11572       ** Move the date to the same time on the next occurrence of
       
 11573       ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
       
 11574       ** date is already on the appropriate weekday, this is a no-op.
       
 11575       */
       
 11576       if( strncmp(z, "weekday ", 8)==0 && getValue(&z[8],&r)>0
       
 11577                  && (n=(int)r)==r && n>=0 && r<7 ){
       
 11578         sqlite3_int64 Z;
       
 11579         computeYMD_HMS(p);
       
 11580         p->validTZ = 0;
       
 11581         p->validJD = 0;
       
 11582         computeJD(p);
       
 11583         Z = ((p->iJD + 129600000)/86400000) % 7;
       
 11584         if( Z>n ) Z -= 7;
       
 11585         p->iJD += (n - Z)*86400000;
       
 11586         clearYMD_HMS_TZ(p);
       
 11587         rc = 0;
       
 11588       }
       
 11589       break;
       
 11590     }
       
 11591     case 's': {
       
 11592       /*
       
 11593       **    start of TTTTT
       
 11594       **
       
 11595       ** Move the date backwards to the beginning of the current day,
       
 11596       ** or month or year.
       
 11597       */
       
 11598       if( strncmp(z, "start of ", 9)!=0 ) break;
       
 11599       z += 9;
       
 11600       computeYMD(p);
       
 11601       p->validHMS = 1;
       
 11602       p->h = p->m = 0;
       
 11603       p->s = 0.0;
       
 11604       p->validTZ = 0;
       
 11605       p->validJD = 0;
       
 11606       if( strcmp(z,"month")==0 ){
       
 11607         p->D = 1;
       
 11608         rc = 0;
       
 11609       }else if( strcmp(z,"year")==0 ){
       
 11610         computeYMD(p);
       
 11611         p->M = 1;
       
 11612         p->D = 1;
       
 11613         rc = 0;
       
 11614       }else if( strcmp(z,"day")==0 ){
       
 11615         rc = 0;
       
 11616       }
       
 11617       break;
       
 11618     }
       
 11619     case '+':
       
 11620     case '-':
       
 11621     case '0':
       
 11622     case '1':
       
 11623     case '2':
       
 11624     case '3':
       
 11625     case '4':
       
 11626     case '5':
       
 11627     case '6':
       
 11628     case '7':
       
 11629     case '8':
       
 11630     case '9': {
       
 11631       double rRounder;
       
 11632       n = getValue(z, &r);
       
 11633       assert( n>=1 );
       
 11634       if( z[n]==':' ){
       
 11635         /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
       
 11636         ** specified number of hours, minutes, seconds, and fractional seconds
       
 11637         ** to the time.  The ".FFF" may be omitted.  The ":SS.FFF" may be
       
 11638         ** omitted.
       
 11639         */
       
 11640         const char *z2 = z;
       
 11641         DateTime tx;
       
 11642         sqlite3_int64 day;
       
 11643         if( !sqlite3Isdigit(*z2) ) z2++;
       
 11644         memset(&tx, 0, sizeof(tx));
       
 11645         if( parseHhMmSs(z2, &tx) ) break;
       
 11646         computeJD(&tx);
       
 11647         tx.iJD -= 43200000;
       
 11648         day = tx.iJD/86400000;
       
 11649         tx.iJD -= day*86400000;
       
 11650         if( z[0]=='-' ) tx.iJD = -tx.iJD;
       
 11651         computeJD(p);
       
 11652         clearYMD_HMS_TZ(p);
       
 11653         p->iJD += tx.iJD;
       
 11654         rc = 0;
       
 11655         break;
       
 11656       }
       
 11657       z += n;
       
 11658       while( sqlite3Isspace(*z) ) z++;
       
 11659       n = sqlite3Strlen30(z);
       
 11660       if( n>10 || n<3 ) break;
       
 11661       if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
       
 11662       computeJD(p);
       
 11663       rc = 0;
       
 11664       rRounder = r<0 ? -0.5 : +0.5;
       
 11665       if( n==3 && strcmp(z,"day")==0 ){
       
 11666         p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
       
 11667       }else if( n==4 && strcmp(z,"hour")==0 ){
       
 11668         p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
       
 11669       }else if( n==6 && strcmp(z,"minute")==0 ){
       
 11670         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
       
 11671       }else if( n==6 && strcmp(z,"second")==0 ){
       
 11672         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
       
 11673       }else if( n==5 && strcmp(z,"month")==0 ){
       
 11674         int x, y;
       
 11675         computeYMD_HMS(p);
       
 11676         p->M += (int)r;
       
 11677         x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
       
 11678         p->Y += x;
       
 11679         p->M -= x*12;
       
 11680         p->validJD = 0;
       
 11681         computeJD(p);
       
 11682         y = (int)r;
       
 11683         if( y!=r ){
       
 11684           p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
       
 11685         }
       
 11686       }else if( n==4 && strcmp(z,"year")==0 ){
       
 11687         int y = (int)r;
       
 11688         computeYMD_HMS(p);
       
 11689         p->Y += y;
       
 11690         p->validJD = 0;
       
 11691         computeJD(p);
       
 11692         if( y!=r ){
       
 11693           p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
       
 11694         }
       
 11695       }else{
       
 11696         rc = 1;
       
 11697       }
       
 11698       clearYMD_HMS_TZ(p);
       
 11699       break;
       
 11700     }
       
 11701     default: {
       
 11702       break;
       
 11703     }
       
 11704   }
       
 11705   return rc;
       
 11706 }
       
 11707 
       
 11708 /*
       
 11709 ** Process time function arguments.  argv[0] is a date-time stamp.
       
 11710 ** argv[1] and following are modifiers.  Parse them all and write
       
 11711 ** the resulting time into the DateTime structure p.  Return 0
       
 11712 ** on success and 1 if there are any errors.
       
 11713 **
       
 11714 ** If there are zero parameters (if even argv[0] is undefined)
       
 11715 ** then assume a default value of "now" for argv[0].
       
 11716 */
       
 11717 static int isDate(
       
 11718   sqlite3_context *context, 
       
 11719   int argc, 
       
 11720   sqlite3_value **argv, 
       
 11721   DateTime *p
       
 11722 ){
       
 11723   int i;
       
 11724   const unsigned char *z;
       
 11725   int eType;
       
 11726   memset(p, 0, sizeof(*p));
       
 11727   if( argc==0 ){
       
 11728     setDateTimeToCurrent(context, p);
       
 11729   }else if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
       
 11730                    || eType==SQLITE_INTEGER ){
       
 11731     p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
       
 11732     p->validJD = 1;
       
 11733   }else{
       
 11734     z = sqlite3_value_text(argv[0]);
       
 11735     if( !z || parseDateOrTime(context, (char*)z, p) ){
       
 11736       return 1;
       
 11737     }
       
 11738   }
       
 11739   for(i=1; i<argc; i++){
       
 11740     if( (z = sqlite3_value_text(argv[i]))==0 || parseModifier((char*)z, p) ){
       
 11741       return 1;
       
 11742     }
       
 11743   }
       
 11744   return 0;
       
 11745 }
       
 11746 
       
 11747 
       
 11748 /*
       
 11749 ** The following routines implement the various date and time functions
       
 11750 ** of SQLite.
       
 11751 */
       
 11752 
       
 11753 /*
       
 11754 **    julianday( TIMESTRING, MOD, MOD, ...)
       
 11755 **
       
 11756 ** Return the julian day number of the date specified in the arguments
       
 11757 */
       
 11758 static void juliandayFunc(
       
 11759   sqlite3_context *context,
       
 11760   int argc,
       
 11761   sqlite3_value **argv
       
 11762 ){
       
 11763   DateTime x;
       
 11764   if( isDate(context, argc, argv, &x)==0 ){
       
 11765     computeJD(&x);
       
 11766     sqlite3_result_double(context, x.iJD/86400000.0);
       
 11767   }
       
 11768 }
       
 11769 
       
 11770 /*
       
 11771 **    datetime( TIMESTRING, MOD, MOD, ...)
       
 11772 **
       
 11773 ** Return YYYY-MM-DD HH:MM:SS
       
 11774 */
       
 11775 static void datetimeFunc(
       
 11776   sqlite3_context *context,
       
 11777   int argc,
       
 11778   sqlite3_value **argv
       
 11779 ){
       
 11780   DateTime x;
       
 11781   if( isDate(context, argc, argv, &x)==0 ){
       
 11782     char zBuf[100];
       
 11783     computeYMD_HMS(&x);
       
 11784     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
       
 11785                      x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
       
 11786     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
       
 11787   }
       
 11788 }
       
 11789 
       
 11790 /*
       
 11791 **    time( TIMESTRING, MOD, MOD, ...)
       
 11792 **
       
 11793 ** Return HH:MM:SS
       
 11794 */
       
 11795 static void timeFunc(
       
 11796   sqlite3_context *context,
       
 11797   int argc,
       
 11798   sqlite3_value **argv
       
 11799 ){
       
 11800   DateTime x;
       
 11801   if( isDate(context, argc, argv, &x)==0 ){
       
 11802     char zBuf[100];
       
 11803     computeHMS(&x);
       
 11804     sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
       
 11805     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
       
 11806   }
       
 11807 }
       
 11808 
       
 11809 /*
       
 11810 **    date( TIMESTRING, MOD, MOD, ...)
       
 11811 **
       
 11812 ** Return YYYY-MM-DD
       
 11813 */
       
 11814 static void dateFunc(
       
 11815   sqlite3_context *context,
       
 11816   int argc,
       
 11817   sqlite3_value **argv
       
 11818 ){
       
 11819   DateTime x;
       
 11820   if( isDate(context, argc, argv, &x)==0 ){
       
 11821     char zBuf[100];
       
 11822     computeYMD(&x);
       
 11823     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
       
 11824     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
       
 11825   }
       
 11826 }
       
 11827 
       
 11828 /*
       
 11829 **    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
       
 11830 **
       
 11831 ** Return a string described by FORMAT.  Conversions as follows:
       
 11832 **
       
 11833 **   %d  day of month
       
 11834 **   %f  ** fractional seconds  SS.SSS
       
 11835 **   %H  hour 00-24
       
 11836 **   %j  day of year 000-366
       
 11837 **   %J  ** Julian day number
       
 11838 **   %m  month 01-12
       
 11839 **   %M  minute 00-59
       
 11840 **   %s  seconds since 1970-01-01
       
 11841 **   %S  seconds 00-59
       
 11842 **   %w  day of week 0-6  sunday==0
       
 11843 **   %W  week of year 00-53
       
 11844 **   %Y  year 0000-9999
       
 11845 **   %%  %
       
 11846 */
       
 11847 static void strftimeFunc(
       
 11848   sqlite3_context *context,
       
 11849   int argc,
       
 11850   sqlite3_value **argv
       
 11851 ){
       
 11852   DateTime x;
       
 11853   u64 n;
       
 11854   size_t i,j;
       
 11855   char *z;
       
 11856   sqlite3 *db;
       
 11857   const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
       
 11858   char zBuf[100];
       
 11859   if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
       
 11860   db = sqlite3_context_db_handle(context);
       
 11861   for(i=0, n=1; zFmt[i]; i++, n++){
       
 11862     if( zFmt[i]=='%' ){
       
 11863       switch( zFmt[i+1] ){
       
 11864         case 'd':
       
 11865         case 'H':
       
 11866         case 'm':
       
 11867         case 'M':
       
 11868         case 'S':
       
 11869         case 'W':
       
 11870           n++;
       
 11871           /* fall thru */
       
 11872         case 'w':
       
 11873         case '%':
       
 11874           break;
       
 11875         case 'f':
       
 11876           n += 8;
       
 11877           break;
       
 11878         case 'j':
       
 11879           n += 3;
       
 11880           break;
       
 11881         case 'Y':
       
 11882           n += 8;
       
 11883           break;
       
 11884         case 's':
       
 11885         case 'J':
       
 11886           n += 50;
       
 11887           break;
       
 11888         default:
       
 11889           return;  /* ERROR.  return a NULL */
       
 11890       }
       
 11891       i++;
       
 11892     }
       
 11893   }
       
 11894   testcase( n==sizeof(zBuf)-1 );
       
 11895   testcase( n==sizeof(zBuf) );
       
 11896   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
       
 11897   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
       
 11898   if( n<sizeof(zBuf) ){
       
 11899     z = zBuf;
       
 11900   }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
       
 11901     sqlite3_result_error_toobig(context);
       
 11902     return;
       
 11903   }else{
       
 11904     z = sqlite3DbMallocRaw(db, (int)n);
       
 11905     if( z==0 ){
       
 11906       sqlite3_result_error_nomem(context);
       
 11907       return;
       
 11908     }
       
 11909   }
       
 11910   computeJD(&x);
       
 11911   computeYMD_HMS(&x);
       
 11912   for(i=j=0; zFmt[i]; i++){
       
 11913     if( zFmt[i]!='%' ){
       
 11914       z[j++] = zFmt[i];
       
 11915     }else{
       
 11916       i++;
       
 11917       switch( zFmt[i] ){
       
 11918         case 'd':  sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
       
 11919         case 'f': {
       
 11920           double s = x.s;
       
 11921           if( s>59.999 ) s = 59.999;
       
 11922           sqlite3_snprintf(7, &z[j],"%06.3f", s);
       
 11923           j += sqlite3Strlen30(&z[j]);
       
 11924           break;
       
 11925         }
       
 11926         case 'H':  sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
       
 11927         case 'W': /* Fall thru */
       
 11928         case 'j': {
       
 11929           int nDay;             /* Number of days since 1st day of year */
       
 11930           DateTime y = x;
       
 11931           y.validJD = 0;
       
 11932           y.M = 1;
       
 11933           y.D = 1;
       
 11934           computeJD(&y);
       
 11935           nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
       
 11936           if( zFmt[i]=='W' ){
       
 11937             int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
       
 11938             wd = (int)(((x.iJD+43200000)/86400000)%7);
       
 11939             sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
       
 11940             j += 2;
       
 11941           }else{
       
 11942             sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
       
 11943             j += 3;
       
 11944           }
       
 11945           break;
       
 11946         }
       
 11947         case 'J': {
       
 11948           sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
       
 11949           j+=sqlite3Strlen30(&z[j]);
       
 11950           break;
       
 11951         }
       
 11952         case 'm':  sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
       
 11953         case 'M':  sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
       
 11954         case 's': {
       
 11955           sqlite3_snprintf(30,&z[j],"%lld",
       
 11956                            (i64)(x.iJD/1000 - 21086676*(i64)10000));
       
 11957           j += sqlite3Strlen30(&z[j]);
       
 11958           break;
       
 11959         }
       
 11960         case 'S':  sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
       
 11961         case 'w': {
       
 11962           z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
       
 11963           break;
       
 11964         }
       
 11965         case 'Y': {
       
 11966           sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
       
 11967           break;
       
 11968         }
       
 11969         default:   z[j++] = '%'; break;
       
 11970       }
       
 11971     }
       
 11972   }
       
 11973   z[j] = 0;
       
 11974   sqlite3_result_text(context, z, -1,
       
 11975                       z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
       
 11976 }
       
 11977 
       
 11978 /*
       
 11979 ** current_time()
       
 11980 **
       
 11981 ** This function returns the same value as time('now').
       
 11982 */
       
 11983 static void ctimeFunc(
       
 11984   sqlite3_context *context,
       
 11985   int NotUsed,
       
 11986   sqlite3_value **NotUsed2
       
 11987 ){
       
 11988   UNUSED_PARAMETER2(NotUsed, NotUsed2);
       
 11989   timeFunc(context, 0, 0);
       
 11990 }
       
 11991 
       
 11992 /*
       
 11993 ** current_date()
       
 11994 **
       
 11995 ** This function returns the same value as date('now').
       
 11996 */
       
 11997 static void cdateFunc(
       
 11998   sqlite3_context *context,
       
 11999   int NotUsed,
       
 12000   sqlite3_value **NotUsed2
       
 12001 ){
       
 12002   UNUSED_PARAMETER2(NotUsed, NotUsed2);
       
 12003   dateFunc(context, 0, 0);
       
 12004 }
       
 12005 
       
 12006 /*
       
 12007 ** current_timestamp()
       
 12008 **
       
 12009 ** This function returns the same value as datetime('now').
       
 12010 */
       
 12011 static void ctimestampFunc(
       
 12012   sqlite3_context *context,
       
 12013   int NotUsed,
       
 12014   sqlite3_value **NotUsed2
       
 12015 ){
       
 12016   UNUSED_PARAMETER2(NotUsed, NotUsed2);
       
 12017   datetimeFunc(context, 0, 0);
       
 12018 }
       
 12019 #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
       
 12020 
       
 12021 #ifdef SQLITE_OMIT_DATETIME_FUNCS
       
 12022 /*
       
 12023 ** If the library is compiled to omit the full-scale date and time
       
 12024 ** handling (to get a smaller binary), the following minimal version
       
 12025 ** of the functions current_time(), current_date() and current_timestamp()
       
 12026 ** are included instead. This is to support column declarations that
       
 12027 ** include "DEFAULT CURRENT_TIME" etc.
       
 12028 **
       
 12029 ** This function uses the C-library functions time(), gmtime()
       
 12030 ** and strftime(). The format string to pass to strftime() is supplied
       
 12031 ** as the user-data for the function.
       
 12032 */
       
 12033 static void currentTimeFunc(
       
 12034   sqlite3_context *context,
       
 12035   int argc,
       
 12036   sqlite3_value **argv
       
 12037 ){
       
 12038   time_t t;
       
 12039   char *zFormat = (char *)sqlite3_user_data(context);
       
 12040   sqlite3 *db;
       
 12041   double rT;
       
 12042   char zBuf[20];
       
 12043 
       
 12044   UNUSED_PARAMETER(argc);
       
 12045   UNUSED_PARAMETER(argv);
       
 12046 
       
 12047   db = sqlite3_context_db_handle(context);
       
 12048   sqlite3OsCurrentTime(db->pVfs, &rT);
       
 12049 #ifndef SQLITE_OMIT_FLOATING_POINT
       
 12050   t = 86400.0*(rT - 2440587.5) + 0.5;
       
 12051 #else
       
 12052   /* without floating point support, rT will have
       
 12053   ** already lost fractional day precision.
       
 12054   */
       
 12055   t = 86400 * (rT - 2440587) - 43200;
       
 12056 #endif
       
 12057 #ifdef HAVE_GMTIME_R
       
 12058   {
       
 12059     struct tm sNow;
       
 12060     gmtime_r(&t, &sNow);
       
 12061     strftime(zBuf, 20, zFormat, &sNow);
       
 12062   }
       
 12063 #else
       
 12064   {
       
 12065     struct tm *pTm;
       
 12066     sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
       
 12067     pTm = gmtime(&t);
       
 12068     strftime(zBuf, 20, zFormat, pTm);
       
 12069     sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
       
 12070   }
       
 12071 #endif
       
 12072 
       
 12073   sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
       
 12074 }
       
 12075 #endif
       
 12076 
       
 12077 /*
       
 12078 ** This function registered all of the above C functions as SQL
       
 12079 ** functions.  This should be the only routine in this file with
       
 12080 ** external linkage.
       
 12081 */
       
 12082 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
       
 12083   static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
       
 12084 #ifndef SQLITE_OMIT_DATETIME_FUNCS
       
 12085     FUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
       
 12086     FUNCTION(date,             -1, 0, 0, dateFunc      ),
       
 12087     FUNCTION(time,             -1, 0, 0, timeFunc      ),
       
 12088     FUNCTION(datetime,         -1, 0, 0, datetimeFunc  ),
       
 12089     FUNCTION(strftime,         -1, 0, 0, strftimeFunc  ),
       
 12090     FUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
       
 12091     FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
       
 12092     FUNCTION(current_date,      0, 0, 0, cdateFunc     ),
       
 12093 #else
       
 12094     STR_FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
       
 12095     STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d",          0, currentTimeFunc),
       
 12096     STR_FUNCTION(current_date,      0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
       
 12097 #endif
       
 12098   };
       
 12099   int i;
       
 12100   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
       
 12101   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
       
 12102 
       
 12103   for(i=0; i<ArraySize(aDateTimeFuncs); i++){
       
 12104     sqlite3FuncDefInsert(pHash, &aFunc[i]);
       
 12105   }
       
 12106 }
       
 12107 
       
 12108 /************** End of date.c ************************************************/
       
 12109 /************** Begin file os.c **********************************************/
       
 12110 /*
       
 12111 ** 2005 November 29
       
 12112 **
       
 12113 ** The author disclaims copyright to this source code.  In place of
       
 12114 ** a legal notice, here is a blessing:
       
 12115 **
       
 12116 **    May you do good and not evil.
       
 12117 **    May you find forgiveness for yourself and forgive others.
       
 12118 **    May you share freely, never taking more than you give.
       
 12119 **
       
 12120 ******************************************************************************
       
 12121 **
       
 12122 ** This file contains OS interface code that is common to all
       
 12123 ** architectures.
       
 12124 **
       
 12125 ** $Id: os.c,v 1.127 2009/07/27 11:41:21 danielk1977 Exp $
       
 12126 */
       
 12127 #define _SQLITE_OS_C_ 1
       
 12128 #undef _SQLITE_OS_C_
       
 12129 
       
 12130 /*
       
 12131 ** The default SQLite sqlite3_vfs implementations do not allocate
       
 12132 ** memory (actually, os_unix.c allocates a small amount of memory
       
 12133 ** from within OsOpen()), but some third-party implementations may.
       
 12134 ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
       
 12135 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
       
 12136 **
       
 12137 ** The following functions are instrumented for malloc() failure 
       
 12138 ** testing:
       
 12139 **
       
 12140 **     sqlite3OsOpen()
       
 12141 **     sqlite3OsRead()
       
 12142 **     sqlite3OsWrite()
       
 12143 **     sqlite3OsSync()
       
 12144 **     sqlite3OsLock()
       
 12145 **
       
 12146 */
       
 12147 #if defined(SQLITE_TEST) && (SQLITE_OS_WIN==0)
       
 12148   #define DO_OS_MALLOC_TEST(x) if (!x || !sqlite3IsMemJournal(x)) {     \
       
 12149     void *pTstAlloc = sqlite3Malloc(10);                             \
       
 12150     if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
       
 12151     sqlite3_free(pTstAlloc);                                         \
       
 12152   }
       
 12153 #else
       
 12154   #define DO_OS_MALLOC_TEST(x)
       
 12155 #endif
       
 12156 
       
 12157 /*
       
 12158 ** The following routines are convenience wrappers around methods
       
 12159 ** of the sqlite3_file object.  This is mostly just syntactic sugar. All
       
 12160 ** of this would be completely automatic if SQLite were coded using
       
 12161 ** C++ instead of plain old C.
       
 12162 */
       
 12163 SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
       
 12164   int rc = SQLITE_OK;
       
 12165   if( pId->pMethods ){
       
 12166     rc = pId->pMethods->xClose(pId);
       
 12167     pId->pMethods = 0;
       
 12168   }
       
 12169   return rc;
       
 12170 }
       
 12171 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
       
 12172   DO_OS_MALLOC_TEST(id);
       
 12173   return id->pMethods->xRead(id, pBuf, amt, offset);
       
 12174 }
       
 12175 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
       
 12176   DO_OS_MALLOC_TEST(id);
       
 12177   return id->pMethods->xWrite(id, pBuf, amt, offset);
       
 12178 }
       
 12179 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
       
 12180   return id->pMethods->xTruncate(id, size);
       
 12181 }
       
 12182 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
       
 12183   DO_OS_MALLOC_TEST(id);
       
 12184   return id->pMethods->xSync(id, flags);
       
 12185 }
       
 12186 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
       
 12187   DO_OS_MALLOC_TEST(id);
       
 12188   return id->pMethods->xFileSize(id, pSize);
       
 12189 }
       
 12190 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
       
 12191   DO_OS_MALLOC_TEST(id);
       
 12192   return id->pMethods->xLock(id, lockType);
       
 12193 }
       
 12194 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
       
 12195   return id->pMethods->xUnlock(id, lockType);
       
 12196 }
       
 12197 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
       
 12198   DO_OS_MALLOC_TEST(id);
       
 12199   return id->pMethods->xCheckReservedLock(id, pResOut);
       
 12200 }
       
 12201 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
       
 12202   return id->pMethods->xFileControl(id, op, pArg);
       
 12203 }
       
 12204 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
       
 12205   int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
       
 12206   return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
       
 12207 }
       
 12208 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
       
 12209   return id->pMethods->xDeviceCharacteristics(id);
       
 12210 }
       
 12211 
       
 12212 /*
       
 12213 ** The next group of routines are convenience wrappers around the
       
 12214 ** VFS methods.
       
 12215 */
       
 12216 SQLITE_PRIVATE int sqlite3OsOpen(
       
 12217   sqlite3_vfs *pVfs, 
       
 12218   const char *zPath, 
       
 12219   sqlite3_file *pFile, 
       
 12220   int flags, 
       
 12221   int *pFlagsOut
       
 12222 ){
       
 12223   int rc;
       
 12224   DO_OS_MALLOC_TEST(0);
       
 12225   /* 0x7f1f is a mask of SQLITE_OPEN_ flags that are valid to be passed
       
 12226   ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
       
 12227   ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
       
 12228   ** reaching the VFS. */
       
 12229   rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x7f1f, pFlagsOut);
       
 12230   assert( rc==SQLITE_OK || pFile->pMethods==0 );
       
 12231   return rc;
       
 12232 }
       
 12233 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
       
 12234   return pVfs->xDelete(pVfs, zPath, dirSync);
       
 12235 }
       
 12236 SQLITE_PRIVATE int sqlite3OsAccess(
       
 12237   sqlite3_vfs *pVfs, 
       
 12238   const char *zPath, 
       
 12239   int flags, 
       
 12240   int *pResOut
       
 12241 ){
       
 12242   DO_OS_MALLOC_TEST(0);
       
 12243   return pVfs->xAccess(pVfs, zPath, flags, pResOut);
       
 12244 }
       
 12245 SQLITE_PRIVATE int sqlite3OsFullPathname(
       
 12246   sqlite3_vfs *pVfs, 
       
 12247   const char *zPath, 
       
 12248   int nPathOut, 
       
 12249   char *zPathOut
       
 12250 ){
       
 12251   return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
       
 12252 }
       
 12253 #ifndef SQLITE_OMIT_LOAD_EXTENSION
       
 12254 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
       
 12255   return pVfs->xDlOpen(pVfs, zPath);
       
 12256 }
       
 12257 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
       
 12258   pVfs->xDlError(pVfs, nByte, zBufOut);
       
 12259 }
       
 12260 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
       
 12261   return pVfs->xDlSym(pVfs, pHdle, zSym);
       
 12262 }
       
 12263 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
       
 12264   pVfs->xDlClose(pVfs, pHandle);
       
 12265 }
       
 12266 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
       
 12267 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
       
 12268   return pVfs->xRandomness(pVfs, nByte, zBufOut);
       
 12269 }
       
 12270 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
       
 12271   return pVfs->xSleep(pVfs, nMicro);
       
 12272 }
       
 12273 SQLITE_PRIVATE int sqlite3OsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
       
 12274   return pVfs->xCurrentTime(pVfs, pTimeOut);
       
 12275 }
       
 12276 
       
 12277 SQLITE_PRIVATE int sqlite3OsOpenMalloc(
       
 12278   sqlite3_vfs *pVfs, 
       
 12279   const char *zFile, 
       
 12280   sqlite3_file **ppFile, 
       
 12281   int flags,
       
 12282   int *pOutFlags
       
 12283 ){
       
 12284   int rc = SQLITE_NOMEM;
       
 12285   sqlite3_file *pFile;
       
 12286   pFile = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile);
       
 12287   if( pFile ){
       
 12288     rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
       
 12289     if( rc!=SQLITE_OK ){
       
 12290       sqlite3_free(pFile);
       
 12291     }else{
       
 12292       *ppFile = pFile;
       
 12293     }
       
 12294   }
       
 12295   return rc;
       
 12296 }
       
 12297 SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
       
 12298   int rc = SQLITE_OK;
       
 12299   assert( pFile );
       
 12300   rc = sqlite3OsClose(pFile);
       
 12301   sqlite3_free(pFile);
       
 12302   return rc;
       
 12303 }
       
 12304 
       
 12305 /*
       
 12306 ** This function is a wrapper around the OS specific implementation of
       
 12307 ** sqlite3_os_init(). The purpose of the wrapper is to provide the
       
 12308 ** ability to simulate a malloc failure, so that the handling of an
       
 12309 ** error in sqlite3_os_init() by the upper layers can be tested.
       
 12310 */
       
 12311 SQLITE_PRIVATE int sqlite3OsInit(void){
       
 12312   void *p = sqlite3_malloc(10);
       
 12313   if( p==0 ) return SQLITE_NOMEM;
       
 12314   sqlite3_free(p);
       
 12315   return sqlite3_os_init();
       
 12316 }
       
 12317 
       
 12318 /*
       
 12319 ** The list of all registered VFS implementations.
       
 12320 */
       
 12321 static sqlite3_vfs * SQLITE_WSD vfsList = 0;
       
 12322 #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
       
 12323 
       
 12324 /*
       
 12325 ** Locate a VFS by name.  If no name is given, simply return the
       
 12326 ** first VFS on the list.
       
 12327 */
       
 12328 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
       
 12329   sqlite3_vfs *pVfs = 0;
       
 12330 #if SQLITE_THREADSAFE
       
 12331   sqlite3_mutex *mutex;
       
 12332 #endif
       
 12333 #ifndef SQLITE_OMIT_AUTOINIT
       
 12334   int rc = sqlite3_initialize();
       
 12335   if( rc ) return 0;
       
 12336 #endif
       
 12337 #if SQLITE_THREADSAFE
       
 12338   mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
       
 12339 #endif
       
 12340   sqlite3_mutex_enter(mutex);
       
 12341   for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
       
 12342     if( zVfs==0 ) break;
       
 12343     if( strcmp(zVfs, pVfs->zName)==0 ) break;
       
 12344   }
       
 12345   sqlite3_mutex_leave(mutex);
       
 12346   return pVfs;
       
 12347 }
       
 12348 
       
 12349 /*
       
 12350 ** Unlink a VFS from the linked list
       
 12351 */
       
 12352 static void vfsUnlink(sqlite3_vfs *pVfs){
       
 12353   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
       
 12354   if( pVfs==0 ){
       
 12355     /* No-op */
       
 12356   }else if( vfsList==pVfs ){
       
 12357     vfsList = pVfs->pNext;
       
 12358   }else if( vfsList ){
       
 12359     sqlite3_vfs *p = vfsList;
       
 12360     while( p->pNext && p->pNext!=pVfs ){
       
 12361       p = p->pNext;
       
 12362     }
       
 12363     if( p->pNext==pVfs ){
       
 12364       p->pNext = pVfs->pNext;
       
 12365     }
       
 12366   }
       
 12367 }
       
 12368 
       
 12369 /*
       
 12370 ** Register a VFS with the system.  It is harmless to register the same
       
 12371 ** VFS multiple times.  The new VFS becomes the default if makeDflt is
       
 12372 ** true.
       
 12373 */
       
 12374 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
       
 12375   sqlite3_mutex *mutex = 0;
       
 12376 #ifndef SQLITE_OMIT_AUTOINIT
       
 12377   int rc = sqlite3_initialize();
       
 12378   if( rc ) return rc;
       
 12379 #endif
       
 12380   mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
       
 12381   sqlite3_mutex_enter(mutex);
       
 12382   vfsUnlink(pVfs);
       
 12383   if( makeDflt || vfsList==0 ){
       
 12384     pVfs->pNext = vfsList;
       
 12385     vfsList = pVfs;
       
 12386   }else{
       
 12387     pVfs->pNext = vfsList->pNext;
       
 12388     vfsList->pNext = pVfs;
       
 12389   }
       
 12390   assert(vfsList);
       
 12391   sqlite3_mutex_leave(mutex);
       
 12392   return SQLITE_OK;
       
 12393 }
       
 12394 
       
 12395 /*
       
 12396 ** Unregister a VFS so that it is no longer accessible.
       
 12397 */
       
 12398 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
       
 12399 #if SQLITE_THREADSAFE
       
 12400   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
       
 12401 #endif
       
 12402   sqlite3_mutex_enter(mutex);
       
 12403   vfsUnlink(pVfs);
       
 12404   sqlite3_mutex_leave(mutex);
       
 12405   return SQLITE_OK;
       
 12406 }
       
 12407 
       
 12408 /************** End of os.c **************************************************/
       
 12409 /************** Begin file fault.c *******************************************/
       
 12410 /*
       
 12411 ** 2008 Jan 22
       
 12412 **
       
 12413 ** The author disclaims copyright to this source code.  In place of
       
 12414 ** a legal notice, here is a blessing:
       
 12415 **
       
 12416 **    May you do good and not evil.
       
 12417 **    May you find forgiveness for yourself and forgive others.
       
 12418 **    May you share freely, never taking more than you give.
       
 12419 **
       
 12420 *************************************************************************
       
 12421 **
       
 12422 ** $Id: fault.c,v 1.11 2008/09/02 00:52:52 drh Exp $
       
 12423 */
       
 12424 
       
 12425 /*
       
 12426 ** This file contains code to support the concept of "benign" 
       
 12427 ** malloc failures (when the xMalloc() or xRealloc() method of the
       
 12428 ** sqlite3_mem_methods structure fails to allocate a block of memory
       
 12429 ** and returns 0). 
       
 12430 **
       
 12431 ** Most malloc failures are non-benign. After they occur, SQLite
       
 12432 ** abandons the current operation and returns an error code (usually
       
 12433 ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
       
 12434 ** fatal. For example, if a malloc fails while resizing a hash table, this 
       
 12435 ** is completely recoverable simply by not carrying out the resize. The 
       
 12436 ** hash table will continue to function normally.  So a malloc failure 
       
 12437 ** during a hash table resize is a benign fault.
       
 12438 */
       
 12439 
       
 12440 
       
 12441 #ifndef SQLITE_OMIT_BUILTIN_TEST
       
 12442 
       
 12443 /*
       
 12444 ** Global variables.
       
 12445 */
       
 12446 typedef struct BenignMallocHooks BenignMallocHooks;
       
 12447 static SQLITE_WSD struct BenignMallocHooks {
       
 12448   void (*xBenignBegin)(void);
       
 12449   void (*xBenignEnd)(void);
       
 12450 } sqlite3Hooks = { 0, 0 };
       
 12451 
       
 12452 /* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
       
 12453 ** structure.  If writable static data is unsupported on the target,
       
 12454 ** we have to locate the state vector at run-time.  In the more common
       
 12455 ** case where writable static data is supported, wsdHooks can refer directly
       
 12456 ** to the "sqlite3Hooks" state vector declared above.
       
 12457 */
       
 12458 #ifdef SQLITE_OMIT_WSD
       
 12459 # define wsdHooksInit \
       
 12460   BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
       
 12461 # define wsdHooks x[0]
       
 12462 #else
       
 12463 # define wsdHooksInit
       
 12464 # define wsdHooks sqlite3Hooks
       
 12465 #endif
       
 12466 
       
 12467 
       
 12468 /*
       
 12469 ** Register hooks to call when sqlite3BeginBenignMalloc() and
       
 12470 ** sqlite3EndBenignMalloc() are called, respectively.
       
 12471 */
       
 12472 SQLITE_PRIVATE void sqlite3BenignMallocHooks(
       
 12473   void (*xBenignBegin)(void),
       
 12474   void (*xBenignEnd)(void)
       
 12475 ){
       
 12476   wsdHooksInit;
       
 12477   wsdHooks.xBenignBegin = xBenignBegin;
       
 12478   wsdHooks.xBenignEnd = xBenignEnd;
       
 12479 }
       
 12480 
       
 12481 /*
       
 12482 ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
       
 12483 ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
       
 12484 ** indicates that subsequent malloc failures are non-benign.
       
 12485 */
       
 12486 SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
       
 12487   wsdHooksInit;
       
 12488   if( wsdHooks.xBenignBegin ){
       
 12489     wsdHooks.xBenignBegin();
       
 12490   }
       
 12491 }
       
 12492 SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
       
 12493   wsdHooksInit;
       
 12494   if( wsdHooks.xBenignEnd ){
       
 12495     wsdHooks.xBenignEnd();
       
 12496   }
       
 12497 }
       
 12498 
       
 12499 #endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
       
 12500 
       
 12501 /************** End of fault.c ***********************************************/
       
 12502 /************** Begin file mem0.c ********************************************/
       
 12503 /*
       
 12504 ** 2008 October 28
       
 12505 **
       
 12506 ** The author disclaims copyright to this source code.  In place of
       
 12507 ** a legal notice, here is a blessing:
       
 12508 **
       
 12509 **    May you do good and not evil.
       
 12510 **    May you find forgiveness for yourself and forgive others.
       
 12511 **    May you share freely, never taking more than you give.
       
 12512 **
       
 12513 *************************************************************************
       
 12514 **
       
 12515 ** This file contains a no-op memory allocation drivers for use when
       
 12516 ** SQLITE_ZERO_MALLOC is defined.  The allocation drivers implemented
       
 12517 ** here always fail.  SQLite will not operate with these drivers.  These
       
 12518 ** are merely placeholders.  Real drivers must be substituted using
       
 12519 ** sqlite3_config() before SQLite will operate.
       
 12520 **
       
 12521 ** $Id: mem0.c,v 1.1 2008/10/28 18:58:20 drh Exp $
       
 12522 */
       
 12523 
       
 12524 /*
       
 12525 ** This version of the memory allocator is the default.  It is
       
 12526 ** used when no other memory allocator is specified using compile-time
       
 12527 ** macros.
       
 12528 */
       
 12529 #ifdef SQLITE_ZERO_MALLOC
       
 12530 
       
 12531 /*
       
 12532 ** No-op versions of all memory allocation routines
       
 12533 */
       
 12534 static void *sqlite3MemMalloc(int nByte){ return 0; }
       
 12535 static void sqlite3MemFree(void *pPrior){ return; }
       
 12536 static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
       
 12537 static int sqlite3MemSize(void *pPrior){ return 0; }
       
 12538 static int sqlite3MemRoundup(int n){ return n; }
       
 12539 static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
       
 12540 static void sqlite3MemShutdown(void *NotUsed){ return; }
       
 12541 
       
 12542 /*
       
 12543 ** This routine is the only routine in this file with external linkage.
       
 12544 **
       
 12545 ** Populate the low-level memory allocation function pointers in
       
 12546 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
       
 12547 */
       
 12548 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
       
 12549   static const sqlite3_mem_methods defaultMethods = {
       
 12550      sqlite3MemMalloc,
       
 12551      sqlite3MemFree,
       
 12552      sqlite3MemRealloc,
       
 12553      sqlite3MemSize,
       
 12554      sqlite3MemRoundup,
       
 12555      sqlite3MemInit,
       
 12556      sqlite3MemShutdown,
       
 12557      0
       
 12558   };
       
 12559   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
       
 12560 }
       
 12561 
       
 12562 #endif /* SQLITE_ZERO_MALLOC */
       
 12563 
       
 12564 /************** End of mem0.c ************************************************/
       
 12565 /************** Begin file mem1.c ********************************************/
       
 12566 /*
       
 12567 ** 2007 August 14
       
 12568 **
       
 12569 ** The author disclaims copyright to this source code.  In place of
       
 12570 ** a legal notice, here is a blessing:
       
 12571 **
       
 12572 **    May you do good and not evil.
       
 12573 **    May you find forgiveness for yourself and forgive others.
       
 12574 **    May you share freely, never taking more than you give.
       
 12575 **
       
 12576 *************************************************************************
       
 12577 **
       
 12578 ** This file contains low-level memory allocation drivers for when
       
 12579 ** SQLite will use the standard C-library malloc/realloc/free interface
       
 12580 ** to obtain the memory it needs.
       
 12581 **
       
 12582 ** This file contains implementations of the low-level memory allocation
       
 12583 ** routines specified in the sqlite3_mem_methods object.
       
 12584 **
       
 12585 ** $Id: mem1.c,v 1.30 2009/03/23 04:33:33 danielk1977 Exp $
       
 12586 */
       
 12587 
       
 12588 /*
       
 12589 ** This version of the memory allocator is the default.  It is
       
 12590 ** used when no other memory allocator is specified using compile-time
       
 12591 ** macros.
       
 12592 */
       
 12593 #ifdef SQLITE_SYSTEM_MALLOC
       
 12594 
       
 12595 /*
       
 12596 ** Like malloc(), but remember the size of the allocation
       
 12597 ** so that we can find it later using sqlite3MemSize().
       
 12598 **
       
 12599 ** For this low-level routine, we are guaranteed that nByte>0 because
       
 12600 ** cases of nByte<=0 will be intercepted and dealt with by higher level
       
 12601 ** routines.
       
 12602 */
       
 12603 static void *sqlite3MemMalloc(int nByte){
       
 12604   sqlite3_int64 *p;
       
 12605   assert( nByte>0 );
       
 12606   nByte = ROUND8(nByte);
       
 12607   p = malloc( nByte+8 );
       
 12608   if( p ){
       
 12609     p[0] = nByte;
       
 12610     p++;
       
 12611   }
       
 12612   return (void *)p;
       
 12613 }
       
 12614 
       
 12615 /*
       
 12616 ** Like free() but works for allocations obtained from sqlite3MemMalloc()
       
 12617 ** or sqlite3MemRealloc().
       
 12618 **
       
 12619 ** For this low-level routine, we already know that pPrior!=0 since
       
 12620 ** cases where pPrior==0 will have been intecepted and dealt with
       
 12621 ** by higher-level routines.
       
 12622 */
       
 12623 static void sqlite3MemFree(void *pPrior){
       
 12624   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
       
 12625   assert( pPrior!=0 );
       
 12626   p--;
       
 12627   free(p);
       
 12628 }
       
 12629 
       
 12630 /*
       
 12631 ** Like realloc().  Resize an allocation previously obtained from
       
 12632 ** sqlite3MemMalloc().
       
 12633 **
       
 12634 ** For this low-level interface, we know that pPrior!=0.  Cases where
       
 12635 ** pPrior==0 while have been intercepted by higher-level routine and
       
 12636 ** redirected to xMalloc.  Similarly, we know that nByte>0 becauses
       
 12637 ** cases where nByte<=0 will have been intercepted by higher-level
       
 12638 ** routines and redirected to xFree.
       
 12639 */
       
 12640 static void *sqlite3MemRealloc(void *pPrior, int nByte){
       
 12641   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
       
 12642   assert( pPrior!=0 && nByte>0 );
       
 12643   nByte = ROUND8(nByte);
       
 12644   p = (sqlite3_int64*)pPrior;
       
 12645   p--;
       
 12646   p = realloc(p, nByte+8 );
       
 12647   if( p ){
       
 12648     p[0] = nByte;
       
 12649     p++;
       
 12650   }
       
 12651   return (void*)p;
       
 12652 }
       
 12653 
       
 12654 /*
       
 12655 ** Report the allocated size of a prior return from xMalloc()
       
 12656 ** or xRealloc().
       
 12657 */
       
 12658 static int sqlite3MemSize(void *pPrior){
       
 12659   sqlite3_int64 *p;
       
 12660   if( pPrior==0 ) return 0;
       
 12661   p = (sqlite3_int64*)pPrior;
       
 12662   p--;
       
 12663   return (int)p[0];
       
 12664 }
       
 12665 
       
 12666 /*
       
 12667 ** Round up a request size to the next valid allocation size.
       
 12668 */
       
 12669 static int sqlite3MemRoundup(int n){
       
 12670   return ROUND8(n);
       
 12671 }
       
 12672 
       
 12673 /*
       
 12674 ** Initialize this module.
       
 12675 */
       
 12676 static int sqlite3MemInit(void *NotUsed){
       
 12677   UNUSED_PARAMETER(NotUsed);
       
 12678   return SQLITE_OK;
       
 12679 }
       
 12680 
       
 12681 /*
       
 12682 ** Deinitialize this module.
       
 12683 */
       
 12684 static void sqlite3MemShutdown(void *NotUsed){
       
 12685   UNUSED_PARAMETER(NotUsed);
       
 12686   return;
       
 12687 }
       
 12688 
       
 12689 /*
       
 12690 ** This routine is the only routine in this file with external linkage.
       
 12691 **
       
 12692 ** Populate the low-level memory allocation function pointers in
       
 12693 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
       
 12694 */
       
 12695 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
       
 12696   static const sqlite3_mem_methods defaultMethods = {
       
 12697      sqlite3MemMalloc,
       
 12698      sqlite3MemFree,
       
 12699      sqlite3MemRealloc,
       
 12700      sqlite3MemSize,
       
 12701      sqlite3MemRoundup,
       
 12702      sqlite3MemInit,
       
 12703      sqlite3MemShutdown,
       
 12704      0
       
 12705   };
       
 12706   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
       
 12707 }
       
 12708 
       
 12709 #endif /* SQLITE_SYSTEM_MALLOC */
       
 12710 
       
 12711 /************** End of mem1.c ************************************************/
       
 12712 /************** Begin file mem2.c ********************************************/
       
 12713 /*
       
 12714 ** 2007 August 15
       
 12715 **
       
 12716 ** The author disclaims copyright to this source code.  In place of
       
 12717 ** a legal notice, here is a blessing:
       
 12718 **
       
 12719 **    May you do good and not evil.
       
 12720 **    May you find forgiveness for yourself and forgive others.
       
 12721 **    May you share freely, never taking more than you give.
       
 12722 **
       
 12723 *************************************************************************
       
 12724 **
       
 12725 ** This file contains low-level memory allocation drivers for when
       
 12726 ** SQLite will use the standard C-library malloc/realloc/free interface
       
 12727 ** to obtain the memory it needs while adding lots of additional debugging
       
 12728 ** information to each allocation in order to help detect and fix memory
       
 12729 ** leaks and memory usage errors.
       
 12730 **
       
 12731 ** This file contains implementations of the low-level memory allocation
       
 12732 ** routines specified in the sqlite3_mem_methods object.
       
 12733 **
       
 12734 ** $Id: mem2.c,v 1.45 2009/03/23 04:33:33 danielk1977 Exp $
       
 12735 */
       
 12736 
       
 12737 /*
       
 12738 ** This version of the memory allocator is used only if the
       
 12739 ** SQLITE_MEMDEBUG macro is defined
       
 12740 */
       
 12741 #ifdef SQLITE_MEMDEBUG
       
 12742 
       
 12743 /*
       
 12744 ** The backtrace functionality is only available with GLIBC
       
 12745 */
       
 12746 #ifdef __GLIBC__
       
 12747   extern int backtrace(void**,int);
       
 12748   extern void backtrace_symbols_fd(void*const*,int,int);
       
 12749 #else
       
 12750 # define backtrace(A,B) 1
       
 12751 # define backtrace_symbols_fd(A,B,C)
       
 12752 #endif
       
 12753 
       
 12754 /*
       
 12755 ** Each memory allocation looks like this:
       
 12756 **
       
 12757 **  ------------------------------------------------------------------------
       
 12758 **  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
       
 12759 **  ------------------------------------------------------------------------
       
 12760 **
       
 12761 ** The application code sees only a pointer to the allocation.  We have
       
 12762 ** to back up from the allocation pointer to find the MemBlockHdr.  The
       
 12763 ** MemBlockHdr tells us the size of the allocation and the number of
       
 12764 ** backtrace pointers.  There is also a guard word at the end of the
       
 12765 ** MemBlockHdr.
       
 12766 */
       
 12767 struct MemBlockHdr {
       
 12768   i64 iSize;                          /* Size of this allocation */
       
 12769   struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
       
 12770   char nBacktrace;                    /* Number of backtraces on this alloc */
       
 12771   char nBacktraceSlots;               /* Available backtrace slots */
       
 12772   short nTitle;                       /* Bytes of title; includes '\0' */
       
 12773   int iForeGuard;                     /* Guard word for sanity */
       
 12774 };
       
 12775 
       
 12776 /*
       
 12777 ** Guard words
       
 12778 */
       
 12779 #define FOREGUARD 0x80F5E153
       
 12780 #define REARGUARD 0xE4676B53
       
 12781 
       
 12782 /*
       
 12783 ** Number of malloc size increments to track.
       
 12784 */
       
 12785 #define NCSIZE  1000
       
 12786 
       
 12787 /*
       
 12788 ** All of the static variables used by this module are collected
       
 12789 ** into a single structure named "mem".  This is to keep the
       
 12790 ** static variables organized and to reduce namespace pollution
       
 12791 ** when this module is combined with other in the amalgamation.
       
 12792 */
       
 12793 static struct {
       
 12794   
       
 12795   /*
       
 12796   ** Mutex to control access to the memory allocation subsystem.
       
 12797   */
       
 12798   sqlite3_mutex *mutex;
       
 12799 
       
 12800   /*
       
 12801   ** Head and tail of a linked list of all outstanding allocations
       
 12802   */
       
 12803   struct MemBlockHdr *pFirst;
       
 12804   struct MemBlockHdr *pLast;
       
 12805   
       
 12806   /*
       
 12807   ** The number of levels of backtrace to save in new allocations.
       
 12808   */
       
 12809   int nBacktrace;
       
 12810   void (*xBacktrace)(int, int, void **);
       
 12811 
       
 12812   /*
       
 12813   ** Title text to insert in front of each block
       
 12814   */
       
 12815   int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
       
 12816   char zTitle[100];  /* The title text */
       
 12817 
       
 12818   /* 
       
 12819   ** sqlite3MallocDisallow() increments the following counter.
       
 12820   ** sqlite3MallocAllow() decrements it.
       
 12821   */
       
 12822   int disallow; /* Do not allow memory allocation */
       
 12823 
       
 12824   /*
       
 12825   ** Gather statistics on the sizes of memory allocations.
       
 12826   ** nAlloc[i] is the number of allocation attempts of i*8
       
 12827   ** bytes.  i==NCSIZE is the number of allocation attempts for
       
 12828   ** sizes more than NCSIZE*8 bytes.
       
 12829   */
       
 12830   int nAlloc[NCSIZE];      /* Total number of allocations */
       
 12831   int nCurrent[NCSIZE];    /* Current number of allocations */
       
 12832   int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
       
 12833 
       
 12834 } mem;
       
 12835 
       
 12836 
       
 12837 /*
       
 12838 ** Adjust memory usage statistics
       
 12839 */
       
 12840 static void adjustStats(int iSize, int increment){
       
 12841   int i = ROUND8(iSize)/8;
       
 12842   if( i>NCSIZE-1 ){
       
 12843     i = NCSIZE - 1;
       
 12844   }
       
 12845   if( increment>0 ){
       
 12846     mem.nAlloc[i]++;
       
 12847     mem.nCurrent[i]++;
       
 12848     if( mem.nCurrent[i]>mem.mxCurrent[i] ){
       
 12849       mem.mxCurrent[i] = mem.nCurrent[i];
       
 12850     }
       
 12851   }else{
       
 12852     mem.nCurrent[i]--;
       
 12853     assert( mem.nCurrent[i]>=0 );
       
 12854   }
       
 12855 }
       
 12856 
       
 12857 /*
       
 12858 ** Given an allocation, find the MemBlockHdr for that allocation.
       
 12859 **
       
 12860 ** This routine checks the guards at either end of the allocation and
       
 12861 ** if they are incorrect it asserts.
       
 12862 */
       
 12863 static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
       
 12864   struct MemBlockHdr *p;
       
 12865   int *pInt;
       
 12866   u8 *pU8;
       
 12867   int nReserve;
       
 12868 
       
 12869   p = (struct MemBlockHdr*)pAllocation;
       
 12870   p--;
       
 12871   assert( p->iForeGuard==(int)FOREGUARD );
       
 12872   nReserve = ROUND8(p->iSize);
       
 12873   pInt = (int*)pAllocation;
       
 12874   pU8 = (u8*)pAllocation;
       
 12875   assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
       
 12876   /* This checks any of the "extra" bytes allocated due
       
 12877   ** to rounding up to an 8 byte boundary to ensure 
       
 12878   ** they haven't been overwritten.
       
 12879   */
       
 12880   while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
       
 12881   return p;
       
 12882 }
       
 12883 
       
 12884 /*
       
 12885 ** Return the number of bytes currently allocated at address p.
       
 12886 */
       
 12887 static int sqlite3MemSize(void *p){
       
 12888   struct MemBlockHdr *pHdr;
       
 12889   if( !p ){
       
 12890     return 0;
       
 12891   }
       
 12892   pHdr = sqlite3MemsysGetHeader(p);
       
 12893   return pHdr->iSize;
       
 12894 }
       
 12895 
       
 12896 /*
       
 12897 ** Initialize the memory allocation subsystem.
       
 12898 */
       
 12899 static int sqlite3MemInit(void *NotUsed){
       
 12900   UNUSED_PARAMETER(NotUsed);
       
 12901   assert( (sizeof(struct MemBlockHdr)&7) == 0 );
       
 12902   if( !sqlite3GlobalConfig.bMemstat ){
       
 12903     /* If memory status is enabled, then the malloc.c wrapper will already
       
 12904     ** hold the STATIC_MEM mutex when the routines here are invoked. */
       
 12905     mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
       
 12906   }
       
 12907   return SQLITE_OK;
       
 12908 }
       
 12909 
       
 12910 /*
       
 12911 ** Deinitialize the memory allocation subsystem.
       
 12912 */
       
 12913 static void sqlite3MemShutdown(void *NotUsed){
       
 12914   UNUSED_PARAMETER(NotUsed);
       
 12915   mem.mutex = 0;
       
 12916 }
       
 12917 
       
 12918 /*
       
 12919 ** Round up a request size to the next valid allocation size.
       
 12920 */
       
 12921 static int sqlite3MemRoundup(int n){
       
 12922   return ROUND8(n);
       
 12923 }
       
 12924 
       
 12925 /*
       
 12926 ** Allocate nByte bytes of memory.
       
 12927 */
       
 12928 static void *sqlite3MemMalloc(int nByte){
       
 12929   struct MemBlockHdr *pHdr;
       
 12930   void **pBt;
       
 12931   char *z;
       
 12932   int *pInt;
       
 12933   void *p = 0;
       
 12934   int totalSize;
       
 12935   int nReserve;
       
 12936   sqlite3_mutex_enter(mem.mutex);
       
 12937   assert( mem.disallow==0 );
       
 12938   nReserve = ROUND8(nByte);
       
 12939   totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
       
 12940                mem.nBacktrace*sizeof(void*) + mem.nTitle;
       
 12941   p = malloc(totalSize);
       
 12942   if( p ){
       
 12943     z = p;
       
 12944     pBt = (void**)&z[mem.nTitle];
       
 12945     pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
       
 12946     pHdr->pNext = 0;
       
 12947     pHdr->pPrev = mem.pLast;
       
 12948     if( mem.pLast ){
       
 12949       mem.pLast->pNext = pHdr;
       
 12950     }else{
       
 12951       mem.pFirst = pHdr;
       
 12952     }
       
 12953     mem.pLast = pHdr;
       
 12954     pHdr->iForeGuard = FOREGUARD;
       
 12955     pHdr->nBacktraceSlots = mem.nBacktrace;
       
 12956     pHdr->nTitle = mem.nTitle;
       
 12957     if( mem.nBacktrace ){
       
 12958       void *aAddr[40];
       
 12959       pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
       
 12960       memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
       
 12961       assert(pBt[0]);
       
 12962       if( mem.xBacktrace ){
       
 12963         mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
       
 12964       }
       
 12965     }else{
       
 12966       pHdr->nBacktrace = 0;
       
 12967     }
       
 12968     if( mem.nTitle ){
       
 12969       memcpy(z, mem.zTitle, mem.nTitle);
       
 12970     }
       
 12971     pHdr->iSize = nByte;
       
 12972     adjustStats(nByte, +1);
       
 12973     pInt = (int*)&pHdr[1];
       
 12974     pInt[nReserve/sizeof(int)] = REARGUARD;
       
 12975     memset(pInt, 0x65, nReserve);
       
 12976     p = (void*)pInt;
       
 12977   }
       
 12978   sqlite3_mutex_leave(mem.mutex);
       
 12979   return p; 
       
 12980 }
       
 12981 
       
 12982 /*
       
 12983 ** Free memory.
       
 12984 */
       
 12985 static void sqlite3MemFree(void *pPrior){
       
 12986   struct MemBlockHdr *pHdr;
       
 12987   void **pBt;
       
 12988   char *z;
       
 12989   assert( sqlite3GlobalConfig.bMemstat || mem.mutex!=0 );
       
 12990   pHdr = sqlite3MemsysGetHeader(pPrior);
       
 12991   pBt = (void**)pHdr;
       
 12992   pBt -= pHdr->nBacktraceSlots;
       
 12993   sqlite3_mutex_enter(mem.mutex);
       
 12994   if( pHdr->pPrev ){
       
 12995     assert( pHdr->pPrev->pNext==pHdr );
       
 12996     pHdr->pPrev->pNext = pHdr->pNext;
       
 12997   }else{
       
 12998     assert( mem.pFirst==pHdr );
       
 12999     mem.pFirst = pHdr->pNext;
       
 13000   }
       
 13001   if( pHdr->pNext ){
       
 13002     assert( pHdr->pNext->pPrev==pHdr );
       
 13003     pHdr->pNext->pPrev = pHdr->pPrev;
       
 13004   }else{
       
 13005     assert( mem.pLast==pHdr );
       
 13006     mem.pLast = pHdr->pPrev;
       
 13007   }
       
 13008   z = (char*)pBt;
       
 13009   z -= pHdr->nTitle;
       
 13010   adjustStats(pHdr->iSize, -1);
       
 13011   memset(z, 0x2b, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
       
 13012                   pHdr->iSize + sizeof(int) + pHdr->nTitle);
       
 13013   free(z);
       
 13014   sqlite3_mutex_leave(mem.mutex);  
       
 13015 }
       
 13016 
       
 13017 /*
       
 13018 ** Change the size of an existing memory allocation.
       
 13019 **
       
 13020 ** For this debugging implementation, we *always* make a copy of the
       
 13021 ** allocation into a new place in memory.  In this way, if the 
       
 13022 ** higher level code is using pointer to the old allocation, it is 
       
 13023 ** much more likely to break and we are much more liking to find
       
 13024 ** the error.
       
 13025 */
       
 13026 static void *sqlite3MemRealloc(void *pPrior, int nByte){
       
 13027   struct MemBlockHdr *pOldHdr;
       
 13028   void *pNew;
       
 13029   assert( mem.disallow==0 );
       
 13030   pOldHdr = sqlite3MemsysGetHeader(pPrior);
       
 13031   pNew = sqlite3MemMalloc(nByte);
       
 13032   if( pNew ){
       
 13033     memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
       
 13034     if( nByte>pOldHdr->iSize ){
       
 13035       memset(&((char*)pNew)[pOldHdr->iSize], 0x2b, nByte - pOldHdr->iSize);
       
 13036     }
       
 13037     sqlite3MemFree(pPrior);
       
 13038   }
       
 13039   return pNew;
       
 13040 }
       
 13041 
       
 13042 /*
       
 13043 ** Populate the low-level memory allocation function pointers in
       
 13044 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
       
 13045 */
       
 13046 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
       
 13047   static const sqlite3_mem_methods defaultMethods = {
       
 13048      sqlite3MemMalloc,
       
 13049      sqlite3MemFree,
       
 13050      sqlite3MemRealloc,
       
 13051      sqlite3MemSize,
       
 13052      sqlite3MemRoundup,
       
 13053      sqlite3MemInit,
       
 13054      sqlite3MemShutdown,
       
 13055      0
       
 13056   };
       
 13057   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
       
 13058 }
       
 13059 
       
 13060 /*
       
 13061 ** Set the number of backtrace levels kept for each allocation.
       
 13062 ** A value of zero turns off backtracing.  The number is always rounded
       
 13063 ** up to a multiple of 2.
       
 13064 */
       
 13065 SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
       
 13066   if( depth<0 ){ depth = 0; }
       
 13067   if( depth>20 ){ depth = 20; }
       
 13068   depth = (depth+1)&0xfe;
       
 13069   mem.nBacktrace = depth;
       
 13070 }
       
 13071 
       
 13072 SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
       
 13073   mem.xBacktrace = xBacktrace;
       
 13074 }
       
 13075 
       
 13076 /*
       
 13077 ** Set the title string for subsequent allocations.
       
 13078 */
       
 13079 SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
       
 13080   unsigned int n = sqlite3Strlen30(zTitle) + 1;
       
 13081   sqlite3_mutex_enter(mem.mutex);
       
 13082   if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
       
 13083   memcpy(mem.zTitle, zTitle, n);
       
 13084   mem.zTitle[n] = 0;
       
 13085   mem.nTitle = ROUND8(n);
       
 13086   sqlite3_mutex_leave(mem.mutex);
       
 13087 }
       
 13088 
       
 13089 SQLITE_PRIVATE void sqlite3MemdebugSync(){
       
 13090   struct MemBlockHdr *pHdr;
       
 13091   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
       
 13092     void **pBt = (void**)pHdr;
       
 13093     pBt -= pHdr->nBacktraceSlots;
       
 13094     mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
       
 13095   }
       
 13096 }
       
 13097 
       
 13098 /*
       
 13099 ** Open the file indicated and write a log of all unfreed memory 
       
 13100 ** allocations into that log.
       
 13101 */
       
 13102 SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
       
 13103   FILE *out;
       
 13104   struct MemBlockHdr *pHdr;
       
 13105   void **pBt;
       
 13106   int i;
       
 13107   out = fopen(zFilename, "w");
       
 13108   if( out==0 ){
       
 13109     fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
       
 13110                     zFilename);
       
 13111     return;
       
 13112   }
       
 13113   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
       
 13114     char *z = (char*)pHdr;
       
 13115     z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
       
 13116     fprintf(out, "**** %lld bytes at %p from %s ****\n", 
       
 13117             pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
       
 13118     if( pHdr->nBacktrace ){
       
 13119       fflush(out);
       
 13120       pBt = (void**)pHdr;
       
 13121       pBt -= pHdr->nBacktraceSlots;
       
 13122       backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
       
 13123       fprintf(out, "\n");
       
 13124     }
       
 13125   }
       
 13126   fprintf(out, "COUNTS:\n");
       
 13127   for(i=0; i<NCSIZE-1; i++){
       
 13128     if( mem.nAlloc[i] ){
       
 13129       fprintf(out, "   %5d: %10d %10d %10d\n", 
       
 13130             i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
       
 13131     }
       
 13132   }
       
 13133   if( mem.nAlloc[NCSIZE-1] ){
       
 13134     fprintf(out, "   %5d: %10d %10d %10d\n",
       
 13135              NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
       
 13136              mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
       
 13137   }
       
 13138   fclose(out);
       
 13139 }
       
 13140 
       
 13141 /*
       
 13142 ** Return the number of times sqlite3MemMalloc() has been called.
       
 13143 */
       
 13144 SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
       
 13145   int i;
       
 13146   int nTotal = 0;
       
 13147   for(i=0; i<NCSIZE; i++){
       
 13148     nTotal += mem.nAlloc[i];
       
 13149   }
       
 13150   return nTotal;
       
 13151 }
       
 13152 
       
 13153 
       
 13154 #endif /* SQLITE_MEMDEBUG */
       
 13155 
       
 13156 /************** End of mem2.c ************************************************/
       
 13157 /************** Begin file mem3.c ********************************************/
       
 13158 /*
       
 13159 ** 2007 October 14
       
 13160 **
       
 13161 ** The author disclaims copyright to this source code.  In place of
       
 13162 ** a legal notice, here is a blessing:
       
 13163 **
       
 13164 **    May you do good and not evil.
       
 13165 **    May you find forgiveness for yourself and forgive others.
       
 13166 **    May you share freely, never taking more than you give.
       
 13167 **
       
 13168 *************************************************************************
       
 13169 ** This file contains the C functions that implement a memory
       
 13170 ** allocation subsystem for use by SQLite. 
       
 13171 **
       
 13172 ** This version of the memory allocation subsystem omits all
       
 13173 ** use of malloc(). The SQLite user supplies a block of memory
       
 13174 ** before calling sqlite3_initialize() from which allocations
       
 13175 ** are made and returned by the xMalloc() and xRealloc() 
       
 13176 ** implementations. Once sqlite3_initialize() has been called,
       
 13177 ** the amount of memory available to SQLite is fixed and cannot
       
 13178 ** be changed.
       
 13179 **
       
 13180 ** This version of the memory allocation subsystem is included
       
 13181 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
       
 13182 **
       
 13183 ** $Id: mem3.c,v 1.25 2008/11/19 16:52:44 danielk1977 Exp $
       
 13184 */
       
 13185 
       
 13186 /*
       
 13187 ** This version of the memory allocator is only built into the library
       
 13188 ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
       
 13189 ** mean that the library will use a memory-pool by default, just that
       
 13190 ** it is available. The mempool allocator is activated by calling
       
 13191 ** sqlite3_config().
       
 13192 */
       
 13193 #ifdef SQLITE_ENABLE_MEMSYS3
       
 13194 
       
 13195 /*
       
 13196 ** Maximum size (in Mem3Blocks) of a "small" chunk.
       
 13197 */
       
 13198 #define MX_SMALL 10
       
 13199 
       
 13200 
       
 13201 /*
       
 13202 ** Number of freelist hash slots
       
 13203 */
       
 13204 #define N_HASH  61
       
 13205 
       
 13206 /*
       
 13207 ** A memory allocation (also called a "chunk") consists of two or 
       
 13208 ** more blocks where each block is 8 bytes.  The first 8 bytes are 
       
 13209 ** a header that is not returned to the user.
       
 13210 **
       
 13211 ** A chunk is two or more blocks that is either checked out or
       
 13212 ** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
       
 13213 ** size of the allocation in blocks if the allocation is free.
       
 13214 ** The u.hdr.size4x&1 bit is true if the chunk is checked out and
       
 13215 ** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
       
 13216 ** is true if the previous chunk is checked out and false if the
       
 13217 ** previous chunk is free.  The u.hdr.prevSize field is the size of
       
 13218 ** the previous chunk in blocks if the previous chunk is on the
       
 13219 ** freelist. If the previous chunk is checked out, then
       
 13220 ** u.hdr.prevSize can be part of the data for that chunk and should
       
 13221 ** not be read or written.
       
 13222 **
       
 13223 ** We often identify a chunk by its index in mem3.aPool[].  When
       
 13224 ** this is done, the chunk index refers to the second block of
       
 13225 ** the chunk.  In this way, the first chunk has an index of 1.
       
 13226 ** A chunk index of 0 means "no such chunk" and is the equivalent
       
 13227 ** of a NULL pointer.
       
 13228 **
       
 13229 ** The second block of free chunks is of the form u.list.  The
       
 13230 ** two fields form a double-linked list of chunks of related sizes.
       
 13231 ** Pointers to the head of the list are stored in mem3.aiSmall[] 
       
 13232 ** for smaller chunks and mem3.aiHash[] for larger chunks.
       
 13233 **
       
 13234 ** The second block of a chunk is user data if the chunk is checked 
       
 13235 ** out.  If a chunk is checked out, the user data may extend into
       
 13236 ** the u.hdr.prevSize value of the following chunk.
       
 13237 */
       
 13238 typedef struct Mem3Block Mem3Block;
       
 13239 struct Mem3Block {
       
 13240   union {
       
 13241     struct {
       
 13242       u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
       
 13243       u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
       
 13244     } hdr;
       
 13245     struct {
       
 13246       u32 next;       /* Index in mem3.aPool[] of next free chunk */
       
 13247       u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
       
 13248     } list;
       
 13249   } u;
       
 13250 };
       
 13251 
       
 13252 /*
       
 13253 ** All of the static variables used by this module are collected
       
 13254 ** into a single structure named "mem3".  This is to keep the
       
 13255 ** static variables organized and to reduce namespace pollution
       
 13256 ** when this module is combined with other in the amalgamation.
       
 13257 */
       
 13258 static SQLITE_WSD struct Mem3Global {
       
 13259   /*
       
 13260   ** Memory available for allocation. nPool is the size of the array
       
 13261   ** (in Mem3Blocks) pointed to by aPool less 2.
       
 13262   */
       
 13263   u32 nPool;
       
 13264   Mem3Block *aPool;
       
 13265 
       
 13266   /*
       
 13267   ** True if we are evaluating an out-of-memory callback.
       
 13268   */
       
 13269   int alarmBusy;
       
 13270   
       
 13271   /*
       
 13272   ** Mutex to control access to the memory allocation subsystem.
       
 13273   */
       
 13274   sqlite3_mutex *mutex;
       
 13275   
       
 13276   /*
       
 13277   ** The minimum amount of free space that we have seen.
       
 13278   */
       
 13279   u32 mnMaster;
       
 13280 
       
 13281   /*
       
 13282   ** iMaster is the index of the master chunk.  Most new allocations
       
 13283   ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
       
 13284   ** of the current master.  iMaster is 0 if there is not master chunk.
       
 13285   ** The master chunk is not in either the aiHash[] or aiSmall[].
       
 13286   */
       
 13287   u32 iMaster;
       
 13288   u32 szMaster;
       
 13289 
       
 13290   /*
       
 13291   ** Array of lists of free blocks according to the block size 
       
 13292   ** for smaller chunks, or a hash on the block size for larger
       
 13293   ** chunks.
       
 13294   */
       
 13295   u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
       
 13296   u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
       
 13297 } mem3 = { 97535575 };
       
 13298 
       
 13299 #define mem3 GLOBAL(struct Mem3Global, mem3)
       
 13300 
       
 13301 /*
       
 13302 ** Unlink the chunk at mem3.aPool[i] from list it is currently
       
 13303 ** on.  *pRoot is the list that i is a member of.
       
 13304 */
       
 13305 static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
       
 13306   u32 next = mem3.aPool[i].u.list.next;
       
 13307   u32 prev = mem3.aPool[i].u.list.prev;
       
 13308   assert( sqlite3_mutex_held(mem3.mutex) );
       
 13309   if( prev==0 ){
       
 13310     *pRoot = next;
       
 13311   }else{
       
 13312     mem3.aPool[prev].u.list.next = next;
       
 13313   }
       
 13314   if( next ){
       
 13315     mem3.aPool[next].u.list.prev = prev;
       
 13316   }
       
 13317   mem3.aPool[i].u.list.next = 0;
       
 13318   mem3.aPool[i].u.list.prev = 0;
       
 13319 }
       
 13320 
       
 13321 /*
       
 13322 ** Unlink the chunk at index i from 
       
 13323 ** whatever list is currently a member of.
       
 13324 */
       
 13325 static void memsys3Unlink(u32 i){
       
 13326   u32 size, hash;
       
 13327   assert( sqlite3_mutex_held(mem3.mutex) );
       
 13328   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
       
 13329   assert( i>=1 );
       
 13330   size = mem3.aPool[i-1].u.hdr.size4x/4;
       
 13331   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
       
 13332   assert( size>=2 );
       
 13333   if( size <= MX_SMALL ){
       
 13334     memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
       
 13335   }else{
       
 13336     hash = size % N_HASH;
       
 13337     memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
       
 13338   }
       
 13339 }
       
 13340 
       
 13341 /*
       
 13342 ** Link the chunk at mem3.aPool[i] so that is on the list rooted
       
 13343 ** at *pRoot.
       
 13344 */
       
 13345 static void memsys3LinkIntoList(u32 i, u32 *pRoot){
       
 13346   assert( sqlite3_mutex_held(mem3.mutex) );
       
 13347   mem3.aPool[i].u.list.next = *pRoot;
       
 13348   mem3.aPool[i].u.list.prev = 0;
       
 13349   if( *pRoot ){
       
 13350     mem3.aPool[*pRoot].u.list.prev = i;
       
 13351   }
       
 13352   *pRoot = i;
       
 13353 }
       
 13354 
       
 13355 /*
       
 13356 ** Link the chunk at index i into either the appropriate
       
 13357 ** small chunk list, or into the large chunk hash table.
       
 13358 */
       
 13359 static void memsys3Link(u32 i){
       
 13360   u32 size, hash;
       
 13361   assert( sqlite3_mutex_held(mem3.mutex) );
       
 13362   assert( i>=1 );
       
 13363   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
       
 13364   size = mem3.aPool[i-1].u.hdr.size4x/4;
       
 13365   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
       
 13366   assert( size>=2 );
       
 13367   if( size <= MX_SMALL ){
       
 13368     memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
       
 13369   }else{
       
 13370     hash = size % N_HASH;
       
 13371     memsys3LinkIntoList(i, &mem3.aiHash[hash]);
       
 13372   }
       
 13373 }
       
 13374 
       
 13375 /*
       
 13376 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
       
 13377 ** will already be held (obtained by code in malloc.c) if
       
 13378 ** sqlite3GlobalConfig.bMemStat is true.
       
 13379 */
       
 13380 static void memsys3Enter(void){
       
 13381   if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
       
 13382     mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
       
 13383   }
       
 13384   sqlite3_mutex_enter(mem3.mutex);
       
 13385 }
       
 13386 static void memsys3Leave(void){
       
 13387   sqlite3_mutex_leave(mem3.mutex);
       
 13388 }
       
 13389 
       
 13390 /*
       
 13391 ** Called when we are unable to satisfy an allocation of nBytes.
       
 13392 */
       
 13393 static void memsys3OutOfMemory(int nByte){
       
 13394   if( !mem3.alarmBusy ){
       
 13395     mem3.alarmBusy = 1;
       
 13396     assert( sqlite3_mutex_held(mem3.mutex) );
       
 13397     sqlite3_mutex_leave(mem3.mutex);
       
 13398     sqlite3_release_memory(nByte);
       
 13399     sqlite3_mutex_enter(mem3.mutex);
       
 13400     mem3.alarmBusy = 0;
       
 13401   }
       
 13402 }
       
 13403 
       
 13404 
       
 13405 /*
       
 13406 ** Chunk i is a free chunk that has been unlinked.  Adjust its 
       
 13407 ** size parameters for check-out and return a pointer to the 
       
 13408 ** user portion of the chunk.
       
 13409 */
       
 13410 static void *memsys3Checkout(u32 i, u32 nBlock){
       
 13411   u32 x;
       
 13412   assert( sqlite3_mutex_held(mem3.mutex) );
       
 13413   assert( i>=1 );
       
 13414   assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
       
 13415   assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
       
 13416   x = mem3.aPool[i-1].u.hdr.size4x;
       
 13417   mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
       
 13418   mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
       
 13419   mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
       
 13420   return &mem3.aPool[i];
       
 13421 }
       
 13422 
       
 13423 /*
       
 13424 ** Carve a piece off of the end of the mem3.iMaster free chunk.
       
 13425 ** Return a pointer to the new allocation.  Or, if the master chunk
       
 13426 ** is not large enough, return 0.
       
 13427 */
       
 13428 static void *memsys3FromMaster(u32 nBlock){
       
 13429   assert( sqlite3_mutex_held(mem3.mutex) );
       
 13430   assert( mem3.szMaster>=nBlock );
       
 13431   if( nBlock>=mem3.szMaster-1 ){
       
 13432     /* Use the entire master */
       
 13433     void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
       
 13434     mem3.iMaster = 0;
       
 13435     mem3.szMaster = 0;
       
 13436     mem3.mnMaster = 0;
       
 13437     return p;
       
 13438   }else{
       
 13439     /* Split the master block.  Return the tail. */
       
 13440     u32 newi, x;
       
 13441     newi = mem3.iMaster + mem3.szMaster - nBlock;
       
 13442     assert( newi > mem3.iMaster+1 );
       
 13443     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
       
 13444     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
       
 13445     mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
       
 13446     mem3.szMaster -= nBlock;
       
 13447     mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
       
 13448     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
       
 13449     mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
       
 13450     if( mem3.szMaster < mem3.mnMaster ){
       
 13451       mem3.mnMaster = mem3.szMaster;
       
 13452     }
       
 13453     return (void*)&mem3.aPool[newi];
       
 13454   }
       
 13455 }
       
 13456 
       
 13457 /*
       
 13458 ** *pRoot is the head of a list of free chunks of the same size
       
 13459 ** or same size hash.  In other words, *pRoot is an entry in either
       
 13460 ** mem3.aiSmall[] or mem3.aiHash[].  
       
 13461 **
       
 13462 ** This routine examines all entries on the given list and tries
       
 13463 ** to coalesce each entries with adjacent free chunks.  
       
 13464 **
       
 13465 ** If it sees a chunk that is larger than mem3.iMaster, it replaces 
       
 13466 ** the current mem3.iMaster with the new larger chunk.  In order for
       
 13467 ** this mem3.iMaster replacement to work, the master chunk must be
       
 13468 ** linked into the hash tables.  That is not the normal state of
       
 13469 ** affairs, of course.  The calling routine must link the master
       
 13470 ** chunk before invoking this routine, then must unlink the (possibly
       
 13471 ** changed) master chunk once this routine has finished.
       
 13472 */
       
 13473 static void memsys3Merge(u32 *pRoot){
       
 13474   u32 iNext, prev, size, i, x;
       
 13475 
       
 13476   assert( sqlite3_mutex_held(mem3.mutex) );
       
 13477   for(i=*pRoot; i>0; i=iNext){
       
 13478     iNext = mem3.aPool[i].u.list.next;
       
 13479     size = mem3.aPool[i-1].u.hdr.size4x;
       
 13480     assert( (size&1)==0 );
       
 13481     if( (size&2)==0 ){
       
 13482       memsys3UnlinkFromList(i, pRoot);
       
 13483       assert( i > mem3.aPool[i-1].u.hdr.prevSize );
       
 13484       prev = i - mem3.aPool[i-1].u.hdr.prevSize;
       
 13485       if( prev==iNext ){
       
 13486         iNext = mem3.aPool[prev].u.list.next;
       
 13487       }
       
 13488       memsys3Unlink(prev);
       
 13489       size = i + size/4 - prev;
       
 13490       x = mem3.aPool[prev-1].u.hdr.size4x & 2;
       
 13491       mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
       
 13492       mem3.aPool[prev+size-1].u.hdr.prevSize = size;
       
 13493       memsys3Link(prev);
       
 13494       i = prev;
       
 13495     }else{
       
 13496       size /= 4;
       
 13497     }
       
 13498     if( size>mem3.szMaster ){
       
 13499       mem3.iMaster = i;
       
 13500       mem3.szMaster = size;
       
 13501     }
       
 13502   }
       
 13503 }
       
 13504 
       
 13505 /*
       
 13506 ** Return a block of memory of at least nBytes in size.
       
 13507 ** Return NULL if unable.
       
 13508 **
       
 13509 ** This function assumes that the necessary mutexes, if any, are
       
 13510 ** already held by the caller. Hence "Unsafe".
       
 13511 */
       
 13512 static void *memsys3MallocUnsafe(int nByte){
       
 13513   u32 i;
       
 13514   u32 nBlock;
       
 13515   u32 toFree;
       
 13516 
       
 13517   assert( sqlite3_mutex_held(mem3.mutex) );
       
 13518   assert( sizeof(Mem3Block)==8 );
       
 13519   if( nByte<=12 ){
       
 13520     nBlock = 2;
       
 13521   }else{
       
 13522     nBlock = (nByte + 11)/8;
       
 13523   }
       
 13524   assert( nBlock>=2 );
       
 13525 
       
 13526   /* STEP 1:
       
 13527   ** Look for an entry of the correct size in either the small
       
 13528   ** chunk table or in the large chunk hash table.  This is
       
 13529   ** successful most of the time (about 9 times out of 10).
       
 13530   */
       
 13531   if( nBlock <= MX_SMALL ){
       
 13532     i = mem3.aiSmall[nBlock-2];
       
 13533     if( i>0 ){
       
 13534       memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
       
 13535       return memsys3Checkout(i, nBlock);
       
 13536     }
       
 13537   }else{
       
 13538     int hash = nBlock % N_HASH;
       
 13539     for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
       
 13540       if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
       
 13541         memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
       
 13542         return memsys3Checkout(i, nBlock);
       
 13543       }
       
 13544     }
       
 13545   }
       
 13546 
       
 13547   /* STEP 2:
       
 13548   ** Try to satisfy the allocation by carving a piece off of the end
       
 13549   ** of the master chunk.  This step usually works if step 1 fails.
       
 13550   */
       
 13551   if( mem3.szMaster>=nBlock ){
       
 13552     return memsys3FromMaster(nBlock);
       
 13553   }
       
 13554 
       
 13555 
       
 13556   /* STEP 3:  
       
 13557   ** Loop through the entire memory pool.  Coalesce adjacent free
       
 13558   ** chunks.  Recompute the master chunk as the largest free chunk.
       
 13559   ** Then try again to satisfy the allocation by carving a piece off
       
 13560   ** of the end of the master chunk.  This step happens very
       
 13561   ** rarely (we hope!)
       
 13562   */
       
 13563   for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
       
 13564     memsys3OutOfMemory(toFree);
       
 13565     if( mem3.iMaster ){
       
 13566       memsys3Link(mem3.iMaster);
       
 13567       mem3.iMaster = 0;
       
 13568       mem3.szMaster = 0;
       
 13569     }
       
 13570     for(i=0; i<N_HASH; i++){
       
 13571       memsys3Merge(&mem3.aiHash[i]);
       
 13572     }
       
 13573     for(i=0; i<MX_SMALL-1; i++){
       
 13574       memsys3Merge(&mem3.aiSmall[i]);
       
 13575     }
       
 13576     if( mem3.szMaster ){
       
 13577       memsys3Unlink(mem3.iMaster);
       
 13578       if( mem3.szMaster>=nBlock ){
       
 13579         return memsys3FromMaster(nBlock);
       
 13580       }
       
 13581     }
       
 13582   }
       
 13583 
       
 13584   /* If none of the above worked, then we fail. */
       
 13585   return 0;
       
 13586 }
       
 13587 
       
 13588 /*
       
 13589 ** Free an outstanding memory allocation.
       
 13590 **
       
 13591 ** This function assumes that the necessary mutexes, if any, are
       
 13592 ** already held by the caller. Hence "Unsafe".
       
 13593 */
       
 13594 void memsys3FreeUnsafe(void *pOld){
       
 13595   Mem3Block *p = (Mem3Block*)pOld;
       
 13596   int i;
       
 13597   u32 size, x;
       
 13598   assert( sqlite3_mutex_held(mem3.mutex) );
       
 13599   assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
       
 13600   i = p - mem3.aPool;
       
 13601   assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
       
 13602   size = mem3.aPool[i-1].u.hdr.size4x/4;
       
 13603   assert( i+size<=mem3.nPool+1 );
       
 13604   mem3.aPool[i-1].u.hdr.size4x &= ~1;
       
 13605   mem3.aPool[i+size-1].u.hdr.prevSize = size;
       
 13606   mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
       
 13607   memsys3Link(i);
       
 13608 
       
 13609   /* Try to expand the master using the newly freed chunk */
       
 13610   if( mem3.iMaster ){
       
 13611     while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
       
 13612       size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
       
 13613       mem3.iMaster -= size;
       
 13614       mem3.szMaster += size;
       
 13615       memsys3Unlink(mem3.iMaster);
       
 13616       x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
       
 13617       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
       
 13618       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
       
 13619     }
       
 13620     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
       
 13621     while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
       
 13622       memsys3Unlink(mem3.iMaster+mem3.szMaster);
       
 13623       mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
       
 13624       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
       
 13625       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
       
 13626     }
       
 13627   }
       
 13628 }
       
 13629 
       
 13630 /*
       
 13631 ** Return the size of an outstanding allocation, in bytes.  The
       
 13632 ** size returned omits the 8-byte header overhead.  This only
       
 13633 ** works for chunks that are currently checked out.
       
 13634 */
       
 13635 static int memsys3Size(void *p){
       
 13636   Mem3Block *pBlock;
       
 13637   if( p==0 ) return 0;
       
 13638   pBlock = (Mem3Block*)p;
       
 13639   assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
       
 13640   return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
       
 13641 }
       
 13642 
       
 13643 /*
       
 13644 ** Round up a request size to the next valid allocation size.
       
 13645 */
       
 13646 static int memsys3Roundup(int n){
       
 13647   if( n<=12 ){
       
 13648     return 12;
       
 13649   }else{
       
 13650     return ((n+11)&~7) - 4;
       
 13651   }
       
 13652 }
       
 13653 
       
 13654 /*
       
 13655 ** Allocate nBytes of memory.
       
 13656 */
       
 13657 static void *memsys3Malloc(int nBytes){
       
 13658   sqlite3_int64 *p;
       
 13659   assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
       
 13660   memsys3Enter();
       
 13661   p = memsys3MallocUnsafe(nBytes);
       
 13662   memsys3Leave();
       
 13663   return (void*)p; 
       
 13664 }
       
 13665 
       
 13666 /*
       
 13667 ** Free memory.
       
 13668 */
       
 13669 void memsys3Free(void *pPrior){
       
 13670   assert( pPrior );
       
 13671   memsys3Enter();
       
 13672   memsys3FreeUnsafe(pPrior);
       
 13673   memsys3Leave();
       
 13674 }
       
 13675 
       
 13676 /*
       
 13677 ** Change the size of an existing memory allocation
       
 13678 */
       
 13679 void *memsys3Realloc(void *pPrior, int nBytes){
       
 13680   int nOld;
       
 13681   void *p;
       
 13682   if( pPrior==0 ){
       
 13683     return sqlite3_malloc(nBytes);
       
 13684   }
       
 13685   if( nBytes<=0 ){
       
 13686     sqlite3_free(pPrior);
       
 13687     return 0;
       
 13688   }
       
 13689   nOld = memsys3Size(pPrior);
       
 13690   if( nBytes<=nOld && nBytes>=nOld-128 ){
       
 13691     return pPrior;
       
 13692   }
       
 13693   memsys3Enter();
       
 13694   p = memsys3MallocUnsafe(nBytes);
       
 13695   if( p ){
       
 13696     if( nOld<nBytes ){
       
 13697       memcpy(p, pPrior, nOld);
       
 13698     }else{
       
 13699       memcpy(p, pPrior, nBytes);
       
 13700     }
       
 13701     memsys3FreeUnsafe(pPrior);
       
 13702   }
       
 13703   memsys3Leave();
       
 13704   return p;
       
 13705 }
       
 13706 
       
 13707 /*
       
 13708 ** Initialize this module.
       
 13709 */
       
 13710 static int memsys3Init(void *NotUsed){
       
 13711   UNUSED_PARAMETER(NotUsed);
       
 13712   if( !sqlite3GlobalConfig.pHeap ){
       
 13713     return SQLITE_ERROR;
       
 13714   }
       
 13715 
       
 13716   /* Store a pointer to the memory block in global structure mem3. */
       
 13717   assert( sizeof(Mem3Block)==8 );
       
 13718   mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
       
 13719   mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
       
 13720 
       
 13721   /* Initialize the master block. */
       
 13722   mem3.szMaster = mem3.nPool;
       
 13723   mem3.mnMaster = mem3.szMaster;
       
 13724   mem3.iMaster = 1;
       
 13725   mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
       
 13726   mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
       
 13727   mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
       
 13728 
       
 13729   return SQLITE_OK;
       
 13730 }
       
 13731 
       
 13732 /*
       
 13733 ** Deinitialize this module.
       
 13734 */
       
 13735 static void memsys3Shutdown(void *NotUsed){
       
 13736   UNUSED_PARAMETER(NotUsed);
       
 13737   mem3.mutex = 0;
       
 13738   return;
       
 13739 }
       
 13740 
       
 13741 
       
 13742 
       
 13743 /*
       
 13744 ** Open the file indicated and write a log of all unfreed memory 
       
 13745 ** allocations into that log.
       
 13746 */
       
 13747 SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
       
 13748 #ifdef SQLITE_DEBUG
       
 13749   FILE *out;
       
 13750   u32 i, j;
       
 13751   u32 size;
       
 13752   if( zFilename==0 || zFilename[0]==0 ){
       
 13753     out = stdout;
       
 13754   }else{
       
 13755     out = fopen(zFilename, "w");
       
 13756     if( out==0 ){
       
 13757       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
       
 13758                       zFilename);
       
 13759       return;
       
 13760     }
       
 13761   }
       
 13762   memsys3Enter();
       
 13763   fprintf(out, "CHUNKS:\n");
       
 13764   for(i=1; i<=mem3.nPool; i+=size/4){
       
 13765     size = mem3.aPool[i-1].u.hdr.size4x;
       
 13766     if( size/4<=1 ){
       
 13767       fprintf(out, "%p size error\n", &mem3.aPool[i]);
       
 13768       assert( 0 );
       
 13769       break;
       
 13770     }
       
 13771     if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
       
 13772       fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
       
 13773       assert( 0 );
       
 13774       break;
       
 13775     }
       
 13776     if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
       
 13777       fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
       
 13778       assert( 0 );
       
 13779       break;
       
 13780     }
       
 13781     if( size&1 ){
       
 13782       fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
       
 13783     }else{
       
 13784       fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
       
 13785                   i==mem3.iMaster ? " **master**" : "");
       
 13786     }
       
 13787   }
       
 13788   for(i=0; i<MX_SMALL-1; i++){
       
 13789     if( mem3.aiSmall[i]==0 ) continue;
       
 13790     fprintf(out, "small(%2d):", i);
       
 13791     for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
       
 13792       fprintf(out, " %p(%d)", &mem3.aPool[j],
       
 13793               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
       
 13794     }
       
 13795     fprintf(out, "\n"); 
       
 13796   }
       
 13797   for(i=0; i<N_HASH; i++){
       
 13798     if( mem3.aiHash[i]==0 ) continue;
       
 13799     fprintf(out, "hash(%2d):", i);
       
 13800     for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
       
 13801       fprintf(out, " %p(%d)", &mem3.aPool[j],
       
 13802               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
       
 13803     }
       
 13804     fprintf(out, "\n"); 
       
 13805   }
       
 13806   fprintf(out, "master=%d\n", mem3.iMaster);
       
 13807   fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
       
 13808   fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
       
 13809   sqlite3_mutex_leave(mem3.mutex);
       
 13810   if( out==stdout ){
       
 13811     fflush(stdout);
       
 13812   }else{
       
 13813     fclose(out);
       
 13814   }
       
 13815 #else
       
 13816   UNUSED_PARAMETER(zFilename);
       
 13817 #endif
       
 13818 }
       
 13819 
       
 13820 /*
       
 13821 ** This routine is the only routine in this file with external 
       
 13822 ** linkage.
       
 13823 **
       
 13824 ** Populate the low-level memory allocation function pointers in
       
 13825 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
       
 13826 ** arguments specify the block of memory to manage.
       
 13827 **
       
 13828 ** This routine is only called by sqlite3_config(), and therefore
       
 13829 ** is not required to be threadsafe (it is not).
       
 13830 */
       
 13831 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
       
 13832   static const sqlite3_mem_methods mempoolMethods = {
       
 13833      memsys3Malloc,
       
 13834      memsys3Free,
       
 13835      memsys3Realloc,
       
 13836      memsys3Size,
       
 13837      memsys3Roundup,
       
 13838      memsys3Init,
       
 13839      memsys3Shutdown,
       
 13840      0
       
 13841   };
       
 13842   return &mempoolMethods;
       
 13843 }
       
 13844 
       
 13845 #endif /* SQLITE_ENABLE_MEMSYS3 */
       
 13846 
       
 13847 /************** End of mem3.c ************************************************/
       
 13848 /************** Begin file mem5.c ********************************************/
       
 13849 /*
       
 13850 ** 2007 October 14
       
 13851 **
       
 13852 ** The author disclaims copyright to this source code.  In place of
       
 13853 ** a legal notice, here is a blessing:
       
 13854 **
       
 13855 **    May you do good and not evil.
       
 13856 **    May you find forgiveness for yourself and forgive others.
       
 13857 **    May you share freely, never taking more than you give.
       
 13858 **
       
 13859 *************************************************************************
       
 13860 ** This file contains the C functions that implement a memory
       
 13861 ** allocation subsystem for use by SQLite. 
       
 13862 **
       
 13863 ** This version of the memory allocation subsystem omits all
       
 13864 ** use of malloc(). The application gives SQLite a block of memory
       
 13865 ** before calling sqlite3_initialize() from which allocations
       
 13866 ** are made and returned by the xMalloc() and xRealloc() 
       
 13867 ** implementations. Once sqlite3_initialize() has been called,
       
 13868 ** the amount of memory available to SQLite is fixed and cannot
       
 13869 ** be changed.
       
 13870 **
       
 13871 ** This version of the memory allocation subsystem is included
       
 13872 ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
       
 13873 **
       
 13874 ** This memory allocator uses the following algorithm:
       
 13875 **
       
 13876 **   1.  All memory allocations sizes are rounded up to a power of 2.
       
 13877 **
       
 13878 **   2.  If two adjacent free blocks are the halves of a larger block,
       
 13879 **       then the two blocks are coalesed into the single larger block.
       
 13880 **
       
 13881 **   3.  New memory is allocated from the first available free block.
       
 13882 **
       
 13883 ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
       
 13884 ** Concerning Dynamic Storage Allocation". Journal of the Association for
       
 13885 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
       
 13886 ** 
       
 13887 ** Let n be the size of the largest allocation divided by the minimum
       
 13888 ** allocation size (after rounding all sizes up to a power of 2.)  Let M
       
 13889 ** be the maximum amount of memory ever outstanding at one time.  Let
       
 13890 ** N be the total amount of memory available for allocation.  Robson
       
 13891 ** proved that this memory allocator will never breakdown due to 
       
 13892 ** fragmentation as long as the following constraint holds:
       
 13893 **
       
 13894 **      N >=  M*(1 + log2(n)/2) - n + 1
       
 13895 **
       
 13896 ** The sqlite3_status() logic tracks the maximum values of n and M so
       
 13897 ** that an application can, at any time, verify this constraint.
       
 13898 */
       
 13899 
       
 13900 /*
       
 13901 ** This version of the memory allocator is used only when 
       
 13902 ** SQLITE_ENABLE_MEMSYS5 is defined.
       
 13903 */
       
 13904 #ifdef SQLITE_ENABLE_MEMSYS5
       
 13905 
       
 13906 /*
       
 13907 ** A minimum allocation is an instance of the following structure.
       
 13908 ** Larger allocations are an array of these structures where the
       
 13909 ** size of the array is a power of 2.
       
 13910 **
       
 13911 ** The size of this object must be a power of two.  That fact is
       
 13912 ** verified in memsys5Init().
       
 13913 */
       
 13914 typedef struct Mem5Link Mem5Link;
       
 13915 struct Mem5Link {
       
 13916   int next;       /* Index of next free chunk */
       
 13917   int prev;       /* Index of previous free chunk */
       
 13918 };
       
 13919 
       
 13920 /*
       
 13921 ** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
       
 13922 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
       
 13923 ** it is not actually possible to reach this limit.
       
 13924 */
       
 13925 #define LOGMAX 30
       
 13926 
       
 13927 /*
       
 13928 ** Masks used for mem5.aCtrl[] elements.
       
 13929 */
       
 13930 #define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block */
       
 13931 #define CTRL_FREE     0x20    /* True if not checked out */
       
 13932 
       
 13933 /*
       
 13934 ** All of the static variables used by this module are collected
       
 13935 ** into a single structure named "mem5".  This is to keep the
       
 13936 ** static variables organized and to reduce namespace pollution
       
 13937 ** when this module is combined with other in the amalgamation.
       
 13938 */
       
 13939 static SQLITE_WSD struct Mem5Global {
       
 13940   /*
       
 13941   ** Memory available for allocation
       
 13942   */
       
 13943   int szAtom;      /* Smallest possible allocation in bytes */
       
 13944   int nBlock;      /* Number of szAtom sized blocks in zPool */
       
 13945   u8 *zPool;       /* Memory available to be allocated */
       
 13946   
       
 13947   /*
       
 13948   ** Mutex to control access to the memory allocation subsystem.
       
 13949   */
       
 13950   sqlite3_mutex *mutex;
       
 13951 
       
 13952   /*
       
 13953   ** Performance statistics
       
 13954   */
       
 13955   u64 nAlloc;         /* Total number of calls to malloc */
       
 13956   u64 totalAlloc;     /* Total of all malloc calls - includes internal frag */
       
 13957   u64 totalExcess;    /* Total internal fragmentation */
       
 13958   u32 currentOut;     /* Current checkout, including internal fragmentation */
       
 13959   u32 currentCount;   /* Current number of distinct checkouts */
       
 13960   u32 maxOut;         /* Maximum instantaneous currentOut */
       
 13961   u32 maxCount;       /* Maximum instantaneous currentCount */
       
 13962   u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
       
 13963   
       
 13964   /*
       
 13965   ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
       
 13966   ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
       
 13967   ** and so forth.
       
 13968   */
       
 13969   int aiFreelist[LOGMAX+1];
       
 13970 
       
 13971   /*
       
 13972   ** Space for tracking which blocks are checked out and the size
       
 13973   ** of each block.  One byte per block.
       
 13974   */
       
 13975   u8 *aCtrl;
       
 13976 
       
 13977 } mem5 = { 0 };
       
 13978 
       
 13979 /*
       
 13980 ** Access the static variable through a macro for SQLITE_OMIT_WSD
       
 13981 */
       
 13982 #define mem5 GLOBAL(struct Mem5Global, mem5)
       
 13983 
       
 13984 /*
       
 13985 ** Assuming mem5.zPool is divided up into an array of Mem5Link
       
 13986 ** structures, return a pointer to the idx-th such lik.
       
 13987 */
       
 13988 #define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
       
 13989 
       
 13990 /*
       
 13991 ** Unlink the chunk at mem5.aPool[i] from list it is currently
       
 13992 ** on.  It should be found on mem5.aiFreelist[iLogsize].
       
 13993 */
       
 13994 static void memsys5Unlink(int i, int iLogsize){
       
 13995   int next, prev;
       
 13996   assert( i>=0 && i<mem5.nBlock );
       
 13997   assert( iLogsize>=0 && iLogsize<=LOGMAX );
       
 13998   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
       
 13999 
       
 14000   next = MEM5LINK(i)->next;
       
 14001   prev = MEM5LINK(i)->prev;
       
 14002   if( prev<0 ){
       
 14003     mem5.aiFreelist[iLogsize] = next;
       
 14004   }else{
       
 14005     MEM5LINK(prev)->next = next;
       
 14006   }
       
 14007   if( next>=0 ){
       
 14008     MEM5LINK(next)->prev = prev;
       
 14009   }
       
 14010 }
       
 14011 
       
 14012 /*
       
 14013 ** Link the chunk at mem5.aPool[i] so that is on the iLogsize
       
 14014 ** free list.
       
 14015 */
       
 14016 static void memsys5Link(int i, int iLogsize){
       
 14017   int x;
       
 14018   assert( sqlite3_mutex_held(mem5.mutex) );
       
 14019   assert( i>=0 && i<mem5.nBlock );
       
 14020   assert( iLogsize>=0 && iLogsize<=LOGMAX );
       
 14021   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
       
 14022 
       
 14023   x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
       
 14024   MEM5LINK(i)->prev = -1;
       
 14025   if( x>=0 ){
       
 14026     assert( x<mem5.nBlock );
       
 14027     MEM5LINK(x)->prev = i;
       
 14028   }
       
 14029   mem5.aiFreelist[iLogsize] = i;
       
 14030 }
       
 14031 
       
 14032 /*
       
 14033 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
       
 14034 ** will already be held (obtained by code in malloc.c) if
       
 14035 ** sqlite3GlobalConfig.bMemStat is true.
       
 14036 */
       
 14037 static void memsys5Enter(void){
       
 14038   sqlite3_mutex_enter(mem5.mutex);
       
 14039 }
       
 14040 static void memsys5Leave(void){
       
 14041   sqlite3_mutex_leave(mem5.mutex);
       
 14042 }
       
 14043 
       
 14044 /*
       
 14045 ** Return the size of an outstanding allocation, in bytes.  The
       
 14046 ** size returned omits the 8-byte header overhead.  This only
       
 14047 ** works for chunks that are currently checked out.
       
 14048 */
       
 14049 static int memsys5Size(void *p){
       
 14050   int iSize = 0;
       
 14051   if( p ){
       
 14052     int i = ((u8 *)p-mem5.zPool)/mem5.szAtom;
       
 14053     assert( i>=0 && i<mem5.nBlock );
       
 14054     iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
       
 14055   }
       
 14056   return iSize;
       
 14057 }
       
 14058 
       
 14059 /*
       
 14060 ** Find the first entry on the freelist iLogsize.  Unlink that
       
 14061 ** entry and return its index. 
       
 14062 */
       
 14063 static int memsys5UnlinkFirst(int iLogsize){
       
 14064   int i;
       
 14065   int iFirst;
       
 14066 
       
 14067   assert( iLogsize>=0 && iLogsize<=LOGMAX );
       
 14068   i = iFirst = mem5.aiFreelist[iLogsize];
       
 14069   assert( iFirst>=0 );
       
 14070   while( i>0 ){
       
 14071     if( i<iFirst ) iFirst = i;
       
 14072     i = MEM5LINK(i)->next;
       
 14073   }
       
 14074   memsys5Unlink(iFirst, iLogsize);
       
 14075   return iFirst;
       
 14076 }
       
 14077 
       
 14078 /*
       
 14079 ** Return a block of memory of at least nBytes in size.
       
 14080 ** Return NULL if unable.  Return NULL if nBytes==0.
       
 14081 **
       
 14082 ** The caller guarantees that nByte positive.
       
 14083 **
       
 14084 ** The caller has obtained a mutex prior to invoking this
       
 14085 ** routine so there is never any chance that two or more
       
 14086 ** threads can be in this routine at the same time.
       
 14087 */
       
 14088 static void *memsys5MallocUnsafe(int nByte){
       
 14089   int i;           /* Index of a mem5.aPool[] slot */
       
 14090   int iBin;        /* Index into mem5.aiFreelist[] */
       
 14091   int iFullSz;     /* Size of allocation rounded up to power of 2 */
       
 14092   int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
       
 14093 
       
 14094   /* nByte must be a positive */
       
 14095   assert( nByte>0 );
       
 14096 
       
 14097   /* Keep track of the maximum allocation request.  Even unfulfilled
       
 14098   ** requests are counted */
       
 14099   if( (u32)nByte>mem5.maxRequest ){
       
 14100     mem5.maxRequest = nByte;
       
 14101   }
       
 14102 
       
 14103   /* Abort if the requested allocation size is larger than the largest
       
 14104   ** power of two that we can represent using 32-bit signed integers.
       
 14105   */
       
 14106   if( nByte > 0x40000000 ){
       
 14107     return 0;
       
 14108   }
       
 14109 
       
 14110   /* Round nByte up to the next valid power of two */
       
 14111   for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
       
 14112 
       
 14113   /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
       
 14114   ** block.  If not, then split a block of the next larger power of
       
 14115   ** two in order to create a new free block of size iLogsize.
       
 14116   */
       
 14117   for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
       
 14118   if( iBin>LOGMAX ) return 0;
       
 14119   i = memsys5UnlinkFirst(iBin);
       
 14120   while( iBin>iLogsize ){
       
 14121     int newSize;
       
 14122 
       
 14123     iBin--;
       
 14124     newSize = 1 << iBin;
       
 14125     mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
       
 14126     memsys5Link(i+newSize, iBin);
       
 14127   }
       
 14128   mem5.aCtrl[i] = iLogsize;
       
 14129 
       
 14130   /* Update allocator performance statistics. */
       
 14131   mem5.nAlloc++;
       
 14132   mem5.totalAlloc += iFullSz;
       
 14133   mem5.totalExcess += iFullSz - nByte;
       
 14134   mem5.currentCount++;
       
 14135   mem5.currentOut += iFullSz;
       
 14136   if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
       
 14137   if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
       
 14138 
       
 14139   /* Return a pointer to the allocated memory. */
       
 14140   return (void*)&mem5.zPool[i*mem5.szAtom];
       
 14141 }
       
 14142 
       
 14143 /*
       
 14144 ** Free an outstanding memory allocation.
       
 14145 */
       
 14146 static void memsys5FreeUnsafe(void *pOld){
       
 14147   u32 size, iLogsize;
       
 14148   int iBlock;
       
 14149 
       
 14150   /* Set iBlock to the index of the block pointed to by pOld in 
       
 14151   ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
       
 14152   */
       
 14153   iBlock = ((u8 *)pOld-mem5.zPool)/mem5.szAtom;
       
 14154 
       
 14155   /* Check that the pointer pOld points to a valid, non-free block. */
       
 14156   assert( iBlock>=0 && iBlock<mem5.nBlock );
       
 14157   assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
       
 14158   assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
       
 14159 
       
 14160   iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
       
 14161   size = 1<<iLogsize;
       
 14162   assert( iBlock+size-1<(u32)mem5.nBlock );
       
 14163 
       
 14164   mem5.aCtrl[iBlock] |= CTRL_FREE;
       
 14165   mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
       
 14166   assert( mem5.currentCount>0 );
       
 14167   assert( mem5.currentOut>=(size*mem5.szAtom) );
       
 14168   mem5.currentCount--;
       
 14169   mem5.currentOut -= size*mem5.szAtom;
       
 14170   assert( mem5.currentOut>0 || mem5.currentCount==0 );
       
 14171   assert( mem5.currentCount>0 || mem5.currentOut==0 );
       
 14172 
       
 14173   mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
       
 14174   while( ALWAYS(iLogsize<LOGMAX) ){
       
 14175     int iBuddy;
       
 14176     if( (iBlock>>iLogsize) & 1 ){
       
 14177       iBuddy = iBlock - size;
       
 14178     }else{
       
 14179       iBuddy = iBlock + size;
       
 14180     }
       
 14181     assert( iBuddy>=0 );
       
 14182     if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
       
 14183     if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
       
 14184     memsys5Unlink(iBuddy, iLogsize);
       
 14185     iLogsize++;
       
 14186     if( iBuddy<iBlock ){
       
 14187       mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
       
 14188       mem5.aCtrl[iBlock] = 0;
       
 14189       iBlock = iBuddy;
       
 14190     }else{
       
 14191       mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
       
 14192       mem5.aCtrl[iBuddy] = 0;
       
 14193     }
       
 14194     size *= 2;
       
 14195   }
       
 14196   memsys5Link(iBlock, iLogsize);
       
 14197 }
       
 14198 
       
 14199 /*
       
 14200 ** Allocate nBytes of memory
       
 14201 */
       
 14202 static void *memsys5Malloc(int nBytes){
       
 14203   sqlite3_int64 *p = 0;
       
 14204   if( nBytes>0 ){
       
 14205     memsys5Enter();
       
 14206     p = memsys5MallocUnsafe(nBytes);
       
 14207     memsys5Leave();
       
 14208   }
       
 14209   return (void*)p; 
       
 14210 }
       
 14211 
       
 14212 /*
       
 14213 ** Free memory.
       
 14214 **
       
 14215 ** The outer layer memory allocator prevents this routine from
       
 14216 ** being called with pPrior==0.
       
 14217 */
       
 14218 static void memsys5Free(void *pPrior){
       
 14219   assert( pPrior!=0 );
       
 14220   memsys5Enter();
       
 14221   memsys5FreeUnsafe(pPrior);
       
 14222   memsys5Leave();  
       
 14223 }
       
 14224 
       
 14225 /*
       
 14226 ** Change the size of an existing memory allocation.
       
 14227 **
       
 14228 ** The outer layer memory allocator prevents this routine from
       
 14229 ** being called with pPrior==0.  
       
 14230 **
       
 14231 ** nBytes is always a value obtained from a prior call to
       
 14232 ** memsys5Round().  Hence nBytes is always a non-negative power
       
 14233 ** of two.  If nBytes==0 that means that an oversize allocation
       
 14234 ** (an allocation larger than 0x40000000) was requested and this
       
 14235 ** routine should return 0 without freeing pPrior.
       
 14236 */
       
 14237 static void *memsys5Realloc(void *pPrior, int nBytes){
       
 14238   int nOld;
       
 14239   void *p;
       
 14240   assert( pPrior!=0 );
       
 14241   assert( (nBytes&(nBytes-1))==0 );
       
 14242   assert( nBytes>=0 );
       
 14243   if( nBytes==0 ){
       
 14244     return 0;
       
 14245   }
       
 14246   nOld = memsys5Size(pPrior);
       
 14247   if( nBytes<=nOld ){
       
 14248     return pPrior;
       
 14249   }
       
 14250   memsys5Enter();
       
 14251   p = memsys5MallocUnsafe(nBytes);
       
 14252   if( p ){
       
 14253     memcpy(p, pPrior, nOld);
       
 14254     memsys5FreeUnsafe(pPrior);
       
 14255   }
       
 14256   memsys5Leave();
       
 14257   return p;
       
 14258 }
       
 14259 
       
 14260 /*
       
 14261 ** Round up a request size to the next valid allocation size.  If
       
 14262 ** the allocation is too large to be handled by this allocation system,
       
 14263 ** return 0.
       
 14264 **
       
 14265 ** All allocations must be a power of two and must be expressed by a
       
 14266 ** 32-bit signed integer.  Hence the largest allocation is 0x40000000
       
 14267 ** or 1073741824 bytes.
       
 14268 */
       
 14269 static int memsys5Roundup(int n){
       
 14270   int iFullSz;
       
 14271   if( n > 0x40000000 ) return 0;
       
 14272   for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
       
 14273   return iFullSz;
       
 14274 }
       
 14275 
       
 14276 /*
       
 14277 ** Return the ceiling of the logarithm base 2 of iValue.
       
 14278 **
       
 14279 ** Examples:   memsys5Log(1) -> 0
       
 14280 **             memsys5Log(2) -> 1
       
 14281 **             memsys5Log(4) -> 2
       
 14282 **             memsys5Log(5) -> 3
       
 14283 **             memsys5Log(8) -> 3
       
 14284 **             memsys5Log(9) -> 4
       
 14285 */
       
 14286 static int memsys5Log(int iValue){
       
 14287   int iLog;
       
 14288   for(iLog=0; (1<<iLog)<iValue; iLog++);
       
 14289   return iLog;
       
 14290 }
       
 14291 
       
 14292 /*
       
 14293 ** Initialize the memory allocator.
       
 14294 **
       
 14295 ** This routine is not threadsafe.  The caller must be holding a mutex
       
 14296 ** to prevent multiple threads from entering at the same time.
       
 14297 */
       
 14298 static int memsys5Init(void *NotUsed){
       
 14299   int ii;            /* Loop counter */
       
 14300   int nByte;         /* Number of bytes of memory available to this allocator */
       
 14301   u8 *zByte;         /* Memory usable by this allocator */
       
 14302   int nMinLog;       /* Log base 2 of minimum allocation size in bytes */
       
 14303   int iOffset;       /* An offset into mem5.aCtrl[] */
       
 14304 
       
 14305   UNUSED_PARAMETER(NotUsed);
       
 14306 
       
 14307   /* For the purposes of this routine, disable the mutex */
       
 14308   mem5.mutex = 0;
       
 14309 
       
 14310   /* The size of a Mem5Link object must be a power of two.  Verify that
       
 14311   ** this is case.
       
 14312   */
       
 14313   assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
       
 14314 
       
 14315   nByte = sqlite3GlobalConfig.nHeap;
       
 14316   zByte = (u8*)sqlite3GlobalConfig.pHeap;
       
 14317   assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
       
 14318 
       
 14319   nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
       
 14320   mem5.szAtom = (1<<nMinLog);
       
 14321   while( (int)sizeof(Mem5Link)>mem5.szAtom ){
       
 14322     mem5.szAtom = mem5.szAtom << 1;
       
 14323   }
       
 14324 
       
 14325   mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
       
 14326   mem5.zPool = zByte;
       
 14327   mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
       
 14328 
       
 14329   for(ii=0; ii<=LOGMAX; ii++){
       
 14330     mem5.aiFreelist[ii] = -1;
       
 14331   }
       
 14332 
       
 14333   iOffset = 0;
       
 14334   for(ii=LOGMAX; ii>=0; ii--){
       
 14335     int nAlloc = (1<<ii);
       
 14336     if( (iOffset+nAlloc)<=mem5.nBlock ){
       
 14337       mem5.aCtrl[iOffset] = ii | CTRL_FREE;
       
 14338       memsys5Link(iOffset, ii);
       
 14339       iOffset += nAlloc;
       
 14340     }
       
 14341     assert((iOffset+nAlloc)>mem5.nBlock);
       
 14342   }
       
 14343 
       
 14344   /* If a mutex is required for normal operation, allocate one */
       
 14345   if( sqlite3GlobalConfig.bMemstat==0 ){
       
 14346     mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
       
 14347   }
       
 14348 
       
 14349   return SQLITE_OK;
       
 14350 }
       
 14351 
       
 14352 /*
       
 14353 ** Deinitialize this module.
       
 14354 */
       
 14355 static void memsys5Shutdown(void *NotUsed){
       
 14356   UNUSED_PARAMETER(NotUsed);
       
 14357   mem5.mutex = 0;
       
 14358   return;
       
 14359 }
       
 14360 
       
 14361 #ifdef SQLITE_TEST
       
 14362 /*
       
 14363 ** Open the file indicated and write a log of all unfreed memory 
       
 14364 ** allocations into that log.
       
 14365 */
       
 14366 SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
       
 14367   FILE *out;
       
 14368   int i, j, n;
       
 14369   int nMinLog;
       
 14370 
       
 14371   if( zFilename==0 || zFilename[0]==0 ){
       
 14372     out = stdout;
       
 14373   }else{
       
 14374     out = fopen(zFilename, "w");
       
 14375     if( out==0 ){
       
 14376       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
       
 14377                       zFilename);
       
 14378       return;
       
 14379     }
       
 14380   }
       
 14381   memsys5Enter();
       
 14382   nMinLog = memsys5Log(mem5.szAtom);
       
 14383   for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
       
 14384     for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
       
 14385     fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
       
 14386   }
       
 14387   fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
       
 14388   fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
       
 14389   fprintf(out, "mem5.totalExcess  = %llu\n", mem5.totalExcess);
       
 14390   fprintf(out, "mem5.currentOut   = %u\n", mem5.currentOut);
       
 14391   fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
       
 14392   fprintf(out, "mem5.maxOut       = %u\n", mem5.maxOut);
       
 14393   fprintf(out, "mem5.maxCount     = %u\n", mem5.maxCount);
       
 14394   fprintf(out, "mem5.maxRequest   = %u\n", mem5.maxRequest);
       
 14395   memsys5Leave();
       
 14396   if( out==stdout ){
       
 14397     fflush(stdout);
       
 14398   }else{
       
 14399     fclose(out);
       
 14400   }
       
 14401 }
       
 14402 #endif
       
 14403 
       
 14404 /*
       
 14405 ** This routine is the only routine in this file with external 
       
 14406 ** linkage. It returns a pointer to a static sqlite3_mem_methods
       
 14407 ** struct populated with the memsys5 methods.
       
 14408 */
       
 14409 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
       
 14410   static const sqlite3_mem_methods memsys5Methods = {
       
 14411      memsys5Malloc,
       
 14412      memsys5Free,
       
 14413      memsys5Realloc,
       
 14414      memsys5Size,
       
 14415      memsys5Roundup,
       
 14416      memsys5Init,
       
 14417      memsys5Shutdown,
       
 14418      0
       
 14419   };
       
 14420   return &memsys5Methods;
       
 14421 }
       
 14422 
       
 14423 #endif /* SQLITE_ENABLE_MEMSYS5 */
       
 14424 
       
 14425 /************** End of mem5.c ************************************************/
       
 14426 /************** Begin file mutex.c *******************************************/
       
 14427 /*
       
 14428 ** 2007 August 14
       
 14429 **
       
 14430 ** The author disclaims copyright to this source code.  In place of
       
 14431 ** a legal notice, here is a blessing:
       
 14432 **
       
 14433 **    May you do good and not evil.
       
 14434 **    May you find forgiveness for yourself and forgive others.
       
 14435 **    May you share freely, never taking more than you give.
       
 14436 **
       
 14437 *************************************************************************
       
 14438 ** This file contains the C functions that implement mutexes.
       
 14439 **
       
 14440 ** This file contains code that is common across all mutex implementations.
       
 14441 
       
 14442 **
       
 14443 ** $Id: mutex.c,v 1.31 2009/07/16 18:21:18 drh Exp $
       
 14444 */
       
 14445 
       
 14446 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
       
 14447 /*
       
 14448 ** For debugging purposes, record when the mutex subsystem is initialized
       
 14449 ** and uninitialized so that we can assert() if there is an attempt to
       
 14450 ** allocate a mutex while the system is uninitialized.
       
 14451 */
       
 14452 static SQLITE_WSD int mutexIsInit = 0;
       
 14453 #endif /* SQLITE_DEBUG */
       
 14454 
       
 14455 
       
 14456 #ifndef SQLITE_MUTEX_OMIT
       
 14457 /*
       
 14458 ** Initialize the mutex system.
       
 14459 */
       
 14460 SQLITE_PRIVATE int sqlite3MutexInit(void){ 
       
 14461   int rc = SQLITE_OK;
       
 14462   if( sqlite3GlobalConfig.bCoreMutex ){
       
 14463     if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
       
 14464       /* If the xMutexAlloc method has not been set, then the user did not
       
 14465       ** install a mutex implementation via sqlite3_config() prior to 
       
 14466       ** sqlite3_initialize() being called. This block copies pointers to
       
 14467       ** the default implementation into the sqlite3GlobalConfig structure.
       
 14468       */
       
 14469       sqlite3_mutex_methods *pFrom = sqlite3DefaultMutex();
       
 14470       sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
       
 14471 
       
 14472       memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
       
 14473       memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
       
 14474              sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
       
 14475       pTo->xMutexAlloc = pFrom->xMutexAlloc;
       
 14476     }
       
 14477     rc = sqlite3GlobalConfig.mutex.xMutexInit();
       
 14478   }
       
 14479 
       
 14480 #ifdef SQLITE_DEBUG
       
 14481   GLOBAL(int, mutexIsInit) = 1;
       
 14482 #endif
       
 14483 
       
 14484   return rc;
       
 14485 }
       
 14486 
       
 14487 /*
       
 14488 ** Shutdown the mutex system. This call frees resources allocated by
       
 14489 ** sqlite3MutexInit().
       
 14490 */
       
 14491 SQLITE_PRIVATE int sqlite3MutexEnd(void){
       
 14492   int rc = SQLITE_OK;
       
 14493   if( sqlite3GlobalConfig.mutex.xMutexEnd ){
       
 14494     rc = sqlite3GlobalConfig.mutex.xMutexEnd();
       
 14495   }
       
 14496 
       
 14497 #ifdef SQLITE_DEBUG
       
 14498   GLOBAL(int, mutexIsInit) = 0;
       
 14499 #endif
       
 14500 
       
 14501   return rc;
       
 14502 }
       
 14503 
       
 14504 /*
       
 14505 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
       
 14506 */
       
 14507 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
       
 14508 #ifndef SQLITE_OMIT_AUTOINIT
       
 14509   if( sqlite3_initialize() ) return 0;
       
 14510 #endif
       
 14511   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
       
 14512 }
       
 14513 
       
 14514 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
       
 14515   if( !sqlite3GlobalConfig.bCoreMutex ){
       
 14516     return 0;
       
 14517   }
       
 14518   assert( GLOBAL(int, mutexIsInit) );
       
 14519   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
       
 14520 }
       
 14521 
       
 14522 /*
       
 14523 ** Free a dynamic mutex.
       
 14524 */
       
 14525 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
       
 14526   if( p ){
       
 14527     sqlite3GlobalConfig.mutex.xMutexFree(p);
       
 14528   }
       
 14529 }
       
 14530 
       
 14531 /*
       
 14532 ** Obtain the mutex p. If some other thread already has the mutex, block
       
 14533 ** until it can be obtained.
       
 14534 */
       
 14535 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
       
 14536   if( p ){
       
 14537     sqlite3GlobalConfig.mutex.xMutexEnter(p);
       
 14538   }
       
 14539 }
       
 14540 
       
 14541 /*
       
 14542 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
       
 14543 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
       
 14544 */
       
 14545 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
       
 14546   int rc = SQLITE_OK;
       
 14547   if( p ){
       
 14548     return sqlite3GlobalConfig.mutex.xMutexTry(p);
       
 14549   }
       
 14550   return rc;
       
 14551 }
       
 14552 
       
 14553 /*
       
 14554 ** The sqlite3_mutex_leave() routine exits a mutex that was previously
       
 14555 ** entered by the same thread.  The behavior is undefined if the mutex 
       
 14556 ** is not currently entered. If a NULL pointer is passed as an argument
       
 14557 ** this function is a no-op.
       
 14558 */
       
 14559 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
       
 14560   if( p ){
       
 14561     sqlite3GlobalConfig.mutex.xMutexLeave(p);
       
 14562   }
       
 14563 }
       
 14564 
       
 14565 #ifndef NDEBUG
       
 14566 /*
       
 14567 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
       
 14568 ** intended for use inside assert() statements.
       
 14569 */
       
 14570 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
       
 14571   return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
       
 14572 }
       
 14573 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
       
 14574   return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
       
 14575 }
       
 14576 #endif
       
 14577 
       
 14578 #endif /* SQLITE_MUTEX_OMIT */
       
 14579 
       
 14580 /************** End of mutex.c ***********************************************/
       
 14581 /************** Begin file mutex_noop.c **************************************/
       
 14582 /*
       
 14583 ** 2008 October 07
       
 14584 **
       
 14585 ** The author disclaims copyright to this source code.  In place of
       
 14586 ** a legal notice, here is a blessing:
       
 14587 **
       
 14588 **    May you do good and not evil.
       
 14589 **    May you find forgiveness for yourself and forgive others.
       
 14590 **    May you share freely, never taking more than you give.
       
 14591 **
       
 14592 *************************************************************************
       
 14593 ** This file contains the C functions that implement mutexes.
       
 14594 **
       
 14595 ** This implementation in this file does not provide any mutual
       
 14596 ** exclusion and is thus suitable for use only in applications
       
 14597 ** that use SQLite in a single thread.  The routines defined
       
 14598 ** here are place-holders.  Applications can substitute working
       
 14599 ** mutex routines at start-time using the
       
 14600 **
       
 14601 **     sqlite3_config(SQLITE_CONFIG_MUTEX,...)
       
 14602 **
       
 14603 ** interface.
       
 14604 **
       
 14605 ** If compiled with SQLITE_DEBUG, then additional logic is inserted
       
 14606 ** that does error checking on mutexes to make sure they are being
       
 14607 ** called correctly.
       
 14608 **
       
 14609 ** $Id: mutex_noop.c,v 1.3 2008/12/05 17:17:08 drh Exp $
       
 14610 */
       
 14611 
       
 14612 
       
 14613 #if defined(SQLITE_MUTEX_NOOP) && !defined(SQLITE_DEBUG)
       
 14614 /*
       
 14615 ** Stub routines for all mutex methods.
       
 14616 **
       
 14617 ** This routines provide no mutual exclusion or error checking.
       
 14618 */
       
 14619 static int noopMutexHeld(sqlite3_mutex *p){ return 1; }
       
 14620 static int noopMutexNotheld(sqlite3_mutex *p){ return 1; }
       
 14621 static int noopMutexInit(void){ return SQLITE_OK; }
       
 14622 static int noopMutexEnd(void){ return SQLITE_OK; }
       
 14623 static sqlite3_mutex *noopMutexAlloc(int id){ return (sqlite3_mutex*)8; }
       
 14624 static void noopMutexFree(sqlite3_mutex *p){ return; }
       
 14625 static void noopMutexEnter(sqlite3_mutex *p){ return; }
       
 14626 static int noopMutexTry(sqlite3_mutex *p){ return SQLITE_OK; }
       
 14627 static void noopMutexLeave(sqlite3_mutex *p){ return; }
       
 14628 
       
 14629 SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
       
 14630   static sqlite3_mutex_methods sMutex = {
       
 14631     noopMutexInit,
       
 14632     noopMutexEnd,
       
 14633     noopMutexAlloc,
       
 14634     noopMutexFree,
       
 14635     noopMutexEnter,
       
 14636     noopMutexTry,
       
 14637     noopMutexLeave,
       
 14638 
       
 14639     noopMutexHeld,
       
 14640     noopMutexNotheld
       
 14641   };
       
 14642 
       
 14643   return &sMutex;
       
 14644 }
       
 14645 #endif /* defined(SQLITE_MUTEX_NOOP) && !defined(SQLITE_DEBUG) */
       
 14646 
       
 14647 #if defined(SQLITE_MUTEX_NOOP) && defined(SQLITE_DEBUG)
       
 14648 /*
       
 14649 ** In this implementation, error checking is provided for testing
       
 14650 ** and debugging purposes.  The mutexes still do not provide any
       
 14651 ** mutual exclusion.
       
 14652 */
       
 14653 
       
 14654 /*
       
 14655 ** The mutex object
       
 14656 */
       
 14657 struct sqlite3_mutex {
       
 14658   int id;     /* The mutex type */
       
 14659   int cnt;    /* Number of entries without a matching leave */
       
 14660 };
       
 14661 
       
 14662 /*
       
 14663 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
       
 14664 ** intended for use inside assert() statements.
       
 14665 */
       
 14666 static int debugMutexHeld(sqlite3_mutex *p){
       
 14667   return p==0 || p->cnt>0;
       
 14668 }
       
 14669 static int debugMutexNotheld(sqlite3_mutex *p){
       
 14670   return p==0 || p->cnt==0;
       
 14671 }
       
 14672 
       
 14673 /*
       
 14674 ** Initialize and deinitialize the mutex subsystem.
       
 14675 */
       
 14676 static int debugMutexInit(void){ return SQLITE_OK; }
       
 14677 static int debugMutexEnd(void){ return SQLITE_OK; }
       
 14678 
       
 14679 /*
       
 14680 ** The sqlite3_mutex_alloc() routine allocates a new
       
 14681 ** mutex and returns a pointer to it.  If it returns NULL
       
 14682 ** that means that a mutex could not be allocated. 
       
 14683 */
       
 14684 static sqlite3_mutex *debugMutexAlloc(int id){
       
 14685   static sqlite3_mutex aStatic[6];
       
 14686   sqlite3_mutex *pNew = 0;
       
 14687   switch( id ){
       
 14688     case SQLITE_MUTEX_FAST:
       
 14689     case SQLITE_MUTEX_RECURSIVE: {
       
 14690       pNew = sqlite3Malloc(sizeof(*pNew));
       
 14691       if( pNew ){
       
 14692         pNew->id = id;
       
 14693         pNew->cnt = 0;
       
 14694       }
       
 14695       break;
       
 14696     }
       
 14697     default: {
       
 14698       assert( id-2 >= 0 );
       
 14699       assert( id-2 < (int)(sizeof(aStatic)/sizeof(aStatic[0])) );
       
 14700       pNew = &aStatic[id-2];
       
 14701       pNew->id = id;
       
 14702       break;
       
 14703     }
       
 14704   }
       
 14705   return pNew;
       
 14706 }
       
 14707 
       
 14708 /*
       
 14709 ** This routine deallocates a previously allocated mutex.
       
 14710 */
       
 14711 static void debugMutexFree(sqlite3_mutex *p){
       
 14712   assert( p->cnt==0 );
       
 14713   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
       
 14714   sqlite3_free(p);
       
 14715 }
       
 14716 
       
 14717 /*
       
 14718 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
       
 14719 ** to enter a mutex.  If another thread is already within the mutex,
       
 14720 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
       
 14721 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
       
 14722 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
       
 14723 ** be entered multiple times by the same thread.  In such cases the,
       
 14724 ** mutex must be exited an equal number of times before another thread
       
 14725 ** can enter.  If the same thread tries to enter any other kind of mutex
       
 14726 ** more than once, the behavior is undefined.
       
 14727 */
       
 14728 static void debugMutexEnter(sqlite3_mutex *p){
       
 14729   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(p) );
       
 14730   p->cnt++;
       
 14731 }
       
 14732 static int debugMutexTry(sqlite3_mutex *p){
       
 14733   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(p) );
       
 14734   p->cnt++;
       
 14735   return SQLITE_OK;
       
 14736 }
       
 14737 
       
 14738 /*
       
 14739 ** The sqlite3_mutex_leave() routine exits a mutex that was
       
 14740 ** previously entered by the same thread.  The behavior
       
 14741 ** is undefined if the mutex is not currently entered or
       
 14742 ** is not currently allocated.  SQLite will never do either.
       
 14743 */
       
 14744 static void debugMutexLeave(sqlite3_mutex *p){
       
 14745   assert( debugMutexHeld(p) );
       
 14746   p->cnt--;
       
 14747   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(p) );
       
 14748 }
       
 14749 
       
 14750 SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
       
 14751   static sqlite3_mutex_methods sMutex = {
       
 14752     debugMutexInit,
       
 14753     debugMutexEnd,
       
 14754     debugMutexAlloc,
       
 14755     debugMutexFree,
       
 14756     debugMutexEnter,
       
 14757     debugMutexTry,
       
 14758     debugMutexLeave,
       
 14759 
       
 14760     debugMutexHeld,
       
 14761     debugMutexNotheld
       
 14762   };
       
 14763 
       
 14764   return &sMutex;
       
 14765 }
       
 14766 #endif /* defined(SQLITE_MUTEX_NOOP) && defined(SQLITE_DEBUG) */
       
 14767 
       
 14768 /************** End of mutex_noop.c ******************************************/
       
 14769 /************** Begin file mutex_os2.c ***************************************/
       
 14770 /*
       
 14771 ** 2007 August 28
       
 14772 **
       
 14773 ** The author disclaims copyright to this source code.  In place of
       
 14774 ** a legal notice, here is a blessing:
       
 14775 **
       
 14776 **    May you do good and not evil.
       
 14777 **    May you find forgiveness for yourself and forgive others.
       
 14778 **    May you share freely, never taking more than you give.
       
 14779 **
       
 14780 *************************************************************************
       
 14781 ** This file contains the C functions that implement mutexes for OS/2
       
 14782 **
       
 14783 ** $Id: mutex_os2.c,v 1.11 2008/11/22 19:50:54 pweilbacher Exp $
       
 14784 */
       
 14785 
       
 14786 /*
       
 14787 ** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
       
 14788 ** See the mutex.h file for details.
       
 14789 */
       
 14790 #ifdef SQLITE_MUTEX_OS2
       
 14791 
       
 14792 /********************** OS/2 Mutex Implementation **********************
       
 14793 **
       
 14794 ** This implementation of mutexes is built using the OS/2 API.
       
 14795 */
       
 14796 
       
 14797 /*
       
 14798 ** The mutex object
       
 14799 ** Each recursive mutex is an instance of the following structure.
       
 14800 */
       
 14801 struct sqlite3_mutex {
       
 14802   HMTX mutex;       /* Mutex controlling the lock */
       
 14803   int  id;          /* Mutex type */
       
 14804   int  nRef;        /* Number of references */
       
 14805   TID  owner;       /* Thread holding this mutex */
       
 14806 };
       
 14807 
       
 14808 #define OS2_MUTEX_INITIALIZER   0,0,0,0
       
 14809 
       
 14810 /*
       
 14811 ** Initialize and deinitialize the mutex subsystem.
       
 14812 */
       
 14813 static int os2MutexInit(void){ return SQLITE_OK; }
       
 14814 static int os2MutexEnd(void){ return SQLITE_OK; }
       
 14815 
       
 14816 /*
       
 14817 ** The sqlite3_mutex_alloc() routine allocates a new
       
 14818 ** mutex and returns a pointer to it.  If it returns NULL
       
 14819 ** that means that a mutex could not be allocated. 
       
 14820 ** SQLite will unwind its stack and return an error.  The argument
       
 14821 ** to sqlite3_mutex_alloc() is one of these integer constants:
       
 14822 **
       
 14823 ** <ul>
       
 14824 ** <li>  SQLITE_MUTEX_FAST               0
       
 14825 ** <li>  SQLITE_MUTEX_RECURSIVE          1
       
 14826 ** <li>  SQLITE_MUTEX_STATIC_MASTER      2
       
 14827 ** <li>  SQLITE_MUTEX_STATIC_MEM         3
       
 14828 ** <li>  SQLITE_MUTEX_STATIC_PRNG        4
       
 14829 ** </ul>
       
 14830 **
       
 14831 ** The first two constants cause sqlite3_mutex_alloc() to create
       
 14832 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
       
 14833 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
       
 14834 ** The mutex implementation does not need to make a distinction
       
 14835 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
       
 14836 ** not want to.  But SQLite will only request a recursive mutex in
       
 14837 ** cases where it really needs one.  If a faster non-recursive mutex
       
 14838 ** implementation is available on the host platform, the mutex subsystem
       
 14839 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
       
 14840 **
       
 14841 ** The other allowed parameters to sqlite3_mutex_alloc() each return
       
 14842 ** a pointer to a static preexisting mutex.  Three static mutexes are
       
 14843 ** used by the current version of SQLite.  Future versions of SQLite
       
 14844 ** may add additional static mutexes.  Static mutexes are for internal
       
 14845 ** use by SQLite only.  Applications that use SQLite mutexes should
       
 14846 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
       
 14847 ** SQLITE_MUTEX_RECURSIVE.
       
 14848 **
       
 14849 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
       
 14850 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
       
 14851 ** returns a different mutex on every call.  But for the static
       
 14852 ** mutex types, the same mutex is returned on every call that has
       
 14853 ** the same type number.
       
 14854 */
       
 14855 static sqlite3_mutex *os2MutexAlloc(int iType){
       
 14856   sqlite3_mutex *p = NULL;
       
 14857   switch( iType ){
       
 14858     case SQLITE_MUTEX_FAST:
       
 14859     case SQLITE_MUTEX_RECURSIVE: {
       
 14860       p = sqlite3MallocZero( sizeof(*p) );
       
 14861       if( p ){
       
 14862         p->id = iType;
       
 14863         if( DosCreateMutexSem( 0, &p->mutex, 0, FALSE ) != NO_ERROR ){
       
 14864           sqlite3_free( p );
       
 14865           p = NULL;
       
 14866         }
       
 14867       }
       
 14868       break;
       
 14869     }
       
 14870     default: {
       
 14871       static volatile int isInit = 0;
       
 14872       static sqlite3_mutex staticMutexes[] = {
       
 14873         { OS2_MUTEX_INITIALIZER, },
       
 14874         { OS2_MUTEX_INITIALIZER, },
       
 14875         { OS2_MUTEX_INITIALIZER, },
       
 14876         { OS2_MUTEX_INITIALIZER, },
       
 14877         { OS2_MUTEX_INITIALIZER, },
       
 14878         { OS2_MUTEX_INITIALIZER, },
       
 14879       };
       
 14880       if ( !isInit ){
       
 14881         APIRET rc;
       
 14882         PTIB ptib;
       
 14883         PPIB ppib;
       
 14884         HMTX mutex;
       
 14885         char name[32];
       
 14886         DosGetInfoBlocks( &ptib, &ppib );
       
 14887         sqlite3_snprintf( sizeof(name), name, "\\SEM32\\SQLITE%04x",
       
 14888                           ppib->pib_ulpid );
       
 14889         while( !isInit ){
       
 14890           mutex = 0;
       
 14891           rc = DosCreateMutexSem( name, &mutex, 0, FALSE);
       
 14892           if( rc == NO_ERROR ){
       
 14893             unsigned int i;
       
 14894             if( !isInit ){
       
 14895               for( i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++ ){
       
 14896                 DosCreateMutexSem( 0, &staticMutexes[i].mutex, 0, FALSE );
       
 14897               }
       
 14898               isInit = 1;
       
 14899             }
       
 14900             DosCloseMutexSem( mutex );
       
 14901           }else if( rc == ERROR_DUPLICATE_NAME ){
       
 14902             DosSleep( 1 );
       
 14903           }else{
       
 14904             return p;
       
 14905           }
       
 14906         }
       
 14907       }
       
 14908       assert( iType-2 >= 0 );
       
 14909       assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
       
 14910       p = &staticMutexes[iType-2];
       
 14911       p->id = iType;
       
 14912       break;
       
 14913     }
       
 14914   }
       
 14915   return p;
       
 14916 }
       
 14917 
       
 14918 
       
 14919 /*
       
 14920 ** This routine deallocates a previously allocated mutex.
       
 14921 ** SQLite is careful to deallocate every mutex that it allocates.
       
 14922 */
       
 14923 static void os2MutexFree(sqlite3_mutex *p){
       
 14924   if( p==0 ) return;
       
 14925   assert( p->nRef==0 );
       
 14926   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
       
 14927   DosCloseMutexSem( p->mutex );
       
 14928   sqlite3_free( p );
       
 14929 }
       
 14930 
       
 14931 /*
       
 14932 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
       
 14933 ** to enter a mutex.  If another thread is already within the mutex,
       
 14934 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
       
 14935 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
       
 14936 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
       
 14937 ** be entered multiple times by the same thread.  In such cases the,
       
 14938 ** mutex must be exited an equal number of times before another thread
       
 14939 ** can enter.  If the same thread tries to enter any other kind of mutex
       
 14940 ** more than once, the behavior is undefined.
       
 14941 */
       
 14942 static void os2MutexEnter(sqlite3_mutex *p){
       
 14943   TID tid;
       
 14944   PID holder1;
       
 14945   ULONG holder2;
       
 14946   if( p==0 ) return;
       
 14947   assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
       
 14948   DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
       
 14949   DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2);
       
 14950   p->owner = tid;
       
 14951   p->nRef++;
       
 14952 }
       
 14953 static int os2MutexTry(sqlite3_mutex *p){
       
 14954   int rc;
       
 14955   TID tid;
       
 14956   PID holder1;
       
 14957   ULONG holder2;
       
 14958   if( p==0 ) return SQLITE_OK;
       
 14959   assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
       
 14960   if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR) {
       
 14961     DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2);
       
 14962     p->owner = tid;
       
 14963     p->nRef++;
       
 14964     rc = SQLITE_OK;
       
 14965   } else {
       
 14966     rc = SQLITE_BUSY;
       
 14967   }
       
 14968 
       
 14969   return rc;
       
 14970 }
       
 14971 
       
 14972 /*
       
 14973 ** The sqlite3_mutex_leave() routine exits a mutex that was
       
 14974 ** previously entered by the same thread.  The behavior
       
 14975 ** is undefined if the mutex is not currently entered or
       
 14976 ** is not currently allocated.  SQLite will never do either.
       
 14977 */
       
 14978 static void os2MutexLeave(sqlite3_mutex *p){
       
 14979   TID tid;
       
 14980   PID holder1;
       
 14981   ULONG holder2;
       
 14982   if( p==0 ) return;
       
 14983   assert( p->nRef>0 );
       
 14984   DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2);
       
 14985   assert( p->owner==tid );
       
 14986   p->nRef--;
       
 14987   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
       
 14988   DosReleaseMutexSem(p->mutex);
       
 14989 }
       
 14990 
       
 14991 #ifdef SQLITE_DEBUG
       
 14992 /*
       
 14993 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
       
 14994 ** intended for use inside assert() statements.
       
 14995 */
       
 14996 static int os2MutexHeld(sqlite3_mutex *p){
       
 14997   TID tid;
       
 14998   PID pid;
       
 14999   ULONG ulCount;
       
 15000   PTIB ptib;
       
 15001   if( p!=0 ) {
       
 15002     DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
       
 15003   } else {
       
 15004     DosGetInfoBlocks(&ptib, NULL);
       
 15005     tid = ptib->tib_ptib2->tib2_ultid;
       
 15006   }
       
 15007   return p==0 || (p->nRef!=0 && p->owner==tid);
       
 15008 }
       
 15009 static int os2MutexNotheld(sqlite3_mutex *p){
       
 15010   TID tid;
       
 15011   PID pid;
       
 15012   ULONG ulCount;
       
 15013   PTIB ptib;
       
 15014   if( p!= 0 ) {
       
 15015     DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
       
 15016   } else {
       
 15017     DosGetInfoBlocks(&ptib, NULL);
       
 15018     tid = ptib->tib_ptib2->tib2_ultid;
       
 15019   }
       
 15020   return p==0 || p->nRef==0 || p->owner!=tid;
       
 15021 }
       
 15022 #endif
       
 15023 
       
 15024 SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
       
 15025   static sqlite3_mutex_methods sMutex = {
       
 15026     os2MutexInit,
       
 15027     os2MutexEnd,
       
 15028     os2MutexAlloc,
       
 15029     os2MutexFree,
       
 15030     os2MutexEnter,
       
 15031     os2MutexTry,
       
 15032     os2MutexLeave,
       
 15033 #ifdef SQLITE_DEBUG
       
 15034     os2MutexHeld,
       
 15035     os2MutexNotheld
       
 15036 #endif
       
 15037   };
       
 15038 
       
 15039   return &sMutex;
       
 15040 }
       
 15041 #endif /* SQLITE_MUTEX_OS2 */
       
 15042 
       
 15043 /************** End of mutex_os2.c *******************************************/
       
 15044 /************** Begin file mutex_unix.c **************************************/
       
 15045 /*
       
 15046 ** 2007 August 28
       
 15047 **
       
 15048 ** The author disclaims copyright to this source code.  In place of
       
 15049 ** a legal notice, here is a blessing:
       
 15050 **
       
 15051 **    May you do good and not evil.
       
 15052 **    May you find forgiveness for yourself and forgive others.
       
 15053 **    May you share freely, never taking more than you give.
       
 15054 **
       
 15055 *************************************************************************
       
 15056 ** This file contains the C functions that implement mutexes for pthreads
       
 15057 **
       
 15058 ** $Id: mutex_unix.c,v 1.16 2008/12/08 18:19:18 drh Exp $
       
 15059 */
       
 15060 
       
 15061 /*
       
 15062 ** The code in this file is only used if we are compiling threadsafe
       
 15063 ** under unix with pthreads.
       
 15064 **
       
 15065 ** Note that this implementation requires a version of pthreads that
       
 15066 ** supports recursive mutexes.
       
 15067 */
       
 15068 #ifdef SQLITE_MUTEX_PTHREADS
       
 15069 
       
 15070 #include <pthread.h>
       
 15071 
       
 15072 
       
 15073 /*
       
 15074 ** Each recursive mutex is an instance of the following structure.
       
 15075 */
       
 15076 struct sqlite3_mutex {
       
 15077   pthread_mutex_t mutex;     /* Mutex controlling the lock */
       
 15078   int id;                    /* Mutex type */
       
 15079   int nRef;                  /* Number of entrances */
       
 15080   pthread_t owner;           /* Thread that is within this mutex */
       
 15081 #ifdef SQLITE_DEBUG
       
 15082   int trace;                 /* True to trace changes */
       
 15083 #endif
       
 15084 };
       
 15085 #ifdef SQLITE_DEBUG
       
 15086 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
       
 15087 #else
       
 15088 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0 }
       
 15089 #endif
       
 15090 
       
 15091 /*
       
 15092 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
       
 15093 ** intended for use only inside assert() statements.  On some platforms,
       
 15094 ** there might be race conditions that can cause these routines to
       
 15095 ** deliver incorrect results.  In particular, if pthread_equal() is
       
 15096 ** not an atomic operation, then these routines might delivery
       
 15097 ** incorrect results.  On most platforms, pthread_equal() is a 
       
 15098 ** comparison of two integers and is therefore atomic.  But we are
       
 15099 ** told that HPUX is not such a platform.  If so, then these routines
       
 15100 ** will not always work correctly on HPUX.
       
 15101 **
       
 15102 ** On those platforms where pthread_equal() is not atomic, SQLite
       
 15103 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
       
 15104 ** make sure no assert() statements are evaluated and hence these
       
 15105 ** routines are never called.
       
 15106 */
       
 15107 #if !defined(NDEBUG) || defined(SQLITE_DEBUG)
       
 15108 static int pthreadMutexHeld(sqlite3_mutex *p){
       
 15109   return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
       
 15110 }
       
 15111 static int pthreadMutexNotheld(sqlite3_mutex *p){
       
 15112   return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
       
 15113 }
       
 15114 #endif
       
 15115 
       
 15116 /*
       
 15117 ** Initialize and deinitialize the mutex subsystem.
       
 15118 */
       
 15119 static int pthreadMutexInit(void){ return SQLITE_OK; }
       
 15120 static int pthreadMutexEnd(void){ return SQLITE_OK; }
       
 15121 
       
 15122 /*
       
 15123 ** The sqlite3_mutex_alloc() routine allocates a new
       
 15124 ** mutex and returns a pointer to it.  If it returns NULL
       
 15125 ** that means that a mutex could not be allocated.  SQLite
       
 15126 ** will unwind its stack and return an error.  The argument
       
 15127 ** to sqlite3_mutex_alloc() is one of these integer constants:
       
 15128 **
       
 15129 ** <ul>
       
 15130 ** <li>  SQLITE_MUTEX_FAST
       
 15131 ** <li>  SQLITE_MUTEX_RECURSIVE
       
 15132 ** <li>  SQLITE_MUTEX_STATIC_MASTER
       
 15133 ** <li>  SQLITE_MUTEX_STATIC_MEM
       
 15134 ** <li>  SQLITE_MUTEX_STATIC_MEM2
       
 15135 ** <li>  SQLITE_MUTEX_STATIC_PRNG
       
 15136 ** <li>  SQLITE_MUTEX_STATIC_LRU
       
 15137 ** <li>  SQLITE_MUTEX_STATIC_LRU2
       
 15138 ** </ul>
       
 15139 **
       
 15140 ** The first two constants cause sqlite3_mutex_alloc() to create
       
 15141 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
       
 15142 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
       
 15143 ** The mutex implementation does not need to make a distinction
       
 15144 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
       
 15145 ** not want to.  But SQLite will only request a recursive mutex in
       
 15146 ** cases where it really needs one.  If a faster non-recursive mutex
       
 15147 ** implementation is available on the host platform, the mutex subsystem
       
 15148 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
       
 15149 **
       
 15150 ** The other allowed parameters to sqlite3_mutex_alloc() each return
       
 15151 ** a pointer to a static preexisting mutex.  Six static mutexes are
       
 15152 ** used by the current version of SQLite.  Future versions of SQLite
       
 15153 ** may add additional static mutexes.  Static mutexes are for internal
       
 15154 ** use by SQLite only.  Applications that use SQLite mutexes should
       
 15155 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
       
 15156 ** SQLITE_MUTEX_RECURSIVE.
       
 15157 **
       
 15158 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
       
 15159 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
       
 15160 ** returns a different mutex on every call.  But for the static 
       
 15161 ** mutex types, the same mutex is returned on every call that has
       
 15162 ** the same type number.
       
 15163 */
       
 15164 static sqlite3_mutex *pthreadMutexAlloc(int iType){
       
 15165   static sqlite3_mutex staticMutexes[] = {
       
 15166     SQLITE3_MUTEX_INITIALIZER,
       
 15167     SQLITE3_MUTEX_INITIALIZER,
       
 15168     SQLITE3_MUTEX_INITIALIZER,
       
 15169     SQLITE3_MUTEX_INITIALIZER,
       
 15170     SQLITE3_MUTEX_INITIALIZER,
       
 15171     SQLITE3_MUTEX_INITIALIZER
       
 15172   };
       
 15173   sqlite3_mutex *p;
       
 15174   switch( iType ){
       
 15175     case SQLITE_MUTEX_RECURSIVE: {
       
 15176       p = sqlite3MallocZero( sizeof(*p) );
       
 15177       if( p ){
       
 15178 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
       
 15179         /* If recursive mutexes are not available, we will have to
       
 15180         ** build our own.  See below. */
       
 15181         pthread_mutex_init(&p->mutex, 0);
       
 15182 #else
       
 15183         /* Use a recursive mutex if it is available */
       
 15184         pthread_mutexattr_t recursiveAttr;
       
 15185         pthread_mutexattr_init(&recursiveAttr);
       
 15186         pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
       
 15187         pthread_mutex_init(&p->mutex, &recursiveAttr);
       
 15188         pthread_mutexattr_destroy(&recursiveAttr);
       
 15189 #endif
       
 15190         p->id = iType;
       
 15191       }
       
 15192       break;
       
 15193     }
       
 15194     case SQLITE_MUTEX_FAST: {
       
 15195       p = sqlite3MallocZero( sizeof(*p) );
       
 15196       if( p ){
       
 15197         p->id = iType;
       
 15198         pthread_mutex_init(&p->mutex, 0);
       
 15199       }
       
 15200       break;
       
 15201     }
       
 15202     default: {
       
 15203       assert( iType-2 >= 0 );
       
 15204       assert( iType-2 < ArraySize(staticMutexes) );
       
 15205       p = &staticMutexes[iType-2];
       
 15206       p->id = iType;
       
 15207       break;
       
 15208     }
       
 15209   }
       
 15210   return p;
       
 15211 }
       
 15212 
       
 15213 
       
 15214 /*
       
 15215 ** This routine deallocates a previously
       
 15216 ** allocated mutex.  SQLite is careful to deallocate every
       
 15217 ** mutex that it allocates.
       
 15218 */
       
 15219 static void pthreadMutexFree(sqlite3_mutex *p){
       
 15220   assert( p->nRef==0 );
       
 15221   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
       
 15222   pthread_mutex_destroy(&p->mutex);
       
 15223   sqlite3_free(p);
       
 15224 }
       
 15225 
       
 15226 /*
       
 15227 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
       
 15228 ** to enter a mutex.  If another thread is already within the mutex,
       
 15229 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
       
 15230 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
       
 15231 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
       
 15232 ** be entered multiple times by the same thread.  In such cases the,
       
 15233 ** mutex must be exited an equal number of times before another thread
       
 15234 ** can enter.  If the same thread tries to enter any other kind of mutex
       
 15235 ** more than once, the behavior is undefined.
       
 15236 */
       
 15237 static void pthreadMutexEnter(sqlite3_mutex *p){
       
 15238   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
       
 15239 
       
 15240 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
       
 15241   /* If recursive mutexes are not available, then we have to grow
       
 15242   ** our own.  This implementation assumes that pthread_equal()
       
 15243   ** is atomic - that it cannot be deceived into thinking self
       
 15244   ** and p->owner are equal if p->owner changes between two values
       
 15245   ** that are not equal to self while the comparison is taking place.
       
 15246   ** This implementation also assumes a coherent cache - that 
       
 15247   ** separate processes cannot read different values from the same
       
 15248   ** address at the same time.  If either of these two conditions
       
 15249   ** are not met, then the mutexes will fail and problems will result.
       
 15250   */
       
 15251   {
       
 15252     pthread_t self = pthread_self();
       
 15253     if( p->nRef>0 && pthread_equal(p->owner, self) ){
       
 15254       p->nRef++;
       
 15255     }else{
       
 15256       pthread_mutex_lock(&p->mutex);
       
 15257       assert( p->nRef==0 );
       
 15258       p->owner = self;
       
 15259       p->nRef = 1;
       
 15260     }
       
 15261   }
       
 15262 #else
       
 15263   /* Use the built-in recursive mutexes if they are available.
       
 15264   */
       
 15265   pthread_mutex_lock(&p->mutex);
       
 15266   p->owner = pthread_self();
       
 15267   p->nRef++;
       
 15268 #endif
       
 15269 
       
 15270 #ifdef SQLITE_DEBUG
       
 15271   if( p->trace ){
       
 15272     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
       
 15273   }
       
 15274 #endif
       
 15275 }
       
 15276 static int pthreadMutexTry(sqlite3_mutex *p){
       
 15277   int rc;
       
 15278   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
       
 15279 
       
 15280 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
       
 15281   /* If recursive mutexes are not available, then we have to grow
       
 15282   ** our own.  This implementation assumes that pthread_equal()
       
 15283   ** is atomic - that it cannot be deceived into thinking self
       
 15284   ** and p->owner are equal if p->owner changes between two values
       
 15285   ** that are not equal to self while the comparison is taking place.
       
 15286   ** This implementation also assumes a coherent cache - that 
       
 15287   ** separate processes cannot read different values from the same
       
 15288   ** address at the same time.  If either of these two conditions
       
 15289   ** are not met, then the mutexes will fail and problems will result.
       
 15290   */
       
 15291   {
       
 15292     pthread_t self = pthread_self();
       
 15293     if( p->nRef>0 && pthread_equal(p->owner, self) ){
       
 15294       p->nRef++;
       
 15295       rc = SQLITE_OK;
       
 15296     }else if( pthread_mutex_trylock(&p->mutex)==0 ){
       
 15297       assert( p->nRef==0 );
       
 15298       p->owner = self;
       
 15299       p->nRef = 1;
       
 15300       rc = SQLITE_OK;
       
 15301     }else{
       
 15302       rc = SQLITE_BUSY;
       
 15303     }
       
 15304   }
       
 15305 #else
       
 15306   /* Use the built-in recursive mutexes if they are available.
       
 15307   */
       
 15308   if( pthread_mutex_trylock(&p->mutex)==0 ){
       
 15309     p->owner = pthread_self();
       
 15310     p->nRef++;
       
 15311     rc = SQLITE_OK;
       
 15312   }else{
       
 15313     rc = SQLITE_BUSY;
       
 15314   }
       
 15315 #endif
       
 15316 
       
 15317 #ifdef SQLITE_DEBUG
       
 15318   if( rc==SQLITE_OK && p->trace ){
       
 15319     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
       
 15320   }
       
 15321 #endif
       
 15322   return rc;
       
 15323 }
       
 15324 
       
 15325 /*
       
 15326 ** The sqlite3_mutex_leave() routine exits a mutex that was
       
 15327 ** previously entered by the same thread.  The behavior
       
 15328 ** is undefined if the mutex is not currently entered or
       
 15329 ** is not currently allocated.  SQLite will never do either.
       
 15330 */
       
 15331 static void pthreadMutexLeave(sqlite3_mutex *p){
       
 15332   assert( pthreadMutexHeld(p) );
       
 15333   p->nRef--;
       
 15334   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
       
 15335 
       
 15336 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
       
 15337   if( p->nRef==0 ){
       
 15338     pthread_mutex_unlock(&p->mutex);
       
 15339   }
       
 15340 #else
       
 15341   pthread_mutex_unlock(&p->mutex);
       
 15342 #endif
       
 15343 
       
 15344 #ifdef SQLITE_DEBUG
       
 15345   if( p->trace ){
       
 15346     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
       
 15347   }
       
 15348 #endif
       
 15349 }
       
 15350 
       
 15351 SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
       
 15352   static sqlite3_mutex_methods sMutex = {
       
 15353     pthreadMutexInit,
       
 15354     pthreadMutexEnd,
       
 15355     pthreadMutexAlloc,
       
 15356     pthreadMutexFree,
       
 15357     pthreadMutexEnter,
       
 15358     pthreadMutexTry,
       
 15359     pthreadMutexLeave,
       
 15360 #ifdef SQLITE_DEBUG
       
 15361     pthreadMutexHeld,
       
 15362     pthreadMutexNotheld
       
 15363 #else
       
 15364     0,
       
 15365     0
       
 15366 #endif
       
 15367   };
       
 15368 
       
 15369   return &sMutex;
       
 15370 }
       
 15371 
       
 15372 #endif /* SQLITE_MUTEX_PTHREAD */
       
 15373 
       
 15374 /************** End of mutex_unix.c ******************************************/
       
 15375 /************** Begin file mutex_w32.c ***************************************/
       
 15376 /*
       
 15377 ** 2007 August 14
       
 15378 **
       
 15379 ** The author disclaims copyright to this source code.  In place of
       
 15380 ** a legal notice, here is a blessing:
       
 15381 **
       
 15382 **    May you do good and not evil.
       
 15383 **    May you find forgiveness for yourself and forgive others.
       
 15384 **    May you share freely, never taking more than you give.
       
 15385 **
       
 15386 *************************************************************************
       
 15387 ** This file contains the C functions that implement mutexes for win32
       
 15388 **
       
 15389 ** $Id: mutex_w32.c,v 1.18 2009/08/10 03:23:21 shane Exp $
       
 15390 */
       
 15391 
       
 15392 /*
       
 15393 ** The code in this file is only used if we are compiling multithreaded
       
 15394 ** on a win32 system.
       
 15395 */
       
 15396 #ifdef SQLITE_MUTEX_W32
       
 15397 
       
 15398 /*
       
 15399 ** Each recursive mutex is an instance of the following structure.
       
 15400 */
       
 15401 struct sqlite3_mutex {
       
 15402   CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
       
 15403   int id;                    /* Mutex type */
       
 15404   int nRef;                  /* Number of enterances */
       
 15405   DWORD owner;               /* Thread holding this mutex */
       
 15406 };
       
 15407 
       
 15408 /*
       
 15409 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
       
 15410 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
       
 15411 **
       
 15412 ** Here is an interesting observation:  Win95, Win98, and WinME lack
       
 15413 ** the LockFileEx() API.  But we can still statically link against that
       
 15414 ** API as long as we don't call it win running Win95/98/ME.  A call to
       
 15415 ** this routine is used to determine if the host is Win95/98/ME or
       
 15416 ** WinNT/2K/XP so that we will know whether or not we can safely call
       
 15417 ** the LockFileEx() API.
       
 15418 **
       
 15419 ** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
       
 15420 ** which is only available if your application was compiled with 
       
 15421 ** _WIN32_WINNT defined to a value >= 0x0400.  Currently, the only
       
 15422 ** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef 
       
 15423 ** this out as well.
       
 15424 */
       
 15425 #if 0
       
 15426 #if SQLITE_OS_WINCE
       
 15427 # define mutexIsNT()  (1)
       
 15428 #else
       
 15429   static int mutexIsNT(void){
       
 15430     static int osType = 0;
       
 15431     if( osType==0 ){
       
 15432       OSVERSIONINFO sInfo;
       
 15433       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
       
 15434       GetVersionEx(&sInfo);
       
 15435       osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
       
 15436     }
       
 15437     return osType==2;
       
 15438   }
       
 15439 #endif /* SQLITE_OS_WINCE */
       
 15440 #endif
       
 15441 
       
 15442 #ifdef SQLITE_DEBUG
       
 15443 /*
       
 15444 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
       
 15445 ** intended for use only inside assert() statements.
       
 15446 */
       
 15447 static int winMutexHeld(sqlite3_mutex *p){
       
 15448   return p->nRef!=0 && p->owner==GetCurrentThreadId();
       
 15449 }
       
 15450 static int winMutexNotheld(sqlite3_mutex *p){
       
 15451   return p->nRef==0 || p->owner!=GetCurrentThreadId();
       
 15452 }
       
 15453 #endif
       
 15454 
       
 15455 
       
 15456 /*
       
 15457 ** Initialize and deinitialize the mutex subsystem.
       
 15458 */
       
 15459 static sqlite3_mutex winMutex_staticMutexes[6];
       
 15460 static int winMutex_isInit = 0;
       
 15461 /* As winMutexInit() and winMutexEnd() are called as part
       
 15462 ** of the sqlite3_initialize and sqlite3_shutdown()
       
 15463 ** processing, the "interlocked" magic is probably not
       
 15464 ** strictly necessary.
       
 15465 */
       
 15466 static long winMutex_lock = 0;
       
 15467 
       
 15468 static int winMutexInit(void){ 
       
 15469   /* The first to increment to 1 does actual initialization */
       
 15470   if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
       
 15471     int i;
       
 15472     for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
       
 15473       InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
       
 15474     }
       
 15475     winMutex_isInit = 1;
       
 15476   }else{
       
 15477     /* Someone else is in the process of initing the static mutexes */
       
 15478     while( !winMutex_isInit ){
       
 15479       Sleep(1);
       
 15480     }
       
 15481   }
       
 15482   return SQLITE_OK; 
       
 15483 }
       
 15484 
       
 15485 static int winMutexEnd(void){ 
       
 15486   /* The first to decrement to 0 does actual shutdown 
       
 15487   ** (which should be the last to shutdown.) */
       
 15488   if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
       
 15489     if( winMutex_isInit==1 ){
       
 15490       int i;
       
 15491       for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
       
 15492         DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
       
 15493       }
       
 15494       winMutex_isInit = 0;
       
 15495     }
       
 15496   }
       
 15497   return SQLITE_OK; 
       
 15498 }
       
 15499 
       
 15500 /*
       
 15501 ** The sqlite3_mutex_alloc() routine allocates a new
       
 15502 ** mutex and returns a pointer to it.  If it returns NULL
       
 15503 ** that means that a mutex could not be allocated.  SQLite
       
 15504 ** will unwind its stack and return an error.  The argument
       
 15505 ** to sqlite3_mutex_alloc() is one of these integer constants:
       
 15506 **
       
 15507 ** <ul>
       
 15508 ** <li>  SQLITE_MUTEX_FAST
       
 15509 ** <li>  SQLITE_MUTEX_RECURSIVE
       
 15510 ** <li>  SQLITE_MUTEX_STATIC_MASTER
       
 15511 ** <li>  SQLITE_MUTEX_STATIC_MEM
       
 15512 ** <li>  SQLITE_MUTEX_STATIC_MEM2
       
 15513 ** <li>  SQLITE_MUTEX_STATIC_PRNG
       
 15514 ** <li>  SQLITE_MUTEX_STATIC_LRU
       
 15515 ** <li>  SQLITE_MUTEX_STATIC_LRU2
       
 15516 ** </ul>
       
 15517 **
       
 15518 ** The first two constants cause sqlite3_mutex_alloc() to create
       
 15519 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
       
 15520 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
       
 15521 ** The mutex implementation does not need to make a distinction
       
 15522 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
       
 15523 ** not want to.  But SQLite will only request a recursive mutex in
       
 15524 ** cases where it really needs one.  If a faster non-recursive mutex
       
 15525 ** implementation is available on the host platform, the mutex subsystem
       
 15526 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
       
 15527 **
       
 15528 ** The other allowed parameters to sqlite3_mutex_alloc() each return
       
 15529 ** a pointer to a static preexisting mutex.  Six static mutexes are
       
 15530 ** used by the current version of SQLite.  Future versions of SQLite
       
 15531 ** may add additional static mutexes.  Static mutexes are for internal
       
 15532 ** use by SQLite only.  Applications that use SQLite mutexes should
       
 15533 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
       
 15534 ** SQLITE_MUTEX_RECURSIVE.
       
 15535 **
       
 15536 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
       
 15537 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
       
 15538 ** returns a different mutex on every call.  But for the static 
       
 15539 ** mutex types, the same mutex is returned on every call that has
       
 15540 ** the same type number.
       
 15541 */
       
 15542 static sqlite3_mutex *winMutexAlloc(int iType){
       
 15543   sqlite3_mutex *p;
       
 15544 
       
 15545   switch( iType ){
       
 15546     case SQLITE_MUTEX_FAST:
       
 15547     case SQLITE_MUTEX_RECURSIVE: {
       
 15548       p = sqlite3MallocZero( sizeof(*p) );
       
 15549       if( p ){  
       
 15550         p->id = iType;
       
 15551         InitializeCriticalSection(&p->mutex);
       
 15552       }
       
 15553       break;
       
 15554     }
       
 15555     default: {
       
 15556       assert( winMutex_isInit==1 );
       
 15557       assert( iType-2 >= 0 );
       
 15558       assert( iType-2 < ArraySize(winMutex_staticMutexes) );
       
 15559       p = &winMutex_staticMutexes[iType-2];
       
 15560       p->id = iType;
       
 15561       break;
       
 15562     }
       
 15563   }
       
 15564   return p;
       
 15565 }
       
 15566 
       
 15567 
       
 15568 /*
       
 15569 ** This routine deallocates a previously
       
 15570 ** allocated mutex.  SQLite is careful to deallocate every
       
 15571 ** mutex that it allocates.
       
 15572 */
       
 15573 static void winMutexFree(sqlite3_mutex *p){
       
 15574   assert( p );
       
 15575   assert( p->nRef==0 );
       
 15576   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
       
 15577   DeleteCriticalSection(&p->mutex);
       
 15578   sqlite3_free(p);
       
 15579 }
       
 15580 
       
 15581 /*
       
 15582 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
       
 15583 ** to enter a mutex.  If another thread is already within the mutex,
       
 15584 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
       
 15585 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
       
 15586 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
       
 15587 ** be entered multiple times by the same thread.  In such cases the,
       
 15588 ** mutex must be exited an equal number of times before another thread
       
 15589 ** can enter.  If the same thread tries to enter any other kind of mutex
       
 15590 ** more than once, the behavior is undefined.
       
 15591 */
       
 15592 static void winMutexEnter(sqlite3_mutex *p){
       
 15593   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld(p) );
       
 15594   EnterCriticalSection(&p->mutex);
       
 15595   p->owner = GetCurrentThreadId(); 
       
 15596   p->nRef++;
       
 15597 }
       
 15598 static int winMutexTry(sqlite3_mutex *p){
       
 15599   int rc = SQLITE_BUSY;
       
 15600   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld(p) );
       
 15601   /*
       
 15602   ** The sqlite3_mutex_try() routine is very rarely used, and when it
       
 15603   ** is used it is merely an optimization.  So it is OK for it to always
       
 15604   ** fail.  
       
 15605   **
       
 15606   ** The TryEnterCriticalSection() interface is only available on WinNT.
       
 15607   ** And some windows compilers complain if you try to use it without
       
 15608   ** first doing some #defines that prevent SQLite from building on Win98.
       
 15609   ** For that reason, we will omit this optimization for now.  See
       
 15610   ** ticket #2685.
       
 15611   */
       
 15612 #if 0
       
 15613   if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
       
 15614     p->owner = GetCurrentThreadId();
       
 15615     p->nRef++;
       
 15616     rc = SQLITE_OK;
       
 15617   }
       
 15618 #else
       
 15619   UNUSED_PARAMETER(p);
       
 15620 #endif
       
 15621   return rc;
       
 15622 }
       
 15623 
       
 15624 /*
       
 15625 ** The sqlite3_mutex_leave() routine exits a mutex that was
       
 15626 ** previously entered by the same thread.  The behavior
       
 15627 ** is undefined if the mutex is not currently entered or
       
 15628 ** is not currently allocated.  SQLite will never do either.
       
 15629 */
       
 15630 static void winMutexLeave(sqlite3_mutex *p){
       
 15631   assert( p->nRef>0 );
       
 15632   assert( p->owner==GetCurrentThreadId() );
       
 15633   p->nRef--;
       
 15634   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
       
 15635   LeaveCriticalSection(&p->mutex);
       
 15636 }
       
 15637 
       
 15638 SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
       
 15639   static sqlite3_mutex_methods sMutex = {
       
 15640     winMutexInit,
       
 15641     winMutexEnd,
       
 15642     winMutexAlloc,
       
 15643     winMutexFree,
       
 15644     winMutexEnter,
       
 15645     winMutexTry,
       
 15646     winMutexLeave,
       
 15647 #ifdef SQLITE_DEBUG
       
 15648     winMutexHeld,
       
 15649     winMutexNotheld
       
 15650 #else
       
 15651     0,
       
 15652     0
       
 15653 #endif
       
 15654   };
       
 15655 
       
 15656   return &sMutex;
       
 15657 }
       
 15658 #endif /* SQLITE_MUTEX_W32 */
       
 15659 
       
 15660 /************** End of mutex_w32.c *******************************************/
       
 15661 /************** Begin file malloc.c ******************************************/
       
 15662 /*
       
 15663 ** 2001 September 15
       
 15664 **
       
 15665 ** The author disclaims copyright to this source code.  In place of
       
 15666 ** a legal notice, here is a blessing:
       
 15667 **
       
 15668 **    May you do good and not evil.
       
 15669 **    May you find forgiveness for yourself and forgive others.
       
 15670 **    May you share freely, never taking more than you give.
       
 15671 **
       
 15672 *************************************************************************
       
 15673 **
       
 15674 ** Memory allocation functions used throughout sqlite.
       
 15675 **
       
 15676 ** $Id: malloc.c,v 1.66 2009/07/17 11:44:07 drh Exp $
       
 15677 */
       
 15678 
       
 15679 /*
       
 15680 ** This routine runs when the memory allocator sees that the
       
 15681 ** total memory allocation is about to exceed the soft heap
       
 15682 ** limit.
       
 15683 */
       
 15684 static void softHeapLimitEnforcer(
       
 15685   void *NotUsed, 
       
 15686   sqlite3_int64 NotUsed2,
       
 15687   int allocSize
       
 15688 ){
       
 15689   UNUSED_PARAMETER2(NotUsed, NotUsed2);
       
 15690   sqlite3_release_memory(allocSize);
       
 15691 }
       
 15692 
       
 15693 /*
       
 15694 ** Set the soft heap-size limit for the library. Passing a zero or 
       
 15695 ** negative value indicates no limit.
       
 15696 */
       
 15697 SQLITE_API void sqlite3_soft_heap_limit(int n){
       
 15698   sqlite3_uint64 iLimit;
       
 15699   int overage;
       
 15700   if( n<0 ){
       
 15701     iLimit = 0;
       
 15702   }else{
       
 15703     iLimit = n;
       
 15704   }
       
 15705 #ifndef SQLITE_OMIT_AUTOINIT
       
 15706   sqlite3_initialize();
       
 15707 #endif
       
 15708   if( iLimit>0 ){
       
 15709     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit);
       
 15710   }else{
       
 15711     sqlite3MemoryAlarm(0, 0, 0);
       
 15712   }
       
 15713   overage = (int)(sqlite3_memory_used() - (i64)n);
       
 15714   if( overage>0 ){
       
 15715     sqlite3_release_memory(overage);
       
 15716   }
       
 15717 }
       
 15718 
       
 15719 /*
       
 15720 ** Attempt to release up to n bytes of non-essential memory currently
       
 15721 ** held by SQLite. An example of non-essential memory is memory used to
       
 15722 ** cache database pages that are not currently in use.
       
 15723 */
       
 15724 SQLITE_API int sqlite3_release_memory(int n){
       
 15725 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
       
 15726   int nRet = 0;
       
 15727 #if 0
       
 15728   nRet += sqlite3VdbeReleaseMemory(n);
       
 15729 #endif
       
 15730   nRet += sqlite3PcacheReleaseMemory(n-nRet);
       
 15731   return nRet;
       
 15732 #else
       
 15733   UNUSED_PARAMETER(n);
       
 15734   return SQLITE_OK;
       
 15735 #endif
       
 15736 }
       
 15737 
       
 15738 /*
       
 15739 ** State information local to the memory allocation subsystem.
       
 15740 */
       
 15741 static SQLITE_WSD struct Mem0Global {
       
 15742   /* Number of free pages for scratch and page-cache memory */
       
 15743   u32 nScratchFree;
       
 15744   u32 nPageFree;
       
 15745 
       
 15746   sqlite3_mutex *mutex;         /* Mutex to serialize access */
       
 15747 
       
 15748   /*
       
 15749   ** The alarm callback and its arguments.  The mem0.mutex lock will
       
 15750   ** be held while the callback is running.  Recursive calls into
       
 15751   ** the memory subsystem are allowed, but no new callbacks will be
       
 15752   ** issued.
       
 15753   */
       
 15754   sqlite3_int64 alarmThreshold;
       
 15755   void (*alarmCallback)(void*, sqlite3_int64,int);
       
 15756   void *alarmArg;
       
 15757 
       
 15758   /*
       
 15759   ** Pointers to the end of sqlite3GlobalConfig.pScratch and
       
 15760   ** sqlite3GlobalConfig.pPage to a block of memory that records
       
 15761   ** which pages are available.
       
 15762   */
       
 15763   u32 *aScratchFree;
       
 15764   u32 *aPageFree;
       
 15765 } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
       
 15766 
       
 15767 #define mem0 GLOBAL(struct Mem0Global, mem0)
       
 15768 
       
 15769 /*
       
 15770 ** Initialize the memory allocation subsystem.
       
 15771 */
       
 15772 SQLITE_PRIVATE int sqlite3MallocInit(void){
       
 15773   if( sqlite3GlobalConfig.m.xMalloc==0 ){
       
 15774     sqlite3MemSetDefault();
       
 15775   }
       
 15776   memset(&mem0, 0, sizeof(mem0));
       
 15777   if( sqlite3GlobalConfig.bCoreMutex ){
       
 15778     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
       
 15779   }
       
 15780   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
       
 15781       && sqlite3GlobalConfig.nScratch>=0 ){
       
 15782     int i;
       
 15783     sqlite3GlobalConfig.szScratch = ROUNDDOWN8(sqlite3GlobalConfig.szScratch-4);
       
 15784     mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch)
       
 15785                   [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch];
       
 15786     for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; }
       
 15787     mem0.nScratchFree = sqlite3GlobalConfig.nScratch;
       
 15788   }else{
       
 15789     sqlite3GlobalConfig.pScratch = 0;
       
 15790     sqlite3GlobalConfig.szScratch = 0;
       
 15791   }
       
 15792   if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512
       
 15793       && sqlite3GlobalConfig.nPage>=1 ){
       
 15794     int i;
       
 15795     int overhead;
       
 15796     int sz = ROUNDDOWN8(sqlite3GlobalConfig.szPage);
       
 15797     int n = sqlite3GlobalConfig.nPage;
       
 15798     overhead = (4*n + sz - 1)/sz;
       
 15799     sqlite3GlobalConfig.nPage -= overhead;
       
 15800     mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage)
       
 15801                   [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage];
       
 15802     for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; }
       
 15803     mem0.nPageFree = sqlite3GlobalConfig.nPage;
       
 15804   }else{
       
 15805     sqlite3GlobalConfig.pPage = 0;
       
 15806     sqlite3GlobalConfig.szPage = 0;
       
 15807   }
       
 15808   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
       
 15809 }
       
 15810 
       
 15811 /*
       
 15812 ** Deinitialize the memory allocation subsystem.
       
 15813 */
       
 15814 SQLITE_PRIVATE void sqlite3MallocEnd(void){
       
 15815   if( sqlite3GlobalConfig.m.xShutdown ){
       
 15816     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
       
 15817   }
       
 15818   memset(&mem0, 0, sizeof(mem0));
       
 15819 }
       
 15820 
       
 15821 /*
       
 15822 ** Return the amount of memory currently checked out.
       
 15823 */
       
 15824 SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
       
 15825   int n, mx;
       
 15826   sqlite3_int64 res;
       
 15827   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
       
 15828   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
       
 15829   return res;
       
 15830 }
       
 15831 
       
 15832 /*
       
 15833 ** Return the maximum amount of memory that has ever been
       
 15834 ** checked out since either the beginning of this process
       
 15835 ** or since the most recent reset.
       
 15836 */
       
 15837 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
       
 15838   int n, mx;
       
 15839   sqlite3_int64 res;
       
 15840   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
       
 15841   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
       
 15842   return res;
       
 15843 }
       
 15844 
       
 15845 /*
       
 15846 ** Change the alarm callback
       
 15847 */
       
 15848 SQLITE_PRIVATE int sqlite3MemoryAlarm(
       
 15849   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
       
 15850   void *pArg,
       
 15851   sqlite3_int64 iThreshold
       
 15852 ){
       
 15853   sqlite3_mutex_enter(mem0.mutex);
       
 15854   mem0.alarmCallback = xCallback;
       
 15855   mem0.alarmArg = pArg;
       
 15856   mem0.alarmThreshold = iThreshold;
       
 15857   sqlite3_mutex_leave(mem0.mutex);
       
 15858   return SQLITE_OK;
       
 15859 }
       
 15860 
       
 15861 #ifndef SQLITE_OMIT_DEPRECATED
       
 15862 /*
       
 15863 ** Deprecated external interface.  Internal/core SQLite code
       
 15864 ** should call sqlite3MemoryAlarm.
       
 15865 */
       
 15866 SQLITE_API int sqlite3_memory_alarm(
       
 15867   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
       
 15868   void *pArg,
       
 15869   sqlite3_int64 iThreshold
       
 15870 ){
       
 15871   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
       
 15872 }
       
 15873 #endif
       
 15874 
       
 15875 /*
       
 15876 ** Trigger the alarm 
       
 15877 */
       
 15878 static void sqlite3MallocAlarm(int nByte){
       
 15879   void (*xCallback)(void*,sqlite3_int64,int);
       
 15880   sqlite3_int64 nowUsed;
       
 15881   void *pArg;
       
 15882   if( mem0.alarmCallback==0 ) return;
       
 15883   xCallback = mem0.alarmCallback;
       
 15884   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
       
 15885   pArg = mem0.alarmArg;
       
 15886   mem0.alarmCallback = 0;
       
 15887   sqlite3_mutex_leave(mem0.mutex);
       
 15888   xCallback(pArg, nowUsed, nByte);
       
 15889   sqlite3_mutex_enter(mem0.mutex);
       
 15890   mem0.alarmCallback = xCallback;
       
 15891   mem0.alarmArg = pArg;
       
 15892 }
       
 15893 
       
 15894 /*
       
 15895 ** Do a memory allocation with statistics and alarms.  Assume the
       
 15896 ** lock is already held.
       
 15897 */
       
 15898 static int mallocWithAlarm(int n, void **pp){
       
 15899   int nFull;
       
 15900   void *p;
       
 15901   assert( sqlite3_mutex_held(mem0.mutex) );
       
 15902   nFull = sqlite3GlobalConfig.m.xRoundup(n);
       
 15903   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
       
 15904   if( mem0.alarmCallback!=0 ){
       
 15905     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
       
 15906     if( nUsed+nFull >= mem0.alarmThreshold ){
       
 15907       sqlite3MallocAlarm(nFull);
       
 15908     }
       
 15909   }
       
 15910   p = sqlite3GlobalConfig.m.xMalloc(nFull);
       
 15911   if( p==0 && mem0.alarmCallback ){
       
 15912     sqlite3MallocAlarm(nFull);
       
 15913     p = sqlite3GlobalConfig.m.xMalloc(nFull);
       
 15914   }
       
 15915   if( p ){
       
 15916     nFull = sqlite3MallocSize(p);
       
 15917     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
       
 15918   }
       
 15919   *pp = p;
       
 15920   return nFull;
       
 15921 }
       
 15922 
       
 15923 /*
       
 15924 ** Allocate memory.  This routine is like sqlite3_malloc() except that it
       
 15925 ** assumes the memory subsystem has already been initialized.
       
 15926 */
       
 15927 SQLITE_PRIVATE void *sqlite3Malloc(int n){
       
 15928   void *p;
       
 15929   if( n<=0 || n>=0x7fffff00 ){
       
 15930     /* A memory allocation of a number of bytes which is near the maximum
       
 15931     ** signed integer value might cause an integer overflow inside of the
       
 15932     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
       
 15933     ** 255 bytes of overhead.  SQLite itself will never use anything near
       
 15934     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
       
 15935     p = 0;
       
 15936   }else if( sqlite3GlobalConfig.bMemstat ){
       
 15937     sqlite3_mutex_enter(mem0.mutex);
       
 15938     mallocWithAlarm(n, &p);
       
 15939     sqlite3_mutex_leave(mem0.mutex);
       
 15940   }else{
       
 15941     p = sqlite3GlobalConfig.m.xMalloc(n);
       
 15942   }
       
 15943   return p;
       
 15944 }
       
 15945 
       
 15946 /*
       
 15947 ** This version of the memory allocation is for use by the application.
       
 15948 ** First make sure the memory subsystem is initialized, then do the
       
 15949 ** allocation.
       
 15950 */
       
 15951 SQLITE_API void *sqlite3_malloc(int n){
       
 15952 #ifndef SQLITE_OMIT_AUTOINIT
       
 15953   if( sqlite3_initialize() ) return 0;
       
 15954 #endif
       
 15955   return sqlite3Malloc(n);
       
 15956 }
       
 15957 
       
 15958 /*
       
 15959 ** Each thread may only have a single outstanding allocation from
       
 15960 ** xScratchMalloc().  We verify this constraint in the single-threaded
       
 15961 ** case by setting scratchAllocOut to 1 when an allocation
       
 15962 ** is outstanding clearing it when the allocation is freed.
       
 15963 */
       
 15964 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
       
 15965 static int scratchAllocOut = 0;
       
 15966 #endif
       
 15967 
       
 15968 
       
 15969 /*
       
 15970 ** Allocate memory that is to be used and released right away.
       
 15971 ** This routine is similar to alloca() in that it is not intended
       
 15972 ** for situations where the memory might be held long-term.  This
       
 15973 ** routine is intended to get memory to old large transient data
       
 15974 ** structures that would not normally fit on the stack of an
       
 15975 ** embedded processor.
       
 15976 */
       
 15977 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
       
 15978   void *p;
       
 15979   assert( n>0 );
       
 15980 
       
 15981 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
       
 15982   /* Verify that no more than one scratch allocation per thread
       
 15983   ** is outstanding at one time.  (This is only checked in the
       
 15984   ** single-threaded case since checking in the multi-threaded case
       
 15985   ** would be much more complicated.) */
       
 15986   assert( scratchAllocOut==0 );
       
 15987 #endif
       
 15988 
       
 15989   if( sqlite3GlobalConfig.szScratch<n ){
       
 15990     goto scratch_overflow;
       
 15991   }else{  
       
 15992     sqlite3_mutex_enter(mem0.mutex);
       
 15993     if( mem0.nScratchFree==0 ){
       
 15994       sqlite3_mutex_leave(mem0.mutex);
       
 15995       goto scratch_overflow;
       
 15996     }else{
       
 15997       int i;
       
 15998       i = mem0.aScratchFree[--mem0.nScratchFree];
       
 15999       i *= sqlite3GlobalConfig.szScratch;
       
 16000       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
       
 16001       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
       
 16002       sqlite3_mutex_leave(mem0.mutex);
       
 16003       p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
       
 16004       assert(  (((u8*)p - (u8*)0) & 7)==0 );
       
 16005     }
       
 16006   }
       
 16007 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
       
 16008   scratchAllocOut = p!=0;
       
 16009 #endif
       
 16010 
       
 16011   return p;
       
 16012 
       
 16013 scratch_overflow:
       
 16014   if( sqlite3GlobalConfig.bMemstat ){
       
 16015     sqlite3_mutex_enter(mem0.mutex);
       
 16016     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
       
 16017     n = mallocWithAlarm(n, &p);
       
 16018     if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
       
 16019     sqlite3_mutex_leave(mem0.mutex);
       
 16020   }else{
       
 16021     p = sqlite3GlobalConfig.m.xMalloc(n);
       
 16022   }
       
 16023 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
       
 16024   scratchAllocOut = p!=0;
       
 16025 #endif
       
 16026   return p;    
       
 16027 }
       
 16028 SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
       
 16029   if( p ){
       
 16030 
       
 16031 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
       
 16032     /* Verify that no more than one scratch allocation per thread
       
 16033     ** is outstanding at one time.  (This is only checked in the
       
 16034     ** single-threaded case since checking in the multi-threaded case
       
 16035     ** would be much more complicated.) */
       
 16036     assert( scratchAllocOut==1 );
       
 16037     scratchAllocOut = 0;
       
 16038 #endif
       
 16039 
       
 16040     if( sqlite3GlobalConfig.pScratch==0
       
 16041            || p<sqlite3GlobalConfig.pScratch
       
 16042            || p>=(void*)mem0.aScratchFree ){
       
 16043       if( sqlite3GlobalConfig.bMemstat ){
       
 16044         int iSize = sqlite3MallocSize(p);
       
 16045         sqlite3_mutex_enter(mem0.mutex);
       
 16046         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
       
 16047         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
       
 16048         sqlite3GlobalConfig.m.xFree(p);
       
 16049         sqlite3_mutex_leave(mem0.mutex);
       
 16050       }else{
       
 16051         sqlite3GlobalConfig.m.xFree(p);
       
 16052       }
       
 16053     }else{
       
 16054       int i;
       
 16055       i = (int)((u8*)p - (u8*)sqlite3GlobalConfig.pScratch);
       
 16056       i /= sqlite3GlobalConfig.szScratch;
       
 16057       assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
       
 16058       sqlite3_mutex_enter(mem0.mutex);
       
 16059       assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
       
 16060       mem0.aScratchFree[mem0.nScratchFree++] = i;
       
 16061       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
       
 16062       sqlite3_mutex_leave(mem0.mutex);
       
 16063     }
       
 16064   }
       
 16065 }
       
 16066 
       
 16067 /*
       
 16068 ** TRUE if p is a lookaside memory allocation from db
       
 16069 */
       
 16070 #ifndef SQLITE_OMIT_LOOKASIDE
       
 16071 static int isLookaside(sqlite3 *db, void *p){
       
 16072   return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
       
 16073 }
       
 16074 #else
       
 16075 #define isLookaside(A,B) 0
       
 16076 #endif
       
 16077 
       
 16078 /*
       
 16079 ** Return the size of a memory allocation previously obtained from
       
 16080 ** sqlite3Malloc() or sqlite3_malloc().
       
 16081 */
       
 16082 SQLITE_PRIVATE int sqlite3MallocSize(void *p){
       
 16083   return sqlite3GlobalConfig.m.xSize(p);
       
 16084 }
       
 16085 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
       
 16086   assert( db==0 || sqlite3_mutex_held(db->mutex) );
       
 16087   if( isLookaside(db, p) ){
       
 16088     return db->lookaside.sz;
       
 16089   }else{
       
 16090     return sqlite3GlobalConfig.m.xSize(p);
       
 16091   }
       
 16092 }
       
 16093 
       
 16094 /*
       
 16095 ** Free memory previously obtained from sqlite3Malloc().
       
 16096 */
       
 16097 SQLITE_API void sqlite3_free(void *p){
       
 16098   if( p==0 ) return;
       
 16099   if( sqlite3GlobalConfig.bMemstat ){
       
 16100     sqlite3_mutex_enter(mem0.mutex);
       
 16101     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
       
 16102     sqlite3GlobalConfig.m.xFree(p);
       
 16103     sqlite3_mutex_leave(mem0.mutex);
       
 16104   }else{
       
 16105     sqlite3GlobalConfig.m.xFree(p);
       
 16106   }
       
 16107 }
       
 16108 
       
 16109 /*
       
 16110 ** Free memory that might be associated with a particular database
       
 16111 ** connection.
       
 16112 */
       
 16113 SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
       
 16114   assert( db==0 || sqlite3_mutex_held(db->mutex) );
       
 16115   if( isLookaside(db, p) ){
       
 16116     LookasideSlot *pBuf = (LookasideSlot*)p;
       
 16117     pBuf->pNext = db->lookaside.pFree;
       
 16118     db->lookaside.pFree = pBuf;
       
 16119     db->lookaside.nOut--;
       
 16120   }else{
       
 16121     sqlite3_free(p);
       
 16122   }
       
 16123 }
       
 16124 
       
 16125 /*
       
 16126 ** Change the size of an existing memory allocation
       
 16127 */
       
 16128 SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){
       
 16129   int nOld, nNew;
       
 16130   void *pNew;
       
 16131   if( pOld==0 ){
       
 16132     return sqlite3Malloc(nBytes);
       
 16133   }
       
 16134   if( nBytes<=0 ){
       
 16135     sqlite3_free(pOld);
       
 16136     return 0;
       
 16137   }
       
 16138   if( nBytes>=0x7fffff00 ){
       
 16139     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
       
 16140     return 0;
       
 16141   }
       
 16142   nOld = sqlite3MallocSize(pOld);
       
 16143   nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
       
 16144   if( nOld==nNew ){
       
 16145     pNew = pOld;
       
 16146   }else if( sqlite3GlobalConfig.bMemstat ){
       
 16147     sqlite3_mutex_enter(mem0.mutex);
       
 16148     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
       
 16149     if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= 
       
 16150           mem0.alarmThreshold ){
       
 16151       sqlite3MallocAlarm(nNew-nOld);
       
 16152     }
       
 16153     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
       
 16154     if( pNew==0 && mem0.alarmCallback ){
       
 16155       sqlite3MallocAlarm(nBytes);
       
 16156       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
       
 16157     }
       
 16158     if( pNew ){
       
 16159       nNew = sqlite3MallocSize(pNew);
       
 16160       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
       
 16161     }
       
 16162     sqlite3_mutex_leave(mem0.mutex);
       
 16163   }else{
       
 16164     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
       
 16165   }
       
 16166   return pNew;
       
 16167 }
       
 16168 
       
 16169 /*
       
 16170 ** The public interface to sqlite3Realloc.  Make sure that the memory
       
 16171 ** subsystem is initialized prior to invoking sqliteRealloc.
       
 16172 */
       
 16173 SQLITE_API void *sqlite3_realloc(void *pOld, int n){
       
 16174 #ifndef SQLITE_OMIT_AUTOINIT
       
 16175   if( sqlite3_initialize() ) return 0;
       
 16176 #endif
       
 16177   return sqlite3Realloc(pOld, n);
       
 16178 }
       
 16179 
       
 16180 
       
 16181 /*
       
 16182 ** Allocate and zero memory.
       
 16183 */ 
       
 16184 SQLITE_PRIVATE void *sqlite3MallocZero(int n){
       
 16185   void *p = sqlite3Malloc(n);
       
 16186   if( p ){
       
 16187     memset(p, 0, n);
       
 16188   }
       
 16189   return p;
       
 16190 }
       
 16191 
       
 16192 /*
       
 16193 ** Allocate and zero memory.  If the allocation fails, make
       
 16194 ** the mallocFailed flag in the connection pointer.
       
 16195 */
       
 16196 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, int n){
       
 16197   void *p = sqlite3DbMallocRaw(db, n);
       
 16198   if( p ){
       
 16199     memset(p, 0, n);
       
 16200   }
       
 16201   return p;
       
 16202 }
       
 16203 
       
 16204 /*
       
 16205 ** Allocate and zero memory.  If the allocation fails, make
       
 16206 ** the mallocFailed flag in the connection pointer.
       
 16207 **
       
 16208 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
       
 16209 ** failure on the same database connection) then always return 0.
       
 16210 ** Hence for a particular database connection, once malloc starts
       
 16211 ** failing, it fails consistently until mallocFailed is reset.
       
 16212 ** This is an important assumption.  There are many places in the
       
 16213 ** code that do things like this:
       
 16214 **
       
 16215 **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
       
 16216 **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
       
 16217 **         if( b ) a[10] = 9;
       
 16218 **
       
 16219 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
       
 16220 ** that all prior mallocs (ex: "a") worked too.
       
 16221 */
       
 16222 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, int n){
       
 16223   void *p;
       
 16224   assert( db==0 || sqlite3_mutex_held(db->mutex) );
       
 16225 #ifndef SQLITE_OMIT_LOOKASIDE
       
 16226   if( db ){
       
 16227     LookasideSlot *pBuf;
       
 16228     if( db->mallocFailed ){
       
 16229       return 0;
       
 16230     }
       
 16231     if( db->lookaside.bEnabled && n<=db->lookaside.sz
       
 16232          && (pBuf = db->lookaside.pFree)!=0 ){
       
 16233       db->lookaside.pFree = pBuf->pNext;
       
 16234       db->lookaside.nOut++;
       
 16235       if( db->lookaside.nOut>db->lookaside.mxOut ){
       
 16236         db->lookaside.mxOut = db->lookaside.nOut;
       
 16237       }
       
 16238       return (void*)pBuf;
       
 16239     }
       
 16240   }
       
 16241 #else
       
 16242   if( db && db->mallocFailed ){
       
 16243     return 0;
       
 16244   }
       
 16245 #endif
       
 16246   p = sqlite3Malloc(n);
       
 16247   if( !p && db ){
       
 16248     db->mallocFailed = 1;
       
 16249   }
       
 16250   return p;
       
 16251 }
       
 16252 
       
 16253 /*
       
 16254 ** Resize the block of memory pointed to by p to n bytes. If the
       
 16255 ** resize fails, set the mallocFailed flag in the connection object.
       
 16256 */
       
 16257 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
       
 16258   void *pNew = 0;
       
 16259   assert( db!=0 );
       
 16260   assert( sqlite3_mutex_held(db->mutex) );
       
 16261   if( db->mallocFailed==0 ){
       
 16262     if( p==0 ){
       
 16263       return sqlite3DbMallocRaw(db, n);
       
 16264     }
       
 16265     if( isLookaside(db, p) ){
       
 16266       if( n<=db->lookaside.sz ){
       
 16267         return p;
       
 16268       }
       
 16269       pNew = sqlite3DbMallocRaw(db, n);
       
 16270       if( pNew ){
       
 16271         memcpy(pNew, p, db->lookaside.sz);
       
 16272         sqlite3DbFree(db, p);
       
 16273       }
       
 16274     }else{
       
 16275       pNew = sqlite3_realloc(p, n);
       
 16276       if( !pNew ){
       
 16277         db->mallocFailed = 1;
       
 16278       }
       
 16279     }
       
 16280   }
       
 16281   return pNew;
       
 16282 }
       
 16283 
       
 16284 /*
       
 16285 ** Attempt to reallocate p.  If the reallocation fails, then free p
       
 16286 ** and set the mallocFailed flag in the database connection.
       
 16287 */
       
 16288 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
       
 16289   void *pNew;
       
 16290   pNew = sqlite3DbRealloc(db, p, n);
       
 16291   if( !pNew ){
       
 16292     sqlite3DbFree(db, p);
       
 16293   }
       
 16294   return pNew;
       
 16295 }
       
 16296 
       
 16297 /*
       
 16298 ** Make a copy of a string in memory obtained from sqliteMalloc(). These 
       
 16299 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
       
 16300 ** is because when memory debugging is turned on, these two functions are 
       
 16301 ** called via macros that record the current file and line number in the
       
 16302 ** ThreadData structure.
       
 16303 */
       
 16304 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
       
 16305   char *zNew;
       
 16306   size_t n;
       
 16307   if( z==0 ){
       
 16308     return 0;
       
 16309   }
       
 16310   n = sqlite3Strlen30(z) + 1;
       
 16311   assert( (n&0x7fffffff)==n );
       
 16312   zNew = sqlite3DbMallocRaw(db, (int)n);
       
 16313   if( zNew ){
       
 16314     memcpy(zNew, z, n);
       
 16315   }
       
 16316   return zNew;
       
 16317 }
       
 16318 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
       
 16319   char *zNew;
       
 16320   if( z==0 ){
       
 16321     return 0;
       
 16322   }
       
 16323   assert( (n&0x7fffffff)==n );
       
 16324   zNew = sqlite3DbMallocRaw(db, n+1);
       
 16325   if( zNew ){
       
 16326     memcpy(zNew, z, n);
       
 16327     zNew[n] = 0;
       
 16328   }
       
 16329   return zNew;
       
 16330 }
       
 16331 
       
 16332 /*
       
 16333 ** Create a string from the zFromat argument and the va_list that follows.
       
 16334 ** Store the string in memory obtained from sqliteMalloc() and make *pz
       
 16335 ** point to that string.
       
 16336 */
       
 16337 SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
       
 16338   va_list ap;
       
 16339   char *z;
       
 16340 
       
 16341   va_start(ap, zFormat);
       
 16342   z = sqlite3VMPrintf(db, zFormat, ap);
       
 16343   va_end(ap);
       
 16344   sqlite3DbFree(db, *pz);
       
 16345   *pz = z;
       
 16346 }
       
 16347 
       
 16348 
       
 16349 /*
       
 16350 ** This function must be called before exiting any API function (i.e. 
       
 16351 ** returning control to the user) that has called sqlite3_malloc or
       
 16352 ** sqlite3_realloc.
       
 16353 **
       
 16354 ** The returned value is normally a copy of the second argument to this
       
 16355 ** function. However, if a malloc() failure has occurred since the previous
       
 16356 ** invocation SQLITE_NOMEM is returned instead. 
       
 16357 **
       
 16358 ** If the first argument, db, is not NULL and a malloc() error has occurred,
       
 16359 ** then the connection error-code (the value returned by sqlite3_errcode())
       
 16360 ** is set to SQLITE_NOMEM.
       
 16361 */
       
 16362 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
       
 16363   /* If the db handle is not NULL, then we must hold the connection handle
       
 16364   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 
       
 16365   ** is unsafe, as is the call to sqlite3Error().
       
 16366   */
       
 16367   assert( !db || sqlite3_mutex_held(db->mutex) );
       
 16368   if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
       
 16369     sqlite3Error(db, SQLITE_NOMEM, 0);
       
 16370     db->mallocFailed = 0;
       
 16371     rc = SQLITE_NOMEM;
       
 16372   }
       
 16373   return rc & (db ? db->errMask : 0xff);
       
 16374 }
       
 16375 
       
 16376 /************** End of malloc.c **********************************************/
       
 16377 /************** Begin file printf.c ******************************************/
       
 16378 /*
       
 16379 ** The "printf" code that follows dates from the 1980's.  It is in
       
 16380 ** the public domain.  The original comments are included here for
       
 16381 ** completeness.  They are very out-of-date but might be useful as
       
 16382 ** an historical reference.  Most of the "enhancements" have been backed
       
 16383 ** out so that the functionality is now the same as standard printf().
       
 16384 **
       
 16385 ** $Id: printf.c,v 1.104 2009/06/03 01:24:54 drh Exp $
       
 16386 **
       
 16387 **************************************************************************
       
 16388 **
       
 16389 ** The following modules is an enhanced replacement for the "printf" subroutines
       
 16390 ** found in the standard C library.  The following enhancements are
       
 16391 ** supported:
       
 16392 **
       
 16393 **      +  Additional functions.  The standard set of "printf" functions
       
 16394 **         includes printf, fprintf, sprintf, vprintf, vfprintf, and
       
 16395 **         vsprintf.  This module adds the following:
       
 16396 **
       
 16397 **           *  snprintf -- Works like sprintf, but has an extra argument
       
 16398 **                          which is the size of the buffer written to.
       
 16399 **
       
 16400 **           *  mprintf --  Similar to sprintf.  Writes output to memory
       
 16401 **                          obtained from malloc.
       
 16402 **
       
 16403 **           *  xprintf --  Calls a function to dispose of output.
       
 16404 **
       
 16405 **           *  nprintf --  No output, but returns the number of characters
       
 16406 **                          that would have been output by printf.
       
 16407 **
       
 16408 **           *  A v- version (ex: vsnprintf) of every function is also
       
 16409 **              supplied.
       
 16410 **
       
 16411 **      +  A few extensions to the formatting notation are supported:
       
 16412 **
       
 16413 **           *  The "=" flag (similar to "-") causes the output to be
       
 16414 **              be centered in the appropriately sized field.
       
 16415 **
       
 16416 **           *  The %b field outputs an integer in binary notation.
       
 16417 **
       
 16418 **           *  The %c field now accepts a precision.  The character output
       
 16419 **              is repeated by the number of times the precision specifies.
       
 16420 **
       
 16421 **           *  The %' field works like %c, but takes as its character the
       
 16422 **              next character of the format string, instead of the next
       
 16423 **              argument.  For example,  printf("%.78'-")  prints 78 minus
       
 16424 **              signs, the same as  printf("%.78c",'-').
       
 16425 **
       
 16426 **      +  When compiled using GCC on a SPARC, this version of printf is
       
 16427 **         faster than the library printf for SUN OS 4.1.
       
 16428 **
       
 16429 **      +  All functions are fully reentrant.
       
 16430 **
       
 16431 */
       
 16432 
       
 16433 /*
       
 16434 ** Conversion types fall into various categories as defined by the
       
 16435 ** following enumeration.
       
 16436 */
       
 16437 #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
       
 16438 #define etFLOAT       2 /* Floating point.  %f */
       
 16439 #define etEXP         3 /* Exponentional notation. %e and %E */
       
 16440 #define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
       
 16441 #define etSIZE        5 /* Return number of characters processed so far. %n */
       
 16442 #define etSTRING      6 /* Strings. %s */
       
 16443 #define etDYNSTRING   7 /* Dynamically allocated strings. %z */
       
 16444 #define etPERCENT     8 /* Percent symbol. %% */
       
 16445 #define etCHARX       9 /* Characters. %c */
       
 16446 /* The rest are extensions, not normally found in printf() */
       
 16447 #define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
       
 16448 #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
       
 16449                           NULL pointers replaced by SQL NULL.  %Q */
       
 16450 #define etTOKEN      12 /* a pointer to a Token structure */
       
 16451 #define etSRCLIST    13 /* a pointer to a SrcList */
       
 16452 #define etPOINTER    14 /* The %p conversion */
       
 16453 #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
       
 16454 #define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
       
 16455 
       
 16456 #define etINVALID     0 /* Any unrecognized conversion type */
       
 16457 
       
 16458 
       
 16459 /*
       
 16460 ** An "etByte" is an 8-bit unsigned value.
       
 16461 */
       
 16462 typedef unsigned char etByte;
       
 16463 
       
 16464 /*
       
 16465 ** Each builtin conversion character (ex: the 'd' in "%d") is described
       
 16466 ** by an instance of the following structure
       
 16467 */
       
 16468 typedef struct et_info {   /* Information about each format field */
       
 16469   char fmttype;            /* The format field code letter */
       
 16470   etByte base;             /* The base for radix conversion */
       
 16471   etByte flags;            /* One or more of FLAG_ constants below */
       
 16472   etByte type;             /* Conversion paradigm */
       
 16473   etByte charset;          /* Offset into aDigits[] of the digits string */
       
 16474   etByte prefix;           /* Offset into aPrefix[] of the prefix string */
       
 16475 } et_info;
       
 16476 
       
 16477 /*
       
 16478 ** Allowed values for et_info.flags
       
 16479 */
       
 16480 #define FLAG_SIGNED  1     /* True if the value to convert is signed */
       
 16481 #define FLAG_INTERN  2     /* True if for internal use only */
       
 16482 #define FLAG_STRING  4     /* Allow infinity precision */
       
 16483 
       
 16484 
       
 16485 /*
       
 16486 ** The following table is searched linearly, so it is good to put the
       
 16487 ** most frequently used conversion types first.
       
 16488 */
       
 16489 static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
       
 16490 static const char aPrefix[] = "-x0\000X0";
       
 16491 static const et_info fmtinfo[] = {
       
 16492   {  'd', 10, 1, etRADIX,      0,  0 },
       
 16493   {  's',  0, 4, etSTRING,     0,  0 },
       
 16494   {  'g',  0, 1, etGENERIC,    30, 0 },
       
 16495   {  'z',  0, 4, etDYNSTRING,  0,  0 },
       
 16496   {  'q',  0, 4, etSQLESCAPE,  0,  0 },
       
 16497   {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
       
 16498   {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
       
 16499   {  'c',  0, 0, etCHARX,      0,  0 },
       
 16500   {  'o',  8, 0, etRADIX,      0,  2 },
       
 16501   {  'u', 10, 0, etRADIX,      0,  0 },
       
 16502   {  'x', 16, 0, etRADIX,      16, 1 },
       
 16503   {  'X', 16, 0, etRADIX,      0,  4 },
       
 16504 #ifndef SQLITE_OMIT_FLOATING_POINT
       
 16505   {  'f',  0, 1, etFLOAT,      0,  0 },
       
 16506   {  'e',  0, 1, etEXP,        30, 0 },
       
 16507   {  'E',  0, 1, etEXP,        14, 0 },
       
 16508   {  'G',  0, 1, etGENERIC,    14, 0 },
       
 16509 #endif
       
 16510   {  'i', 10, 1, etRADIX,      0,  0 },
       
 16511   {  'n',  0, 0, etSIZE,       0,  0 },
       
 16512   {  '%',  0, 0, etPERCENT,    0,  0 },
       
 16513   {  'p', 16, 0, etPOINTER,    0,  1 },
       
 16514 
       
 16515 /* All the rest have the FLAG_INTERN bit set and are thus for internal
       
 16516 ** use only */
       
 16517   {  'T',  0, 2, etTOKEN,      0,  0 },
       
 16518   {  'S',  0, 2, etSRCLIST,    0,  0 },
       
 16519   {  'r', 10, 3, etORDINAL,    0,  0 },
       
 16520 };
       
 16521 
       
 16522 /*
       
 16523 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
       
 16524 ** conversions will work.
       
 16525 */
       
 16526 #ifndef SQLITE_OMIT_FLOATING_POINT
       
 16527 /*
       
 16528 ** "*val" is a double such that 0.1 <= *val < 10.0
       
 16529 ** Return the ascii code for the leading digit of *val, then
       
 16530 ** multiply "*val" by 10.0 to renormalize.
       
 16531 **
       
 16532 ** Example:
       
 16533 **     input:     *val = 3.14159
       
 16534 **     output:    *val = 1.4159    function return = '3'
       
 16535 **
       
 16536 ** The counter *cnt is incremented each time.  After counter exceeds
       
 16537 ** 16 (the number of significant digits in a 64-bit float) '0' is
       
 16538 ** always returned.
       
 16539 */
       
 16540 static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
       
 16541   int digit;
       
 16542   LONGDOUBLE_TYPE d;
       
 16543   if( (*cnt)++ >= 16 ) return '0';
       
 16544   digit = (int)*val;
       
 16545   d = digit;
       
 16546   digit += '0';
       
 16547   *val = (*val - d)*10.0;
       
 16548   return (char)digit;
       
 16549 }
       
 16550 #endif /* SQLITE_OMIT_FLOATING_POINT */
       
 16551 
       
 16552 /*
       
 16553 ** Append N space characters to the given string buffer.
       
 16554 */
       
 16555 static void appendSpace(StrAccum *pAccum, int N){
       
 16556   static const char zSpaces[] = "                             ";
       
 16557   while( N>=(int)sizeof(zSpaces)-1 ){
       
 16558     sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
       
 16559     N -= sizeof(zSpaces)-1;
       
 16560   }
       
 16561   if( N>0 ){
       
 16562     sqlite3StrAccumAppend(pAccum, zSpaces, N);
       
 16563   }
       
 16564 }
       
 16565 
       
 16566 /*
       
 16567 ** On machines with a small stack size, you can redefine the
       
 16568 ** SQLITE_PRINT_BUF_SIZE to be less than 350.
       
 16569 */
       
 16570 #ifndef SQLITE_PRINT_BUF_SIZE
       
 16571 # if defined(SQLITE_SMALL_STACK)
       
 16572 #   define SQLITE_PRINT_BUF_SIZE 50
       
 16573 # else
       
 16574 #   define SQLITE_PRINT_BUF_SIZE 350
       
 16575 # endif
       
 16576 #endif
       
 16577 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
       
 16578 
       
 16579 /*
       
 16580 ** The root program.  All variations call this core.
       
 16581 **
       
 16582 ** INPUTS:
       
 16583 **   func   This is a pointer to a function taking three arguments
       
 16584 **            1. A pointer to anything.  Same as the "arg" parameter.
       
 16585 **            2. A pointer to the list of characters to be output
       
 16586 **               (Note, this list is NOT null terminated.)
       
 16587 **            3. An integer number of characters to be output.
       
 16588 **               (Note: This number might be zero.)
       
 16589 **
       
 16590 **   arg    This is the pointer to anything which will be passed as the
       
 16591 **          first argument to "func".  Use it for whatever you like.
       
 16592 **
       
 16593 **   fmt    This is the format string, as in the usual print.
       
 16594 **
       
 16595 **   ap     This is a pointer to a list of arguments.  Same as in
       
 16596 **          vfprint.
       
 16597 **
       
 16598 ** OUTPUTS:
       
 16599 **          The return value is the total number of characters sent to
       
 16600 **          the function "func".  Returns -1 on a error.
       
 16601 **
       
 16602 ** Note that the order in which automatic variables are declared below
       
 16603 ** seems to make a big difference in determining how fast this beast
       
 16604 ** will run.
       
 16605 */
       
 16606 SQLITE_PRIVATE void sqlite3VXPrintf(
       
 16607   StrAccum *pAccum,                  /* Accumulate results here */
       
 16608   int useExtended,                   /* Allow extended %-conversions */
       
 16609   const char *fmt,                   /* Format string */
       
 16610   va_list ap                         /* arguments */
       
 16611 ){
       
 16612   int c;                     /* Next character in the format string */
       
 16613   char *bufpt;               /* Pointer to the conversion buffer */
       
 16614   int precision;             /* Precision of the current field */
       
 16615   int length;                /* Length of the field */
       
 16616   int idx;                   /* A general purpose loop counter */
       
 16617   int width;                 /* Width of the current field */
       
 16618   etByte flag_leftjustify;   /* True if "-" flag is present */
       
 16619   etByte flag_plussign;      /* True if "+" flag is present */
       
 16620   etByte flag_blanksign;     /* True if " " flag is present */
       
 16621   etByte flag_alternateform; /* True if "#" flag is present */
       
 16622   etByte flag_altform2;      /* True if "!" flag is present */
       
 16623   etByte flag_zeropad;       /* True if field width constant starts with zero */
       
 16624   etByte flag_long;          /* True if "l" flag is present */
       
 16625   etByte flag_longlong;      /* True if the "ll" flag is present */
       
 16626   etByte done;               /* Loop termination flag */
       
 16627   sqlite_uint64 longvalue;   /* Value for integer types */
       
 16628   LONGDOUBLE_TYPE realvalue; /* Value for real types */
       
 16629   const et_info *infop;      /* Pointer to the appropriate info structure */
       
 16630   char buf[etBUFSIZE];       /* Conversion buffer */
       
 16631   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
       
 16632   etByte xtype = 0;          /* Conversion paradigm */
       
 16633   char *zExtra;              /* Extra memory used for etTCLESCAPE conversions */
       
 16634 #ifndef SQLITE_OMIT_FLOATING_POINT
       
 16635   int  exp, e2;              /* exponent of real numbers */
       
 16636   double rounder;            /* Used for rounding floating point values */
       
 16637   etByte flag_dp;            /* True if decimal point should be shown */
       
 16638   etByte flag_rtz;           /* True if trailing zeros should be removed */
       
 16639   etByte flag_exp;           /* True to force display of the exponent */
       
 16640   int nsd;                   /* Number of significant digits returned */
       
 16641 #endif
       
 16642 
       
 16643   length = 0;
       
 16644   bufpt = 0;
       
 16645   for(; (c=(*fmt))!=0; ++fmt){
       
 16646     if( c!='%' ){
       
 16647       int amt;
       
 16648       bufpt = (char *)fmt;
       
 16649       amt = 1;
       
 16650       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
       
 16651       sqlite3StrAccumAppend(pAccum, bufpt, amt);
       
 16652       if( c==0 ) break;
       
 16653     }
       
 16654     if( (c=(*++fmt))==0 ){
       
 16655       sqlite3StrAccumAppend(pAccum, "%", 1);
       
 16656       break;
       
 16657     }
       
 16658     /* Find out what flags are present */
       
 16659     flag_leftjustify = flag_plussign = flag_blanksign = 
       
 16660      flag_alternateform = flag_altform2 = flag_zeropad = 0;
       
 16661     done = 0;
       
 16662     do{
       
 16663       switch( c ){
       
 16664         case '-':   flag_leftjustify = 1;     break;
       
 16665         case '+':   flag_plussign = 1;        break;
       
 16666         case ' ':   flag_blanksign = 1;       break;
       
 16667         case '#':   flag_alternateform = 1;   break;
       
 16668         case '!':   flag_altform2 = 1;        break;
       
 16669         case '0':   flag_zeropad = 1;         break;
       
 16670         default:    done = 1;                 break;
       
 16671       }
       
 16672     }while( !done && (c=(*++fmt))!=0 );
       
 16673     /* Get the field width */
       
 16674     width = 0;
       
 16675     if( c=='*' ){
       
 16676       width = va_arg(ap,int);
       
 16677       if( width<0 ){
       
 16678         flag_leftjustify = 1;
       
 16679         width = -width;
       
 16680       }
       
 16681       c = *++fmt;
       
 16682     }else{
       
 16683       while( c>='0' && c<='9' ){
       
 16684         width = width*10 + c - '0';
       
 16685         c = *++fmt;
       
 16686       }
       
 16687     }
       
 16688     if( width > etBUFSIZE-10 ){
       
 16689       width = etBUFSIZE-10;
       
 16690     }
       
 16691     /* Get the precision */
       
 16692     if( c=='.' ){
       
 16693       precision = 0;
       
 16694       c = *++fmt;
       
 16695       if( c=='*' ){
       
 16696         precision = va_arg(ap,int);
       
 16697         if( precision<0 ) precision = -precision;
       
 16698         c = *++fmt;
       
 16699       }else{
       
 16700         while( c>='0' && c<='9' ){
       
 16701           precision = precision*10 + c - '0';
       
 16702           c = *++fmt;
       
 16703         }
       
 16704       }
       
 16705     }else{
       
 16706       precision = -1;
       
 16707     }
       
 16708     /* Get the conversion type modifier */
       
 16709     if( c=='l' ){
       
 16710       flag_long = 1;
       
 16711       c = *++fmt;
       
 16712       if( c=='l' ){
       
 16713         flag_longlong = 1;
       
 16714         c = *++fmt;
       
 16715       }else{
       
 16716         flag_longlong = 0;
       
 16717       }
       
 16718     }else{
       
 16719       flag_long = flag_longlong = 0;
       
 16720     }
       
 16721     /* Fetch the info entry for the field */
       
 16722     infop = &fmtinfo[0];
       
 16723     xtype = etINVALID;
       
 16724     for(idx=0; idx<ArraySize(fmtinfo); idx++){
       
 16725       if( c==fmtinfo[idx].fmttype ){
       
 16726         infop = &fmtinfo[idx];
       
 16727         if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
       
 16728           xtype = infop->type;
       
 16729         }else{
       
 16730           return;
       
 16731         }
       
 16732         break;
       
 16733       }
       
 16734     }
       
 16735     zExtra = 0;
       
 16736 
       
 16737 
       
 16738     /* Limit the precision to prevent overflowing buf[] during conversion */
       
 16739     if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
       
 16740       precision = etBUFSIZE-40;
       
 16741     }
       
 16742 
       
 16743     /*
       
 16744     ** At this point, variables are initialized as follows:
       
 16745     **
       
 16746     **   flag_alternateform          TRUE if a '#' is present.
       
 16747     **   flag_altform2               TRUE if a '!' is present.
       
 16748     **   flag_plussign               TRUE if a '+' is present.
       
 16749     **   flag_leftjustify            TRUE if a '-' is present or if the
       
 16750     **                               field width was negative.
       
 16751     **   flag_zeropad                TRUE if the width began with 0.
       
 16752     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
       
 16753     **                               the conversion character.
       
 16754     **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
       
 16755     **                               the conversion character.
       
 16756     **   flag_blanksign              TRUE if a ' ' is present.
       
 16757     **   width                       The specified field width.  This is
       
 16758     **                               always non-negative.  Zero is the default.
       
 16759     **   precision                   The specified precision.  The default
       
 16760     **                               is -1.
       
 16761     **   xtype                       The class of the conversion.
       
 16762     **   infop                       Pointer to the appropriate info struct.
       
 16763     */
       
 16764     switch( xtype ){
       
 16765       case etPOINTER:
       
 16766         flag_longlong = sizeof(char*)==sizeof(i64);
       
 16767         flag_long = sizeof(char*)==sizeof(long int);
       
 16768         /* Fall through into the next case */
       
 16769       case etORDINAL:
       
 16770       case etRADIX:
       
 16771         if( infop->flags & FLAG_SIGNED ){
       
 16772           i64 v;
       
 16773           if( flag_longlong ){
       
 16774             v = va_arg(ap,i64);
       
 16775           }else if( flag_long ){
       
 16776             v = va_arg(ap,long int);
       
 16777           }else{
       
 16778             v = va_arg(ap,int);
       
 16779           }
       
 16780           if( v<0 ){
       
 16781             longvalue = -v;
       
 16782             prefix = '-';
       
 16783           }else{
       
 16784             longvalue = v;
       
 16785             if( flag_plussign )        prefix = '+';
       
 16786             else if( flag_blanksign )  prefix = ' ';
       
 16787             else                       prefix = 0;
       
 16788           }
       
 16789         }else{
       
 16790           if( flag_longlong ){
       
 16791             longvalue = va_arg(ap,u64);
       
 16792           }else if( flag_long ){
       
 16793             longvalue = va_arg(ap,unsigned long int);
       
 16794           }else{
       
 16795             longvalue = va_arg(ap,unsigned int);
       
 16796           }
       
 16797           prefix = 0;
       
 16798         }
       
 16799         if( longvalue==0 ) flag_alternateform = 0;
       
 16800         if( flag_zeropad && precision<width-(prefix!=0) ){
       
 16801           precision = width-(prefix!=0);
       
 16802         }
       
 16803         bufpt = &buf[etBUFSIZE-1];
       
 16804         if( xtype==etORDINAL ){
       
 16805           static const char zOrd[] = "thstndrd";
       
 16806           int x = (int)(longvalue % 10);
       
 16807           if( x>=4 || (longvalue/10)%10==1 ){
       
 16808             x = 0;
       
 16809           }
       
 16810           buf[etBUFSIZE-3] = zOrd[x*2];
       
 16811           buf[etBUFSIZE-2] = zOrd[x*2+1];
       
 16812           bufpt -= 2;
       
 16813         }
       
 16814         {
       
 16815           register const char *cset;      /* Use registers for speed */
       
 16816           register int base;
       
 16817           cset = &aDigits[infop->charset];
       
 16818           base = infop->base;
       
 16819           do{                                           /* Convert to ascii */
       
 16820             *(--bufpt) = cset[longvalue%base];
       
 16821             longvalue = longvalue/base;
       
 16822           }while( longvalue>0 );
       
 16823         }
       
 16824         length = (int)(&buf[etBUFSIZE-1]-bufpt);
       
 16825         for(idx=precision-length; idx>0; idx--){
       
 16826           *(--bufpt) = '0';                             /* Zero pad */
       
 16827         }
       
 16828         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
       
 16829         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
       
 16830           const char *pre;
       
 16831           char x;
       
 16832           pre = &aPrefix[infop->prefix];
       
 16833           for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
       
 16834         }
       
 16835         length = (int)(&buf[etBUFSIZE-1]-bufpt);
       
 16836         break;
       
 16837       case etFLOAT:
       
 16838       case etEXP:
       
 16839       case etGENERIC:
       
 16840         realvalue = va_arg(ap,double);
       
 16841 #ifndef SQLITE_OMIT_FLOATING_POINT
       
 16842         if( precision<0 ) precision = 6;         /* Set default precision */
       
 16843         if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
       
 16844         if( realvalue<0.0 ){
       
 16845           realvalue = -realvalue;
       
 16846           prefix = '-';
       
 16847         }else{
       
 16848           if( flag_plussign )          prefix = '+';
       
 16849           else if( flag_blanksign )    prefix = ' ';
       
 16850           else                         prefix = 0;
       
 16851         }
       
 16852         if( xtype==etGENERIC && precision>0 ) precision--;
       
 16853 #if 0
       
 16854         /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
       
 16855         for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
       
 16856 #else
       
 16857         /* It makes more sense to use 0.5 */
       
 16858         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
       
 16859 #endif
       
 16860         if( xtype==etFLOAT ) realvalue += rounder;
       
 16861         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
       
 16862         exp = 0;
       
 16863         if( sqlite3IsNaN((double)realvalue) ){
       
 16864           bufpt = "NaN";
       
 16865           length = 3;
       
 16866           break;
       
 16867         }
       
 16868         if( realvalue>0.0 ){
       
 16869           while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
       
 16870           while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
       
 16871           while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
       
 16872           while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
       
 16873           while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
       
 16874           if( exp>350 ){
       
 16875             if( prefix=='-' ){
       
 16876               bufpt = "-Inf";
       
 16877             }else if( prefix=='+' ){
       
 16878               bufpt = "+Inf";
       
 16879             }else{
       
 16880               bufpt = "Inf";
       
 16881             }
       
 16882             length = sqlite3Strlen30(bufpt);
       
 16883             break;
       
 16884           }
       
 16885         }
       
 16886         bufpt = buf;
       
 16887         /*
       
 16888         ** If the field type is etGENERIC, then convert to either etEXP
       
 16889         ** or etFLOAT, as appropriate.
       
 16890         */
       
 16891         flag_exp = xtype==etEXP;
       
 16892         if( xtype!=etFLOAT ){
       
 16893           realvalue += rounder;
       
 16894           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
       
 16895         }
       
 16896         if( xtype==etGENERIC ){
       
 16897           flag_rtz = !flag_alternateform;
       
 16898           if( exp<-4 || exp>precision ){
       
 16899             xtype = etEXP;
       
 16900           }else{
       
 16901             precision = precision - exp;
       
 16902             xtype = etFLOAT;
       
 16903           }
       
 16904         }else{
       
 16905           flag_rtz = 0;
       
 16906         }
       
 16907         if( xtype==etEXP ){
       
 16908           e2 = 0;
       
 16909         }else{
       
 16910           e2 = exp;
       
 16911         }
       
 16912         nsd = 0;
       
 16913         flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
       
 16914         /* The sign in front of the number */
       
 16915         if( prefix ){
       
 16916           *(bufpt++) = prefix;
       
 16917         }
       
 16918         /* Digits prior to the decimal point */
       
 16919         if( e2<0 ){
       
 16920           *(bufpt++) = '0';
       
 16921         }else{
       
 16922           for(; e2>=0; e2--){
       
 16923             *(bufpt++) = et_getdigit(&realvalue,&nsd);
       
 16924           }
       
 16925         }
       
 16926         /* The decimal point */
       
 16927         if( flag_dp ){
       
 16928           *(bufpt++) = '.';
       
 16929         }
       
 16930         /* "0" digits after the decimal point but before the first
       
 16931         ** significant digit of the number */
       
 16932         for(e2++; e2<0; precision--, e2++){
       
 16933           assert( precision>0 );
       
 16934           *(bufpt++) = '0';
       
 16935         }
       
 16936         /* Significant digits after the decimal point */
       
 16937         while( (precision--)>0 ){
       
 16938           *(bufpt++) = et_getdigit(&realvalue,&nsd);
       
 16939         }
       
 16940         /* Remove trailing zeros and the "." if no digits follow the "." */
       
 16941         if( flag_rtz && flag_dp ){
       
 16942           while( bufpt[-1]=='0' ) *(--bufpt) = 0;
       
 16943           assert( bufpt>buf );
       
 16944           if( bufpt[-1]=='.' ){
       
 16945             if( flag_altform2 ){
       
 16946               *(bufpt++) = '0';
       
 16947             }else{
       
 16948               *(--bufpt) = 0;
       
 16949             }
       
 16950           }
       
 16951         }
       
 16952         /* Add the "eNNN" suffix */
       
 16953         if( flag_exp || xtype==etEXP ){
       
 16954           *(bufpt++) = aDigits[infop->charset];
       
 16955           if( exp<0 ){
       
 16956             *(bufpt++) = '-'; exp = -exp;
       
 16957           }else{
       
 16958             *(bufpt++) = '+';
       
 16959           }
       
 16960           if( exp>=100 ){
       
 16961             *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
       
 16962             exp %= 100;
       
 16963           }
       
 16964           *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
       
 16965           *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
       
 16966         }
       
 16967         *bufpt = 0;
       
 16968 
       
 16969         /* The converted number is in buf[] and zero terminated. Output it.
       
 16970         ** Note that the number is in the usual order, not reversed as with
       
 16971         ** integer conversions. */
       
 16972         length = (int)(bufpt-buf);
       
 16973         bufpt = buf;
       
 16974 
       
 16975         /* Special case:  Add leading zeros if the flag_zeropad flag is
       
 16976         ** set and we are not left justified */
       
 16977         if( flag_zeropad && !flag_leftjustify && length < width){
       
 16978           int i;
       
 16979           int nPad = width - length;
       
 16980           for(i=width; i>=nPad; i--){
       
 16981             bufpt[i] = bufpt[i-nPad];
       
 16982           }
       
 16983           i = prefix!=0;
       
 16984           while( nPad-- ) bufpt[i++] = '0';
       
 16985           length = width;
       
 16986         }
       
 16987 #endif
       
 16988         break;
       
 16989       case etSIZE:
       
 16990         *(va_arg(ap,int*)) = pAccum->nChar;
       
 16991         length = width = 0;
       
 16992         break;
       
 16993       case etPERCENT:
       
 16994         buf[0] = '%';
       
 16995         bufpt = buf;
       
 16996         length = 1;
       
 16997         break;
       
 16998       case etCHARX:
       
 16999         c = va_arg(ap,int);
       
 17000         buf[0] = (char)c;
       
 17001         if( precision>=0 ){
       
 17002           for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
       
 17003           length = precision;
       
 17004         }else{
       
 17005           length =1;
       
 17006         }
       
 17007         bufpt = buf;
       
 17008         break;
       
 17009       case etSTRING:
       
 17010       case etDYNSTRING:
       
 17011         bufpt = va_arg(ap,char*);
       
 17012         if( bufpt==0 ){
       
 17013           bufpt = "";
       
 17014         }else if( xtype==etDYNSTRING ){
       
 17015           zExtra = bufpt;
       
 17016         }
       
 17017         if( precision>=0 ){
       
 17018           for(length=0; length<precision && bufpt[length]; length++){}
       
 17019         }else{
       
 17020           length = sqlite3Strlen30(bufpt);
       
 17021         }
       
 17022         break;
       
 17023       case etSQLESCAPE:
       
 17024       case etSQLESCAPE2:
       
 17025       case etSQLESCAPE3: {
       
 17026         int i, j, n, isnull;
       
 17027         int needQuote;
       
 17028         char ch;
       
 17029         char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
       
 17030         char *escarg = va_arg(ap,char*);
       
 17031         isnull = escarg==0;
       
 17032         if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
       
 17033         for(i=n=0; (ch=escarg[i])!=0; i++){
       
 17034           if( ch==q )  n++;
       
 17035         }
       
 17036         needQuote = !isnull && xtype==etSQLESCAPE2;
       
 17037         n += i + 1 + needQuote*2;
       
 17038         if( n>etBUFSIZE ){
       
 17039           bufpt = zExtra = sqlite3Malloc( n );
       
 17040           if( bufpt==0 ){
       
 17041             pAccum->mallocFailed = 1;
       
 17042             return;
       
 17043           }
       
 17044         }else{
       
 17045           bufpt = buf;
       
 17046         }
       
 17047         j = 0;
       
 17048         if( needQuote ) bufpt[j++] = q;
       
 17049         for(i=0; (ch=escarg[i])!=0; i++){
       
 17050           bufpt[j++] = ch;
       
 17051           if( ch==q ) bufpt[j++] = ch;
       
 17052         }
       
 17053         if( needQuote ) bufpt[j++] = q;
       
 17054         bufpt[j] = 0;
       
 17055         length = j;
       
 17056         /* The precision is ignored on %q and %Q */
       
 17057         /* if( precision>=0 && precision<length ) length = precision; */
       
 17058         break;
       
 17059       }
       
 17060       case etTOKEN: {
       
 17061         Token *pToken = va_arg(ap, Token*);
       
 17062         if( pToken ){
       
 17063           sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
       
 17064         }
       
 17065         length = width = 0;
       
 17066         break;
       
 17067       }
       
 17068       case etSRCLIST: {
       
 17069         SrcList *pSrc = va_arg(ap, SrcList*);
       
 17070         int k = va_arg(ap, int);
       
 17071         struct SrcList_item *pItem = &pSrc->a[k];
       
 17072         assert( k>=0 && k<pSrc->nSrc );
       
 17073         if( pItem->zDatabase ){
       
 17074           sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
       
 17075           sqlite3StrAccumAppend(pAccum, ".", 1);
       
 17076         }
       
 17077         sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
       
 17078         length = width = 0;
       
 17079         break;
       
 17080       }
       
 17081       default: {
       
 17082         assert( xtype==etINVALID );
       
 17083         return;
       
 17084       }
       
 17085     }/* End switch over the format type */
       
 17086     /*
       
 17087     ** The text of the conversion is pointed to by "bufpt" and is
       
 17088     ** "length" characters long.  The field width is "width".  Do
       
 17089     ** the output.
       
 17090     */
       
 17091     if( !flag_leftjustify ){
       
 17092       register int nspace;
       
 17093       nspace = width-length;
       
 17094       if( nspace>0 ){
       
 17095         appendSpace(pAccum, nspace);
       
 17096       }
       
 17097     }
       
 17098     if( length>0 ){
       
 17099       sqlite3StrAccumAppend(pAccum, bufpt, length);
       
 17100     }
       
 17101     if( flag_leftjustify ){
       
 17102       register int nspace;
       
 17103       nspace = width-length;
       
 17104       if( nspace>0 ){
       
 17105         appendSpace(pAccum, nspace);
       
 17106       }
       
 17107     }
       
 17108     if( zExtra ){
       
 17109       sqlite3_free(zExtra);
       
 17110     }
       
 17111   }/* End for loop over the format string */
       
 17112 } /* End of function */
       
 17113 
       
 17114 /*
       
 17115 ** Append N bytes of text from z to the StrAccum object.
       
 17116 */
       
 17117 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
       
 17118   assert( z!=0 || N==0 );
       
 17119   if( p->tooBig | p->mallocFailed ){
       
 17120     testcase(p->tooBig);
       
 17121     testcase(p->mallocFailed);
       
 17122     return;
       
 17123   }
       
 17124   if( N<0 ){
       
 17125     N = sqlite3Strlen30(z);
       
 17126   }
       
 17127   if( N==0 || NEVER(z==0) ){
       
 17128     return;
       
 17129   }
       
 17130   if( p->nChar+N >= p->nAlloc ){
       
 17131     char *zNew;
       
 17132     if( !p->useMalloc ){
       
 17133       p->tooBig = 1;
       
 17134       N = p->nAlloc - p->nChar - 1;
       
 17135       if( N<=0 ){
       
 17136         return;
       
 17137       }
       
 17138     }else{
       
 17139       i64 szNew = p->nChar;
       
 17140       szNew += N + 1;
       
 17141       if( szNew > p->mxAlloc ){
       
 17142         sqlite3StrAccumReset(p);
       
 17143         p->tooBig = 1;
       
 17144         return;
       
 17145       }else{
       
 17146         p->nAlloc = (int)szNew;
       
 17147       }
       
 17148       zNew = sqlite3DbMallocRaw(p->db, p->nAlloc );
       
 17149       if( zNew ){
       
 17150         memcpy(zNew, p->zText, p->nChar);
       
 17151         sqlite3StrAccumReset(p);
       
 17152         p->zText = zNew;
       
 17153       }else{
       
 17154         p->mallocFailed = 1;
       
 17155         sqlite3StrAccumReset(p);
       
 17156         return;
       
 17157       }
       
 17158     }
       
 17159   }
       
 17160   memcpy(&p->zText[p->nChar], z, N);
       
 17161   p->nChar += N;
       
 17162 }
       
 17163 
       
 17164 /*
       
 17165 ** Finish off a string by making sure it is zero-terminated.
       
 17166 ** Return a pointer to the resulting string.  Return a NULL
       
 17167 ** pointer if any kind of error was encountered.
       
 17168 */
       
 17169 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
       
 17170   if( p->zText ){
       
 17171     p->zText[p->nChar] = 0;
       
 17172     if( p->useMalloc && p->zText==p->zBase ){
       
 17173       p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
       
 17174       if( p->zText ){
       
 17175         memcpy(p->zText, p->zBase, p->nChar+1);
       
 17176       }else{
       
 17177         p->mallocFailed = 1;
       
 17178       }
       
 17179     }
       
 17180   }
       
 17181   return p->zText;
       
 17182 }
       
 17183 
       
 17184 /*
       
 17185 ** Reset an StrAccum string.  Reclaim all malloced memory.
       
 17186 */
       
 17187 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
       
 17188   if( p->zText!=p->zBase ){
       
 17189     sqlite3DbFree(p->db, p->zText);
       
 17190   }
       
 17191   p->zText = 0;
       
 17192 }
       
 17193 
       
 17194 /*
       
 17195 ** Initialize a string accumulator
       
 17196 */
       
 17197 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
       
 17198   p->zText = p->zBase = zBase;
       
 17199   p->db = 0;
       
 17200   p->nChar = 0;
       
 17201   p->nAlloc = n;
       
 17202   p->mxAlloc = mx;
       
 17203   p->useMalloc = 1;
       
 17204   p->tooBig = 0;
       
 17205   p->mallocFailed = 0;
       
 17206 }
       
 17207 
       
 17208 /*
       
 17209 ** Print into memory obtained from sqliteMalloc().  Use the internal
       
 17210 ** %-conversion extensions.
       
 17211 */
       
 17212 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
       
 17213   char *z;
       
 17214   char zBase[SQLITE_PRINT_BUF_SIZE];
       
 17215   StrAccum acc;
       
 17216   assert( db!=0 );
       
 17217   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
       
 17218                       db->aLimit[SQLITE_LIMIT_LENGTH]);
       
 17219   acc.db = db;
       
 17220   sqlite3VXPrintf(&acc, 1, zFormat, ap);
       
 17221   z = sqlite3StrAccumFinish(&acc);
       
 17222   if( acc.mallocFailed ){
       
 17223     db->mallocFailed = 1;
       
 17224   }
       
 17225   return z;
       
 17226 }
       
 17227 
       
 17228 /*
       
 17229 ** Print into memory obtained from sqliteMalloc().  Use the internal
       
 17230 ** %-conversion extensions.
       
 17231 */
       
 17232 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
       
 17233   va_list ap;
       
 17234   char *z;
       
 17235   va_start(ap, zFormat);
       
 17236   z = sqlite3VMPrintf(db, zFormat, ap);
       
 17237   va_end(ap);
       
 17238   return z;
       
 17239 }
       
 17240 
       
 17241 /*
       
 17242 ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
       
 17243 ** the string and before returnning.  This routine is intended to be used
       
 17244 ** to modify an existing string.  For example:
       
 17245 **
       
 17246 **       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
       
 17247 **
       
 17248 */
       
 17249 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
       
 17250   va_list ap;
       
 17251   char *z;
       
 17252   va_start(ap, zFormat);
       
 17253   z = sqlite3VMPrintf(db, zFormat, ap);
       
 17254   va_end(ap);
       
 17255   sqlite3DbFree(db, zStr);
       
 17256   return z;
       
 17257 }
       
 17258 
       
 17259 /*
       
 17260 ** Print into memory obtained from sqlite3_malloc().  Omit the internal
       
 17261 ** %-conversion extensions.
       
 17262 */
       
 17263 SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
       
 17264   char *z;
       
 17265   char zBase[SQLITE_PRINT_BUF_SIZE];
       
 17266   StrAccum acc;
       
 17267 #ifndef SQLITE_OMIT_AUTOINIT
       
 17268   if( sqlite3_initialize() ) return 0;
       
 17269 #endif
       
 17270   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
       
 17271   sqlite3VXPrintf(&acc, 0, zFormat, ap);
       
 17272   z = sqlite3StrAccumFinish(&acc);
       
 17273   return z;
       
 17274 }
       
 17275 
       
 17276 /*
       
 17277 ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
       
 17278 ** %-conversion extensions.
       
 17279 */
       
 17280 SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
       
 17281   va_list ap;
       
 17282   char *z;
       
 17283 #ifndef SQLITE_OMIT_AUTOINIT
       
 17284   if( sqlite3_initialize() ) return 0;
       
 17285 #endif
       
 17286   va_start(ap, zFormat);
       
 17287   z = sqlite3_vmprintf(zFormat, ap);
       
 17288   va_end(ap);
       
 17289   return z;
       
 17290 }
       
 17291 
       
 17292 /*
       
 17293 ** sqlite3_snprintf() works like snprintf() except that it ignores the
       
 17294 ** current locale settings.  This is important for SQLite because we
       
 17295 ** are not able to use a "," as the decimal point in place of "." as
       
 17296 ** specified by some locales.
       
 17297 */
       
 17298 SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
       
 17299   char *z;
       
 17300   va_list ap;
       
 17301   StrAccum acc;
       
 17302 
       
 17303   if( n<=0 ){
       
 17304     return zBuf;
       
 17305   }
       
 17306   sqlite3StrAccumInit(&acc, zBuf, n, 0);
       
 17307   acc.useMalloc = 0;
       
 17308   va_start(ap,zFormat);
       
 17309   sqlite3VXPrintf(&acc, 0, zFormat, ap);
       
 17310   va_end(ap);
       
 17311   z = sqlite3StrAccumFinish(&acc);
       
 17312   return z;
       
 17313 }
       
 17314 
       
 17315 #if defined(SQLITE_DEBUG)
       
 17316 /*
       
 17317 ** A version of printf() that understands %lld.  Used for debugging.
       
 17318 ** The printf() built into some versions of windows does not understand %lld
       
 17319 ** and segfaults if you give it a long long int.
       
 17320 */
       
 17321 SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
       
 17322   va_list ap;
       
 17323   StrAccum acc;
       
 17324   char zBuf[500];
       
 17325   sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
       
 17326   acc.useMalloc = 0;
       
 17327   va_start(ap,zFormat);
       
 17328   sqlite3VXPrintf(&acc, 0, zFormat, ap);
       
 17329   va_end(ap);
       
 17330   sqlite3StrAccumFinish(&acc);
       
 17331   fprintf(stdout,"%s", zBuf);
       
 17332   fflush(stdout);
       
 17333 }
       
 17334 #endif
       
 17335 
       
 17336 /************** End of printf.c **********************************************/
       
 17337 /************** Begin file random.c ******************************************/
       
 17338 /*
       
 17339 ** 2001 September 15
       
 17340 **
       
 17341 ** The author disclaims copyright to this source code.  In place of
       
 17342 ** a legal notice, here is a blessing:
       
 17343 **
       
 17344 **    May you do good and not evil.
       
 17345 **    May you find forgiveness for yourself and forgive others.
       
 17346 **    May you share freely, never taking more than you give.
       
 17347 **
       
 17348 *************************************************************************
       
 17349 ** This file contains code to implement a pseudo-random number
       
 17350 ** generator (PRNG) for SQLite.
       
 17351 **
       
 17352 ** Random numbers are used by some of the database backends in order
       
 17353 ** to generate random integer keys for tables or random filenames.
       
 17354 **
       
 17355 ** $Id: random.c,v 1.29 2008/12/10 19:26:24 drh Exp $
       
 17356 */
       
 17357 
       
 17358 
       
 17359 /* All threads share a single random number generator.
       
 17360 ** This structure is the current state of the generator.
       
 17361 */
       
 17362 static SQLITE_WSD struct sqlite3PrngType {
       
 17363   unsigned char isInit;          /* True if initialized */
       
 17364   unsigned char i, j;            /* State variables */
       
 17365   unsigned char s[256];          /* State variables */
       
 17366 } sqlite3Prng;
       
 17367 
       
 17368 /*
       
 17369 ** Get a single 8-bit random value from the RC4 PRNG.  The Mutex
       
 17370 ** must be held while executing this routine.
       
 17371 **
       
 17372 ** Why not just use a library random generator like lrand48() for this?
       
 17373 ** Because the OP_NewRowid opcode in the VDBE depends on having a very
       
 17374 ** good source of random numbers.  The lrand48() library function may
       
 17375 ** well be good enough.  But maybe not.  Or maybe lrand48() has some
       
 17376 ** subtle problems on some systems that could cause problems.  It is hard
       
 17377 ** to know.  To minimize the risk of problems due to bad lrand48()
       
 17378 ** implementations, SQLite uses this random number generator based
       
 17379 ** on RC4, which we know works very well.
       
 17380 **
       
 17381 ** (Later):  Actually, OP_NewRowid does not depend on a good source of
       
 17382 ** randomness any more.  But we will leave this code in all the same.
       
 17383 */
       
 17384 static u8 randomByte(void){
       
 17385   unsigned char t;
       
 17386 
       
 17387 
       
 17388   /* The "wsdPrng" macro will resolve to the pseudo-random number generator
       
 17389   ** state vector.  If writable static data is unsupported on the target,
       
 17390   ** we have to locate the state vector at run-time.  In the more common
       
 17391   ** case where writable static data is supported, wsdPrng can refer directly
       
 17392   ** to the "sqlite3Prng" state vector declared above.
       
 17393   */
       
 17394 #ifdef SQLITE_OMIT_WSD
       
 17395   struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
       
 17396 # define wsdPrng p[0]
       
 17397 #else
       
 17398 # define wsdPrng sqlite3Prng
       
 17399 #endif
       
 17400 
       
 17401 
       
 17402   /* Initialize the state of the random number generator once,
       
 17403   ** the first time this routine is called.  The seed value does
       
 17404   ** not need to contain a lot of randomness since we are not
       
 17405   ** trying to do secure encryption or anything like that...
       
 17406   **
       
 17407   ** Nothing in this file or anywhere else in SQLite does any kind of
       
 17408   ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
       
 17409   ** number generator) not as an encryption device.
       
 17410   */
       
 17411   if( !wsdPrng.isInit ){
       
 17412     int i;
       
 17413     char k[256];
       
 17414     wsdPrng.j = 0;
       
 17415     wsdPrng.i = 0;
       
 17416     sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
       
 17417     for(i=0; i<256; i++){
       
 17418       wsdPrng.s[i] = (u8)i;
       
 17419     }
       
 17420     for(i=0; i<256; i++){
       
 17421       wsdPrng.j += wsdPrng.s[i] + k[i];
       
 17422       t = wsdPrng.s[wsdPrng.j];
       
 17423       wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
       
 17424       wsdPrng.s[i] = t;
       
 17425     }
       
 17426     wsdPrng.isInit = 1;
       
 17427   }
       
 17428 
       
 17429   /* Generate and return single random byte
       
 17430   */
       
 17431   wsdPrng.i++;
       
 17432   t = wsdPrng.s[wsdPrng.i];
       
 17433   wsdPrng.j += t;
       
 17434   wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
       
 17435   wsdPrng.s[wsdPrng.j] = t;
       
 17436   t += wsdPrng.s[wsdPrng.i];
       
 17437   return wsdPrng.s[t];
       
 17438 }
       
 17439 
       
 17440 /*
       
 17441 ** Return N random bytes.
       
 17442 */
       
 17443 SQLITE_API void sqlite3_randomness(int N, void *pBuf){
       
 17444   unsigned char *zBuf = pBuf;
       
 17445 #if SQLITE_THREADSAFE
       
 17446   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
       
 17447 #endif
       
 17448   sqlite3_mutex_enter(mutex);
       
 17449   while( N-- ){
       
 17450     *(zBuf++) = randomByte();
       
 17451   }
       
 17452   sqlite3_mutex_leave(mutex);
       
 17453 }
       
 17454 
       
 17455 #ifndef SQLITE_OMIT_BUILTIN_TEST
       
 17456 /*
       
 17457 ** For testing purposes, we sometimes want to preserve the state of
       
 17458 ** PRNG and restore the PRNG to its saved state at a later time, or
       
 17459 ** to reset the PRNG to its initial state.  These routines accomplish
       
 17460 ** those tasks.
       
 17461 **
       
 17462 ** The sqlite3_test_control() interface calls these routines to
       
 17463 ** control the PRNG.
       
 17464 */
       
 17465 static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
       
 17466 SQLITE_PRIVATE void sqlite3PrngSaveState(void){
       
 17467   memcpy(
       
 17468     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
       
 17469     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
       
 17470     sizeof(sqlite3Prng)
       
 17471   );
       
 17472 }
       
 17473 SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
       
 17474   memcpy(
       
 17475     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
       
 17476     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
       
 17477     sizeof(sqlite3Prng)
       
 17478   );
       
 17479 }
       
 17480 SQLITE_PRIVATE void sqlite3PrngResetState(void){
       
 17481   GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
       
 17482 }
       
 17483 #endif /* SQLITE_OMIT_BUILTIN_TEST */
       
 17484 
       
 17485 /************** End of random.c **********************************************/
       
 17486 /************** Begin file utf.c *********************************************/
       
 17487 /*
       
 17488 ** 2004 April 13
       
 17489 **
       
 17490 ** The author disclaims copyright to this source code.  In place of
       
 17491 ** a legal notice, here is a blessing:
       
 17492 **
       
 17493 **    May you do good and not evil.
       
 17494 **    May you find forgiveness for yourself and forgive others.
       
 17495 **    May you share freely, never taking more than you give.
       
 17496 **
       
 17497 *************************************************************************
       
 17498 ** This file contains routines used to translate between UTF-8, 
       
 17499 ** UTF-16, UTF-16BE, and UTF-16LE.
       
 17500 **
       
 17501 ** $Id: utf.c,v 1.73 2009/04/01 18:40:32 drh Exp $
       
 17502 **
       
 17503 ** Notes on UTF-8:
       
 17504 **
       
 17505 **   Byte-0    Byte-1    Byte-2    Byte-3    Value
       
 17506 **  0xxxxxxx                                 00000000 00000000 0xxxxxxx
       
 17507 **  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
       
 17508 **  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
       
 17509 **  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
       
 17510 **
       
 17511 **
       
 17512 ** Notes on UTF-16:  (with wwww+1==uuuuu)
       
 17513 **
       
 17514 **      Word-0               Word-1          Value
       
 17515 **  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
       
 17516 **  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
       
 17517 **
       
 17518 **
       
 17519 ** BOM or Byte Order Mark:
       
 17520 **     0xff 0xfe   little-endian utf-16 follows
       
 17521 **     0xfe 0xff   big-endian utf-16 follows
       
 17522 **
       
 17523 */
       
 17524 /************** Include vdbeInt.h in the middle of utf.c *********************/
       
 17525 /************** Begin file vdbeInt.h *****************************************/
       
 17526 /*
       
 17527 ** 2003 September 6
       
 17528 **
       
 17529 ** The author disclaims copyright to this source code.  In place of
       
 17530 ** a legal notice, here is a blessing:
       
 17531 **
       
 17532 **    May you do good and not evil.
       
 17533 **    May you find forgiveness for yourself and forgive others.
       
 17534 **    May you share freely, never taking more than you give.
       
 17535 **
       
 17536 *************************************************************************
       
 17537 ** This is the header file for information that is private to the
       
 17538 ** VDBE.  This information used to all be at the top of the single
       
 17539 ** source code file "vdbe.c".  When that file became too big (over
       
 17540 ** 6000 lines long) it was split up into several smaller files and
       
 17541 ** this header information was factored out.
       
 17542 **
       
 17543 ** $Id: vdbeInt.h,v 1.174 2009/06/23 14:15:04 drh Exp $
       
 17544 */
       
 17545 #ifndef _VDBEINT_H_
       
 17546 #define _VDBEINT_H_
       
 17547 
       
 17548 /*
       
 17549 ** SQL is translated into a sequence of instructions to be
       
 17550 ** executed by a virtual machine.  Each instruction is an instance
       
 17551 ** of the following structure.
       
 17552 */
       
 17553 typedef struct VdbeOp Op;
       
 17554 
       
 17555 /*
       
 17556 ** Boolean values
       
 17557 */
       
 17558 typedef unsigned char Bool;
       
 17559 
       
 17560 /*
       
 17561 ** A cursor is a pointer into a single BTree within a database file.
       
 17562 ** The cursor can seek to a BTree entry with a particular key, or
       
 17563 ** loop over all entries of the Btree.  You can also insert new BTree
       
 17564 ** entries or retrieve the key or data from the entry that the cursor
       
 17565 ** is currently pointing to.
       
 17566 ** 
       
 17567 ** Every cursor that the virtual machine has open is represented by an
       
 17568 ** instance of the following structure.
       
 17569 **
       
 17570 ** If the VdbeCursor.isTriggerRow flag is set it means that this cursor is
       
 17571 ** really a single row that represents the NEW or OLD pseudo-table of
       
 17572 ** a row trigger.  The data for the row is stored in VdbeCursor.pData and
       
 17573 ** the rowid is in VdbeCursor.iKey.
       
 17574 */
       
 17575 struct VdbeCursor {
       
 17576   BtCursor *pCursor;    /* The cursor structure of the backend */
       
 17577   int iDb;              /* Index of cursor database in db->aDb[] (or -1) */
       
 17578   i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
       
 17579   Bool zeroed;          /* True if zeroed out and ready for reuse */
       
 17580   Bool rowidIsValid;    /* True if lastRowid is valid */
       
 17581   Bool atFirst;         /* True if pointing to first entry */
       
 17582   Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
       
 17583   Bool nullRow;         /* True if pointing to a row with no data */
       
 17584   Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
       
 17585   Bool isTable;         /* True if a table requiring integer keys */
       
 17586   Bool isIndex;         /* True if an index containing keys only - no data */
       
 17587   i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
       
 17588   Btree *pBt;           /* Separate file holding temporary table */
       
 17589   int pseudoTableReg;   /* Register holding pseudotable content. */
       
 17590   KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
       
 17591   int nField;           /* Number of fields in the header */
       
 17592   i64 seqCount;         /* Sequence counter */
       
 17593   sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
       
 17594   const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
       
 17595 
       
 17596   /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or 
       
 17597   ** OP_IsUnique opcode on this cursor. */
       
 17598   int seekResult;
       
 17599 
       
 17600   /* Cached information about the header for the data record that the
       
 17601   ** cursor is currently pointing to.  Only valid if cacheStatus matches
       
 17602   ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
       
 17603   ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
       
 17604   ** the cache is out of date.
       
 17605   **
       
 17606   ** aRow might point to (ephemeral) data for the current row, or it might
       
 17607   ** be NULL.
       
 17608   */
       
 17609   u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
       
 17610   int payloadSize;      /* Total number of bytes in the record */
       
 17611   u32 *aType;           /* Type values for all entries in the record */
       
 17612   u32 *aOffset;         /* Cached offsets to the start of each columns data */
       
 17613   u8 *aRow;             /* Data for the current row, if all on one page */
       
 17614 };
       
 17615 typedef struct VdbeCursor VdbeCursor;
       
 17616 
       
 17617 /*
       
 17618 ** When a sub-program is executed (OP_Program), a structure of this type
       
 17619 ** is allocated to store the current value of the program counter, as
       
 17620 ** well as the current memory cell array and various other frame specific
       
 17621 ** values stored in the Vdbe struct. When the sub-program is finished, 
       
 17622 ** these values are copied back to the Vdbe from the VdbeFrame structure,
       
 17623 ** restoring the state of the VM to as it was before the sub-program
       
 17624 ** began executing.
       
 17625 **
       
 17626 ** Frames are stored in a linked list headed at Vdbe.pParent. Vdbe.pParent
       
 17627 ** is the parent of the current frame, or zero if the current frame
       
 17628 ** is the main Vdbe program.
       
 17629 */
       
 17630 typedef struct VdbeFrame VdbeFrame;
       
 17631 struct VdbeFrame {
       
 17632   Vdbe *v;                /* VM this frame belongs to */
       
 17633   int pc;                 /* Program Counter */
       
 17634   Op *aOp;                /* Program instructions */
       
 17635   int nOp;                /* Size of aOp array */
       
 17636   Mem *aMem;              /* Array of memory cells */
       
 17637   int nMem;               /* Number of entries in aMem */
       
 17638   VdbeCursor **apCsr;     /* Element of Vdbe cursors */
       
 17639   u16 nCursor;            /* Number of entries in apCsr */
       
 17640   void *token;            /* Copy of SubProgram.token */
       
 17641   int nChildMem;          /* Number of memory cells for child frame */
       
 17642   int nChildCsr;          /* Number of cursors for child frame */
       
 17643   i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
       
 17644   int nChange;            /* Statement changes (Vdbe.nChanges)     */
       
 17645   VdbeFrame *pParent;     /* Parent of this frame */
       
 17646 };
       
 17647 
       
 17648 #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
       
 17649 
       
 17650 /*
       
 17651 ** A value for VdbeCursor.cacheValid that means the cache is always invalid.
       
 17652 */
       
 17653 #define CACHE_STALE 0
       
 17654 
       
 17655 /*
       
 17656 ** Internally, the vdbe manipulates nearly all SQL values as Mem
       
 17657 ** structures. Each Mem struct may cache multiple representations (string,
       
 17658 ** integer etc.) of the same value.  A value (and therefore Mem structure)
       
 17659 ** has the following properties:
       
 17660 **
       
 17661 ** Each value has a manifest type. The manifest type of the value stored
       
 17662 ** in a Mem struct is returned by the MemType(Mem*) macro. The type is
       
 17663 ** one of SQLITE_NULL, SQLITE_INTEGER, SQLITE_REAL, SQLITE_TEXT or
       
 17664 ** SQLITE_BLOB.
       
 17665 */
       
 17666 struct Mem {
       
 17667   union {
       
 17668     i64 i;              /* Integer value. */
       
 17669     int nZero;          /* Used when bit MEM_Zero is set in flags */
       
 17670     FuncDef *pDef;      /* Used only when flags==MEM_Agg */
       
 17671     RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
       
 17672     VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
       
 17673   } u;
       
 17674   double r;           /* Real value */
       
 17675   sqlite3 *db;        /* The associated database connection */
       
 17676   char *z;            /* String or BLOB value */
       
 17677   int n;              /* Number of characters in string value, excluding '\0' */
       
 17678   u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
       
 17679   u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
       
 17680   u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
       
 17681   void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
       
 17682   char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
       
 17683 };
       
 17684 
       
 17685 /* One or more of the following flags are set to indicate the validOK
       
 17686 ** representations of the value stored in the Mem struct.
       
 17687 **
       
 17688 ** If the MEM_Null flag is set, then the value is an SQL NULL value.
       
 17689 ** No other flags may be set in this case.
       
 17690 **
       
 17691 ** If the MEM_Str flag is set then Mem.z points at a string representation.
       
 17692 ** Usually this is encoded in the same unicode encoding as the main
       
 17693 ** database (see below for exceptions). If the MEM_Term flag is also
       
 17694 ** set, then the string is nul terminated. The MEM_Int and MEM_Real 
       
 17695 ** flags may coexist with the MEM_Str flag.
       
 17696 **
       
 17697 ** Multiple of these values can appear in Mem.flags.  But only one
       
 17698 ** at a time can appear in Mem.type.
       
 17699 */
       
 17700 #define MEM_Null      0x0001   /* Value is NULL */
       
 17701 #define MEM_Str       0x0002   /* Value is a string */
       
 17702 #define MEM_Int       0x0004   /* Value is an integer */
       
 17703 #define MEM_Real      0x0008   /* Value is a real number */
       
 17704 #define MEM_Blob      0x0010   /* Value is a BLOB */
       
 17705 #define MEM_RowSet    0x0020   /* Value is a RowSet object */
       
 17706 #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
       
 17707 #define MEM_TypeMask  0x00ff   /* Mask of type bits */
       
 17708 
       
 17709 /* Whenever Mem contains a valid string or blob representation, one of
       
 17710 ** the following flags must be set to determine the memory management
       
 17711 ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
       
 17712 ** string is \000 or \u0000 terminated
       
 17713 */
       
 17714 #define MEM_Term      0x0200   /* String rep is nul terminated */
       
 17715 #define MEM_Dyn       0x0400   /* Need to call sqliteFree() on Mem.z */
       
 17716 #define MEM_Static    0x0800   /* Mem.z points to a static string */
       
 17717 #define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
       
 17718 #define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
       
 17719 #define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
       
 17720 
       
 17721 #ifdef SQLITE_OMIT_INCRBLOB
       
 17722   #undef MEM_Zero
       
 17723   #define MEM_Zero 0x0000
       
 17724 #endif
       
 17725 
       
 17726 
       
 17727 /*
       
 17728 ** Clear any existing type flags from a Mem and replace them with f
       
 17729 */
       
 17730 #define MemSetTypeFlag(p, f) \
       
 17731    ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
       
 17732 
       
 17733 
       
 17734 /* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains
       
 17735 ** additional information about auxiliary information bound to arguments
       
 17736 ** of the function.  This is used to implement the sqlite3_get_auxdata()
       
 17737 ** and sqlite3_set_auxdata() APIs.  The "auxdata" is some auxiliary data
       
 17738 ** that can be associated with a constant argument to a function.  This
       
 17739 ** allows functions such as "regexp" to compile their constant regular
       
 17740 ** expression argument once and reused the compiled code for multiple
       
 17741 ** invocations.
       
 17742 */
       
 17743 struct VdbeFunc {
       
 17744   FuncDef *pFunc;               /* The definition of the function */
       
 17745   int nAux;                     /* Number of entries allocated for apAux[] */
       
 17746   struct AuxData {
       
 17747     void *pAux;                   /* Aux data for the i-th argument */
       
 17748     void (*xDelete)(void *);      /* Destructor for the aux data */
       
 17749   } apAux[1];                   /* One slot for each function argument */
       
 17750 };
       
 17751 
       
 17752 /*
       
 17753 ** The "context" argument for a installable function.  A pointer to an
       
 17754 ** instance of this structure is the first argument to the routines used
       
 17755 ** implement the SQL functions.
       
 17756 **
       
 17757 ** There is a typedef for this structure in sqlite.h.  So all routines,
       
 17758 ** even the public interface to SQLite, can use a pointer to this structure.
       
 17759 ** But this file is the only place where the internal details of this
       
 17760 ** structure are known.
       
 17761 **
       
 17762 ** This structure is defined inside of vdbeInt.h because it uses substructures
       
 17763 ** (Mem) which are only defined there.
       
 17764 */
       
 17765 struct sqlite3_context {
       
 17766   FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
       
 17767   VdbeFunc *pVdbeFunc;  /* Auxilary data, if created. */
       
 17768   Mem s;                /* The return value is stored here */
       
 17769   Mem *pMem;            /* Memory cell used to store aggregate context */
       
 17770   int isError;          /* Error code returned by the function. */
       
 17771   CollSeq *pColl;       /* Collating sequence */
       
 17772 };
       
 17773 
       
 17774 /*
       
 17775 ** A Set structure is used for quick testing to see if a value
       
 17776 ** is part of a small set.  Sets are used to implement code like
       
 17777 ** this:
       
 17778 **            x.y IN ('hi','hoo','hum')
       
 17779 */
       
 17780 typedef struct Set Set;
       
 17781 struct Set {
       
 17782   Hash hash;             /* A set is just a hash table */
       
 17783   HashElem *prev;        /* Previously accessed hash elemen */
       
 17784 };
       
 17785 
       
 17786 /*
       
 17787 ** An instance of the virtual machine.  This structure contains the complete
       
 17788 ** state of the virtual machine.
       
 17789 **
       
 17790 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_compile()
       
 17791 ** is really a pointer to an instance of this structure.
       
 17792 **
       
 17793 ** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
       
 17794 ** any virtual table method invocations made by the vdbe program. It is
       
 17795 ** set to 2 for xDestroy method calls and 1 for all other methods. This
       
 17796 ** variable is used for two purposes: to allow xDestroy methods to execute
       
 17797 ** "DROP TABLE" statements and to prevent some nasty side effects of
       
 17798 ** malloc failure when SQLite is invoked recursively by a virtual table 
       
 17799 ** method function.
       
 17800 */
       
 17801 struct Vdbe {
       
 17802   sqlite3 *db;            /* The database connection that owns this statement */
       
 17803   Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
       
 17804   int nOp;                /* Number of instructions in the program */
       
 17805   int nOpAlloc;           /* Number of slots allocated for aOp[] */
       
 17806   Op *aOp;                /* Space to hold the virtual machine's program */
       
 17807   int nLabel;             /* Number of labels used */
       
 17808   int nLabelAlloc;        /* Number of slots allocated in aLabel[] */
       
 17809   int *aLabel;            /* Space to hold the labels */
       
 17810   Mem **apArg;            /* Arguments to currently executing user function */
       
 17811   Mem *aColName;          /* Column names to return */
       
 17812   Mem *pResultSet;        /* Pointer to an array of results */
       
 17813   u16 nResColumn;         /* Number of columns in one row of the result set */
       
 17814   u16 nCursor;            /* Number of slots in apCsr[] */
       
 17815   VdbeCursor **apCsr;     /* One element of this array for each open cursor */
       
 17816   u8 errorAction;         /* Recovery action to do in case of an error */
       
 17817   u8 okVar;               /* True if azVar[] has been initialized */
       
 17818   u16 nVar;               /* Number of entries in aVar[] */
       
 17819   Mem *aVar;              /* Values for the OP_Variable opcode. */
       
 17820   char **azVar;           /* Name of variables */
       
 17821   u32 magic;              /* Magic number for sanity checking */
       
 17822   int nMem;               /* Number of memory locations currently allocated */
       
 17823   Mem *aMem;              /* The memory locations */
       
 17824   u32 cacheCtr;           /* VdbeCursor row cache generation counter */
       
 17825   int pc;                 /* The program counter */
       
 17826   int rc;                 /* Value to return */
       
 17827   char *zErrMsg;          /* Error message written here */
       
 17828   u8 explain;             /* True if EXPLAIN present on SQL command */
       
 17829   u8 changeCntOn;         /* True to update the change-counter */
       
 17830   u8 expired;             /* True if the VM needs to be recompiled */
       
 17831   u8 minWriteFileFormat;  /* Minimum file format for writable database files */
       
 17832   u8 inVtabMethod;        /* See comments above */
       
 17833   u8 usesStmtJournal;     /* True if uses a statement journal */
       
 17834   u8 readOnly;            /* True for read-only statements */
       
 17835   u8 isPrepareV2;         /* True if prepared with prepare_v2() */
       
 17836   int nChange;            /* Number of db changes made since last reset */
       
 17837   int btreeMask;          /* Bitmask of db->aDb[] entries referenced */
       
 17838   i64 startTime;          /* Time when query started - used for profiling */
       
 17839   BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
       
 17840   int aCounter[2];        /* Counters used by sqlite3_stmt_status() */
       
 17841   char *zSql;             /* Text of the SQL statement that generated this */
       
 17842   void *pFree;            /* Free this when deleting the vdbe */
       
 17843   i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
       
 17844   i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
       
 17845   int iStatement;         /* Statement number (or 0 if has not opened stmt) */
       
 17846 #ifdef SQLITE_DEBUG
       
 17847   FILE *trace;            /* Write an execution trace here, if not NULL */
       
 17848 #endif
       
 17849   VdbeFrame *pFrame;      /* Parent frame */
       
 17850   int nFrame;             /* Number of frames in pFrame list */
       
 17851 };
       
 17852 
       
 17853 /*
       
 17854 ** The following are allowed values for Vdbe.magic
       
 17855 */
       
 17856 #define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
       
 17857 #define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
       
 17858 #define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
       
 17859 #define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
       
 17860 
       
 17861 /*
       
 17862 ** Function prototypes
       
 17863 */
       
 17864 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
       
 17865 void sqliteVdbePopStack(Vdbe*,int);
       
 17866 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
       
 17867 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
       
 17868 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
       
 17869 #endif
       
 17870 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
       
 17871 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
       
 17872 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
       
 17873 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
       
 17874 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
       
 17875 
       
 17876 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
       
 17877 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
       
 17878 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
       
 17879 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
       
 17880 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
       
 17881 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
       
 17882 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
       
 17883 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
       
 17884 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
       
 17885 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
       
 17886 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
       
 17887 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
       
 17888 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
       
 17889 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
       
 17890 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
       
 17891 SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double);
       
 17892 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
       
 17893 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
       
 17894 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
       
 17895 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
       
 17896 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, int);
       
 17897 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
       
 17898 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
       
 17899 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
       
 17900 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
       
 17901 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
       
 17902 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
       
 17903 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
       
 17904 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
       
 17905 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
       
 17906 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
       
 17907 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
       
 17908 SQLITE_PRIVATE int sqlite3VdbeOpcodeHasProperty(int, int);
       
 17909 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
       
 17910 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
       
 17911 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
       
 17912 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
       
 17913 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
       
 17914 SQLITE_PRIVATE int sqlite3VdbeReleaseBuffers(Vdbe *p);
       
 17915 #endif
       
 17916 
       
 17917 #ifndef SQLITE_OMIT_FOREIGN_KEY
       
 17918 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
       
 17919 #else
       
 17920 # define sqlite3VdbeCheckFk(p,i) 0
       
 17921 #endif
       
 17922 
       
 17923 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 17924 SQLITE_PRIVATE void sqlite3VdbeMutexArrayEnter(Vdbe *p);
       
 17925 #else
       
 17926 # define sqlite3VdbeMutexArrayEnter(p)
       
 17927 #endif
       
 17928 
       
 17929 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
       
 17930 #ifdef SQLITE_DEBUG
       
 17931 SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
       
 17932 SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
       
 17933 #endif
       
 17934 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
       
 17935 
       
 17936 #ifndef SQLITE_OMIT_INCRBLOB
       
 17937 SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
       
 17938 #else
       
 17939   #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
       
 17940 #endif
       
 17941 
       
 17942 #endif /* !defined(_VDBEINT_H_) */
       
 17943 
       
 17944 /************** End of vdbeInt.h *********************************************/
       
 17945 /************** Continuing where we left off in utf.c ************************/
       
 17946 
       
 17947 #ifndef SQLITE_AMALGAMATION
       
 17948 /*
       
 17949 ** The following constant value is used by the SQLITE_BIGENDIAN and
       
 17950 ** SQLITE_LITTLEENDIAN macros.
       
 17951 */
       
 17952 SQLITE_PRIVATE const int sqlite3one = 1;
       
 17953 #endif /* SQLITE_AMALGAMATION */
       
 17954 
       
 17955 /*
       
 17956 ** This lookup table is used to help decode the first byte of
       
 17957 ** a multi-byte UTF8 character.
       
 17958 */
       
 17959 static const unsigned char sqlite3Utf8Trans1[] = {
       
 17960   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
       
 17961   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
       
 17962   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
       
 17963   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
       
 17964   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
       
 17965   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
       
 17966   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
       
 17967   0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
       
 17968 };
       
 17969 
       
 17970 
       
 17971 #define WRITE_UTF8(zOut, c) {                          \
       
 17972   if( c<0x00080 ){                                     \
       
 17973     *zOut++ = (u8)(c&0xFF);                            \
       
 17974   }                                                    \
       
 17975   else if( c<0x00800 ){                                \
       
 17976     *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
       
 17977     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
       
 17978   }                                                    \
       
 17979   else if( c<0x10000 ){                                \
       
 17980     *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
       
 17981     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
       
 17982     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
       
 17983   }else{                                               \
       
 17984     *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
       
 17985     *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
       
 17986     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
       
 17987     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
       
 17988   }                                                    \
       
 17989 }
       
 17990 
       
 17991 #define WRITE_UTF16LE(zOut, c) {                                    \
       
 17992   if( c<=0xFFFF ){                                                  \
       
 17993     *zOut++ = (u8)(c&0x00FF);                                       \
       
 17994     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
       
 17995   }else{                                                            \
       
 17996     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
       
 17997     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
       
 17998     *zOut++ = (u8)(c&0x00FF);                                       \
       
 17999     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
       
 18000   }                                                                 \
       
 18001 }
       
 18002 
       
 18003 #define WRITE_UTF16BE(zOut, c) {                                    \
       
 18004   if( c<=0xFFFF ){                                                  \
       
 18005     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
       
 18006     *zOut++ = (u8)(c&0x00FF);                                       \
       
 18007   }else{                                                            \
       
 18008     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
       
 18009     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
       
 18010     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
       
 18011     *zOut++ = (u8)(c&0x00FF);                                       \
       
 18012   }                                                                 \
       
 18013 }
       
 18014 
       
 18015 #define READ_UTF16LE(zIn, c){                                         \
       
 18016   c = (*zIn++);                                                       \
       
 18017   c += ((*zIn++)<<8);                                                 \
       
 18018   if( c>=0xD800 && c<0xE000 ){                                        \
       
 18019     int c2 = (*zIn++);                                                \
       
 18020     c2 += ((*zIn++)<<8);                                              \
       
 18021     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
       
 18022   }                                                                   \
       
 18023 }
       
 18024 
       
 18025 #define READ_UTF16BE(zIn, c){                                         \
       
 18026   c = ((*zIn++)<<8);                                                  \
       
 18027   c += (*zIn++);                                                      \
       
 18028   if( c>=0xD800 && c<0xE000 ){                                        \
       
 18029     int c2 = ((*zIn++)<<8);                                           \
       
 18030     c2 += (*zIn++);                                                   \
       
 18031     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
       
 18032   }                                                                   \
       
 18033 }
       
 18034 
       
 18035 /*
       
 18036 ** Translate a single UTF-8 character.  Return the unicode value.
       
 18037 **
       
 18038 ** During translation, assume that the byte that zTerm points
       
 18039 ** is a 0x00.
       
 18040 **
       
 18041 ** Write a pointer to the next unread byte back into *pzNext.
       
 18042 **
       
 18043 ** Notes On Invalid UTF-8:
       
 18044 **
       
 18045 **  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
       
 18046 **     be encoded as a multi-byte character.  Any multi-byte character that
       
 18047 **     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
       
 18048 **
       
 18049 **  *  This routine never allows a UTF16 surrogate value to be encoded.
       
 18050 **     If a multi-byte character attempts to encode a value between
       
 18051 **     0xd800 and 0xe000 then it is rendered as 0xfffd.
       
 18052 **
       
 18053 **  *  Bytes in the range of 0x80 through 0xbf which occur as the first
       
 18054 **     byte of a character are interpreted as single-byte characters
       
 18055 **     and rendered as themselves even though they are technically
       
 18056 **     invalid characters.
       
 18057 **
       
 18058 **  *  This routine accepts an infinite number of different UTF8 encodings
       
 18059 **     for unicode values 0x80 and greater.  It do not change over-length
       
 18060 **     encodings to 0xfffd as some systems recommend.
       
 18061 */
       
 18062 #define READ_UTF8(zIn, zTerm, c)                           \
       
 18063   c = *(zIn++);                                            \
       
 18064   if( c>=0xc0 ){                                           \
       
 18065     c = sqlite3Utf8Trans1[c-0xc0];                         \
       
 18066     while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
       
 18067       c = (c<<6) + (0x3f & *(zIn++));                      \
       
 18068     }                                                      \
       
 18069     if( c<0x80                                             \
       
 18070         || (c&0xFFFFF800)==0xD800                          \
       
 18071         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
       
 18072   }
       
 18073 SQLITE_PRIVATE int sqlite3Utf8Read(
       
 18074   const unsigned char *zIn,       /* First byte of UTF-8 character */
       
 18075   const unsigned char **pzNext    /* Write first byte past UTF-8 char here */
       
 18076 ){
       
 18077   int c;
       
 18078 
       
 18079   /* Same as READ_UTF8() above but without the zTerm parameter.
       
 18080   ** For this routine, we assume the UTF8 string is always zero-terminated.
       
 18081   */
       
 18082   c = *(zIn++);
       
 18083   if( c>=0xc0 ){
       
 18084     c = sqlite3Utf8Trans1[c-0xc0];
       
 18085     while( (*zIn & 0xc0)==0x80 ){
       
 18086       c = (c<<6) + (0x3f & *(zIn++));
       
 18087     }
       
 18088     if( c<0x80
       
 18089         || (c&0xFFFFF800)==0xD800
       
 18090         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
       
 18091   }
       
 18092   *pzNext = zIn;
       
 18093   return c;
       
 18094 }
       
 18095 
       
 18096 
       
 18097 
       
 18098 
       
 18099 /*
       
 18100 ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
       
 18101 ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
       
 18102 */ 
       
 18103 /* #define TRANSLATE_TRACE 1 */
       
 18104 
       
 18105 #ifndef SQLITE_OMIT_UTF16
       
 18106 /*
       
 18107 ** This routine transforms the internal text encoding used by pMem to
       
 18108 ** desiredEnc. It is an error if the string is already of the desired
       
 18109 ** encoding, or if *pMem does not contain a string value.
       
 18110 */
       
 18111 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
       
 18112   int len;                    /* Maximum length of output string in bytes */
       
 18113   unsigned char *zOut;                  /* Output buffer */
       
 18114   unsigned char *zIn;                   /* Input iterator */
       
 18115   unsigned char *zTerm;                 /* End of input */
       
 18116   unsigned char *z;                     /* Output iterator */
       
 18117   unsigned int c;
       
 18118 
       
 18119   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
 18120   assert( pMem->flags&MEM_Str );
       
 18121   assert( pMem->enc!=desiredEnc );
       
 18122   assert( pMem->enc!=0 );
       
 18123   assert( pMem->n>=0 );
       
 18124 
       
 18125 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
       
 18126   {
       
 18127     char zBuf[100];
       
 18128     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
       
 18129     fprintf(stderr, "INPUT:  %s\n", zBuf);
       
 18130   }
       
 18131 #endif
       
 18132 
       
 18133   /* If the translation is between UTF-16 little and big endian, then 
       
 18134   ** all that is required is to swap the byte order. This case is handled
       
 18135   ** differently from the others.
       
 18136   */
       
 18137   if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
       
 18138     u8 temp;
       
 18139     int rc;
       
 18140     rc = sqlite3VdbeMemMakeWriteable(pMem);
       
 18141     if( rc!=SQLITE_OK ){
       
 18142       assert( rc==SQLITE_NOMEM );
       
 18143       return SQLITE_NOMEM;
       
 18144     }
       
 18145     zIn = (u8*)pMem->z;
       
 18146     zTerm = &zIn[pMem->n&~1];
       
 18147     while( zIn<zTerm ){
       
 18148       temp = *zIn;
       
 18149       *zIn = *(zIn+1);
       
 18150       zIn++;
       
 18151       *zIn++ = temp;
       
 18152     }
       
 18153     pMem->enc = desiredEnc;
       
 18154     goto translate_out;
       
 18155   }
       
 18156 
       
 18157   /* Set len to the maximum number of bytes required in the output buffer. */
       
 18158   if( desiredEnc==SQLITE_UTF8 ){
       
 18159     /* When converting from UTF-16, the maximum growth results from
       
 18160     ** translating a 2-byte character to a 4-byte UTF-8 character.
       
 18161     ** A single byte is required for the output string
       
 18162     ** nul-terminator.
       
 18163     */
       
 18164     pMem->n &= ~1;
       
 18165     len = pMem->n * 2 + 1;
       
 18166   }else{
       
 18167     /* When converting from UTF-8 to UTF-16 the maximum growth is caused
       
 18168     ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
       
 18169     ** character. Two bytes are required in the output buffer for the
       
 18170     ** nul-terminator.
       
 18171     */
       
 18172     len = pMem->n * 2 + 2;
       
 18173   }
       
 18174 
       
 18175   /* Set zIn to point at the start of the input buffer and zTerm to point 1
       
 18176   ** byte past the end.
       
 18177   **
       
 18178   ** Variable zOut is set to point at the output buffer, space obtained
       
 18179   ** from sqlite3_malloc().
       
 18180   */
       
 18181   zIn = (u8*)pMem->z;
       
 18182   zTerm = &zIn[pMem->n];
       
 18183   zOut = sqlite3DbMallocRaw(pMem->db, len);
       
 18184   if( !zOut ){
       
 18185     return SQLITE_NOMEM;
       
 18186   }
       
 18187   z = zOut;
       
 18188 
       
 18189   if( pMem->enc==SQLITE_UTF8 ){
       
 18190     if( desiredEnc==SQLITE_UTF16LE ){
       
 18191       /* UTF-8 -> UTF-16 Little-endian */
       
 18192       while( zIn<zTerm ){
       
 18193         /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
       
 18194         READ_UTF8(zIn, zTerm, c);
       
 18195         WRITE_UTF16LE(z, c);
       
 18196       }
       
 18197     }else{
       
 18198       assert( desiredEnc==SQLITE_UTF16BE );
       
 18199       /* UTF-8 -> UTF-16 Big-endian */
       
 18200       while( zIn<zTerm ){
       
 18201         /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
       
 18202         READ_UTF8(zIn, zTerm, c);
       
 18203         WRITE_UTF16BE(z, c);
       
 18204       }
       
 18205     }
       
 18206     pMem->n = (int)(z - zOut);
       
 18207     *z++ = 0;
       
 18208   }else{
       
 18209     assert( desiredEnc==SQLITE_UTF8 );
       
 18210     if( pMem->enc==SQLITE_UTF16LE ){
       
 18211       /* UTF-16 Little-endian -> UTF-8 */
       
 18212       while( zIn<zTerm ){
       
 18213         READ_UTF16LE(zIn, c); 
       
 18214         WRITE_UTF8(z, c);
       
 18215       }
       
 18216     }else{
       
 18217       /* UTF-16 Big-endian -> UTF-8 */
       
 18218       while( zIn<zTerm ){
       
 18219         READ_UTF16BE(zIn, c); 
       
 18220         WRITE_UTF8(z, c);
       
 18221       }
       
 18222     }
       
 18223     pMem->n = (int)(z - zOut);
       
 18224   }
       
 18225   *z = 0;
       
 18226   assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
       
 18227 
       
 18228   sqlite3VdbeMemRelease(pMem);
       
 18229   pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
       
 18230   pMem->enc = desiredEnc;
       
 18231   pMem->flags |= (MEM_Term|MEM_Dyn);
       
 18232   pMem->z = (char*)zOut;
       
 18233   pMem->zMalloc = pMem->z;
       
 18234 
       
 18235 translate_out:
       
 18236 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
       
 18237   {
       
 18238     char zBuf[100];
       
 18239     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
       
 18240     fprintf(stderr, "OUTPUT: %s\n", zBuf);
       
 18241   }
       
 18242 #endif
       
 18243   return SQLITE_OK;
       
 18244 }
       
 18245 
       
 18246 /*
       
 18247 ** This routine checks for a byte-order mark at the beginning of the 
       
 18248 ** UTF-16 string stored in *pMem. If one is present, it is removed and
       
 18249 ** the encoding of the Mem adjusted. This routine does not do any
       
 18250 ** byte-swapping, it just sets Mem.enc appropriately.
       
 18251 **
       
 18252 ** The allocation (static, dynamic etc.) and encoding of the Mem may be
       
 18253 ** changed by this function.
       
 18254 */
       
 18255 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
       
 18256   int rc = SQLITE_OK;
       
 18257   u8 bom = 0;
       
 18258 
       
 18259   assert( pMem->n>=0 );
       
 18260   if( pMem->n>1 ){
       
 18261     u8 b1 = *(u8 *)pMem->z;
       
 18262     u8 b2 = *(((u8 *)pMem->z) + 1);
       
 18263     if( b1==0xFE && b2==0xFF ){
       
 18264       bom = SQLITE_UTF16BE;
       
 18265     }
       
 18266     if( b1==0xFF && b2==0xFE ){
       
 18267       bom = SQLITE_UTF16LE;
       
 18268     }
       
 18269   }
       
 18270   
       
 18271   if( bom ){
       
 18272     rc = sqlite3VdbeMemMakeWriteable(pMem);
       
 18273     if( rc==SQLITE_OK ){
       
 18274       pMem->n -= 2;
       
 18275       memmove(pMem->z, &pMem->z[2], pMem->n);
       
 18276       pMem->z[pMem->n] = '\0';
       
 18277       pMem->z[pMem->n+1] = '\0';
       
 18278       pMem->flags |= MEM_Term;
       
 18279       pMem->enc = bom;
       
 18280     }
       
 18281   }
       
 18282   return rc;
       
 18283 }
       
 18284 #endif /* SQLITE_OMIT_UTF16 */
       
 18285 
       
 18286 /*
       
 18287 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
       
 18288 ** return the number of unicode characters in pZ up to (but not including)
       
 18289 ** the first 0x00 byte. If nByte is not less than zero, return the
       
 18290 ** number of unicode characters in the first nByte of pZ (or up to 
       
 18291 ** the first 0x00, whichever comes first).
       
 18292 */
       
 18293 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
       
 18294   int r = 0;
       
 18295   const u8 *z = (const u8*)zIn;
       
 18296   const u8 *zTerm;
       
 18297   if( nByte>=0 ){
       
 18298     zTerm = &z[nByte];
       
 18299   }else{
       
 18300     zTerm = (const u8*)(-1);
       
 18301   }
       
 18302   assert( z<=zTerm );
       
 18303   while( *z!=0 && z<zTerm ){
       
 18304     SQLITE_SKIP_UTF8(z);
       
 18305     r++;
       
 18306   }
       
 18307   return r;
       
 18308 }
       
 18309 
       
 18310 /* This test function is not currently used by the automated test-suite. 
       
 18311 ** Hence it is only available in debug builds.
       
 18312 */
       
 18313 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
       
 18314 /*
       
 18315 ** Translate UTF-8 to UTF-8.
       
 18316 **
       
 18317 ** This has the effect of making sure that the string is well-formed
       
 18318 ** UTF-8.  Miscoded characters are removed.
       
 18319 **
       
 18320 ** The translation is done in-place (since it is impossible for the
       
 18321 ** correct UTF-8 encoding to be longer than a malformed encoding).
       
 18322 */
       
 18323 SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
       
 18324   unsigned char *zOut = zIn;
       
 18325   unsigned char *zStart = zIn;
       
 18326   u32 c;
       
 18327 
       
 18328   while( zIn[0] ){
       
 18329     c = sqlite3Utf8Read(zIn, (const u8**)&zIn);
       
 18330     if( c!=0xfffd ){
       
 18331       WRITE_UTF8(zOut, c);
       
 18332     }
       
 18333   }
       
 18334   *zOut = 0;
       
 18335   return (int)(zOut - zStart);
       
 18336 }
       
 18337 #endif
       
 18338 
       
 18339 #ifndef SQLITE_OMIT_UTF16
       
 18340 /*
       
 18341 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
       
 18342 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
       
 18343 ** be freed by the calling function.
       
 18344 **
       
 18345 ** NULL is returned if there is an allocation error.
       
 18346 */
       
 18347 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte){
       
 18348   Mem m;
       
 18349   memset(&m, 0, sizeof(m));
       
 18350   m.db = db;
       
 18351   sqlite3VdbeMemSetStr(&m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC);
       
 18352   sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
       
 18353   if( db->mallocFailed ){
       
 18354     sqlite3VdbeMemRelease(&m);
       
 18355     m.z = 0;
       
 18356   }
       
 18357   assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
       
 18358   assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
       
 18359   return (m.flags & MEM_Dyn)!=0 ? m.z : sqlite3DbStrDup(db, m.z);
       
 18360 }
       
 18361 
       
 18362 /*
       
 18363 ** Convert a UTF-8 string to the UTF-16 encoding specified by parameter
       
 18364 ** enc. A pointer to the new string is returned, and the value of *pnOut
       
 18365 ** is set to the length of the returned string in bytes. The call should
       
 18366 ** arrange to call sqlite3DbFree() on the returned pointer when it is
       
 18367 ** no longer required.
       
 18368 ** 
       
 18369 ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
       
 18370 ** flag set.
       
 18371 */
       
 18372 #ifdef SQLITE_ENABLE_STAT2
       
 18373 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
       
 18374   Mem m;
       
 18375   memset(&m, 0, sizeof(m));
       
 18376   m.db = db;
       
 18377   sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
       
 18378   if( sqlite3VdbeMemTranslate(&m, enc) ){
       
 18379     assert( db->mallocFailed );
       
 18380     return 0;
       
 18381   }
       
 18382   assert( m.z==m.zMalloc );
       
 18383   *pnOut = m.n;
       
 18384   return m.z;
       
 18385 }
       
 18386 #endif
       
 18387 
       
 18388 /*
       
 18389 ** pZ is a UTF-16 encoded unicode string at least nChar characters long.
       
 18390 ** Return the number of bytes in the first nChar unicode characters
       
 18391 ** in pZ.  nChar must be non-negative.
       
 18392 */
       
 18393 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
       
 18394   int c;
       
 18395   unsigned char const *z = zIn;
       
 18396   int n = 0;
       
 18397   if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
       
 18398     /* Using an "if (SQLITE_UTF16NATIVE==SQLITE_UTF16BE)" construct here
       
 18399     ** and in other parts of this file means that at one branch will
       
 18400     ** not be covered by coverage testing on any single host. But coverage
       
 18401     ** will be complete if the tests are run on both a little-endian and 
       
 18402     ** big-endian host. Because both the UTF16NATIVE and SQLITE_UTF16BE
       
 18403     ** macros are constant at compile time the compiler can determine
       
 18404     ** which branch will be followed. It is therefore assumed that no runtime
       
 18405     ** penalty is paid for this "if" statement.
       
 18406     */
       
 18407     while( n<nChar ){
       
 18408       READ_UTF16BE(z, c);
       
 18409       n++;
       
 18410     }
       
 18411   }else{
       
 18412     while( n<nChar ){
       
 18413       READ_UTF16LE(z, c);
       
 18414       n++;
       
 18415     }
       
 18416   }
       
 18417   return (int)(z-(unsigned char const *)zIn);
       
 18418 }
       
 18419 
       
 18420 #if defined(SQLITE_TEST)
       
 18421 /*
       
 18422 ** This routine is called from the TCL test function "translate_selftest".
       
 18423 ** It checks that the primitives for serializing and deserializing
       
 18424 ** characters in each encoding are inverses of each other.
       
 18425 */
       
 18426 SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
       
 18427   unsigned int i, t;
       
 18428   unsigned char zBuf[20];
       
 18429   unsigned char *z;
       
 18430   int n;
       
 18431   unsigned int c;
       
 18432 
       
 18433   for(i=0; i<0x00110000; i++){
       
 18434     z = zBuf;
       
 18435     WRITE_UTF8(z, i);
       
 18436     n = (int)(z-zBuf);
       
 18437     assert( n>0 && n<=4 );
       
 18438     z[0] = 0;
       
 18439     z = zBuf;
       
 18440     c = sqlite3Utf8Read(z, (const u8**)&z);
       
 18441     t = i;
       
 18442     if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
       
 18443     if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
       
 18444     assert( c==t );
       
 18445     assert( (z-zBuf)==n );
       
 18446   }
       
 18447   for(i=0; i<0x00110000; i++){
       
 18448     if( i>=0xD800 && i<0xE000 ) continue;
       
 18449     z = zBuf;
       
 18450     WRITE_UTF16LE(z, i);
       
 18451     n = (int)(z-zBuf);
       
 18452     assert( n>0 && n<=4 );
       
 18453     z[0] = 0;
       
 18454     z = zBuf;
       
 18455     READ_UTF16LE(z, c);
       
 18456     assert( c==i );
       
 18457     assert( (z-zBuf)==n );
       
 18458   }
       
 18459   for(i=0; i<0x00110000; i++){
       
 18460     if( i>=0xD800 && i<0xE000 ) continue;
       
 18461     z = zBuf;
       
 18462     WRITE_UTF16BE(z, i);
       
 18463     n = (int)(z-zBuf);
       
 18464     assert( n>0 && n<=4 );
       
 18465     z[0] = 0;
       
 18466     z = zBuf;
       
 18467     READ_UTF16BE(z, c);
       
 18468     assert( c==i );
       
 18469     assert( (z-zBuf)==n );
       
 18470   }
       
 18471 }
       
 18472 #endif /* SQLITE_TEST */
       
 18473 #endif /* SQLITE_OMIT_UTF16 */
       
 18474 
       
 18475 /************** End of utf.c *************************************************/
       
 18476 /************** Begin file util.c ********************************************/
       
 18477 /*
       
 18478 ** 2001 September 15
       
 18479 **
       
 18480 ** The author disclaims copyright to this source code.  In place of
       
 18481 ** a legal notice, here is a blessing:
       
 18482 **
       
 18483 **    May you do good and not evil.
       
 18484 **    May you find forgiveness for yourself and forgive others.
       
 18485 **    May you share freely, never taking more than you give.
       
 18486 **
       
 18487 *************************************************************************
       
 18488 ** Utility functions used throughout sqlite.
       
 18489 **
       
 18490 ** This file contains functions for allocating memory, comparing
       
 18491 ** strings, and stuff like that.
       
 18492 **
       
 18493 */
       
 18494 #ifdef SQLITE_HAVE_ISNAN
       
 18495 # include <math.h>
       
 18496 #endif
       
 18497 
       
 18498 /*
       
 18499 ** Routine needed to support the testcase() macro.
       
 18500 */
       
 18501 #ifdef SQLITE_COVERAGE_TEST
       
 18502 SQLITE_PRIVATE void sqlite3Coverage(int x){
       
 18503   static int dummy = 0;
       
 18504   dummy += x;
       
 18505 }
       
 18506 #endif
       
 18507 
       
 18508 /*
       
 18509 ** Return true if the floating point value is Not a Number (NaN).
       
 18510 **
       
 18511 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
       
 18512 ** Otherwise, we have our own implementation that works on most systems.
       
 18513 */
       
 18514 SQLITE_PRIVATE int sqlite3IsNaN(double x){
       
 18515   int rc;   /* The value return */
       
 18516 #if !defined(SQLITE_HAVE_ISNAN)
       
 18517   /*
       
 18518   ** Systems that support the isnan() library function should probably
       
 18519   ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
       
 18520   ** found that many systems do not have a working isnan() function so
       
 18521   ** this implementation is provided as an alternative.
       
 18522   **
       
 18523   ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
       
 18524   ** On the other hand, the use of -ffast-math comes with the following
       
 18525   ** warning:
       
 18526   **
       
 18527   **      This option [-ffast-math] should never be turned on by any
       
 18528   **      -O option since it can result in incorrect output for programs
       
 18529   **      which depend on an exact implementation of IEEE or ISO 
       
 18530   **      rules/specifications for math functions.
       
 18531   **
       
 18532   ** Under MSVC, this NaN test may fail if compiled with a floating-
       
 18533   ** point precision mode other than /fp:precise.  From the MSDN 
       
 18534   ** documentation:
       
 18535   **
       
 18536   **      The compiler [with /fp:precise] will properly handle comparisons 
       
 18537   **      involving NaN. For example, x != x evaluates to true if x is NaN 
       
 18538   **      ...
       
 18539   */
       
 18540 #ifdef __FAST_MATH__
       
 18541 # error SQLite will not work correctly with the -ffast-math option of GCC.
       
 18542 #endif
       
 18543   volatile double y = x;
       
 18544   volatile double z = y;
       
 18545   rc = (y!=z);
       
 18546 #else  /* if defined(SQLITE_HAVE_ISNAN) */
       
 18547   rc = isnan(x);
       
 18548 #endif /* SQLITE_HAVE_ISNAN */
       
 18549   testcase( rc );
       
 18550   return rc;
       
 18551 }
       
 18552 
       
 18553 /*
       
 18554 ** Compute a string length that is limited to what can be stored in
       
 18555 ** lower 30 bits of a 32-bit signed integer.
       
 18556 **
       
 18557 ** The value returned will never be negative.  Nor will it ever be greater
       
 18558 ** than the actual length of the string.  For very long strings (greater
       
 18559 ** than 1GiB) the value returned might be less than the true string length.
       
 18560 */
       
 18561 SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
       
 18562   const char *z2 = z;
       
 18563   if( z==0 ) return 0;
       
 18564   while( *z2 ){ z2++; }
       
 18565   return 0x3fffffff & (int)(z2 - z);
       
 18566 }
       
 18567 
       
 18568 /*
       
 18569 ** Set the most recent error code and error string for the sqlite
       
 18570 ** handle "db". The error code is set to "err_code".
       
 18571 **
       
 18572 ** If it is not NULL, string zFormat specifies the format of the
       
 18573 ** error string in the style of the printf functions: The following
       
 18574 ** format characters are allowed:
       
 18575 **
       
 18576 **      %s      Insert a string
       
 18577 **      %z      A string that should be freed after use
       
 18578 **      %d      Insert an integer
       
 18579 **      %T      Insert a token
       
 18580 **      %S      Insert the first element of a SrcList
       
 18581 **
       
 18582 ** zFormat and any string tokens that follow it are assumed to be
       
 18583 ** encoded in UTF-8.
       
 18584 **
       
 18585 ** To clear the most recent error for sqlite handle "db", sqlite3Error
       
 18586 ** should be called with err_code set to SQLITE_OK and zFormat set
       
 18587 ** to NULL.
       
 18588 */
       
 18589 SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
       
 18590   if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
       
 18591     db->errCode = err_code;
       
 18592     if( zFormat ){
       
 18593       char *z;
       
 18594       va_list ap;
       
 18595       va_start(ap, zFormat);
       
 18596       z = sqlite3VMPrintf(db, zFormat, ap);
       
 18597       va_end(ap);
       
 18598       sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
       
 18599     }else{
       
 18600       sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
       
 18601     }
       
 18602   }
       
 18603 }
       
 18604 
       
 18605 /*
       
 18606 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
       
 18607 ** The following formatting characters are allowed:
       
 18608 **
       
 18609 **      %s      Insert a string
       
 18610 **      %z      A string that should be freed after use
       
 18611 **      %d      Insert an integer
       
 18612 **      %T      Insert a token
       
 18613 **      %S      Insert the first element of a SrcList
       
 18614 **
       
 18615 ** This function should be used to report any error that occurs whilst
       
 18616 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
       
 18617 ** last thing the sqlite3_prepare() function does is copy the error
       
 18618 ** stored by this function into the database handle using sqlite3Error().
       
 18619 ** Function sqlite3Error() should be used during statement execution
       
 18620 ** (sqlite3_step() etc.).
       
 18621 */
       
 18622 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
       
 18623   va_list ap;
       
 18624   sqlite3 *db = pParse->db;
       
 18625   pParse->nErr++;
       
 18626   sqlite3DbFree(db, pParse->zErrMsg);
       
 18627   va_start(ap, zFormat);
       
 18628   pParse->zErrMsg = sqlite3VMPrintf(db, zFormat, ap);
       
 18629   va_end(ap);
       
 18630   pParse->rc = SQLITE_ERROR;
       
 18631 }
       
 18632 
       
 18633 /*
       
 18634 ** Clear the error message in pParse, if any
       
 18635 */
       
 18636 SQLITE_PRIVATE void sqlite3ErrorClear(Parse *pParse){
       
 18637   sqlite3DbFree(pParse->db, pParse->zErrMsg);
       
 18638   pParse->zErrMsg = 0;
       
 18639   pParse->nErr = 0;
       
 18640 }
       
 18641 
       
 18642 /*
       
 18643 ** Convert an SQL-style quoted string into a normal string by removing
       
 18644 ** the quote characters.  The conversion is done in-place.  If the
       
 18645 ** input does not begin with a quote character, then this routine
       
 18646 ** is a no-op.
       
 18647 **
       
 18648 ** The input string must be zero-terminated.  A new zero-terminator
       
 18649 ** is added to the dequoted string.
       
 18650 **
       
 18651 ** The return value is -1 if no dequoting occurs or the length of the
       
 18652 ** dequoted string, exclusive of the zero terminator, if dequoting does
       
 18653 ** occur.
       
 18654 **
       
 18655 ** 2002-Feb-14: This routine is extended to remove MS-Access style
       
 18656 ** brackets from around identifers.  For example:  "[a-b-c]" becomes
       
 18657 ** "a-b-c".
       
 18658 */
       
 18659 SQLITE_PRIVATE int sqlite3Dequote(char *z){
       
 18660   char quote;
       
 18661   int i, j;
       
 18662   if( z==0 ) return -1;
       
 18663   quote = z[0];
       
 18664   switch( quote ){
       
 18665     case '\'':  break;
       
 18666     case '"':   break;
       
 18667     case '`':   break;                /* For MySQL compatibility */
       
 18668     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
       
 18669     default:    return -1;
       
 18670   }
       
 18671   for(i=1, j=0; ALWAYS(z[i]); i++){
       
 18672     if( z[i]==quote ){
       
 18673       if( z[i+1]==quote ){
       
 18674         z[j++] = quote;
       
 18675         i++;
       
 18676       }else{
       
 18677         break;
       
 18678       }
       
 18679     }else{
       
 18680       z[j++] = z[i];
       
 18681     }
       
 18682   }
       
 18683   z[j] = 0;
       
 18684   return j;
       
 18685 }
       
 18686 
       
 18687 /* Convenient short-hand */
       
 18688 #define UpperToLower sqlite3UpperToLower
       
 18689 
       
 18690 /*
       
 18691 ** Some systems have stricmp().  Others have strcasecmp().  Because
       
 18692 ** there is no consistency, we will define our own.
       
 18693 */
       
 18694 SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
       
 18695   register unsigned char *a, *b;
       
 18696   a = (unsigned char *)zLeft;
       
 18697   b = (unsigned char *)zRight;
       
 18698   while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
       
 18699   return UpperToLower[*a] - UpperToLower[*b];
       
 18700 }
       
 18701 SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
       
 18702   register unsigned char *a, *b;
       
 18703   a = (unsigned char *)zLeft;
       
 18704   b = (unsigned char *)zRight;
       
 18705   while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
       
 18706   return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
       
 18707 }
       
 18708 
       
 18709 /*
       
 18710 ** Return TRUE if z is a pure numeric string.  Return FALSE and leave
       
 18711 ** *realnum unchanged if the string contains any character which is not
       
 18712 ** part of a number.
       
 18713 **
       
 18714 ** If the string is pure numeric, set *realnum to TRUE if the string
       
 18715 ** contains the '.' character or an "E+000" style exponentiation suffix.
       
 18716 ** Otherwise set *realnum to FALSE.  Note that just becaue *realnum is
       
 18717 ** false does not mean that the number can be successfully converted into
       
 18718 ** an integer - it might be too big.
       
 18719 **
       
 18720 ** An empty string is considered non-numeric.
       
 18721 */
       
 18722 SQLITE_PRIVATE int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
       
 18723   int incr = (enc==SQLITE_UTF8?1:2);
       
 18724   if( enc==SQLITE_UTF16BE ) z++;
       
 18725   if( *z=='-' || *z=='+' ) z += incr;
       
 18726   if( !sqlite3Isdigit(*z) ){
       
 18727     return 0;
       
 18728   }
       
 18729   z += incr;
       
 18730   *realnum = 0;
       
 18731   while( sqlite3Isdigit(*z) ){ z += incr; }
       
 18732   if( *z=='.' ){
       
 18733     z += incr;
       
 18734     if( !sqlite3Isdigit(*z) ) return 0;
       
 18735     while( sqlite3Isdigit(*z) ){ z += incr; }
       
 18736     *realnum = 1;
       
 18737   }
       
 18738   if( *z=='e' || *z=='E' ){
       
 18739     z += incr;
       
 18740     if( *z=='+' || *z=='-' ) z += incr;
       
 18741     if( !sqlite3Isdigit(*z) ) return 0;
       
 18742     while( sqlite3Isdigit(*z) ){ z += incr; }
       
 18743     *realnum = 1;
       
 18744   }
       
 18745   return *z==0;
       
 18746 }
       
 18747 
       
 18748 /*
       
 18749 ** The string z[] is an ASCII representation of a real number.
       
 18750 ** Convert this string to a double.
       
 18751 **
       
 18752 ** This routine assumes that z[] really is a valid number.  If it
       
 18753 ** is not, the result is undefined.
       
 18754 **
       
 18755 ** This routine is used instead of the library atof() function because
       
 18756 ** the library atof() might want to use "," as the decimal point instead
       
 18757 ** of "." depending on how locale is set.  But that would cause problems
       
 18758 ** for SQL.  So this routine always uses "." regardless of locale.
       
 18759 */
       
 18760 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult){
       
 18761 #ifndef SQLITE_OMIT_FLOATING_POINT
       
 18762   const char *zBegin = z;
       
 18763   /* sign * significand * (10 ^ (esign * exponent)) */
       
 18764   int sign = 1;   /* sign of significand */
       
 18765   i64 s = 0;      /* significand */
       
 18766   int d = 0;      /* adjust exponent for shifting decimal point */
       
 18767   int esign = 1;  /* sign of exponent */
       
 18768   int e = 0;      /* exponent */
       
 18769   double result;
       
 18770   int nDigits = 0;
       
 18771 
       
 18772   /* skip leading spaces */
       
 18773   while( sqlite3Isspace(*z) ) z++;
       
 18774   /* get sign of significand */
       
 18775   if( *z=='-' ){
       
 18776     sign = -1;
       
 18777     z++;
       
 18778   }else if( *z=='+' ){
       
 18779     z++;
       
 18780   }
       
 18781   /* skip leading zeroes */
       
 18782   while( z[0]=='0' ) z++, nDigits++;
       
 18783 
       
 18784   /* copy max significant digits to significand */
       
 18785   while( sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
       
 18786     s = s*10 + (*z - '0');
       
 18787     z++, nDigits++;
       
 18788   }
       
 18789   /* skip non-significant significand digits
       
 18790   ** (increase exponent by d to shift decimal left) */
       
 18791   while( sqlite3Isdigit(*z) ) z++, nDigits++, d++;
       
 18792 
       
 18793   /* if decimal point is present */
       
 18794   if( *z=='.' ){
       
 18795     z++;
       
 18796     /* copy digits from after decimal to significand
       
 18797     ** (decrease exponent by d to shift decimal right) */
       
 18798     while( sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
       
 18799       s = s*10 + (*z - '0');
       
 18800       z++, nDigits++, d--;
       
 18801     }
       
 18802     /* skip non-significant digits */
       
 18803     while( sqlite3Isdigit(*z) ) z++, nDigits++;
       
 18804   }
       
 18805 
       
 18806   /* if exponent is present */
       
 18807   if( *z=='e' || *z=='E' ){
       
 18808     z++;
       
 18809     /* get sign of exponent */
       
 18810     if( *z=='-' ){
       
 18811       esign = -1;
       
 18812       z++;
       
 18813     }else if( *z=='+' ){
       
 18814       z++;
       
 18815     }
       
 18816     /* copy digits to exponent */
       
 18817     while( sqlite3Isdigit(*z) ){
       
 18818       e = e*10 + (*z - '0');
       
 18819       z++;
       
 18820     }
       
 18821   }
       
 18822 
       
 18823   /* adjust exponent by d, and update sign */
       
 18824   e = (e*esign) + d;
       
 18825   if( e<0 ) {
       
 18826     esign = -1;
       
 18827     e *= -1;
       
 18828   } else {
       
 18829     esign = 1;
       
 18830   }
       
 18831 
       
 18832   /* if 0 significand */
       
 18833   if( !s ) {
       
 18834     /* In the IEEE 754 standard, zero is signed.
       
 18835     ** Add the sign if we've seen at least one digit */
       
 18836     result = (sign<0 && nDigits) ? -(double)0 : (double)0;
       
 18837   } else {
       
 18838     /* attempt to reduce exponent */
       
 18839     if( esign>0 ){
       
 18840       while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
       
 18841     }else{
       
 18842       while( !(s%10) && e>0 ) e--,s/=10;
       
 18843     }
       
 18844 
       
 18845     /* adjust the sign of significand */
       
 18846     s = sign<0 ? -s : s;
       
 18847 
       
 18848     /* if exponent, scale significand as appropriate
       
 18849     ** and store in result. */
       
 18850     if( e ){
       
 18851       double scale = 1.0;
       
 18852       /* attempt to handle extremely small/large numbers better */
       
 18853       if( e>307 && e<342 ){
       
 18854         while( e%308 ) { scale *= 1.0e+1; e -= 1; }
       
 18855         if( esign<0 ){
       
 18856           result = s / scale;
       
 18857           result /= 1.0e+308;
       
 18858         }else{
       
 18859           result = s * scale;
       
 18860           result *= 1.0e+308;
       
 18861         }
       
 18862       }else{
       
 18863         /* 1.0e+22 is the largest power of 10 than can be 
       
 18864         ** represented exactly. */
       
 18865         while( e%22 ) { scale *= 1.0e+1; e -= 1; }
       
 18866         while( e>0 ) { scale *= 1.0e+22; e -= 22; }
       
 18867         if( esign<0 ){
       
 18868           result = s / scale;
       
 18869         }else{
       
 18870           result = s * scale;
       
 18871         }
       
 18872       }
       
 18873     } else {
       
 18874       result = (double)s;
       
 18875     }
       
 18876   }
       
 18877 
       
 18878   /* store the result */
       
 18879   *pResult = result;
       
 18880 
       
 18881   /* return number of characters used */
       
 18882   return (int)(z - zBegin);
       
 18883 #else
       
 18884   return sqlite3Atoi64(z, pResult);
       
 18885 #endif /* SQLITE_OMIT_FLOATING_POINT */
       
 18886 }
       
 18887 
       
 18888 /*
       
 18889 ** Compare the 19-character string zNum against the text representation
       
 18890 ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
       
 18891 ** if zNum is less than, equal to, or greater than the string.
       
 18892 **
       
 18893 ** Unlike memcmp() this routine is guaranteed to return the difference
       
 18894 ** in the values of the last digit if the only difference is in the
       
 18895 ** last digit.  So, for example,
       
 18896 **
       
 18897 **      compare2pow63("9223372036854775800")
       
 18898 **
       
 18899 ** will return -8.
       
 18900 */
       
 18901 static int compare2pow63(const char *zNum){
       
 18902   int c;
       
 18903   c = memcmp(zNum,"922337203685477580",18)*10;
       
 18904   if( c==0 ){
       
 18905     c = zNum[18] - '8';
       
 18906   }
       
 18907   return c;
       
 18908 }
       
 18909 
       
 18910 
       
 18911 /*
       
 18912 ** Return TRUE if zNum is a 64-bit signed integer and write
       
 18913 ** the value of the integer into *pNum.  If zNum is not an integer
       
 18914 ** or is an integer that is too large to be expressed with 64 bits,
       
 18915 ** then return false.
       
 18916 **
       
 18917 ** When this routine was originally written it dealt with only
       
 18918 ** 32-bit numbers.  At that time, it was much faster than the
       
 18919 ** atoi() library routine in RedHat 7.2.
       
 18920 */
       
 18921 SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum){
       
 18922   i64 v = 0;
       
 18923   int neg;
       
 18924   int i, c;
       
 18925   const char *zStart;
       
 18926   while( sqlite3Isspace(*zNum) ) zNum++;
       
 18927   if( *zNum=='-' ){
       
 18928     neg = 1;
       
 18929     zNum++;
       
 18930   }else if( *zNum=='+' ){
       
 18931     neg = 0;
       
 18932     zNum++;
       
 18933   }else{
       
 18934     neg = 0;
       
 18935   }
       
 18936   zStart = zNum;
       
 18937   while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */
       
 18938   for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
       
 18939     v = v*10 + c - '0';
       
 18940   }
       
 18941   *pNum = neg ? -v : v;
       
 18942   if( c!=0 || (i==0 && zStart==zNum) || i>19 ){
       
 18943     /* zNum is empty or contains non-numeric text or is longer
       
 18944     ** than 19 digits (thus guaranting that it is too large) */
       
 18945     return 0;
       
 18946   }else if( i<19 ){
       
 18947     /* Less than 19 digits, so we know that it fits in 64 bits */
       
 18948     return 1;
       
 18949   }else{
       
 18950     /* 19-digit numbers must be no larger than 9223372036854775807 if positive
       
 18951     ** or 9223372036854775808 if negative.  Note that 9223372036854665808
       
 18952     ** is 2^63. */
       
 18953     return compare2pow63(zNum)<neg;
       
 18954   }
       
 18955 }
       
 18956 
       
 18957 /*
       
 18958 ** The string zNum represents an unsigned integer.  The zNum string
       
 18959 ** consists of one or more digit characters and is terminated by
       
 18960 ** a zero character.  Any stray characters in zNum result in undefined
       
 18961 ** behavior.
       
 18962 **
       
 18963 ** If the unsigned integer that zNum represents will fit in a
       
 18964 ** 64-bit signed integer, return TRUE.  Otherwise return FALSE.
       
 18965 **
       
 18966 ** If the negFlag parameter is true, that means that zNum really represents
       
 18967 ** a negative number.  (The leading "-" is omitted from zNum.)  This
       
 18968 ** parameter is needed to determine a boundary case.  A string
       
 18969 ** of "9223373036854775808" returns false if negFlag is false or true
       
 18970 ** if negFlag is true.
       
 18971 **
       
 18972 ** Leading zeros are ignored.
       
 18973 */
       
 18974 SQLITE_PRIVATE int sqlite3FitsIn64Bits(const char *zNum, int negFlag){
       
 18975   int i;
       
 18976   int neg = 0;
       
 18977 
       
 18978   assert( zNum[0]>='0' && zNum[0]<='9' ); /* zNum is an unsigned number */
       
 18979 
       
 18980   if( negFlag ) neg = 1-neg;
       
 18981   while( *zNum=='0' ){
       
 18982     zNum++;   /* Skip leading zeros.  Ticket #2454 */
       
 18983   }
       
 18984   for(i=0; zNum[i]; i++){ assert( zNum[i]>='0' && zNum[i]<='9' ); }
       
 18985   if( i<19 ){
       
 18986     /* Guaranteed to fit if less than 19 digits */
       
 18987     return 1;
       
 18988   }else if( i>19 ){
       
 18989     /* Guaranteed to be too big if greater than 19 digits */
       
 18990     return 0;
       
 18991   }else{
       
 18992     /* Compare against 2^63. */
       
 18993     return compare2pow63(zNum)<neg;
       
 18994   }
       
 18995 }
       
 18996 
       
 18997 /*
       
 18998 ** If zNum represents an integer that will fit in 32-bits, then set
       
 18999 ** *pValue to that integer and return true.  Otherwise return false.
       
 19000 **
       
 19001 ** Any non-numeric characters that following zNum are ignored.
       
 19002 ** This is different from sqlite3Atoi64() which requires the
       
 19003 ** input number to be zero-terminated.
       
 19004 */
       
 19005 SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
       
 19006   sqlite_int64 v = 0;
       
 19007   int i, c;
       
 19008   int neg = 0;
       
 19009   if( zNum[0]=='-' ){
       
 19010     neg = 1;
       
 19011     zNum++;
       
 19012   }else if( zNum[0]=='+' ){
       
 19013     zNum++;
       
 19014   }
       
 19015   while( zNum[0]=='0' ) zNum++;
       
 19016   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
       
 19017     v = v*10 + c;
       
 19018   }
       
 19019 
       
 19020   /* The longest decimal representation of a 32 bit integer is 10 digits:
       
 19021   **
       
 19022   **             1234567890
       
 19023   **     2^31 -> 2147483648
       
 19024   */
       
 19025   if( i>10 ){
       
 19026     return 0;
       
 19027   }
       
 19028   if( v-neg>2147483647 ){
       
 19029     return 0;
       
 19030   }
       
 19031   if( neg ){
       
 19032     v = -v;
       
 19033   }
       
 19034   *pValue = (int)v;
       
 19035   return 1;
       
 19036 }
       
 19037 
       
 19038 /*
       
 19039 ** The variable-length integer encoding is as follows:
       
 19040 **
       
 19041 ** KEY:
       
 19042 **         A = 0xxxxxxx    7 bits of data and one flag bit
       
 19043 **         B = 1xxxxxxx    7 bits of data and one flag bit
       
 19044 **         C = xxxxxxxx    8 bits of data
       
 19045 **
       
 19046 **  7 bits - A
       
 19047 ** 14 bits - BA
       
 19048 ** 21 bits - BBA
       
 19049 ** 28 bits - BBBA
       
 19050 ** 35 bits - BBBBA
       
 19051 ** 42 bits - BBBBBA
       
 19052 ** 49 bits - BBBBBBA
       
 19053 ** 56 bits - BBBBBBBA
       
 19054 ** 64 bits - BBBBBBBBC
       
 19055 */
       
 19056 
       
 19057 /*
       
 19058 ** Write a 64-bit variable-length integer to memory starting at p[0].
       
 19059 ** The length of data write will be between 1 and 9 bytes.  The number
       
 19060 ** of bytes written is returned.
       
 19061 **
       
 19062 ** A variable-length integer consists of the lower 7 bits of each byte
       
 19063 ** for all bytes that have the 8th bit set and one byte with the 8th
       
 19064 ** bit clear.  Except, if we get to the 9th byte, it stores the full
       
 19065 ** 8 bits and is the last byte.
       
 19066 */
       
 19067 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
       
 19068   int i, j, n;
       
 19069   u8 buf[10];
       
 19070   if( v & (((u64)0xff000000)<<32) ){
       
 19071     p[8] = (u8)v;
       
 19072     v >>= 8;
       
 19073     for(i=7; i>=0; i--){
       
 19074       p[i] = (u8)((v & 0x7f) | 0x80);
       
 19075       v >>= 7;
       
 19076     }
       
 19077     return 9;
       
 19078   }    
       
 19079   n = 0;
       
 19080   do{
       
 19081     buf[n++] = (u8)((v & 0x7f) | 0x80);
       
 19082     v >>= 7;
       
 19083   }while( v!=0 );
       
 19084   buf[0] &= 0x7f;
       
 19085   assert( n<=9 );
       
 19086   for(i=0, j=n-1; j>=0; j--, i++){
       
 19087     p[i] = buf[j];
       
 19088   }
       
 19089   return n;
       
 19090 }
       
 19091 
       
 19092 /*
       
 19093 ** This routine is a faster version of sqlite3PutVarint() that only
       
 19094 ** works for 32-bit positive integers and which is optimized for
       
 19095 ** the common case of small integers.  A MACRO version, putVarint32,
       
 19096 ** is provided which inlines the single-byte case.  All code should use
       
 19097 ** the MACRO version as this function assumes the single-byte case has
       
 19098 ** already been handled.
       
 19099 */
       
 19100 SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char *p, u32 v){
       
 19101 #ifndef putVarint32
       
 19102   if( (v & ~0x7f)==0 ){
       
 19103     p[0] = v;
       
 19104     return 1;
       
 19105   }
       
 19106 #endif
       
 19107   if( (v & ~0x3fff)==0 ){
       
 19108     p[0] = (u8)((v>>7) | 0x80);
       
 19109     p[1] = (u8)(v & 0x7f);
       
 19110     return 2;
       
 19111   }
       
 19112   return sqlite3PutVarint(p, v);
       
 19113 }
       
 19114 
       
 19115 /*
       
 19116 ** Read a 64-bit variable-length integer from memory starting at p[0].
       
 19117 ** Return the number of bytes read.  The value is stored in *v.
       
 19118 */
       
 19119 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
       
 19120   u32 a,b,s;
       
 19121 
       
 19122   a = *p;
       
 19123   /* a: p0 (unmasked) */
       
 19124   if (!(a&0x80))
       
 19125   {
       
 19126     *v = a;
       
 19127     return 1;
       
 19128   }
       
 19129 
       
 19130   p++;
       
 19131   b = *p;
       
 19132   /* b: p1 (unmasked) */
       
 19133   if (!(b&0x80))
       
 19134   {
       
 19135     a &= 0x7f;
       
 19136     a = a<<7;
       
 19137     a |= b;
       
 19138     *v = a;
       
 19139     return 2;
       
 19140   }
       
 19141 
       
 19142   p++;
       
 19143   a = a<<14;
       
 19144   a |= *p;
       
 19145   /* a: p0<<14 | p2 (unmasked) */
       
 19146   if (!(a&0x80))
       
 19147   {
       
 19148     a &= (0x7f<<14)|(0x7f);
       
 19149     b &= 0x7f;
       
 19150     b = b<<7;
       
 19151     a |= b;
       
 19152     *v = a;
       
 19153     return 3;
       
 19154   }
       
 19155 
       
 19156   /* CSE1 from below */
       
 19157   a &= (0x7f<<14)|(0x7f);
       
 19158   p++;
       
 19159   b = b<<14;
       
 19160   b |= *p;
       
 19161   /* b: p1<<14 | p3 (unmasked) */
       
 19162   if (!(b&0x80))
       
 19163   {
       
 19164     b &= (0x7f<<14)|(0x7f);
       
 19165     /* moved CSE1 up */
       
 19166     /* a &= (0x7f<<14)|(0x7f); */
       
 19167     a = a<<7;
       
 19168     a |= b;
       
 19169     *v = a;
       
 19170     return 4;
       
 19171   }
       
 19172 
       
 19173   /* a: p0<<14 | p2 (masked) */
       
 19174   /* b: p1<<14 | p3 (unmasked) */
       
 19175   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
       
 19176   /* moved CSE1 up */
       
 19177   /* a &= (0x7f<<14)|(0x7f); */
       
 19178   b &= (0x7f<<14)|(0x7f);
       
 19179   s = a;
       
 19180   /* s: p0<<14 | p2 (masked) */
       
 19181 
       
 19182   p++;
       
 19183   a = a<<14;
       
 19184   a |= *p;
       
 19185   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
       
 19186   if (!(a&0x80))
       
 19187   {
       
 19188     /* we can skip these cause they were (effectively) done above in calc'ing s */
       
 19189     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
       
 19190     /* b &= (0x7f<<14)|(0x7f); */
       
 19191     b = b<<7;
       
 19192     a |= b;
       
 19193     s = s>>18;
       
 19194     *v = ((u64)s)<<32 | a;
       
 19195     return 5;
       
 19196   }
       
 19197 
       
 19198   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
       
 19199   s = s<<7;
       
 19200   s |= b;
       
 19201   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
       
 19202 
       
 19203   p++;
       
 19204   b = b<<14;
       
 19205   b |= *p;
       
 19206   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
       
 19207   if (!(b&0x80))
       
 19208   {
       
 19209     /* we can skip this cause it was (effectively) done above in calc'ing s */
       
 19210     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
       
 19211     a &= (0x7f<<14)|(0x7f);
       
 19212     a = a<<7;
       
 19213     a |= b;
       
 19214     s = s>>18;
       
 19215     *v = ((u64)s)<<32 | a;
       
 19216     return 6;
       
 19217   }
       
 19218 
       
 19219   p++;
       
 19220   a = a<<14;
       
 19221   a |= *p;
       
 19222   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
       
 19223   if (!(a&0x80))
       
 19224   {
       
 19225     a &= (0x1f<<28)|(0x7f<<14)|(0x7f);
       
 19226     b &= (0x7f<<14)|(0x7f);
       
 19227     b = b<<7;
       
 19228     a |= b;
       
 19229     s = s>>11;
       
 19230     *v = ((u64)s)<<32 | a;
       
 19231     return 7;
       
 19232   }
       
 19233 
       
 19234   /* CSE2 from below */
       
 19235   a &= (0x7f<<14)|(0x7f);
       
 19236   p++;
       
 19237   b = b<<14;
       
 19238   b |= *p;
       
 19239   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
       
 19240   if (!(b&0x80))
       
 19241   {
       
 19242     b &= (0x1f<<28)|(0x7f<<14)|(0x7f);
       
 19243     /* moved CSE2 up */
       
 19244     /* a &= (0x7f<<14)|(0x7f); */
       
 19245     a = a<<7;
       
 19246     a |= b;
       
 19247     s = s>>4;
       
 19248     *v = ((u64)s)<<32 | a;
       
 19249     return 8;
       
 19250   }
       
 19251 
       
 19252   p++;
       
 19253   a = a<<15;
       
 19254   a |= *p;
       
 19255   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
       
 19256 
       
 19257   /* moved CSE2 up */
       
 19258   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
       
 19259   b &= (0x7f<<14)|(0x7f);
       
 19260   b = b<<8;
       
 19261   a |= b;
       
 19262 
       
 19263   s = s<<4;
       
 19264   b = p[-4];
       
 19265   b &= 0x7f;
       
 19266   b = b>>3;
       
 19267   s |= b;
       
 19268 
       
 19269   *v = ((u64)s)<<32 | a;
       
 19270 
       
 19271   return 9;
       
 19272 }
       
 19273 
       
 19274 /*
       
 19275 ** Read a 32-bit variable-length integer from memory starting at p[0].
       
 19276 ** Return the number of bytes read.  The value is stored in *v.
       
 19277 **
       
 19278 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
       
 19279 ** integer, then set *v to 0xffffffff.
       
 19280 **
       
 19281 ** A MACRO version, getVarint32, is provided which inlines the 
       
 19282 ** single-byte case.  All code should use the MACRO version as 
       
 19283 ** this function assumes the single-byte case has already been handled.
       
 19284 */
       
 19285 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
       
 19286   u32 a,b;
       
 19287 
       
 19288   /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
       
 19289   ** by the getVarin32() macro */
       
 19290   a = *p;
       
 19291   /* a: p0 (unmasked) */
       
 19292 #ifndef getVarint32
       
 19293   if (!(a&0x80))
       
 19294   {
       
 19295     /* Values between 0 and 127 */
       
 19296     *v = a;
       
 19297     return 1;
       
 19298   }
       
 19299 #endif
       
 19300 
       
 19301   /* The 2-byte case */
       
 19302   p++;
       
 19303   b = *p;
       
 19304   /* b: p1 (unmasked) */
       
 19305   if (!(b&0x80))
       
 19306   {
       
 19307     /* Values between 128 and 16383 */
       
 19308     a &= 0x7f;
       
 19309     a = a<<7;
       
 19310     *v = a | b;
       
 19311     return 2;
       
 19312   }
       
 19313 
       
 19314   /* The 3-byte case */
       
 19315   p++;
       
 19316   a = a<<14;
       
 19317   a |= *p;
       
 19318   /* a: p0<<14 | p2 (unmasked) */
       
 19319   if (!(a&0x80))
       
 19320   {
       
 19321     /* Values between 16384 and 2097151 */
       
 19322     a &= (0x7f<<14)|(0x7f);
       
 19323     b &= 0x7f;
       
 19324     b = b<<7;
       
 19325     *v = a | b;
       
 19326     return 3;
       
 19327   }
       
 19328 
       
 19329   /* A 32-bit varint is used to store size information in btrees.
       
 19330   ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
       
 19331   ** A 3-byte varint is sufficient, for example, to record the size
       
 19332   ** of a 1048569-byte BLOB or string.
       
 19333   **
       
 19334   ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
       
 19335   ** rare larger cases can be handled by the slower 64-bit varint
       
 19336   ** routine.
       
 19337   */
       
 19338 #if 1
       
 19339   {
       
 19340     u64 v64;
       
 19341     u8 n;
       
 19342 
       
 19343     p -= 2;
       
 19344     n = sqlite3GetVarint(p, &v64);
       
 19345     assert( n>3 && n<=9 );
       
 19346     if( (v64 & SQLITE_MAX_U32)!=v64 ){
       
 19347       *v = 0xffffffff;
       
 19348     }else{
       
 19349       *v = (u32)v64;
       
 19350     }
       
 19351     return n;
       
 19352   }
       
 19353 
       
 19354 #else
       
 19355   /* For following code (kept for historical record only) shows an
       
 19356   ** unrolling for the 3- and 4-byte varint cases.  This code is
       
 19357   ** slightly faster, but it is also larger and much harder to test.
       
 19358   */
       
 19359   p++;
       
 19360   b = b<<14;
       
 19361   b |= *p;
       
 19362   /* b: p1<<14 | p3 (unmasked) */
       
 19363   if (!(b&0x80))
       
 19364   {
       
 19365     /* Values between 2097152 and 268435455 */
       
 19366     b &= (0x7f<<14)|(0x7f);
       
 19367     a &= (0x7f<<14)|(0x7f);
       
 19368     a = a<<7;
       
 19369     *v = a | b;
       
 19370     return 4;
       
 19371   }
       
 19372 
       
 19373   p++;
       
 19374   a = a<<14;
       
 19375   a |= *p;
       
 19376   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
       
 19377   if (!(a&0x80))
       
 19378   {
       
 19379     /* Walues  between 268435456 and 34359738367 */
       
 19380     a &= (0x1f<<28)|(0x7f<<14)|(0x7f);
       
 19381     b &= (0x1f<<28)|(0x7f<<14)|(0x7f);
       
 19382     b = b<<7;
       
 19383     *v = a | b;
       
 19384     return 5;
       
 19385   }
       
 19386 
       
 19387   /* We can only reach this point when reading a corrupt database
       
 19388   ** file.  In that case we are not in any hurry.  Use the (relatively
       
 19389   ** slow) general-purpose sqlite3GetVarint() routine to extract the
       
 19390   ** value. */
       
 19391   {
       
 19392     u64 v64;
       
 19393     u8 n;
       
 19394 
       
 19395     p -= 4;
       
 19396     n = sqlite3GetVarint(p, &v64);
       
 19397     assert( n>5 && n<=9 );
       
 19398     *v = (u32)v64;
       
 19399     return n;
       
 19400   }
       
 19401 #endif
       
 19402 }
       
 19403 
       
 19404 /*
       
 19405 ** Return the number of bytes that will be needed to store the given
       
 19406 ** 64-bit integer.
       
 19407 */
       
 19408 SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
       
 19409   int i = 0;
       
 19410   do{
       
 19411     i++;
       
 19412     v >>= 7;
       
 19413   }while( v!=0 && ALWAYS(i<9) );
       
 19414   return i;
       
 19415 }
       
 19416 
       
 19417 
       
 19418 /*
       
 19419 ** Read or write a four-byte big-endian integer value.
       
 19420 */
       
 19421 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
       
 19422   return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
       
 19423 }
       
 19424 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
       
 19425   p[0] = (u8)(v>>24);
       
 19426   p[1] = (u8)(v>>16);
       
 19427   p[2] = (u8)(v>>8);
       
 19428   p[3] = (u8)v;
       
 19429 }
       
 19430 
       
 19431 
       
 19432 
       
 19433 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
       
 19434 /*
       
 19435 ** Translate a single byte of Hex into an integer.
       
 19436 ** This routine only works if h really is a valid hexadecimal
       
 19437 ** character:  0..9a..fA..F
       
 19438 */
       
 19439 static u8 hexToInt(int h){
       
 19440   assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
       
 19441 #ifdef SQLITE_ASCII
       
 19442   h += 9*(1&(h>>6));
       
 19443 #endif
       
 19444 #ifdef SQLITE_EBCDIC
       
 19445   h += 9*(1&~(h>>4));
       
 19446 #endif
       
 19447   return (u8)(h & 0xf);
       
 19448 }
       
 19449 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
       
 19450 
       
 19451 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
       
 19452 /*
       
 19453 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
       
 19454 ** value.  Return a pointer to its binary value.  Space to hold the
       
 19455 ** binary value has been obtained from malloc and must be freed by
       
 19456 ** the calling routine.
       
 19457 */
       
 19458 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
       
 19459   char *zBlob;
       
 19460   int i;
       
 19461 
       
 19462   zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
       
 19463   n--;
       
 19464   if( zBlob ){
       
 19465     for(i=0; i<n; i+=2){
       
 19466       zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
       
 19467     }
       
 19468     zBlob[i/2] = 0;
       
 19469   }
       
 19470   return zBlob;
       
 19471 }
       
 19472 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
       
 19473 
       
 19474 
       
 19475 /*
       
 19476 ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
       
 19477 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
       
 19478 ** when this routine is called.
       
 19479 **
       
 19480 ** This routine is called when entering an SQLite API.  The SQLITE_MAGIC_OPEN
       
 19481 ** value indicates that the database connection passed into the API is
       
 19482 ** open and is not being used by another thread.  By changing the value
       
 19483 ** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
       
 19484 ** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
       
 19485 ** when the API exits. 
       
 19486 **
       
 19487 ** This routine is a attempt to detect if two threads use the
       
 19488 ** same sqlite* pointer at the same time.  There is a race 
       
 19489 ** condition so it is possible that the error is not detected.
       
 19490 ** But usually the problem will be seen.  The result will be an
       
 19491 ** error which can be used to debug the application that is
       
 19492 ** using SQLite incorrectly.
       
 19493 **
       
 19494 ** Ticket #202:  If db->magic is not a valid open value, take care not
       
 19495 ** to modify the db structure at all.  It could be that db is a stale
       
 19496 ** pointer.  In other words, it could be that there has been a prior
       
 19497 ** call to sqlite3_close(db) and db has been deallocated.  And we do
       
 19498 ** not want to write into deallocated memory.
       
 19499 */
       
 19500 #ifdef SQLITE_DEBUG
       
 19501 SQLITE_PRIVATE int sqlite3SafetyOn(sqlite3 *db){
       
 19502   if( db->magic==SQLITE_MAGIC_OPEN ){
       
 19503     db->magic = SQLITE_MAGIC_BUSY;
       
 19504     assert( sqlite3_mutex_held(db->mutex) );
       
 19505     return 0;
       
 19506   }else if( db->magic==SQLITE_MAGIC_BUSY ){
       
 19507     db->magic = SQLITE_MAGIC_ERROR;
       
 19508     db->u1.isInterrupted = 1;
       
 19509   }
       
 19510   return 1;
       
 19511 }
       
 19512 #endif
       
 19513 
       
 19514 /*
       
 19515 ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
       
 19516 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
       
 19517 ** when this routine is called.
       
 19518 */
       
 19519 #ifdef SQLITE_DEBUG
       
 19520 SQLITE_PRIVATE int sqlite3SafetyOff(sqlite3 *db){
       
 19521   if( db->magic==SQLITE_MAGIC_BUSY ){
       
 19522     db->magic = SQLITE_MAGIC_OPEN;
       
 19523     assert( sqlite3_mutex_held(db->mutex) );
       
 19524     return 0;
       
 19525   }else{
       
 19526     db->magic = SQLITE_MAGIC_ERROR;
       
 19527     db->u1.isInterrupted = 1;
       
 19528     return 1;
       
 19529   }
       
 19530 }
       
 19531 #endif
       
 19532 
       
 19533 /*
       
 19534 ** Check to make sure we have a valid db pointer.  This test is not
       
 19535 ** foolproof but it does provide some measure of protection against
       
 19536 ** misuse of the interface such as passing in db pointers that are
       
 19537 ** NULL or which have been previously closed.  If this routine returns
       
 19538 ** 1 it means that the db pointer is valid and 0 if it should not be
       
 19539 ** dereferenced for any reason.  The calling function should invoke
       
 19540 ** SQLITE_MISUSE immediately.
       
 19541 **
       
 19542 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
       
 19543 ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
       
 19544 ** open properly and is not fit for general use but which can be
       
 19545 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
       
 19546 */
       
 19547 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
       
 19548   u32 magic;
       
 19549   if( db==0 ) return 0;
       
 19550   magic = db->magic;
       
 19551   if( magic!=SQLITE_MAGIC_OPEN 
       
 19552 #ifdef SQLITE_DEBUG
       
 19553      && magic!=SQLITE_MAGIC_BUSY
       
 19554 #endif
       
 19555   ){
       
 19556     return 0;
       
 19557   }else{
       
 19558     return 1;
       
 19559   }
       
 19560 }
       
 19561 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
       
 19562   u32 magic;
       
 19563   magic = db->magic;
       
 19564   if( magic!=SQLITE_MAGIC_SICK &&
       
 19565       magic!=SQLITE_MAGIC_OPEN &&
       
 19566       magic!=SQLITE_MAGIC_BUSY ) return 0;
       
 19567   return 1;
       
 19568 }
       
 19569 
       
 19570 /************** End of util.c ************************************************/
       
 19571 /************** Begin file hash.c ********************************************/
       
 19572 /*
       
 19573 ** 2001 September 22
       
 19574 **
       
 19575 ** The author disclaims copyright to this source code.  In place of
       
 19576 ** a legal notice, here is a blessing:
       
 19577 **
       
 19578 **    May you do good and not evil.
       
 19579 **    May you find forgiveness for yourself and forgive others.
       
 19580 **    May you share freely, never taking more than you give.
       
 19581 **
       
 19582 *************************************************************************
       
 19583 ** This is the implementation of generic hash-tables
       
 19584 ** used in SQLite.
       
 19585 **
       
 19586 ** $Id: hash.c,v 1.38 2009/05/09 23:29:12 drh Exp $
       
 19587 */
       
 19588 
       
 19589 /* Turn bulk memory into a hash table object by initializing the
       
 19590 ** fields of the Hash structure.
       
 19591 **
       
 19592 ** "pNew" is a pointer to the hash table that is to be initialized.
       
 19593 */
       
 19594 SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
       
 19595   assert( pNew!=0 );
       
 19596   pNew->first = 0;
       
 19597   pNew->count = 0;
       
 19598   pNew->htsize = 0;
       
 19599   pNew->ht = 0;
       
 19600 }
       
 19601 
       
 19602 /* Remove all entries from a hash table.  Reclaim all memory.
       
 19603 ** Call this routine to delete a hash table or to reset a hash table
       
 19604 ** to the empty state.
       
 19605 */
       
 19606 SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
       
 19607   HashElem *elem;         /* For looping over all elements of the table */
       
 19608 
       
 19609   assert( pH!=0 );
       
 19610   elem = pH->first;
       
 19611   pH->first = 0;
       
 19612   sqlite3_free(pH->ht);
       
 19613   pH->ht = 0;
       
 19614   pH->htsize = 0;
       
 19615   while( elem ){
       
 19616     HashElem *next_elem = elem->next;
       
 19617     sqlite3_free(elem);
       
 19618     elem = next_elem;
       
 19619   }
       
 19620   pH->count = 0;
       
 19621 }
       
 19622 
       
 19623 /*
       
 19624 ** The hashing function.
       
 19625 */
       
 19626 static unsigned int strHash(const char *z, int nKey){
       
 19627   int h = 0;
       
 19628   assert( nKey>=0 );
       
 19629   while( nKey > 0  ){
       
 19630     h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
       
 19631     nKey--;
       
 19632   }
       
 19633   return h;
       
 19634 }
       
 19635 
       
 19636 
       
 19637 /* Link pNew element into the hash table pH.  If pEntry!=0 then also
       
 19638 ** insert pNew into the pEntry hash bucket.
       
 19639 */
       
 19640 static void insertElement(
       
 19641   Hash *pH,              /* The complete hash table */
       
 19642   struct _ht *pEntry,    /* The entry into which pNew is inserted */
       
 19643   HashElem *pNew         /* The element to be inserted */
       
 19644 ){
       
 19645   HashElem *pHead;       /* First element already in pEntry */
       
 19646   if( pEntry ){
       
 19647     pHead = pEntry->count ? pEntry->chain : 0;
       
 19648     pEntry->count++;
       
 19649     pEntry->chain = pNew;
       
 19650   }else{
       
 19651     pHead = 0;
       
 19652   }
       
 19653   if( pHead ){
       
 19654     pNew->next = pHead;
       
 19655     pNew->prev = pHead->prev;
       
 19656     if( pHead->prev ){ pHead->prev->next = pNew; }
       
 19657     else             { pH->first = pNew; }
       
 19658     pHead->prev = pNew;
       
 19659   }else{
       
 19660     pNew->next = pH->first;
       
 19661     if( pH->first ){ pH->first->prev = pNew; }
       
 19662     pNew->prev = 0;
       
 19663     pH->first = pNew;
       
 19664   }
       
 19665 }
       
 19666 
       
 19667 
       
 19668 /* Resize the hash table so that it cantains "new_size" buckets.
       
 19669 **
       
 19670 ** The hash table might fail to resize if sqlite3_malloc() fails or
       
 19671 ** if the new size is the same as the prior size.
       
 19672 ** Return TRUE if the resize occurs and false if not.
       
 19673 */
       
 19674 static int rehash(Hash *pH, unsigned int new_size){
       
 19675   struct _ht *new_ht;            /* The new hash table */
       
 19676   HashElem *elem, *next_elem;    /* For looping over existing elements */
       
 19677 
       
 19678 #if SQLITE_MALLOC_SOFT_LIMIT>0
       
 19679   if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
       
 19680     new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
       
 19681   }
       
 19682   if( new_size==pH->htsize ) return 0;
       
 19683 #endif
       
 19684 
       
 19685   /* The inability to allocates space for a larger hash table is
       
 19686   ** a performance hit but it is not a fatal error.  So mark the
       
 19687   ** allocation as a benign.
       
 19688   */
       
 19689   sqlite3BeginBenignMalloc();
       
 19690   new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
       
 19691   sqlite3EndBenignMalloc();
       
 19692 
       
 19693   if( new_ht==0 ) return 0;
       
 19694   sqlite3_free(pH->ht);
       
 19695   pH->ht = new_ht;
       
 19696   pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
       
 19697   memset(new_ht, 0, new_size*sizeof(struct _ht));
       
 19698   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
       
 19699     unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
       
 19700     next_elem = elem->next;
       
 19701     insertElement(pH, &new_ht[h], elem);
       
 19702   }
       
 19703   return 1;
       
 19704 }
       
 19705 
       
 19706 /* This function (for internal use only) locates an element in an
       
 19707 ** hash table that matches the given key.  The hash for this key has
       
 19708 ** already been computed and is passed as the 4th parameter.
       
 19709 */
       
 19710 static HashElem *findElementGivenHash(
       
 19711   const Hash *pH,     /* The pH to be searched */
       
 19712   const char *pKey,   /* The key we are searching for */
       
 19713   int nKey,           /* Bytes in key (not counting zero terminator) */
       
 19714   unsigned int h      /* The hash for this key. */
       
 19715 ){
       
 19716   HashElem *elem;                /* Used to loop thru the element list */
       
 19717   int count;                     /* Number of elements left to test */
       
 19718 
       
 19719   if( pH->ht ){
       
 19720     struct _ht *pEntry = &pH->ht[h];
       
 19721     elem = pEntry->chain;
       
 19722     count = pEntry->count;
       
 19723   }else{
       
 19724     elem = pH->first;
       
 19725     count = pH->count;
       
 19726   }
       
 19727   while( count-- && ALWAYS(elem) ){
       
 19728     if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){ 
       
 19729       return elem;
       
 19730     }
       
 19731     elem = elem->next;
       
 19732   }
       
 19733   return 0;
       
 19734 }
       
 19735 
       
 19736 /* Remove a single entry from the hash table given a pointer to that
       
 19737 ** element and a hash on the element's key.
       
 19738 */
       
 19739 static void removeElementGivenHash(
       
 19740   Hash *pH,         /* The pH containing "elem" */
       
 19741   HashElem* elem,   /* The element to be removed from the pH */
       
 19742   unsigned int h    /* Hash value for the element */
       
 19743 ){
       
 19744   struct _ht *pEntry;
       
 19745   if( elem->prev ){
       
 19746     elem->prev->next = elem->next; 
       
 19747   }else{
       
 19748     pH->first = elem->next;
       
 19749   }
       
 19750   if( elem->next ){
       
 19751     elem->next->prev = elem->prev;
       
 19752   }
       
 19753   if( pH->ht ){
       
 19754     pEntry = &pH->ht[h];
       
 19755     if( pEntry->chain==elem ){
       
 19756       pEntry->chain = elem->next;
       
 19757     }
       
 19758     pEntry->count--;
       
 19759     assert( pEntry->count>=0 );
       
 19760   }
       
 19761   sqlite3_free( elem );
       
 19762   pH->count--;
       
 19763   if( pH->count<=0 ){
       
 19764     assert( pH->first==0 );
       
 19765     assert( pH->count==0 );
       
 19766     sqlite3HashClear(pH);
       
 19767   }
       
 19768 }
       
 19769 
       
 19770 /* Attempt to locate an element of the hash table pH with a key
       
 19771 ** that matches pKey,nKey.  Return the data for this element if it is
       
 19772 ** found, or NULL if there is no match.
       
 19773 */
       
 19774 SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
       
 19775   HashElem *elem;    /* The element that matches key */
       
 19776   unsigned int h;    /* A hash on key */
       
 19777 
       
 19778   assert( pH!=0 );
       
 19779   assert( pKey!=0 );
       
 19780   assert( nKey>=0 );
       
 19781   if( pH->ht ){
       
 19782     h = strHash(pKey, nKey) % pH->htsize;
       
 19783   }else{
       
 19784     h = 0;
       
 19785   }
       
 19786   elem = findElementGivenHash(pH, pKey, nKey, h);
       
 19787   return elem ? elem->data : 0;
       
 19788 }
       
 19789 
       
 19790 /* Insert an element into the hash table pH.  The key is pKey,nKey
       
 19791 ** and the data is "data".
       
 19792 **
       
 19793 ** If no element exists with a matching key, then a new
       
 19794 ** element is created and NULL is returned.
       
 19795 **
       
 19796 ** If another element already exists with the same key, then the
       
 19797 ** new data replaces the old data and the old data is returned.
       
 19798 ** The key is not copied in this instance.  If a malloc fails, then
       
 19799 ** the new data is returned and the hash table is unchanged.
       
 19800 **
       
 19801 ** If the "data" parameter to this function is NULL, then the
       
 19802 ** element corresponding to "key" is removed from the hash table.
       
 19803 */
       
 19804 SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
       
 19805   unsigned int h;       /* the hash of the key modulo hash table size */
       
 19806   HashElem *elem;       /* Used to loop thru the element list */
       
 19807   HashElem *new_elem;   /* New element added to the pH */
       
 19808 
       
 19809   assert( pH!=0 );
       
 19810   assert( pKey!=0 );
       
 19811   assert( nKey>=0 );
       
 19812   if( pH->htsize ){
       
 19813     h = strHash(pKey, nKey) % pH->htsize;
       
 19814   }else{
       
 19815     h = 0;
       
 19816   }
       
 19817   elem = findElementGivenHash(pH,pKey,nKey,h);
       
 19818   if( elem ){
       
 19819     void *old_data = elem->data;
       
 19820     if( data==0 ){
       
 19821       removeElementGivenHash(pH,elem,h);
       
 19822     }else{
       
 19823       elem->data = data;
       
 19824       elem->pKey = pKey;
       
 19825       assert(nKey==elem->nKey);
       
 19826     }
       
 19827     return old_data;
       
 19828   }
       
 19829   if( data==0 ) return 0;
       
 19830   new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
       
 19831   if( new_elem==0 ) return data;
       
 19832   new_elem->pKey = pKey;
       
 19833   new_elem->nKey = nKey;
       
 19834   new_elem->data = data;
       
 19835   pH->count++;
       
 19836   if( pH->count>=10 && pH->count > 2*pH->htsize ){
       
 19837     if( rehash(pH, pH->count*2) ){
       
 19838       assert( pH->htsize>0 );
       
 19839       h = strHash(pKey, nKey) % pH->htsize;
       
 19840     }
       
 19841   }
       
 19842   if( pH->ht ){
       
 19843     insertElement(pH, &pH->ht[h], new_elem);
       
 19844   }else{
       
 19845     insertElement(pH, 0, new_elem);
       
 19846   }
       
 19847   return 0;
       
 19848 }
       
 19849 
       
 19850 /************** End of hash.c ************************************************/
       
 19851 /************** Begin file opcodes.c *****************************************/
       
 19852 /* Automatically generated.  Do not edit */
       
 19853 /* See the mkopcodec.awk script for details. */
       
 19854 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
       
 19855 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
       
 19856  static const char *const azName[] = { "?",
       
 19857      /*   1 */ "VNext",
       
 19858      /*   2 */ "Affinity",
       
 19859      /*   3 */ "Column",
       
 19860      /*   4 */ "SetCookie",
       
 19861      /*   5 */ "Seek",
       
 19862      /*   6 */ "Sequence",
       
 19863      /*   7 */ "Savepoint",
       
 19864      /*   8 */ "RowKey",
       
 19865      /*   9 */ "SCopy",
       
 19866      /*  10 */ "OpenWrite",
       
 19867      /*  11 */ "If",
       
 19868      /*  12 */ "CollSeq",
       
 19869      /*  13 */ "OpenRead",
       
 19870      /*  14 */ "Expire",
       
 19871      /*  15 */ "AutoCommit",
       
 19872      /*  16 */ "Pagecount",
       
 19873      /*  17 */ "IntegrityCk",
       
 19874      /*  18 */ "Sort",
       
 19875      /*  19 */ "Not",
       
 19876      /*  20 */ "Copy",
       
 19877      /*  21 */ "Trace",
       
 19878      /*  22 */ "Function",
       
 19879      /*  23 */ "IfNeg",
       
 19880      /*  24 */ "Noop",
       
 19881      /*  25 */ "Program",
       
 19882      /*  26 */ "Return",
       
 19883      /*  27 */ "NewRowid",
       
 19884      /*  28 */ "FkCounter",
       
 19885      /*  29 */ "Variable",
       
 19886      /*  30 */ "String",
       
 19887      /*  31 */ "RealAffinity",
       
 19888      /*  32 */ "VRename",
       
 19889      /*  33 */ "ParseSchema",
       
 19890      /*  34 */ "VOpen",
       
 19891      /*  35 */ "Close",
       
 19892      /*  36 */ "CreateIndex",
       
 19893      /*  37 */ "IsUnique",
       
 19894      /*  38 */ "NotFound",
       
 19895      /*  39 */ "Int64",
       
 19896      /*  40 */ "MustBeInt",
       
 19897      /*  41 */ "Halt",
       
 19898      /*  42 */ "Rowid",
       
 19899      /*  43 */ "IdxLT",
       
 19900      /*  44 */ "AddImm",
       
 19901      /*  45 */ "RowData",
       
 19902      /*  46 */ "MemMax",
       
 19903      /*  47 */ "NotExists",
       
 19904      /*  48 */ "Gosub",
       
 19905      /*  49 */ "Integer",
       
 19906      /*  50 */ "Prev",
       
 19907      /*  51 */ "RowSetRead",
       
 19908      /*  52 */ "RowSetAdd",
       
 19909      /*  53 */ "VColumn",
       
 19910      /*  54 */ "CreateTable",
       
 19911      /*  55 */ "Last",
       
 19912      /*  56 */ "SeekLe",
       
 19913      /*  57 */ "IncrVacuum",
       
 19914      /*  58 */ "IdxRowid",
       
 19915      /*  59 */ "ResetCount",
       
 19916      /*  60 */ "Yield",
       
 19917      /*  61 */ "DropTrigger",
       
 19918      /*  62 */ "DropIndex",
       
 19919      /*  63 */ "Param",
       
 19920      /*  64 */ "IdxGE",
       
 19921      /*  65 */ "IdxDelete",
       
 19922      /*  66 */ "Vacuum",
       
 19923      /*  67 */ "IfNot",
       
 19924      /*  68 */ "Or",
       
 19925      /*  69 */ "And",
       
 19926      /*  70 */ "DropTable",
       
 19927      /*  71 */ "SeekLt",
       
 19928      /*  72 */ "MakeRecord",
       
 19929      /*  73 */ "IsNull",
       
 19930      /*  74 */ "NotNull",
       
 19931      /*  75 */ "Ne",
       
 19932      /*  76 */ "Eq",
       
 19933      /*  77 */ "Gt",
       
 19934      /*  78 */ "Le",
       
 19935      /*  79 */ "Lt",
       
 19936      /*  80 */ "Ge",
       
 19937      /*  81 */ "ResultRow",
       
 19938      /*  82 */ "BitAnd",
       
 19939      /*  83 */ "BitOr",
       
 19940      /*  84 */ "ShiftLeft",
       
 19941      /*  85 */ "ShiftRight",
       
 19942      /*  86 */ "Add",
       
 19943      /*  87 */ "Subtract",
       
 19944      /*  88 */ "Multiply",
       
 19945      /*  89 */ "Divide",
       
 19946      /*  90 */ "Remainder",
       
 19947      /*  91 */ "Concat",
       
 19948      /*  92 */ "Delete",
       
 19949      /*  93 */ "BitNot",
       
 19950      /*  94 */ "String8",
       
 19951      /*  95 */ "AggFinal",
       
 19952      /*  96 */ "Compare",
       
 19953      /*  97 */ "Goto",
       
 19954      /*  98 */ "TableLock",
       
 19955      /*  99 */ "Clear",
       
 19956      /* 100 */ "VerifyCookie",
       
 19957      /* 101 */ "AggStep",
       
 19958      /* 102 */ "Transaction",
       
 19959      /* 103 */ "VFilter",
       
 19960      /* 104 */ "VDestroy",
       
 19961      /* 105 */ "Next",
       
 19962      /* 106 */ "Count",
       
 19963      /* 107 */ "IdxInsert",
       
 19964      /* 108 */ "FkIfZero",
       
 19965      /* 109 */ "SeekGe",
       
 19966      /* 110 */ "Insert",
       
 19967      /* 111 */ "Destroy",
       
 19968      /* 112 */ "ReadCookie",
       
 19969      /* 113 */ "RowSetTest",
       
 19970      /* 114 */ "LoadAnalysis",
       
 19971      /* 115 */ "Explain",
       
 19972      /* 116 */ "HaltIfNull",
       
 19973      /* 117 */ "OpenPseudo",
       
 19974      /* 118 */ "OpenEphemeral",
       
 19975      /* 119 */ "Null",
       
 19976      /* 120 */ "Move",
       
 19977      /* 121 */ "Blob",
       
 19978      /* 122 */ "Rewind",
       
 19979      /* 123 */ "SeekGt",
       
 19980      /* 124 */ "VBegin",
       
 19981      /* 125 */ "VUpdate",
       
 19982      /* 126 */ "IfZero",
       
 19983      /* 127 */ "VCreate",
       
 19984      /* 128 */ "Found",
       
 19985      /* 129 */ "IfPos",
       
 19986      /* 130 */ "Real",
       
 19987      /* 131 */ "NullRow",
       
 19988      /* 132 */ "Jump",
       
 19989      /* 133 */ "Permutation",
       
 19990      /* 134 */ "NotUsed_134",
       
 19991      /* 135 */ "NotUsed_135",
       
 19992      /* 136 */ "NotUsed_136",
       
 19993      /* 137 */ "NotUsed_137",
       
 19994      /* 138 */ "NotUsed_138",
       
 19995      /* 139 */ "NotUsed_139",
       
 19996      /* 140 */ "NotUsed_140",
       
 19997      /* 141 */ "ToText",
       
 19998      /* 142 */ "ToBlob",
       
 19999      /* 143 */ "ToNumeric",
       
 20000      /* 144 */ "ToInt",
       
 20001      /* 145 */ "ToReal",
       
 20002   };
       
 20003   return azName[i];
       
 20004 }
       
 20005 #endif
       
 20006 
       
 20007 /************** End of opcodes.c *********************************************/
       
 20008 /************** Begin file os_os2.c ******************************************/
       
 20009 /*
       
 20010 ** 2006 Feb 14
       
 20011 **
       
 20012 ** The author disclaims copyright to this source code.  In place of
       
 20013 ** a legal notice, here is a blessing:
       
 20014 **
       
 20015 **    May you do good and not evil.
       
 20016 **    May you find forgiveness for yourself and forgive others.
       
 20017 **    May you share freely, never taking more than you give.
       
 20018 **
       
 20019 ******************************************************************************
       
 20020 **
       
 20021 ** This file contains code that is specific to OS/2.
       
 20022 **
       
 20023 ** $Id: os_os2.c,v 1.63 2008/12/10 19:26:24 drh Exp $
       
 20024 */
       
 20025 
       
 20026 
       
 20027 #if SQLITE_OS_OS2
       
 20028 
       
 20029 /*
       
 20030 ** A Note About Memory Allocation:
       
 20031 **
       
 20032 ** This driver uses malloc()/free() directly rather than going through
       
 20033 ** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
       
 20034 ** are designed for use on embedded systems where memory is scarce and
       
 20035 ** malloc failures happen frequently.  OS/2 does not typically run on
       
 20036 ** embedded systems, and when it does the developers normally have bigger
       
 20037 ** problems to worry about than running out of memory.  So there is not
       
 20038 ** a compelling need to use the wrappers.
       
 20039 **
       
 20040 ** But there is a good reason to not use the wrappers.  If we use the
       
 20041 ** wrappers then we will get simulated malloc() failures within this
       
 20042 ** driver.  And that causes all kinds of problems for our tests.  We
       
 20043 ** could enhance SQLite to deal with simulated malloc failures within
       
 20044 ** the OS driver, but the code to deal with those failure would not
       
 20045 ** be exercised on Linux (which does not need to malloc() in the driver)
       
 20046 ** and so we would have difficulty writing coverage tests for that
       
 20047 ** code.  Better to leave the code out, we think.
       
 20048 **
       
 20049 ** The point of this discussion is as follows:  When creating a new
       
 20050 ** OS layer for an embedded system, if you use this file as an example,
       
 20051 ** avoid the use of malloc()/free().  Those routines work ok on OS/2
       
 20052 ** desktops but not so well in embedded systems.
       
 20053 */
       
 20054 
       
 20055 /*
       
 20056 ** Macros used to determine whether or not to use threads.
       
 20057 */
       
 20058 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE
       
 20059 # define SQLITE_OS2_THREADS 1
       
 20060 #endif
       
 20061 
       
 20062 /*
       
 20063 ** Include code that is common to all os_*.c files
       
 20064 */
       
 20065 /************** Include os_common.h in the middle of os_os2.c ****************/
       
 20066 /************** Begin file os_common.h ***************************************/
       
 20067 /*
       
 20068 ** 2004 May 22
       
 20069 **
       
 20070 ** The author disclaims copyright to this source code.  In place of
       
 20071 ** a legal notice, here is a blessing:
       
 20072 **
       
 20073 **    May you do good and not evil.
       
 20074 **    May you find forgiveness for yourself and forgive others.
       
 20075 **    May you share freely, never taking more than you give.
       
 20076 **
       
 20077 ******************************************************************************
       
 20078 **
       
 20079 ** This file contains macros and a little bit of code that is common to
       
 20080 ** all of the platform-specific files (os_*.c) and is #included into those
       
 20081 ** files.
       
 20082 **
       
 20083 ** This file should be #included by the os_*.c files only.  It is not a
       
 20084 ** general purpose header file.
       
 20085 **
       
 20086 ** $Id: os_common.h,v 1.38 2009/02/24 18:40:50 danielk1977 Exp $
       
 20087 */
       
 20088 #ifndef _OS_COMMON_H_
       
 20089 #define _OS_COMMON_H_
       
 20090 
       
 20091 /*
       
 20092 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
       
 20093 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
       
 20094 ** switch.  The following code should catch this problem at compile-time.
       
 20095 */
       
 20096 #ifdef MEMORY_DEBUG
       
 20097 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
       
 20098 #endif
       
 20099 
       
 20100 #ifdef SQLITE_DEBUG
       
 20101 SQLITE_PRIVATE int sqlite3OSTrace = 0;
       
 20102 #define OSTRACE1(X)         if( sqlite3OSTrace ) sqlite3DebugPrintf(X)
       
 20103 #define OSTRACE2(X,Y)       if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y)
       
 20104 #define OSTRACE3(X,Y,Z)     if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z)
       
 20105 #define OSTRACE4(X,Y,Z,A)   if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A)
       
 20106 #define OSTRACE5(X,Y,Z,A,B) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A,B)
       
 20107 #define OSTRACE6(X,Y,Z,A,B,C) \
       
 20108     if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
       
 20109 #define OSTRACE7(X,Y,Z,A,B,C,D) \
       
 20110     if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
       
 20111 #else
       
 20112 #define OSTRACE1(X)
       
 20113 #define OSTRACE2(X,Y)
       
 20114 #define OSTRACE3(X,Y,Z)
       
 20115 #define OSTRACE4(X,Y,Z,A)
       
 20116 #define OSTRACE5(X,Y,Z,A,B)
       
 20117 #define OSTRACE6(X,Y,Z,A,B,C)
       
 20118 #define OSTRACE7(X,Y,Z,A,B,C,D)
       
 20119 #endif
       
 20120 
       
 20121 /*
       
 20122 ** Macros for performance tracing.  Normally turned off.  Only works
       
 20123 ** on i486 hardware.
       
 20124 */
       
 20125 #ifdef SQLITE_PERFORMANCE_TRACE
       
 20126 
       
 20127 /* 
       
 20128 ** hwtime.h contains inline assembler code for implementing 
       
 20129 ** high-performance timing routines.
       
 20130 */
       
 20131 /************** Include hwtime.h in the middle of os_common.h ****************/
       
 20132 /************** Begin file hwtime.h ******************************************/
       
 20133 /*
       
 20134 ** 2008 May 27
       
 20135 **
       
 20136 ** The author disclaims copyright to this source code.  In place of
       
 20137 ** a legal notice, here is a blessing:
       
 20138 **
       
 20139 **    May you do good and not evil.
       
 20140 **    May you find forgiveness for yourself and forgive others.
       
 20141 **    May you share freely, never taking more than you give.
       
 20142 **
       
 20143 ******************************************************************************
       
 20144 **
       
 20145 ** This file contains inline asm code for retrieving "high-performance"
       
 20146 ** counters for x86 class CPUs.
       
 20147 **
       
 20148 ** $Id: hwtime.h,v 1.3 2008/08/01 14:33:15 shane Exp $
       
 20149 */
       
 20150 #ifndef _HWTIME_H_
       
 20151 #define _HWTIME_H_
       
 20152 
       
 20153 /*
       
 20154 ** The following routine only works on pentium-class (or newer) processors.
       
 20155 ** It uses the RDTSC opcode to read the cycle count value out of the
       
 20156 ** processor and returns that value.  This can be used for high-res
       
 20157 ** profiling.
       
 20158 */
       
 20159 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
       
 20160       (defined(i386) || defined(__i386__) || defined(_M_IX86))
       
 20161 
       
 20162   #if defined(__GNUC__)
       
 20163 
       
 20164   __inline__ sqlite_uint64 sqlite3Hwtime(void){
       
 20165      unsigned int lo, hi;
       
 20166      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
       
 20167      return (sqlite_uint64)hi << 32 | lo;
       
 20168   }
       
 20169 
       
 20170   #elif defined(_MSC_VER)
       
 20171 
       
 20172   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
       
 20173      __asm {
       
 20174         rdtsc
       
 20175         ret       ; return value at EDX:EAX
       
 20176      }
       
 20177   }
       
 20178 
       
 20179   #endif
       
 20180 
       
 20181 #elif (defined(__GNUC__) && defined(__x86_64__))
       
 20182 
       
 20183   __inline__ sqlite_uint64 sqlite3Hwtime(void){
       
 20184       unsigned long val;
       
 20185       __asm__ __volatile__ ("rdtsc" : "=A" (val));
       
 20186       return val;
       
 20187   }
       
 20188  
       
 20189 #elif (defined(__GNUC__) && defined(__ppc__))
       
 20190 
       
 20191   __inline__ sqlite_uint64 sqlite3Hwtime(void){
       
 20192       unsigned long long retval;
       
 20193       unsigned long junk;
       
 20194       __asm__ __volatile__ ("\n\
       
 20195           1:      mftbu   %1\n\
       
 20196                   mftb    %L0\n\
       
 20197                   mftbu   %0\n\
       
 20198                   cmpw    %0,%1\n\
       
 20199                   bne     1b"
       
 20200                   : "=r" (retval), "=r" (junk));
       
 20201       return retval;
       
 20202   }
       
 20203 
       
 20204 #else
       
 20205 
       
 20206   #error Need implementation of sqlite3Hwtime() for your platform.
       
 20207 
       
 20208   /*
       
 20209   ** To compile without implementing sqlite3Hwtime() for your platform,
       
 20210   ** you can remove the above #error and use the following
       
 20211   ** stub function.  You will lose timing support for many
       
 20212   ** of the debugging and testing utilities, but it should at
       
 20213   ** least compile and run.
       
 20214   */
       
 20215 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
       
 20216 
       
 20217 #endif
       
 20218 
       
 20219 #endif /* !defined(_HWTIME_H_) */
       
 20220 
       
 20221 /************** End of hwtime.h **********************************************/
       
 20222 /************** Continuing where we left off in os_common.h ******************/
       
 20223 
       
 20224 static sqlite_uint64 g_start;
       
 20225 static sqlite_uint64 g_elapsed;
       
 20226 #define TIMER_START       g_start=sqlite3Hwtime()
       
 20227 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
       
 20228 #define TIMER_ELAPSED     g_elapsed
       
 20229 #else
       
 20230 #define TIMER_START
       
 20231 #define TIMER_END
       
 20232 #define TIMER_ELAPSED     ((sqlite_uint64)0)
       
 20233 #endif
       
 20234 
       
 20235 /*
       
 20236 ** If we compile with the SQLITE_TEST macro set, then the following block
       
 20237 ** of code will give us the ability to simulate a disk I/O error.  This
       
 20238 ** is used for testing the I/O recovery logic.
       
 20239 */
       
 20240 #ifdef SQLITE_TEST
       
 20241 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
       
 20242 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
       
 20243 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
       
 20244 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
       
 20245 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
       
 20246 SQLITE_API int sqlite3_diskfull_pending = 0;
       
 20247 SQLITE_API int sqlite3_diskfull = 0;
       
 20248 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
       
 20249 #define SimulateIOError(CODE)  \
       
 20250   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
       
 20251        || sqlite3_io_error_pending-- == 1 )  \
       
 20252               { local_ioerr(); CODE; }
       
 20253 static void local_ioerr(){
       
 20254   IOTRACE(("IOERR\n"));
       
 20255   sqlite3_io_error_hit++;
       
 20256   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
       
 20257 }
       
 20258 #define SimulateDiskfullError(CODE) \
       
 20259    if( sqlite3_diskfull_pending ){ \
       
 20260      if( sqlite3_diskfull_pending == 1 ){ \
       
 20261        local_ioerr(); \
       
 20262        sqlite3_diskfull = 1; \
       
 20263        sqlite3_io_error_hit = 1; \
       
 20264        CODE; \
       
 20265      }else{ \
       
 20266        sqlite3_diskfull_pending--; \
       
 20267      } \
       
 20268    }
       
 20269 #else
       
 20270 #define SimulateIOErrorBenign(X)
       
 20271 #define SimulateIOError(A)
       
 20272 #define SimulateDiskfullError(A)
       
 20273 #endif
       
 20274 
       
 20275 /*
       
 20276 ** When testing, keep a count of the number of open files.
       
 20277 */
       
 20278 #ifdef SQLITE_TEST
       
 20279 SQLITE_API int sqlite3_open_file_count = 0;
       
 20280 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
       
 20281 #else
       
 20282 #define OpenCounter(X)
       
 20283 #endif
       
 20284 
       
 20285 #endif /* !defined(_OS_COMMON_H_) */
       
 20286 
       
 20287 /************** End of os_common.h *******************************************/
       
 20288 /************** Continuing where we left off in os_os2.c *********************/
       
 20289 
       
 20290 /*
       
 20291 ** The os2File structure is subclass of sqlite3_file specific for the OS/2
       
 20292 ** protability layer.
       
 20293 */
       
 20294 typedef struct os2File os2File;
       
 20295 struct os2File {
       
 20296   const sqlite3_io_methods *pMethod;  /* Always the first entry */
       
 20297   HFILE h;                  /* Handle for accessing the file */
       
 20298   char* pathToDel;          /* Name of file to delete on close, NULL if not */
       
 20299   unsigned char locktype;   /* Type of lock currently held on this file */
       
 20300 };
       
 20301 
       
 20302 #define LOCK_TIMEOUT 10L /* the default locking timeout */
       
 20303 
       
 20304 /*****************************************************************************
       
 20305 ** The next group of routines implement the I/O methods specified
       
 20306 ** by the sqlite3_io_methods object.
       
 20307 ******************************************************************************/
       
 20308 
       
 20309 /*
       
 20310 ** Close a file.
       
 20311 */
       
 20312 static int os2Close( sqlite3_file *id ){
       
 20313   APIRET rc = NO_ERROR;
       
 20314   os2File *pFile;
       
 20315   if( id && (pFile = (os2File*)id) != 0 ){
       
 20316     OSTRACE2( "CLOSE %d\n", pFile->h );
       
 20317     rc = DosClose( pFile->h );
       
 20318     pFile->locktype = NO_LOCK;
       
 20319     if( pFile->pathToDel != NULL ){
       
 20320       rc = DosForceDelete( (PSZ)pFile->pathToDel );
       
 20321       free( pFile->pathToDel );
       
 20322       pFile->pathToDel = NULL;
       
 20323     }
       
 20324     id = 0;
       
 20325     OpenCounter( -1 );
       
 20326   }
       
 20327 
       
 20328   return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
       
 20329 }
       
 20330 
       
 20331 /*
       
 20332 ** Read data from a file into a buffer.  Return SQLITE_OK if all
       
 20333 ** bytes were read successfully and SQLITE_IOERR if anything goes
       
 20334 ** wrong.
       
 20335 */
       
 20336 static int os2Read(
       
 20337   sqlite3_file *id,               /* File to read from */
       
 20338   void *pBuf,                     /* Write content into this buffer */
       
 20339   int amt,                        /* Number of bytes to read */
       
 20340   sqlite3_int64 offset            /* Begin reading at this offset */
       
 20341 ){
       
 20342   ULONG fileLocation = 0L;
       
 20343   ULONG got;
       
 20344   os2File *pFile = (os2File*)id;
       
 20345   assert( id!=0 );
       
 20346   SimulateIOError( return SQLITE_IOERR_READ );
       
 20347   OSTRACE3( "READ %d lock=%d\n", pFile->h, pFile->locktype );
       
 20348   if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
       
 20349     return SQLITE_IOERR;
       
 20350   }
       
 20351   if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
       
 20352     return SQLITE_IOERR_READ;
       
 20353   }
       
 20354   if( got == (ULONG)amt )
       
 20355     return SQLITE_OK;
       
 20356   else {
       
 20357     /* Unread portions of the input buffer must be zero-filled */
       
 20358     memset(&((char*)pBuf)[got], 0, amt-got);
       
 20359     return SQLITE_IOERR_SHORT_READ;
       
 20360   }
       
 20361 }
       
 20362 
       
 20363 /*
       
 20364 ** Write data from a buffer into a file.  Return SQLITE_OK on success
       
 20365 ** or some other error code on failure.
       
 20366 */
       
 20367 static int os2Write(
       
 20368   sqlite3_file *id,               /* File to write into */
       
 20369   const void *pBuf,               /* The bytes to be written */
       
 20370   int amt,                        /* Number of bytes to write */
       
 20371   sqlite3_int64 offset            /* Offset into the file to begin writing at */
       
 20372 ){
       
 20373   ULONG fileLocation = 0L;
       
 20374   APIRET rc = NO_ERROR;
       
 20375   ULONG wrote;
       
 20376   os2File *pFile = (os2File*)id;
       
 20377   assert( id!=0 );
       
 20378   SimulateIOError( return SQLITE_IOERR_WRITE );
       
 20379   SimulateDiskfullError( return SQLITE_FULL );
       
 20380   OSTRACE3( "WRITE %d lock=%d\n", pFile->h, pFile->locktype );
       
 20381   if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
       
 20382     return SQLITE_IOERR;
       
 20383   }
       
 20384   assert( amt>0 );
       
 20385   while( amt > 0 &&
       
 20386          ( rc = DosWrite( pFile->h, (PVOID)pBuf, amt, &wrote ) ) == NO_ERROR &&
       
 20387          wrote > 0
       
 20388   ){
       
 20389     amt -= wrote;
       
 20390     pBuf = &((char*)pBuf)[wrote];
       
 20391   }
       
 20392 
       
 20393   return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
       
 20394 }
       
 20395 
       
 20396 /*
       
 20397 ** Truncate an open file to a specified size
       
 20398 */
       
 20399 static int os2Truncate( sqlite3_file *id, i64 nByte ){
       
 20400   APIRET rc = NO_ERROR;
       
 20401   os2File *pFile = (os2File*)id;
       
 20402   OSTRACE3( "TRUNCATE %d %lld\n", pFile->h, nByte );
       
 20403   SimulateIOError( return SQLITE_IOERR_TRUNCATE );
       
 20404   rc = DosSetFileSize( pFile->h, nByte );
       
 20405   return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
       
 20406 }
       
 20407 
       
 20408 #ifdef SQLITE_TEST
       
 20409 /*
       
 20410 ** Count the number of fullsyncs and normal syncs.  This is used to test
       
 20411 ** that syncs and fullsyncs are occuring at the right times.
       
 20412 */
       
 20413 SQLITE_API int sqlite3_sync_count = 0;
       
 20414 SQLITE_API int sqlite3_fullsync_count = 0;
       
 20415 #endif
       
 20416 
       
 20417 /*
       
 20418 ** Make sure all writes to a particular file are committed to disk.
       
 20419 */
       
 20420 static int os2Sync( sqlite3_file *id, int flags ){
       
 20421   os2File *pFile = (os2File*)id;
       
 20422   OSTRACE3( "SYNC %d lock=%d\n", pFile->h, pFile->locktype );
       
 20423 #ifdef SQLITE_TEST
       
 20424   if( flags & SQLITE_SYNC_FULL){
       
 20425     sqlite3_fullsync_count++;
       
 20426   }
       
 20427   sqlite3_sync_count++;
       
 20428 #endif
       
 20429   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
       
 20430   ** no-op
       
 20431   */
       
 20432 #ifdef SQLITE_NO_SYNC
       
 20433   UNUSED_PARAMETER(pFile);
       
 20434   return SQLITE_OK;
       
 20435 #else
       
 20436   return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
       
 20437 #endif
       
 20438 }
       
 20439 
       
 20440 /*
       
 20441 ** Determine the current size of a file in bytes
       
 20442 */
       
 20443 static int os2FileSize( sqlite3_file *id, sqlite3_int64 *pSize ){
       
 20444   APIRET rc = NO_ERROR;
       
 20445   FILESTATUS3 fsts3FileInfo;
       
 20446   memset(&fsts3FileInfo, 0, sizeof(fsts3FileInfo));
       
 20447   assert( id!=0 );
       
 20448   SimulateIOError( return SQLITE_IOERR_FSTAT );
       
 20449   rc = DosQueryFileInfo( ((os2File*)id)->h, FIL_STANDARD, &fsts3FileInfo, sizeof(FILESTATUS3) );
       
 20450   if( rc == NO_ERROR ){
       
 20451     *pSize = fsts3FileInfo.cbFile;
       
 20452     return SQLITE_OK;
       
 20453   }else{
       
 20454     return SQLITE_IOERR_FSTAT;
       
 20455   }
       
 20456 }
       
 20457 
       
 20458 /*
       
 20459 ** Acquire a reader lock.
       
 20460 */
       
 20461 static int getReadLock( os2File *pFile ){
       
 20462   FILELOCK  LockArea,
       
 20463             UnlockArea;
       
 20464   APIRET res;
       
 20465   memset(&LockArea, 0, sizeof(LockArea));
       
 20466   memset(&UnlockArea, 0, sizeof(UnlockArea));
       
 20467   LockArea.lOffset = SHARED_FIRST;
       
 20468   LockArea.lRange = SHARED_SIZE;
       
 20469   UnlockArea.lOffset = 0L;
       
 20470   UnlockArea.lRange = 0L;
       
 20471   res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
       
 20472   OSTRACE3( "GETREADLOCK %d res=%d\n", pFile->h, res );
       
 20473   return res;
       
 20474 }
       
 20475 
       
 20476 /*
       
 20477 ** Undo a readlock
       
 20478 */
       
 20479 static int unlockReadLock( os2File *id ){
       
 20480   FILELOCK  LockArea,
       
 20481             UnlockArea;
       
 20482   APIRET res;
       
 20483   memset(&LockArea, 0, sizeof(LockArea));
       
 20484   memset(&UnlockArea, 0, sizeof(UnlockArea));
       
 20485   LockArea.lOffset = 0L;
       
 20486   LockArea.lRange = 0L;
       
 20487   UnlockArea.lOffset = SHARED_FIRST;
       
 20488   UnlockArea.lRange = SHARED_SIZE;
       
 20489   res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
       
 20490   OSTRACE3( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res );
       
 20491   return res;
       
 20492 }
       
 20493 
       
 20494 /*
       
 20495 ** Lock the file with the lock specified by parameter locktype - one
       
 20496 ** of the following:
       
 20497 **
       
 20498 **     (1) SHARED_LOCK
       
 20499 **     (2) RESERVED_LOCK
       
 20500 **     (3) PENDING_LOCK
       
 20501 **     (4) EXCLUSIVE_LOCK
       
 20502 **
       
 20503 ** Sometimes when requesting one lock state, additional lock states
       
 20504 ** are inserted in between.  The locking might fail on one of the later
       
 20505 ** transitions leaving the lock state different from what it started but
       
 20506 ** still short of its goal.  The following chart shows the allowed
       
 20507 ** transitions and the inserted intermediate states:
       
 20508 **
       
 20509 **    UNLOCKED -> SHARED
       
 20510 **    SHARED -> RESERVED
       
 20511 **    SHARED -> (PENDING) -> EXCLUSIVE
       
 20512 **    RESERVED -> (PENDING) -> EXCLUSIVE
       
 20513 **    PENDING -> EXCLUSIVE
       
 20514 **
       
 20515 ** This routine will only increase a lock.  The os2Unlock() routine
       
 20516 ** erases all locks at once and returns us immediately to locking level 0.
       
 20517 ** It is not possible to lower the locking level one step at a time.  You
       
 20518 ** must go straight to locking level 0.
       
 20519 */
       
 20520 static int os2Lock( sqlite3_file *id, int locktype ){
       
 20521   int rc = SQLITE_OK;       /* Return code from subroutines */
       
 20522   APIRET res = NO_ERROR;    /* Result of an OS/2 lock call */
       
 20523   int newLocktype;       /* Set pFile->locktype to this value before exiting */
       
 20524   int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
       
 20525   FILELOCK  LockArea,
       
 20526             UnlockArea;
       
 20527   os2File *pFile = (os2File*)id;
       
 20528   memset(&LockArea, 0, sizeof(LockArea));
       
 20529   memset(&UnlockArea, 0, sizeof(UnlockArea));
       
 20530   assert( pFile!=0 );
       
 20531   OSTRACE4( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype );
       
 20532 
       
 20533   /* If there is already a lock of this type or more restrictive on the
       
 20534   ** os2File, do nothing. Don't use the end_lock: exit path, as
       
 20535   ** sqlite3_mutex_enter() hasn't been called yet.
       
 20536   */
       
 20537   if( pFile->locktype>=locktype ){
       
 20538     OSTRACE3( "LOCK %d %d ok (already held)\n", pFile->h, locktype );
       
 20539     return SQLITE_OK;
       
 20540   }
       
 20541 
       
 20542   /* Make sure the locking sequence is correct
       
 20543   */
       
 20544   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
       
 20545   assert( locktype!=PENDING_LOCK );
       
 20546   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
       
 20547 
       
 20548   /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
       
 20549   ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
       
 20550   ** the PENDING_LOCK byte is temporary.
       
 20551   */
       
 20552   newLocktype = pFile->locktype;
       
 20553   if( pFile->locktype==NO_LOCK
       
 20554       || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
       
 20555   ){
       
 20556     LockArea.lOffset = PENDING_BYTE;
       
 20557     LockArea.lRange = 1L;
       
 20558     UnlockArea.lOffset = 0L;
       
 20559     UnlockArea.lRange = 0L;
       
 20560 
       
 20561     /* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
       
 20562     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
       
 20563     if( res == NO_ERROR ){
       
 20564       gotPendingLock = 1;
       
 20565       OSTRACE3( "LOCK %d pending lock boolean set.  res=%d\n", pFile->h, res );
       
 20566     }
       
 20567   }
       
 20568 
       
 20569   /* Acquire a shared lock
       
 20570   */
       
 20571   if( locktype==SHARED_LOCK && res == NO_ERROR ){
       
 20572     assert( pFile->locktype==NO_LOCK );
       
 20573     res = getReadLock(pFile);
       
 20574     if( res == NO_ERROR ){
       
 20575       newLocktype = SHARED_LOCK;
       
 20576     }
       
 20577     OSTRACE3( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res );
       
 20578   }
       
 20579 
       
 20580   /* Acquire a RESERVED lock
       
 20581   */
       
 20582   if( locktype==RESERVED_LOCK && res == NO_ERROR ){
       
 20583     assert( pFile->locktype==SHARED_LOCK );
       
 20584     LockArea.lOffset = RESERVED_BYTE;
       
 20585     LockArea.lRange = 1L;
       
 20586     UnlockArea.lOffset = 0L;
       
 20587     UnlockArea.lRange = 0L;
       
 20588     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
       
 20589     if( res == NO_ERROR ){
       
 20590       newLocktype = RESERVED_LOCK;
       
 20591     }
       
 20592     OSTRACE3( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res );
       
 20593   }
       
 20594 
       
 20595   /* Acquire a PENDING lock
       
 20596   */
       
 20597   if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
       
 20598     newLocktype = PENDING_LOCK;
       
 20599     gotPendingLock = 0;
       
 20600     OSTRACE2( "LOCK %d acquire pending lock. pending lock boolean unset.\n", pFile->h );
       
 20601   }
       
 20602 
       
 20603   /* Acquire an EXCLUSIVE lock
       
 20604   */
       
 20605   if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
       
 20606     assert( pFile->locktype>=SHARED_LOCK );
       
 20607     res = unlockReadLock(pFile);
       
 20608     OSTRACE2( "unreadlock = %d\n", res );
       
 20609     LockArea.lOffset = SHARED_FIRST;
       
 20610     LockArea.lRange = SHARED_SIZE;
       
 20611     UnlockArea.lOffset = 0L;
       
 20612     UnlockArea.lRange = 0L;
       
 20613     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
       
 20614     if( res == NO_ERROR ){
       
 20615       newLocktype = EXCLUSIVE_LOCK;
       
 20616     }else{
       
 20617       OSTRACE2( "OS/2 error-code = %d\n", res );
       
 20618       getReadLock(pFile);
       
 20619     }
       
 20620     OSTRACE3( "LOCK %d acquire exclusive lock.  res=%d\n", pFile->h, res );
       
 20621   }
       
 20622 
       
 20623   /* If we are holding a PENDING lock that ought to be released, then
       
 20624   ** release it now.
       
 20625   */
       
 20626   if( gotPendingLock && locktype==SHARED_LOCK ){
       
 20627     int r;
       
 20628     LockArea.lOffset = 0L;
       
 20629     LockArea.lRange = 0L;
       
 20630     UnlockArea.lOffset = PENDING_BYTE;
       
 20631     UnlockArea.lRange = 1L;
       
 20632     r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
       
 20633     OSTRACE3( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r );
       
 20634   }
       
 20635 
       
 20636   /* Update the state of the lock has held in the file descriptor then
       
 20637   ** return the appropriate result code.
       
 20638   */
       
 20639   if( res == NO_ERROR ){
       
 20640     rc = SQLITE_OK;
       
 20641   }else{
       
 20642     OSTRACE4( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
       
 20643               locktype, newLocktype );
       
 20644     rc = SQLITE_BUSY;
       
 20645   }
       
 20646   pFile->locktype = newLocktype;
       
 20647   OSTRACE3( "LOCK %d now %d\n", pFile->h, pFile->locktype );
       
 20648   return rc;
       
 20649 }
       
 20650 
       
 20651 /*
       
 20652 ** This routine checks if there is a RESERVED lock held on the specified
       
 20653 ** file by this or any other process. If such a lock is held, return
       
 20654 ** non-zero, otherwise zero.
       
 20655 */
       
 20656 static int os2CheckReservedLock( sqlite3_file *id, int *pOut ){
       
 20657   int r = 0;
       
 20658   os2File *pFile = (os2File*)id;
       
 20659   assert( pFile!=0 );
       
 20660   if( pFile->locktype>=RESERVED_LOCK ){
       
 20661     r = 1;
       
 20662     OSTRACE3( "TEST WR-LOCK %d %d (local)\n", pFile->h, r );
       
 20663   }else{
       
 20664     FILELOCK  LockArea,
       
 20665               UnlockArea;
       
 20666     APIRET rc = NO_ERROR;
       
 20667     memset(&LockArea, 0, sizeof(LockArea));
       
 20668     memset(&UnlockArea, 0, sizeof(UnlockArea));
       
 20669     LockArea.lOffset = RESERVED_BYTE;
       
 20670     LockArea.lRange = 1L;
       
 20671     UnlockArea.lOffset = 0L;
       
 20672     UnlockArea.lRange = 0L;
       
 20673     rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
       
 20674     OSTRACE3( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc );
       
 20675     if( rc == NO_ERROR ){
       
 20676       APIRET rcu = NO_ERROR; /* return code for unlocking */
       
 20677       LockArea.lOffset = 0L;
       
 20678       LockArea.lRange = 0L;
       
 20679       UnlockArea.lOffset = RESERVED_BYTE;
       
 20680       UnlockArea.lRange = 1L;
       
 20681       rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
       
 20682       OSTRACE3( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu );
       
 20683     }
       
 20684     r = !(rc == NO_ERROR);
       
 20685     OSTRACE3( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r );
       
 20686   }
       
 20687   *pOut = r;
       
 20688   return SQLITE_OK;
       
 20689 }
       
 20690 
       
 20691 /*
       
 20692 ** Lower the locking level on file descriptor id to locktype.  locktype
       
 20693 ** must be either NO_LOCK or SHARED_LOCK.
       
 20694 **
       
 20695 ** If the locking level of the file descriptor is already at or below
       
 20696 ** the requested locking level, this routine is a no-op.
       
 20697 **
       
 20698 ** It is not possible for this routine to fail if the second argument
       
 20699 ** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
       
 20700 ** might return SQLITE_IOERR;
       
 20701 */
       
 20702 static int os2Unlock( sqlite3_file *id, int locktype ){
       
 20703   int type;
       
 20704   os2File *pFile = (os2File*)id;
       
 20705   APIRET rc = SQLITE_OK;
       
 20706   APIRET res = NO_ERROR;
       
 20707   FILELOCK  LockArea,
       
 20708             UnlockArea;
       
 20709   memset(&LockArea, 0, sizeof(LockArea));
       
 20710   memset(&UnlockArea, 0, sizeof(UnlockArea));
       
 20711   assert( pFile!=0 );
       
 20712   assert( locktype<=SHARED_LOCK );
       
 20713   OSTRACE4( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype );
       
 20714   type = pFile->locktype;
       
 20715   if( type>=EXCLUSIVE_LOCK ){
       
 20716     LockArea.lOffset = 0L;
       
 20717     LockArea.lRange = 0L;
       
 20718     UnlockArea.lOffset = SHARED_FIRST;
       
 20719     UnlockArea.lRange = SHARED_SIZE;
       
 20720     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
       
 20721     OSTRACE3( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res );
       
 20722     if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
       
 20723       /* This should never happen.  We should always be able to
       
 20724       ** reacquire the read lock */
       
 20725       OSTRACE3( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype );
       
 20726       rc = SQLITE_IOERR_UNLOCK;
       
 20727     }
       
 20728   }
       
 20729   if( type>=RESERVED_LOCK ){
       
 20730     LockArea.lOffset = 0L;
       
 20731     LockArea.lRange = 0L;
       
 20732     UnlockArea.lOffset = RESERVED_BYTE;
       
 20733     UnlockArea.lRange = 1L;
       
 20734     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
       
 20735     OSTRACE3( "UNLOCK %d reserved res=%d\n", pFile->h, res );
       
 20736   }
       
 20737   if( locktype==NO_LOCK && type>=SHARED_LOCK ){
       
 20738     res = unlockReadLock(pFile);
       
 20739     OSTRACE5( "UNLOCK %d is %d want %d res=%d\n", pFile->h, type, locktype, res );
       
 20740   }
       
 20741   if( type>=PENDING_LOCK ){
       
 20742     LockArea.lOffset = 0L;
       
 20743     LockArea.lRange = 0L;
       
 20744     UnlockArea.lOffset = PENDING_BYTE;
       
 20745     UnlockArea.lRange = 1L;
       
 20746     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
       
 20747     OSTRACE3( "UNLOCK %d pending res=%d\n", pFile->h, res );
       
 20748   }
       
 20749   pFile->locktype = locktype;
       
 20750   OSTRACE3( "UNLOCK %d now %d\n", pFile->h, pFile->locktype );
       
 20751   return rc;
       
 20752 }
       
 20753 
       
 20754 /*
       
 20755 ** Control and query of the open file handle.
       
 20756 */
       
 20757 static int os2FileControl(sqlite3_file *id, int op, void *pArg){
       
 20758   switch( op ){
       
 20759     case SQLITE_FCNTL_LOCKSTATE: {
       
 20760       *(int*)pArg = ((os2File*)id)->locktype;
       
 20761       OSTRACE3( "FCNTL_LOCKSTATE %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
       
 20762       return SQLITE_OK;
       
 20763     }
       
 20764   }
       
 20765   return SQLITE_ERROR;
       
 20766 }
       
 20767 
       
 20768 /*
       
 20769 ** Return the sector size in bytes of the underlying block device for
       
 20770 ** the specified file. This is almost always 512 bytes, but may be
       
 20771 ** larger for some devices.
       
 20772 **
       
 20773 ** SQLite code assumes this function cannot fail. It also assumes that
       
 20774 ** if two files are created in the same file-system directory (i.e.
       
 20775 ** a database and its journal file) that the sector size will be the
       
 20776 ** same for both.
       
 20777 */
       
 20778 static int os2SectorSize(sqlite3_file *id){
       
 20779   return SQLITE_DEFAULT_SECTOR_SIZE;
       
 20780 }
       
 20781 
       
 20782 /*
       
 20783 ** Return a vector of device characteristics.
       
 20784 */
       
 20785 static int os2DeviceCharacteristics(sqlite3_file *id){
       
 20786   return 0;
       
 20787 }
       
 20788 
       
 20789 
       
 20790 /*
       
 20791 ** Character set conversion objects used by conversion routines.
       
 20792 */
       
 20793 static UconvObject ucUtf8 = NULL; /* convert between UTF-8 and UCS-2 */
       
 20794 static UconvObject uclCp = NULL;  /* convert between local codepage and UCS-2 */
       
 20795 
       
 20796 /*
       
 20797 ** Helper function to initialize the conversion objects from and to UTF-8.
       
 20798 */
       
 20799 static void initUconvObjects( void ){
       
 20800   if( UniCreateUconvObject( UTF_8, &ucUtf8 ) != ULS_SUCCESS )
       
 20801     ucUtf8 = NULL;
       
 20802   if ( UniCreateUconvObject( (UniChar *)L"@path=yes", &uclCp ) != ULS_SUCCESS )
       
 20803     uclCp = NULL;
       
 20804 }
       
 20805 
       
 20806 /*
       
 20807 ** Helper function to free the conversion objects from and to UTF-8.
       
 20808 */
       
 20809 static void freeUconvObjects( void ){
       
 20810   if ( ucUtf8 )
       
 20811     UniFreeUconvObject( ucUtf8 );
       
 20812   if ( uclCp )
       
 20813     UniFreeUconvObject( uclCp );
       
 20814   ucUtf8 = NULL;
       
 20815   uclCp = NULL;
       
 20816 }
       
 20817 
       
 20818 /*
       
 20819 ** Helper function to convert UTF-8 filenames to local OS/2 codepage.
       
 20820 ** The two-step process: first convert the incoming UTF-8 string
       
 20821 ** into UCS-2 and then from UCS-2 to the current codepage.
       
 20822 ** The returned char pointer has to be freed.
       
 20823 */
       
 20824 static char *convertUtf8PathToCp( const char *in ){
       
 20825   UniChar tempPath[CCHMAXPATH];
       
 20826   char *out = (char *)calloc( CCHMAXPATH, 1 );
       
 20827 
       
 20828   if( !out )
       
 20829     return NULL;
       
 20830 
       
 20831   if( !ucUtf8 || !uclCp )
       
 20832     initUconvObjects();
       
 20833 
       
 20834   /* determine string for the conversion of UTF-8 which is CP1208 */
       
 20835   if( UniStrToUcs( ucUtf8, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
       
 20836     return out; /* if conversion fails, return the empty string */
       
 20837 
       
 20838   /* conversion for current codepage which can be used for paths */
       
 20839   UniStrFromUcs( uclCp, out, tempPath, CCHMAXPATH );
       
 20840 
       
 20841   return out;
       
 20842 }
       
 20843 
       
 20844 /*
       
 20845 ** Helper function to convert filenames from local codepage to UTF-8.
       
 20846 ** The two-step process: first convert the incoming codepage-specific
       
 20847 ** string into UCS-2 and then from UCS-2 to the codepage of UTF-8.
       
 20848 ** The returned char pointer has to be freed.
       
 20849 **
       
 20850 ** This function is non-static to be able to use this in shell.c and
       
 20851 ** similar applications that take command line arguments.
       
 20852 */
       
 20853 char *convertCpPathToUtf8( const char *in ){
       
 20854   UniChar tempPath[CCHMAXPATH];
       
 20855   char *out = (char *)calloc( CCHMAXPATH, 1 );
       
 20856 
       
 20857   if( !out )
       
 20858     return NULL;
       
 20859 
       
 20860   if( !ucUtf8 || !uclCp )
       
 20861     initUconvObjects();
       
 20862 
       
 20863   /* conversion for current codepage which can be used for paths */
       
 20864   if( UniStrToUcs( uclCp, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
       
 20865     return out; /* if conversion fails, return the empty string */
       
 20866 
       
 20867   /* determine string for the conversion of UTF-8 which is CP1208 */
       
 20868   UniStrFromUcs( ucUtf8, out, tempPath, CCHMAXPATH );
       
 20869 
       
 20870   return out;
       
 20871 }
       
 20872 
       
 20873 /*
       
 20874 ** This vector defines all the methods that can operate on an
       
 20875 ** sqlite3_file for os2.
       
 20876 */
       
 20877 static const sqlite3_io_methods os2IoMethod = {
       
 20878   1,                        /* iVersion */
       
 20879   os2Close,
       
 20880   os2Read,
       
 20881   os2Write,
       
 20882   os2Truncate,
       
 20883   os2Sync,
       
 20884   os2FileSize,
       
 20885   os2Lock,
       
 20886   os2Unlock,
       
 20887   os2CheckReservedLock,
       
 20888   os2FileControl,
       
 20889   os2SectorSize,
       
 20890   os2DeviceCharacteristics
       
 20891 };
       
 20892 
       
 20893 /***************************************************************************
       
 20894 ** Here ends the I/O methods that form the sqlite3_io_methods object.
       
 20895 **
       
 20896 ** The next block of code implements the VFS methods.
       
 20897 ****************************************************************************/
       
 20898 
       
 20899 /*
       
 20900 ** Create a temporary file name in zBuf.  zBuf must be big enough to
       
 20901 ** hold at pVfs->mxPathname characters.
       
 20902 */
       
 20903 static int getTempname(int nBuf, char *zBuf ){
       
 20904   static const unsigned char zChars[] =
       
 20905     "abcdefghijklmnopqrstuvwxyz"
       
 20906     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
       
 20907     "0123456789";
       
 20908   int i, j;
       
 20909   char zTempPathBuf[3];
       
 20910   PSZ zTempPath = (PSZ)&zTempPathBuf;
       
 20911   if( sqlite3_temp_directory ){
       
 20912     zTempPath = sqlite3_temp_directory;
       
 20913   }else{
       
 20914     if( DosScanEnv( (PSZ)"TEMP", &zTempPath ) ){
       
 20915       if( DosScanEnv( (PSZ)"TMP", &zTempPath ) ){
       
 20916         if( DosScanEnv( (PSZ)"TMPDIR", &zTempPath ) ){
       
 20917            ULONG ulDriveNum = 0, ulDriveMap = 0;
       
 20918            DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap );
       
 20919            sprintf( (char*)zTempPath, "%c:", (char)( 'A' + ulDriveNum - 1 ) );
       
 20920         }
       
 20921       }
       
 20922     }
       
 20923   }
       
 20924   /* Strip off a trailing slashes or backslashes, otherwise we would get *
       
 20925    * multiple (back)slashes which causes DosOpen() to fail.              *
       
 20926    * Trailing spaces are not allowed, either.                            */
       
 20927   j = sqlite3Strlen30(zTempPath);
       
 20928   while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/'
       
 20929                     || zTempPath[j-1] == ' ' ) ){
       
 20930     j--;
       
 20931   }
       
 20932   zTempPath[j] = '\0';
       
 20933   if( !sqlite3_temp_directory ){
       
 20934     char *zTempPathUTF = convertCpPathToUtf8( zTempPath );
       
 20935     sqlite3_snprintf( nBuf-30, zBuf,
       
 20936                       "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPathUTF );
       
 20937     free( zTempPathUTF );
       
 20938   }else{
       
 20939     sqlite3_snprintf( nBuf-30, zBuf,
       
 20940                       "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath );
       
 20941   }
       
 20942   j = sqlite3Strlen30( zBuf );
       
 20943   sqlite3_randomness( 20, &zBuf[j] );
       
 20944   for( i = 0; i < 20; i++, j++ ){
       
 20945     zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
       
 20946   }
       
 20947   zBuf[j] = 0;
       
 20948   OSTRACE2( "TEMP FILENAME: %s\n", zBuf );
       
 20949   return SQLITE_OK;
       
 20950 }
       
 20951 
       
 20952 
       
 20953 /*
       
 20954 ** Turn a relative pathname into a full pathname.  Write the full
       
 20955 ** pathname into zFull[].  zFull[] will be at least pVfs->mxPathname
       
 20956 ** bytes in size.
       
 20957 */
       
 20958 static int os2FullPathname(
       
 20959   sqlite3_vfs *pVfs,          /* Pointer to vfs object */
       
 20960   const char *zRelative,      /* Possibly relative input path */
       
 20961   int nFull,                  /* Size of output buffer in bytes */
       
 20962   char *zFull                 /* Output buffer */
       
 20963 ){
       
 20964   char *zRelativeCp = convertUtf8PathToCp( zRelative );
       
 20965   char zFullCp[CCHMAXPATH] = "\0";
       
 20966   char *zFullUTF;
       
 20967   APIRET rc = DosQueryPathInfo( zRelativeCp, FIL_QUERYFULLNAME, zFullCp,
       
 20968                                 CCHMAXPATH );
       
 20969   free( zRelativeCp );
       
 20970   zFullUTF = convertCpPathToUtf8( zFullCp );
       
 20971   sqlite3_snprintf( nFull, zFull, zFullUTF );
       
 20972   free( zFullUTF );
       
 20973   return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
       
 20974 }
       
 20975 
       
 20976 
       
 20977 /*
       
 20978 ** Open a file.
       
 20979 */
       
 20980 static int os2Open(
       
 20981   sqlite3_vfs *pVfs,            /* Not used */
       
 20982   const char *zName,            /* Name of the file */
       
 20983   sqlite3_file *id,             /* Write the SQLite file handle here */
       
 20984   int flags,                    /* Open mode flags */
       
 20985   int *pOutFlags                /* Status return flags */
       
 20986 ){
       
 20987   HFILE h;
       
 20988   ULONG ulFileAttribute = FILE_NORMAL;
       
 20989   ULONG ulOpenFlags = 0;
       
 20990   ULONG ulOpenMode = 0;
       
 20991   os2File *pFile = (os2File*)id;
       
 20992   APIRET rc = NO_ERROR;
       
 20993   ULONG ulAction;
       
 20994   char *zNameCp;
       
 20995   char zTmpname[CCHMAXPATH+1];    /* Buffer to hold name of temp file */
       
 20996 
       
 20997   /* If the second argument to this function is NULL, generate a 
       
 20998   ** temporary file name to use 
       
 20999   */
       
 21000   if( !zName ){
       
 21001     int rc = getTempname(CCHMAXPATH+1, zTmpname);
       
 21002     if( rc!=SQLITE_OK ){
       
 21003       return rc;
       
 21004     }
       
 21005     zName = zTmpname;
       
 21006   }
       
 21007 
       
 21008 
       
 21009   memset( pFile, 0, sizeof(*pFile) );
       
 21010 
       
 21011   OSTRACE2( "OPEN want %d\n", flags );
       
 21012 
       
 21013   if( flags & SQLITE_OPEN_READWRITE ){
       
 21014     ulOpenMode |= OPEN_ACCESS_READWRITE;
       
 21015     OSTRACE1( "OPEN read/write\n" );
       
 21016   }else{
       
 21017     ulOpenMode |= OPEN_ACCESS_READONLY;
       
 21018     OSTRACE1( "OPEN read only\n" );
       
 21019   }
       
 21020 
       
 21021   if( flags & SQLITE_OPEN_CREATE ){
       
 21022     ulOpenFlags |= OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
       
 21023     OSTRACE1( "OPEN open new/create\n" );
       
 21024   }else{
       
 21025     ulOpenFlags |= OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW;
       
 21026     OSTRACE1( "OPEN open existing\n" );
       
 21027   }
       
 21028 
       
 21029   if( flags & SQLITE_OPEN_MAIN_DB ){
       
 21030     ulOpenMode |= OPEN_SHARE_DENYNONE;
       
 21031     OSTRACE1( "OPEN share read/write\n" );
       
 21032   }else{
       
 21033     ulOpenMode |= OPEN_SHARE_DENYWRITE;
       
 21034     OSTRACE1( "OPEN share read only\n" );
       
 21035   }
       
 21036 
       
 21037   if( flags & SQLITE_OPEN_DELETEONCLOSE ){
       
 21038     char pathUtf8[CCHMAXPATH];
       
 21039 #ifdef NDEBUG /* when debugging we want to make sure it is deleted */
       
 21040     ulFileAttribute = FILE_HIDDEN;
       
 21041 #endif
       
 21042     os2FullPathname( pVfs, zName, CCHMAXPATH, pathUtf8 );
       
 21043     pFile->pathToDel = convertUtf8PathToCp( pathUtf8 );
       
 21044     OSTRACE1( "OPEN hidden/delete on close file attributes\n" );
       
 21045   }else{
       
 21046     pFile->pathToDel = NULL;
       
 21047     OSTRACE1( "OPEN normal file attribute\n" );
       
 21048   }
       
 21049 
       
 21050   /* always open in random access mode for possibly better speed */
       
 21051   ulOpenMode |= OPEN_FLAGS_RANDOM;
       
 21052   ulOpenMode |= OPEN_FLAGS_FAIL_ON_ERROR;
       
 21053   ulOpenMode |= OPEN_FLAGS_NOINHERIT;
       
 21054 
       
 21055   zNameCp = convertUtf8PathToCp( zName );
       
 21056   rc = DosOpen( (PSZ)zNameCp,
       
 21057                 &h,
       
 21058                 &ulAction,
       
 21059                 0L,
       
 21060                 ulFileAttribute,
       
 21061                 ulOpenFlags,
       
 21062                 ulOpenMode,
       
 21063                 (PEAOP2)NULL );
       
 21064   free( zNameCp );
       
 21065   if( rc != NO_ERROR ){
       
 21066     OSTRACE7( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulAttr=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
       
 21067               rc, zName, ulAction, ulFileAttribute, ulOpenFlags, ulOpenMode );
       
 21068     if( pFile->pathToDel )
       
 21069       free( pFile->pathToDel );
       
 21070     pFile->pathToDel = NULL;
       
 21071     if( flags & SQLITE_OPEN_READWRITE ){
       
 21072       OSTRACE2( "OPEN %d Invalid handle\n", ((flags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE) );
       
 21073       return os2Open( pVfs, zName, id,
       
 21074                       ((flags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE),
       
 21075                       pOutFlags );
       
 21076     }else{
       
 21077       return SQLITE_CANTOPEN;
       
 21078     }
       
 21079   }
       
 21080 
       
 21081   if( pOutFlags ){
       
 21082     *pOutFlags = flags & SQLITE_OPEN_READWRITE ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
       
 21083   }
       
 21084 
       
 21085   pFile->pMethod = &os2IoMethod;
       
 21086   pFile->h = h;
       
 21087   OpenCounter(+1);
       
 21088   OSTRACE3( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags );
       
 21089   return SQLITE_OK;
       
 21090 }
       
 21091 
       
 21092 /*
       
 21093 ** Delete the named file.
       
 21094 */
       
 21095 static int os2Delete(
       
 21096   sqlite3_vfs *pVfs,                     /* Not used on os2 */
       
 21097   const char *zFilename,                 /* Name of file to delete */
       
 21098   int syncDir                            /* Not used on os2 */
       
 21099 ){
       
 21100   APIRET rc = NO_ERROR;
       
 21101   char *zFilenameCp = convertUtf8PathToCp( zFilename );
       
 21102   SimulateIOError( return SQLITE_IOERR_DELETE );
       
 21103   rc = DosDelete( (PSZ)zFilenameCp );
       
 21104   free( zFilenameCp );
       
 21105   OSTRACE2( "DELETE \"%s\"\n", zFilename );
       
 21106   return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_DELETE;
       
 21107 }
       
 21108 
       
 21109 /*
       
 21110 ** Check the existance and status of a file.
       
 21111 */
       
 21112 static int os2Access(
       
 21113   sqlite3_vfs *pVfs,        /* Not used on os2 */
       
 21114   const char *zFilename,    /* Name of file to check */
       
 21115   int flags,                /* Type of test to make on this file */
       
 21116   int *pOut                 /* Write results here */
       
 21117 ){
       
 21118   FILESTATUS3 fsts3ConfigInfo;
       
 21119   APIRET rc = NO_ERROR;
       
 21120   char *zFilenameCp = convertUtf8PathToCp( zFilename );
       
 21121 
       
 21122   memset( &fsts3ConfigInfo, 0, sizeof(fsts3ConfigInfo) );
       
 21123   rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
       
 21124                          &fsts3ConfigInfo, sizeof(FILESTATUS3) );
       
 21125   free( zFilenameCp );
       
 21126   OSTRACE4( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
       
 21127             fsts3ConfigInfo.attrFile, flags, rc );
       
 21128   switch( flags ){
       
 21129     case SQLITE_ACCESS_READ:
       
 21130     case SQLITE_ACCESS_EXISTS:
       
 21131       rc = (rc == NO_ERROR);
       
 21132       OSTRACE3( "ACCESS %s access of read and exists  rc=%d\n", zFilename, rc );
       
 21133       break;
       
 21134     case SQLITE_ACCESS_READWRITE:
       
 21135       rc = (rc == NO_ERROR) && ( (fsts3ConfigInfo.attrFile & FILE_READONLY) == 0 );
       
 21136       OSTRACE3( "ACCESS %s access of read/write  rc=%d\n", zFilename, rc );
       
 21137       break;
       
 21138     default:
       
 21139       assert( !"Invalid flags argument" );
       
 21140   }
       
 21141   *pOut = rc;
       
 21142   return SQLITE_OK;
       
 21143 }
       
 21144 
       
 21145 
       
 21146 #ifndef SQLITE_OMIT_LOAD_EXTENSION
       
 21147 /*
       
 21148 ** Interfaces for opening a shared library, finding entry points
       
 21149 ** within the shared library, and closing the shared library.
       
 21150 */
       
 21151 /*
       
 21152 ** Interfaces for opening a shared library, finding entry points
       
 21153 ** within the shared library, and closing the shared library.
       
 21154 */
       
 21155 static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){
       
 21156   UCHAR loadErr[256];
       
 21157   HMODULE hmod;
       
 21158   APIRET rc;
       
 21159   char *zFilenameCp = convertUtf8PathToCp(zFilename);
       
 21160   rc = DosLoadModule((PSZ)loadErr, sizeof(loadErr), zFilenameCp, &hmod);
       
 21161   free(zFilenameCp);
       
 21162   return rc != NO_ERROR ? 0 : (void*)hmod;
       
 21163 }
       
 21164 /*
       
 21165 ** A no-op since the error code is returned on the DosLoadModule call.
       
 21166 ** os2Dlopen returns zero if DosLoadModule is not successful.
       
 21167 */
       
 21168 static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
       
 21169 /* no-op */
       
 21170 }
       
 21171 static void *os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
       
 21172   PFN pfn;
       
 21173   APIRET rc;
       
 21174   rc = DosQueryProcAddr((HMODULE)pHandle, 0L, zSymbol, &pfn);
       
 21175   if( rc != NO_ERROR ){
       
 21176     /* if the symbol itself was not found, search again for the same
       
 21177      * symbol with an extra underscore, that might be needed depending
       
 21178      * on the calling convention */
       
 21179     char _zSymbol[256] = "_";
       
 21180     strncat(_zSymbol, zSymbol, 255);
       
 21181     rc = DosQueryProcAddr((HMODULE)pHandle, 0L, _zSymbol, &pfn);
       
 21182   }
       
 21183   return rc != NO_ERROR ? 0 : (void*)pfn;
       
 21184 }
       
 21185 static void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){
       
 21186   DosFreeModule((HMODULE)pHandle);
       
 21187 }
       
 21188 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
       
 21189   #define os2DlOpen 0
       
 21190   #define os2DlError 0
       
 21191   #define os2DlSym 0
       
 21192   #define os2DlClose 0
       
 21193 #endif
       
 21194 
       
 21195 
       
 21196 /*
       
 21197 ** Write up to nBuf bytes of randomness into zBuf.
       
 21198 */
       
 21199 static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
       
 21200   int n = 0;
       
 21201 #if defined(SQLITE_TEST)
       
 21202   n = nBuf;
       
 21203   memset(zBuf, 0, nBuf);
       
 21204 #else
       
 21205   int sizeofULong = sizeof(ULONG);
       
 21206   if( (int)sizeof(DATETIME) <= nBuf - n ){
       
 21207     DATETIME x;
       
 21208     DosGetDateTime(&x);
       
 21209     memcpy(&zBuf[n], &x, sizeof(x));
       
 21210     n += sizeof(x);
       
 21211   }
       
 21212 
       
 21213   if( sizeofULong <= nBuf - n ){
       
 21214     PPIB ppib;
       
 21215     DosGetInfoBlocks(NULL, &ppib);
       
 21216     memcpy(&zBuf[n], &ppib->pib_ulpid, sizeofULong);
       
 21217     n += sizeofULong;
       
 21218   }
       
 21219 
       
 21220   if( sizeofULong <= nBuf - n ){
       
 21221     PTIB ptib;
       
 21222     DosGetInfoBlocks(&ptib, NULL);
       
 21223     memcpy(&zBuf[n], &ptib->tib_ptib2->tib2_ultid, sizeofULong);
       
 21224     n += sizeofULong;
       
 21225   }
       
 21226 
       
 21227   /* if we still haven't filled the buffer yet the following will */
       
 21228   /* grab everything once instead of making several calls for a single item */
       
 21229   if( sizeofULong <= nBuf - n ){
       
 21230     ULONG ulSysInfo[QSV_MAX];
       
 21231     DosQuerySysInfo(1L, QSV_MAX, ulSysInfo, sizeofULong * QSV_MAX);
       
 21232 
       
 21233     memcpy(&zBuf[n], &ulSysInfo[QSV_MS_COUNT - 1], sizeofULong);
       
 21234     n += sizeofULong;
       
 21235 
       
 21236     if( sizeofULong <= nBuf - n ){
       
 21237       memcpy(&zBuf[n], &ulSysInfo[QSV_TIMER_INTERVAL - 1], sizeofULong);
       
 21238       n += sizeofULong;
       
 21239     }
       
 21240     if( sizeofULong <= nBuf - n ){
       
 21241       memcpy(&zBuf[n], &ulSysInfo[QSV_TIME_LOW - 1], sizeofULong);
       
 21242       n += sizeofULong;
       
 21243     }
       
 21244     if( sizeofULong <= nBuf - n ){
       
 21245       memcpy(&zBuf[n], &ulSysInfo[QSV_TIME_HIGH - 1], sizeofULong);
       
 21246       n += sizeofULong;
       
 21247     }
       
 21248     if( sizeofULong <= nBuf - n ){
       
 21249       memcpy(&zBuf[n], &ulSysInfo[QSV_TOTAVAILMEM - 1], sizeofULong);
       
 21250       n += sizeofULong;
       
 21251     }
       
 21252   }
       
 21253 #endif
       
 21254 
       
 21255   return n;
       
 21256 }
       
 21257 
       
 21258 /*
       
 21259 ** Sleep for a little while.  Return the amount of time slept.
       
 21260 ** The argument is the number of microseconds we want to sleep.
       
 21261 ** The return value is the number of microseconds of sleep actually
       
 21262 ** requested from the underlying operating system, a number which
       
 21263 ** might be greater than or equal to the argument, but not less
       
 21264 ** than the argument.
       
 21265 */
       
 21266 static int os2Sleep( sqlite3_vfs *pVfs, int microsec ){
       
 21267   DosSleep( (microsec/1000) );
       
 21268   return microsec;
       
 21269 }
       
 21270 
       
 21271 /*
       
 21272 ** The following variable, if set to a non-zero value, becomes the result
       
 21273 ** returned from sqlite3OsCurrentTime().  This is used for testing.
       
 21274 */
       
 21275 #ifdef SQLITE_TEST
       
 21276 SQLITE_API int sqlite3_current_time = 0;
       
 21277 #endif
       
 21278 
       
 21279 /*
       
 21280 ** Find the current time (in Universal Coordinated Time).  Write the
       
 21281 ** current time and date as a Julian Day number into *prNow and
       
 21282 ** return 0.  Return 1 if the time and date cannot be found.
       
 21283 */
       
 21284 int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
       
 21285   double now;
       
 21286   SHORT minute; /* needs to be able to cope with negative timezone offset */
       
 21287   USHORT second, hour,
       
 21288          day, month, year;
       
 21289   DATETIME dt;
       
 21290   DosGetDateTime( &dt );
       
 21291   second = (USHORT)dt.seconds;
       
 21292   minute = (SHORT)dt.minutes + dt.timezone;
       
 21293   hour = (USHORT)dt.hours;
       
 21294   day = (USHORT)dt.day;
       
 21295   month = (USHORT)dt.month;
       
 21296   year = (USHORT)dt.year;
       
 21297 
       
 21298   /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
       
 21299      http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c */
       
 21300   /* Calculate the Julian days */
       
 21301   now = day - 32076 +
       
 21302     1461*(year + 4800 + (month - 14)/12)/4 +
       
 21303     367*(month - 2 - (month - 14)/12*12)/12 -
       
 21304     3*((year + 4900 + (month - 14)/12)/100)/4;
       
 21305 
       
 21306   /* Add the fractional hours, mins and seconds */
       
 21307   now += (hour + 12.0)/24.0;
       
 21308   now += minute/1440.0;
       
 21309   now += second/86400.0;
       
 21310   *prNow = now;
       
 21311 #ifdef SQLITE_TEST
       
 21312   if( sqlite3_current_time ){
       
 21313     *prNow = sqlite3_current_time/86400.0 + 2440587.5;
       
 21314   }
       
 21315 #endif
       
 21316   return 0;
       
 21317 }
       
 21318 
       
 21319 static int os2GetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
       
 21320   return 0;
       
 21321 }
       
 21322 
       
 21323 /*
       
 21324 ** Initialize and deinitialize the operating system interface.
       
 21325 */
       
 21326 SQLITE_API int sqlite3_os_init(void){
       
 21327   static sqlite3_vfs os2Vfs = {
       
 21328     1,                 /* iVersion */
       
 21329     sizeof(os2File),   /* szOsFile */
       
 21330     CCHMAXPATH,        /* mxPathname */
       
 21331     0,                 /* pNext */
       
 21332     "os2",             /* zName */
       
 21333     0,                 /* pAppData */
       
 21334 
       
 21335     os2Open,           /* xOpen */
       
 21336     os2Delete,         /* xDelete */
       
 21337     os2Access,         /* xAccess */
       
 21338     os2FullPathname,   /* xFullPathname */
       
 21339     os2DlOpen,         /* xDlOpen */
       
 21340     os2DlError,        /* xDlError */
       
 21341     os2DlSym,          /* xDlSym */
       
 21342     os2DlClose,        /* xDlClose */
       
 21343     os2Randomness,     /* xRandomness */
       
 21344     os2Sleep,          /* xSleep */
       
 21345     os2CurrentTime,    /* xCurrentTime */
       
 21346     os2GetLastError    /* xGetLastError */
       
 21347   };
       
 21348   sqlite3_vfs_register(&os2Vfs, 1);
       
 21349   initUconvObjects();
       
 21350   return SQLITE_OK;
       
 21351 }
       
 21352 SQLITE_API int sqlite3_os_end(void){
       
 21353   freeUconvObjects();
       
 21354   return SQLITE_OK;
       
 21355 }
       
 21356 
       
 21357 #endif /* SQLITE_OS_OS2 */
       
 21358 
       
 21359 /************** End of os_os2.c **********************************************/
       
 21360 /************** Begin file os_unix.c *****************************************/
       
 21361 /*
       
 21362 ** 2004 May 22
       
 21363 **
       
 21364 ** The author disclaims copyright to this source code.  In place of
       
 21365 ** a legal notice, here is a blessing:
       
 21366 **
       
 21367 **    May you do good and not evil.
       
 21368 **    May you find forgiveness for yourself and forgive others.
       
 21369 **    May you share freely, never taking more than you give.
       
 21370 **
       
 21371 ******************************************************************************
       
 21372 **
       
 21373 ** This file contains the VFS implementation for unix-like operating systems
       
 21374 ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
       
 21375 **
       
 21376 ** There are actually several different VFS implementations in this file.
       
 21377 ** The differences are in the way that file locking is done.  The default
       
 21378 ** implementation uses Posix Advisory Locks.  Alternative implementations
       
 21379 ** use flock(), dot-files, various proprietary locking schemas, or simply
       
 21380 ** skip locking all together.
       
 21381 **
       
 21382 ** This source file is organized into divisions where the logic for various
       
 21383 ** subfunctions is contained within the appropriate division.  PLEASE
       
 21384 ** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed
       
 21385 ** in the correct division and should be clearly labeled.
       
 21386 **
       
 21387 ** The layout of divisions is as follows:
       
 21388 **
       
 21389 **   *  General-purpose declarations and utility functions.
       
 21390 **   *  Unique file ID logic used by VxWorks.
       
 21391 **   *  Various locking primitive implementations (all except proxy locking):
       
 21392 **      + for Posix Advisory Locks
       
 21393 **      + for no-op locks
       
 21394 **      + for dot-file locks
       
 21395 **      + for flock() locking
       
 21396 **      + for named semaphore locks (VxWorks only)
       
 21397 **      + for AFP filesystem locks (MacOSX only)
       
 21398 **   *  sqlite3_file methods not associated with locking.
       
 21399 **   *  Definitions of sqlite3_io_methods objects for all locking
       
 21400 **      methods plus "finder" functions for each locking method.
       
 21401 **   *  sqlite3_vfs method implementations.
       
 21402 **   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
       
 21403 **   *  Definitions of sqlite3_vfs objects for all locking methods
       
 21404 **      plus implementations of sqlite3_os_init() and sqlite3_os_end().
       
 21405 */
       
 21406 #if SQLITE_OS_UNIX              /* This file is used on unix only */
       
 21407 
       
 21408 #include <qconfig.h>
       
 21409 
       
 21410 /*
       
 21411 ** There are various methods for file locking used for concurrency
       
 21412 ** control:
       
 21413 **
       
 21414 **   1. POSIX locking (the default),
       
 21415 **   2. No locking,
       
 21416 **   3. Dot-file locking,
       
 21417 **   4. flock() locking,
       
 21418 **   5. AFP locking (OSX only),
       
 21419 **   6. Named POSIX semaphores (VXWorks only),
       
 21420 **   7. proxy locking. (OSX only)
       
 21421 **
       
 21422 ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
       
 21423 ** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
       
 21424 ** selection of the appropriate locking style based on the filesystem
       
 21425 ** where the database is located.  
       
 21426 */
       
 21427 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
       
 21428 #  if defined(__APPLE__)
       
 21429 #    define SQLITE_ENABLE_LOCKING_STYLE 1
       
 21430 #  else
       
 21431 #    define SQLITE_ENABLE_LOCKING_STYLE 0
       
 21432 #  endif
       
 21433 #endif
       
 21434 
       
 21435 /*
       
 21436 ** Define the OS_VXWORKS pre-processor macro to 1 if building on 
       
 21437 ** vxworks, or 0 otherwise.
       
 21438 */
       
 21439 #ifndef OS_VXWORKS
       
 21440 #  if defined(__RTP__) || defined(_WRS_KERNEL)
       
 21441 #    define OS_VXWORKS 1
       
 21442 #  else
       
 21443 #    define OS_VXWORKS 0
       
 21444 #  endif
       
 21445 #endif
       
 21446 
       
 21447 /*
       
 21448 ** These #defines should enable >2GB file support on Posix if the
       
 21449 ** underlying operating system supports it.  If the OS lacks
       
 21450 ** large file support, these should be no-ops.
       
 21451 **
       
 21452 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
       
 21453 ** on the compiler command line.  This is necessary if you are compiling
       
 21454 ** on a recent machine (ex: RedHat 7.2) but you want your code to work
       
 21455 ** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
       
 21456 ** without this option, LFS is enable.  But LFS does not exist in the kernel
       
 21457 ** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
       
 21458 ** portability you should omit LFS.
       
 21459 **
       
 21460 ** The previous paragraph was written in 2005.  (This paragraph is written
       
 21461 ** on 2008-11-28.) These days, all Linux kernels support large files, so
       
 21462 ** you should probably leave LFS enabled.  But some embedded platforms might
       
 21463 ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
       
 21464 */
       
 21465 #ifndef SQLITE_DISABLE_LFS
       
 21466 # define _LARGE_FILE       1
       
 21467 # ifndef _FILE_OFFSET_BITS
       
 21468 #   define _FILE_OFFSET_BITS 64
       
 21469 # endif
       
 21470 # define _LARGEFILE_SOURCE 1
       
 21471 #endif
       
 21472 
       
 21473 /*
       
 21474 ** standard include files.
       
 21475 */
       
 21476 #include <sys/types.h>
       
 21477 #include <sys/stat.h>
       
 21478 #include <fcntl.h>
       
 21479 #include <unistd.h>
       
 21480 #ifdef VXWORKS
       
 21481 # include <sys/times.h>
       
 21482 #else
       
 21483 # include <sys/time.h>
       
 21484 #endif
       
 21485 #include <errno.h>
       
 21486 
       
 21487 #if SQLITE_ENABLE_LOCKING_STYLE
       
 21488 # include <sys/ioctl.h>
       
 21489 # if OS_VXWORKS
       
 21490 #  include <semaphore.h>
       
 21491 #  include <limits.h>
       
 21492 # else
       
 21493 #  include <sys/file.h>
       
 21494 #  include <sys/param.h>
       
 21495 #  include <sys/mount.h>
       
 21496 # endif
       
 21497 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
       
 21498 
       
 21499 /*
       
 21500 ** If we are to be thread-safe, include the pthreads header and define
       
 21501 ** the SQLITE_UNIX_THREADS macro.
       
 21502 */
       
 21503 #ifndef QT_NO_THREAD
       
 21504 # define SQLITE_UNIX_THREADS 1
       
 21505 #endif
       
 21506 
       
 21507 /*
       
 21508 ** Default permissions when creating a new file
       
 21509 */
       
 21510 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
       
 21511 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
       
 21512 #endif
       
 21513 
       
 21514 /*
       
 21515  ** Default permissions when creating auto proxy dir
       
 21516  */
       
 21517 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
       
 21518 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
       
 21519 #endif
       
 21520 
       
 21521 /*
       
 21522 ** Maximum supported path-length.
       
 21523 */
       
 21524 #define MAX_PATHNAME 512
       
 21525 
       
 21526 /*
       
 21527 ** Only set the lastErrno if the error code is a real error and not 
       
 21528 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
       
 21529 */
       
 21530 #define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
       
 21531 
       
 21532 
       
 21533 /*
       
 21534 ** Sometimes, after a file handle is closed by SQLite, the file descriptor
       
 21535 ** cannot be closed immediately. In these cases, instances of the following
       
 21536 ** structure are used to store the file descriptor while waiting for an
       
 21537 ** opportunity to either close or reuse it.
       
 21538 */
       
 21539 typedef struct UnixUnusedFd UnixUnusedFd;
       
 21540 struct UnixUnusedFd {
       
 21541   int fd;                   /* File descriptor to close */
       
 21542   int flags;                /* Flags this file descriptor was opened with */
       
 21543   UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */
       
 21544 };
       
 21545 
       
 21546 /*
       
 21547 ** The unixFile structure is subclass of sqlite3_file specific to the unix
       
 21548 ** VFS implementations.
       
 21549 */
       
 21550 typedef struct unixFile unixFile;
       
 21551 struct unixFile {
       
 21552   sqlite3_io_methods const *pMethod;  /* Always the first entry */
       
 21553   struct unixOpenCnt *pOpen;       /* Info about all open fd's on this inode */
       
 21554   struct unixLockInfo *pLock;      /* Info about locks on this inode */
       
 21555   int h;                           /* The file descriptor */
       
 21556   int dirfd;                       /* File descriptor for the directory */
       
 21557   unsigned char locktype;          /* The type of lock held on this fd */
       
 21558   int lastErrno;                   /* The unix errno from the last I/O error */
       
 21559   void *lockingContext;            /* Locking style specific state */
       
 21560   UnixUnusedFd *pUnused;           /* Pre-allocated UnixUnusedFd */
       
 21561   int fileFlags;                   /* Miscellanous flags */
       
 21562 #if SQLITE_ENABLE_LOCKING_STYLE
       
 21563   int openFlags;                   /* The flags specified at open() */
       
 21564 #endif
       
 21565 #if SQLITE_THREADSAFE && defined(__linux__)
       
 21566   pthread_t tid;                   /* The thread that "owns" this unixFile */
       
 21567 #endif
       
 21568 #if OS_VXWORKS
       
 21569   int isDelete;                    /* Delete on close if true */
       
 21570   struct vxworksFileId *pId;       /* Unique file ID */
       
 21571 #endif
       
 21572 #ifndef NDEBUG
       
 21573   /* The next group of variables are used to track whether or not the
       
 21574   ** transaction counter in bytes 24-27 of database files are updated
       
 21575   ** whenever any part of the database changes.  An assertion fault will
       
 21576   ** occur if a file is updated without also updating the transaction
       
 21577   ** counter.  This test is made to avoid new problems similar to the
       
 21578   ** one described by ticket #3584. 
       
 21579   */
       
 21580   unsigned char transCntrChng;   /* True if the transaction counter changed */
       
 21581   unsigned char dbUpdate;        /* True if any part of database file changed */
       
 21582   unsigned char inNormalWrite;   /* True if in a normal write operation */
       
 21583 #endif
       
 21584 #ifdef SQLITE_TEST
       
 21585   /* In test mode, increase the size of this structure a bit so that 
       
 21586   ** it is larger than the struct CrashFile defined in test6.c.
       
 21587   */
       
 21588   char aPadding[32];
       
 21589 #endif
       
 21590 };
       
 21591 
       
 21592 /*
       
 21593 ** The following macros define bits in unixFile.fileFlags
       
 21594 */
       
 21595 #define SQLITE_WHOLE_FILE_LOCKING  0x0001   /* Use whole-file locking */
       
 21596 
       
 21597 /*
       
 21598 ** Include code that is common to all os_*.c files
       
 21599 */
       
 21600 /************** Include os_common.h in the middle of os_unix.c ***************/
       
 21601 /************** Begin file os_common.h ***************************************/
       
 21602 /*
       
 21603 ** 2004 May 22
       
 21604 **
       
 21605 ** The author disclaims copyright to this source code.  In place of
       
 21606 ** a legal notice, here is a blessing:
       
 21607 **
       
 21608 **    May you do good and not evil.
       
 21609 **    May you find forgiveness for yourself and forgive others.
       
 21610 **    May you share freely, never taking more than you give.
       
 21611 **
       
 21612 ******************************************************************************
       
 21613 **
       
 21614 ** This file contains macros and a little bit of code that is common to
       
 21615 ** all of the platform-specific files (os_*.c) and is #included into those
       
 21616 ** files.
       
 21617 **
       
 21618 ** This file should be #included by the os_*.c files only.  It is not a
       
 21619 ** general purpose header file.
       
 21620 **
       
 21621 ** $Id: os_common.h,v 1.38 2009/02/24 18:40:50 danielk1977 Exp $
       
 21622 */
       
 21623 #ifndef _OS_COMMON_H_
       
 21624 #define _OS_COMMON_H_
       
 21625 
       
 21626 /*
       
 21627 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
       
 21628 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
       
 21629 ** switch.  The following code should catch this problem at compile-time.
       
 21630 */
       
 21631 #ifdef MEMORY_DEBUG
       
 21632 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
       
 21633 #endif
       
 21634 
       
 21635 #ifdef SQLITE_DEBUG
       
 21636 SQLITE_PRIVATE int sqlite3OSTrace = 0;
       
 21637 #define OSTRACE1(X)         if( sqlite3OSTrace ) sqlite3DebugPrintf(X)
       
 21638 #define OSTRACE2(X,Y)       if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y)
       
 21639 #define OSTRACE3(X,Y,Z)     if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z)
       
 21640 #define OSTRACE4(X,Y,Z,A)   if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A)
       
 21641 #define OSTRACE5(X,Y,Z,A,B) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A,B)
       
 21642 #define OSTRACE6(X,Y,Z,A,B,C) \
       
 21643     if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
       
 21644 #define OSTRACE7(X,Y,Z,A,B,C,D) \
       
 21645     if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
       
 21646 #else
       
 21647 #define OSTRACE1(X)
       
 21648 #define OSTRACE2(X,Y)
       
 21649 #define OSTRACE3(X,Y,Z)
       
 21650 #define OSTRACE4(X,Y,Z,A)
       
 21651 #define OSTRACE5(X,Y,Z,A,B)
       
 21652 #define OSTRACE6(X,Y,Z,A,B,C)
       
 21653 #define OSTRACE7(X,Y,Z,A,B,C,D)
       
 21654 #endif
       
 21655 
       
 21656 /*
       
 21657 ** Macros for performance tracing.  Normally turned off.  Only works
       
 21658 ** on i486 hardware.
       
 21659 */
       
 21660 #ifdef SQLITE_PERFORMANCE_TRACE
       
 21661 
       
 21662 /* 
       
 21663 ** hwtime.h contains inline assembler code for implementing 
       
 21664 ** high-performance timing routines.
       
 21665 */
       
 21666 /************** Include hwtime.h in the middle of os_common.h ****************/
       
 21667 /************** Begin file hwtime.h ******************************************/
       
 21668 /*
       
 21669 ** 2008 May 27
       
 21670 **
       
 21671 ** The author disclaims copyright to this source code.  In place of
       
 21672 ** a legal notice, here is a blessing:
       
 21673 **
       
 21674 **    May you do good and not evil.
       
 21675 **    May you find forgiveness for yourself and forgive others.
       
 21676 **    May you share freely, never taking more than you give.
       
 21677 **
       
 21678 ******************************************************************************
       
 21679 **
       
 21680 ** This file contains inline asm code for retrieving "high-performance"
       
 21681 ** counters for x86 class CPUs.
       
 21682 **
       
 21683 ** $Id: hwtime.h,v 1.3 2008/08/01 14:33:15 shane Exp $
       
 21684 */
       
 21685 #ifndef _HWTIME_H_
       
 21686 #define _HWTIME_H_
       
 21687 
       
 21688 /*
       
 21689 ** The following routine only works on pentium-class (or newer) processors.
       
 21690 ** It uses the RDTSC opcode to read the cycle count value out of the
       
 21691 ** processor and returns that value.  This can be used for high-res
       
 21692 ** profiling.
       
 21693 */
       
 21694 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
       
 21695       (defined(i386) || defined(__i386__) || defined(_M_IX86))
       
 21696 
       
 21697   #if defined(__GNUC__)
       
 21698 
       
 21699   __inline__ sqlite_uint64 sqlite3Hwtime(void){
       
 21700      unsigned int lo, hi;
       
 21701      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
       
 21702      return (sqlite_uint64)hi << 32 | lo;
       
 21703   }
       
 21704 
       
 21705   #elif defined(_MSC_VER)
       
 21706 
       
 21707   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
       
 21708      __asm {
       
 21709         rdtsc
       
 21710         ret       ; return value at EDX:EAX
       
 21711      }
       
 21712   }
       
 21713 
       
 21714   #endif
       
 21715 
       
 21716 #elif (defined(__GNUC__) && defined(__x86_64__))
       
 21717 
       
 21718   __inline__ sqlite_uint64 sqlite3Hwtime(void){
       
 21719       unsigned long val;
       
 21720       __asm__ __volatile__ ("rdtsc" : "=A" (val));
       
 21721       return val;
       
 21722   }
       
 21723  
       
 21724 #elif (defined(__GNUC__) && defined(__ppc__))
       
 21725 
       
 21726   __inline__ sqlite_uint64 sqlite3Hwtime(void){
       
 21727       unsigned long long retval;
       
 21728       unsigned long junk;
       
 21729       __asm__ __volatile__ ("\n\
       
 21730           1:      mftbu   %1\n\
       
 21731                   mftb    %L0\n\
       
 21732                   mftbu   %0\n\
       
 21733                   cmpw    %0,%1\n\
       
 21734                   bne     1b"
       
 21735                   : "=r" (retval), "=r" (junk));
       
 21736       return retval;
       
 21737   }
       
 21738 
       
 21739 #else
       
 21740 
       
 21741   #error Need implementation of sqlite3Hwtime() for your platform.
       
 21742 
       
 21743   /*
       
 21744   ** To compile without implementing sqlite3Hwtime() for your platform,
       
 21745   ** you can remove the above #error and use the following
       
 21746   ** stub function.  You will lose timing support for many
       
 21747   ** of the debugging and testing utilities, but it should at
       
 21748   ** least compile and run.
       
 21749   */
       
 21750 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
       
 21751 
       
 21752 #endif
       
 21753 
       
 21754 #endif /* !defined(_HWTIME_H_) */
       
 21755 
       
 21756 /************** End of hwtime.h **********************************************/
       
 21757 /************** Continuing where we left off in os_common.h ******************/
       
 21758 
       
 21759 static sqlite_uint64 g_start;
       
 21760 static sqlite_uint64 g_elapsed;
       
 21761 #define TIMER_START       g_start=sqlite3Hwtime()
       
 21762 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
       
 21763 #define TIMER_ELAPSED     g_elapsed
       
 21764 #else
       
 21765 #define TIMER_START
       
 21766 #define TIMER_END
       
 21767 #define TIMER_ELAPSED     ((sqlite_uint64)0)
       
 21768 #endif
       
 21769 
       
 21770 /*
       
 21771 ** If we compile with the SQLITE_TEST macro set, then the following block
       
 21772 ** of code will give us the ability to simulate a disk I/O error.  This
       
 21773 ** is used for testing the I/O recovery logic.
       
 21774 */
       
 21775 #ifdef SQLITE_TEST
       
 21776 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
       
 21777 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
       
 21778 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
       
 21779 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
       
 21780 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
       
 21781 SQLITE_API int sqlite3_diskfull_pending = 0;
       
 21782 SQLITE_API int sqlite3_diskfull = 0;
       
 21783 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
       
 21784 #define SimulateIOError(CODE)  \
       
 21785   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
       
 21786        || sqlite3_io_error_pending-- == 1 )  \
       
 21787               { local_ioerr(); CODE; }
       
 21788 static void local_ioerr(){
       
 21789   IOTRACE(("IOERR\n"));
       
 21790   sqlite3_io_error_hit++;
       
 21791   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
       
 21792 }
       
 21793 #define SimulateDiskfullError(CODE) \
       
 21794    if( sqlite3_diskfull_pending ){ \
       
 21795      if( sqlite3_diskfull_pending == 1 ){ \
       
 21796        local_ioerr(); \
       
 21797        sqlite3_diskfull = 1; \
       
 21798        sqlite3_io_error_hit = 1; \
       
 21799        CODE; \
       
 21800      }else{ \
       
 21801        sqlite3_diskfull_pending--; \
       
 21802      } \
       
 21803    }
       
 21804 #else
       
 21805 #define SimulateIOErrorBenign(X)
       
 21806 #define SimulateIOError(A)
       
 21807 #define SimulateDiskfullError(A)
       
 21808 #endif
       
 21809 
       
 21810 /*
       
 21811 ** When testing, keep a count of the number of open files.
       
 21812 */
       
 21813 #ifdef SQLITE_TEST
       
 21814 SQLITE_API int sqlite3_open_file_count = 0;
       
 21815 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
       
 21816 #else
       
 21817 #define OpenCounter(X)
       
 21818 #endif
       
 21819 
       
 21820 #endif /* !defined(_OS_COMMON_H_) */
       
 21821 
       
 21822 /************** End of os_common.h *******************************************/
       
 21823 /************** Continuing where we left off in os_unix.c ********************/
       
 21824 
       
 21825 /*
       
 21826 ** Define various macros that are missing from some systems.
       
 21827 */
       
 21828 #ifndef O_LARGEFILE
       
 21829 # define O_LARGEFILE 0
       
 21830 #endif
       
 21831 #ifdef SQLITE_DISABLE_LFS
       
 21832 # undef O_LARGEFILE
       
 21833 # define O_LARGEFILE 0
       
 21834 #endif
       
 21835 #ifndef O_NOFOLLOW
       
 21836 # define O_NOFOLLOW 0
       
 21837 #endif
       
 21838 #ifndef O_BINARY
       
 21839 # define O_BINARY 0
       
 21840 #endif
       
 21841 
       
 21842 /*
       
 21843 ** The DJGPP compiler environment looks mostly like Unix, but it
       
 21844 ** lacks the fcntl() system call.  So redefine fcntl() to be something
       
 21845 ** that always succeeds.  This means that locking does not occur under
       
 21846 ** DJGPP.  But it is DOS - what did you expect?
       
 21847 */
       
 21848 #ifdef __DJGPP__
       
 21849 # define fcntl(A,B,C) 0
       
 21850 #endif
       
 21851 
       
 21852 /*
       
 21853 ** The threadid macro resolves to the thread-id or to 0.  Used for
       
 21854 ** testing and debugging only.
       
 21855 */
       
 21856 #if SQLITE_THREADSAFE
       
 21857 #define threadid pthread_self()
       
 21858 #else
       
 21859 #define threadid 0
       
 21860 #endif
       
 21861 
       
 21862 
       
 21863 /*
       
 21864 ** Helper functions to obtain and relinquish the global mutex. The
       
 21865 ** global mutex is used to protect the unixOpenCnt, unixLockInfo and
       
 21866 ** vxworksFileId objects used by this file, all of which may be 
       
 21867 ** shared by multiple threads.
       
 21868 **
       
 21869 ** Function unixMutexHeld() is used to assert() that the global mutex 
       
 21870 ** is held when required. This function is only used as part of assert() 
       
 21871 ** statements. e.g.
       
 21872 **
       
 21873 **   unixEnterMutex()
       
 21874 **     assert( unixMutexHeld() );
       
 21875 **   unixEnterLeave()
       
 21876 */
       
 21877 static void unixEnterMutex(void){
       
 21878   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
       
 21879 }
       
 21880 static void unixLeaveMutex(void){
       
 21881   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
       
 21882 }
       
 21883 #ifdef SQLITE_DEBUG
       
 21884 static int unixMutexHeld(void) {
       
 21885   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
       
 21886 }
       
 21887 #endif
       
 21888 
       
 21889 
       
 21890 #ifdef SQLITE_DEBUG
       
 21891 /*
       
 21892 ** Helper function for printing out trace information from debugging
       
 21893 ** binaries. This returns the string represetation of the supplied
       
 21894 ** integer lock-type.
       
 21895 */
       
 21896 static const char *locktypeName(int locktype){
       
 21897   switch( locktype ){
       
 21898     case NO_LOCK: return "NONE";
       
 21899     case SHARED_LOCK: return "SHARED";
       
 21900     case RESERVED_LOCK: return "RESERVED";
       
 21901     case PENDING_LOCK: return "PENDING";
       
 21902     case EXCLUSIVE_LOCK: return "EXCLUSIVE";
       
 21903   }
       
 21904   return "ERROR";
       
 21905 }
       
 21906 #endif
       
 21907 
       
 21908 #ifdef SQLITE_LOCK_TRACE
       
 21909 /*
       
 21910 ** Print out information about all locking operations.
       
 21911 **
       
 21912 ** This routine is used for troubleshooting locks on multithreaded
       
 21913 ** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
       
 21914 ** command-line option on the compiler.  This code is normally
       
 21915 ** turned off.
       
 21916 */
       
 21917 static int lockTrace(int fd, int op, struct flock *p){
       
 21918   char *zOpName, *zType;
       
 21919   int s;
       
 21920   int savedErrno;
       
 21921   if( op==F_GETLK ){
       
 21922     zOpName = "GETLK";
       
 21923   }else if( op==F_SETLK ){
       
 21924     zOpName = "SETLK";
       
 21925   }else{
       
 21926     s = fcntl(fd, op, p);
       
 21927     sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
       
 21928     return s;
       
 21929   }
       
 21930   if( p->l_type==F_RDLCK ){
       
 21931     zType = "RDLCK";
       
 21932   }else if( p->l_type==F_WRLCK ){
       
 21933     zType = "WRLCK";
       
 21934   }else if( p->l_type==F_UNLCK ){
       
 21935     zType = "UNLCK";
       
 21936   }else{
       
 21937     assert( 0 );
       
 21938   }
       
 21939   assert( p->l_whence==SEEK_SET );
       
 21940   s = fcntl(fd, op, p);
       
 21941   savedErrno = errno;
       
 21942   sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
       
 21943      threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
       
 21944      (int)p->l_pid, s);
       
 21945   if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
       
 21946     struct flock l2;
       
 21947     l2 = *p;
       
 21948     fcntl(fd, F_GETLK, &l2);
       
 21949     if( l2.l_type==F_RDLCK ){
       
 21950       zType = "RDLCK";
       
 21951     }else if( l2.l_type==F_WRLCK ){
       
 21952       zType = "WRLCK";
       
 21953     }else if( l2.l_type==F_UNLCK ){
       
 21954       zType = "UNLCK";
       
 21955     }else{
       
 21956       assert( 0 );
       
 21957     }
       
 21958     sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
       
 21959        zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
       
 21960   }
       
 21961   errno = savedErrno;
       
 21962   return s;
       
 21963 }
       
 21964 #define fcntl lockTrace
       
 21965 #endif /* SQLITE_LOCK_TRACE */
       
 21966 
       
 21967 
       
 21968 
       
 21969 /*
       
 21970 ** This routine translates a standard POSIX errno code into something
       
 21971 ** useful to the clients of the sqlite3 functions.  Specifically, it is
       
 21972 ** intended to translate a variety of "try again" errors into SQLITE_BUSY
       
 21973 ** and a variety of "please close the file descriptor NOW" errors into 
       
 21974 ** SQLITE_IOERR
       
 21975 ** 
       
 21976 ** Errors during initialization of locks, or file system support for locks,
       
 21977 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
       
 21978 */
       
 21979 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
       
 21980   switch (posixError) {
       
 21981   case 0: 
       
 21982     return SQLITE_OK;
       
 21983     
       
 21984   case EAGAIN:
       
 21985   case ETIMEDOUT:
       
 21986   case EBUSY:
       
 21987   case EINTR:
       
 21988   case ENOLCK:  
       
 21989     /* random NFS retry error, unless during file system support 
       
 21990      * introspection, in which it actually means what it says */
       
 21991     return SQLITE_BUSY;
       
 21992     
       
 21993   case EACCES: 
       
 21994     /* EACCES is like EAGAIN during locking operations, but not any other time*/
       
 21995     if( (sqliteIOErr == SQLITE_IOERR_LOCK) || 
       
 21996 	(sqliteIOErr == SQLITE_IOERR_UNLOCK) || 
       
 21997 	(sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
       
 21998 	(sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
       
 21999       return SQLITE_BUSY;
       
 22000     }
       
 22001     /* else fall through */
       
 22002   case EPERM: 
       
 22003     return SQLITE_PERM;
       
 22004     
       
 22005   case EDEADLK:
       
 22006     return SQLITE_IOERR_BLOCKED;
       
 22007     
       
 22008 #if EOPNOTSUPP!=ENOTSUP
       
 22009   case EOPNOTSUPP: 
       
 22010     /* something went terribly awry, unless during file system support 
       
 22011      * introspection, in which it actually means what it says */
       
 22012 #endif
       
 22013 #ifdef ENOTSUP
       
 22014   case ENOTSUP: 
       
 22015     /* invalid fd, unless during file system support introspection, in which 
       
 22016      * it actually means what it says */
       
 22017 #endif
       
 22018   case EIO:
       
 22019   case EBADF:
       
 22020   case EINVAL:
       
 22021   case ENOTCONN:
       
 22022   case ENODEV:
       
 22023   case ENXIO:
       
 22024   case ENOENT:
       
 22025   case ESTALE:
       
 22026   case ENOSYS:
       
 22027     /* these should force the client to close the file and reconnect */
       
 22028     
       
 22029   default: 
       
 22030     return sqliteIOErr;
       
 22031   }
       
 22032 }
       
 22033 
       
 22034 
       
 22035 
       
 22036 /******************************************************************************
       
 22037 ****************** Begin Unique File ID Utility Used By VxWorks ***************
       
 22038 **
       
 22039 ** On most versions of unix, we can get a unique ID for a file by concatenating
       
 22040 ** the device number and the inode number.  But this does not work on VxWorks.
       
 22041 ** On VxWorks, a unique file id must be based on the canonical filename.
       
 22042 **
       
 22043 ** A pointer to an instance of the following structure can be used as a
       
 22044 ** unique file ID in VxWorks.  Each instance of this structure contains
       
 22045 ** a copy of the canonical filename.  There is also a reference count.  
       
 22046 ** The structure is reclaimed when the number of pointers to it drops to
       
 22047 ** zero.
       
 22048 **
       
 22049 ** There are never very many files open at one time and lookups are not
       
 22050 ** a performance-critical path, so it is sufficient to put these
       
 22051 ** structures on a linked list.
       
 22052 */
       
 22053 struct vxworksFileId {
       
 22054   struct vxworksFileId *pNext;  /* Next in a list of them all */
       
 22055   int nRef;                     /* Number of references to this one */
       
 22056   int nName;                    /* Length of the zCanonicalName[] string */
       
 22057   char *zCanonicalName;         /* Canonical filename */
       
 22058 };
       
 22059 
       
 22060 #if OS_VXWORKS
       
 22061 /* 
       
 22062 ** All unique filenames are held on a linked list headed by this
       
 22063 ** variable:
       
 22064 */
       
 22065 static struct vxworksFileId *vxworksFileList = 0;
       
 22066 
       
 22067 /*
       
 22068 ** Simplify a filename into its canonical form
       
 22069 ** by making the following changes:
       
 22070 **
       
 22071 **  * removing any trailing and duplicate /
       
 22072 **  * convert /./ into just /
       
 22073 **  * convert /A/../ where A is any simple name into just /
       
 22074 **
       
 22075 ** Changes are made in-place.  Return the new name length.
       
 22076 **
       
 22077 ** The original filename is in z[0..n-1].  Return the number of
       
 22078 ** characters in the simplified name.
       
 22079 */
       
 22080 static int vxworksSimplifyName(char *z, int n){
       
 22081   int i, j;
       
 22082   while( n>1 && z[n-1]=='/' ){ n--; }
       
 22083   for(i=j=0; i<n; i++){
       
 22084     if( z[i]=='/' ){
       
 22085       if( z[i+1]=='/' ) continue;
       
 22086       if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
       
 22087         i += 1;
       
 22088         continue;
       
 22089       }
       
 22090       if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
       
 22091         while( j>0 && z[j-1]!='/' ){ j--; }
       
 22092         if( j>0 ){ j--; }
       
 22093         i += 2;
       
 22094         continue;
       
 22095       }
       
 22096     }
       
 22097     z[j++] = z[i];
       
 22098   }
       
 22099   z[j] = 0;
       
 22100   return j;
       
 22101 }
       
 22102 
       
 22103 /*
       
 22104 ** Find a unique file ID for the given absolute pathname.  Return
       
 22105 ** a pointer to the vxworksFileId object.  This pointer is the unique
       
 22106 ** file ID.
       
 22107 **
       
 22108 ** The nRef field of the vxworksFileId object is incremented before
       
 22109 ** the object is returned.  A new vxworksFileId object is created
       
 22110 ** and added to the global list if necessary.
       
 22111 **
       
 22112 ** If a memory allocation error occurs, return NULL.
       
 22113 */
       
 22114 static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
       
 22115   struct vxworksFileId *pNew;         /* search key and new file ID */
       
 22116   struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
       
 22117   int n;                              /* Length of zAbsoluteName string */
       
 22118 
       
 22119   assert( zAbsoluteName[0]=='/' );
       
 22120   n = (int)strlen(zAbsoluteName);
       
 22121   pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
       
 22122   if( pNew==0 ) return 0;
       
 22123   pNew->zCanonicalName = (char*)&pNew[1];
       
 22124   memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
       
 22125   n = vxworksSimplifyName(pNew->zCanonicalName, n);
       
 22126 
       
 22127   /* Search for an existing entry that matching the canonical name.
       
 22128   ** If found, increment the reference count and return a pointer to
       
 22129   ** the existing file ID.
       
 22130   */
       
 22131   unixEnterMutex();
       
 22132   for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
       
 22133     if( pCandidate->nName==n 
       
 22134      && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
       
 22135     ){
       
 22136        sqlite3_free(pNew);
       
 22137        pCandidate->nRef++;
       
 22138        unixLeaveMutex();
       
 22139        return pCandidate;
       
 22140     }
       
 22141   }
       
 22142 
       
 22143   /* No match was found.  We will make a new file ID */
       
 22144   pNew->nRef = 1;
       
 22145   pNew->nName = n;
       
 22146   pNew->pNext = vxworksFileList;
       
 22147   vxworksFileList = pNew;
       
 22148   unixLeaveMutex();
       
 22149   return pNew;
       
 22150 }
       
 22151 
       
 22152 /*
       
 22153 ** Decrement the reference count on a vxworksFileId object.  Free
       
 22154 ** the object when the reference count reaches zero.
       
 22155 */
       
 22156 static void vxworksReleaseFileId(struct vxworksFileId *pId){
       
 22157   unixEnterMutex();
       
 22158   assert( pId->nRef>0 );
       
 22159   pId->nRef--;
       
 22160   if( pId->nRef==0 ){
       
 22161     struct vxworksFileId **pp;
       
 22162     for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
       
 22163     assert( *pp==pId );
       
 22164     *pp = pId->pNext;
       
 22165     sqlite3_free(pId);
       
 22166   }
       
 22167   unixLeaveMutex();
       
 22168 }
       
 22169 #endif /* OS_VXWORKS */
       
 22170 /*************** End of Unique File ID Utility Used By VxWorks ****************
       
 22171 ******************************************************************************/
       
 22172 
       
 22173 
       
 22174 /******************************************************************************
       
 22175 *************************** Posix Advisory Locking ****************************
       
 22176 **
       
 22177 ** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996)
       
 22178 ** section 6.5.2.2 lines 483 through 490 specify that when a process
       
 22179 ** sets or clears a lock, that operation overrides any prior locks set
       
 22180 ** by the same process.  It does not explicitly say so, but this implies
       
 22181 ** that it overrides locks set by the same process using a different
       
 22182 ** file descriptor.  Consider this test case:
       
 22183 **
       
 22184 **       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
       
 22185 **       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
       
 22186 **
       
 22187 ** Suppose ./file1 and ./file2 are really the same file (because
       
 22188 ** one is a hard or symbolic link to the other) then if you set
       
 22189 ** an exclusive lock on fd1, then try to get an exclusive lock
       
 22190 ** on fd2, it works.  I would have expected the second lock to
       
 22191 ** fail since there was already a lock on the file due to fd1.
       
 22192 ** But not so.  Since both locks came from the same process, the
       
 22193 ** second overrides the first, even though they were on different
       
 22194 ** file descriptors opened on different file names.
       
 22195 **
       
 22196 ** This means that we cannot use POSIX locks to synchronize file access
       
 22197 ** among competing threads of the same process.  POSIX locks will work fine
       
 22198 ** to synchronize access for threads in separate processes, but not
       
 22199 ** threads within the same process.
       
 22200 **
       
 22201 ** To work around the problem, SQLite has to manage file locks internally
       
 22202 ** on its own.  Whenever a new database is opened, we have to find the
       
 22203 ** specific inode of the database file (the inode is determined by the
       
 22204 ** st_dev and st_ino fields of the stat structure that fstat() fills in)
       
 22205 ** and check for locks already existing on that inode.  When locks are
       
 22206 ** created or removed, we have to look at our own internal record of the
       
 22207 ** locks to see if another thread has previously set a lock on that same
       
 22208 ** inode.
       
 22209 **
       
 22210 ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
       
 22211 ** For VxWorks, we have to use the alternative unique ID system based on
       
 22212 ** canonical filename and implemented in the previous division.)
       
 22213 **
       
 22214 ** The sqlite3_file structure for POSIX is no longer just an integer file
       
 22215 ** descriptor.  It is now a structure that holds the integer file
       
 22216 ** descriptor and a pointer to a structure that describes the internal
       
 22217 ** locks on the corresponding inode.  There is one locking structure
       
 22218 ** per inode, so if the same inode is opened twice, both unixFile structures
       
 22219 ** point to the same locking structure.  The locking structure keeps
       
 22220 ** a reference count (so we will know when to delete it) and a "cnt"
       
 22221 ** field that tells us its internal lock status.  cnt==0 means the
       
 22222 ** file is unlocked.  cnt==-1 means the file has an exclusive lock.
       
 22223 ** cnt>0 means there are cnt shared locks on the file.
       
 22224 **
       
 22225 ** Any attempt to lock or unlock a file first checks the locking
       
 22226 ** structure.  The fcntl() system call is only invoked to set a 
       
 22227 ** POSIX lock if the internal lock structure transitions between
       
 22228 ** a locked and an unlocked state.
       
 22229 **
       
 22230 ** But wait:  there are yet more problems with POSIX advisory locks.
       
 22231 **
       
 22232 ** If you close a file descriptor that points to a file that has locks,
       
 22233 ** all locks on that file that are owned by the current process are
       
 22234 ** released.  To work around this problem, each unixFile structure contains
       
 22235 ** a pointer to an unixOpenCnt structure.  There is one unixOpenCnt structure
       
 22236 ** per open inode, which means that multiple unixFile can point to a single
       
 22237 ** unixOpenCnt.  When an attempt is made to close an unixFile, if there are
       
 22238 ** other unixFile open on the same inode that are holding locks, the call
       
 22239 ** to close() the file descriptor is deferred until all of the locks clear.
       
 22240 ** The unixOpenCnt structure keeps a list of file descriptors that need to
       
 22241 ** be closed and that list is walked (and cleared) when the last lock
       
 22242 ** clears.
       
 22243 **
       
 22244 ** Yet another problem:  LinuxThreads do not play well with posix locks.
       
 22245 **
       
 22246 ** Many older versions of linux use the LinuxThreads library which is
       
 22247 ** not posix compliant.  Under LinuxThreads, a lock created by thread
       
 22248 ** A cannot be modified or overridden by a different thread B.
       
 22249 ** Only thread A can modify the lock.  Locking behavior is correct
       
 22250 ** if the appliation uses the newer Native Posix Thread Library (NPTL)
       
 22251 ** on linux - with NPTL a lock created by thread A can override locks
       
 22252 ** in thread B.  But there is no way to know at compile-time which
       
 22253 ** threading library is being used.  So there is no way to know at
       
 22254 ** compile-time whether or not thread A can override locks on thread B.
       
 22255 ** We have to do a run-time check to discover the behavior of the
       
 22256 ** current process.
       
 22257 **
       
 22258 ** On systems where thread A is unable to modify locks created by
       
 22259 ** thread B, we have to keep track of which thread created each
       
 22260 ** lock.  Hence there is an extra field in the key to the unixLockInfo
       
 22261 ** structure to record this information.  And on those systems it
       
 22262 ** is illegal to begin a transaction in one thread and finish it
       
 22263 ** in another.  For this latter restriction, there is no work-around.
       
 22264 ** It is a limitation of LinuxThreads.
       
 22265 */
       
 22266 
       
 22267 /*
       
 22268 ** Set or check the unixFile.tid field.  This field is set when an unixFile
       
 22269 ** is first opened.  All subsequent uses of the unixFile verify that the
       
 22270 ** same thread is operating on the unixFile.  Some operating systems do
       
 22271 ** not allow locks to be overridden by other threads and that restriction
       
 22272 ** means that sqlite3* database handles cannot be moved from one thread
       
 22273 ** to another while locks are held.
       
 22274 **
       
 22275 ** Version 3.3.1 (2006-01-15):  unixFile can be moved from one thread to
       
 22276 ** another as long as we are running on a system that supports threads
       
 22277 ** overriding each others locks (which is now the most common behavior)
       
 22278 ** or if no locks are held.  But the unixFile.pLock field needs to be
       
 22279 ** recomputed because its key includes the thread-id.  See the 
       
 22280 ** transferOwnership() function below for additional information
       
 22281 */
       
 22282 #if SQLITE_THREADSAFE && defined(__linux__)
       
 22283 # define SET_THREADID(X)   (X)->tid = pthread_self()
       
 22284 # define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
       
 22285                             !pthread_equal((X)->tid, pthread_self()))
       
 22286 #else
       
 22287 # define SET_THREADID(X)
       
 22288 # define CHECK_THREADID(X) 0
       
 22289 #endif
       
 22290 
       
 22291 /*
       
 22292 ** An instance of the following structure serves as the key used
       
 22293 ** to locate a particular unixOpenCnt structure given its inode.  This
       
 22294 ** is the same as the unixLockKey except that the thread ID is omitted.
       
 22295 */
       
 22296 struct unixFileId {
       
 22297   dev_t dev;                  /* Device number */
       
 22298 #if OS_VXWORKS
       
 22299   struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
       
 22300 #else
       
 22301   ino_t ino;                  /* Inode number */
       
 22302 #endif
       
 22303 };
       
 22304 
       
 22305 /*
       
 22306 ** An instance of the following structure serves as the key used
       
 22307 ** to locate a particular unixLockInfo structure given its inode.
       
 22308 **
       
 22309 ** If threads cannot override each others locks (LinuxThreads), then we
       
 22310 ** set the unixLockKey.tid field to the thread ID.  If threads can override
       
 22311 ** each others locks (Posix and NPTL) then tid is always set to zero.
       
 22312 ** tid is omitted if we compile without threading support or on an OS
       
 22313 ** other than linux.
       
 22314 */
       
 22315 struct unixLockKey {
       
 22316   struct unixFileId fid;  /* Unique identifier for the file */
       
 22317 #if SQLITE_THREADSAFE && defined(__linux__)
       
 22318   pthread_t tid;  /* Thread ID of lock owner. Zero if not using LinuxThreads */
       
 22319 #endif
       
 22320 };
       
 22321 
       
 22322 /*
       
 22323 ** An instance of the following structure is allocated for each open
       
 22324 ** inode.  Or, on LinuxThreads, there is one of these structures for
       
 22325 ** each inode opened by each thread.
       
 22326 **
       
 22327 ** A single inode can have multiple file descriptors, so each unixFile
       
 22328 ** structure contains a pointer to an instance of this object and this
       
 22329 ** object keeps a count of the number of unixFile pointing to it.
       
 22330 */
       
 22331 struct unixLockInfo {
       
 22332   struct unixLockKey lockKey;     /* The lookup key */
       
 22333   int cnt;                        /* Number of SHARED locks held */
       
 22334   int locktype;                   /* One of SHARED_LOCK, RESERVED_LOCK etc. */
       
 22335   int nRef;                       /* Number of pointers to this structure */
       
 22336   struct unixLockInfo *pNext;     /* List of all unixLockInfo objects */
       
 22337   struct unixLockInfo *pPrev;     /*    .... doubly linked */
       
 22338 };
       
 22339 
       
 22340 /*
       
 22341 ** An instance of the following structure is allocated for each open
       
 22342 ** inode.  This structure keeps track of the number of locks on that
       
 22343 ** inode.  If a close is attempted against an inode that is holding
       
 22344 ** locks, the close is deferred until all locks clear by adding the
       
 22345 ** file descriptor to be closed to the pending list.
       
 22346 **
       
 22347 ** TODO:  Consider changing this so that there is only a single file
       
 22348 ** descriptor for each open file, even when it is opened multiple times.
       
 22349 ** The close() system call would only occur when the last database
       
 22350 ** using the file closes.
       
 22351 */
       
 22352 struct unixOpenCnt {
       
 22353   struct unixFileId fileId;   /* The lookup key */
       
 22354   int nRef;                   /* Number of pointers to this structure */
       
 22355   int nLock;                  /* Number of outstanding locks */
       
 22356   UnixUnusedFd *pUnused;      /* Unused file descriptors to close */
       
 22357 #if OS_VXWORKS
       
 22358   sem_t *pSem;                     /* Named POSIX semaphore */
       
 22359   char aSemName[MAX_PATHNAME+2];   /* Name of that semaphore */
       
 22360 #endif
       
 22361   struct unixOpenCnt *pNext, *pPrev;   /* List of all unixOpenCnt objects */
       
 22362 };
       
 22363 
       
 22364 /*
       
 22365 ** Lists of all unixLockInfo and unixOpenCnt objects.  These used to be hash
       
 22366 ** tables.  But the number of objects is rarely more than a dozen and
       
 22367 ** never exceeds a few thousand.  And lookup is not on a critical
       
 22368 ** path so a simple linked list will suffice.
       
 22369 */
       
 22370 static struct unixLockInfo *lockList = 0;
       
 22371 static struct unixOpenCnt *openList = 0;
       
 22372 
       
 22373 /*
       
 22374 ** This variable remembers whether or not threads can override each others
       
 22375 ** locks.
       
 22376 **
       
 22377 **    0:  No.  Threads cannot override each others locks.  (LinuxThreads)
       
 22378 **    1:  Yes.  Threads can override each others locks.  (Posix & NLPT)
       
 22379 **   -1:  We don't know yet.
       
 22380 **
       
 22381 ** On some systems, we know at compile-time if threads can override each
       
 22382 ** others locks.  On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
       
 22383 ** will be set appropriately.  On other systems, we have to check at
       
 22384 ** runtime.  On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
       
 22385 ** undefined.
       
 22386 **
       
 22387 ** This variable normally has file scope only.  But during testing, we make
       
 22388 ** it a global so that the test code can change its value in order to verify
       
 22389 ** that the right stuff happens in either case.
       
 22390 */
       
 22391 #if SQLITE_THREADSAFE && defined(__linux__)
       
 22392 #  ifndef SQLITE_THREAD_OVERRIDE_LOCK
       
 22393 #    define SQLITE_THREAD_OVERRIDE_LOCK -1
       
 22394 #  endif
       
 22395 #  ifdef SQLITE_TEST
       
 22396 int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
       
 22397 #  else
       
 22398 static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
       
 22399 #  endif
       
 22400 #endif
       
 22401 
       
 22402 /*
       
 22403 ** This structure holds information passed into individual test
       
 22404 ** threads by the testThreadLockingBehavior() routine.
       
 22405 */
       
 22406 struct threadTestData {
       
 22407   int fd;                /* File to be locked */
       
 22408   struct flock lock;     /* The locking operation */
       
 22409   int result;            /* Result of the locking operation */
       
 22410 };
       
 22411 
       
 22412 #if SQLITE_THREADSAFE && defined(__linux__)
       
 22413 /*
       
 22414 ** This function is used as the main routine for a thread launched by
       
 22415 ** testThreadLockingBehavior(). It tests whether the shared-lock obtained
       
 22416 ** by the main thread in testThreadLockingBehavior() conflicts with a
       
 22417 ** hypothetical write-lock obtained by this thread on the same file.
       
 22418 **
       
 22419 ** The write-lock is not actually acquired, as this is not possible if 
       
 22420 ** the file is open in read-only mode (see ticket #3472).
       
 22421 */ 
       
 22422 static void *threadLockingTest(void *pArg){
       
 22423   struct threadTestData *pData = (struct threadTestData*)pArg;
       
 22424   pData->result = fcntl(pData->fd, F_GETLK, &pData->lock);
       
 22425   return pArg;
       
 22426 }
       
 22427 #endif /* SQLITE_THREADSAFE && defined(__linux__) */
       
 22428 
       
 22429 
       
 22430 #if SQLITE_THREADSAFE && defined(__linux__)
       
 22431 /*
       
 22432 ** This procedure attempts to determine whether or not threads
       
 22433 ** can override each others locks then sets the 
       
 22434 ** threadsOverrideEachOthersLocks variable appropriately.
       
 22435 */
       
 22436 static void testThreadLockingBehavior(int fd_orig){
       
 22437   int fd;
       
 22438   int rc;
       
 22439   struct threadTestData d;
       
 22440   struct flock l;
       
 22441   pthread_t t;
       
 22442 
       
 22443   fd = dup(fd_orig);
       
 22444   if( fd<0 ) return;
       
 22445   memset(&l, 0, sizeof(l));
       
 22446   l.l_type = F_RDLCK;
       
 22447   l.l_len = 1;
       
 22448   l.l_start = 0;
       
 22449   l.l_whence = SEEK_SET;
       
 22450   rc = fcntl(fd_orig, F_SETLK, &l);
       
 22451   if( rc!=0 ) return;
       
 22452   memset(&d, 0, sizeof(d));
       
 22453   d.fd = fd;
       
 22454   d.lock = l;
       
 22455   d.lock.l_type = F_WRLCK;
       
 22456   if( pthread_create(&t, 0, threadLockingTest, &d)==0 ){
       
 22457     pthread_join(t, 0);
       
 22458   }
       
 22459   close(fd);
       
 22460   if( d.result!=0 ) return;
       
 22461   threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK);
       
 22462 }
       
 22463 #endif /* SQLITE_THREADSAFE && defined(__linux__) */
       
 22464 
       
 22465 /*
       
 22466 ** Release a unixLockInfo structure previously allocated by findLockInfo().
       
 22467 **
       
 22468 ** The mutex entered using the unixEnterMutex() function must be held
       
 22469 ** when this function is called.
       
 22470 */
       
 22471 static void releaseLockInfo(struct unixLockInfo *pLock){
       
 22472   assert( unixMutexHeld() );
       
 22473   if( pLock ){
       
 22474     pLock->nRef--;
       
 22475     if( pLock->nRef==0 ){
       
 22476       if( pLock->pPrev ){
       
 22477         assert( pLock->pPrev->pNext==pLock );
       
 22478         pLock->pPrev->pNext = pLock->pNext;
       
 22479       }else{
       
 22480         assert( lockList==pLock );
       
 22481         lockList = pLock->pNext;
       
 22482       }
       
 22483       if( pLock->pNext ){
       
 22484         assert( pLock->pNext->pPrev==pLock );
       
 22485         pLock->pNext->pPrev = pLock->pPrev;
       
 22486       }
       
 22487       sqlite3_free(pLock);
       
 22488     }
       
 22489   }
       
 22490 }
       
 22491 
       
 22492 /*
       
 22493 ** Release a unixOpenCnt structure previously allocated by findLockInfo().
       
 22494 **
       
 22495 ** The mutex entered using the unixEnterMutex() function must be held
       
 22496 ** when this function is called.
       
 22497 */
       
 22498 static void releaseOpenCnt(struct unixOpenCnt *pOpen){
       
 22499   assert( unixMutexHeld() );
       
 22500   if( pOpen ){
       
 22501     pOpen->nRef--;
       
 22502     if( pOpen->nRef==0 ){
       
 22503       if( pOpen->pPrev ){
       
 22504         assert( pOpen->pPrev->pNext==pOpen );
       
 22505         pOpen->pPrev->pNext = pOpen->pNext;
       
 22506       }else{
       
 22507         assert( openList==pOpen );
       
 22508         openList = pOpen->pNext;
       
 22509       }
       
 22510       if( pOpen->pNext ){
       
 22511         assert( pOpen->pNext->pPrev==pOpen );
       
 22512         pOpen->pNext->pPrev = pOpen->pPrev;
       
 22513       }
       
 22514 #if SQLITE_THREADSAFE && defined(__linux__)
       
 22515       assert( !pOpen->pUnused || threadsOverrideEachOthersLocks==0 );
       
 22516 #endif
       
 22517 
       
 22518       /* If pOpen->pUnused is not null, then memory and file-descriptors
       
 22519       ** are leaked.
       
 22520       **
       
 22521       ** This will only happen if, under Linuxthreads, the user has opened
       
 22522       ** a transaction in one thread, then attempts to close the database
       
 22523       ** handle from another thread (without first unlocking the db file).
       
 22524       ** This is a misuse.  */
       
 22525       sqlite3_free(pOpen);
       
 22526     }
       
 22527   }
       
 22528 }
       
 22529 
       
 22530 /*
       
 22531 ** Given a file descriptor, locate unixLockInfo and unixOpenCnt structures that
       
 22532 ** describes that file descriptor.  Create new ones if necessary.  The
       
 22533 ** return values might be uninitialized if an error occurs.
       
 22534 **
       
 22535 ** The mutex entered using the unixEnterMutex() function must be held
       
 22536 ** when this function is called.
       
 22537 **
       
 22538 ** Return an appropriate error code.
       
 22539 */
       
 22540 static int findLockInfo(
       
 22541   unixFile *pFile,               /* Unix file with file desc used in the key */
       
 22542   struct unixLockInfo **ppLock,  /* Return the unixLockInfo structure here */
       
 22543   struct unixOpenCnt **ppOpen    /* Return the unixOpenCnt structure here */
       
 22544 ){
       
 22545   int rc;                        /* System call return code */
       
 22546   int fd;                        /* The file descriptor for pFile */
       
 22547   struct unixLockKey lockKey;    /* Lookup key for the unixLockInfo structure */
       
 22548   struct unixFileId fileId;      /* Lookup key for the unixOpenCnt struct */
       
 22549   struct stat statbuf;           /* Low-level file information */
       
 22550   struct unixLockInfo *pLock = 0;/* Candidate unixLockInfo object */
       
 22551   struct unixOpenCnt *pOpen;     /* Candidate unixOpenCnt object */
       
 22552 
       
 22553   assert( unixMutexHeld() );
       
 22554 
       
 22555   /* Get low-level information about the file that we can used to
       
 22556   ** create a unique name for the file.
       
 22557   */
       
 22558   fd = pFile->h;
       
 22559   rc = fstat(fd, &statbuf);
       
 22560   if( rc!=0 ){
       
 22561     pFile->lastErrno = errno;
       
 22562 #ifdef EOVERFLOW
       
 22563     if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
       
 22564 #endif
       
 22565     return SQLITE_IOERR;
       
 22566   }
       
 22567 
       
 22568 #ifdef __APPLE__
       
 22569   /* On OS X on an msdos filesystem, the inode number is reported
       
 22570   ** incorrectly for zero-size files.  See ticket #3260.  To work
       
 22571   ** around this problem (we consider it a bug in OS X, not SQLite)
       
 22572   ** we always increase the file size to 1 by writing a single byte
       
 22573   ** prior to accessing the inode number.  The one byte written is
       
 22574   ** an ASCII 'S' character which also happens to be the first byte
       
 22575   ** in the header of every SQLite database.  In this way, if there
       
 22576   ** is a race condition such that another thread has already populated
       
 22577   ** the first page of the database, no damage is done.
       
 22578   */
       
 22579   if( statbuf.st_size==0 ){
       
 22580     rc = write(fd, "S", 1);
       
 22581     if( rc!=1 ){
       
 22582       return SQLITE_IOERR;
       
 22583     }
       
 22584     rc = fstat(fd, &statbuf);
       
 22585     if( rc!=0 ){
       
 22586       pFile->lastErrno = errno;
       
 22587       return SQLITE_IOERR;
       
 22588     }
       
 22589   }
       
 22590 #endif
       
 22591 
       
 22592   memset(&lockKey, 0, sizeof(lockKey));
       
 22593   lockKey.fid.dev = statbuf.st_dev;
       
 22594 #if OS_VXWORKS
       
 22595   lockKey.fid.pId = pFile->pId;
       
 22596 #else
       
 22597   lockKey.fid.ino = statbuf.st_ino;
       
 22598 #endif
       
 22599 #if SQLITE_THREADSAFE && defined(__linux__)
       
 22600   if( threadsOverrideEachOthersLocks<0 ){
       
 22601     testThreadLockingBehavior(fd);
       
 22602   }
       
 22603   lockKey.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
       
 22604 #endif
       
 22605   fileId = lockKey.fid;
       
 22606   if( ppLock!=0 ){
       
 22607     pLock = lockList;
       
 22608     while( pLock && memcmp(&lockKey, &pLock->lockKey, sizeof(lockKey)) ){
       
 22609       pLock = pLock->pNext;
       
 22610     }
       
 22611     if( pLock==0 ){
       
 22612       pLock = sqlite3_malloc( sizeof(*pLock) );
       
 22613       if( pLock==0 ){
       
 22614         rc = SQLITE_NOMEM;
       
 22615         goto exit_findlockinfo;
       
 22616       }
       
 22617       memcpy(&pLock->lockKey,&lockKey,sizeof(lockKey));
       
 22618       pLock->nRef = 1;
       
 22619       pLock->cnt = 0;
       
 22620       pLock->locktype = 0;
       
 22621       pLock->pNext = lockList;
       
 22622       pLock->pPrev = 0;
       
 22623       if( lockList ) lockList->pPrev = pLock;
       
 22624       lockList = pLock;
       
 22625     }else{
       
 22626       pLock->nRef++;
       
 22627     }
       
 22628     *ppLock = pLock;
       
 22629   }
       
 22630   if( ppOpen!=0 ){
       
 22631     pOpen = openList;
       
 22632     while( pOpen && memcmp(&fileId, &pOpen->fileId, sizeof(fileId)) ){
       
 22633       pOpen = pOpen->pNext;
       
 22634     }
       
 22635     if( pOpen==0 ){
       
 22636       pOpen = sqlite3_malloc( sizeof(*pOpen) );
       
 22637       if( pOpen==0 ){
       
 22638         releaseLockInfo(pLock);
       
 22639         rc = SQLITE_NOMEM;
       
 22640         goto exit_findlockinfo;
       
 22641       }
       
 22642       memset(pOpen, 0, sizeof(*pOpen));
       
 22643       pOpen->fileId = fileId;
       
 22644       pOpen->nRef = 1;
       
 22645       pOpen->pNext = openList;
       
 22646       if( openList ) openList->pPrev = pOpen;
       
 22647       openList = pOpen;
       
 22648     }else{
       
 22649       pOpen->nRef++;
       
 22650     }
       
 22651     *ppOpen = pOpen;
       
 22652   }
       
 22653 
       
 22654 exit_findlockinfo:
       
 22655   return rc;
       
 22656 }
       
 22657 
       
 22658 /*
       
 22659 ** If we are currently in a different thread than the thread that the
       
 22660 ** unixFile argument belongs to, then transfer ownership of the unixFile
       
 22661 ** over to the current thread.
       
 22662 **
       
 22663 ** A unixFile is only owned by a thread on systems that use LinuxThreads.
       
 22664 **
       
 22665 ** Ownership transfer is only allowed if the unixFile is currently unlocked.
       
 22666 ** If the unixFile is locked and an ownership is wrong, then return
       
 22667 ** SQLITE_MISUSE.  SQLITE_OK is returned if everything works.
       
 22668 */
       
 22669 #if SQLITE_THREADSAFE && defined(__linux__)
       
 22670 static int transferOwnership(unixFile *pFile){
       
 22671   int rc;
       
 22672   pthread_t hSelf;
       
 22673   if( threadsOverrideEachOthersLocks ){
       
 22674     /* Ownership transfers not needed on this system */
       
 22675     return SQLITE_OK;
       
 22676   }
       
 22677   hSelf = pthread_self();
       
 22678   if( pthread_equal(pFile->tid, hSelf) ){
       
 22679     /* We are still in the same thread */
       
 22680     OSTRACE1("No-transfer, same thread\n");
       
 22681     return SQLITE_OK;
       
 22682   }
       
 22683   if( pFile->locktype!=NO_LOCK ){
       
 22684     /* We cannot change ownership while we are holding a lock! */
       
 22685     return SQLITE_MISUSE;
       
 22686   }
       
 22687   OSTRACE4("Transfer ownership of %d from %d to %d\n",
       
 22688             pFile->h, pFile->tid, hSelf);
       
 22689   pFile->tid = hSelf;
       
 22690   if (pFile->pLock != NULL) {
       
 22691     releaseLockInfo(pFile->pLock);
       
 22692     rc = findLockInfo(pFile, &pFile->pLock, 0);
       
 22693     OSTRACE5("LOCK    %d is now %s(%s,%d)\n", pFile->h,
       
 22694            locktypeName(pFile->locktype),
       
 22695            locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
       
 22696     return rc;
       
 22697   } else {
       
 22698     return SQLITE_OK;
       
 22699   }
       
 22700 }
       
 22701 #else  /* if not SQLITE_THREADSAFE */
       
 22702   /* On single-threaded builds, ownership transfer is a no-op */
       
 22703 # define transferOwnership(X) SQLITE_OK
       
 22704 #endif /* SQLITE_THREADSAFE */
       
 22705 
       
 22706 
       
 22707 /*
       
 22708 ** This routine checks if there is a RESERVED lock held on the specified
       
 22709 ** file by this or any other process. If such a lock is held, set *pResOut
       
 22710 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
       
 22711 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
       
 22712 */
       
 22713 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
       
 22714   int rc = SQLITE_OK;
       
 22715   int reserved = 0;
       
 22716   unixFile *pFile = (unixFile*)id;
       
 22717 
       
 22718   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
       
 22719 
       
 22720   assert( pFile );
       
 22721   unixEnterMutex(); /* Because pFile->pLock is shared across threads */
       
 22722 
       
 22723   /* Check if a thread in this process holds such a lock */
       
 22724   if( pFile->pLock->locktype>SHARED_LOCK ){
       
 22725     reserved = 1;
       
 22726   }
       
 22727 
       
 22728   /* Otherwise see if some other process holds it.
       
 22729   */
       
 22730 #ifndef __DJGPP__
       
 22731   if( !reserved ){
       
 22732     struct flock lock;
       
 22733     lock.l_whence = SEEK_SET;
       
 22734     lock.l_start = RESERVED_BYTE;
       
 22735     lock.l_len = 1;
       
 22736     lock.l_type = F_WRLCK;
       
 22737     if (-1 == fcntl(pFile->h, F_GETLK, &lock)) {
       
 22738       int tErrno = errno;
       
 22739       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
       
 22740       pFile->lastErrno = tErrno;
       
 22741     } else if( lock.l_type!=F_UNLCK ){
       
 22742       reserved = 1;
       
 22743     }
       
 22744   }
       
 22745 #endif
       
 22746   
       
 22747   unixLeaveMutex();
       
 22748   OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
       
 22749 
       
 22750   *pResOut = reserved;
       
 22751   return rc;
       
 22752 }
       
 22753 
       
 22754 /*
       
 22755 ** Perform a file locking operation on a range of bytes in a file.
       
 22756 ** The "op" parameter should be one of F_RDLCK, F_WRLCK, or F_UNLCK.
       
 22757 ** Return 0 on success or -1 for failure.  On failure, write the error
       
 22758 ** code into *pErrcode.
       
 22759 **
       
 22760 ** If the SQLITE_WHOLE_FILE_LOCKING bit is clear, then only lock
       
 22761 ** the range of bytes on the locking page between SHARED_FIRST and
       
 22762 ** SHARED_SIZE.  If SQLITE_WHOLE_FILE_LOCKING is set, then lock all
       
 22763 ** bytes from 0 up to but not including PENDING_BYTE, and all bytes
       
 22764 ** that follow SHARED_FIRST.
       
 22765 **
       
 22766 ** In other words, of SQLITE_WHOLE_FILE_LOCKING if false (the historical
       
 22767 ** default case) then only lock a small range of bytes from SHARED_FIRST
       
 22768 ** through SHARED_FIRST+SHARED_SIZE-1.  But if SQLITE_WHOLE_FILE_LOCKING is
       
 22769 ** true then lock every byte in the file except for PENDING_BYTE and
       
 22770 ** RESERVED_BYTE.
       
 22771 **
       
 22772 ** SQLITE_WHOLE_FILE_LOCKING=true overlaps SQLITE_WHOLE_FILE_LOCKING=false
       
 22773 ** and so the locking schemes are compatible.  One type of lock will
       
 22774 ** effectively exclude the other type.  The reason for using the
       
 22775 ** SQLITE_WHOLE_FILE_LOCKING=true is that by indicating the full range
       
 22776 ** of bytes to be read or written, we give hints to NFS to help it
       
 22777 ** maintain cache coherency.  On the other hand, whole file locking
       
 22778 ** is slower, so we don't want to use it except for NFS.
       
 22779 */
       
 22780 static int rangeLock(unixFile *pFile, int op, int *pErrcode){
       
 22781   struct flock lock;
       
 22782   int rc;
       
 22783   lock.l_type = op;
       
 22784   lock.l_start = SHARED_FIRST;
       
 22785   lock.l_whence = SEEK_SET;
       
 22786   if( (pFile->fileFlags & SQLITE_WHOLE_FILE_LOCKING)==0 ){
       
 22787     lock.l_len = SHARED_SIZE;
       
 22788     rc = fcntl(pFile->h, F_SETLK, &lock);
       
 22789     *pErrcode = errno;
       
 22790   }else{
       
 22791     lock.l_len = 0;
       
 22792     rc = fcntl(pFile->h, F_SETLK, &lock);
       
 22793     *pErrcode = errno;
       
 22794     if( NEVER(op==F_UNLCK) || rc!=(-1) ){
       
 22795       lock.l_start = 0;
       
 22796       lock.l_len = PENDING_BYTE;
       
 22797       rc = fcntl(pFile->h, F_SETLK, &lock);
       
 22798       if( ALWAYS(op!=F_UNLCK) && rc==(-1) ){
       
 22799         *pErrcode = errno;
       
 22800         lock.l_type = F_UNLCK;
       
 22801         lock.l_start = SHARED_FIRST;
       
 22802         lock.l_len = 0;
       
 22803         fcntl(pFile->h, F_SETLK, &lock);
       
 22804       }
       
 22805     }
       
 22806   }
       
 22807   return rc;
       
 22808 }
       
 22809 
       
 22810 /*
       
 22811 ** Lock the file with the lock specified by parameter locktype - one
       
 22812 ** of the following:
       
 22813 **
       
 22814 **     (1) SHARED_LOCK
       
 22815 **     (2) RESERVED_LOCK
       
 22816 **     (3) PENDING_LOCK
       
 22817 **     (4) EXCLUSIVE_LOCK
       
 22818 **
       
 22819 ** Sometimes when requesting one lock state, additional lock states
       
 22820 ** are inserted in between.  The locking might fail on one of the later
       
 22821 ** transitions leaving the lock state different from what it started but
       
 22822 ** still short of its goal.  The following chart shows the allowed
       
 22823 ** transitions and the inserted intermediate states:
       
 22824 **
       
 22825 **    UNLOCKED -> SHARED
       
 22826 **    SHARED -> RESERVED
       
 22827 **    SHARED -> (PENDING) -> EXCLUSIVE
       
 22828 **    RESERVED -> (PENDING) -> EXCLUSIVE
       
 22829 **    PENDING -> EXCLUSIVE
       
 22830 **
       
 22831 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
       
 22832 ** routine to lower a locking level.
       
 22833 */
       
 22834 static int unixLock(sqlite3_file *id, int locktype){
       
 22835   /* The following describes the implementation of the various locks and
       
 22836   ** lock transitions in terms of the POSIX advisory shared and exclusive
       
 22837   ** lock primitives (called read-locks and write-locks below, to avoid
       
 22838   ** confusion with SQLite lock names). The algorithms are complicated
       
 22839   ** slightly in order to be compatible with windows systems simultaneously
       
 22840   ** accessing the same database file, in case that is ever required.
       
 22841   **
       
 22842   ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
       
 22843   ** byte', each single bytes at well known offsets, and the 'shared byte
       
 22844   ** range', a range of 510 bytes at a well known offset.
       
 22845   **
       
 22846   ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
       
 22847   ** byte'.  If this is successful, a random byte from the 'shared byte
       
 22848   ** range' is read-locked and the lock on the 'pending byte' released.
       
 22849   **
       
 22850   ** A process may only obtain a RESERVED lock after it has a SHARED lock.
       
 22851   ** A RESERVED lock is implemented by grabbing a write-lock on the
       
 22852   ** 'reserved byte'. 
       
 22853   **
       
 22854   ** A process may only obtain a PENDING lock after it has obtained a
       
 22855   ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
       
 22856   ** on the 'pending byte'. This ensures that no new SHARED locks can be
       
 22857   ** obtained, but existing SHARED locks are allowed to persist. A process
       
 22858   ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
       
 22859   ** This property is used by the algorithm for rolling back a journal file
       
 22860   ** after a crash.
       
 22861   **
       
 22862   ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
       
 22863   ** implemented by obtaining a write-lock on the entire 'shared byte
       
 22864   ** range'. Since all other locks require a read-lock on one of the bytes
       
 22865   ** within this range, this ensures that no other locks are held on the
       
 22866   ** database. 
       
 22867   **
       
 22868   ** The reason a single byte cannot be used instead of the 'shared byte
       
 22869   ** range' is that some versions of windows do not support read-locks. By
       
 22870   ** locking a random byte from a range, concurrent SHARED locks may exist
       
 22871   ** even if the locking primitive used is always a write-lock.
       
 22872   */
       
 22873   int rc = SQLITE_OK;
       
 22874   unixFile *pFile = (unixFile*)id;
       
 22875   struct unixLockInfo *pLock = pFile->pLock;
       
 22876   struct flock lock;
       
 22877   int s = 0;
       
 22878   int tErrno;
       
 22879 
       
 22880   assert( pFile );
       
 22881   OSTRACE7("LOCK    %d %s was %s(%s,%d) pid=%d\n", pFile->h,
       
 22882       locktypeName(locktype), locktypeName(pFile->locktype),
       
 22883       locktypeName(pLock->locktype), pLock->cnt , getpid());
       
 22884 
       
 22885   /* If there is already a lock of this type or more restrictive on the
       
 22886   ** unixFile, do nothing. Don't use the end_lock: exit path, as
       
 22887   ** unixEnterMutex() hasn't been called yet.
       
 22888   */
       
 22889   if( pFile->locktype>=locktype ){
       
 22890     OSTRACE3("LOCK    %d %s ok (already held)\n", pFile->h,
       
 22891             locktypeName(locktype));
       
 22892     return SQLITE_OK;
       
 22893   }
       
 22894 
       
 22895   /* Make sure the locking sequence is correct.
       
 22896   **  (1) We never move from unlocked to anything higher than shared lock.
       
 22897   **  (2) SQLite never explicitly requests a pendig lock.
       
 22898   **  (3) A shared lock is always held when a reserve lock is requested.
       
 22899   */
       
 22900   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
       
 22901   assert( locktype!=PENDING_LOCK );
       
 22902   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
       
 22903 
       
 22904   /* This mutex is needed because pFile->pLock is shared across threads
       
 22905   */
       
 22906   unixEnterMutex();
       
 22907 
       
 22908   /* Make sure the current thread owns the pFile.
       
 22909   */
       
 22910   rc = transferOwnership(pFile);
       
 22911   if( rc!=SQLITE_OK ){
       
 22912     unixLeaveMutex();
       
 22913     return rc;
       
 22914   }
       
 22915   pLock = pFile->pLock;
       
 22916 
       
 22917   /* If some thread using this PID has a lock via a different unixFile*
       
 22918   ** handle that precludes the requested lock, return BUSY.
       
 22919   */
       
 22920   if( (pFile->locktype!=pLock->locktype && 
       
 22921           (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
       
 22922   ){
       
 22923     rc = SQLITE_BUSY;
       
 22924     goto end_lock;
       
 22925   }
       
 22926 
       
 22927   /* If a SHARED lock is requested, and some thread using this PID already
       
 22928   ** has a SHARED or RESERVED lock, then increment reference counts and
       
 22929   ** return SQLITE_OK.
       
 22930   */
       
 22931   if( locktype==SHARED_LOCK && 
       
 22932       (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
       
 22933     assert( locktype==SHARED_LOCK );
       
 22934     assert( pFile->locktype==0 );
       
 22935     assert( pLock->cnt>0 );
       
 22936     pFile->locktype = SHARED_LOCK;
       
 22937     pLock->cnt++;
       
 22938     pFile->pOpen->nLock++;
       
 22939     goto end_lock;
       
 22940   }
       
 22941 
       
 22942 
       
 22943   /* A PENDING lock is needed before acquiring a SHARED lock and before
       
 22944   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
       
 22945   ** be released.
       
 22946   */
       
 22947   lock.l_len = 1L;
       
 22948   lock.l_whence = SEEK_SET;
       
 22949   if( locktype==SHARED_LOCK 
       
 22950       || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
       
 22951   ){
       
 22952     lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
       
 22953     lock.l_start = PENDING_BYTE;
       
 22954     s = fcntl(pFile->h, F_SETLK, &lock);
       
 22955     if( s==(-1) ){
       
 22956       tErrno = errno;
       
 22957       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
       
 22958       if( IS_LOCK_ERROR(rc) ){
       
 22959         pFile->lastErrno = tErrno;
       
 22960       }
       
 22961       goto end_lock;
       
 22962     }
       
 22963   }
       
 22964 
       
 22965 
       
 22966   /* If control gets to this point, then actually go ahead and make
       
 22967   ** operating system calls for the specified lock.
       
 22968   */
       
 22969   if( locktype==SHARED_LOCK ){
       
 22970     assert( pLock->cnt==0 );
       
 22971     assert( pLock->locktype==0 );
       
 22972 
       
 22973     /* Now get the read-lock */
       
 22974     s = rangeLock(pFile, F_RDLCK, &tErrno);
       
 22975 
       
 22976     /* Drop the temporary PENDING lock */
       
 22977     lock.l_start = PENDING_BYTE;
       
 22978     lock.l_len = 1L;
       
 22979     lock.l_type = F_UNLCK;
       
 22980     if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
       
 22981       if( s != -1 ){
       
 22982         /* This could happen with a network mount */
       
 22983         tErrno = errno; 
       
 22984         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); 
       
 22985         if( IS_LOCK_ERROR(rc) ){
       
 22986           pFile->lastErrno = tErrno;
       
 22987         }
       
 22988         goto end_lock;
       
 22989       }
       
 22990     }
       
 22991     if( s==(-1) ){
       
 22992       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
       
 22993       if( IS_LOCK_ERROR(rc) ){
       
 22994         pFile->lastErrno = tErrno;
       
 22995       }
       
 22996     }else{
       
 22997       pFile->locktype = SHARED_LOCK;
       
 22998       pFile->pOpen->nLock++;
       
 22999       pLock->cnt = 1;
       
 23000     }
       
 23001   }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
       
 23002     /* We are trying for an exclusive lock but another thread in this
       
 23003     ** same process is still holding a shared lock. */
       
 23004     rc = SQLITE_BUSY;
       
 23005   }else{
       
 23006     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
       
 23007     ** assumed that there is a SHARED or greater lock on the file
       
 23008     ** already.
       
 23009     */
       
 23010     assert( 0!=pFile->locktype );
       
 23011     lock.l_type = F_WRLCK;
       
 23012     switch( locktype ){
       
 23013       case RESERVED_LOCK:
       
 23014         lock.l_start = RESERVED_BYTE;
       
 23015         s = fcntl(pFile->h, F_SETLK, &lock);
       
 23016         tErrno = errno;
       
 23017         break;
       
 23018       case EXCLUSIVE_LOCK:
       
 23019         s = rangeLock(pFile, F_WRLCK, &tErrno);
       
 23020         break;
       
 23021       default:
       
 23022         assert(0);
       
 23023     }
       
 23024     if( s==(-1) ){
       
 23025       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
       
 23026       if( IS_LOCK_ERROR(rc) ){
       
 23027         pFile->lastErrno = tErrno;
       
 23028       }
       
 23029     }
       
 23030   }
       
 23031   
       
 23032 
       
 23033 #ifndef NDEBUG
       
 23034   /* Set up the transaction-counter change checking flags when
       
 23035   ** transitioning from a SHARED to a RESERVED lock.  The change
       
 23036   ** from SHARED to RESERVED marks the beginning of a normal
       
 23037   ** write operation (not a hot journal rollback).
       
 23038   */
       
 23039   if( rc==SQLITE_OK
       
 23040    && pFile->locktype<=SHARED_LOCK
       
 23041    && locktype==RESERVED_LOCK
       
 23042   ){
       
 23043     pFile->transCntrChng = 0;
       
 23044     pFile->dbUpdate = 0;
       
 23045     pFile->inNormalWrite = 1;
       
 23046   }
       
 23047 #endif
       
 23048 
       
 23049 
       
 23050   if( rc==SQLITE_OK ){
       
 23051     pFile->locktype = locktype;
       
 23052     pLock->locktype = locktype;
       
 23053   }else if( locktype==EXCLUSIVE_LOCK ){
       
 23054     pFile->locktype = PENDING_LOCK;
       
 23055     pLock->locktype = PENDING_LOCK;
       
 23056   }
       
 23057 
       
 23058 end_lock:
       
 23059   unixLeaveMutex();
       
 23060   OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype), 
       
 23061       rc==SQLITE_OK ? "ok" : "failed");
       
 23062   return rc;
       
 23063 }
       
 23064 
       
 23065 /*
       
 23066 ** Close all file descriptors accumuated in the unixOpenCnt->pUnused list.
       
 23067 ** If all such file descriptors are closed without error, the list is
       
 23068 ** cleared and SQLITE_OK returned.
       
 23069 **
       
 23070 ** Otherwise, if an error occurs, then successfully closed file descriptor
       
 23071 ** entries are removed from the list, and SQLITE_IOERR_CLOSE returned. 
       
 23072 ** not deleted and SQLITE_IOERR_CLOSE returned.
       
 23073 */ 
       
 23074 static int closePendingFds(unixFile *pFile){
       
 23075   int rc = SQLITE_OK;
       
 23076   struct unixOpenCnt *pOpen = pFile->pOpen;
       
 23077   UnixUnusedFd *pError = 0;
       
 23078   UnixUnusedFd *p;
       
 23079   UnixUnusedFd *pNext;
       
 23080   for(p=pOpen->pUnused; p; p=pNext){
       
 23081     pNext = p->pNext;
       
 23082     if( close(p->fd) ){
       
 23083       pFile->lastErrno = errno;
       
 23084       rc = SQLITE_IOERR_CLOSE;
       
 23085       p->pNext = pError;
       
 23086       pError = p;
       
 23087     }else{
       
 23088       sqlite3_free(p);
       
 23089     }
       
 23090   }
       
 23091   pOpen->pUnused = pError;
       
 23092   return rc;
       
 23093 }
       
 23094 
       
 23095 /*
       
 23096 ** Add the file descriptor used by file handle pFile to the corresponding
       
 23097 ** pUnused list.
       
 23098 */
       
 23099 static void setPendingFd(unixFile *pFile){
       
 23100   struct unixOpenCnt *pOpen = pFile->pOpen;
       
 23101   UnixUnusedFd *p = pFile->pUnused;
       
 23102   p->pNext = pOpen->pUnused;
       
 23103   pOpen->pUnused = p;
       
 23104   pFile->h = -1;
       
 23105   pFile->pUnused = 0;
       
 23106 }
       
 23107 
       
 23108 /*
       
 23109 ** Lower the locking level on file descriptor pFile to locktype.  locktype
       
 23110 ** must be either NO_LOCK or SHARED_LOCK.
       
 23111 **
       
 23112 ** If the locking level of the file descriptor is already at or below
       
 23113 ** the requested locking level, this routine is a no-op.
       
 23114 */
       
 23115 static int unixUnlock(sqlite3_file *id, int locktype){
       
 23116   unixFile *pFile = (unixFile*)id; /* The open file */
       
 23117   struct unixLockInfo *pLock;      /* Structure describing current lock state */
       
 23118   struct flock lock;               /* Information passed into fcntl() */
       
 23119   int rc = SQLITE_OK;              /* Return code from this interface */
       
 23120   int h;                           /* The underlying file descriptor */
       
 23121   int tErrno;                      /* Error code from system call errors */
       
 23122 
       
 23123   assert( pFile );
       
 23124   OSTRACE7("UNLOCK  %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
       
 23125       pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
       
 23126 
       
 23127   assert( locktype<=SHARED_LOCK );
       
 23128   if( pFile->locktype<=locktype ){
       
 23129     return SQLITE_OK;
       
 23130   }
       
 23131   if( CHECK_THREADID(pFile) ){
       
 23132     return SQLITE_MISUSE;
       
 23133   }
       
 23134   unixEnterMutex();
       
 23135   h = pFile->h;
       
 23136   pLock = pFile->pLock;
       
 23137   assert( pLock->cnt!=0 );
       
 23138   if( pFile->locktype>SHARED_LOCK ){
       
 23139     assert( pLock->locktype==pFile->locktype );
       
 23140     SimulateIOErrorBenign(1);
       
 23141     SimulateIOError( h=(-1) )
       
 23142     SimulateIOErrorBenign(0);
       
 23143 
       
 23144 #ifndef NDEBUG
       
 23145     /* When reducing a lock such that other processes can start
       
 23146     ** reading the database file again, make sure that the
       
 23147     ** transaction counter was updated if any part of the database
       
 23148     ** file changed.  If the transaction counter is not updated,
       
 23149     ** other connections to the same file might not realize that
       
 23150     ** the file has changed and hence might not know to flush their
       
 23151     ** cache.  The use of a stale cache can lead to database corruption.
       
 23152     */
       
 23153     assert( pFile->inNormalWrite==0
       
 23154          || pFile->dbUpdate==0
       
 23155          || pFile->transCntrChng==1 );
       
 23156     pFile->inNormalWrite = 0;
       
 23157 #endif
       
 23158 
       
 23159 
       
 23160     if( locktype==SHARED_LOCK ){
       
 23161       if( rangeLock(pFile, F_RDLCK, &tErrno)==(-1) ){
       
 23162         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
       
 23163         if( IS_LOCK_ERROR(rc) ){
       
 23164           pFile->lastErrno = tErrno;
       
 23165         }
       
 23166         goto end_unlock;
       
 23167       }
       
 23168     }
       
 23169     lock.l_type = F_UNLCK;
       
 23170     lock.l_whence = SEEK_SET;
       
 23171     lock.l_start = PENDING_BYTE;
       
 23172     lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
       
 23173     if( fcntl(h, F_SETLK, &lock)!=(-1) ){
       
 23174       pLock->locktype = SHARED_LOCK;
       
 23175     }else{
       
 23176       tErrno = errno;
       
 23177       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
       
 23178       if( IS_LOCK_ERROR(rc) ){
       
 23179         pFile->lastErrno = tErrno;
       
 23180       }
       
 23181       goto end_unlock;
       
 23182     }
       
 23183   }
       
 23184   if( locktype==NO_LOCK ){
       
 23185     struct unixOpenCnt *pOpen;
       
 23186 
       
 23187     /* Decrement the shared lock counter.  Release the lock using an
       
 23188     ** OS call only when all threads in this same process have released
       
 23189     ** the lock.
       
 23190     */
       
 23191     pLock->cnt--;
       
 23192     if( pLock->cnt==0 ){
       
 23193       lock.l_type = F_UNLCK;
       
 23194       lock.l_whence = SEEK_SET;
       
 23195       lock.l_start = lock.l_len = 0L;
       
 23196       SimulateIOErrorBenign(1);
       
 23197       SimulateIOError( h=(-1) )
       
 23198       SimulateIOErrorBenign(0);
       
 23199       if( fcntl(h, F_SETLK, &lock)!=(-1) ){
       
 23200         pLock->locktype = NO_LOCK;
       
 23201       }else{
       
 23202         tErrno = errno;
       
 23203         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
       
 23204         if( IS_LOCK_ERROR(rc) ){
       
 23205           pFile->lastErrno = tErrno;
       
 23206         }
       
 23207         pLock->locktype = NO_LOCK;
       
 23208         pFile->locktype = NO_LOCK;
       
 23209       }
       
 23210     }
       
 23211 
       
 23212     /* Decrement the count of locks against this same file.  When the
       
 23213     ** count reaches zero, close any other file descriptors whose close
       
 23214     ** was deferred because of outstanding locks.
       
 23215     */
       
 23216     pOpen = pFile->pOpen;
       
 23217     pOpen->nLock--;
       
 23218     assert( pOpen->nLock>=0 );
       
 23219     if( pOpen->nLock==0 ){
       
 23220       int rc2 = closePendingFds(pFile);
       
 23221       if( rc==SQLITE_OK ){
       
 23222         rc = rc2;
       
 23223       }
       
 23224     }
       
 23225   }
       
 23226 	
       
 23227 end_unlock:
       
 23228   unixLeaveMutex();
       
 23229   if( rc==SQLITE_OK ) pFile->locktype = locktype;
       
 23230   return rc;
       
 23231 }
       
 23232 
       
 23233 /*
       
 23234 ** This function performs the parts of the "close file" operation 
       
 23235 ** common to all locking schemes. It closes the directory and file
       
 23236 ** handles, if they are valid, and sets all fields of the unixFile
       
 23237 ** structure to 0.
       
 23238 **
       
 23239 ** It is *not* necessary to hold the mutex when this routine is called,
       
 23240 ** even on VxWorks.  A mutex will be acquired on VxWorks by the
       
 23241 ** vxworksReleaseFileId() routine.
       
 23242 */
       
 23243 static int closeUnixFile(sqlite3_file *id){
       
 23244   unixFile *pFile = (unixFile*)id;
       
 23245   if( pFile ){
       
 23246     if( pFile->dirfd>=0 ){
       
 23247       int err = close(pFile->dirfd);
       
 23248       if( err ){
       
 23249         pFile->lastErrno = errno;
       
 23250         return SQLITE_IOERR_DIR_CLOSE;
       
 23251       }else{
       
 23252         pFile->dirfd=-1;
       
 23253       }
       
 23254     }
       
 23255     if( pFile->h>=0 ){
       
 23256       int err = close(pFile->h);
       
 23257       if( err ){
       
 23258         pFile->lastErrno = errno;
       
 23259         return SQLITE_IOERR_CLOSE;
       
 23260       }
       
 23261     }
       
 23262 #if OS_VXWORKS
       
 23263     if( pFile->pId ){
       
 23264       if( pFile->isDelete ){
       
 23265         unlink(pFile->pId->zCanonicalName);
       
 23266       }
       
 23267       vxworksReleaseFileId(pFile->pId);
       
 23268       pFile->pId = 0;
       
 23269     }
       
 23270 #endif
       
 23271     OSTRACE2("CLOSE   %-3d\n", pFile->h);
       
 23272     OpenCounter(-1);
       
 23273     sqlite3_free(pFile->pUnused);
       
 23274     memset(pFile, 0, sizeof(unixFile));
       
 23275   }
       
 23276   return SQLITE_OK;
       
 23277 }
       
 23278 
       
 23279 /*
       
 23280 ** Close a file.
       
 23281 */
       
 23282 static int unixClose(sqlite3_file *id){
       
 23283   int rc = SQLITE_OK;
       
 23284   if( id ){
       
 23285     unixFile *pFile = (unixFile *)id;
       
 23286     unixUnlock(id, NO_LOCK);
       
 23287     unixEnterMutex();
       
 23288     if( pFile->pOpen && pFile->pOpen->nLock ){
       
 23289       /* If there are outstanding locks, do not actually close the file just
       
 23290       ** yet because that would clear those locks.  Instead, add the file
       
 23291       ** descriptor to pOpen->pUnused list.  It will be automatically closed 
       
 23292       ** when the last lock is cleared.
       
 23293       */
       
 23294       setPendingFd(pFile);
       
 23295     }
       
 23296     releaseLockInfo(pFile->pLock);
       
 23297     releaseOpenCnt(pFile->pOpen);
       
 23298     rc = closeUnixFile(id);
       
 23299     unixLeaveMutex();
       
 23300   }
       
 23301   return rc;
       
 23302 }
       
 23303 
       
 23304 /************** End of the posix advisory lock implementation *****************
       
 23305 ******************************************************************************/
       
 23306 
       
 23307 /******************************************************************************
       
 23308 ****************************** No-op Locking **********************************
       
 23309 **
       
 23310 ** Of the various locking implementations available, this is by far the
       
 23311 ** simplest:  locking is ignored.  No attempt is made to lock the database
       
 23312 ** file for reading or writing.
       
 23313 **
       
 23314 ** This locking mode is appropriate for use on read-only databases
       
 23315 ** (ex: databases that are burned into CD-ROM, for example.)  It can
       
 23316 ** also be used if the application employs some external mechanism to
       
 23317 ** prevent simultaneous access of the same database by two or more
       
 23318 ** database connections.  But there is a serious risk of database
       
 23319 ** corruption if this locking mode is used in situations where multiple
       
 23320 ** database connections are accessing the same database file at the same
       
 23321 ** time and one or more of those connections are writing.
       
 23322 */
       
 23323 
       
 23324 static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
       
 23325   UNUSED_PARAMETER(NotUsed);
       
 23326   *pResOut = 0;
       
 23327   return SQLITE_OK;
       
 23328 }
       
 23329 static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
       
 23330   UNUSED_PARAMETER2(NotUsed, NotUsed2);
       
 23331   return SQLITE_OK;
       
 23332 }
       
 23333 static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
       
 23334   UNUSED_PARAMETER2(NotUsed, NotUsed2);
       
 23335   return SQLITE_OK;
       
 23336 }
       
 23337 
       
 23338 /*
       
 23339 ** Close the file.
       
 23340 */
       
 23341 static int nolockClose(sqlite3_file *id) {
       
 23342   return closeUnixFile(id);
       
 23343 }
       
 23344 
       
 23345 /******************* End of the no-op lock implementation *********************
       
 23346 ******************************************************************************/
       
 23347 
       
 23348 /******************************************************************************
       
 23349 ************************* Begin dot-file Locking ******************************
       
 23350 **
       
 23351 ** The dotfile locking implementation uses the existance of separate lock
       
 23352 ** files in order to control access to the database.  This works on just
       
 23353 ** about every filesystem imaginable.  But there are serious downsides:
       
 23354 **
       
 23355 **    (1)  There is zero concurrency.  A single reader blocks all other
       
 23356 **         connections from reading or writing the database.
       
 23357 **
       
 23358 **    (2)  An application crash or power loss can leave stale lock files
       
 23359 **         sitting around that need to be cleared manually.
       
 23360 **
       
 23361 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
       
 23362 ** other locking strategy is available.
       
 23363 **
       
 23364 ** Dotfile locking works by creating a file in the same directory as the
       
 23365 ** database and with the same name but with a ".lock" extension added.
       
 23366 ** The existance of a lock file implies an EXCLUSIVE lock.  All other lock
       
 23367 ** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
       
 23368 */
       
 23369 
       
 23370 /*
       
 23371 ** The file suffix added to the data base filename in order to create the
       
 23372 ** lock file.
       
 23373 */
       
 23374 #define DOTLOCK_SUFFIX ".lock"
       
 23375 
       
 23376 /*
       
 23377 ** This routine checks if there is a RESERVED lock held on the specified
       
 23378 ** file by this or any other process. If such a lock is held, set *pResOut
       
 23379 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
       
 23380 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
       
 23381 **
       
 23382 ** In dotfile locking, either a lock exists or it does not.  So in this
       
 23383 ** variation of CheckReservedLock(), *pResOut is set to true if any lock
       
 23384 ** is held on the file and false if the file is unlocked.
       
 23385 */
       
 23386 static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
       
 23387   int rc = SQLITE_OK;
       
 23388   int reserved = 0;
       
 23389   unixFile *pFile = (unixFile*)id;
       
 23390 
       
 23391   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
       
 23392   
       
 23393   assert( pFile );
       
 23394 
       
 23395   /* Check if a thread in this process holds such a lock */
       
 23396   if( pFile->locktype>SHARED_LOCK ){
       
 23397     /* Either this connection or some other connection in the same process
       
 23398     ** holds a lock on the file.  No need to check further. */
       
 23399     reserved = 1;
       
 23400   }else{
       
 23401     /* The lock is held if and only if the lockfile exists */
       
 23402     const char *zLockFile = (const char*)pFile->lockingContext;
       
 23403     reserved = access(zLockFile, 0)==0;
       
 23404   }
       
 23405   OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
       
 23406   *pResOut = reserved;
       
 23407   return rc;
       
 23408 }
       
 23409 
       
 23410 /*
       
 23411 ** Lock the file with the lock specified by parameter locktype - one
       
 23412 ** of the following:
       
 23413 **
       
 23414 **     (1) SHARED_LOCK
       
 23415 **     (2) RESERVED_LOCK
       
 23416 **     (3) PENDING_LOCK
       
 23417 **     (4) EXCLUSIVE_LOCK
       
 23418 **
       
 23419 ** Sometimes when requesting one lock state, additional lock states
       
 23420 ** are inserted in between.  The locking might fail on one of the later
       
 23421 ** transitions leaving the lock state different from what it started but
       
 23422 ** still short of its goal.  The following chart shows the allowed
       
 23423 ** transitions and the inserted intermediate states:
       
 23424 **
       
 23425 **    UNLOCKED -> SHARED
       
 23426 **    SHARED -> RESERVED
       
 23427 **    SHARED -> (PENDING) -> EXCLUSIVE
       
 23428 **    RESERVED -> (PENDING) -> EXCLUSIVE
       
 23429 **    PENDING -> EXCLUSIVE
       
 23430 **
       
 23431 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
       
 23432 ** routine to lower a locking level.
       
 23433 **
       
 23434 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
       
 23435 ** But we track the other locking levels internally.
       
 23436 */
       
 23437 static int dotlockLock(sqlite3_file *id, int locktype) {
       
 23438   unixFile *pFile = (unixFile*)id;
       
 23439   int fd;
       
 23440   char *zLockFile = (char *)pFile->lockingContext;
       
 23441   int rc = SQLITE_OK;
       
 23442 
       
 23443 
       
 23444   /* If we have any lock, then the lock file already exists.  All we have
       
 23445   ** to do is adjust our internal record of the lock level.
       
 23446   */
       
 23447   if( pFile->locktype > NO_LOCK ){
       
 23448     pFile->locktype = locktype;
       
 23449 #if !OS_VXWORKS
       
 23450     /* Always update the timestamp on the old file */
       
 23451     utimes(zLockFile, NULL);
       
 23452 #endif
       
 23453     return SQLITE_OK;
       
 23454   }
       
 23455   
       
 23456   /* grab an exclusive lock */
       
 23457   fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
       
 23458   if( fd<0 ){
       
 23459     /* failed to open/create the file, someone else may have stolen the lock */
       
 23460     int tErrno = errno;
       
 23461     if( EEXIST == tErrno ){
       
 23462       rc = SQLITE_BUSY;
       
 23463     } else {
       
 23464       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
       
 23465       if( IS_LOCK_ERROR(rc) ){
       
 23466         pFile->lastErrno = tErrno;
       
 23467       }
       
 23468     }
       
 23469     return rc;
       
 23470   } 
       
 23471   if( close(fd) ){
       
 23472     pFile->lastErrno = errno;
       
 23473     rc = SQLITE_IOERR_CLOSE;
       
 23474   }
       
 23475   
       
 23476   /* got it, set the type and return ok */
       
 23477   pFile->locktype = locktype;
       
 23478   return rc;
       
 23479 }
       
 23480 
       
 23481 /*
       
 23482 ** Lower the locking level on file descriptor pFile to locktype.  locktype
       
 23483 ** must be either NO_LOCK or SHARED_LOCK.
       
 23484 **
       
 23485 ** If the locking level of the file descriptor is already at or below
       
 23486 ** the requested locking level, this routine is a no-op.
       
 23487 **
       
 23488 ** When the locking level reaches NO_LOCK, delete the lock file.
       
 23489 */
       
 23490 static int dotlockUnlock(sqlite3_file *id, int locktype) {
       
 23491   unixFile *pFile = (unixFile*)id;
       
 23492   char *zLockFile = (char *)pFile->lockingContext;
       
 23493 
       
 23494   assert( pFile );
       
 23495   OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype,
       
 23496 	   pFile->locktype, getpid());
       
 23497   assert( locktype<=SHARED_LOCK );
       
 23498   
       
 23499   /* no-op if possible */
       
 23500   if( pFile->locktype==locktype ){
       
 23501     return SQLITE_OK;
       
 23502   }
       
 23503 
       
 23504   /* To downgrade to shared, simply update our internal notion of the
       
 23505   ** lock state.  No need to mess with the file on disk.
       
 23506   */
       
 23507   if( locktype==SHARED_LOCK ){
       
 23508     pFile->locktype = SHARED_LOCK;
       
 23509     return SQLITE_OK;
       
 23510   }
       
 23511   
       
 23512   /* To fully unlock the database, delete the lock file */
       
 23513   assert( locktype==NO_LOCK );
       
 23514   if( unlink(zLockFile) ){
       
 23515     int rc = 0;
       
 23516     int tErrno = errno;
       
 23517     if( ENOENT != tErrno ){
       
 23518       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
       
 23519     }
       
 23520     if( IS_LOCK_ERROR(rc) ){
       
 23521       pFile->lastErrno = tErrno;
       
 23522     }
       
 23523     return rc; 
       
 23524   }
       
 23525   pFile->locktype = NO_LOCK;
       
 23526   return SQLITE_OK;
       
 23527 }
       
 23528 
       
 23529 /*
       
 23530 ** Close a file.  Make sure the lock has been released before closing.
       
 23531 */
       
 23532 static int dotlockClose(sqlite3_file *id) {
       
 23533   int rc;
       
 23534   if( id ){
       
 23535     unixFile *pFile = (unixFile*)id;
       
 23536     dotlockUnlock(id, NO_LOCK);
       
 23537     sqlite3_free(pFile->lockingContext);
       
 23538   }
       
 23539   rc = closeUnixFile(id);
       
 23540   return rc;
       
 23541 }
       
 23542 /****************** End of the dot-file lock implementation *******************
       
 23543 ******************************************************************************/
       
 23544 
       
 23545 /******************************************************************************
       
 23546 ************************** Begin flock Locking ********************************
       
 23547 **
       
 23548 ** Use the flock() system call to do file locking.
       
 23549 **
       
 23550 ** flock() locking is like dot-file locking in that the various
       
 23551 ** fine-grain locking levels supported by SQLite are collapsed into
       
 23552 ** a single exclusive lock.  In other words, SHARED, RESERVED, and
       
 23553 ** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
       
 23554 ** still works when you do this, but concurrency is reduced since
       
 23555 ** only a single process can be reading the database at a time.
       
 23556 **
       
 23557 ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
       
 23558 ** compiling for VXWORKS.
       
 23559 */
       
 23560 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
       
 23561 
       
 23562 /*
       
 23563 ** This routine checks if there is a RESERVED lock held on the specified
       
 23564 ** file by this or any other process. If such a lock is held, set *pResOut
       
 23565 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
       
 23566 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
       
 23567 */
       
 23568 static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
       
 23569   int rc = SQLITE_OK;
       
 23570   int reserved = 0;
       
 23571   unixFile *pFile = (unixFile*)id;
       
 23572   
       
 23573   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
       
 23574   
       
 23575   assert( pFile );
       
 23576   
       
 23577   /* Check if a thread in this process holds such a lock */
       
 23578   if( pFile->locktype>SHARED_LOCK ){
       
 23579     reserved = 1;
       
 23580   }
       
 23581   
       
 23582   /* Otherwise see if some other process holds it. */
       
 23583   if( !reserved ){
       
 23584     /* attempt to get the lock */
       
 23585     int lrc = flock(pFile->h, LOCK_EX | LOCK_NB);
       
 23586     if( !lrc ){
       
 23587       /* got the lock, unlock it */
       
 23588       lrc = flock(pFile->h, LOCK_UN);
       
 23589       if ( lrc ) {
       
 23590         int tErrno = errno;
       
 23591         /* unlock failed with an error */
       
 23592         lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); 
       
 23593         if( IS_LOCK_ERROR(lrc) ){
       
 23594           pFile->lastErrno = tErrno;
       
 23595           rc = lrc;
       
 23596         }
       
 23597       }
       
 23598     } else {
       
 23599       int tErrno = errno;
       
 23600       reserved = 1;
       
 23601       /* someone else might have it reserved */
       
 23602       lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); 
       
 23603       if( IS_LOCK_ERROR(lrc) ){
       
 23604         pFile->lastErrno = tErrno;
       
 23605         rc = lrc;
       
 23606       }
       
 23607     }
       
 23608   }
       
 23609   OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
       
 23610 
       
 23611 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
       
 23612   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
       
 23613     rc = SQLITE_OK;
       
 23614     reserved=1;
       
 23615   }
       
 23616 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
       
 23617   *pResOut = reserved;
       
 23618   return rc;
       
 23619 }
       
 23620 
       
 23621 /*
       
 23622 ** Lock the file with the lock specified by parameter locktype - one
       
 23623 ** of the following:
       
 23624 **
       
 23625 **     (1) SHARED_LOCK
       
 23626 **     (2) RESERVED_LOCK
       
 23627 **     (3) PENDING_LOCK
       
 23628 **     (4) EXCLUSIVE_LOCK
       
 23629 **
       
 23630 ** Sometimes when requesting one lock state, additional lock states
       
 23631 ** are inserted in between.  The locking might fail on one of the later
       
 23632 ** transitions leaving the lock state different from what it started but
       
 23633 ** still short of its goal.  The following chart shows the allowed
       
 23634 ** transitions and the inserted intermediate states:
       
 23635 **
       
 23636 **    UNLOCKED -> SHARED
       
 23637 **    SHARED -> RESERVED
       
 23638 **    SHARED -> (PENDING) -> EXCLUSIVE
       
 23639 **    RESERVED -> (PENDING) -> EXCLUSIVE
       
 23640 **    PENDING -> EXCLUSIVE
       
 23641 **
       
 23642 ** flock() only really support EXCLUSIVE locks.  We track intermediate
       
 23643 ** lock states in the sqlite3_file structure, but all locks SHARED or
       
 23644 ** above are really EXCLUSIVE locks and exclude all other processes from
       
 23645 ** access the file.
       
 23646 **
       
 23647 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
       
 23648 ** routine to lower a locking level.
       
 23649 */
       
 23650 static int flockLock(sqlite3_file *id, int locktype) {
       
 23651   int rc = SQLITE_OK;
       
 23652   unixFile *pFile = (unixFile*)id;
       
 23653 
       
 23654   assert( pFile );
       
 23655 
       
 23656   /* if we already have a lock, it is exclusive.  
       
 23657   ** Just adjust level and punt on outta here. */
       
 23658   if (pFile->locktype > NO_LOCK) {
       
 23659     pFile->locktype = locktype;
       
 23660     return SQLITE_OK;
       
 23661   }
       
 23662   
       
 23663   /* grab an exclusive lock */
       
 23664   
       
 23665   if (flock(pFile->h, LOCK_EX | LOCK_NB)) {
       
 23666     int tErrno = errno;
       
 23667     /* didn't get, must be busy */
       
 23668     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
       
 23669     if( IS_LOCK_ERROR(rc) ){
       
 23670       pFile->lastErrno = tErrno;
       
 23671     }
       
 23672   } else {
       
 23673     /* got it, set the type and return ok */
       
 23674     pFile->locktype = locktype;
       
 23675   }
       
 23676   OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype), 
       
 23677            rc==SQLITE_OK ? "ok" : "failed");
       
 23678 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
       
 23679   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
       
 23680     rc = SQLITE_BUSY;
       
 23681   }
       
 23682 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
       
 23683   return rc;
       
 23684 }
       
 23685 
       
 23686 
       
 23687 /*
       
 23688 ** Lower the locking level on file descriptor pFile to locktype.  locktype
       
 23689 ** must be either NO_LOCK or SHARED_LOCK.
       
 23690 **
       
 23691 ** If the locking level of the file descriptor is already at or below
       
 23692 ** the requested locking level, this routine is a no-op.
       
 23693 */
       
 23694 static int flockUnlock(sqlite3_file *id, int locktype) {
       
 23695   unixFile *pFile = (unixFile*)id;
       
 23696   
       
 23697   assert( pFile );
       
 23698   OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype,
       
 23699            pFile->locktype, getpid());
       
 23700   assert( locktype<=SHARED_LOCK );
       
 23701   
       
 23702   /* no-op if possible */
       
 23703   if( pFile->locktype==locktype ){
       
 23704     return SQLITE_OK;
       
 23705   }
       
 23706   
       
 23707   /* shared can just be set because we always have an exclusive */
       
 23708   if (locktype==SHARED_LOCK) {
       
 23709     pFile->locktype = locktype;
       
 23710     return SQLITE_OK;
       
 23711   }
       
 23712   
       
 23713   /* no, really, unlock. */
       
 23714   int rc = flock(pFile->h, LOCK_UN);
       
 23715   if (rc) {
       
 23716     int r, tErrno = errno;
       
 23717     r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
       
 23718     if( IS_LOCK_ERROR(r) ){
       
 23719       pFile->lastErrno = tErrno;
       
 23720     }
       
 23721 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
       
 23722     if( (r & SQLITE_IOERR) == SQLITE_IOERR ){
       
 23723       r = SQLITE_BUSY;
       
 23724     }
       
 23725 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
       
 23726     
       
 23727     return r;
       
 23728   } else {
       
 23729     pFile->locktype = NO_LOCK;
       
 23730     return SQLITE_OK;
       
 23731   }
       
 23732 }
       
 23733 
       
 23734 /*
       
 23735 ** Close a file.
       
 23736 */
       
 23737 static int flockClose(sqlite3_file *id) {
       
 23738   if( id ){
       
 23739     flockUnlock(id, NO_LOCK);
       
 23740   }
       
 23741   return closeUnixFile(id);
       
 23742 }
       
 23743 
       
 23744 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
       
 23745 
       
 23746 /******************* End of the flock lock implementation *********************
       
 23747 ******************************************************************************/
       
 23748 
       
 23749 /******************************************************************************
       
 23750 ************************ Begin Named Semaphore Locking ************************
       
 23751 **
       
 23752 ** Named semaphore locking is only supported on VxWorks.
       
 23753 **
       
 23754 ** Semaphore locking is like dot-lock and flock in that it really only
       
 23755 ** supports EXCLUSIVE locking.  Only a single process can read or write
       
 23756 ** the database file at a time.  This reduces potential concurrency, but
       
 23757 ** makes the lock implementation much easier.
       
 23758 */
       
 23759 #if OS_VXWORKS
       
 23760 
       
 23761 /*
       
 23762 ** This routine checks if there is a RESERVED lock held on the specified
       
 23763 ** file by this or any other process. If such a lock is held, set *pResOut
       
 23764 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
       
 23765 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
       
 23766 */
       
 23767 static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
       
 23768   int rc = SQLITE_OK;
       
 23769   int reserved = 0;
       
 23770   unixFile *pFile = (unixFile*)id;
       
 23771 
       
 23772   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
       
 23773   
       
 23774   assert( pFile );
       
 23775 
       
 23776   /* Check if a thread in this process holds such a lock */
       
 23777   if( pFile->locktype>SHARED_LOCK ){
       
 23778     reserved = 1;
       
 23779   }
       
 23780   
       
 23781   /* Otherwise see if some other process holds it. */
       
 23782   if( !reserved ){
       
 23783     sem_t *pSem = pFile->pOpen->pSem;
       
 23784     struct stat statBuf;
       
 23785 
       
 23786     if( sem_trywait(pSem)==-1 ){
       
 23787       int tErrno = errno;
       
 23788       if( EAGAIN != tErrno ){
       
 23789         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
       
 23790         pFile->lastErrno = tErrno;
       
 23791       } else {
       
 23792         /* someone else has the lock when we are in NO_LOCK */
       
 23793         reserved = (pFile->locktype < SHARED_LOCK);
       
 23794       }
       
 23795     }else{
       
 23796       /* we could have it if we want it */
       
 23797       sem_post(pSem);
       
 23798     }
       
 23799   }
       
 23800   OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
       
 23801 
       
 23802   *pResOut = reserved;
       
 23803   return rc;
       
 23804 }
       
 23805 
       
 23806 /*
       
 23807 ** Lock the file with the lock specified by parameter locktype - one
       
 23808 ** of the following:
       
 23809 **
       
 23810 **     (1) SHARED_LOCK
       
 23811 **     (2) RESERVED_LOCK
       
 23812 **     (3) PENDING_LOCK
       
 23813 **     (4) EXCLUSIVE_LOCK
       
 23814 **
       
 23815 ** Sometimes when requesting one lock state, additional lock states
       
 23816 ** are inserted in between.  The locking might fail on one of the later
       
 23817 ** transitions leaving the lock state different from what it started but
       
 23818 ** still short of its goal.  The following chart shows the allowed
       
 23819 ** transitions and the inserted intermediate states:
       
 23820 **
       
 23821 **    UNLOCKED -> SHARED
       
 23822 **    SHARED -> RESERVED
       
 23823 **    SHARED -> (PENDING) -> EXCLUSIVE
       
 23824 **    RESERVED -> (PENDING) -> EXCLUSIVE
       
 23825 **    PENDING -> EXCLUSIVE
       
 23826 **
       
 23827 ** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
       
 23828 ** lock states in the sqlite3_file structure, but all locks SHARED or
       
 23829 ** above are really EXCLUSIVE locks and exclude all other processes from
       
 23830 ** access the file.
       
 23831 **
       
 23832 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
       
 23833 ** routine to lower a locking level.
       
 23834 */
       
 23835 static int semLock(sqlite3_file *id, int locktype) {
       
 23836   unixFile *pFile = (unixFile*)id;
       
 23837   int fd;
       
 23838   sem_t *pSem = pFile->pOpen->pSem;
       
 23839   int rc = SQLITE_OK;
       
 23840 
       
 23841   /* if we already have a lock, it is exclusive.  
       
 23842   ** Just adjust level and punt on outta here. */
       
 23843   if (pFile->locktype > NO_LOCK) {
       
 23844     pFile->locktype = locktype;
       
 23845     rc = SQLITE_OK;
       
 23846     goto sem_end_lock;
       
 23847   }
       
 23848   
       
 23849   /* lock semaphore now but bail out when already locked. */
       
 23850   if( sem_trywait(pSem)==-1 ){
       
 23851     rc = SQLITE_BUSY;
       
 23852     goto sem_end_lock;
       
 23853   }
       
 23854 
       
 23855   /* got it, set the type and return ok */
       
 23856   pFile->locktype = locktype;
       
 23857 
       
 23858  sem_end_lock:
       
 23859   return rc;
       
 23860 }
       
 23861 
       
 23862 /*
       
 23863 ** Lower the locking level on file descriptor pFile to locktype.  locktype
       
 23864 ** must be either NO_LOCK or SHARED_LOCK.
       
 23865 **
       
 23866 ** If the locking level of the file descriptor is already at or below
       
 23867 ** the requested locking level, this routine is a no-op.
       
 23868 */
       
 23869 static int semUnlock(sqlite3_file *id, int locktype) {
       
 23870   unixFile *pFile = (unixFile*)id;
       
 23871   sem_t *pSem = pFile->pOpen->pSem;
       
 23872 
       
 23873   assert( pFile );
       
 23874   assert( pSem );
       
 23875   OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype,
       
 23876 	   pFile->locktype, getpid());
       
 23877   assert( locktype<=SHARED_LOCK );
       
 23878   
       
 23879   /* no-op if possible */
       
 23880   if( pFile->locktype==locktype ){
       
 23881     return SQLITE_OK;
       
 23882   }
       
 23883   
       
 23884   /* shared can just be set because we always have an exclusive */
       
 23885   if (locktype==SHARED_LOCK) {
       
 23886     pFile->locktype = locktype;
       
 23887     return SQLITE_OK;
       
 23888   }
       
 23889   
       
 23890   /* no, really unlock. */
       
 23891   if ( sem_post(pSem)==-1 ) {
       
 23892     int rc, tErrno = errno;
       
 23893     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
       
 23894     if( IS_LOCK_ERROR(rc) ){
       
 23895       pFile->lastErrno = tErrno;
       
 23896     }
       
 23897     return rc; 
       
 23898   }
       
 23899   pFile->locktype = NO_LOCK;
       
 23900   return SQLITE_OK;
       
 23901 }
       
 23902 
       
 23903 /*
       
 23904  ** Close a file.
       
 23905  */
       
 23906 static int semClose(sqlite3_file *id) {
       
 23907   if( id ){
       
 23908     unixFile *pFile = (unixFile*)id;
       
 23909     semUnlock(id, NO_LOCK);
       
 23910     assert( pFile );
       
 23911     unixEnterMutex();
       
 23912     releaseLockInfo(pFile->pLock);
       
 23913     releaseOpenCnt(pFile->pOpen);
       
 23914     unixLeaveMutex();
       
 23915     closeUnixFile(id);
       
 23916   }
       
 23917   return SQLITE_OK;
       
 23918 }
       
 23919 
       
 23920 #endif /* OS_VXWORKS */
       
 23921 /*
       
 23922 ** Named semaphore locking is only available on VxWorks.
       
 23923 **
       
 23924 *************** End of the named semaphore lock implementation ****************
       
 23925 ******************************************************************************/
       
 23926 
       
 23927 
       
 23928 /******************************************************************************
       
 23929 *************************** Begin AFP Locking *********************************
       
 23930 **
       
 23931 ** AFP is the Apple Filing Protocol.  AFP is a network filesystem found
       
 23932 ** on Apple Macintosh computers - both OS9 and OSX.
       
 23933 **
       
 23934 ** Third-party implementations of AFP are available.  But this code here
       
 23935 ** only works on OSX.
       
 23936 */
       
 23937 
       
 23938 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
       
 23939 /*
       
 23940 ** The afpLockingContext structure contains all afp lock specific state
       
 23941 */
       
 23942 typedef struct afpLockingContext afpLockingContext;
       
 23943 struct afpLockingContext {
       
 23944   unsigned long long sharedByte;
       
 23945   const char *dbPath;             /* Name of the open file */
       
 23946 };
       
 23947 
       
 23948 struct ByteRangeLockPB2
       
 23949 {
       
 23950   unsigned long long offset;        /* offset to first byte to lock */
       
 23951   unsigned long long length;        /* nbr of bytes to lock */
       
 23952   unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
       
 23953   unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
       
 23954   unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
       
 23955   int fd;                           /* file desc to assoc this lock with */
       
 23956 };
       
 23957 
       
 23958 #define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
       
 23959 
       
 23960 /*
       
 23961 ** This is a utility for setting or clearing a bit-range lock on an
       
 23962 ** AFP filesystem.
       
 23963 ** 
       
 23964 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
       
 23965 */
       
 23966 static int afpSetLock(
       
 23967   const char *path,              /* Name of the file to be locked or unlocked */
       
 23968   unixFile *pFile,               /* Open file descriptor on path */
       
 23969   unsigned long long offset,     /* First byte to be locked */
       
 23970   unsigned long long length,     /* Number of bytes to lock */
       
 23971   int setLockFlag                /* True to set lock.  False to clear lock */
       
 23972 ){
       
 23973   struct ByteRangeLockPB2 pb;
       
 23974   int err;
       
 23975   
       
 23976   pb.unLockFlag = setLockFlag ? 0 : 1;
       
 23977   pb.startEndFlag = 0;
       
 23978   pb.offset = offset;
       
 23979   pb.length = length; 
       
 23980   pb.fd = pFile->h;
       
 23981   
       
 23982   OSTRACE6("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", 
       
 23983     (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
       
 23984     offset, length);
       
 23985   err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
       
 23986   if ( err==-1 ) {
       
 23987     int rc;
       
 23988     int tErrno = errno;
       
 23989     OSTRACE4("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
       
 23990              path, tErrno, strerror(tErrno));
       
 23991 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
       
 23992     rc = SQLITE_BUSY;
       
 23993 #else
       
 23994     rc = sqliteErrorFromPosixError(tErrno,
       
 23995                     setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
       
 23996 #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
       
 23997     if( IS_LOCK_ERROR(rc) ){
       
 23998       pFile->lastErrno = tErrno;
       
 23999     }
       
 24000     return rc;
       
 24001   } else {
       
 24002     return SQLITE_OK;
       
 24003   }
       
 24004 }
       
 24005 
       
 24006 /*
       
 24007 ** This routine checks if there is a RESERVED lock held on the specified
       
 24008 ** file by this or any other process. If such a lock is held, set *pResOut
       
 24009 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
       
 24010 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
       
 24011 */
       
 24012 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
       
 24013   int rc = SQLITE_OK;
       
 24014   int reserved = 0;
       
 24015   unixFile *pFile = (unixFile*)id;
       
 24016   
       
 24017   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
       
 24018   
       
 24019   assert( pFile );
       
 24020   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
       
 24021   
       
 24022   /* Check if a thread in this process holds such a lock */
       
 24023   if( pFile->locktype>SHARED_LOCK ){
       
 24024     reserved = 1;
       
 24025   }
       
 24026   
       
 24027   /* Otherwise see if some other process holds it.
       
 24028    */
       
 24029   if( !reserved ){
       
 24030     /* lock the RESERVED byte */
       
 24031     int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);  
       
 24032     if( SQLITE_OK==lrc ){
       
 24033       /* if we succeeded in taking the reserved lock, unlock it to restore
       
 24034       ** the original state */
       
 24035       lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
       
 24036     } else {
       
 24037       /* if we failed to get the lock then someone else must have it */
       
 24038       reserved = 1;
       
 24039     }
       
 24040     if( IS_LOCK_ERROR(lrc) ){
       
 24041       rc=lrc;
       
 24042     }
       
 24043   }
       
 24044   
       
 24045   OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
       
 24046   
       
 24047   *pResOut = reserved;
       
 24048   return rc;
       
 24049 }
       
 24050 
       
 24051 /*
       
 24052 ** Lock the file with the lock specified by parameter locktype - one
       
 24053 ** of the following:
       
 24054 **
       
 24055 **     (1) SHARED_LOCK
       
 24056 **     (2) RESERVED_LOCK
       
 24057 **     (3) PENDING_LOCK
       
 24058 **     (4) EXCLUSIVE_LOCK
       
 24059 **
       
 24060 ** Sometimes when requesting one lock state, additional lock states
       
 24061 ** are inserted in between.  The locking might fail on one of the later
       
 24062 ** transitions leaving the lock state different from what it started but
       
 24063 ** still short of its goal.  The following chart shows the allowed
       
 24064 ** transitions and the inserted intermediate states:
       
 24065 **
       
 24066 **    UNLOCKED -> SHARED
       
 24067 **    SHARED -> RESERVED
       
 24068 **    SHARED -> (PENDING) -> EXCLUSIVE
       
 24069 **    RESERVED -> (PENDING) -> EXCLUSIVE
       
 24070 **    PENDING -> EXCLUSIVE
       
 24071 **
       
 24072 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
       
 24073 ** routine to lower a locking level.
       
 24074 */
       
 24075 static int afpLock(sqlite3_file *id, int locktype){
       
 24076   int rc = SQLITE_OK;
       
 24077   unixFile *pFile = (unixFile*)id;
       
 24078   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
       
 24079   
       
 24080   assert( pFile );
       
 24081   OSTRACE5("LOCK    %d %s was %s pid=%d\n", pFile->h,
       
 24082          locktypeName(locktype), locktypeName(pFile->locktype), getpid());
       
 24083 
       
 24084   /* If there is already a lock of this type or more restrictive on the
       
 24085   ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
       
 24086   ** unixEnterMutex() hasn't been called yet.
       
 24087   */
       
 24088   if( pFile->locktype>=locktype ){
       
 24089     OSTRACE3("LOCK    %d %s ok (already held)\n", pFile->h,
       
 24090            locktypeName(locktype));
       
 24091     return SQLITE_OK;
       
 24092   }
       
 24093 
       
 24094   /* Make sure the locking sequence is correct
       
 24095   */
       
 24096   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
       
 24097   assert( locktype!=PENDING_LOCK );
       
 24098   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
       
 24099   
       
 24100   /* This mutex is needed because pFile->pLock is shared across threads
       
 24101   */
       
 24102   unixEnterMutex();
       
 24103 
       
 24104   /* Make sure the current thread owns the pFile.
       
 24105   */
       
 24106   rc = transferOwnership(pFile);
       
 24107   if( rc!=SQLITE_OK ){
       
 24108     unixLeaveMutex();
       
 24109     return rc;
       
 24110   }
       
 24111     
       
 24112   /* A PENDING lock is needed before acquiring a SHARED lock and before
       
 24113   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
       
 24114   ** be released.
       
 24115   */
       
 24116   if( locktype==SHARED_LOCK 
       
 24117       || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
       
 24118   ){
       
 24119     int failed;
       
 24120     failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
       
 24121     if (failed) {
       
 24122       rc = failed;
       
 24123       goto afp_end_lock;
       
 24124     }
       
 24125   }
       
 24126   
       
 24127   /* If control gets to this point, then actually go ahead and make
       
 24128   ** operating system calls for the specified lock.
       
 24129   */
       
 24130   if( locktype==SHARED_LOCK ){
       
 24131     int lk, lrc1, lrc2, lrc1Errno;
       
 24132     
       
 24133     /* Now get the read-lock SHARED_LOCK */
       
 24134     /* note that the quality of the randomness doesn't matter that much */
       
 24135     lk = random(); 
       
 24136     context->sharedByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
       
 24137     lrc1 = afpSetLock(context->dbPath, pFile, 
       
 24138           SHARED_FIRST+context->sharedByte, 1, 1);
       
 24139     if( IS_LOCK_ERROR(lrc1) ){
       
 24140       lrc1Errno = pFile->lastErrno;
       
 24141     }
       
 24142     /* Drop the temporary PENDING lock */
       
 24143     lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
       
 24144     
       
 24145     if( IS_LOCK_ERROR(lrc1) ) {
       
 24146       pFile->lastErrno = lrc1Errno;
       
 24147       rc = lrc1;
       
 24148       goto afp_end_lock;
       
 24149     } else if( IS_LOCK_ERROR(lrc2) ){
       
 24150       rc = lrc2;
       
 24151       goto afp_end_lock;
       
 24152     } else if( lrc1 != SQLITE_OK ) {
       
 24153       rc = lrc1;
       
 24154     } else {
       
 24155       pFile->locktype = SHARED_LOCK;
       
 24156       pFile->pOpen->nLock++;
       
 24157     }
       
 24158   }else{
       
 24159     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
       
 24160     ** assumed that there is a SHARED or greater lock on the file
       
 24161     ** already.
       
 24162     */
       
 24163     int failed = 0;
       
 24164     assert( 0!=pFile->locktype );
       
 24165     if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
       
 24166         /* Acquire a RESERVED lock */
       
 24167         failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
       
 24168     }
       
 24169     if (!failed && locktype == EXCLUSIVE_LOCK) {
       
 24170       /* Acquire an EXCLUSIVE lock */
       
 24171         
       
 24172       /* Remove the shared lock before trying the range.  we'll need to 
       
 24173       ** reestablish the shared lock if we can't get the  afpUnlock
       
 24174       */
       
 24175       if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
       
 24176                          context->sharedByte, 1, 0)) ){
       
 24177         int failed2 = SQLITE_OK;
       
 24178         /* now attemmpt to get the exclusive lock range */
       
 24179         failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, 
       
 24180                                SHARED_SIZE, 1);
       
 24181         if( failed && (failed2 = afpSetLock(context->dbPath, pFile, 
       
 24182                        SHARED_FIRST + context->sharedByte, 1, 1)) ){
       
 24183           /* Can't reestablish the shared lock.  Sqlite can't deal, this is
       
 24184           ** a critical I/O error
       
 24185           */
       
 24186           rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : 
       
 24187                SQLITE_IOERR_LOCK;
       
 24188           goto afp_end_lock;
       
 24189         } 
       
 24190       }else{
       
 24191         rc = failed; 
       
 24192       }
       
 24193     }
       
 24194     if( failed ){
       
 24195       rc = failed;
       
 24196     }
       
 24197   }
       
 24198   
       
 24199   if( rc==SQLITE_OK ){
       
 24200     pFile->locktype = locktype;
       
 24201   }else if( locktype==EXCLUSIVE_LOCK ){
       
 24202     pFile->locktype = PENDING_LOCK;
       
 24203   }
       
 24204   
       
 24205 afp_end_lock:
       
 24206   unixLeaveMutex();
       
 24207   OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype), 
       
 24208          rc==SQLITE_OK ? "ok" : "failed");
       
 24209   return rc;
       
 24210 }
       
 24211 
       
 24212 /*
       
 24213 ** Lower the locking level on file descriptor pFile to locktype.  locktype
       
 24214 ** must be either NO_LOCK or SHARED_LOCK.
       
 24215 **
       
 24216 ** If the locking level of the file descriptor is already at or below
       
 24217 ** the requested locking level, this routine is a no-op.
       
 24218 */
       
 24219 static int afpUnlock(sqlite3_file *id, int locktype) {
       
 24220   int rc = SQLITE_OK;
       
 24221   unixFile *pFile = (unixFile*)id;
       
 24222   afpLockingContext *pCtx = (afpLockingContext *) pFile->lockingContext;
       
 24223 
       
 24224   assert( pFile );
       
 24225   OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype,
       
 24226          pFile->locktype, getpid());
       
 24227 
       
 24228   assert( locktype<=SHARED_LOCK );
       
 24229   if( pFile->locktype<=locktype ){
       
 24230     return SQLITE_OK;
       
 24231   }
       
 24232   if( CHECK_THREADID(pFile) ){
       
 24233     return SQLITE_MISUSE;
       
 24234   }
       
 24235   unixEnterMutex();
       
 24236   if( pFile->locktype>SHARED_LOCK ){
       
 24237     
       
 24238     if( pFile->locktype==EXCLUSIVE_LOCK ){
       
 24239       rc = afpSetLock(pCtx->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
       
 24240       if( rc==SQLITE_OK && locktype==SHARED_LOCK ){
       
 24241         /* only re-establish the shared lock if necessary */
       
 24242         int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
       
 24243         rc = afpSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 1);
       
 24244       }
       
 24245     }
       
 24246     if( rc==SQLITE_OK && pFile->locktype>=PENDING_LOCK ){
       
 24247       rc = afpSetLock(pCtx->dbPath, pFile, PENDING_BYTE, 1, 0);
       
 24248     } 
       
 24249     if( rc==SQLITE_OK && pFile->locktype>=RESERVED_LOCK ){
       
 24250       rc = afpSetLock(pCtx->dbPath, pFile, RESERVED_BYTE, 1, 0);
       
 24251     }
       
 24252   }else if( locktype==NO_LOCK ){
       
 24253     /* clear the shared lock */
       
 24254     int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
       
 24255     rc = afpSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 0);
       
 24256   }
       
 24257 
       
 24258   if( rc==SQLITE_OK ){
       
 24259     if( locktype==NO_LOCK ){
       
 24260       struct unixOpenCnt *pOpen = pFile->pOpen;
       
 24261       pOpen->nLock--;
       
 24262       assert( pOpen->nLock>=0 );
       
 24263       if( pOpen->nLock==0 ){
       
 24264         rc = closePendingFds(pFile);
       
 24265       }
       
 24266     }
       
 24267   }
       
 24268   unixLeaveMutex();
       
 24269   if( rc==SQLITE_OK ){
       
 24270     pFile->locktype = locktype;
       
 24271   }
       
 24272   return rc;
       
 24273 }
       
 24274 
       
 24275 /*
       
 24276 ** Close a file & cleanup AFP specific locking context 
       
 24277 */
       
 24278 static int afpClose(sqlite3_file *id) {
       
 24279   if( id ){
       
 24280     unixFile *pFile = (unixFile*)id;
       
 24281     afpUnlock(id, NO_LOCK);
       
 24282     unixEnterMutex();
       
 24283     if( pFile->pOpen && pFile->pOpen->nLock ){
       
 24284       /* If there are outstanding locks, do not actually close the file just
       
 24285       ** yet because that would clear those locks.  Instead, add the file
       
 24286       ** descriptor to pOpen->aPending.  It will be automatically closed when
       
 24287       ** the last lock is cleared.
       
 24288       */
       
 24289       setPendingFd(pFile);
       
 24290     }
       
 24291     releaseOpenCnt(pFile->pOpen);
       
 24292     sqlite3_free(pFile->lockingContext);
       
 24293     closeUnixFile(id);
       
 24294     unixLeaveMutex();
       
 24295   }
       
 24296   return SQLITE_OK;
       
 24297 }
       
 24298 
       
 24299 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
       
 24300 /*
       
 24301 ** The code above is the AFP lock implementation.  The code is specific
       
 24302 ** to MacOSX and does not work on other unix platforms.  No alternative
       
 24303 ** is available.  If you don't compile for a mac, then the "unix-afp"
       
 24304 ** VFS is not available.
       
 24305 **
       
 24306 ********************* End of the AFP lock implementation **********************
       
 24307 ******************************************************************************/
       
 24308 
       
 24309 
       
 24310 /******************************************************************************
       
 24311 **************** Non-locking sqlite3_file methods *****************************
       
 24312 **
       
 24313 ** The next division contains implementations for all methods of the 
       
 24314 ** sqlite3_file object other than the locking methods.  The locking
       
 24315 ** methods were defined in divisions above (one locking method per
       
 24316 ** division).  Those methods that are common to all locking modes
       
 24317 ** are gather together into this division.
       
 24318 */
       
 24319 
       
 24320 /*
       
 24321 ** Seek to the offset passed as the second argument, then read cnt 
       
 24322 ** bytes into pBuf. Return the number of bytes actually read.
       
 24323 **
       
 24324 ** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
       
 24325 ** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
       
 24326 ** one system to another.  Since SQLite does not define USE_PREAD
       
 24327 ** any any form by default, we will not attempt to define _XOPEN_SOURCE.
       
 24328 ** See tickets #2741 and #2681.
       
 24329 **
       
 24330 ** To avoid stomping the errno value on a failed read the lastErrno value
       
 24331 ** is set before returning.
       
 24332 */
       
 24333 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
       
 24334   int got;
       
 24335   i64 newOffset;
       
 24336   TIMER_START;
       
 24337 #if defined(USE_PREAD)
       
 24338   got = pread(id->h, pBuf, cnt, offset);
       
 24339   SimulateIOError( got = -1 );
       
 24340 #elif defined(USE_PREAD64)
       
 24341   got = pread64(id->h, pBuf, cnt, offset);
       
 24342   SimulateIOError( got = -1 );
       
 24343 #else
       
 24344   newOffset = lseek(id->h, offset, SEEK_SET);
       
 24345   SimulateIOError( newOffset-- );
       
 24346   if( newOffset!=offset ){
       
 24347     if( newOffset == -1 ){
       
 24348       ((unixFile*)id)->lastErrno = errno;
       
 24349     }else{
       
 24350       ((unixFile*)id)->lastErrno = 0;			
       
 24351     }
       
 24352     return -1;
       
 24353   }
       
 24354   got = read(id->h, pBuf, cnt);
       
 24355 #endif
       
 24356   TIMER_END;
       
 24357   if( got<0 ){
       
 24358     ((unixFile*)id)->lastErrno = errno;
       
 24359   }
       
 24360   OSTRACE5("READ    %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
       
 24361   return got;
       
 24362 }
       
 24363 
       
 24364 /*
       
 24365 ** Read data from a file into a buffer.  Return SQLITE_OK if all
       
 24366 ** bytes were read successfully and SQLITE_IOERR if anything goes
       
 24367 ** wrong.
       
 24368 */
       
 24369 static int unixRead(
       
 24370   sqlite3_file *id, 
       
 24371   void *pBuf, 
       
 24372   int amt,
       
 24373   sqlite3_int64 offset
       
 24374 ){
       
 24375   unixFile *pFile = (unixFile *)id;
       
 24376   int got;
       
 24377   assert( id );
       
 24378 
       
 24379   /* If this is a database file (not a journal, master-journal or temp
       
 24380   ** file), the bytes in the locking range should never be read or written. */
       
 24381   assert( pFile->pUnused==0
       
 24382        || offset>=PENDING_BYTE+512
       
 24383        || offset+amt<=PENDING_BYTE 
       
 24384   );
       
 24385 
       
 24386   got = seekAndRead(pFile, offset, pBuf, amt);
       
 24387   if( got==amt ){
       
 24388     return SQLITE_OK;
       
 24389   }else if( got<0 ){
       
 24390     /* lastErrno set by seekAndRead */
       
 24391     return SQLITE_IOERR_READ;
       
 24392   }else{
       
 24393     pFile->lastErrno = 0; /* not a system error */
       
 24394     /* Unread parts of the buffer must be zero-filled */
       
 24395     memset(&((char*)pBuf)[got], 0, amt-got);
       
 24396     return SQLITE_IOERR_SHORT_READ;
       
 24397   }
       
 24398 }
       
 24399 
       
 24400 /*
       
 24401 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
       
 24402 ** Return the number of bytes actually read.  Update the offset.
       
 24403 **
       
 24404 ** To avoid stomping the errno value on a failed write the lastErrno value
       
 24405 ** is set before returning.
       
 24406 */
       
 24407 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
       
 24408   int got;
       
 24409   i64 newOffset;
       
 24410   TIMER_START;
       
 24411 #if defined(USE_PREAD)
       
 24412   got = pwrite(id->h, pBuf, cnt, offset);
       
 24413 #elif defined(USE_PREAD64)
       
 24414   got = pwrite64(id->h, pBuf, cnt, offset);
       
 24415 #else
       
 24416   newOffset = lseek(id->h, offset, SEEK_SET);
       
 24417   if( newOffset!=offset ){
       
 24418     if( newOffset == -1 ){
       
 24419       ((unixFile*)id)->lastErrno = errno;
       
 24420     }else{
       
 24421       ((unixFile*)id)->lastErrno = 0;			
       
 24422     }
       
 24423     return -1;
       
 24424   }
       
 24425 # ifndef VXWORKS
       
 24426    got = write(id->h, pBuf, cnt);
       
 24427 # else
       
 24428   got = write(id->h, (char *)pBuf, cnt);
       
 24429 # endif
       
 24430 #endif
       
 24431   TIMER_END;
       
 24432   if( got<0 ){
       
 24433     ((unixFile*)id)->lastErrno = errno;
       
 24434   }
       
 24435 
       
 24436   OSTRACE5("WRITE   %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
       
 24437   return got;
       
 24438 }
       
 24439 
       
 24440 
       
 24441 /*
       
 24442 ** Write data from a buffer into a file.  Return SQLITE_OK on success
       
 24443 ** or some other error code on failure.
       
 24444 */
       
 24445 static int unixWrite(
       
 24446   sqlite3_file *id, 
       
 24447   const void *pBuf, 
       
 24448   int amt,
       
 24449   sqlite3_int64 offset 
       
 24450 ){
       
 24451   unixFile *pFile = (unixFile*)id;
       
 24452   int wrote = 0;
       
 24453   assert( id );
       
 24454   assert( amt>0 );
       
 24455 
       
 24456   /* If this is a database file (not a journal, master-journal or temp
       
 24457   ** file), the bytes in the locking range should never be read or written. */
       
 24458   assert( pFile->pUnused==0
       
 24459        || offset>=PENDING_BYTE+512
       
 24460        || offset+amt<=PENDING_BYTE 
       
 24461   );
       
 24462 
       
 24463 #ifndef NDEBUG
       
 24464   /* If we are doing a normal write to a database file (as opposed to
       
 24465   ** doing a hot-journal rollback or a write to some file other than a
       
 24466   ** normal database file) then record the fact that the database
       
 24467   ** has changed.  If the transaction counter is modified, record that
       
 24468   ** fact too.
       
 24469   */
       
 24470   if( pFile->inNormalWrite ){
       
 24471     pFile->dbUpdate = 1;  /* The database has been modified */
       
 24472     if( offset<=24 && offset+amt>=27 ){
       
 24473       int rc;
       
 24474       char oldCntr[4];
       
 24475       SimulateIOErrorBenign(1);
       
 24476       rc = seekAndRead(pFile, 24, oldCntr, 4);
       
 24477       SimulateIOErrorBenign(0);
       
 24478       if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
       
 24479         pFile->transCntrChng = 1;  /* The transaction counter has changed */
       
 24480       }
       
 24481     }
       
 24482   }
       
 24483 #endif
       
 24484 
       
 24485   while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
       
 24486     amt -= wrote;
       
 24487     offset += wrote;
       
 24488     pBuf = &((char*)pBuf)[wrote];
       
 24489   }
       
 24490   SimulateIOError(( wrote=(-1), amt=1 ));
       
 24491   SimulateDiskfullError(( wrote=0, amt=1 ));
       
 24492   if( amt>0 ){
       
 24493     if( wrote<0 ){
       
 24494       /* lastErrno set by seekAndWrite */
       
 24495       return SQLITE_IOERR_WRITE;
       
 24496     }else{
       
 24497       pFile->lastErrno = 0; /* not a system error */
       
 24498       return SQLITE_FULL;
       
 24499     }
       
 24500   }
       
 24501   return SQLITE_OK;
       
 24502 }
       
 24503 
       
 24504 #ifdef SQLITE_TEST
       
 24505 /*
       
 24506 ** Count the number of fullsyncs and normal syncs.  This is used to test
       
 24507 ** that syncs and fullsyncs are occurring at the right times.
       
 24508 */
       
 24509 SQLITE_API int sqlite3_sync_count = 0;
       
 24510 SQLITE_API int sqlite3_fullsync_count = 0;
       
 24511 #endif
       
 24512 
       
 24513 /*
       
 24514 ** We do not trust systems to provide a working fdatasync().  Some do.
       
 24515 ** Others do no.  To be safe, we will stick with the (slower) fsync().
       
 24516 ** If you know that your system does support fdatasync() correctly,
       
 24517 ** then simply compile with -Dfdatasync=fdatasync
       
 24518 */
       
 24519 #if !defined(fdatasync) && !defined(__linux__)
       
 24520 # define fdatasync fsync
       
 24521 #endif
       
 24522 
       
 24523 /*
       
 24524 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
       
 24525 ** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
       
 24526 ** only available on Mac OS X.  But that could change.
       
 24527 */
       
 24528 #ifdef F_FULLFSYNC
       
 24529 # define HAVE_FULLFSYNC 1
       
 24530 #else
       
 24531 # define HAVE_FULLFSYNC 0
       
 24532 #endif
       
 24533 
       
 24534 
       
 24535 /*
       
 24536 ** The fsync() system call does not work as advertised on many
       
 24537 ** unix systems.  The following procedure is an attempt to make
       
 24538 ** it work better.
       
 24539 **
       
 24540 ** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
       
 24541 ** for testing when we want to run through the test suite quickly.
       
 24542 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
       
 24543 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
       
 24544 ** or power failure will likely corrupt the database file.
       
 24545 **
       
 24546 ** SQLite sets the dataOnly flag if the size of the file is unchanged.
       
 24547 ** The idea behind dataOnly is that it should only write the file content
       
 24548 ** to disk, not the inode.  We only set dataOnly if the file size is 
       
 24549 ** unchanged since the file size is part of the inode.  However, 
       
 24550 ** Ted Ts'o tells us that fdatasync() will also write the inode if the
       
 24551 ** file size has changed.  The only real difference between fdatasync()
       
 24552 ** and fsync(), Ted tells us, is that fdatasync() will not flush the
       
 24553 ** inode if the mtime or owner or other inode attributes have changed.
       
 24554 ** We only care about the file size, not the other file attributes, so
       
 24555 ** as far as SQLite is concerned, an fdatasync() is always adequate.
       
 24556 ** So, we always use fdatasync() if it is available, regardless of
       
 24557 ** the value of the dataOnly flag.
       
 24558 */
       
 24559 static int full_fsync(int fd, int fullSync, int dataOnly){
       
 24560   int rc;
       
 24561 
       
 24562   /* The following "ifdef/elif/else/" block has the same structure as
       
 24563   ** the one below. It is replicated here solely to avoid cluttering 
       
 24564   ** up the real code with the UNUSED_PARAMETER() macros.
       
 24565   */
       
 24566 #ifdef SQLITE_NO_SYNC
       
 24567   UNUSED_PARAMETER(fd);
       
 24568   UNUSED_PARAMETER(fullSync);
       
 24569   UNUSED_PARAMETER(dataOnly);
       
 24570 #elif HAVE_FULLFSYNC
       
 24571   UNUSED_PARAMETER(dataOnly);
       
 24572 #else
       
 24573   UNUSED_PARAMETER(fullSync);
       
 24574   UNUSED_PARAMETER(dataOnly);
       
 24575 #endif
       
 24576 
       
 24577   /* Record the number of times that we do a normal fsync() and 
       
 24578   ** FULLSYNC.  This is used during testing to verify that this procedure
       
 24579   ** gets called with the correct arguments.
       
 24580   */
       
 24581 #ifdef SQLITE_TEST
       
 24582   if( fullSync ) sqlite3_fullsync_count++;
       
 24583   sqlite3_sync_count++;
       
 24584 #endif
       
 24585 
       
 24586   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
       
 24587   ** no-op
       
 24588   */
       
 24589 #ifdef SQLITE_NO_SYNC
       
 24590   rc = SQLITE_OK;
       
 24591 #elif HAVE_FULLFSYNC
       
 24592   if( fullSync ){
       
 24593     rc = fcntl(fd, F_FULLFSYNC, 0);
       
 24594   }else{
       
 24595     rc = 1;
       
 24596   }
       
 24597   /* If the FULLFSYNC failed, fall back to attempting an fsync().
       
 24598   ** It shouldn't be possible for fullfsync to fail on the local 
       
 24599   ** file system (on OSX), so failure indicates that FULLFSYNC
       
 24600   ** isn't supported for this file system. So, attempt an fsync 
       
 24601   ** and (for now) ignore the overhead of a superfluous fcntl call.  
       
 24602   ** It'd be better to detect fullfsync support once and avoid 
       
 24603   ** the fcntl call every time sync is called.
       
 24604   */
       
 24605   if( rc ) rc = fsync(fd);
       
 24606 
       
 24607 #else 
       
 24608   rc = fdatasync(fd);
       
 24609 #if OS_VXWORKS
       
 24610   if( rc==-1 && errno==ENOTSUP ){
       
 24611     rc = fsync(fd);
       
 24612   }
       
 24613 #endif /* OS_VXWORKS */
       
 24614 #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
       
 24615 
       
 24616   if( OS_VXWORKS && rc!= -1 ){
       
 24617     rc = 0;
       
 24618   }
       
 24619   return rc;
       
 24620 }
       
 24621 
       
 24622 /*
       
 24623 ** Make sure all writes to a particular file are committed to disk.
       
 24624 **
       
 24625 ** If dataOnly==0 then both the file itself and its metadata (file
       
 24626 ** size, access time, etc) are synced.  If dataOnly!=0 then only the
       
 24627 ** file data is synced.
       
 24628 **
       
 24629 ** Under Unix, also make sure that the directory entry for the file
       
 24630 ** has been created by fsync-ing the directory that contains the file.
       
 24631 ** If we do not do this and we encounter a power failure, the directory
       
 24632 ** entry for the journal might not exist after we reboot.  The next
       
 24633 ** SQLite to access the file will not know that the journal exists (because
       
 24634 ** the directory entry for the journal was never created) and the transaction
       
 24635 ** will not roll back - possibly leading to database corruption.
       
 24636 */
       
 24637 static int unixSync(sqlite3_file *id, int flags){
       
 24638   int rc;
       
 24639   unixFile *pFile = (unixFile*)id;
       
 24640 
       
 24641   int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
       
 24642   int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
       
 24643 
       
 24644   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
       
 24645   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
       
 24646       || (flags&0x0F)==SQLITE_SYNC_FULL
       
 24647   );
       
 24648 
       
 24649   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
       
 24650   ** line is to test that doing so does not cause any problems.
       
 24651   */
       
 24652   SimulateDiskfullError( return SQLITE_FULL );
       
 24653 
       
 24654   assert( pFile );
       
 24655   OSTRACE2("SYNC    %-3d\n", pFile->h);
       
 24656   rc = full_fsync(pFile->h, isFullsync, isDataOnly);
       
 24657   SimulateIOError( rc=1 );
       
 24658   if( rc ){
       
 24659     pFile->lastErrno = errno;
       
 24660     return SQLITE_IOERR_FSYNC;
       
 24661   }
       
 24662   if( pFile->dirfd>=0 ){
       
 24663     int err;
       
 24664     OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
       
 24665             HAVE_FULLFSYNC, isFullsync);
       
 24666 #ifndef SQLITE_DISABLE_DIRSYNC
       
 24667     /* The directory sync is only attempted if full_fsync is
       
 24668     ** turned off or unavailable.  If a full_fsync occurred above,
       
 24669     ** then the directory sync is superfluous.
       
 24670     */
       
 24671     if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
       
 24672        /*
       
 24673        ** We have received multiple reports of fsync() returning
       
 24674        ** errors when applied to directories on certain file systems.
       
 24675        ** A failed directory sync is not a big deal.  So it seems
       
 24676        ** better to ignore the error.  Ticket #1657
       
 24677        */
       
 24678        /* pFile->lastErrno = errno; */
       
 24679        /* return SQLITE_IOERR; */
       
 24680     }
       
 24681 #endif
       
 24682     err = close(pFile->dirfd); /* Only need to sync once, so close the */
       
 24683     if( err==0 ){              /* directory when we are done */
       
 24684       pFile->dirfd = -1;
       
 24685     }else{
       
 24686       pFile->lastErrno = errno;
       
 24687       rc = SQLITE_IOERR_DIR_CLOSE;
       
 24688     }
       
 24689   }
       
 24690   return rc;
       
 24691 }
       
 24692 
       
 24693 /*
       
 24694 ** Truncate an open file to a specified size
       
 24695 */
       
 24696 static int unixTruncate(sqlite3_file *id, i64 nByte){
       
 24697   int rc;
       
 24698   assert( id );
       
 24699   SimulateIOError( return SQLITE_IOERR_TRUNCATE );
       
 24700   rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
       
 24701   if( rc ){
       
 24702     ((unixFile*)id)->lastErrno = errno;
       
 24703     return SQLITE_IOERR_TRUNCATE;
       
 24704   }else{
       
 24705     return SQLITE_OK;
       
 24706   }
       
 24707 }
       
 24708 
       
 24709 /*
       
 24710 ** Determine the current size of a file in bytes
       
 24711 */
       
 24712 static int unixFileSize(sqlite3_file *id, i64 *pSize){
       
 24713   int rc;
       
 24714   struct stat buf;
       
 24715   assert( id );
       
 24716   rc = fstat(((unixFile*)id)->h, &buf);
       
 24717   SimulateIOError( rc=1 );
       
 24718   if( rc!=0 ){
       
 24719     ((unixFile*)id)->lastErrno = errno;
       
 24720     return SQLITE_IOERR_FSTAT;
       
 24721   }
       
 24722   *pSize = buf.st_size;
       
 24723 
       
 24724   /* When opening a zero-size database, the findLockInfo() procedure
       
 24725   ** writes a single byte into that file in order to work around a bug
       
 24726   ** in the OS-X msdos filesystem.  In order to avoid problems with upper
       
 24727   ** layers, we need to report this file size as zero even though it is
       
 24728   ** really 1.   Ticket #3260.
       
 24729   */
       
 24730   if( *pSize==1 ) *pSize = 0;
       
 24731 
       
 24732 
       
 24733   return SQLITE_OK;
       
 24734 }
       
 24735 
       
 24736 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
       
 24737 /*
       
 24738 ** Handler for proxy-locking file-control verbs.  Defined below in the
       
 24739 ** proxying locking division.
       
 24740 */
       
 24741 static int proxyFileControl(sqlite3_file*,int,void*);
       
 24742 #endif
       
 24743 
       
 24744 
       
 24745 /*
       
 24746 ** Information and control of an open file handle.
       
 24747 */
       
 24748 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
       
 24749   switch( op ){
       
 24750     case SQLITE_FCNTL_LOCKSTATE: {
       
 24751       *(int*)pArg = ((unixFile*)id)->locktype;
       
 24752       return SQLITE_OK;
       
 24753     }
       
 24754     case SQLITE_LAST_ERRNO: {
       
 24755       *(int*)pArg = ((unixFile*)id)->lastErrno;
       
 24756       return SQLITE_OK;
       
 24757     }
       
 24758 #ifndef NDEBUG
       
 24759     /* The pager calls this method to signal that it has done
       
 24760     ** a rollback and that the database is therefore unchanged and
       
 24761     ** it hence it is OK for the transaction change counter to be
       
 24762     ** unchanged.
       
 24763     */
       
 24764     case SQLITE_FCNTL_DB_UNCHANGED: {
       
 24765       ((unixFile*)id)->dbUpdate = 0;
       
 24766       return SQLITE_OK;
       
 24767     }
       
 24768 #endif
       
 24769 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
       
 24770     case SQLITE_SET_LOCKPROXYFILE:
       
 24771     case SQLITE_GET_LOCKPROXYFILE: {
       
 24772       return proxyFileControl(id,op,pArg);
       
 24773     }
       
 24774 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
       
 24775   }
       
 24776   return SQLITE_ERROR;
       
 24777 }
       
 24778 
       
 24779 /*
       
 24780 ** Return the sector size in bytes of the underlying block device for
       
 24781 ** the specified file. This is almost always 512 bytes, but may be
       
 24782 ** larger for some devices.
       
 24783 **
       
 24784 ** SQLite code assumes this function cannot fail. It also assumes that
       
 24785 ** if two files are created in the same file-system directory (i.e.
       
 24786 ** a database and its journal file) that the sector size will be the
       
 24787 ** same for both.
       
 24788 */
       
 24789 static int unixSectorSize(sqlite3_file *NotUsed){
       
 24790   UNUSED_PARAMETER(NotUsed);
       
 24791   return SQLITE_DEFAULT_SECTOR_SIZE;
       
 24792 }
       
 24793 
       
 24794 /*
       
 24795 ** Return the device characteristics for the file. This is always 0 for unix.
       
 24796 */
       
 24797 static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
       
 24798   UNUSED_PARAMETER(NotUsed);
       
 24799   return 0;
       
 24800 }
       
 24801 
       
 24802 /*
       
 24803 ** Here ends the implementation of all sqlite3_file methods.
       
 24804 **
       
 24805 ********************** End sqlite3_file Methods *******************************
       
 24806 ******************************************************************************/
       
 24807 
       
 24808 /*
       
 24809 ** This division contains definitions of sqlite3_io_methods objects that
       
 24810 ** implement various file locking strategies.  It also contains definitions
       
 24811 ** of "finder" functions.  A finder-function is used to locate the appropriate
       
 24812 ** sqlite3_io_methods object for a particular database file.  The pAppData
       
 24813 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
       
 24814 ** the correct finder-function for that VFS.
       
 24815 **
       
 24816 ** Most finder functions return a pointer to a fixed sqlite3_io_methods
       
 24817 ** object.  The only interesting finder-function is autolockIoFinder, which
       
 24818 ** looks at the filesystem type and tries to guess the best locking
       
 24819 ** strategy from that.
       
 24820 **
       
 24821 ** For finder-funtion F, two objects are created:
       
 24822 **
       
 24823 **    (1) The real finder-function named "FImpt()".
       
 24824 **
       
 24825 **    (2) A constant pointer to this function named just "F".
       
 24826 **
       
 24827 **
       
 24828 ** A pointer to the F pointer is used as the pAppData value for VFS
       
 24829 ** objects.  We have to do this instead of letting pAppData point
       
 24830 ** directly at the finder-function since C90 rules prevent a void*
       
 24831 ** from be cast into a function pointer.
       
 24832 **
       
 24833 **
       
 24834 ** Each instance of this macro generates two objects:
       
 24835 **
       
 24836 **   *  A constant sqlite3_io_methods object call METHOD that has locking
       
 24837 **      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
       
 24838 **
       
 24839 **   *  An I/O method finder function called FINDER that returns a pointer
       
 24840 **      to the METHOD object in the previous bullet.
       
 24841 */
       
 24842 #define IOMETHODS(FINDER, METHOD, CLOSE, LOCK, UNLOCK, CKLOCK)               \
       
 24843 static const sqlite3_io_methods METHOD = {                                   \
       
 24844    1,                          /* iVersion */                                \
       
 24845    CLOSE,                      /* xClose */                                  \
       
 24846    unixRead,                   /* xRead */                                   \
       
 24847    unixWrite,                  /* xWrite */                                  \
       
 24848    unixTruncate,               /* xTruncate */                               \
       
 24849    unixSync,                   /* xSync */                                   \
       
 24850    unixFileSize,               /* xFileSize */                               \
       
 24851    LOCK,                       /* xLock */                                   \
       
 24852    UNLOCK,                     /* xUnlock */                                 \
       
 24853    CKLOCK,                     /* xCheckReservedLock */                      \
       
 24854    unixFileControl,            /* xFileControl */                            \
       
 24855    unixSectorSize,             /* xSectorSize */                             \
       
 24856    unixDeviceCharacteristics   /* xDeviceCapabilities */                     \
       
 24857 };                                                                           \
       
 24858 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
       
 24859   UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
       
 24860   return &METHOD;                                                            \
       
 24861 }                                                                            \
       
 24862 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
       
 24863     = FINDER##Impl;
       
 24864 
       
 24865 /*
       
 24866 ** Here are all of the sqlite3_io_methods objects for each of the
       
 24867 ** locking strategies.  Functions that return pointers to these methods
       
 24868 ** are also created.
       
 24869 */
       
 24870 IOMETHODS(
       
 24871   posixIoFinder,            /* Finder function name */
       
 24872   posixIoMethods,           /* sqlite3_io_methods object name */
       
 24873   unixClose,                /* xClose method */
       
 24874   unixLock,                 /* xLock method */
       
 24875   unixUnlock,               /* xUnlock method */
       
 24876   unixCheckReservedLock     /* xCheckReservedLock method */
       
 24877 )
       
 24878 IOMETHODS(
       
 24879   nolockIoFinder,           /* Finder function name */
       
 24880   nolockIoMethods,          /* sqlite3_io_methods object name */
       
 24881   nolockClose,              /* xClose method */
       
 24882   nolockLock,               /* xLock method */
       
 24883   nolockUnlock,             /* xUnlock method */
       
 24884   nolockCheckReservedLock   /* xCheckReservedLock method */
       
 24885 )
       
 24886 IOMETHODS(
       
 24887   dotlockIoFinder,          /* Finder function name */
       
 24888   dotlockIoMethods,         /* sqlite3_io_methods object name */
       
 24889   dotlockClose,             /* xClose method */
       
 24890   dotlockLock,              /* xLock method */
       
 24891   dotlockUnlock,            /* xUnlock method */
       
 24892   dotlockCheckReservedLock  /* xCheckReservedLock method */
       
 24893 )
       
 24894 
       
 24895 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
       
 24896 IOMETHODS(
       
 24897   flockIoFinder,            /* Finder function name */
       
 24898   flockIoMethods,           /* sqlite3_io_methods object name */
       
 24899   flockClose,               /* xClose method */
       
 24900   flockLock,                /* xLock method */
       
 24901   flockUnlock,              /* xUnlock method */
       
 24902   flockCheckReservedLock    /* xCheckReservedLock method */
       
 24903 )
       
 24904 #endif
       
 24905 
       
 24906 #if OS_VXWORKS
       
 24907 IOMETHODS(
       
 24908   semIoFinder,              /* Finder function name */
       
 24909   semIoMethods,             /* sqlite3_io_methods object name */
       
 24910   semClose,                 /* xClose method */
       
 24911   semLock,                  /* xLock method */
       
 24912   semUnlock,                /* xUnlock method */
       
 24913   semCheckReservedLock      /* xCheckReservedLock method */
       
 24914 )
       
 24915 #endif
       
 24916 
       
 24917 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
       
 24918 IOMETHODS(
       
 24919   afpIoFinder,              /* Finder function name */
       
 24920   afpIoMethods,             /* sqlite3_io_methods object name */
       
 24921   afpClose,                 /* xClose method */
       
 24922   afpLock,                  /* xLock method */
       
 24923   afpUnlock,                /* xUnlock method */
       
 24924   afpCheckReservedLock      /* xCheckReservedLock method */
       
 24925 )
       
 24926 #endif
       
 24927 
       
 24928 /*
       
 24929 ** The "Whole File Locking" finder returns the same set of methods as
       
 24930 ** the posix locking finder.  But it also sets the SQLITE_WHOLE_FILE_LOCKING
       
 24931 ** flag to force the posix advisory locks to cover the whole file instead
       
 24932 ** of just a small span of bytes near the 1GiB boundary.  Whole File Locking
       
 24933 ** is useful on NFS-mounted files since it helps NFS to maintain cache
       
 24934 ** coherency.  But it is a detriment to other filesystems since it runs
       
 24935 ** slower.
       
 24936 */
       
 24937 static const sqlite3_io_methods *posixWflIoFinderImpl(const char*z, unixFile*p){
       
 24938   UNUSED_PARAMETER(z);
       
 24939   p->fileFlags = SQLITE_WHOLE_FILE_LOCKING;
       
 24940   return &posixIoMethods;
       
 24941 }
       
 24942 static const sqlite3_io_methods 
       
 24943   *(*const posixWflIoFinder)(const char*,unixFile *p) = posixWflIoFinderImpl;
       
 24944 
       
 24945 /*
       
 24946 ** The proxy locking method is a "super-method" in the sense that it
       
 24947 ** opens secondary file descriptors for the conch and lock files and
       
 24948 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
       
 24949 ** secondary files.  For this reason, the division that implements
       
 24950 ** proxy locking is located much further down in the file.  But we need
       
 24951 ** to go ahead and define the sqlite3_io_methods and finder function
       
 24952 ** for proxy locking here.  So we forward declare the I/O methods.
       
 24953 */
       
 24954 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
       
 24955 static int proxyClose(sqlite3_file*);
       
 24956 static int proxyLock(sqlite3_file*, int);
       
 24957 static int proxyUnlock(sqlite3_file*, int);
       
 24958 static int proxyCheckReservedLock(sqlite3_file*, int*);
       
 24959 IOMETHODS(
       
 24960   proxyIoFinder,            /* Finder function name */
       
 24961   proxyIoMethods,           /* sqlite3_io_methods object name */
       
 24962   proxyClose,               /* xClose method */
       
 24963   proxyLock,                /* xLock method */
       
 24964   proxyUnlock,              /* xUnlock method */
       
 24965   proxyCheckReservedLock    /* xCheckReservedLock method */
       
 24966 )
       
 24967 #endif
       
 24968 
       
 24969 
       
 24970 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
       
 24971 /* 
       
 24972 ** This "finder" function attempts to determine the best locking strategy 
       
 24973 ** for the database file "filePath".  It then returns the sqlite3_io_methods
       
 24974 ** object that implements that strategy.
       
 24975 **
       
 24976 ** This is for MacOSX only.
       
 24977 */
       
 24978 static const sqlite3_io_methods *autolockIoFinderImpl(
       
 24979   const char *filePath,    /* name of the database file */
       
 24980   unixFile *pNew           /* open file object for the database file */
       
 24981 ){
       
 24982   static const struct Mapping {
       
 24983     const char *zFilesystem;              /* Filesystem type name */
       
 24984     const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
       
 24985   } aMap[] = {
       
 24986     { "hfs",    &posixIoMethods },
       
 24987     { "ufs",    &posixIoMethods },
       
 24988     { "afpfs",  &afpIoMethods },
       
 24989 #ifdef SQLITE_ENABLE_AFP_LOCKING_SMB
       
 24990     { "smbfs",  &afpIoMethods },
       
 24991 #else
       
 24992     { "smbfs",  &flockIoMethods },
       
 24993 #endif
       
 24994     { "webdav", &nolockIoMethods },
       
 24995     { 0, 0 }
       
 24996   };
       
 24997   int i;
       
 24998   struct statfs fsInfo;
       
 24999   struct flock lockInfo;
       
 25000 
       
 25001   if( !filePath ){
       
 25002     /* If filePath==NULL that means we are dealing with a transient file
       
 25003     ** that does not need to be locked. */
       
 25004     return &nolockIoMethods;
       
 25005   }
       
 25006   if( statfs(filePath, &fsInfo) != -1 ){
       
 25007     if( fsInfo.f_flags & MNT_RDONLY ){
       
 25008       return &nolockIoMethods;
       
 25009     }
       
 25010     for(i=0; aMap[i].zFilesystem; i++){
       
 25011       if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
       
 25012         return aMap[i].pMethods;
       
 25013       }
       
 25014     }
       
 25015   }
       
 25016 
       
 25017   /* Default case. Handles, amongst others, "nfs".
       
 25018   ** Test byte-range lock using fcntl(). If the call succeeds, 
       
 25019   ** assume that the file-system supports POSIX style locks. 
       
 25020   */
       
 25021   lockInfo.l_len = 1;
       
 25022   lockInfo.l_start = 0;
       
 25023   lockInfo.l_whence = SEEK_SET;
       
 25024   lockInfo.l_type = F_RDLCK;
       
 25025   if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
       
 25026     pNew->fileFlags = SQLITE_WHOLE_FILE_LOCKING;
       
 25027     return &posixIoMethods;
       
 25028   }else{
       
 25029     return &dotlockIoMethods;
       
 25030   }
       
 25031 }
       
 25032 static const sqlite3_io_methods 
       
 25033   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
       
 25034 
       
 25035 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
       
 25036 
       
 25037 #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE
       
 25038 /* 
       
 25039 ** This "finder" function attempts to determine the best locking strategy 
       
 25040 ** for the database file "filePath".  It then returns the sqlite3_io_methods
       
 25041 ** object that implements that strategy.
       
 25042 **
       
 25043 ** This is for VXWorks only.
       
 25044 */
       
 25045 static const sqlite3_io_methods *autolockIoFinderImpl(
       
 25046   const char *filePath,    /* name of the database file */
       
 25047   unixFile *pNew           /* the open file object */
       
 25048 ){
       
 25049   struct flock lockInfo;
       
 25050 
       
 25051   if( !filePath ){
       
 25052     /* If filePath==NULL that means we are dealing with a transient file
       
 25053     ** that does not need to be locked. */
       
 25054     return &nolockIoMethods;
       
 25055   }
       
 25056 
       
 25057   /* Test if fcntl() is supported and use POSIX style locks.
       
 25058   ** Otherwise fall back to the named semaphore method.
       
 25059   */
       
 25060   lockInfo.l_len = 1;
       
 25061   lockInfo.l_start = 0;
       
 25062   lockInfo.l_whence = SEEK_SET;
       
 25063   lockInfo.l_type = F_RDLCK;
       
 25064   if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
       
 25065     return &posixIoMethods;
       
 25066   }else{
       
 25067     return &semIoMethods;
       
 25068   }
       
 25069 }
       
 25070 static const sqlite3_io_methods 
       
 25071   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
       
 25072 
       
 25073 #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
       
 25074 
       
 25075 /*
       
 25076 ** An abstract type for a pointer to a IO method finder function:
       
 25077 */
       
 25078 typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
       
 25079 
       
 25080 
       
 25081 /****************************************************************************
       
 25082 **************************** sqlite3_vfs methods ****************************
       
 25083 **
       
 25084 ** This division contains the implementation of methods on the
       
 25085 ** sqlite3_vfs object.
       
 25086 */
       
 25087 
       
 25088 /*
       
 25089 ** Initialize the contents of the unixFile structure pointed to by pId.
       
 25090 */
       
 25091 static int fillInUnixFile(
       
 25092   sqlite3_vfs *pVfs,      /* Pointer to vfs object */
       
 25093   int h,                  /* Open file descriptor of file being opened */
       
 25094   int dirfd,              /* Directory file descriptor */
       
 25095   sqlite3_file *pId,      /* Write to the unixFile structure here */
       
 25096   const char *zFilename,  /* Name of the file being opened */
       
 25097   int noLock,             /* Omit locking if true */
       
 25098   int isDelete            /* Delete on close if true */
       
 25099 ){
       
 25100   const sqlite3_io_methods *pLockingStyle;
       
 25101   unixFile *pNew = (unixFile *)pId;
       
 25102   int rc = SQLITE_OK;
       
 25103 
       
 25104   assert( pNew->pLock==NULL );
       
 25105   assert( pNew->pOpen==NULL );
       
 25106 
       
 25107   /* Parameter isDelete is only used on vxworks. Express this explicitly 
       
 25108   ** here to prevent compiler warnings about unused parameters.
       
 25109   */
       
 25110   UNUSED_PARAMETER(isDelete);
       
 25111 
       
 25112   OSTRACE3("OPEN    %-3d %s\n", h, zFilename);    
       
 25113   pNew->h = h;
       
 25114   pNew->dirfd = dirfd;
       
 25115   SET_THREADID(pNew);
       
 25116   pNew->fileFlags = 0;
       
 25117 
       
 25118 #if OS_VXWORKS
       
 25119   pNew->pId = vxworksFindFileId(zFilename);
       
 25120   if( pNew->pId==0 ){
       
 25121     noLock = 1;
       
 25122     rc = SQLITE_NOMEM;
       
 25123   }
       
 25124 #endif
       
 25125 
       
 25126   if( noLock ){
       
 25127     pLockingStyle = &nolockIoMethods;
       
 25128   }else{
       
 25129     pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
       
 25130 #if SQLITE_ENABLE_LOCKING_STYLE
       
 25131     /* Cache zFilename in the locking context (AFP and dotlock override) for
       
 25132     ** proxyLock activation is possible (remote proxy is based on db name)
       
 25133     ** zFilename remains valid until file is closed, to support */
       
 25134     pNew->lockingContext = (void*)zFilename;
       
 25135 #endif
       
 25136   }
       
 25137 
       
 25138   if( pLockingStyle == &posixIoMethods ){
       
 25139     unixEnterMutex();
       
 25140     rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
       
 25141     if( rc!=SQLITE_OK ){
       
 25142       /* If an error occured in findLockInfo(), close the file descriptor
       
 25143       ** immediately, before releasing the mutex. findLockInfo() may fail
       
 25144       ** in two scenarios:
       
 25145       **
       
 25146       **   (a) A call to fstat() failed.
       
 25147       **   (b) A malloc failed.
       
 25148       **
       
 25149       ** Scenario (b) may only occur if the process is holding no other
       
 25150       ** file descriptors open on the same file. If there were other file
       
 25151       ** descriptors on this file, then no malloc would be required by
       
 25152       ** findLockInfo(). If this is the case, it is quite safe to close
       
 25153       ** handle h - as it is guaranteed that no posix locks will be released
       
 25154       ** by doing so.
       
 25155       **
       
 25156       ** If scenario (a) caused the error then things are not so safe. The
       
 25157       ** implicit assumption here is that if fstat() fails, things are in
       
 25158       ** such bad shape that dropping a lock or two doesn't matter much.
       
 25159       */
       
 25160       close(h);
       
 25161       h = -1;
       
 25162     }
       
 25163     unixLeaveMutex();
       
 25164   }
       
 25165 
       
 25166 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
       
 25167   else if( pLockingStyle == &afpIoMethods ){
       
 25168     /* AFP locking uses the file path so it needs to be included in
       
 25169     ** the afpLockingContext.
       
 25170     */
       
 25171     afpLockingContext *pCtx;
       
 25172     pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
       
 25173     if( pCtx==0 ){
       
 25174       rc = SQLITE_NOMEM;
       
 25175     }else{
       
 25176       /* NB: zFilename exists and remains valid until the file is closed
       
 25177       ** according to requirement F11141.  So we do not need to make a
       
 25178       ** copy of the filename. */
       
 25179       pCtx->dbPath = zFilename;
       
 25180       srandomdev();
       
 25181       unixEnterMutex();
       
 25182       rc = findLockInfo(pNew, NULL, &pNew->pOpen);
       
 25183       unixLeaveMutex();        
       
 25184     }
       
 25185   }
       
 25186 #endif
       
 25187 
       
 25188   else if( pLockingStyle == &dotlockIoMethods ){
       
 25189     /* Dotfile locking uses the file path so it needs to be included in
       
 25190     ** the dotlockLockingContext 
       
 25191     */
       
 25192     char *zLockFile;
       
 25193     int nFilename;
       
 25194     nFilename = (int)strlen(zFilename) + 6;
       
 25195     zLockFile = (char *)sqlite3_malloc(nFilename);
       
 25196     if( zLockFile==0 ){
       
 25197       rc = SQLITE_NOMEM;
       
 25198     }else{
       
 25199       sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
       
 25200     }
       
 25201     pNew->lockingContext = zLockFile;
       
 25202   }
       
 25203 
       
 25204 #if OS_VXWORKS
       
 25205   else if( pLockingStyle == &semIoMethods ){
       
 25206     /* Named semaphore locking uses the file path so it needs to be
       
 25207     ** included in the semLockingContext
       
 25208     */
       
 25209     unixEnterMutex();
       
 25210     rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
       
 25211     if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){
       
 25212       char *zSemName = pNew->pOpen->aSemName;
       
 25213       int n;
       
 25214       sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
       
 25215                        pNew->pId->zCanonicalName);
       
 25216       for( n=1; zSemName[n]; n++ )
       
 25217         if( zSemName[n]=='/' ) zSemName[n] = '_';
       
 25218       pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
       
 25219       if( pNew->pOpen->pSem == SEM_FAILED ){
       
 25220         rc = SQLITE_NOMEM;
       
 25221         pNew->pOpen->aSemName[0] = '\0';
       
 25222       }
       
 25223     }
       
 25224     unixLeaveMutex();
       
 25225   }
       
 25226 #endif
       
 25227   
       
 25228   pNew->lastErrno = 0;
       
 25229 #if OS_VXWORKS
       
 25230   if( rc!=SQLITE_OK ){
       
 25231     unlink(zFilename);
       
 25232     isDelete = 0;
       
 25233   }
       
 25234   pNew->isDelete = isDelete;
       
 25235 #endif
       
 25236   if( rc!=SQLITE_OK ){
       
 25237     if( dirfd>=0 ) close(dirfd); /* silent leak if fail, already in error */
       
 25238     if( h>=0 ) close(h);
       
 25239   }else{
       
 25240     pNew->pMethod = pLockingStyle;
       
 25241     OpenCounter(+1);
       
 25242   }
       
 25243   return rc;
       
 25244 }
       
 25245 
       
 25246 /*
       
 25247 ** Open a file descriptor to the directory containing file zFilename.
       
 25248 ** If successful, *pFd is set to the opened file descriptor and
       
 25249 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
       
 25250 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
       
 25251 ** value.
       
 25252 **
       
 25253 ** If SQLITE_OK is returned, the caller is responsible for closing
       
 25254 ** the file descriptor *pFd using close().
       
 25255 */
       
 25256 static int openDirectory(const char *zFilename, int *pFd){
       
 25257   int ii;
       
 25258   int fd = -1;
       
 25259   char zDirname[MAX_PATHNAME+1];
       
 25260 
       
 25261   sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
       
 25262   for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
       
 25263   if( ii>0 ){
       
 25264     zDirname[ii] = '\0';
       
 25265     fd = open(zDirname, O_RDONLY|O_BINARY, 0);
       
 25266     if( fd>=0 ){
       
 25267 #ifdef FD_CLOEXEC
       
 25268       fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
       
 25269 #endif
       
 25270       OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
       
 25271     }
       
 25272   }
       
 25273   *pFd = fd;
       
 25274   return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
       
 25275 }
       
 25276 
       
 25277 /*
       
 25278 ** Create a temporary file name in zBuf.  zBuf must be allocated
       
 25279 ** by the calling process and must be big enough to hold at least
       
 25280 ** pVfs->mxPathname bytes.
       
 25281 */
       
 25282 static int getTempname(int nBuf, char *zBuf){
       
 25283   static const char *azDirs[] = {
       
 25284      0,
       
 25285      0,
       
 25286      "/var/tmp",
       
 25287      "/usr/tmp",
       
 25288      "/tmp",
       
 25289      ".",
       
 25290   };
       
 25291   static const unsigned char zChars[] =
       
 25292     "abcdefghijklmnopqrstuvwxyz"
       
 25293     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
       
 25294     "0123456789";
       
 25295   unsigned int i, j;
       
 25296   struct stat buf;
       
 25297   const char *zDir = ".";
       
 25298 
       
 25299   /* It's odd to simulate an io-error here, but really this is just
       
 25300   ** using the io-error infrastructure to test that SQLite handles this
       
 25301   ** function failing. 
       
 25302   */
       
 25303   SimulateIOError( return SQLITE_IOERR );
       
 25304 
       
 25305   azDirs[0] = sqlite3_temp_directory;
       
 25306   if (NULL == azDirs[1]) {
       
 25307     azDirs[1] = getenv("TMPDIR");
       
 25308   }
       
 25309   
       
 25310   for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
       
 25311     if( azDirs[i]==0 ) continue;
       
 25312     if( stat(azDirs[i], &buf) ) continue;
       
 25313     if( !S_ISDIR(buf.st_mode) ) continue;
       
 25314     if( access(azDirs[i], 07) ) continue;
       
 25315     zDir = azDirs[i];
       
 25316     break;
       
 25317   }
       
 25318 
       
 25319   /* Check that the output buffer is large enough for the temporary file 
       
 25320   ** name. If it is not, return SQLITE_ERROR.
       
 25321   */
       
 25322   if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
       
 25323     return SQLITE_ERROR;
       
 25324   }
       
 25325 
       
 25326   do{
       
 25327     sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
       
 25328     j = (int)strlen(zBuf);
       
 25329     sqlite3_randomness(15, &zBuf[j]);
       
 25330     for(i=0; i<15; i++, j++){
       
 25331       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
       
 25332     }
       
 25333     zBuf[j] = 0;
       
 25334   }while( access(zBuf,0)==0 );
       
 25335   return SQLITE_OK;
       
 25336 }
       
 25337 
       
 25338 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
       
 25339 /*
       
 25340 ** Routine to transform a unixFile into a proxy-locking unixFile.
       
 25341 ** Implementation in the proxy-lock division, but used by unixOpen()
       
 25342 ** if SQLITE_PREFER_PROXY_LOCKING is defined.
       
 25343 */
       
 25344 static int proxyTransformUnixFile(unixFile*, const char*);
       
 25345 #endif
       
 25346 
       
 25347 /*
       
 25348 ** Search for an unused file descriptor that was opened on the database 
       
 25349 ** file (not a journal or master-journal file) identified by pathname
       
 25350 ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
       
 25351 ** argument to this function.
       
 25352 **
       
 25353 ** Such a file descriptor may exist if a database connection was closed
       
 25354 ** but the associated file descriptor could not be closed because some
       
 25355 ** other file descriptor open on the same file is holding a file-lock.
       
 25356 ** Refer to comments in the unixClose() function and the lengthy comment
       
 25357 ** describing "Posix Advisory Locking" at the start of this file for 
       
 25358 ** further details. Also, ticket #4018.
       
 25359 **
       
 25360 ** If a suitable file descriptor is found, then it is returned. If no
       
 25361 ** such file descriptor is located, -1 is returned.
       
 25362 */
       
 25363 static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
       
 25364   UnixUnusedFd *pUnused = 0;
       
 25365 
       
 25366   /* Do not search for an unused file descriptor on vxworks. Not because
       
 25367   ** vxworks would not benefit from the change (it might, we're not sure),
       
 25368   ** but because no way to test it is currently available. It is better 
       
 25369   ** not to risk breaking vxworks support for the sake of such an obscure 
       
 25370   ** feature.  */
       
 25371 #if !OS_VXWORKS
       
 25372   struct stat sStat;                   /* Results of stat() call */
       
 25373 
       
 25374   /* A stat() call may fail for various reasons. If this happens, it is
       
 25375   ** almost certain that an open() call on the same path will also fail.
       
 25376   ** For this reason, if an error occurs in the stat() call here, it is
       
 25377   ** ignored and -1 is returned. The caller will try to open a new file
       
 25378   ** descriptor on the same path, fail, and return an error to SQLite.
       
 25379   **
       
 25380   ** Even if a subsequent open() call does succeed, the consequences of
       
 25381   ** not searching for a resusable file descriptor are not dire.  */
       
 25382   if( 0==stat(zPath, &sStat) ){
       
 25383     struct unixOpenCnt *pO;
       
 25384     struct unixFileId id;
       
 25385     id.dev = sStat.st_dev;
       
 25386     id.ino = sStat.st_ino;
       
 25387 
       
 25388     unixEnterMutex();
       
 25389     for(pO=openList; pO && memcmp(&id, &pO->fileId, sizeof(id)); pO=pO->pNext);
       
 25390     if( pO ){
       
 25391       UnixUnusedFd **pp;
       
 25392       for(pp=&pO->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
       
 25393       pUnused = *pp;
       
 25394       if( pUnused ){
       
 25395         *pp = pUnused->pNext;
       
 25396       }
       
 25397     }
       
 25398     unixLeaveMutex();
       
 25399   }
       
 25400 #endif    /* if !OS_VXWORKS */
       
 25401   return pUnused;
       
 25402 }
       
 25403 
       
 25404 /*
       
 25405 ** Open the file zPath.
       
 25406 ** 
       
 25407 ** Previously, the SQLite OS layer used three functions in place of this
       
 25408 ** one:
       
 25409 **
       
 25410 **     sqlite3OsOpenReadWrite();
       
 25411 **     sqlite3OsOpenReadOnly();
       
 25412 **     sqlite3OsOpenExclusive();
       
 25413 **
       
 25414 ** These calls correspond to the following combinations of flags:
       
 25415 **
       
 25416 **     ReadWrite() ->     (READWRITE | CREATE)
       
 25417 **     ReadOnly()  ->     (READONLY) 
       
 25418 **     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
       
 25419 **
       
 25420 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
       
 25421 ** true, the file was configured to be automatically deleted when the
       
 25422 ** file handle closed. To achieve the same effect using this new 
       
 25423 ** interface, add the DELETEONCLOSE flag to those specified above for 
       
 25424 ** OpenExclusive().
       
 25425 */
       
 25426 static int unixOpen(
       
 25427   sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
       
 25428   const char *zPath,           /* Pathname of file to be opened */
       
 25429   sqlite3_file *pFile,         /* The file descriptor to be filled in */
       
 25430   int flags,                   /* Input flags to control the opening */
       
 25431   int *pOutFlags               /* Output flags returned to SQLite core */
       
 25432 ){
       
 25433   unixFile *p = (unixFile *)pFile;
       
 25434   int fd = -1;                   /* File descriptor returned by open() */
       
 25435   int dirfd = -1;                /* Directory file descriptor */
       
 25436   int openFlags = 0;             /* Flags to pass to open() */
       
 25437   int eType = flags&0xFFFFFF00;  /* Type of file to open */
       
 25438   int noLock;                    /* True to omit locking primitives */
       
 25439   int rc = SQLITE_OK;            /* Function Return Code */
       
 25440 
       
 25441   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
       
 25442   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
       
 25443   int isCreate     = (flags & SQLITE_OPEN_CREATE);
       
 25444   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
       
 25445   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
       
 25446 
       
 25447   /* If creating a master or main-file journal, this function will open
       
 25448   ** a file-descriptor on the directory too. The first time unixSync()
       
 25449   ** is called the directory file descriptor will be fsync()ed and close()d.
       
 25450   */
       
 25451   int isOpenDirectory = (isCreate && 
       
 25452       (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
       
 25453   );
       
 25454 
       
 25455   /* If argument zPath is a NULL pointer, this function is required to open
       
 25456   ** a temporary file. Use this buffer to store the file name in.
       
 25457   */
       
 25458   char zTmpname[MAX_PATHNAME+1];
       
 25459   const char *zName = zPath;
       
 25460 
       
 25461   /* Check the following statements are true: 
       
 25462   **
       
 25463   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
       
 25464   **   (b) if CREATE is set, then READWRITE must also be set, and
       
 25465   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
       
 25466   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
       
 25467   */
       
 25468   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
       
 25469   assert(isCreate==0 || isReadWrite);
       
 25470   assert(isExclusive==0 || isCreate);
       
 25471   assert(isDelete==0 || isCreate);
       
 25472 
       
 25473   /* The main DB, main journal, and master journal are never automatically
       
 25474   ** deleted. Nor are they ever temporary files.  */
       
 25475   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
       
 25476   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
       
 25477   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
       
 25478 
       
 25479   /* Assert that the upper layer has set one of the "file-type" flags. */
       
 25480   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
       
 25481        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
       
 25482        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
       
 25483        || eType==SQLITE_OPEN_TRANSIENT_DB
       
 25484   );
       
 25485 
       
 25486   memset(p, 0, sizeof(unixFile));
       
 25487 
       
 25488   if( eType==SQLITE_OPEN_MAIN_DB ){
       
 25489     UnixUnusedFd *pUnused;
       
 25490     pUnused = findReusableFd(zName, flags);
       
 25491     if( pUnused ){
       
 25492       fd = pUnused->fd;
       
 25493     }else{
       
 25494       pUnused = sqlite3_malloc(sizeof(*pUnused));
       
 25495       if( !pUnused ){
       
 25496         return SQLITE_NOMEM;
       
 25497       }
       
 25498     }
       
 25499     p->pUnused = pUnused;
       
 25500   }else if( !zName ){
       
 25501     /* If zName is NULL, the upper layer is requesting a temp file. */
       
 25502     assert(isDelete && !isOpenDirectory);
       
 25503     rc = getTempname(MAX_PATHNAME+1, zTmpname);
       
 25504     if( rc!=SQLITE_OK ){
       
 25505       return rc;
       
 25506     }
       
 25507     zName = zTmpname;
       
 25508   }
       
 25509 
       
 25510   /* Determine the value of the flags parameter passed to POSIX function
       
 25511   ** open(). These must be calculated even if open() is not called, as
       
 25512   ** they may be stored as part of the file handle and used by the 
       
 25513   ** 'conch file' locking functions later on.  */
       
 25514   if( isReadonly )  openFlags |= O_RDONLY;
       
 25515   if( isReadWrite ) openFlags |= O_RDWR;
       
 25516   if( isCreate )    openFlags |= O_CREAT;
       
 25517   if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
       
 25518   openFlags |= (O_LARGEFILE|O_BINARY);
       
 25519 
       
 25520   if( fd<0 ){
       
 25521     mode_t openMode = (isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
       
 25522     fd = open(zName, openFlags, openMode);
       
 25523     OSTRACE4("OPENX   %-3d %s 0%o\n", fd, zName, openFlags);
       
 25524     if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
       
 25525       /* Failed to open the file for read/write access. Try read-only. */
       
 25526       flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
       
 25527       openFlags &= ~(O_RDWR|O_CREAT);
       
 25528       flags |= SQLITE_OPEN_READONLY;
       
 25529       openFlags |= O_RDONLY;
       
 25530       fd = open(zName, openFlags, openMode);
       
 25531     }
       
 25532     if( fd<0 ){
       
 25533       rc = SQLITE_CANTOPEN;
       
 25534       goto open_finished;
       
 25535     }
       
 25536   }
       
 25537   assert( fd>=0 );
       
 25538   if( pOutFlags ){
       
 25539     *pOutFlags = flags;
       
 25540   }
       
 25541 
       
 25542   if( p->pUnused ){
       
 25543     p->pUnused->fd = fd;
       
 25544     p->pUnused->flags = flags;
       
 25545   }
       
 25546 
       
 25547   if( isDelete ){
       
 25548 #if OS_VXWORKS
       
 25549     zPath = zName;
       
 25550 #else
       
 25551     unlink(zName);
       
 25552 #endif
       
 25553   }
       
 25554 #if SQLITE_ENABLE_LOCKING_STYLE
       
 25555   else{
       
 25556     p->openFlags = openFlags;
       
 25557   }
       
 25558 #endif
       
 25559 
       
 25560   if( isOpenDirectory ){
       
 25561     rc = openDirectory(zPath, &dirfd);
       
 25562     if( rc!=SQLITE_OK ){
       
 25563       /* It is safe to close fd at this point, because it is guaranteed not
       
 25564       ** to be open on a database file. If it were open on a database file,
       
 25565       ** it would not be safe to close as this would release any locks held
       
 25566       ** on the file by this process.  */
       
 25567       assert( eType!=SQLITE_OPEN_MAIN_DB );
       
 25568       close(fd);             /* silently leak if fail, already in error */
       
 25569       goto open_finished;
       
 25570     }
       
 25571   }
       
 25572 
       
 25573 #ifdef FD_CLOEXEC
       
 25574   fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
       
 25575 #endif
       
 25576 
       
 25577   noLock = eType!=SQLITE_OPEN_MAIN_DB;
       
 25578 
       
 25579 #if SQLITE_PREFER_PROXY_LOCKING
       
 25580   if( zPath!=NULL && !noLock && pVfs->xOpen ){
       
 25581     char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
       
 25582     int useProxy = 0;
       
 25583 
       
 25584     /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means 
       
 25585     ** never use proxy, NULL means use proxy for non-local files only.  */
       
 25586     if( envforce!=NULL ){
       
 25587       useProxy = atoi(envforce)>0;
       
 25588     }else{
       
 25589       struct statfs fsInfo;
       
 25590       if( statfs(zPath, &fsInfo) == -1 ){
       
 25591         /* In theory, the close(fd) call is sub-optimal. If the file opened
       
 25592         ** with fd is a database file, and there are other connections open
       
 25593         ** on that file that are currently holding advisory locks on it,
       
 25594         ** then the call to close() will cancel those locks. In practice,
       
 25595         ** we're assuming that statfs() doesn't fail very often. At least
       
 25596         ** not while other file descriptors opened by the same process on
       
 25597         ** the same file are working.  */
       
 25598         p->lastErrno = errno;
       
 25599         if( dirfd>=0 ){
       
 25600           close(dirfd); /* silently leak if fail, in error */
       
 25601         }
       
 25602         close(fd); /* silently leak if fail, in error */
       
 25603         rc = SQLITE_IOERR_ACCESS;
       
 25604         goto open_finished;
       
 25605       }
       
 25606       useProxy = !(fsInfo.f_flags&MNT_LOCAL);
       
 25607     }
       
 25608     if( useProxy ){
       
 25609       rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
       
 25610       if( rc==SQLITE_OK ){
       
 25611         rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
       
 25612       }
       
 25613       goto open_finished;
       
 25614     }
       
 25615   }
       
 25616 #endif
       
 25617   
       
 25618   rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
       
 25619 open_finished:
       
 25620   if( rc!=SQLITE_OK ){
       
 25621     sqlite3_free(p->pUnused);
       
 25622   }
       
 25623   return rc;
       
 25624 }
       
 25625 
       
 25626 
       
 25627 /*
       
 25628 ** Delete the file at zPath. If the dirSync argument is true, fsync()
       
 25629 ** the directory after deleting the file.
       
 25630 */
       
 25631 static int unixDelete(
       
 25632   sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
       
 25633   const char *zPath,        /* Name of file to be deleted */
       
 25634   int dirSync               /* If true, fsync() directory after deleting file */
       
 25635 ){
       
 25636   int rc = SQLITE_OK;
       
 25637   UNUSED_PARAMETER(NotUsed);
       
 25638   SimulateIOError(return SQLITE_IOERR_DELETE);
       
 25639   unlink(zPath);
       
 25640 #ifndef SQLITE_DISABLE_DIRSYNC
       
 25641   if( dirSync ){
       
 25642     int fd;
       
 25643     rc = openDirectory(zPath, &fd);
       
 25644     if( rc==SQLITE_OK ){
       
 25645 #if OS_VXWORKS
       
 25646       if( fsync(fd)==-1 )
       
 25647 #else
       
 25648       if( fsync(fd) )
       
 25649 #endif
       
 25650       {
       
 25651         rc = SQLITE_IOERR_DIR_FSYNC;
       
 25652       }
       
 25653       if( close(fd)&&!rc ){
       
 25654         rc = SQLITE_IOERR_DIR_CLOSE;
       
 25655       }
       
 25656     }
       
 25657   }
       
 25658 #endif
       
 25659   return rc;
       
 25660 }
       
 25661 
       
 25662 /*
       
 25663 ** Test the existance of or access permissions of file zPath. The
       
 25664 ** test performed depends on the value of flags:
       
 25665 **
       
 25666 **     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
       
 25667 **     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
       
 25668 **     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
       
 25669 **
       
 25670 ** Otherwise return 0.
       
 25671 */
       
 25672 static int unixAccess(
       
 25673   sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
       
 25674   const char *zPath,      /* Path of the file to examine */
       
 25675   int flags,              /* What do we want to learn about the zPath file? */
       
 25676   int *pResOut            /* Write result boolean here */
       
 25677 ){
       
 25678   int amode = 0;
       
 25679   UNUSED_PARAMETER(NotUsed);
       
 25680   SimulateIOError( return SQLITE_IOERR_ACCESS; );
       
 25681   switch( flags ){
       
 25682     case SQLITE_ACCESS_EXISTS:
       
 25683       amode = F_OK;
       
 25684       break;
       
 25685     case SQLITE_ACCESS_READWRITE:
       
 25686       amode = W_OK|R_OK;
       
 25687       break;
       
 25688     case SQLITE_ACCESS_READ:
       
 25689       amode = R_OK;
       
 25690       break;
       
 25691 
       
 25692     default:
       
 25693       assert(!"Invalid flags argument");
       
 25694   }
       
 25695   *pResOut = (access(zPath, amode)==0);
       
 25696   return SQLITE_OK;
       
 25697 }
       
 25698 
       
 25699 
       
 25700 /*
       
 25701 ** Turn a relative pathname into a full pathname. The relative path
       
 25702 ** is stored as a nul-terminated string in the buffer pointed to by
       
 25703 ** zPath. 
       
 25704 **
       
 25705 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes 
       
 25706 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
       
 25707 ** this buffer before returning.
       
 25708 */
       
 25709 static int unixFullPathname(
       
 25710   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
       
 25711   const char *zPath,            /* Possibly relative input path */
       
 25712   int nOut,                     /* Size of output buffer in bytes */
       
 25713   char *zOut                    /* Output buffer */
       
 25714 ){
       
 25715 
       
 25716   /* It's odd to simulate an io-error here, but really this is just
       
 25717   ** using the io-error infrastructure to test that SQLite handles this
       
 25718   ** function failing. This function could fail if, for example, the
       
 25719   ** current working directory has been unlinked.
       
 25720   */
       
 25721   SimulateIOError( return SQLITE_ERROR );
       
 25722 
       
 25723   assert( pVfs->mxPathname==MAX_PATHNAME );
       
 25724   UNUSED_PARAMETER(pVfs);
       
 25725 
       
 25726   zOut[nOut-1] = '\0';
       
 25727   if( zPath[0]=='/' ){
       
 25728     sqlite3_snprintf(nOut, zOut, "%s", zPath);
       
 25729   }else{
       
 25730     int nCwd;
       
 25731     if( getcwd(zOut, nOut-1)==0 ){
       
 25732       return SQLITE_CANTOPEN;
       
 25733     }
       
 25734     nCwd = (int)strlen(zOut);
       
 25735     sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
       
 25736   }
       
 25737   return SQLITE_OK;
       
 25738 }
       
 25739 
       
 25740 
       
 25741 #ifndef SQLITE_OMIT_LOAD_EXTENSION
       
 25742 /*
       
 25743 ** Interfaces for opening a shared library, finding entry points
       
 25744 ** within the shared library, and closing the shared library.
       
 25745 */
       
 25746 #include <dlfcn.h>
       
 25747 static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
       
 25748   UNUSED_PARAMETER(NotUsed);
       
 25749   return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
       
 25750 }
       
 25751 
       
 25752 /*
       
 25753 ** SQLite calls this function immediately after a call to unixDlSym() or
       
 25754 ** unixDlOpen() fails (returns a null pointer). If a more detailed error
       
 25755 ** message is available, it is written to zBufOut. If no error message
       
 25756 ** is available, zBufOut is left unmodified and SQLite uses a default
       
 25757 ** error message.
       
 25758 */
       
 25759 static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
       
 25760   char *zErr;
       
 25761   UNUSED_PARAMETER(NotUsed);
       
 25762   unixEnterMutex();
       
 25763   zErr = dlerror();
       
 25764   if( zErr ){
       
 25765     sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
       
 25766   }
       
 25767   unixLeaveMutex();
       
 25768 }
       
 25769 static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
       
 25770   /* 
       
 25771   ** GCC with -pedantic-errors says that C90 does not allow a void* to be
       
 25772   ** cast into a pointer to a function.  And yet the library dlsym() routine
       
 25773   ** returns a void* which is really a pointer to a function.  So how do we
       
 25774   ** use dlsym() with -pedantic-errors?
       
 25775   **
       
 25776   ** Variable x below is defined to be a pointer to a function taking
       
 25777   ** parameters void* and const char* and returning a pointer to a function.
       
 25778   ** We initialize x by assigning it a pointer to the dlsym() function.
       
 25779   ** (That assignment requires a cast.)  Then we call the function that
       
 25780   ** x points to.  
       
 25781   **
       
 25782   ** This work-around is unlikely to work correctly on any system where
       
 25783   ** you really cannot cast a function pointer into void*.  But then, on the
       
 25784   ** other hand, dlsym() will not work on such a system either, so we have
       
 25785   ** not really lost anything.
       
 25786   */
       
 25787   void (*(*x)(void*,const char*))(void);
       
 25788   UNUSED_PARAMETER(NotUsed);
       
 25789   x = (void(*(*)(void*,const char*))(void))dlsym;
       
 25790   return (*x)(p, zSym);
       
 25791 }
       
 25792 static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
       
 25793   UNUSED_PARAMETER(NotUsed);
       
 25794   dlclose(pHandle);
       
 25795 }
       
 25796 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
       
 25797   #define unixDlOpen  0
       
 25798   #define unixDlError 0
       
 25799   #define unixDlSym   0
       
 25800   #define unixDlClose 0
       
 25801 #endif
       
 25802 
       
 25803 /*
       
 25804 ** Write nBuf bytes of random data to the supplied buffer zBuf.
       
 25805 */
       
 25806 static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
       
 25807   UNUSED_PARAMETER(NotUsed);
       
 25808   assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
       
 25809 
       
 25810   /* We have to initialize zBuf to prevent valgrind from reporting
       
 25811   ** errors.  The reports issued by valgrind are incorrect - we would
       
 25812   ** prefer that the randomness be increased by making use of the
       
 25813   ** uninitialized space in zBuf - but valgrind errors tend to worry
       
 25814   ** some users.  Rather than argue, it seems easier just to initialize
       
 25815   ** the whole array and silence valgrind, even if that means less randomness
       
 25816   ** in the random seed.
       
 25817   **
       
 25818   ** When testing, initializing zBuf[] to zero is all we do.  That means
       
 25819   ** that we always use the same random number sequence.  This makes the
       
 25820   ** tests repeatable.
       
 25821   */
       
 25822   memset(zBuf, 0, nBuf);
       
 25823 #if !defined(SQLITE_TEST)
       
 25824   {
       
 25825     int pid, fd;
       
 25826     fd = open("/dev/urandom", O_RDONLY, 0);
       
 25827     if( fd<0 ){
       
 25828       time_t t;
       
 25829       time(&t);
       
 25830       memcpy(zBuf, &t, sizeof(t));
       
 25831 #ifndef VXWORKS
       
 25832        pid = getpid();
       
 25833 #else
       
 25834       pid = (int)taskIdCurrent();
       
 25835 #endif
       
 25836       memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
       
 25837       assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
       
 25838       nBuf = sizeof(t) + sizeof(pid);
       
 25839     }else{
       
 25840       nBuf = read(fd, zBuf, nBuf);
       
 25841       close(fd);
       
 25842     }
       
 25843   }
       
 25844 #endif
       
 25845   return nBuf;
       
 25846 }
       
 25847 
       
 25848 
       
 25849 /*
       
 25850 ** Sleep for a little while.  Return the amount of time slept.
       
 25851 ** The argument is the number of microseconds we want to sleep.
       
 25852 ** The return value is the number of microseconds of sleep actually
       
 25853 ** requested from the underlying operating system, a number which
       
 25854 ** might be greater than or equal to the argument, but not less
       
 25855 ** than the argument.
       
 25856 */
       
 25857 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
       
 25858 #if OS_VXWORKS
       
 25859   struct timespec sp;
       
 25860 
       
 25861   sp.tv_sec = microseconds / 1000000;
       
 25862   sp.tv_nsec = (microseconds % 1000000) * 1000;
       
 25863   nanosleep(&sp, NULL);
       
 25864   UNUSED_PARAMETER(NotUsed);
       
 25865   return microseconds;
       
 25866 #elif defined(HAVE_USLEEP) && HAVE_USLEEP
       
 25867   usleep(microseconds);
       
 25868   UNUSED_PARAMETER(NotUsed);
       
 25869   return microseconds;
       
 25870 #else
       
 25871   int seconds = (microseconds+999999)/1000000;
       
 25872   sleep(seconds);
       
 25873   UNUSED_PARAMETER(NotUsed);
       
 25874   return seconds*1000000;
       
 25875 #endif
       
 25876 }
       
 25877 
       
 25878 /*
       
 25879 ** The following variable, if set to a non-zero value, is interpreted as
       
 25880 ** the number of seconds since 1970 and is used to set the result of
       
 25881 ** sqlite3OsCurrentTime() during testing.
       
 25882 */
       
 25883 #ifdef SQLITE_TEST
       
 25884 SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
       
 25885 #endif
       
 25886 
       
 25887 /*
       
 25888 ** Find the current time (in Universal Coordinated Time).  Write the
       
 25889 ** current time and date as a Julian Day number into *prNow and
       
 25890 ** return 0.  Return 1 if the time and date cannot be found.
       
 25891 */
       
 25892 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
       
 25893 #if defined(SQLITE_OMIT_FLOATING_POINT)
       
 25894   time_t t;
       
 25895   time(&t);
       
 25896   *prNow = (((sqlite3_int64)t)/8640 + 24405875)/10;
       
 25897 #elif defined(NO_GETTOD)
       
 25898   time_t t;
       
 25899   time(&t);
       
 25900   *prNow = t/86400.0 + 2440587.5;
       
 25901 #elif OS_VXWORKS
       
 25902   struct timespec sNow;
       
 25903   clock_gettime(CLOCK_REALTIME, &sNow);
       
 25904   *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0;
       
 25905 #else
       
 25906   struct timeval sNow;
       
 25907   gettimeofday(&sNow, 0);
       
 25908   *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
       
 25909 #endif
       
 25910 
       
 25911 #ifdef SQLITE_TEST
       
 25912   if( sqlite3_current_time ){
       
 25913     *prNow = sqlite3_current_time/86400.0 + 2440587.5;
       
 25914   }
       
 25915 #endif
       
 25916   UNUSED_PARAMETER(NotUsed);
       
 25917   return 0;
       
 25918 }
       
 25919 
       
 25920 /*
       
 25921 ** We added the xGetLastError() method with the intention of providing
       
 25922 ** better low-level error messages when operating-system problems come up
       
 25923 ** during SQLite operation.  But so far, none of that has been implemented
       
 25924 ** in the core.  So this routine is never called.  For now, it is merely
       
 25925 ** a place-holder.
       
 25926 */
       
 25927 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
       
 25928   UNUSED_PARAMETER(NotUsed);
       
 25929   UNUSED_PARAMETER(NotUsed2);
       
 25930   UNUSED_PARAMETER(NotUsed3);
       
 25931   return 0;
       
 25932 }
       
 25933 
       
 25934 /*
       
 25935 ************************ End of sqlite3_vfs methods ***************************
       
 25936 ******************************************************************************/
       
 25937 
       
 25938 /******************************************************************************
       
 25939 ************************** Begin Proxy Locking ********************************
       
 25940 **
       
 25941 ** Proxy locking is a "uber-locking-method" in this sense:  It uses the
       
 25942 ** other locking methods on secondary lock files.  Proxy locking is a
       
 25943 ** meta-layer over top of the primitive locking implemented above.  For
       
 25944 ** this reason, the division that implements of proxy locking is deferred
       
 25945 ** until late in the file (here) after all of the other I/O methods have
       
 25946 ** been defined - so that the primitive locking methods are available
       
 25947 ** as services to help with the implementation of proxy locking.
       
 25948 **
       
 25949 ****
       
 25950 **
       
 25951 ** The default locking schemes in SQLite use byte-range locks on the
       
 25952 ** database file to coordinate safe, concurrent access by multiple readers
       
 25953 ** and writers [http://sqlite.org/lockingv3.html].  The five file locking
       
 25954 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
       
 25955 ** as POSIX read & write locks over fixed set of locations (via fsctl),
       
 25956 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
       
 25957 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
       
 25958 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
       
 25959 ** address in the shared range is taken for a SHARED lock, the entire
       
 25960 ** shared range is taken for an EXCLUSIVE lock):
       
 25961 **
       
 25962 **      PENDING_BYTE        0x40000000		   	
       
 25963 **      RESERVED_BYTE       0x40000001
       
 25964 **      SHARED_RANGE        0x40000002 -> 0x40000200
       
 25965 **
       
 25966 ** This works well on the local file system, but shows a nearly 100x
       
 25967 ** slowdown in read performance on AFP because the AFP client disables
       
 25968 ** the read cache when byte-range locks are present.  Enabling the read
       
 25969 ** cache exposes a cache coherency problem that is present on all OS X
       
 25970 ** supported network file systems.  NFS and AFP both observe the
       
 25971 ** close-to-open semantics for ensuring cache coherency
       
 25972 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
       
 25973 ** address the requirements for concurrent database access by multiple
       
 25974 ** readers and writers
       
 25975 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
       
 25976 **
       
 25977 ** To address the performance and cache coherency issues, proxy file locking
       
 25978 ** changes the way database access is controlled by limiting access to a
       
 25979 ** single host at a time and moving file locks off of the database file
       
 25980 ** and onto a proxy file on the local file system.  
       
 25981 **
       
 25982 **
       
 25983 ** Using proxy locks
       
 25984 ** -----------------
       
 25985 **
       
 25986 ** C APIs
       
 25987 **
       
 25988 **  sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
       
 25989 **                       <proxy_path> | ":auto:");
       
 25990 **  sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
       
 25991 **
       
 25992 **
       
 25993 ** SQL pragmas
       
 25994 **
       
 25995 **  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
       
 25996 **  PRAGMA [database.]lock_proxy_file
       
 25997 **
       
 25998 ** Specifying ":auto:" means that if there is a conch file with a matching
       
 25999 ** host ID in it, the proxy path in the conch file will be used, otherwise
       
 26000 ** a proxy path based on the user's temp dir
       
 26001 ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
       
 26002 ** actual proxy file name is generated from the name and path of the
       
 26003 ** database file.  For example:
       
 26004 **
       
 26005 **       For database path "/Users/me/foo.db" 
       
 26006 **       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
       
 26007 **
       
 26008 ** Once a lock proxy is configured for a database connection, it can not
       
 26009 ** be removed, however it may be switched to a different proxy path via
       
 26010 ** the above APIs (assuming the conch file is not being held by another
       
 26011 ** connection or process). 
       
 26012 **
       
 26013 **
       
 26014 ** How proxy locking works
       
 26015 ** -----------------------
       
 26016 **
       
 26017 ** Proxy file locking relies primarily on two new supporting files: 
       
 26018 **
       
 26019 **   *  conch file to limit access to the database file to a single host
       
 26020 **      at a time
       
 26021 **
       
 26022 **   *  proxy file to act as a proxy for the advisory locks normally
       
 26023 **      taken on the database
       
 26024 **
       
 26025 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
       
 26026 ** by taking an sqlite-style shared lock on the conch file, reading the
       
 26027 ** contents and comparing the host's unique host ID (see below) and lock
       
 26028 ** proxy path against the values stored in the conch.  The conch file is
       
 26029 ** stored in the same directory as the database file and the file name
       
 26030 ** is patterned after the database file name as ".<databasename>-conch".
       
 26031 ** If the conch file does not exist, or it's contents do not match the
       
 26032 ** host ID and/or proxy path, then the lock is escalated to an exclusive
       
 26033 ** lock and the conch file contents is updated with the host ID and proxy
       
 26034 ** path and the lock is downgraded to a shared lock again.  If the conch
       
 26035 ** is held by another process (with a shared lock), the exclusive lock
       
 26036 ** will fail and SQLITE_BUSY is returned.
       
 26037 **
       
 26038 ** The proxy file - a single-byte file used for all advisory file locks
       
 26039 ** normally taken on the database file.   This allows for safe sharing
       
 26040 ** of the database file for multiple readers and writers on the same
       
 26041 ** host (the conch ensures that they all use the same local lock file).
       
 26042 **
       
 26043 ** There is a third file - the host ID file - used as a persistent record
       
 26044 ** of a unique identifier for the host, a 128-byte unique host id file
       
 26045 ** in the path defined by the HOSTIDPATH macro (default value is
       
 26046 ** /Library/Caches/.com.apple.sqliteConchHostId).
       
 26047 **
       
 26048 ** Requesting the lock proxy does not immediately take the conch, it is
       
 26049 ** only taken when the first request to lock database file is made.  
       
 26050 ** This matches the semantics of the traditional locking behavior, where
       
 26051 ** opening a connection to a database file does not take a lock on it.
       
 26052 ** The shared lock and an open file descriptor are maintained until 
       
 26053 ** the connection to the database is closed. 
       
 26054 **
       
 26055 ** The proxy file and the lock file are never deleted so they only need
       
 26056 ** to be created the first time they are used.
       
 26057 **
       
 26058 ** Configuration options
       
 26059 ** ---------------------
       
 26060 **
       
 26061 **  SQLITE_PREFER_PROXY_LOCKING
       
 26062 **
       
 26063 **       Database files accessed on non-local file systems are
       
 26064 **       automatically configured for proxy locking, lock files are
       
 26065 **       named automatically using the same logic as
       
 26066 **       PRAGMA lock_proxy_file=":auto:"
       
 26067 **    
       
 26068 **  SQLITE_PROXY_DEBUG
       
 26069 **
       
 26070 **       Enables the logging of error messages during host id file
       
 26071 **       retrieval and creation
       
 26072 **
       
 26073 **  HOSTIDPATH
       
 26074 **
       
 26075 **       Overrides the default host ID file path location
       
 26076 **
       
 26077 **  LOCKPROXYDIR
       
 26078 **
       
 26079 **       Overrides the default directory used for lock proxy files that
       
 26080 **       are named automatically via the ":auto:" setting
       
 26081 **
       
 26082 **  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
       
 26083 **
       
 26084 **       Permissions to use when creating a directory for storing the
       
 26085 **       lock proxy files, only used when LOCKPROXYDIR is not set.
       
 26086 **    
       
 26087 **    
       
 26088 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
       
 26089 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
       
 26090 ** force proxy locking to be used for every database file opened, and 0
       
 26091 ** will force automatic proxy locking to be disabled for all database
       
 26092 ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
       
 26093 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
       
 26094 */
       
 26095 
       
 26096 /*
       
 26097 ** Proxy locking is only available on MacOSX 
       
 26098 */
       
 26099 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
       
 26100 
       
 26101 #ifdef SQLITE_TEST
       
 26102 /* simulate multiple hosts by creating unique hostid file paths */
       
 26103 SQLITE_API int sqlite3_hostid_num = 0;
       
 26104 #endif
       
 26105 
       
 26106 /*
       
 26107 ** The proxyLockingContext has the path and file structures for the remote 
       
 26108 ** and local proxy files in it
       
 26109 */
       
 26110 typedef struct proxyLockingContext proxyLockingContext;
       
 26111 struct proxyLockingContext {
       
 26112   unixFile *conchFile;         /* Open conch file */
       
 26113   char *conchFilePath;         /* Name of the conch file */
       
 26114   unixFile *lockProxy;         /* Open proxy lock file */
       
 26115   char *lockProxyPath;         /* Name of the proxy lock file */
       
 26116   char *dbPath;                /* Name of the open file */
       
 26117   int conchHeld;               /* True if the conch is currently held */
       
 26118   void *oldLockingContext;     /* Original lockingcontext to restore on close */
       
 26119   sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
       
 26120 };
       
 26121 
       
 26122 /* HOSTIDLEN and CONCHLEN both include space for the string 
       
 26123 ** terminating nul 
       
 26124 */
       
 26125 #define HOSTIDLEN         128
       
 26126 #define CONCHLEN          (MAXPATHLEN+HOSTIDLEN+1)
       
 26127 #ifndef HOSTIDPATH
       
 26128 # define HOSTIDPATH       "/Library/Caches/.com.apple.sqliteConchHostId"
       
 26129 #endif
       
 26130 
       
 26131 /* basically a copy of unixRandomness with different
       
 26132 ** test behavior built in */
       
 26133 static int proxyGenerateHostID(char *pHostID){
       
 26134   int pid, fd, len;
       
 26135   unsigned char *key = (unsigned char *)pHostID;
       
 26136   
       
 26137   memset(key, 0, HOSTIDLEN);
       
 26138   len = 0;
       
 26139   fd = open("/dev/urandom", O_RDONLY);
       
 26140   if( fd>=0 ){
       
 26141     len = read(fd, key, HOSTIDLEN);
       
 26142     close(fd); /* silently leak the fd if it fails */
       
 26143   }
       
 26144   if( len < HOSTIDLEN ){
       
 26145     time_t t;
       
 26146     time(&t);
       
 26147     memcpy(key, &t, sizeof(t));
       
 26148     pid = getpid();
       
 26149     memcpy(&key[sizeof(t)], &pid, sizeof(pid));
       
 26150   }
       
 26151   
       
 26152 #ifdef MAKE_PRETTY_HOSTID
       
 26153   {
       
 26154     int i;
       
 26155     /* filter the bytes into printable ascii characters and NUL terminate */
       
 26156     key[(HOSTIDLEN-1)] = 0x00;
       
 26157     for( i=0; i<(HOSTIDLEN-1); i++ ){
       
 26158       unsigned char pa = key[i]&0x7F;
       
 26159       if( pa<0x20 ){
       
 26160         key[i] = (key[i]&0x80 == 0x80) ? pa+0x40 : pa+0x20;
       
 26161       }else if( pa==0x7F ){
       
 26162         key[i] = (key[i]&0x80 == 0x80) ? pa=0x20 : pa+0x7E;
       
 26163       }
       
 26164     }
       
 26165   }
       
 26166 #endif
       
 26167   return SQLITE_OK;
       
 26168 }
       
 26169 
       
 26170 /* writes the host id path to path, path should be an pre-allocated buffer
       
 26171 ** with enough space for a path 
       
 26172 */
       
 26173 static void proxyGetHostIDPath(char *path, size_t len){
       
 26174   strlcpy(path, HOSTIDPATH, len);
       
 26175 #ifdef SQLITE_TEST
       
 26176   if( sqlite3_hostid_num>0 ){
       
 26177     char suffix[2] = "1";
       
 26178     suffix[0] = suffix[0] + sqlite3_hostid_num;
       
 26179     strlcat(path, suffix, len);
       
 26180   }
       
 26181 #endif
       
 26182   OSTRACE3("GETHOSTIDPATH  %s pid=%d\n", path, getpid());
       
 26183 }
       
 26184 
       
 26185 /* get the host ID from a sqlite hostid file stored in the 
       
 26186 ** user-specific tmp directory, create the ID if it's not there already 
       
 26187 */
       
 26188 static int proxyGetHostID(char *pHostID, int *pError){
       
 26189   int fd;
       
 26190   char path[MAXPATHLEN]; 
       
 26191   size_t len;
       
 26192   int rc=SQLITE_OK;
       
 26193 
       
 26194   proxyGetHostIDPath(path, MAXPATHLEN);
       
 26195   /* try to create the host ID file, if it already exists read the contents */
       
 26196   fd = open(path, O_CREAT|O_WRONLY|O_EXCL, 0644);
       
 26197   if( fd<0 ){
       
 26198     int err=errno;
       
 26199 		
       
 26200     if( err!=EEXIST ){
       
 26201 #ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
       
 26202       fprintf(stderr, "sqlite error creating host ID file %s: %s\n",
       
 26203               path, strerror(err));
       
 26204 #endif
       
 26205       return SQLITE_PERM;
       
 26206     }
       
 26207     /* couldn't create the file, read it instead */
       
 26208     fd = open(path, O_RDONLY|O_EXCL);
       
 26209     if( fd<0 ){
       
 26210 #ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
       
 26211       int err = errno;
       
 26212       fprintf(stderr, "sqlite error opening host ID file %s: %s\n",
       
 26213               path, strerror(err));
       
 26214 #endif
       
 26215       return SQLITE_PERM;
       
 26216     }
       
 26217     len = pread(fd, pHostID, HOSTIDLEN, 0);
       
 26218     if( len<0 ){
       
 26219       *pError = errno;
       
 26220       rc = SQLITE_IOERR_READ;
       
 26221     }else if( len<HOSTIDLEN ){
       
 26222       *pError = 0;
       
 26223       rc = SQLITE_IOERR_SHORT_READ;
       
 26224     }
       
 26225     close(fd); /* silently leak the fd if it fails */
       
 26226     OSTRACE3("GETHOSTID  read %s pid=%d\n", pHostID, getpid());
       
 26227     return rc;
       
 26228   }else{
       
 26229     /* we're creating the host ID file (use a random string of bytes) */
       
 26230     proxyGenerateHostID(pHostID);
       
 26231     len = pwrite(fd, pHostID, HOSTIDLEN, 0);
       
 26232     if( len<0 ){
       
 26233       *pError = errno;
       
 26234       rc = SQLITE_IOERR_WRITE;
       
 26235     }else if( len<HOSTIDLEN ){
       
 26236       *pError = 0;
       
 26237       rc = SQLITE_IOERR_WRITE;
       
 26238     }
       
 26239     close(fd); /* silently leak the fd if it fails */
       
 26240     OSTRACE3("GETHOSTID  wrote %s pid=%d\n", pHostID, getpid());
       
 26241     return rc;
       
 26242   }
       
 26243 }
       
 26244 
       
 26245 static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
       
 26246   int len;
       
 26247   int dbLen;
       
 26248   int i;
       
 26249 
       
 26250 #ifdef LOCKPROXYDIR
       
 26251   len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
       
 26252 #else
       
 26253 # ifdef _CS_DARWIN_USER_TEMP_DIR
       
 26254   {
       
 26255     confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen);
       
 26256     len = strlcat(lPath, "sqliteplocks", maxLen);
       
 26257     if( mkdir(lPath, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
       
 26258       /* if mkdir fails, handle as lock file creation failure */
       
 26259 #  ifdef SQLITE_DEBUG
       
 26260       int err = errno;
       
 26261       if( err!=EEXIST ){
       
 26262         fprintf(stderr, "proxyGetLockPath: mkdir(%s,0%o) error %d %s\n", lPath,
       
 26263                 SQLITE_DEFAULT_PROXYDIR_PERMISSIONS, err, strerror(err));
       
 26264       }
       
 26265 #  endif
       
 26266     }else{
       
 26267       OSTRACE3("GETLOCKPATH  mkdir %s pid=%d\n", lPath, getpid());
       
 26268     }
       
 26269     
       
 26270   }
       
 26271 # else
       
 26272   len = strlcpy(lPath, "/tmp/", maxLen);
       
 26273 # endif
       
 26274 #endif
       
 26275 
       
 26276   if( lPath[len-1]!='/' ){
       
 26277     len = strlcat(lPath, "/", maxLen);
       
 26278   }
       
 26279   
       
 26280   /* transform the db path to a unique cache name */
       
 26281   dbLen = (int)strlen(dbPath);
       
 26282   for( i=0; i<dbLen && (i+len+7)<maxLen; i++){
       
 26283     char c = dbPath[i];
       
 26284     lPath[i+len] = (c=='/')?'_':c;
       
 26285   }
       
 26286   lPath[i+len]='\0';
       
 26287   strlcat(lPath, ":auto:", maxLen);
       
 26288   return SQLITE_OK;
       
 26289 }
       
 26290 
       
 26291 /*
       
 26292 ** Create a new VFS file descriptor (stored in memory obtained from
       
 26293 ** sqlite3_malloc) and open the file named "path" in the file descriptor.
       
 26294 **
       
 26295 ** The caller is responsible not only for closing the file descriptor
       
 26296 ** but also for freeing the memory associated with the file descriptor.
       
 26297 */
       
 26298 static int proxyCreateUnixFile(const char *path, unixFile **ppFile) {
       
 26299   unixFile *pNew;
       
 26300   int flags = SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
       
 26301   int rc = SQLITE_OK;
       
 26302   sqlite3_vfs dummyVfs;
       
 26303 
       
 26304   pNew = (unixFile *)sqlite3_malloc(sizeof(unixFile));
       
 26305   if( !pNew ){
       
 26306     return SQLITE_NOMEM;
       
 26307   }
       
 26308   memset(pNew, 0, sizeof(unixFile));
       
 26309 
       
 26310   /* Call unixOpen() to open the proxy file. The flags passed to unixOpen()
       
 26311   ** suggest that the file being opened is a "main database". This is
       
 26312   ** necessary as other file types do not necessarily support locking. It
       
 26313   ** is better to use unixOpen() instead of opening the file directly with
       
 26314   ** open(), as unixOpen() sets up the various mechanisms required to
       
 26315   ** make sure a call to close() does not cause the system to discard
       
 26316   ** POSIX locks prematurely.
       
 26317   **
       
 26318   ** It is important that the xOpen member of the VFS object passed to 
       
 26319   ** unixOpen() is NULL. This tells unixOpen() may try to open a proxy-file 
       
 26320   ** for the proxy-file (creating a potential infinite loop).
       
 26321   */
       
 26322   dummyVfs.pAppData = (void*)&autolockIoFinder;
       
 26323   dummyVfs.xOpen = 0;
       
 26324   rc = unixOpen(&dummyVfs, path, (sqlite3_file *)pNew, flags, &flags);
       
 26325   if( rc==SQLITE_OK && (flags&SQLITE_OPEN_READONLY) ){
       
 26326     pNew->pMethod->xClose((sqlite3_file *)pNew);
       
 26327     rc = SQLITE_CANTOPEN;
       
 26328   }
       
 26329 
       
 26330   if( rc!=SQLITE_OK ){
       
 26331     sqlite3_free(pNew);
       
 26332     pNew = 0;
       
 26333   }
       
 26334 
       
 26335   *ppFile = pNew;
       
 26336   return rc;
       
 26337 }
       
 26338 
       
 26339 /* takes the conch by taking a shared lock and read the contents conch, if 
       
 26340 ** lockPath is non-NULL, the host ID and lock file path must match.  A NULL 
       
 26341 ** lockPath means that the lockPath in the conch file will be used if the 
       
 26342 ** host IDs match, or a new lock path will be generated automatically 
       
 26343 ** and written to the conch file.
       
 26344 */
       
 26345 static int proxyTakeConch(unixFile *pFile){
       
 26346   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
       
 26347   
       
 26348   if( pCtx->conchHeld>0 ){
       
 26349     return SQLITE_OK;
       
 26350   }else{
       
 26351     unixFile *conchFile = pCtx->conchFile;
       
 26352     char testValue[CONCHLEN];
       
 26353     char conchValue[CONCHLEN];
       
 26354     char lockPath[MAXPATHLEN];
       
 26355     char *tLockPath = NULL;
       
 26356     int rc = SQLITE_OK;
       
 26357     int readRc = SQLITE_OK;
       
 26358     int syncPerms = 0;
       
 26359 
       
 26360     OSTRACE4("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
       
 26361              (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid());
       
 26362 
       
 26363     rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
       
 26364     if( rc==SQLITE_OK ){
       
 26365       int pError = 0;
       
 26366       memset(testValue, 0, CONCHLEN); /* conch is fixed size */
       
 26367       rc = proxyGetHostID(testValue, &pError);
       
 26368       if( (rc&0xff)==SQLITE_IOERR ){
       
 26369         pFile->lastErrno = pError;
       
 26370       }
       
 26371       if( pCtx->lockProxyPath ){
       
 26372         strlcpy(&testValue[HOSTIDLEN], pCtx->lockProxyPath, MAXPATHLEN);
       
 26373       }
       
 26374     }
       
 26375     if( rc!=SQLITE_OK ){
       
 26376       goto end_takeconch;
       
 26377     }
       
 26378     
       
 26379     readRc = unixRead((sqlite3_file *)conchFile, conchValue, CONCHLEN, 0);
       
 26380     if( readRc!=SQLITE_IOERR_SHORT_READ ){
       
 26381       if( readRc!=SQLITE_OK ){
       
 26382         if( (rc&0xff)==SQLITE_IOERR ){
       
 26383           pFile->lastErrno = conchFile->lastErrno;
       
 26384         }
       
 26385         rc = readRc;
       
 26386         goto end_takeconch;
       
 26387       }
       
 26388       /* if the conch has data compare the contents */
       
 26389       if( !pCtx->lockProxyPath ){
       
 26390         /* for auto-named local lock file, just check the host ID and we'll
       
 26391          ** use the local lock file path that's already in there */
       
 26392         if( !memcmp(testValue, conchValue, HOSTIDLEN) ){
       
 26393           tLockPath = (char *)&conchValue[HOSTIDLEN];
       
 26394           goto end_takeconch;
       
 26395         }
       
 26396       }else{
       
 26397         /* we've got the conch if conchValue matches our path and host ID */
       
 26398         if( !memcmp(testValue, conchValue, CONCHLEN) ){
       
 26399           goto end_takeconch;
       
 26400         }
       
 26401       }
       
 26402     }else{
       
 26403       /* a short read means we're "creating" the conch (even though it could 
       
 26404       ** have been user-intervention), if we acquire the exclusive lock,
       
 26405       ** we'll try to match the current on-disk permissions of the database
       
 26406       */
       
 26407       syncPerms = 1;
       
 26408     }
       
 26409     
       
 26410     /* either conch was emtpy or didn't match */
       
 26411     if( !pCtx->lockProxyPath ){
       
 26412       proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
       
 26413       tLockPath = lockPath;
       
 26414       strlcpy(&testValue[HOSTIDLEN], lockPath, MAXPATHLEN);
       
 26415     }
       
 26416     
       
 26417     /* update conch with host and path (this will fail if other process
       
 26418      ** has a shared lock already) */
       
 26419     rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
       
 26420     if( rc==SQLITE_OK ){
       
 26421       rc = unixWrite((sqlite3_file *)conchFile, testValue, CONCHLEN, 0);
       
 26422       if( rc==SQLITE_OK && syncPerms ){
       
 26423         struct stat buf;
       
 26424         int err = fstat(pFile->h, &buf);
       
 26425         if( err==0 ){
       
 26426           /* try to match the database file permissions, ignore failure */
       
 26427 #ifndef SQLITE_PROXY_DEBUG
       
 26428           fchmod(conchFile->h, buf.st_mode);
       
 26429 #else
       
 26430           if( fchmod(conchFile->h, buf.st_mode)!=0 ){
       
 26431             int code = errno;
       
 26432             fprintf(stderr, "fchmod %o FAILED with %d %s\n",
       
 26433                              buf.st_mode, code, strerror(code));
       
 26434           } else {
       
 26435             fprintf(stderr, "fchmod %o SUCCEDED\n",buf.st_mode);
       
 26436           }
       
 26437         }else{
       
 26438           int code = errno;
       
 26439           fprintf(stderr, "STAT FAILED[%d] with %d %s\n", 
       
 26440                           err, code, strerror(code));
       
 26441 #endif
       
 26442         }
       
 26443       }
       
 26444     }
       
 26445     conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
       
 26446   
       
 26447 end_takeconch:
       
 26448     OSTRACE2("TRANSPROXY: CLOSE  %d\n", pFile->h);
       
 26449     if( rc==SQLITE_OK && pFile->openFlags ){
       
 26450       if( pFile->h>=0 ){
       
 26451 #ifdef STRICT_CLOSE_ERROR
       
 26452         if( close(pFile->h) ){
       
 26453           pFile->lastErrno = errno;
       
 26454           return SQLITE_IOERR_CLOSE;
       
 26455         }
       
 26456 #else
       
 26457         close(pFile->h); /* silently leak fd if fail */
       
 26458 #endif
       
 26459       }
       
 26460       pFile->h = -1;
       
 26461       int fd = open(pCtx->dbPath, pFile->openFlags,
       
 26462                     SQLITE_DEFAULT_FILE_PERMISSIONS);
       
 26463       OSTRACE2("TRANSPROXY: OPEN  %d\n", fd);
       
 26464       if( fd>=0 ){
       
 26465         pFile->h = fd;
       
 26466       }else{
       
 26467         rc=SQLITE_CANTOPEN; /* SQLITE_BUSY? proxyTakeConch called
       
 26468                                during locking */
       
 26469       }
       
 26470     }
       
 26471     if( rc==SQLITE_OK && !pCtx->lockProxy ){
       
 26472       char *path = tLockPath ? tLockPath : pCtx->lockProxyPath;
       
 26473       /* ACS: Need to make a copy of path sometimes */
       
 26474       rc = proxyCreateUnixFile(path, &pCtx->lockProxy);
       
 26475     }
       
 26476     if( rc==SQLITE_OK ){
       
 26477       pCtx->conchHeld = 1;
       
 26478 
       
 26479       if( tLockPath ){
       
 26480         pCtx->lockProxyPath = sqlite3DbStrDup(0, tLockPath);
       
 26481         if( pCtx->lockProxy->pMethod == &afpIoMethods ){
       
 26482           ((afpLockingContext *)pCtx->lockProxy->lockingContext)->dbPath =
       
 26483                      pCtx->lockProxyPath;
       
 26484         }
       
 26485       }
       
 26486     } else {
       
 26487       conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
       
 26488     }
       
 26489     OSTRACE3("TAKECONCH  %d %s\n", conchFile->h, rc==SQLITE_OK?"ok":"failed");
       
 26490     return rc;
       
 26491   }
       
 26492 }
       
 26493 
       
 26494 /*
       
 26495 ** If pFile holds a lock on a conch file, then release that lock.
       
 26496 */
       
 26497 static int proxyReleaseConch(unixFile *pFile){
       
 26498   int rc;                     /* Subroutine return code */
       
 26499   proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
       
 26500   unixFile *conchFile;        /* Name of the conch file */
       
 26501 
       
 26502   pCtx = (proxyLockingContext *)pFile->lockingContext;
       
 26503   conchFile = pCtx->conchFile;
       
 26504   OSTRACE4("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
       
 26505            (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), 
       
 26506            getpid());
       
 26507   pCtx->conchHeld = 0;
       
 26508   rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
       
 26509   OSTRACE3("RELEASECONCH  %d %s\n", conchFile->h,
       
 26510            (rc==SQLITE_OK ? "ok" : "failed"));
       
 26511   return rc;
       
 26512 }
       
 26513 
       
 26514 /*
       
 26515 ** Given the name of a database file, compute the name of its conch file.
       
 26516 ** Store the conch filename in memory obtained from sqlite3_malloc().
       
 26517 ** Make *pConchPath point to the new name.  Return SQLITE_OK on success
       
 26518 ** or SQLITE_NOMEM if unable to obtain memory.
       
 26519 **
       
 26520 ** The caller is responsible for ensuring that the allocated memory
       
 26521 ** space is eventually freed.
       
 26522 **
       
 26523 ** *pConchPath is set to NULL if a memory allocation error occurs.
       
 26524 */
       
 26525 static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
       
 26526   int i;                        /* Loop counter */
       
 26527   int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
       
 26528   char *conchPath;              /* buffer in which to construct conch name */
       
 26529 
       
 26530   /* Allocate space for the conch filename and initialize the name to
       
 26531   ** the name of the original database file. */  
       
 26532   *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
       
 26533   if( conchPath==0 ){
       
 26534     return SQLITE_NOMEM;
       
 26535   }
       
 26536   memcpy(conchPath, dbPath, len+1);
       
 26537   
       
 26538   /* now insert a "." before the last / character */
       
 26539   for( i=(len-1); i>=0; i-- ){
       
 26540     if( conchPath[i]=='/' ){
       
 26541       i++;
       
 26542       break;
       
 26543     }
       
 26544   }
       
 26545   conchPath[i]='.';
       
 26546   while ( i<len ){
       
 26547     conchPath[i+1]=dbPath[i];
       
 26548     i++;
       
 26549   }
       
 26550 
       
 26551   /* append the "-conch" suffix to the file */
       
 26552   memcpy(&conchPath[i+1], "-conch", 7);
       
 26553   assert( (int)strlen(conchPath) == len+7 );
       
 26554 
       
 26555   return SQLITE_OK;
       
 26556 }
       
 26557 
       
 26558 
       
 26559 /* Takes a fully configured proxy locking-style unix file and switches
       
 26560 ** the local lock file path 
       
 26561 */
       
 26562 static int switchLockProxyPath(unixFile *pFile, const char *path) {
       
 26563   proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
       
 26564   char *oldPath = pCtx->lockProxyPath;
       
 26565   int rc = SQLITE_OK;
       
 26566 
       
 26567   if( pFile->locktype!=NO_LOCK ){
       
 26568     return SQLITE_BUSY;
       
 26569   }  
       
 26570 
       
 26571   /* nothing to do if the path is NULL, :auto: or matches the existing path */
       
 26572   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
       
 26573     (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
       
 26574     return SQLITE_OK;
       
 26575   }else{
       
 26576     unixFile *lockProxy = pCtx->lockProxy;
       
 26577     pCtx->lockProxy=NULL;
       
 26578     pCtx->conchHeld = 0;
       
 26579     if( lockProxy!=NULL ){
       
 26580       rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
       
 26581       if( rc ) return rc;
       
 26582       sqlite3_free(lockProxy);
       
 26583     }
       
 26584     sqlite3_free(oldPath);
       
 26585     pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
       
 26586   }
       
 26587   
       
 26588   return rc;
       
 26589 }
       
 26590 
       
 26591 /*
       
 26592 ** pFile is a file that has been opened by a prior xOpen call.  dbPath
       
 26593 ** is a string buffer at least MAXPATHLEN+1 characters in size.
       
 26594 **
       
 26595 ** This routine find the filename associated with pFile and writes it
       
 26596 ** int dbPath.
       
 26597 */
       
 26598 static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
       
 26599 #if defined(__APPLE__)
       
 26600   if( pFile->pMethod == &afpIoMethods ){
       
 26601     /* afp style keeps a reference to the db path in the filePath field 
       
 26602     ** of the struct */
       
 26603     assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
       
 26604     strcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath);
       
 26605   }else
       
 26606 #endif
       
 26607   if( pFile->pMethod == &dotlockIoMethods ){
       
 26608     /* dot lock style uses the locking context to store the dot lock
       
 26609     ** file path */
       
 26610     int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
       
 26611     memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
       
 26612   }else{
       
 26613     /* all other styles use the locking context to store the db file path */
       
 26614     assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
       
 26615     strcpy(dbPath, (char *)pFile->lockingContext);
       
 26616   }
       
 26617   return SQLITE_OK;
       
 26618 }
       
 26619 
       
 26620 /*
       
 26621 ** Takes an already filled in unix file and alters it so all file locking 
       
 26622 ** will be performed on the local proxy lock file.  The following fields
       
 26623 ** are preserved in the locking context so that they can be restored and 
       
 26624 ** the unix structure properly cleaned up at close time:
       
 26625 **  ->lockingContext
       
 26626 **  ->pMethod
       
 26627 */
       
 26628 static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
       
 26629   proxyLockingContext *pCtx;
       
 26630   char dbPath[MAXPATHLEN+1];       /* Name of the database file */
       
 26631   char *lockPath=NULL;
       
 26632   int rc = SQLITE_OK;
       
 26633   
       
 26634   if( pFile->locktype!=NO_LOCK ){
       
 26635     return SQLITE_BUSY;
       
 26636   }
       
 26637   proxyGetDbPathForUnixFile(pFile, dbPath);
       
 26638   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
       
 26639     lockPath=NULL;
       
 26640   }else{
       
 26641     lockPath=(char *)path;
       
 26642   }
       
 26643   
       
 26644   OSTRACE4("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
       
 26645            (lockPath ? lockPath : ":auto:"), getpid());
       
 26646 
       
 26647   pCtx = sqlite3_malloc( sizeof(*pCtx) );
       
 26648   if( pCtx==0 ){
       
 26649     return SQLITE_NOMEM;
       
 26650   }
       
 26651   memset(pCtx, 0, sizeof(*pCtx));
       
 26652 
       
 26653   rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
       
 26654   if( rc==SQLITE_OK ){
       
 26655     rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile);
       
 26656   }  
       
 26657   if( rc==SQLITE_OK && lockPath ){
       
 26658     pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
       
 26659   }
       
 26660 
       
 26661   if( rc==SQLITE_OK ){
       
 26662     /* all memory is allocated, proxys are created and assigned, 
       
 26663     ** switch the locking context and pMethod then return.
       
 26664     */
       
 26665     pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
       
 26666     pCtx->oldLockingContext = pFile->lockingContext;
       
 26667     pFile->lockingContext = pCtx;
       
 26668     pCtx->pOldMethod = pFile->pMethod;
       
 26669     pFile->pMethod = &proxyIoMethods;
       
 26670   }else{
       
 26671     if( pCtx->conchFile ){ 
       
 26672       rc = pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
       
 26673       if( rc ) return rc;
       
 26674       sqlite3_free(pCtx->conchFile);
       
 26675     }
       
 26676     sqlite3_free(pCtx->conchFilePath); 
       
 26677     sqlite3_free(pCtx);
       
 26678   }
       
 26679   OSTRACE3("TRANSPROXY  %d %s\n", pFile->h,
       
 26680            (rc==SQLITE_OK ? "ok" : "failed"));
       
 26681   return rc;
       
 26682 }
       
 26683 
       
 26684 
       
 26685 /*
       
 26686 ** This routine handles sqlite3_file_control() calls that are specific
       
 26687 ** to proxy locking.
       
 26688 */
       
 26689 static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
       
 26690   switch( op ){
       
 26691     case SQLITE_GET_LOCKPROXYFILE: {
       
 26692       unixFile *pFile = (unixFile*)id;
       
 26693       if( pFile->pMethod == &proxyIoMethods ){
       
 26694         proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
       
 26695         proxyTakeConch(pFile);
       
 26696         if( pCtx->lockProxyPath ){
       
 26697           *(const char **)pArg = pCtx->lockProxyPath;
       
 26698         }else{
       
 26699           *(const char **)pArg = ":auto: (not held)";
       
 26700         }
       
 26701       } else {
       
 26702         *(const char **)pArg = NULL;
       
 26703       }
       
 26704       return SQLITE_OK;
       
 26705     }
       
 26706     case SQLITE_SET_LOCKPROXYFILE: {
       
 26707       unixFile *pFile = (unixFile*)id;
       
 26708       int rc = SQLITE_OK;
       
 26709       int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
       
 26710       if( pArg==NULL || (const char *)pArg==0 ){
       
 26711         if( isProxyStyle ){
       
 26712           /* turn off proxy locking - not supported */
       
 26713           rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
       
 26714         }else{
       
 26715           /* turn off proxy locking - already off - NOOP */
       
 26716           rc = SQLITE_OK;
       
 26717         }
       
 26718       }else{
       
 26719         const char *proxyPath = (const char *)pArg;
       
 26720         if( isProxyStyle ){
       
 26721           proxyLockingContext *pCtx = 
       
 26722             (proxyLockingContext*)pFile->lockingContext;
       
 26723           if( !strcmp(pArg, ":auto:") 
       
 26724            || (pCtx->lockProxyPath &&
       
 26725                !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
       
 26726           ){
       
 26727             rc = SQLITE_OK;
       
 26728           }else{
       
 26729             rc = switchLockProxyPath(pFile, proxyPath);
       
 26730           }
       
 26731         }else{
       
 26732           /* turn on proxy file locking */
       
 26733           rc = proxyTransformUnixFile(pFile, proxyPath);
       
 26734         }
       
 26735       }
       
 26736       return rc;
       
 26737     }
       
 26738     default: {
       
 26739       assert( 0 );  /* The call assures that only valid opcodes are sent */
       
 26740     }
       
 26741   }
       
 26742   /*NOTREACHED*/
       
 26743   return SQLITE_ERROR;
       
 26744 }
       
 26745 
       
 26746 /*
       
 26747 ** Within this division (the proxying locking implementation) the procedures
       
 26748 ** above this point are all utilities.  The lock-related methods of the
       
 26749 ** proxy-locking sqlite3_io_method object follow.
       
 26750 */
       
 26751 
       
 26752 
       
 26753 /*
       
 26754 ** This routine checks if there is a RESERVED lock held on the specified
       
 26755 ** file by this or any other process. If such a lock is held, set *pResOut
       
 26756 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
       
 26757 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
       
 26758 */
       
 26759 static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
       
 26760   unixFile *pFile = (unixFile*)id;
       
 26761   int rc = proxyTakeConch(pFile);
       
 26762   if( rc==SQLITE_OK ){
       
 26763     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
       
 26764     unixFile *proxy = pCtx->lockProxy;
       
 26765     return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
       
 26766   }
       
 26767   return rc;
       
 26768 }
       
 26769 
       
 26770 /*
       
 26771 ** Lock the file with the lock specified by parameter locktype - one
       
 26772 ** of the following:
       
 26773 **
       
 26774 **     (1) SHARED_LOCK
       
 26775 **     (2) RESERVED_LOCK
       
 26776 **     (3) PENDING_LOCK
       
 26777 **     (4) EXCLUSIVE_LOCK
       
 26778 **
       
 26779 ** Sometimes when requesting one lock state, additional lock states
       
 26780 ** are inserted in between.  The locking might fail on one of the later
       
 26781 ** transitions leaving the lock state different from what it started but
       
 26782 ** still short of its goal.  The following chart shows the allowed
       
 26783 ** transitions and the inserted intermediate states:
       
 26784 **
       
 26785 **    UNLOCKED -> SHARED
       
 26786 **    SHARED -> RESERVED
       
 26787 **    SHARED -> (PENDING) -> EXCLUSIVE
       
 26788 **    RESERVED -> (PENDING) -> EXCLUSIVE
       
 26789 **    PENDING -> EXCLUSIVE
       
 26790 **
       
 26791 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
       
 26792 ** routine to lower a locking level.
       
 26793 */
       
 26794 static int proxyLock(sqlite3_file *id, int locktype) {
       
 26795   unixFile *pFile = (unixFile*)id;
       
 26796   int rc = proxyTakeConch(pFile);
       
 26797   if( rc==SQLITE_OK ){
       
 26798     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
       
 26799     unixFile *proxy = pCtx->lockProxy;
       
 26800     rc = proxy->pMethod->xLock((sqlite3_file*)proxy, locktype);
       
 26801     pFile->locktype = proxy->locktype;
       
 26802   }
       
 26803   return rc;
       
 26804 }
       
 26805 
       
 26806 
       
 26807 /*
       
 26808 ** Lower the locking level on file descriptor pFile to locktype.  locktype
       
 26809 ** must be either NO_LOCK or SHARED_LOCK.
       
 26810 **
       
 26811 ** If the locking level of the file descriptor is already at or below
       
 26812 ** the requested locking level, this routine is a no-op.
       
 26813 */
       
 26814 static int proxyUnlock(sqlite3_file *id, int locktype) {
       
 26815   unixFile *pFile = (unixFile*)id;
       
 26816   int rc = proxyTakeConch(pFile);
       
 26817   if( rc==SQLITE_OK ){
       
 26818     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
       
 26819     unixFile *proxy = pCtx->lockProxy;
       
 26820     rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, locktype);
       
 26821     pFile->locktype = proxy->locktype;
       
 26822   }
       
 26823   return rc;
       
 26824 }
       
 26825 
       
 26826 /*
       
 26827 ** Close a file that uses proxy locks.
       
 26828 */
       
 26829 static int proxyClose(sqlite3_file *id) {
       
 26830   if( id ){
       
 26831     unixFile *pFile = (unixFile*)id;
       
 26832     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
       
 26833     unixFile *lockProxy = pCtx->lockProxy;
       
 26834     unixFile *conchFile = pCtx->conchFile;
       
 26835     int rc = SQLITE_OK;
       
 26836     
       
 26837     if( lockProxy ){
       
 26838       rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
       
 26839       if( rc ) return rc;
       
 26840       rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
       
 26841       if( rc ) return rc;
       
 26842       sqlite3_free(lockProxy);
       
 26843       pCtx->lockProxy = 0;
       
 26844     }
       
 26845     if( conchFile ){
       
 26846       if( pCtx->conchHeld ){
       
 26847         rc = proxyReleaseConch(pFile);
       
 26848         if( rc ) return rc;
       
 26849       }
       
 26850       rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
       
 26851       if( rc ) return rc;
       
 26852       sqlite3_free(conchFile);
       
 26853     }
       
 26854     sqlite3_free(pCtx->lockProxyPath);
       
 26855     sqlite3_free(pCtx->conchFilePath);
       
 26856     sqlite3_free(pCtx->dbPath);
       
 26857     /* restore the original locking context and pMethod then close it */
       
 26858     pFile->lockingContext = pCtx->oldLockingContext;
       
 26859     pFile->pMethod = pCtx->pOldMethod;
       
 26860     sqlite3_free(pCtx);
       
 26861     return pFile->pMethod->xClose(id);
       
 26862   }
       
 26863   return SQLITE_OK;
       
 26864 }
       
 26865 
       
 26866 
       
 26867 
       
 26868 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
       
 26869 /*
       
 26870 ** The proxy locking style is intended for use with AFP filesystems.
       
 26871 ** And since AFP is only supported on MacOSX, the proxy locking is also
       
 26872 ** restricted to MacOSX.
       
 26873 ** 
       
 26874 **
       
 26875 ******************* End of the proxy lock implementation **********************
       
 26876 ******************************************************************************/
       
 26877 
       
 26878 /*
       
 26879 ** Initialize the operating system interface.
       
 26880 **
       
 26881 ** This routine registers all VFS implementations for unix-like operating
       
 26882 ** systems.  This routine, and the sqlite3_os_end() routine that follows,
       
 26883 ** should be the only routines in this file that are visible from other
       
 26884 ** files.
       
 26885 **
       
 26886 ** This routine is called once during SQLite initialization and by a
       
 26887 ** single thread.  The memory allocation and mutex subsystems have not
       
 26888 ** necessarily been initialized when this routine is called, and so they
       
 26889 ** should not be used.
       
 26890 */
       
 26891 SQLITE_API int sqlite3_os_init(void){ 
       
 26892   /* 
       
 26893   ** The following macro defines an initializer for an sqlite3_vfs object.
       
 26894   ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
       
 26895   ** to the "finder" function.  (pAppData is a pointer to a pointer because
       
 26896   ** silly C90 rules prohibit a void* from being cast to a function pointer
       
 26897   ** and so we have to go through the intermediate pointer to avoid problems
       
 26898   ** when compiling with -pedantic-errors on GCC.)
       
 26899   **
       
 26900   ** The FINDER parameter to this macro is the name of the pointer to the
       
 26901   ** finder-function.  The finder-function returns a pointer to the
       
 26902   ** sqlite_io_methods object that implements the desired locking
       
 26903   ** behaviors.  See the division above that contains the IOMETHODS
       
 26904   ** macro for addition information on finder-functions.
       
 26905   **
       
 26906   ** Most finders simply return a pointer to a fixed sqlite3_io_methods
       
 26907   ** object.  But the "autolockIoFinder" available on MacOSX does a little
       
 26908   ** more than that; it looks at the filesystem type that hosts the 
       
 26909   ** database file and tries to choose an locking method appropriate for
       
 26910   ** that filesystem time.
       
 26911   */
       
 26912   #define UNIXVFS(VFSNAME, FINDER) {                        \
       
 26913     1,                    /* iVersion */                    \
       
 26914     sizeof(unixFile),     /* szOsFile */                    \
       
 26915     MAX_PATHNAME,         /* mxPathname */                  \
       
 26916     0,                    /* pNext */                       \
       
 26917     VFSNAME,              /* zName */                       \
       
 26918     (void*)&FINDER,       /* pAppData */                    \
       
 26919     unixOpen,             /* xOpen */                       \
       
 26920     unixDelete,           /* xDelete */                     \
       
 26921     unixAccess,           /* xAccess */                     \
       
 26922     unixFullPathname,     /* xFullPathname */               \
       
 26923     unixDlOpen,           /* xDlOpen */                     \
       
 26924     unixDlError,          /* xDlError */                    \
       
 26925     unixDlSym,            /* xDlSym */                      \
       
 26926     unixDlClose,          /* xDlClose */                    \
       
 26927     unixRandomness,       /* xRandomness */                 \
       
 26928     unixSleep,            /* xSleep */                      \
       
 26929     unixCurrentTime,      /* xCurrentTime */                \
       
 26930     unixGetLastError      /* xGetLastError */               \
       
 26931   }
       
 26932 
       
 26933   /*
       
 26934   ** All default VFSes for unix are contained in the following array.
       
 26935   **
       
 26936   ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
       
 26937   ** by the SQLite core when the VFS is registered.  So the following
       
 26938   ** array cannot be const.
       
 26939   */
       
 26940   static sqlite3_vfs aVfs[] = {
       
 26941 #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
       
 26942     UNIXVFS("unix",          autolockIoFinder ),
       
 26943 #else
       
 26944     UNIXVFS("unix",          posixIoFinder ),
       
 26945 #endif
       
 26946     UNIXVFS("unix-none",     nolockIoFinder ),
       
 26947     UNIXVFS("unix-dotfile",  dotlockIoFinder ),
       
 26948     UNIXVFS("unix-wfl",      posixWflIoFinder ),
       
 26949 #if OS_VXWORKS
       
 26950     UNIXVFS("unix-namedsem", semIoFinder ),
       
 26951 #endif
       
 26952 #if SQLITE_ENABLE_LOCKING_STYLE
       
 26953     UNIXVFS("unix-posix",    posixIoFinder ),
       
 26954 #if !OS_VXWORKS
       
 26955     UNIXVFS("unix-flock",    flockIoFinder ),
       
 26956 #endif
       
 26957 #endif
       
 26958 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
       
 26959     UNIXVFS("unix-afp",      afpIoFinder ),
       
 26960     UNIXVFS("unix-proxy",    proxyIoFinder ),
       
 26961 #endif
       
 26962   };
       
 26963   unsigned int i;          /* Loop counter */
       
 26964 
       
 26965   /* Register all VFSes defined in the aVfs[] array */
       
 26966   for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
       
 26967     sqlite3_vfs_register(&aVfs[i], i==0);
       
 26968   }
       
 26969   return SQLITE_OK; 
       
 26970 }
       
 26971 
       
 26972 /*
       
 26973 ** Shutdown the operating system interface.
       
 26974 **
       
 26975 ** Some operating systems might need to do some cleanup in this routine,
       
 26976 ** to release dynamically allocated objects.  But not on unix.
       
 26977 ** This routine is a no-op for unix.
       
 26978 */
       
 26979 SQLITE_API int sqlite3_os_end(void){ 
       
 26980   return SQLITE_OK; 
       
 26981 }
       
 26982  
       
 26983 #endif /* SQLITE_OS_UNIX */
       
 26984 
       
 26985 /************** End of os_unix.c *********************************************/
       
 26986 /************** Begin file os_win.c ******************************************/
       
 26987 /*
       
 26988 ** 2004 May 22
       
 26989 **
       
 26990 ** The author disclaims copyright to this source code.  In place of
       
 26991 ** a legal notice, here is a blessing:
       
 26992 **
       
 26993 **    May you do good and not evil.
       
 26994 **    May you find forgiveness for yourself and forgive others.
       
 26995 **    May you share freely, never taking more than you give.
       
 26996 **
       
 26997 ******************************************************************************
       
 26998 **
       
 26999 ** This file contains code that is specific to windows.
       
 27000 */
       
 27001 #if SQLITE_OS_WIN               /* This file is used for windows only */
       
 27002 
       
 27003 
       
 27004 /*
       
 27005 ** A Note About Memory Allocation:
       
 27006 **
       
 27007 ** This driver uses malloc()/free() directly rather than going through
       
 27008 ** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
       
 27009 ** are designed for use on embedded systems where memory is scarce and
       
 27010 ** malloc failures happen frequently.  Win32 does not typically run on
       
 27011 ** embedded systems, and when it does the developers normally have bigger
       
 27012 ** problems to worry about than running out of memory.  So there is not
       
 27013 ** a compelling need to use the wrappers.
       
 27014 **
       
 27015 ** But there is a good reason to not use the wrappers.  If we use the
       
 27016 ** wrappers then we will get simulated malloc() failures within this
       
 27017 ** driver.  And that causes all kinds of problems for our tests.  We
       
 27018 ** could enhance SQLite to deal with simulated malloc failures within
       
 27019 ** the OS driver, but the code to deal with those failure would not
       
 27020 ** be exercised on Linux (which does not need to malloc() in the driver)
       
 27021 ** and so we would have difficulty writing coverage tests for that
       
 27022 ** code.  Better to leave the code out, we think.
       
 27023 **
       
 27024 ** The point of this discussion is as follows:  When creating a new
       
 27025 ** OS layer for an embedded system, if you use this file as an example,
       
 27026 ** avoid the use of malloc()/free().  Those routines work ok on windows
       
 27027 ** desktops but not so well in embedded systems.
       
 27028 */
       
 27029 
       
 27030 #include <qconfig.h>
       
 27031 
       
 27032 #include <winbase.h>
       
 27033 
       
 27034 #ifdef __CYGWIN__
       
 27035 # include <sys/cygwin.h>
       
 27036 #endif
       
 27037 
       
 27038 /*
       
 27039 ** Macros used to determine whether or not to use threads.
       
 27040 */
       
 27041 #ifndef QT_NO_THREAD
       
 27042 # define SQLITE_W32_THREADS 1
       
 27043 #endif
       
 27044 
       
 27045 /*
       
 27046 ** Include code that is common to all os_*.c files
       
 27047 */
       
 27048 /************** Include os_common.h in the middle of os_win.c ****************/
       
 27049 /************** Begin file os_common.h ***************************************/
       
 27050 /*
       
 27051 ** 2004 May 22
       
 27052 **
       
 27053 ** The author disclaims copyright to this source code.  In place of
       
 27054 ** a legal notice, here is a blessing:
       
 27055 **
       
 27056 **    May you do good and not evil.
       
 27057 **    May you find forgiveness for yourself and forgive others.
       
 27058 **    May you share freely, never taking more than you give.
       
 27059 **
       
 27060 ******************************************************************************
       
 27061 **
       
 27062 ** This file contains macros and a little bit of code that is common to
       
 27063 ** all of the platform-specific files (os_*.c) and is #included into those
       
 27064 ** files.
       
 27065 **
       
 27066 ** This file should be #included by the os_*.c files only.  It is not a
       
 27067 ** general purpose header file.
       
 27068 **
       
 27069 ** $Id: os_common.h,v 1.38 2009/02/24 18:40:50 danielk1977 Exp $
       
 27070 */
       
 27071 #ifndef _OS_COMMON_H_
       
 27072 #define _OS_COMMON_H_
       
 27073 
       
 27074 /*
       
 27075 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
       
 27076 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
       
 27077 ** switch.  The following code should catch this problem at compile-time.
       
 27078 */
       
 27079 #ifdef MEMORY_DEBUG
       
 27080 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
       
 27081 #endif
       
 27082 
       
 27083 #ifdef SQLITE_DEBUG
       
 27084 SQLITE_PRIVATE int sqlite3OSTrace = 0;
       
 27085 #define OSTRACE1(X)         if( sqlite3OSTrace ) sqlite3DebugPrintf(X)
       
 27086 #define OSTRACE2(X,Y)       if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y)
       
 27087 #define OSTRACE3(X,Y,Z)     if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z)
       
 27088 #define OSTRACE4(X,Y,Z,A)   if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A)
       
 27089 #define OSTRACE5(X,Y,Z,A,B) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A,B)
       
 27090 #define OSTRACE6(X,Y,Z,A,B,C) \
       
 27091     if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
       
 27092 #define OSTRACE7(X,Y,Z,A,B,C,D) \
       
 27093     if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
       
 27094 #else
       
 27095 #define OSTRACE1(X)
       
 27096 #define OSTRACE2(X,Y)
       
 27097 #define OSTRACE3(X,Y,Z)
       
 27098 #define OSTRACE4(X,Y,Z,A)
       
 27099 #define OSTRACE5(X,Y,Z,A,B)
       
 27100 #define OSTRACE6(X,Y,Z,A,B,C)
       
 27101 #define OSTRACE7(X,Y,Z,A,B,C,D)
       
 27102 #endif
       
 27103 
       
 27104 /*
       
 27105 ** Macros for performance tracing.  Normally turned off.  Only works
       
 27106 ** on i486 hardware.
       
 27107 */
       
 27108 #ifdef SQLITE_PERFORMANCE_TRACE
       
 27109 
       
 27110 /* 
       
 27111 ** hwtime.h contains inline assembler code for implementing 
       
 27112 ** high-performance timing routines.
       
 27113 */
       
 27114 /************** Include hwtime.h in the middle of os_common.h ****************/
       
 27115 /************** Begin file hwtime.h ******************************************/
       
 27116 /*
       
 27117 ** 2008 May 27
       
 27118 **
       
 27119 ** The author disclaims copyright to this source code.  In place of
       
 27120 ** a legal notice, here is a blessing:
       
 27121 **
       
 27122 **    May you do good and not evil.
       
 27123 **    May you find forgiveness for yourself and forgive others.
       
 27124 **    May you share freely, never taking more than you give.
       
 27125 **
       
 27126 ******************************************************************************
       
 27127 **
       
 27128 ** This file contains inline asm code for retrieving "high-performance"
       
 27129 ** counters for x86 class CPUs.
       
 27130 **
       
 27131 ** $Id: hwtime.h,v 1.3 2008/08/01 14:33:15 shane Exp $
       
 27132 */
       
 27133 #ifndef _HWTIME_H_
       
 27134 #define _HWTIME_H_
       
 27135 
       
 27136 /*
       
 27137 ** The following routine only works on pentium-class (or newer) processors.
       
 27138 ** It uses the RDTSC opcode to read the cycle count value out of the
       
 27139 ** processor and returns that value.  This can be used for high-res
       
 27140 ** profiling.
       
 27141 */
       
 27142 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
       
 27143       (defined(i386) || defined(__i386__) || defined(_M_IX86))
       
 27144 
       
 27145   #if defined(__GNUC__)
       
 27146 
       
 27147   __inline__ sqlite_uint64 sqlite3Hwtime(void){
       
 27148      unsigned int lo, hi;
       
 27149      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
       
 27150      return (sqlite_uint64)hi << 32 | lo;
       
 27151   }
       
 27152 
       
 27153   #elif defined(_MSC_VER)
       
 27154 
       
 27155   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
       
 27156      __asm {
       
 27157         rdtsc
       
 27158         ret       ; return value at EDX:EAX
       
 27159      }
       
 27160   }
       
 27161 
       
 27162   #endif
       
 27163 
       
 27164 #elif (defined(__GNUC__) && defined(__x86_64__))
       
 27165 
       
 27166   __inline__ sqlite_uint64 sqlite3Hwtime(void){
       
 27167       unsigned long val;
       
 27168       __asm__ __volatile__ ("rdtsc" : "=A" (val));
       
 27169       return val;
       
 27170   }
       
 27171  
       
 27172 #elif (defined(__GNUC__) && defined(__ppc__))
       
 27173 
       
 27174   __inline__ sqlite_uint64 sqlite3Hwtime(void){
       
 27175       unsigned long long retval;
       
 27176       unsigned long junk;
       
 27177       __asm__ __volatile__ ("\n\
       
 27178           1:      mftbu   %1\n\
       
 27179                   mftb    %L0\n\
       
 27180                   mftbu   %0\n\
       
 27181                   cmpw    %0,%1\n\
       
 27182                   bne     1b"
       
 27183                   : "=r" (retval), "=r" (junk));
       
 27184       return retval;
       
 27185   }
       
 27186 
       
 27187 #else
       
 27188 
       
 27189   #error Need implementation of sqlite3Hwtime() for your platform.
       
 27190 
       
 27191   /*
       
 27192   ** To compile without implementing sqlite3Hwtime() for your platform,
       
 27193   ** you can remove the above #error and use the following
       
 27194   ** stub function.  You will lose timing support for many
       
 27195   ** of the debugging and testing utilities, but it should at
       
 27196   ** least compile and run.
       
 27197   */
       
 27198 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
       
 27199 
       
 27200 #endif
       
 27201 
       
 27202 #endif /* !defined(_HWTIME_H_) */
       
 27203 
       
 27204 /************** End of hwtime.h **********************************************/
       
 27205 /************** Continuing where we left off in os_common.h ******************/
       
 27206 
       
 27207 static sqlite_uint64 g_start;
       
 27208 static sqlite_uint64 g_elapsed;
       
 27209 #define TIMER_START       g_start=sqlite3Hwtime()
       
 27210 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
       
 27211 #define TIMER_ELAPSED     g_elapsed
       
 27212 #else
       
 27213 #define TIMER_START
       
 27214 #define TIMER_END
       
 27215 #define TIMER_ELAPSED     ((sqlite_uint64)0)
       
 27216 #endif
       
 27217 
       
 27218 /*
       
 27219 ** If we compile with the SQLITE_TEST macro set, then the following block
       
 27220 ** of code will give us the ability to simulate a disk I/O error.  This
       
 27221 ** is used for testing the I/O recovery logic.
       
 27222 */
       
 27223 #ifdef SQLITE_TEST
       
 27224 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
       
 27225 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
       
 27226 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
       
 27227 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
       
 27228 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
       
 27229 SQLITE_API int sqlite3_diskfull_pending = 0;
       
 27230 SQLITE_API int sqlite3_diskfull = 0;
       
 27231 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
       
 27232 #define SimulateIOError(CODE)  \
       
 27233   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
       
 27234        || sqlite3_io_error_pending-- == 1 )  \
       
 27235               { local_ioerr(); CODE; }
       
 27236 static void local_ioerr(){
       
 27237   IOTRACE(("IOERR\n"));
       
 27238   sqlite3_io_error_hit++;
       
 27239   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
       
 27240 }
       
 27241 #define SimulateDiskfullError(CODE) \
       
 27242    if( sqlite3_diskfull_pending ){ \
       
 27243      if( sqlite3_diskfull_pending == 1 ){ \
       
 27244        local_ioerr(); \
       
 27245        sqlite3_diskfull = 1; \
       
 27246        sqlite3_io_error_hit = 1; \
       
 27247        CODE; \
       
 27248      }else{ \
       
 27249        sqlite3_diskfull_pending--; \
       
 27250      } \
       
 27251    }
       
 27252 #else
       
 27253 #define SimulateIOErrorBenign(X)
       
 27254 #define SimulateIOError(A)
       
 27255 #define SimulateDiskfullError(A)
       
 27256 #endif
       
 27257 
       
 27258 /*
       
 27259 ** When testing, keep a count of the number of open files.
       
 27260 */
       
 27261 #ifdef SQLITE_TEST
       
 27262 SQLITE_API int sqlite3_open_file_count = 0;
       
 27263 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
       
 27264 #else
       
 27265 #define OpenCounter(X)
       
 27266 #endif
       
 27267 
       
 27268 #endif /* !defined(_OS_COMMON_H_) */
       
 27269 
       
 27270 /************** End of os_common.h *******************************************/
       
 27271 /************** Continuing where we left off in os_win.c *********************/
       
 27272 
       
 27273 /*
       
 27274 ** Some microsoft compilers lack this definition.
       
 27275 */
       
 27276 #ifndef INVALID_FILE_ATTRIBUTES
       
 27277 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1) 
       
 27278 #endif
       
 27279 
       
 27280 /*
       
 27281 ** Determine if we are dealing with WindowsCE - which has a much
       
 27282 ** reduced API.
       
 27283 */
       
 27284 #if SQLITE_OS_WINCE
       
 27285 # define AreFileApisANSI() 1
       
 27286 # define GetDiskFreeSpaceW() 0
       
 27287 #endif
       
 27288 
       
 27289 /*
       
 27290 ** WinCE lacks native support for file locking so we have to fake it
       
 27291 ** with some code of our own.
       
 27292 */
       
 27293 #if SQLITE_OS_WINCE
       
 27294 typedef struct winceLock {
       
 27295   int nReaders;       /* Number of reader locks obtained */
       
 27296   BOOL bPending;      /* Indicates a pending lock has been obtained */
       
 27297   BOOL bReserved;     /* Indicates a reserved lock has been obtained */
       
 27298   BOOL bExclusive;    /* Indicates an exclusive lock has been obtained */
       
 27299 } winceLock;
       
 27300 #endif
       
 27301 
       
 27302 /*
       
 27303 ** The winFile structure is a subclass of sqlite3_file* specific to the win32
       
 27304 ** portability layer.
       
 27305 */
       
 27306 typedef struct winFile winFile;
       
 27307 struct winFile {
       
 27308   const sqlite3_io_methods *pMethod;/* Must be first */
       
 27309   HANDLE h;               /* Handle for accessing the file */
       
 27310   unsigned char locktype; /* Type of lock currently held on this file */
       
 27311   short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
       
 27312   DWORD lastErrno;        /* The Windows errno from the last I/O error */
       
 27313   DWORD sectorSize;       /* Sector size of the device file is on */
       
 27314 #if SQLITE_OS_WINCE
       
 27315   WCHAR *zDeleteOnClose;  /* Name of file to delete when closing */
       
 27316   HANDLE hMutex;          /* Mutex used to control access to shared lock */  
       
 27317   HANDLE hShared;         /* Shared memory segment used for locking */
       
 27318   winceLock local;        /* Locks obtained by this instance of winFile */
       
 27319   winceLock *shared;      /* Global shared lock memory for the file  */
       
 27320 #endif
       
 27321 };
       
 27322 
       
 27323 /*
       
 27324 ** Forward prototypes.
       
 27325 */
       
 27326 static int getSectorSize(
       
 27327     sqlite3_vfs *pVfs,
       
 27328     const char *zRelative     /* UTF-8 file name */
       
 27329 );
       
 27330 
       
 27331 /*
       
 27332 ** The following variable is (normally) set once and never changes
       
 27333 ** thereafter.  It records whether the operating system is Win95
       
 27334 ** or WinNT.
       
 27335 **
       
 27336 ** 0:   Operating system unknown.
       
 27337 ** 1:   Operating system is Win95.
       
 27338 ** 2:   Operating system is WinNT.
       
 27339 **
       
 27340 ** In order to facilitate testing on a WinNT system, the test fixture
       
 27341 ** can manually set this value to 1 to emulate Win98 behavior.
       
 27342 */
       
 27343 #ifdef SQLITE_TEST
       
 27344 SQLITE_API int sqlite3_os_type = 0;
       
 27345 #else
       
 27346 static int sqlite3_os_type = 0;
       
 27347 #endif
       
 27348 
       
 27349 /*
       
 27350 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
       
 27351 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
       
 27352 **
       
 27353 ** Here is an interesting observation:  Win95, Win98, and WinME lack
       
 27354 ** the LockFileEx() API.  But we can still statically link against that
       
 27355 ** API as long as we don't call it when running Win95/98/ME.  A call to
       
 27356 ** this routine is used to determine if the host is Win95/98/ME or
       
 27357 ** WinNT/2K/XP so that we will know whether or not we can safely call
       
 27358 ** the LockFileEx() API.
       
 27359 */
       
 27360 #if SQLITE_OS_WINCE
       
 27361 # define isNT()  (1)
       
 27362 #else
       
 27363   static int isNT(void){
       
 27364     if( sqlite3_os_type==0 ){
       
 27365       OSVERSIONINFO sInfo;
       
 27366       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
       
 27367       GetVersionEx(&sInfo);
       
 27368       sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
       
 27369     }
       
 27370     return sqlite3_os_type==2;
       
 27371   }
       
 27372 #endif /* SQLITE_OS_WINCE */
       
 27373 
       
 27374 /*
       
 27375 ** Convert a UTF-8 string to microsoft unicode (UTF-16?). 
       
 27376 **
       
 27377 ** Space to hold the returned string is obtained from malloc.
       
 27378 */
       
 27379 static WCHAR *utf8ToUnicode(const char *zFilename){
       
 27380   int nChar;
       
 27381   WCHAR *zWideFilename;
       
 27382 
       
 27383   nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
       
 27384   zWideFilename = malloc( nChar*sizeof(zWideFilename[0]) );
       
 27385   if( zWideFilename==0 ){
       
 27386     return 0;
       
 27387   }
       
 27388   nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar);
       
 27389   if( nChar==0 ){
       
 27390     free(zWideFilename);
       
 27391     zWideFilename = 0;
       
 27392   }
       
 27393   return zWideFilename;
       
 27394 }
       
 27395 
       
 27396 /*
       
 27397 ** Convert microsoft unicode to UTF-8.  Space to hold the returned string is
       
 27398 ** obtained from malloc().
       
 27399 */
       
 27400 static char *unicodeToUtf8(const WCHAR *zWideFilename){
       
 27401   int nByte;
       
 27402   char *zFilename;
       
 27403 
       
 27404   nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
       
 27405   zFilename = malloc( nByte );
       
 27406   if( zFilename==0 ){
       
 27407     return 0;
       
 27408   }
       
 27409   nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
       
 27410                               0, 0);
       
 27411   if( nByte == 0 ){
       
 27412     free(zFilename);
       
 27413     zFilename = 0;
       
 27414   }
       
 27415   return zFilename;
       
 27416 }
       
 27417 
       
 27418 /*
       
 27419 ** Convert an ansi string to microsoft unicode, based on the
       
 27420 ** current codepage settings for file apis.
       
 27421 ** 
       
 27422 ** Space to hold the returned string is obtained
       
 27423 ** from malloc.
       
 27424 */
       
 27425 static WCHAR *mbcsToUnicode(const char *zFilename){
       
 27426   int nByte;
       
 27427   WCHAR *zMbcsFilename;
       
 27428   int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
       
 27429 
       
 27430   nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR);
       
 27431   zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) );
       
 27432   if( zMbcsFilename==0 ){
       
 27433     return 0;
       
 27434   }
       
 27435   nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte);
       
 27436   if( nByte==0 ){
       
 27437     free(zMbcsFilename);
       
 27438     zMbcsFilename = 0;
       
 27439   }
       
 27440   return zMbcsFilename;
       
 27441 }
       
 27442 
       
 27443 /*
       
 27444 ** Convert microsoft unicode to multibyte character string, based on the
       
 27445 ** user's Ansi codepage.
       
 27446 **
       
 27447 ** Space to hold the returned string is obtained from
       
 27448 ** malloc().
       
 27449 */
       
 27450 static char *unicodeToMbcs(const WCHAR *zWideFilename){
       
 27451   int nByte;
       
 27452   char *zFilename;
       
 27453   int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
       
 27454 
       
 27455   nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
       
 27456   zFilename = malloc( nByte );
       
 27457   if( zFilename==0 ){
       
 27458     return 0;
       
 27459   }
       
 27460   nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte,
       
 27461                               0, 0);
       
 27462   if( nByte == 0 ){
       
 27463     free(zFilename);
       
 27464     zFilename = 0;
       
 27465   }
       
 27466   return zFilename;
       
 27467 }
       
 27468 
       
 27469 /*
       
 27470 ** Convert multibyte character string to UTF-8.  Space to hold the
       
 27471 ** returned string is obtained from malloc().
       
 27472 */
       
 27473 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
       
 27474   char *zFilenameUtf8;
       
 27475   WCHAR *zTmpWide;
       
 27476 
       
 27477   zTmpWide = mbcsToUnicode(zFilename);
       
 27478   if( zTmpWide==0 ){
       
 27479     return 0;
       
 27480   }
       
 27481   zFilenameUtf8 = unicodeToUtf8(zTmpWide);
       
 27482   free(zTmpWide);
       
 27483   return zFilenameUtf8;
       
 27484 }
       
 27485 
       
 27486 /*
       
 27487 ** Convert UTF-8 to multibyte character string.  Space to hold the 
       
 27488 ** returned string is obtained from malloc().
       
 27489 */
       
 27490 static char *utf8ToMbcs(const char *zFilename){
       
 27491   char *zFilenameMbcs;
       
 27492   WCHAR *zTmpWide;
       
 27493 
       
 27494   zTmpWide = utf8ToUnicode(zFilename);
       
 27495   if( zTmpWide==0 ){
       
 27496     return 0;
       
 27497   }
       
 27498   zFilenameMbcs = unicodeToMbcs(zTmpWide);
       
 27499   free(zTmpWide);
       
 27500   return zFilenameMbcs;
       
 27501 }
       
 27502 
       
 27503 #if SQLITE_OS_WINCE
       
 27504 /*************************************************************************
       
 27505 ** This section contains code for WinCE only.
       
 27506 */
       
 27507 /*
       
 27508 ** WindowsCE does not have a localtime() function.  So create a
       
 27509 ** substitute.
       
 27510 */
       
 27511 static struct tm *__cdecl localtime(const time_t *t)
       
 27512 {
       
 27513   static struct tm y;
       
 27514   FILETIME uTm, lTm;
       
 27515   SYSTEMTIME pTm;
       
 27516   sqlite3_int64 t64;
       
 27517   t64 = *t;
       
 27518   t64 = (t64 + 11644473600)*10000000;
       
 27519   uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
       
 27520   uTm.dwHighDateTime= (DWORD)(t64 >> 32);
       
 27521   FileTimeToLocalFileTime(&uTm,&lTm);
       
 27522   FileTimeToSystemTime(&lTm,&pTm);
       
 27523   y.tm_year = pTm.wYear - 1900;
       
 27524   y.tm_mon = pTm.wMonth - 1;
       
 27525   y.tm_wday = pTm.wDayOfWeek;
       
 27526   y.tm_mday = pTm.wDay;
       
 27527   y.tm_hour = pTm.wHour;
       
 27528   y.tm_min = pTm.wMinute;
       
 27529   y.tm_sec = pTm.wSecond;
       
 27530   return &y;
       
 27531 }
       
 27532 
       
 27533 /* This will never be called, but defined to make the code compile */
       
 27534 #define GetTempPathA(a,b)
       
 27535 
       
 27536 #define LockFile(a,b,c,d,e)       winceLockFile(&a, b, c, d, e)
       
 27537 #define UnlockFile(a,b,c,d,e)     winceUnlockFile(&a, b, c, d, e)
       
 27538 #define LockFileEx(a,b,c,d,e,f)   winceLockFileEx(&a, b, c, d, e, f)
       
 27539 
       
 27540 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
       
 27541 
       
 27542 /*
       
 27543 ** Acquire a lock on the handle h
       
 27544 */
       
 27545 static void winceMutexAcquire(HANDLE h){
       
 27546    DWORD dwErr;
       
 27547    do {
       
 27548      dwErr = WaitForSingleObject(h, INFINITE);
       
 27549    } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
       
 27550 }
       
 27551 /*
       
 27552 ** Release a lock acquired by winceMutexAcquire()
       
 27553 */
       
 27554 #define winceMutexRelease(h) ReleaseMutex(h)
       
 27555 
       
 27556 /*
       
 27557 ** Create the mutex and shared memory used for locking in the file
       
 27558 ** descriptor pFile
       
 27559 */
       
 27560 static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
       
 27561   WCHAR *zTok;
       
 27562   WCHAR *zName = utf8ToUnicode(zFilename);
       
 27563   BOOL bInit = TRUE;
       
 27564 
       
 27565   /* Initialize the local lockdata */
       
 27566   ZeroMemory(&pFile->local, sizeof(pFile->local));
       
 27567 
       
 27568   /* Replace the backslashes from the filename and lowercase it
       
 27569   ** to derive a mutex name. */
       
 27570   zTok = CharLowerW(zName);
       
 27571   for (;*zTok;zTok++){
       
 27572     if (*zTok == '\\') *zTok = '_';
       
 27573   }
       
 27574 
       
 27575   /* Create/open the named mutex */
       
 27576   pFile->hMutex = CreateMutexW(NULL, FALSE, zName);
       
 27577   if (!pFile->hMutex){
       
 27578     pFile->lastErrno = GetLastError();
       
 27579     free(zName);
       
 27580     return FALSE;
       
 27581   }
       
 27582 
       
 27583   /* Acquire the mutex before continuing */
       
 27584   winceMutexAcquire(pFile->hMutex);
       
 27585   
       
 27586   /* Since the names of named mutexes, semaphores, file mappings etc are 
       
 27587   ** case-sensitive, take advantage of that by uppercasing the mutex name
       
 27588   ** and using that as the shared filemapping name.
       
 27589   */
       
 27590   CharUpperW(zName);
       
 27591   pFile->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
       
 27592                                        PAGE_READWRITE, 0, sizeof(winceLock),
       
 27593                                        zName);  
       
 27594 
       
 27595   /* Set a flag that indicates we're the first to create the memory so it 
       
 27596   ** must be zero-initialized */
       
 27597   if (GetLastError() == ERROR_ALREADY_EXISTS){
       
 27598     bInit = FALSE;
       
 27599   }
       
 27600 
       
 27601   free(zName);
       
 27602 
       
 27603   /* If we succeeded in making the shared memory handle, map it. */
       
 27604   if (pFile->hShared){
       
 27605     pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared, 
       
 27606              FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
       
 27607     /* If mapping failed, close the shared memory handle and erase it */
       
 27608     if (!pFile->shared){
       
 27609       pFile->lastErrno = GetLastError();
       
 27610       CloseHandle(pFile->hShared);
       
 27611       pFile->hShared = NULL;
       
 27612     }
       
 27613   }
       
 27614 
       
 27615   /* If shared memory could not be created, then close the mutex and fail */
       
 27616   if (pFile->hShared == NULL){
       
 27617     winceMutexRelease(pFile->hMutex);
       
 27618     CloseHandle(pFile->hMutex);
       
 27619     pFile->hMutex = NULL;
       
 27620     return FALSE;
       
 27621   }
       
 27622   
       
 27623   /* Initialize the shared memory if we're supposed to */
       
 27624   if (bInit) {
       
 27625     ZeroMemory(pFile->shared, sizeof(winceLock));
       
 27626   }
       
 27627 
       
 27628   winceMutexRelease(pFile->hMutex);
       
 27629   return TRUE;
       
 27630 }
       
 27631 
       
 27632 /*
       
 27633 ** Destroy the part of winFile that deals with wince locks
       
 27634 */
       
 27635 static void winceDestroyLock(winFile *pFile){
       
 27636   if (pFile->hMutex){
       
 27637     /* Acquire the mutex */
       
 27638     winceMutexAcquire(pFile->hMutex);
       
 27639 
       
 27640     /* The following blocks should probably assert in debug mode, but they
       
 27641        are to cleanup in case any locks remained open */
       
 27642     if (pFile->local.nReaders){
       
 27643       pFile->shared->nReaders --;
       
 27644     }
       
 27645     if (pFile->local.bReserved){
       
 27646       pFile->shared->bReserved = FALSE;
       
 27647     }
       
 27648     if (pFile->local.bPending){
       
 27649       pFile->shared->bPending = FALSE;
       
 27650     }
       
 27651     if (pFile->local.bExclusive){
       
 27652       pFile->shared->bExclusive = FALSE;
       
 27653     }
       
 27654 
       
 27655     /* De-reference and close our copy of the shared memory handle */
       
 27656     UnmapViewOfFile(pFile->shared);
       
 27657     CloseHandle(pFile->hShared);
       
 27658 
       
 27659     /* Done with the mutex */
       
 27660     winceMutexRelease(pFile->hMutex);    
       
 27661     CloseHandle(pFile->hMutex);
       
 27662     pFile->hMutex = NULL;
       
 27663   }
       
 27664 }
       
 27665 
       
 27666 /* 
       
 27667 ** An implementation of the LockFile() API of windows for wince
       
 27668 */
       
 27669 static BOOL winceLockFile(
       
 27670   HANDLE *phFile,
       
 27671   DWORD dwFileOffsetLow,
       
 27672   DWORD dwFileOffsetHigh,
       
 27673   DWORD nNumberOfBytesToLockLow,
       
 27674   DWORD nNumberOfBytesToLockHigh
       
 27675 ){
       
 27676   winFile *pFile = HANDLE_TO_WINFILE(phFile);
       
 27677   BOOL bReturn = FALSE;
       
 27678 
       
 27679   UNUSED_PARAMETER(dwFileOffsetHigh);
       
 27680   UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
       
 27681 
       
 27682   if (!pFile->hMutex) return TRUE;
       
 27683   winceMutexAcquire(pFile->hMutex);
       
 27684 
       
 27685   /* Wanting an exclusive lock? */
       
 27686   if (dwFileOffsetLow == (DWORD)SHARED_FIRST
       
 27687        && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
       
 27688     if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
       
 27689        pFile->shared->bExclusive = TRUE;
       
 27690        pFile->local.bExclusive = TRUE;
       
 27691        bReturn = TRUE;
       
 27692     }
       
 27693   }
       
 27694 
       
 27695   /* Want a read-only lock? */
       
 27696   else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
       
 27697            nNumberOfBytesToLockLow == 1){
       
 27698     if (pFile->shared->bExclusive == 0){
       
 27699       pFile->local.nReaders ++;
       
 27700       if (pFile->local.nReaders == 1){
       
 27701         pFile->shared->nReaders ++;
       
 27702       }
       
 27703       bReturn = TRUE;
       
 27704     }
       
 27705   }
       
 27706 
       
 27707   /* Want a pending lock? */
       
 27708   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToLockLow == 1){
       
 27709     /* If no pending lock has been acquired, then acquire it */
       
 27710     if (pFile->shared->bPending == 0) {
       
 27711       pFile->shared->bPending = TRUE;
       
 27712       pFile->local.bPending = TRUE;
       
 27713       bReturn = TRUE;
       
 27714     }
       
 27715   }
       
 27716 
       
 27717   /* Want a reserved lock? */
       
 27718   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToLockLow == 1){
       
 27719     if (pFile->shared->bReserved == 0) {
       
 27720       pFile->shared->bReserved = TRUE;
       
 27721       pFile->local.bReserved = TRUE;
       
 27722       bReturn = TRUE;
       
 27723     }
       
 27724   }
       
 27725 
       
 27726   winceMutexRelease(pFile->hMutex);
       
 27727   return bReturn;
       
 27728 }
       
 27729 
       
 27730 /*
       
 27731 ** An implementation of the UnlockFile API of windows for wince
       
 27732 */
       
 27733 static BOOL winceUnlockFile(
       
 27734   HANDLE *phFile,
       
 27735   DWORD dwFileOffsetLow,
       
 27736   DWORD dwFileOffsetHigh,
       
 27737   DWORD nNumberOfBytesToUnlockLow,
       
 27738   DWORD nNumberOfBytesToUnlockHigh
       
 27739 ){
       
 27740   winFile *pFile = HANDLE_TO_WINFILE(phFile);
       
 27741   BOOL bReturn = FALSE;
       
 27742 
       
 27743   UNUSED_PARAMETER(dwFileOffsetHigh);
       
 27744   UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
       
 27745 
       
 27746   if (!pFile->hMutex) return TRUE;
       
 27747   winceMutexAcquire(pFile->hMutex);
       
 27748 
       
 27749   /* Releasing a reader lock or an exclusive lock */
       
 27750   if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
       
 27751     /* Did we have an exclusive lock? */
       
 27752     if (pFile->local.bExclusive){
       
 27753       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
       
 27754       pFile->local.bExclusive = FALSE;
       
 27755       pFile->shared->bExclusive = FALSE;
       
 27756       bReturn = TRUE;
       
 27757     }
       
 27758 
       
 27759     /* Did we just have a reader lock? */
       
 27760     else if (pFile->local.nReaders){
       
 27761       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE || nNumberOfBytesToUnlockLow == 1);
       
 27762       pFile->local.nReaders --;
       
 27763       if (pFile->local.nReaders == 0)
       
 27764       {
       
 27765         pFile->shared->nReaders --;
       
 27766       }
       
 27767       bReturn = TRUE;
       
 27768     }
       
 27769   }
       
 27770 
       
 27771   /* Releasing a pending lock */
       
 27772   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){
       
 27773     if (pFile->local.bPending){
       
 27774       pFile->local.bPending = FALSE;
       
 27775       pFile->shared->bPending = FALSE;
       
 27776       bReturn = TRUE;
       
 27777     }
       
 27778   }
       
 27779   /* Releasing a reserved lock */
       
 27780   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){
       
 27781     if (pFile->local.bReserved) {
       
 27782       pFile->local.bReserved = FALSE;
       
 27783       pFile->shared->bReserved = FALSE;
       
 27784       bReturn = TRUE;
       
 27785     }
       
 27786   }
       
 27787 
       
 27788   winceMutexRelease(pFile->hMutex);
       
 27789   return bReturn;
       
 27790 }
       
 27791 
       
 27792 /*
       
 27793 ** An implementation of the LockFileEx() API of windows for wince
       
 27794 */
       
 27795 static BOOL winceLockFileEx(
       
 27796   HANDLE *phFile,
       
 27797   DWORD dwFlags,
       
 27798   DWORD dwReserved,
       
 27799   DWORD nNumberOfBytesToLockLow,
       
 27800   DWORD nNumberOfBytesToLockHigh,
       
 27801   LPOVERLAPPED lpOverlapped
       
 27802 ){
       
 27803   UNUSED_PARAMETER(dwReserved);
       
 27804   UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
       
 27805 
       
 27806   /* If the caller wants a shared read lock, forward this call
       
 27807   ** to winceLockFile */
       
 27808   if (lpOverlapped->Offset == (DWORD)SHARED_FIRST &&
       
 27809       dwFlags == 1 &&
       
 27810       nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
       
 27811     return winceLockFile(phFile, SHARED_FIRST, 0, 1, 0);
       
 27812   }
       
 27813   return FALSE;
       
 27814 }
       
 27815 /*
       
 27816 ** End of the special code for wince
       
 27817 *****************************************************************************/
       
 27818 #endif /* SQLITE_OS_WINCE */
       
 27819 
       
 27820 /*****************************************************************************
       
 27821 ** The next group of routines implement the I/O methods specified
       
 27822 ** by the sqlite3_io_methods object.
       
 27823 ******************************************************************************/
       
 27824 
       
 27825 /*
       
 27826 ** Close a file.
       
 27827 **
       
 27828 ** It is reported that an attempt to close a handle might sometimes
       
 27829 ** fail.  This is a very unreasonable result, but windows is notorious
       
 27830 ** for being unreasonable so I do not doubt that it might happen.  If
       
 27831 ** the close fails, we pause for 100 milliseconds and try again.  As
       
 27832 ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
       
 27833 ** giving up and returning an error.
       
 27834 */
       
 27835 #define MX_CLOSE_ATTEMPT 3
       
 27836 static int winClose(sqlite3_file *id){
       
 27837   int rc, cnt = 0;
       
 27838   winFile *pFile = (winFile*)id;
       
 27839 
       
 27840   assert( id!=0 );
       
 27841   OSTRACE2("CLOSE %d\n", pFile->h);
       
 27842   do{
       
 27843     rc = CloseHandle(pFile->h);
       
 27844   }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
       
 27845 #if SQLITE_OS_WINCE
       
 27846 #define WINCE_DELETION_ATTEMPTS 3
       
 27847   winceDestroyLock(pFile);
       
 27848   if( pFile->zDeleteOnClose ){
       
 27849     int cnt = 0;
       
 27850     while(
       
 27851            DeleteFileW(pFile->zDeleteOnClose)==0
       
 27852         && GetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff 
       
 27853         && cnt++ < WINCE_DELETION_ATTEMPTS
       
 27854     ){
       
 27855        Sleep(100);  /* Wait a little before trying again */
       
 27856     }
       
 27857     free(pFile->zDeleteOnClose);
       
 27858   }
       
 27859 #endif
       
 27860   OpenCounter(-1);
       
 27861   return rc ? SQLITE_OK : SQLITE_IOERR;
       
 27862 }
       
 27863 
       
 27864 /*
       
 27865 ** Some microsoft compilers lack this definition.
       
 27866 */
       
 27867 #ifndef INVALID_SET_FILE_POINTER
       
 27868 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
       
 27869 #endif
       
 27870 
       
 27871 /*
       
 27872 ** Read data from a file into a buffer.  Return SQLITE_OK if all
       
 27873 ** bytes were read successfully and SQLITE_IOERR if anything goes
       
 27874 ** wrong.
       
 27875 */
       
 27876 static int winRead(
       
 27877   sqlite3_file *id,          /* File to read from */
       
 27878   void *pBuf,                /* Write content into this buffer */
       
 27879   int amt,                   /* Number of bytes to read */
       
 27880   sqlite3_int64 offset       /* Begin reading at this offset */
       
 27881 ){
       
 27882   LONG upperBits = (LONG)((offset>>32) & 0x7fffffff);
       
 27883   LONG lowerBits = (LONG)(offset & 0xffffffff);
       
 27884   DWORD rc;
       
 27885   winFile *pFile = (winFile*)id;
       
 27886   DWORD error;
       
 27887   DWORD got;
       
 27888 
       
 27889   assert( id!=0 );
       
 27890   SimulateIOError(return SQLITE_IOERR_READ);
       
 27891   OSTRACE3("READ %d lock=%d\n", pFile->h, pFile->locktype);
       
 27892   rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
       
 27893   if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
       
 27894     pFile->lastErrno = error;
       
 27895     return SQLITE_FULL;
       
 27896   }
       
 27897   if( !ReadFile(pFile->h, pBuf, amt, &got, 0) ){
       
 27898     pFile->lastErrno = GetLastError();
       
 27899     return SQLITE_IOERR_READ;
       
 27900   }
       
 27901   if( got==(DWORD)amt ){
       
 27902     return SQLITE_OK;
       
 27903   }else{
       
 27904     /* Unread parts of the buffer must be zero-filled */
       
 27905     memset(&((char*)pBuf)[got], 0, amt-got);
       
 27906     return SQLITE_IOERR_SHORT_READ;
       
 27907   }
       
 27908 }
       
 27909 
       
 27910 /*
       
 27911 ** Write data from a buffer into a file.  Return SQLITE_OK on success
       
 27912 ** or some other error code on failure.
       
 27913 */
       
 27914 static int winWrite(
       
 27915   sqlite3_file *id,         /* File to write into */
       
 27916   const void *pBuf,         /* The bytes to be written */
       
 27917   int amt,                  /* Number of bytes to write */
       
 27918   sqlite3_int64 offset      /* Offset into the file to begin writing at */
       
 27919 ){
       
 27920   LONG upperBits = (LONG)((offset>>32) & 0x7fffffff);
       
 27921   LONG lowerBits = (LONG)(offset & 0xffffffff);
       
 27922   DWORD rc;
       
 27923   winFile *pFile = (winFile*)id;
       
 27924   DWORD error;
       
 27925   DWORD wrote = 0;
       
 27926 
       
 27927   assert( id!=0 );
       
 27928   SimulateIOError(return SQLITE_IOERR_WRITE);
       
 27929   SimulateDiskfullError(return SQLITE_FULL);
       
 27930   OSTRACE3("WRITE %d lock=%d\n", pFile->h, pFile->locktype);
       
 27931   rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
       
 27932   if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
       
 27933     pFile->lastErrno = error;
       
 27934     return SQLITE_FULL;
       
 27935   }
       
 27936   assert( amt>0 );
       
 27937   while(
       
 27938      amt>0
       
 27939      && (rc = WriteFile(pFile->h, pBuf, amt, &wrote, 0))!=0
       
 27940      && wrote>0
       
 27941   ){
       
 27942     amt -= wrote;
       
 27943     pBuf = &((char*)pBuf)[wrote];
       
 27944   }
       
 27945   if( !rc || amt>(int)wrote ){
       
 27946     pFile->lastErrno = GetLastError();
       
 27947     return SQLITE_FULL;
       
 27948   }
       
 27949   return SQLITE_OK;
       
 27950 }
       
 27951 
       
 27952 /*
       
 27953 ** Truncate an open file to a specified size
       
 27954 */
       
 27955 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
       
 27956   LONG upperBits = (LONG)((nByte>>32) & 0x7fffffff);
       
 27957   LONG lowerBits = (LONG)(nByte & 0xffffffff);
       
 27958   DWORD rc;
       
 27959   winFile *pFile = (winFile*)id;
       
 27960   DWORD error;
       
 27961 
       
 27962   assert( id!=0 );
       
 27963   OSTRACE3("TRUNCATE %d %lld\n", pFile->h, nByte);
       
 27964   SimulateIOError(return SQLITE_IOERR_TRUNCATE);
       
 27965   rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
       
 27966   if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
       
 27967     pFile->lastErrno = error;
       
 27968     return SQLITE_IOERR_TRUNCATE;
       
 27969   }
       
 27970   /* SetEndOfFile will fail if nByte is negative */
       
 27971   if( !SetEndOfFile(pFile->h) ){
       
 27972     pFile->lastErrno = GetLastError();
       
 27973     return SQLITE_IOERR_TRUNCATE;
       
 27974   }
       
 27975   return SQLITE_OK;
       
 27976 }
       
 27977 
       
 27978 #ifdef SQLITE_TEST
       
 27979 /*
       
 27980 ** Count the number of fullsyncs and normal syncs.  This is used to test
       
 27981 ** that syncs and fullsyncs are occuring at the right times.
       
 27982 */
       
 27983 SQLITE_API int sqlite3_sync_count = 0;
       
 27984 SQLITE_API int sqlite3_fullsync_count = 0;
       
 27985 #endif
       
 27986 
       
 27987 /*
       
 27988 ** Make sure all writes to a particular file are committed to disk.
       
 27989 */
       
 27990 static int winSync(sqlite3_file *id, int flags){
       
 27991 #ifndef SQLITE_NO_SYNC
       
 27992   winFile *pFile = (winFile*)id;
       
 27993 
       
 27994   assert( id!=0 );
       
 27995   OSTRACE3("SYNC %d lock=%d\n", pFile->h, pFile->locktype);
       
 27996 #else
       
 27997   UNUSED_PARAMETER(id);
       
 27998 #endif
       
 27999 #ifndef SQLITE_TEST
       
 28000   UNUSED_PARAMETER(flags);
       
 28001 #else
       
 28002   if( flags & SQLITE_SYNC_FULL ){
       
 28003     sqlite3_fullsync_count++;
       
 28004   }
       
 28005   sqlite3_sync_count++;
       
 28006 #endif
       
 28007   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
       
 28008   ** no-op
       
 28009   */
       
 28010 #ifdef SQLITE_NO_SYNC
       
 28011     return SQLITE_OK;
       
 28012 #else
       
 28013   if( FlushFileBuffers(pFile->h) ){
       
 28014     return SQLITE_OK;
       
 28015   }else{
       
 28016     pFile->lastErrno = GetLastError();
       
 28017     return SQLITE_IOERR;
       
 28018   }
       
 28019 #endif
       
 28020 }
       
 28021 
       
 28022 /*
       
 28023 ** Determine the current size of a file in bytes
       
 28024 */
       
 28025 static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
       
 28026   DWORD upperBits;
       
 28027   DWORD lowerBits;
       
 28028   winFile *pFile = (winFile*)id;
       
 28029   DWORD error;
       
 28030 
       
 28031   assert( id!=0 );
       
 28032   SimulateIOError(return SQLITE_IOERR_FSTAT);
       
 28033   lowerBits = GetFileSize(pFile->h, &upperBits);
       
 28034   if(   (lowerBits == INVALID_FILE_SIZE)
       
 28035      && ((error = GetLastError()) != NO_ERROR) )
       
 28036   {
       
 28037     pFile->lastErrno = error;
       
 28038     return SQLITE_IOERR_FSTAT;
       
 28039   }
       
 28040   *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
       
 28041   return SQLITE_OK;
       
 28042 }
       
 28043 
       
 28044 /*
       
 28045 ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
       
 28046 */
       
 28047 #ifndef LOCKFILE_FAIL_IMMEDIATELY
       
 28048 # define LOCKFILE_FAIL_IMMEDIATELY 1
       
 28049 #endif
       
 28050 
       
 28051 /*
       
 28052 ** Acquire a reader lock.
       
 28053 ** Different API routines are called depending on whether or not this
       
 28054 ** is Win95 or WinNT.
       
 28055 */
       
 28056 static int getReadLock(winFile *pFile){
       
 28057   int res;
       
 28058   if( isNT() ){
       
 28059     OVERLAPPED ovlp;
       
 28060     ovlp.Offset = SHARED_FIRST;
       
 28061     ovlp.OffsetHigh = 0;
       
 28062     ovlp.hEvent = 0;
       
 28063     res = LockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY,
       
 28064                      0, SHARED_SIZE, 0, &ovlp);
       
 28065 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
       
 28066 */
       
 28067 #if SQLITE_OS_WINCE==0
       
 28068   }else{
       
 28069     int lk;
       
 28070     sqlite3_randomness(sizeof(lk), &lk);
       
 28071     pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
       
 28072     res = LockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
       
 28073 #endif
       
 28074   }
       
 28075   if( res == 0 ){
       
 28076     pFile->lastErrno = GetLastError();
       
 28077   }
       
 28078   return res;
       
 28079 }
       
 28080 
       
 28081 /*
       
 28082 ** Undo a readlock
       
 28083 */
       
 28084 static int unlockReadLock(winFile *pFile){
       
 28085   int res;
       
 28086   if( isNT() ){
       
 28087     res = UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
       
 28088 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
       
 28089 */
       
 28090 #if SQLITE_OS_WINCE==0
       
 28091   }else{
       
 28092     res = UnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0);
       
 28093 #endif
       
 28094   }
       
 28095   if( res == 0 ){
       
 28096     pFile->lastErrno = GetLastError();
       
 28097   }
       
 28098   return res;
       
 28099 }
       
 28100 
       
 28101 /*
       
 28102 ** Lock the file with the lock specified by parameter locktype - one
       
 28103 ** of the following:
       
 28104 **
       
 28105 **     (1) SHARED_LOCK
       
 28106 **     (2) RESERVED_LOCK
       
 28107 **     (3) PENDING_LOCK
       
 28108 **     (4) EXCLUSIVE_LOCK
       
 28109 **
       
 28110 ** Sometimes when requesting one lock state, additional lock states
       
 28111 ** are inserted in between.  The locking might fail on one of the later
       
 28112 ** transitions leaving the lock state different from what it started but
       
 28113 ** still short of its goal.  The following chart shows the allowed
       
 28114 ** transitions and the inserted intermediate states:
       
 28115 **
       
 28116 **    UNLOCKED -> SHARED
       
 28117 **    SHARED -> RESERVED
       
 28118 **    SHARED -> (PENDING) -> EXCLUSIVE
       
 28119 **    RESERVED -> (PENDING) -> EXCLUSIVE
       
 28120 **    PENDING -> EXCLUSIVE
       
 28121 **
       
 28122 ** This routine will only increase a lock.  The winUnlock() routine
       
 28123 ** erases all locks at once and returns us immediately to locking level 0.
       
 28124 ** It is not possible to lower the locking level one step at a time.  You
       
 28125 ** must go straight to locking level 0.
       
 28126 */
       
 28127 static int winLock(sqlite3_file *id, int locktype){
       
 28128   int rc = SQLITE_OK;    /* Return code from subroutines */
       
 28129   int res = 1;           /* Result of a windows lock call */
       
 28130   int newLocktype;       /* Set pFile->locktype to this value before exiting */
       
 28131   int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
       
 28132   winFile *pFile = (winFile*)id;
       
 28133   DWORD error = NO_ERROR;
       
 28134 
       
 28135   assert( id!=0 );
       
 28136   OSTRACE5("LOCK %d %d was %d(%d)\n",
       
 28137           pFile->h, locktype, pFile->locktype, pFile->sharedLockByte);
       
 28138 
       
 28139   /* If there is already a lock of this type or more restrictive on the
       
 28140   ** OsFile, do nothing. Don't use the end_lock: exit path, as
       
 28141   ** sqlite3OsEnterMutex() hasn't been called yet.
       
 28142   */
       
 28143   if( pFile->locktype>=locktype ){
       
 28144     return SQLITE_OK;
       
 28145   }
       
 28146 
       
 28147   /* Make sure the locking sequence is correct
       
 28148   */
       
 28149   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
       
 28150   assert( locktype!=PENDING_LOCK );
       
 28151   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
       
 28152 
       
 28153   /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
       
 28154   ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
       
 28155   ** the PENDING_LOCK byte is temporary.
       
 28156   */
       
 28157   newLocktype = pFile->locktype;
       
 28158   if(   (pFile->locktype==NO_LOCK)
       
 28159      || (   (locktype==EXCLUSIVE_LOCK)
       
 28160          && (pFile->locktype==RESERVED_LOCK))
       
 28161   ){
       
 28162     int cnt = 3;
       
 28163     while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
       
 28164       /* Try 3 times to get the pending lock.  The pending lock might be
       
 28165       ** held by another reader process who will release it momentarily.
       
 28166       */
       
 28167       OSTRACE2("could not get a PENDING lock. cnt=%d\n", cnt);
       
 28168       Sleep(1);
       
 28169     }
       
 28170     gotPendingLock = res;
       
 28171     if( !res ){
       
 28172       error = GetLastError();
       
 28173     }
       
 28174   }
       
 28175 
       
 28176   /* Acquire a shared lock
       
 28177   */
       
 28178   if( locktype==SHARED_LOCK && res ){
       
 28179     assert( pFile->locktype==NO_LOCK );
       
 28180     res = getReadLock(pFile);
       
 28181     if( res ){
       
 28182       newLocktype = SHARED_LOCK;
       
 28183     }else{
       
 28184       error = GetLastError();
       
 28185     }
       
 28186   }
       
 28187 
       
 28188   /* Acquire a RESERVED lock
       
 28189   */
       
 28190   if( locktype==RESERVED_LOCK && res ){
       
 28191     assert( pFile->locktype==SHARED_LOCK );
       
 28192     res = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
       
 28193     if( res ){
       
 28194       newLocktype = RESERVED_LOCK;
       
 28195     }else{
       
 28196       error = GetLastError();
       
 28197     }
       
 28198   }
       
 28199 
       
 28200   /* Acquire a PENDING lock
       
 28201   */
       
 28202   if( locktype==EXCLUSIVE_LOCK && res ){
       
 28203     newLocktype = PENDING_LOCK;
       
 28204     gotPendingLock = 0;
       
 28205   }
       
 28206 
       
 28207   /* Acquire an EXCLUSIVE lock
       
 28208   */
       
 28209   if( locktype==EXCLUSIVE_LOCK && res ){
       
 28210     assert( pFile->locktype>=SHARED_LOCK );
       
 28211     res = unlockReadLock(pFile);
       
 28212     OSTRACE2("unreadlock = %d\n", res);
       
 28213     res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
       
 28214     if( res ){
       
 28215       newLocktype = EXCLUSIVE_LOCK;
       
 28216     }else{
       
 28217       error = GetLastError();
       
 28218       OSTRACE2("error-code = %d\n", error);
       
 28219       getReadLock(pFile);
       
 28220     }
       
 28221   }
       
 28222 
       
 28223   /* If we are holding a PENDING lock that ought to be released, then
       
 28224   ** release it now.
       
 28225   */
       
 28226   if( gotPendingLock && locktype==SHARED_LOCK ){
       
 28227     UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
       
 28228   }
       
 28229 
       
 28230   /* Update the state of the lock has held in the file descriptor then
       
 28231   ** return the appropriate result code.
       
 28232   */
       
 28233   if( res ){
       
 28234     rc = SQLITE_OK;
       
 28235   }else{
       
 28236     OSTRACE4("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
       
 28237            locktype, newLocktype);
       
 28238     pFile->lastErrno = error;
       
 28239     rc = SQLITE_BUSY;
       
 28240   }
       
 28241   pFile->locktype = (u8)newLocktype;
       
 28242   return rc;
       
 28243 }
       
 28244 
       
 28245 /*
       
 28246 ** This routine checks if there is a RESERVED lock held on the specified
       
 28247 ** file by this or any other process. If such a lock is held, return
       
 28248 ** non-zero, otherwise zero.
       
 28249 */
       
 28250 static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
       
 28251   int rc;
       
 28252   winFile *pFile = (winFile*)id;
       
 28253 
       
 28254   assert( id!=0 );
       
 28255   if( pFile->locktype>=RESERVED_LOCK ){
       
 28256     rc = 1;
       
 28257     OSTRACE3("TEST WR-LOCK %d %d (local)\n", pFile->h, rc);
       
 28258   }else{
       
 28259     rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
       
 28260     if( rc ){
       
 28261       UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
       
 28262     }
       
 28263     rc = !rc;
       
 28264     OSTRACE3("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc);
       
 28265   }
       
 28266   *pResOut = rc;
       
 28267   return SQLITE_OK;
       
 28268 }
       
 28269 
       
 28270 /*
       
 28271 ** Lower the locking level on file descriptor id to locktype.  locktype
       
 28272 ** must be either NO_LOCK or SHARED_LOCK.
       
 28273 **
       
 28274 ** If the locking level of the file descriptor is already at or below
       
 28275 ** the requested locking level, this routine is a no-op.
       
 28276 **
       
 28277 ** It is not possible for this routine to fail if the second argument
       
 28278 ** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
       
 28279 ** might return SQLITE_IOERR;
       
 28280 */
       
 28281 static int winUnlock(sqlite3_file *id, int locktype){
       
 28282   int type;
       
 28283   winFile *pFile = (winFile*)id;
       
 28284   int rc = SQLITE_OK;
       
 28285   assert( pFile!=0 );
       
 28286   assert( locktype<=SHARED_LOCK );
       
 28287   OSTRACE5("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
       
 28288           pFile->locktype, pFile->sharedLockByte);
       
 28289   type = pFile->locktype;
       
 28290   if( type>=EXCLUSIVE_LOCK ){
       
 28291     UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
       
 28292     if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
       
 28293       /* This should never happen.  We should always be able to
       
 28294       ** reacquire the read lock */
       
 28295       rc = SQLITE_IOERR_UNLOCK;
       
 28296     }
       
 28297   }
       
 28298   if( type>=RESERVED_LOCK ){
       
 28299     UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
       
 28300   }
       
 28301   if( locktype==NO_LOCK && type>=SHARED_LOCK ){
       
 28302     unlockReadLock(pFile);
       
 28303   }
       
 28304   if( type>=PENDING_LOCK ){
       
 28305     UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
       
 28306   }
       
 28307   pFile->locktype = (u8)locktype;
       
 28308   return rc;
       
 28309 }
       
 28310 
       
 28311 /*
       
 28312 ** Control and query of the open file handle.
       
 28313 */
       
 28314 static int winFileControl(sqlite3_file *id, int op, void *pArg){
       
 28315   switch( op ){
       
 28316     case SQLITE_FCNTL_LOCKSTATE: {
       
 28317       *(int*)pArg = ((winFile*)id)->locktype;
       
 28318       return SQLITE_OK;
       
 28319     }
       
 28320     case SQLITE_LAST_ERRNO: {
       
 28321       *(int*)pArg = (int)((winFile*)id)->lastErrno;
       
 28322       return SQLITE_OK;
       
 28323     }
       
 28324   }
       
 28325   return SQLITE_ERROR;
       
 28326 }
       
 28327 
       
 28328 /*
       
 28329 ** Return the sector size in bytes of the underlying block device for
       
 28330 ** the specified file. This is almost always 512 bytes, but may be
       
 28331 ** larger for some devices.
       
 28332 **
       
 28333 ** SQLite code assumes this function cannot fail. It also assumes that
       
 28334 ** if two files are created in the same file-system directory (i.e.
       
 28335 ** a database and its journal file) that the sector size will be the
       
 28336 ** same for both.
       
 28337 */
       
 28338 static int winSectorSize(sqlite3_file *id){
       
 28339   assert( id!=0 );
       
 28340   return (int)(((winFile*)id)->sectorSize);
       
 28341 }
       
 28342 
       
 28343 /*
       
 28344 ** Return a vector of device characteristics.
       
 28345 */
       
 28346 static int winDeviceCharacteristics(sqlite3_file *id){
       
 28347   UNUSED_PARAMETER(id);
       
 28348   return 0;
       
 28349 }
       
 28350 
       
 28351 /*
       
 28352 ** This vector defines all the methods that can operate on an
       
 28353 ** sqlite3_file for win32.
       
 28354 */
       
 28355 static const sqlite3_io_methods winIoMethod = {
       
 28356   1,                        /* iVersion */
       
 28357   winClose,
       
 28358   winRead,
       
 28359   winWrite,
       
 28360   winTruncate,
       
 28361   winSync,
       
 28362   winFileSize,
       
 28363   winLock,
       
 28364   winUnlock,
       
 28365   winCheckReservedLock,
       
 28366   winFileControl,
       
 28367   winSectorSize,
       
 28368   winDeviceCharacteristics
       
 28369 };
       
 28370 
       
 28371 /***************************************************************************
       
 28372 ** Here ends the I/O methods that form the sqlite3_io_methods object.
       
 28373 **
       
 28374 ** The next block of code implements the VFS methods.
       
 28375 ****************************************************************************/
       
 28376 
       
 28377 /*
       
 28378 ** Convert a UTF-8 filename into whatever form the underlying
       
 28379 ** operating system wants filenames in.  Space to hold the result
       
 28380 ** is obtained from malloc and must be freed by the calling
       
 28381 ** function.
       
 28382 */
       
 28383 static void *convertUtf8Filename(const char *zFilename){
       
 28384   void *zConverted = 0;
       
 28385   if( isNT() ){
       
 28386     zConverted = utf8ToUnicode(zFilename);
       
 28387 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
       
 28388 */
       
 28389 #if SQLITE_OS_WINCE==0
       
 28390   }else{
       
 28391     zConverted = utf8ToMbcs(zFilename);
       
 28392 #endif
       
 28393   }
       
 28394   /* caller will handle out of memory */
       
 28395   return zConverted;
       
 28396 }
       
 28397 
       
 28398 /*
       
 28399 ** Create a temporary file name in zBuf.  zBuf must be big enough to
       
 28400 ** hold at pVfs->mxPathname characters.
       
 28401 */
       
 28402 static int getTempname(int nBuf, char *zBuf){
       
 28403   static char zChars[] =
       
 28404     "abcdefghijklmnopqrstuvwxyz"
       
 28405     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
       
 28406     "0123456789";
       
 28407   size_t i, j;
       
 28408   char zTempPath[MAX_PATH+1];
       
 28409   if( sqlite3_temp_directory ){
       
 28410     sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
       
 28411   }else if( isNT() ){
       
 28412     char *zMulti;
       
 28413     WCHAR zWidePath[MAX_PATH];
       
 28414     GetTempPathW(MAX_PATH-30, zWidePath);
       
 28415     zMulti = unicodeToUtf8(zWidePath);
       
 28416     if( zMulti ){
       
 28417       sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
       
 28418       free(zMulti);
       
 28419     }else{
       
 28420       return SQLITE_NOMEM;
       
 28421     }
       
 28422 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
       
 28423 ** Since the ASCII version of these Windows API do not exist for WINCE,
       
 28424 ** it's important to not reference them for WINCE builds.
       
 28425 */
       
 28426 #if SQLITE_OS_WINCE==0
       
 28427   }else{
       
 28428     char *zUtf8;
       
 28429     char zMbcsPath[MAX_PATH];
       
 28430     GetTempPathA(MAX_PATH-30, zMbcsPath);
       
 28431     zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
       
 28432     if( zUtf8 ){
       
 28433       sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
       
 28434       free(zUtf8);
       
 28435     }else{
       
 28436       return SQLITE_NOMEM;
       
 28437     }
       
 28438 #endif
       
 28439   }
       
 28440   for(i=sqlite3Strlen30(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
       
 28441   zTempPath[i] = 0;
       
 28442   sqlite3_snprintf(nBuf-30, zBuf,
       
 28443                    "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
       
 28444   j = sqlite3Strlen30(zBuf);
       
 28445   sqlite3_randomness(20, &zBuf[j]);
       
 28446   for(i=0; i<20; i++, j++){
       
 28447     zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
       
 28448   }
       
 28449   zBuf[j] = 0;
       
 28450   OSTRACE2("TEMP FILENAME: %s\n", zBuf);
       
 28451   return SQLITE_OK; 
       
 28452 }
       
 28453 
       
 28454 /*
       
 28455 ** The return value of getLastErrorMsg
       
 28456 ** is zero if the error message fits in the buffer, or non-zero
       
 28457 ** otherwise (if the message was truncated).
       
 28458 */
       
 28459 static int getLastErrorMsg(int nBuf, char *zBuf){
       
 28460   DWORD error = GetLastError();
       
 28461 
       
 28462 #if SQLITE_OS_WINCE
       
 28463   sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", error, error);
       
 28464 #else
       
 28465   /* FormatMessage returns 0 on failure.  Otherwise it
       
 28466   ** returns the number of TCHARs written to the output
       
 28467   ** buffer, excluding the terminating null char.
       
 28468   */
       
 28469   if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
       
 28470                       NULL,
       
 28471                       error,
       
 28472                       0,
       
 28473                       zBuf,
       
 28474                       nBuf-1,
       
 28475                       0))
       
 28476   {
       
 28477     sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", error, error);
       
 28478   }
       
 28479 #endif
       
 28480 
       
 28481   return 0;
       
 28482 }
       
 28483 
       
 28484 /*
       
 28485 ** Open a file.
       
 28486 */
       
 28487 static int winOpen(
       
 28488   sqlite3_vfs *pVfs,        /* Not used */
       
 28489   const char *zName,        /* Name of the file (UTF-8) */
       
 28490   sqlite3_file *id,         /* Write the SQLite file handle here */
       
 28491   int flags,                /* Open mode flags */
       
 28492   int *pOutFlags            /* Status return flags */
       
 28493 ){
       
 28494   HANDLE h;
       
 28495   DWORD dwDesiredAccess;
       
 28496   DWORD dwShareMode;
       
 28497   DWORD dwCreationDisposition;
       
 28498   DWORD dwFlagsAndAttributes = 0;
       
 28499 #if SQLITE_OS_WINCE
       
 28500   int isTemp = 0;
       
 28501 #endif
       
 28502   winFile *pFile = (winFile*)id;
       
 28503   void *zConverted;                 /* Filename in OS encoding */
       
 28504   const char *zUtf8Name = zName;    /* Filename in UTF-8 encoding */
       
 28505   char zTmpname[MAX_PATH+1];        /* Buffer used to create temp filename */
       
 28506 
       
 28507   assert( id!=0 );
       
 28508   UNUSED_PARAMETER(pVfs);
       
 28509 
       
 28510   /* If the second argument to this function is NULL, generate a 
       
 28511   ** temporary file name to use 
       
 28512   */
       
 28513   if( !zUtf8Name ){
       
 28514     int rc = getTempname(MAX_PATH+1, zTmpname);
       
 28515     if( rc!=SQLITE_OK ){
       
 28516       return rc;
       
 28517     }
       
 28518     zUtf8Name = zTmpname;
       
 28519   }
       
 28520 
       
 28521   /* Convert the filename to the system encoding. */
       
 28522   zConverted = convertUtf8Filename(zUtf8Name);
       
 28523   if( zConverted==0 ){
       
 28524     return SQLITE_NOMEM;
       
 28525   }
       
 28526 
       
 28527   if( flags & SQLITE_OPEN_READWRITE ){
       
 28528     dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
       
 28529   }else{
       
 28530     dwDesiredAccess = GENERIC_READ;
       
 28531   }
       
 28532   /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
       
 28533   ** created. SQLite doesn't use it to indicate "exclusive access" 
       
 28534   ** as it is usually understood.
       
 28535   */
       
 28536   assert(!(flags & SQLITE_OPEN_EXCLUSIVE) || (flags & SQLITE_OPEN_CREATE));
       
 28537   if( flags & SQLITE_OPEN_EXCLUSIVE ){
       
 28538     /* Creates a new file, only if it does not already exist. */
       
 28539     /* If the file exists, it fails. */
       
 28540     dwCreationDisposition = CREATE_NEW;
       
 28541   }else if( flags & SQLITE_OPEN_CREATE ){
       
 28542     /* Open existing file, or create if it doesn't exist */
       
 28543     dwCreationDisposition = OPEN_ALWAYS;
       
 28544   }else{
       
 28545     /* Opens a file, only if it exists. */
       
 28546     dwCreationDisposition = OPEN_EXISTING;
       
 28547   }
       
 28548   dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
       
 28549   if( flags & SQLITE_OPEN_DELETEONCLOSE ){
       
 28550 #if SQLITE_OS_WINCE
       
 28551     dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
       
 28552     isTemp = 1;
       
 28553 #else
       
 28554     dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
       
 28555                                | FILE_ATTRIBUTE_HIDDEN
       
 28556                                | FILE_FLAG_DELETE_ON_CLOSE;
       
 28557 #endif
       
 28558   }else{
       
 28559     dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
       
 28560   }
       
 28561   /* Reports from the internet are that performance is always
       
 28562   ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
       
 28563 #if SQLITE_OS_WINCE
       
 28564   dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
       
 28565 #endif
       
 28566   if( isNT() ){
       
 28567     h = CreateFileW((WCHAR*)zConverted,
       
 28568        dwDesiredAccess,
       
 28569        dwShareMode,
       
 28570        NULL,
       
 28571        dwCreationDisposition,
       
 28572        dwFlagsAndAttributes,
       
 28573        NULL
       
 28574     );
       
 28575 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
       
 28576 ** Since the ASCII version of these Windows API do not exist for WINCE,
       
 28577 ** it's important to not reference them for WINCE builds.
       
 28578 */
       
 28579 #if SQLITE_OS_WINCE==0
       
 28580   }else{
       
 28581     h = CreateFileA((char*)zConverted,
       
 28582        dwDesiredAccess,
       
 28583        dwShareMode,
       
 28584        NULL,
       
 28585        dwCreationDisposition,
       
 28586        dwFlagsAndAttributes,
       
 28587        NULL
       
 28588     );
       
 28589 #endif
       
 28590   }
       
 28591   if( h==INVALID_HANDLE_VALUE ){
       
 28592     free(zConverted);
       
 28593     if( flags & SQLITE_OPEN_READWRITE ){
       
 28594       return winOpen(pVfs, zName, id, 
       
 28595              ((flags|SQLITE_OPEN_READONLY)&~SQLITE_OPEN_READWRITE), pOutFlags);
       
 28596     }else{
       
 28597       return SQLITE_CANTOPEN;
       
 28598     }
       
 28599   }
       
 28600   if( pOutFlags ){
       
 28601     if( flags & SQLITE_OPEN_READWRITE ){
       
 28602       *pOutFlags = SQLITE_OPEN_READWRITE;
       
 28603     }else{
       
 28604       *pOutFlags = SQLITE_OPEN_READONLY;
       
 28605     }
       
 28606   }
       
 28607   memset(pFile, 0, sizeof(*pFile));
       
 28608   pFile->pMethod = &winIoMethod;
       
 28609   pFile->h = h;
       
 28610   pFile->lastErrno = NO_ERROR;
       
 28611   pFile->sectorSize = getSectorSize(pVfs, zUtf8Name);
       
 28612 #if SQLITE_OS_WINCE
       
 28613   if( (flags & (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)) ==
       
 28614                (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)
       
 28615        && !winceCreateLock(zName, pFile)
       
 28616   ){
       
 28617     CloseHandle(h);
       
 28618     free(zConverted);
       
 28619     return SQLITE_CANTOPEN;
       
 28620   }
       
 28621   if( isTemp ){
       
 28622     pFile->zDeleteOnClose = zConverted;
       
 28623   }else
       
 28624 #endif
       
 28625   {
       
 28626     free(zConverted);
       
 28627   }
       
 28628   OpenCounter(+1);
       
 28629   return SQLITE_OK;
       
 28630 }
       
 28631 
       
 28632 /*
       
 28633 ** Delete the named file.
       
 28634 **
       
 28635 ** Note that windows does not allow a file to be deleted if some other
       
 28636 ** process has it open.  Sometimes a virus scanner or indexing program
       
 28637 ** will open a journal file shortly after it is created in order to do
       
 28638 ** whatever it does.  While this other process is holding the
       
 28639 ** file open, we will be unable to delete it.  To work around this
       
 28640 ** problem, we delay 100 milliseconds and try to delete again.  Up
       
 28641 ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
       
 28642 ** up and returning an error.
       
 28643 */
       
 28644 #define MX_DELETION_ATTEMPTS 5
       
 28645 static int winDelete(
       
 28646   sqlite3_vfs *pVfs,          /* Not used on win32 */
       
 28647   const char *zFilename,      /* Name of file to delete */
       
 28648   int syncDir                 /* Not used on win32 */
       
 28649 ){
       
 28650   int cnt = 0;
       
 28651   DWORD rc;
       
 28652   DWORD error = 0;
       
 28653   void *zConverted = convertUtf8Filename(zFilename);
       
 28654   UNUSED_PARAMETER(pVfs);
       
 28655   UNUSED_PARAMETER(syncDir);
       
 28656   if( zConverted==0 ){
       
 28657     return SQLITE_NOMEM;
       
 28658   }
       
 28659   SimulateIOError(return SQLITE_IOERR_DELETE);
       
 28660   if( isNT() ){
       
 28661     do{
       
 28662       DeleteFileW(zConverted);
       
 28663     }while(   (   ((rc = GetFileAttributesW(zConverted)) != INVALID_FILE_ATTRIBUTES)
       
 28664                || ((error = GetLastError()) == ERROR_ACCESS_DENIED))
       
 28665            && (++cnt < MX_DELETION_ATTEMPTS)
       
 28666            && (Sleep(100), 1) );
       
 28667 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
       
 28668 ** Since the ASCII version of these Windows API do not exist for WINCE,
       
 28669 ** it's important to not reference them for WINCE builds.
       
 28670 */
       
 28671 #if SQLITE_OS_WINCE==0
       
 28672   }else{
       
 28673     do{
       
 28674       DeleteFileA(zConverted);
       
 28675     }while(   (   ((rc = GetFileAttributesA(zConverted)) != INVALID_FILE_ATTRIBUTES)
       
 28676                || ((error = GetLastError()) == ERROR_ACCESS_DENIED))
       
 28677            && (++cnt < MX_DELETION_ATTEMPTS)
       
 28678            && (Sleep(100), 1) );
       
 28679 #endif
       
 28680   }
       
 28681   free(zConverted);
       
 28682   OSTRACE2("DELETE \"%s\"\n", zFilename);
       
 28683   return (   (rc == INVALID_FILE_ATTRIBUTES) 
       
 28684           && (error == ERROR_FILE_NOT_FOUND)) ? SQLITE_OK : SQLITE_IOERR_DELETE;
       
 28685 }
       
 28686 
       
 28687 /*
       
 28688 ** Check the existance and status of a file.
       
 28689 */
       
 28690 static int winAccess(
       
 28691   sqlite3_vfs *pVfs,         /* Not used on win32 */
       
 28692   const char *zFilename,     /* Name of file to check */
       
 28693   int flags,                 /* Type of test to make on this file */
       
 28694   int *pResOut               /* OUT: Result */
       
 28695 ){
       
 28696   DWORD attr;
       
 28697   int rc = 0;
       
 28698   void *zConverted = convertUtf8Filename(zFilename);
       
 28699   UNUSED_PARAMETER(pVfs);
       
 28700   if( zConverted==0 ){
       
 28701     return SQLITE_NOMEM;
       
 28702   }
       
 28703   if( isNT() ){
       
 28704     attr = GetFileAttributesW((WCHAR*)zConverted);
       
 28705 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
       
 28706 ** Since the ASCII version of these Windows API do not exist for WINCE,
       
 28707 ** it's important to not reference them for WINCE builds.
       
 28708 */
       
 28709 #if SQLITE_OS_WINCE==0
       
 28710   }else{
       
 28711     attr = GetFileAttributesA((char*)zConverted);
       
 28712 #endif
       
 28713   }
       
 28714   free(zConverted);
       
 28715   switch( flags ){
       
 28716     case SQLITE_ACCESS_READ:
       
 28717     case SQLITE_ACCESS_EXISTS:
       
 28718       rc = attr!=INVALID_FILE_ATTRIBUTES;
       
 28719       break;
       
 28720     case SQLITE_ACCESS_READWRITE:
       
 28721       rc = (attr & FILE_ATTRIBUTE_READONLY)==0;
       
 28722       break;
       
 28723     default:
       
 28724       assert(!"Invalid flags argument");
       
 28725   }
       
 28726   *pResOut = rc;
       
 28727   return SQLITE_OK;
       
 28728 }
       
 28729 
       
 28730 
       
 28731 /*
       
 28732 ** Turn a relative pathname into a full pathname.  Write the full
       
 28733 ** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
       
 28734 ** bytes in size.
       
 28735 */
       
 28736 static int winFullPathname(
       
 28737   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
       
 28738   const char *zRelative,        /* Possibly relative input path */
       
 28739   int nFull,                    /* Size of output buffer in bytes */
       
 28740   char *zFull                   /* Output buffer */
       
 28741 ){
       
 28742   
       
 28743 #if defined(__CYGWIN__)
       
 28744   UNUSED_PARAMETER(nFull);
       
 28745   cygwin_conv_to_full_win32_path(zRelative, zFull);
       
 28746   return SQLITE_OK;
       
 28747 #endif
       
 28748 
       
 28749 #if SQLITE_OS_WINCE
       
 28750   UNUSED_PARAMETER(nFull);
       
 28751   /* WinCE has no concept of a relative pathname, or so I am told. */
       
 28752   sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zRelative);
       
 28753   return SQLITE_OK;
       
 28754 #endif
       
 28755 
       
 28756 #if !SQLITE_OS_WINCE && !defined(__CYGWIN__)
       
 28757   int nByte;
       
 28758   void *zConverted;
       
 28759   char *zOut;
       
 28760   UNUSED_PARAMETER(nFull);
       
 28761   zConverted = convertUtf8Filename(zRelative);
       
 28762   if( isNT() ){
       
 28763     WCHAR *zTemp;
       
 28764     nByte = GetFullPathNameW((WCHAR*)zConverted, 0, 0, 0) + 3;
       
 28765     zTemp = malloc( nByte*sizeof(zTemp[0]) );
       
 28766     if( zTemp==0 ){
       
 28767       free(zConverted);
       
 28768       return SQLITE_NOMEM;
       
 28769     }
       
 28770     GetFullPathNameW((WCHAR*)zConverted, nByte, zTemp, 0);
       
 28771     free(zConverted);
       
 28772     zOut = unicodeToUtf8(zTemp);
       
 28773     free(zTemp);
       
 28774 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
       
 28775 ** Since the ASCII version of these Windows API do not exist for WINCE,
       
 28776 ** it's important to not reference them for WINCE builds.
       
 28777 */
       
 28778 #if SQLITE_OS_WINCE==0
       
 28779   }else{
       
 28780     char *zTemp;
       
 28781     nByte = GetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
       
 28782     zTemp = malloc( nByte*sizeof(zTemp[0]) );
       
 28783     if( zTemp==0 ){
       
 28784       free(zConverted);
       
 28785       return SQLITE_NOMEM;
       
 28786     }
       
 28787     GetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
       
 28788     free(zConverted);
       
 28789     zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
       
 28790     free(zTemp);
       
 28791 #endif
       
 28792   }
       
 28793   if( zOut ){
       
 28794     sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
       
 28795     free(zOut);
       
 28796     return SQLITE_OK;
       
 28797   }else{
       
 28798     return SQLITE_NOMEM;
       
 28799   }
       
 28800 #endif
       
 28801 }
       
 28802 
       
 28803 /*
       
 28804 ** Get the sector size of the device used to store
       
 28805 ** file.
       
 28806 */
       
 28807 static int getSectorSize(
       
 28808     sqlite3_vfs *pVfs,
       
 28809     const char *zRelative     /* UTF-8 file name */
       
 28810 ){
       
 28811   DWORD bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
       
 28812   /* GetDiskFreeSpace is not supported under WINCE */
       
 28813 #if SQLITE_OS_WINCE
       
 28814   UNUSED_PARAMETER(pVfs);
       
 28815   UNUSED_PARAMETER(zRelative);
       
 28816 #else
       
 28817   char zFullpath[MAX_PATH+1];
       
 28818   int rc;
       
 28819   DWORD dwRet = 0;
       
 28820   DWORD dwDummy;
       
 28821 
       
 28822   /*
       
 28823   ** We need to get the full path name of the file
       
 28824   ** to get the drive letter to look up the sector
       
 28825   ** size.
       
 28826   */
       
 28827   rc = winFullPathname(pVfs, zRelative, MAX_PATH, zFullpath);
       
 28828   if( rc == SQLITE_OK )
       
 28829   {
       
 28830     void *zConverted = convertUtf8Filename(zFullpath);
       
 28831     if( zConverted ){
       
 28832       if( isNT() ){
       
 28833         /* trim path to just drive reference */
       
 28834         WCHAR *p = zConverted;
       
 28835         for(;*p;p++){
       
 28836           if( *p == '\\' ){
       
 28837             *p = '\0';
       
 28838             break;
       
 28839           }
       
 28840         }
       
 28841         dwRet = GetDiskFreeSpaceW((WCHAR*)zConverted,
       
 28842                                   &dwDummy,
       
 28843                                   &bytesPerSector,
       
 28844                                   &dwDummy,
       
 28845                                   &dwDummy);
       
 28846       }else{
       
 28847         /* trim path to just drive reference */
       
 28848         CHAR *p = (CHAR *)zConverted;
       
 28849         for(;*p;p++){
       
 28850           if( *p == '\\' ){
       
 28851             *p = '\0';
       
 28852             break;
       
 28853           }
       
 28854         }
       
 28855         dwRet = GetDiskFreeSpaceA((CHAR*)zConverted,
       
 28856                                   &dwDummy,
       
 28857                                   &bytesPerSector,
       
 28858                                   &dwDummy,
       
 28859                                   &dwDummy);
       
 28860       }
       
 28861       free(zConverted);
       
 28862     }
       
 28863     if( !dwRet ){
       
 28864       bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
       
 28865     }
       
 28866   }
       
 28867 #endif
       
 28868   return (int) bytesPerSector; 
       
 28869 }
       
 28870 
       
 28871 #ifndef SQLITE_OMIT_LOAD_EXTENSION
       
 28872 /*
       
 28873 ** Interfaces for opening a shared library, finding entry points
       
 28874 ** within the shared library, and closing the shared library.
       
 28875 */
       
 28876 /*
       
 28877 ** Interfaces for opening a shared library, finding entry points
       
 28878 ** within the shared library, and closing the shared library.
       
 28879 */
       
 28880 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
       
 28881   HANDLE h;
       
 28882   void *zConverted = convertUtf8Filename(zFilename);
       
 28883   UNUSED_PARAMETER(pVfs);
       
 28884   if( zConverted==0 ){
       
 28885     return 0;
       
 28886   }
       
 28887   if( isNT() ){
       
 28888     h = LoadLibraryW((WCHAR*)zConverted);
       
 28889 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
       
 28890 ** Since the ASCII version of these Windows API do not exist for WINCE,
       
 28891 ** it's important to not reference them for WINCE builds.
       
 28892 */
       
 28893 #if SQLITE_OS_WINCE==0
       
 28894   }else{
       
 28895     h = LoadLibraryA((char*)zConverted);
       
 28896 #endif
       
 28897   }
       
 28898   free(zConverted);
       
 28899   return (void*)h;
       
 28900 }
       
 28901 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
       
 28902   UNUSED_PARAMETER(pVfs);
       
 28903   getLastErrorMsg(nBuf, zBufOut);
       
 28904 }
       
 28905 void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
       
 28906   UNUSED_PARAMETER(pVfs);
       
 28907 #if SQLITE_OS_WINCE
       
 28908   /* The GetProcAddressA() routine is only available on wince. */
       
 28909   return (void(*)(void))GetProcAddressA((HANDLE)pHandle, zSymbol);
       
 28910 #else
       
 28911   /* All other windows platforms expect GetProcAddress() to take
       
 28912   ** an Ansi string regardless of the _UNICODE setting */
       
 28913   return (void(*)(void))GetProcAddress((HANDLE)pHandle, zSymbol);
       
 28914 #endif
       
 28915 }
       
 28916 void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
       
 28917   UNUSED_PARAMETER(pVfs);
       
 28918   FreeLibrary((HANDLE)pHandle);
       
 28919 }
       
 28920 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
       
 28921   #define winDlOpen  0
       
 28922   #define winDlError 0
       
 28923   #define winDlSym   0
       
 28924   #define winDlClose 0
       
 28925 #endif
       
 28926 
       
 28927 
       
 28928 /*
       
 28929 ** Write up to nBuf bytes of randomness into zBuf.
       
 28930 */
       
 28931 static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
       
 28932   int n = 0;
       
 28933   UNUSED_PARAMETER(pVfs);
       
 28934 #if defined(SQLITE_TEST)
       
 28935   n = nBuf;
       
 28936   memset(zBuf, 0, nBuf);
       
 28937 #else
       
 28938   if( sizeof(SYSTEMTIME)<=nBuf-n ){
       
 28939     SYSTEMTIME x;
       
 28940     GetSystemTime(&x);
       
 28941     memcpy(&zBuf[n], &x, sizeof(x));
       
 28942     n += sizeof(x);
       
 28943   }
       
 28944   if( sizeof(DWORD)<=nBuf-n ){
       
 28945     DWORD pid = GetCurrentProcessId();
       
 28946     memcpy(&zBuf[n], &pid, sizeof(pid));
       
 28947     n += sizeof(pid);
       
 28948   }
       
 28949   if( sizeof(DWORD)<=nBuf-n ){
       
 28950     DWORD cnt = GetTickCount();
       
 28951     memcpy(&zBuf[n], &cnt, sizeof(cnt));
       
 28952     n += sizeof(cnt);
       
 28953   }
       
 28954   if( sizeof(LARGE_INTEGER)<=nBuf-n ){
       
 28955     LARGE_INTEGER i;
       
 28956     QueryPerformanceCounter(&i);
       
 28957     memcpy(&zBuf[n], &i, sizeof(i));
       
 28958     n += sizeof(i);
       
 28959   }
       
 28960 #endif
       
 28961   return n;
       
 28962 }
       
 28963 
       
 28964 
       
 28965 /*
       
 28966 ** Sleep for a little while.  Return the amount of time slept.
       
 28967 */
       
 28968 static int winSleep(sqlite3_vfs *pVfs, int microsec){
       
 28969   Sleep((microsec+999)/1000);
       
 28970   UNUSED_PARAMETER(pVfs);
       
 28971   return ((microsec+999)/1000)*1000;
       
 28972 }
       
 28973 
       
 28974 /*
       
 28975 ** The following variable, if set to a non-zero value, becomes the result
       
 28976 ** returned from sqlite3OsCurrentTime().  This is used for testing.
       
 28977 */
       
 28978 #ifdef SQLITE_TEST
       
 28979 SQLITE_API int sqlite3_current_time = 0;
       
 28980 #endif
       
 28981 
       
 28982 /*
       
 28983 ** Find the current time (in Universal Coordinated Time).  Write the
       
 28984 ** current time and date as a Julian Day number into *prNow and
       
 28985 ** return 0.  Return 1 if the time and date cannot be found.
       
 28986 */
       
 28987 int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
       
 28988   FILETIME ft;
       
 28989   /* FILETIME structure is a 64-bit value representing the number of 
       
 28990      100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). 
       
 28991   */
       
 28992   sqlite3_int64 timeW;   /* Whole days */
       
 28993   sqlite3_int64 timeF;   /* Fractional Days */
       
 28994 
       
 28995   /* Number of 100-nanosecond intervals in a single day */
       
 28996   static const sqlite3_int64 ntuPerDay = 
       
 28997       10000000*(sqlite3_int64)86400;
       
 28998 
       
 28999   /* Number of 100-nanosecond intervals in half of a day */
       
 29000   static const sqlite3_int64 ntuPerHalfDay = 
       
 29001       10000000*(sqlite3_int64)43200;
       
 29002 
       
 29003   /* 2^32 - to avoid use of LL and warnings in gcc */
       
 29004   static const sqlite3_int64 max32BitValue = 
       
 29005       (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
       
 29006 
       
 29007 #if SQLITE_OS_WINCE
       
 29008   SYSTEMTIME time;
       
 29009   GetSystemTime(&time);
       
 29010   /* if SystemTimeToFileTime() fails, it returns zero. */
       
 29011   if (!SystemTimeToFileTime(&time,&ft)){
       
 29012     return 1;
       
 29013   }
       
 29014 #else
       
 29015   GetSystemTimeAsFileTime( &ft );
       
 29016 #endif
       
 29017   UNUSED_PARAMETER(pVfs);
       
 29018   timeW = (((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + (sqlite3_int64)ft.dwLowDateTime;
       
 29019   timeF = timeW % ntuPerDay;          /* fractional days (100-nanoseconds) */
       
 29020   timeW = timeW / ntuPerDay;          /* whole days */
       
 29021   timeW = timeW + 2305813;            /* add whole days (from 2305813.5) */
       
 29022   timeF = timeF + ntuPerHalfDay;      /* add half a day (from 2305813.5) */
       
 29023   timeW = timeW + (timeF/ntuPerDay);  /* add whole day if half day made one */
       
 29024   timeF = timeF % ntuPerDay;          /* compute new fractional days */
       
 29025   *prNow = (double)timeW + ((double)timeF / (double)ntuPerDay);
       
 29026 #ifdef SQLITE_TEST
       
 29027   if( sqlite3_current_time ){
       
 29028     *prNow = ((double)sqlite3_current_time + (double)43200) / (double)86400 + (double)2440587;
       
 29029   }
       
 29030 #endif
       
 29031   return 0;
       
 29032 }
       
 29033 
       
 29034 /*
       
 29035 ** The idea is that this function works like a combination of
       
 29036 ** GetLastError() and FormatMessage() on windows (or errno and
       
 29037 ** strerror_r() on unix). After an error is returned by an OS
       
 29038 ** function, SQLite calls this function with zBuf pointing to
       
 29039 ** a buffer of nBuf bytes. The OS layer should populate the
       
 29040 ** buffer with a nul-terminated UTF-8 encoded error message
       
 29041 ** describing the last IO error to have occurred within the calling
       
 29042 ** thread.
       
 29043 **
       
 29044 ** If the error message is too large for the supplied buffer,
       
 29045 ** it should be truncated. The return value of xGetLastError
       
 29046 ** is zero if the error message fits in the buffer, or non-zero
       
 29047 ** otherwise (if the message was truncated). If non-zero is returned,
       
 29048 ** then it is not necessary to include the nul-terminator character
       
 29049 ** in the output buffer.
       
 29050 **
       
 29051 ** Not supplying an error message will have no adverse effect
       
 29052 ** on SQLite. It is fine to have an implementation that never
       
 29053 ** returns an error message:
       
 29054 **
       
 29055 **   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
       
 29056 **     assert(zBuf[0]=='\0');
       
 29057 **     return 0;
       
 29058 **   }
       
 29059 **
       
 29060 ** However if an error message is supplied, it will be incorporated
       
 29061 ** by sqlite into the error message available to the user using
       
 29062 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
       
 29063 */
       
 29064 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
       
 29065   UNUSED_PARAMETER(pVfs);
       
 29066   return getLastErrorMsg(nBuf, zBuf);
       
 29067 }
       
 29068 
       
 29069 /*
       
 29070 ** Initialize and deinitialize the operating system interface.
       
 29071 */
       
 29072 SQLITE_API int sqlite3_os_init(void){
       
 29073   static sqlite3_vfs winVfs = {
       
 29074     1,                 /* iVersion */
       
 29075     sizeof(winFile),   /* szOsFile */
       
 29076     MAX_PATH,          /* mxPathname */
       
 29077     0,                 /* pNext */
       
 29078     "win32",           /* zName */
       
 29079     0,                 /* pAppData */
       
 29080  
       
 29081     winOpen,           /* xOpen */
       
 29082     winDelete,         /* xDelete */
       
 29083     winAccess,         /* xAccess */
       
 29084     winFullPathname,   /* xFullPathname */
       
 29085     winDlOpen,         /* xDlOpen */
       
 29086     winDlError,        /* xDlError */
       
 29087     winDlSym,          /* xDlSym */
       
 29088     winDlClose,        /* xDlClose */
       
 29089     winRandomness,     /* xRandomness */
       
 29090     winSleep,          /* xSleep */
       
 29091     winCurrentTime,    /* xCurrentTime */
       
 29092     winGetLastError    /* xGetLastError */
       
 29093   };
       
 29094 
       
 29095   sqlite3_vfs_register(&winVfs, 1);
       
 29096   return SQLITE_OK; 
       
 29097 }
       
 29098 SQLITE_API int sqlite3_os_end(void){ 
       
 29099   return SQLITE_OK;
       
 29100 }
       
 29101 
       
 29102 #endif /* SQLITE_OS_WIN */
       
 29103 
       
 29104 /************** End of os_win.c **********************************************/
       
 29105 /************** Begin file bitvec.c ******************************************/
       
 29106 /*
       
 29107 ** 2008 February 16
       
 29108 **
       
 29109 ** The author disclaims copyright to this source code.  In place of
       
 29110 ** a legal notice, here is a blessing:
       
 29111 **
       
 29112 **    May you do good and not evil.
       
 29113 **    May you find forgiveness for yourself and forgive others.
       
 29114 **    May you share freely, never taking more than you give.
       
 29115 **
       
 29116 *************************************************************************
       
 29117 ** This file implements an object that represents a fixed-length
       
 29118 ** bitmap.  Bits are numbered starting with 1.
       
 29119 **
       
 29120 ** A bitmap is used to record which pages of a database file have been
       
 29121 ** journalled during a transaction, or which pages have the "dont-write"
       
 29122 ** property.  Usually only a few pages are meet either condition.
       
 29123 ** So the bitmap is usually sparse and has low cardinality.
       
 29124 ** But sometimes (for example when during a DROP of a large table) most
       
 29125 ** or all of the pages in a database can get journalled.  In those cases, 
       
 29126 ** the bitmap becomes dense with high cardinality.  The algorithm needs 
       
 29127 ** to handle both cases well.
       
 29128 **
       
 29129 ** The size of the bitmap is fixed when the object is created.
       
 29130 **
       
 29131 ** All bits are clear when the bitmap is created.  Individual bits
       
 29132 ** may be set or cleared one at a time.
       
 29133 **
       
 29134 ** Test operations are about 100 times more common that set operations.
       
 29135 ** Clear operations are exceedingly rare.  There are usually between
       
 29136 ** 5 and 500 set operations per Bitvec object, though the number of sets can
       
 29137 ** sometimes grow into tens of thousands or larger.  The size of the
       
 29138 ** Bitvec object is the number of pages in the database file at the
       
 29139 ** start of a transaction, and is thus usually less than a few thousand,
       
 29140 ** but can be as large as 2 billion for a really big database.
       
 29141 **
       
 29142 ** @(#) $Id: bitvec.c,v 1.17 2009/07/25 17:33:26 drh Exp $
       
 29143 */
       
 29144 
       
 29145 /* Size of the Bitvec structure in bytes. */
       
 29146 #define BITVEC_SZ        (sizeof(void*)*128)  /* 512 on 32bit.  1024 on 64bit */
       
 29147 
       
 29148 /* Round the union size down to the nearest pointer boundary, since that's how 
       
 29149 ** it will be aligned within the Bitvec struct. */
       
 29150 #define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
       
 29151 
       
 29152 /* Type of the array "element" for the bitmap representation. 
       
 29153 ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. 
       
 29154 ** Setting this to the "natural word" size of your CPU may improve
       
 29155 ** performance. */
       
 29156 #define BITVEC_TELEM     u8
       
 29157 /* Size, in bits, of the bitmap element. */
       
 29158 #define BITVEC_SZELEM    8
       
 29159 /* Number of elements in a bitmap array. */
       
 29160 #define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
       
 29161 /* Number of bits in the bitmap array. */
       
 29162 #define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
       
 29163 
       
 29164 /* Number of u32 values in hash table. */
       
 29165 #define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
       
 29166 /* Maximum number of entries in hash table before 
       
 29167 ** sub-dividing and re-hashing. */
       
 29168 #define BITVEC_MXHASH    (BITVEC_NINT/2)
       
 29169 /* Hashing function for the aHash representation.
       
 29170 ** Empirical testing showed that the *37 multiplier 
       
 29171 ** (an arbitrary prime)in the hash function provided 
       
 29172 ** no fewer collisions than the no-op *1. */
       
 29173 #define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
       
 29174 
       
 29175 #define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
       
 29176 
       
 29177 
       
 29178 /*
       
 29179 ** A bitmap is an instance of the following structure.
       
 29180 **
       
 29181 ** This bitmap records the existance of zero or more bits
       
 29182 ** with values between 1 and iSize, inclusive.
       
 29183 **
       
 29184 ** There are three possible representations of the bitmap.
       
 29185 ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
       
 29186 ** bitmap.  The least significant bit is bit 1.
       
 29187 **
       
 29188 ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
       
 29189 ** a hash table that will hold up to BITVEC_MXHASH distinct values.
       
 29190 **
       
 29191 ** Otherwise, the value i is redirected into one of BITVEC_NPTR
       
 29192 ** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
       
 29193 ** handles up to iDivisor separate values of i.  apSub[0] holds
       
 29194 ** values between 1 and iDivisor.  apSub[1] holds values between
       
 29195 ** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
       
 29196 ** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
       
 29197 ** to hold deal with values between 1 and iDivisor.
       
 29198 */
       
 29199 struct Bitvec {
       
 29200   u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
       
 29201   u32 nSet;       /* Number of bits that are set - only valid for aHash
       
 29202                   ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
       
 29203                   ** this would be 125. */
       
 29204   u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
       
 29205                   /* Should >=0 for apSub element. */
       
 29206                   /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
       
 29207                   /* For a BITVEC_SZ of 512, this would be 34,359,739. */
       
 29208   union {
       
 29209     BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
       
 29210     u32 aHash[BITVEC_NINT];      /* Hash table representation */
       
 29211     Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
       
 29212   } u;
       
 29213 };
       
 29214 
       
 29215 /*
       
 29216 ** Create a new bitmap object able to handle bits between 0 and iSize,
       
 29217 ** inclusive.  Return a pointer to the new object.  Return NULL if 
       
 29218 ** malloc fails.
       
 29219 */
       
 29220 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
       
 29221   Bitvec *p;
       
 29222   assert( sizeof(*p)==BITVEC_SZ );
       
 29223   p = sqlite3MallocZero( sizeof(*p) );
       
 29224   if( p ){
       
 29225     p->iSize = iSize;
       
 29226   }
       
 29227   return p;
       
 29228 }
       
 29229 
       
 29230 /*
       
 29231 ** Check to see if the i-th bit is set.  Return true or false.
       
 29232 ** If p is NULL (if the bitmap has not been created) or if
       
 29233 ** i is out of range, then return false.
       
 29234 */
       
 29235 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
       
 29236   if( p==0 ) return 0;
       
 29237   if( i>p->iSize || i==0 ) return 0;
       
 29238   i--;
       
 29239   while( p->iDivisor ){
       
 29240     u32 bin = i/p->iDivisor;
       
 29241     i = i%p->iDivisor;
       
 29242     p = p->u.apSub[bin];
       
 29243     if (!p) {
       
 29244       return 0;
       
 29245     }
       
 29246   }
       
 29247   if( p->iSize<=BITVEC_NBIT ){
       
 29248     return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
       
 29249   } else{
       
 29250     u32 h = BITVEC_HASH(i++);
       
 29251     while( p->u.aHash[h] ){
       
 29252       if( p->u.aHash[h]==i ) return 1;
       
 29253       h = (h+1) % BITVEC_NINT;
       
 29254     }
       
 29255     return 0;
       
 29256   }
       
 29257 }
       
 29258 
       
 29259 /*
       
 29260 ** Set the i-th bit.  Return 0 on success and an error code if
       
 29261 ** anything goes wrong.
       
 29262 **
       
 29263 ** This routine might cause sub-bitmaps to be allocated.  Failing
       
 29264 ** to get the memory needed to hold the sub-bitmap is the only
       
 29265 ** that can go wrong with an insert, assuming p and i are valid.
       
 29266 **
       
 29267 ** The calling function must ensure that p is a valid Bitvec object
       
 29268 ** and that the value for "i" is within range of the Bitvec object.
       
 29269 ** Otherwise the behavior is undefined.
       
 29270 */
       
 29271 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
       
 29272   u32 h;
       
 29273   if( p==0 ) return SQLITE_OK;
       
 29274   assert( i>0 );
       
 29275   assert( i<=p->iSize );
       
 29276   i--;
       
 29277   while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
       
 29278     u32 bin = i/p->iDivisor;
       
 29279     i = i%p->iDivisor;
       
 29280     if( p->u.apSub[bin]==0 ){
       
 29281       p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
       
 29282       if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
       
 29283     }
       
 29284     p = p->u.apSub[bin];
       
 29285   }
       
 29286   if( p->iSize<=BITVEC_NBIT ){
       
 29287     p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
       
 29288     return SQLITE_OK;
       
 29289   }
       
 29290   h = BITVEC_HASH(i++);
       
 29291   /* if there wasn't a hash collision, and this doesn't */
       
 29292   /* completely fill the hash, then just add it without */
       
 29293   /* worring about sub-dividing and re-hashing. */
       
 29294   if( !p->u.aHash[h] ){
       
 29295     if (p->nSet<(BITVEC_NINT-1)) {
       
 29296       goto bitvec_set_end;
       
 29297     } else {
       
 29298       goto bitvec_set_rehash;
       
 29299     }
       
 29300   }
       
 29301   /* there was a collision, check to see if it's already */
       
 29302   /* in hash, if not, try to find a spot for it */
       
 29303   do {
       
 29304     if( p->u.aHash[h]==i ) return SQLITE_OK;
       
 29305     h++;
       
 29306     if( h>=BITVEC_NINT ) h = 0;
       
 29307   } while( p->u.aHash[h] );
       
 29308   /* we didn't find it in the hash.  h points to the first */
       
 29309   /* available free spot. check to see if this is going to */
       
 29310   /* make our hash too "full".  */
       
 29311 bitvec_set_rehash:
       
 29312   if( p->nSet>=BITVEC_MXHASH ){
       
 29313     unsigned int j;
       
 29314     int rc;
       
 29315     u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
       
 29316     if( aiValues==0 ){
       
 29317       return SQLITE_NOMEM;
       
 29318     }else{
       
 29319       memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
       
 29320       memset(p->u.apSub, 0, sizeof(p->u.apSub));
       
 29321       p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
       
 29322       rc = sqlite3BitvecSet(p, i);
       
 29323       for(j=0; j<BITVEC_NINT; j++){
       
 29324         if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
       
 29325       }
       
 29326       sqlite3StackFree(0, aiValues);
       
 29327       return rc;
       
 29328     }
       
 29329   }
       
 29330 bitvec_set_end:
       
 29331   p->nSet++;
       
 29332   p->u.aHash[h] = i;
       
 29333   return SQLITE_OK;
       
 29334 }
       
 29335 
       
 29336 /*
       
 29337 ** Clear the i-th bit.
       
 29338 **
       
 29339 ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
       
 29340 ** that BitvecClear can use to rebuilt its hash table.
       
 29341 */
       
 29342 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
       
 29343   if( p==0 ) return;
       
 29344   assert( i>0 );
       
 29345   i--;
       
 29346   while( p->iDivisor ){
       
 29347     u32 bin = i/p->iDivisor;
       
 29348     i = i%p->iDivisor;
       
 29349     p = p->u.apSub[bin];
       
 29350     if (!p) {
       
 29351       return;
       
 29352     }
       
 29353   }
       
 29354   if( p->iSize<=BITVEC_NBIT ){
       
 29355     p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
       
 29356   }else{
       
 29357     unsigned int j;
       
 29358     u32 *aiValues = pBuf;
       
 29359     memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
       
 29360     memset(p->u.aHash, 0, sizeof(p->u.aHash));
       
 29361     p->nSet = 0;
       
 29362     for(j=0; j<BITVEC_NINT; j++){
       
 29363       if( aiValues[j] && aiValues[j]!=(i+1) ){
       
 29364         u32 h = BITVEC_HASH(aiValues[j]-1);
       
 29365         p->nSet++;
       
 29366         while( p->u.aHash[h] ){
       
 29367           h++;
       
 29368           if( h>=BITVEC_NINT ) h = 0;
       
 29369         }
       
 29370         p->u.aHash[h] = aiValues[j];
       
 29371       }
       
 29372     }
       
 29373   }
       
 29374 }
       
 29375 
       
 29376 /*
       
 29377 ** Destroy a bitmap object.  Reclaim all memory used.
       
 29378 */
       
 29379 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
       
 29380   if( p==0 ) return;
       
 29381   if( p->iDivisor ){
       
 29382     unsigned int i;
       
 29383     for(i=0; i<BITVEC_NPTR; i++){
       
 29384       sqlite3BitvecDestroy(p->u.apSub[i]);
       
 29385     }
       
 29386   }
       
 29387   sqlite3_free(p);
       
 29388 }
       
 29389 
       
 29390 /*
       
 29391 ** Return the value of the iSize parameter specified when Bitvec *p
       
 29392 ** was created.
       
 29393 */
       
 29394 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
       
 29395   return p->iSize;
       
 29396 }
       
 29397 
       
 29398 #ifndef SQLITE_OMIT_BUILTIN_TEST
       
 29399 /*
       
 29400 ** Let V[] be an array of unsigned characters sufficient to hold
       
 29401 ** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
       
 29402 ** Then the following macros can be used to set, clear, or test
       
 29403 ** individual bits within V.
       
 29404 */
       
 29405 #define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
       
 29406 #define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
       
 29407 #define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
       
 29408 
       
 29409 /*
       
 29410 ** This routine runs an extensive test of the Bitvec code.
       
 29411 **
       
 29412 ** The input is an array of integers that acts as a program
       
 29413 ** to test the Bitvec.  The integers are opcodes followed
       
 29414 ** by 0, 1, or 3 operands, depending on the opcode.  Another
       
 29415 ** opcode follows immediately after the last operand.
       
 29416 **
       
 29417 ** There are 6 opcodes numbered from 0 through 5.  0 is the
       
 29418 ** "halt" opcode and causes the test to end.
       
 29419 **
       
 29420 **    0          Halt and return the number of errors
       
 29421 **    1 N S X    Set N bits beginning with S and incrementing by X
       
 29422 **    2 N S X    Clear N bits beginning with S and incrementing by X
       
 29423 **    3 N        Set N randomly chosen bits
       
 29424 **    4 N        Clear N randomly chosen bits
       
 29425 **    5 N S X    Set N bits from S increment X in array only, not in bitvec
       
 29426 **
       
 29427 ** The opcodes 1 through 4 perform set and clear operations are performed
       
 29428 ** on both a Bitvec object and on a linear array of bits obtained from malloc.
       
 29429 ** Opcode 5 works on the linear array only, not on the Bitvec.
       
 29430 ** Opcode 5 is used to deliberately induce a fault in order to
       
 29431 ** confirm that error detection works.
       
 29432 **
       
 29433 ** At the conclusion of the test the linear array is compared
       
 29434 ** against the Bitvec object.  If there are any differences,
       
 29435 ** an error is returned.  If they are the same, zero is returned.
       
 29436 **
       
 29437 ** If a memory allocation error occurs, return -1.
       
 29438 */
       
 29439 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
       
 29440   Bitvec *pBitvec = 0;
       
 29441   unsigned char *pV = 0;
       
 29442   int rc = -1;
       
 29443   int i, nx, pc, op;
       
 29444   void *pTmpSpace;
       
 29445 
       
 29446   /* Allocate the Bitvec to be tested and a linear array of
       
 29447   ** bits to act as the reference */
       
 29448   pBitvec = sqlite3BitvecCreate( sz );
       
 29449   pV = sqlite3_malloc( (sz+7)/8 + 1 );
       
 29450   pTmpSpace = sqlite3_malloc(BITVEC_SZ);
       
 29451   if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
       
 29452   memset(pV, 0, (sz+7)/8 + 1);
       
 29453 
       
 29454   /* NULL pBitvec tests */
       
 29455   sqlite3BitvecSet(0, 1);
       
 29456   sqlite3BitvecClear(0, 1, pTmpSpace);
       
 29457 
       
 29458   /* Run the program */
       
 29459   pc = 0;
       
 29460   while( (op = aOp[pc])!=0 ){
       
 29461     switch( op ){
       
 29462       case 1:
       
 29463       case 2:
       
 29464       case 5: {
       
 29465         nx = 4;
       
 29466         i = aOp[pc+2] - 1;
       
 29467         aOp[pc+2] += aOp[pc+3];
       
 29468         break;
       
 29469       }
       
 29470       case 3:
       
 29471       case 4: 
       
 29472       default: {
       
 29473         nx = 2;
       
 29474         sqlite3_randomness(sizeof(i), &i);
       
 29475         break;
       
 29476       }
       
 29477     }
       
 29478     if( (--aOp[pc+1]) > 0 ) nx = 0;
       
 29479     pc += nx;
       
 29480     i = (i & 0x7fffffff)%sz;
       
 29481     if( (op & 1)!=0 ){
       
 29482       SETBIT(pV, (i+1));
       
 29483       if( op!=5 ){
       
 29484         if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
       
 29485       }
       
 29486     }else{
       
 29487       CLEARBIT(pV, (i+1));
       
 29488       sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
       
 29489     }
       
 29490   }
       
 29491 
       
 29492   /* Test to make sure the linear array exactly matches the
       
 29493   ** Bitvec object.  Start with the assumption that they do
       
 29494   ** match (rc==0).  Change rc to non-zero if a discrepancy
       
 29495   ** is found.
       
 29496   */
       
 29497   rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
       
 29498           + sqlite3BitvecTest(pBitvec, 0)
       
 29499           + (sqlite3BitvecSize(pBitvec) - sz);
       
 29500   for(i=1; i<=sz; i++){
       
 29501     if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
       
 29502       rc = i;
       
 29503       break;
       
 29504     }
       
 29505   }
       
 29506 
       
 29507   /* Free allocated structure */
       
 29508 bitvec_end:
       
 29509   sqlite3_free(pTmpSpace);
       
 29510   sqlite3_free(pV);
       
 29511   sqlite3BitvecDestroy(pBitvec);
       
 29512   return rc;
       
 29513 }
       
 29514 #endif /* SQLITE_OMIT_BUILTIN_TEST */
       
 29515 
       
 29516 /************** End of bitvec.c **********************************************/
       
 29517 /************** Begin file pcache.c ******************************************/
       
 29518 /*
       
 29519 ** 2008 August 05
       
 29520 **
       
 29521 ** The author disclaims copyright to this source code.  In place of
       
 29522 ** a legal notice, here is a blessing:
       
 29523 **
       
 29524 **    May you do good and not evil.
       
 29525 **    May you find forgiveness for yourself and forgive others.
       
 29526 **    May you share freely, never taking more than you give.
       
 29527 **
       
 29528 *************************************************************************
       
 29529 ** This file implements that page cache.
       
 29530 **
       
 29531 ** @(#) $Id: pcache.c,v 1.47 2009/07/25 11:46:49 danielk1977 Exp $
       
 29532 */
       
 29533 
       
 29534 /*
       
 29535 ** A complete page cache is an instance of this structure.
       
 29536 */
       
 29537 struct PCache {
       
 29538   PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
       
 29539   PgHdr *pSynced;                     /* Last synced page in dirty page list */
       
 29540   int nRef;                           /* Number of referenced pages */
       
 29541   int nMax;                           /* Configured cache size */
       
 29542   int szPage;                         /* Size of every page in this cache */
       
 29543   int szExtra;                        /* Size of extra space for each page */
       
 29544   int bPurgeable;                     /* True if pages are on backing store */
       
 29545   int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
       
 29546   void *pStress;                      /* Argument to xStress */
       
 29547   sqlite3_pcache *pCache;             /* Pluggable cache module */
       
 29548   PgHdr *pPage1;                      /* Reference to page 1 */
       
 29549 };
       
 29550 
       
 29551 /*
       
 29552 ** Some of the assert() macros in this code are too expensive to run
       
 29553 ** even during normal debugging.  Use them only rarely on long-running
       
 29554 ** tests.  Enable the expensive asserts using the
       
 29555 ** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
       
 29556 */
       
 29557 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
       
 29558 # define expensive_assert(X)  assert(X)
       
 29559 #else
       
 29560 # define expensive_assert(X)
       
 29561 #endif
       
 29562 
       
 29563 /********************************** Linked List Management ********************/
       
 29564 
       
 29565 #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
       
 29566 /*
       
 29567 ** Check that the pCache->pSynced variable is set correctly. If it
       
 29568 ** is not, either fail an assert or return zero. Otherwise, return
       
 29569 ** non-zero. This is only used in debugging builds, as follows:
       
 29570 **
       
 29571 **   expensive_assert( pcacheCheckSynced(pCache) );
       
 29572 */
       
 29573 static int pcacheCheckSynced(PCache *pCache){
       
 29574   PgHdr *p;
       
 29575   for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
       
 29576     assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
       
 29577   }
       
 29578   return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
       
 29579 }
       
 29580 #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
       
 29581 
       
 29582 /*
       
 29583 ** Remove page pPage from the list of dirty pages.
       
 29584 */
       
 29585 static void pcacheRemoveFromDirtyList(PgHdr *pPage){
       
 29586   PCache *p = pPage->pCache;
       
 29587 
       
 29588   assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
       
 29589   assert( pPage->pDirtyPrev || pPage==p->pDirty );
       
 29590 
       
 29591   /* Update the PCache1.pSynced variable if necessary. */
       
 29592   if( p->pSynced==pPage ){
       
 29593     PgHdr *pSynced = pPage->pDirtyPrev;
       
 29594     while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
       
 29595       pSynced = pSynced->pDirtyPrev;
       
 29596     }
       
 29597     p->pSynced = pSynced;
       
 29598   }
       
 29599 
       
 29600   if( pPage->pDirtyNext ){
       
 29601     pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
       
 29602   }else{
       
 29603     assert( pPage==p->pDirtyTail );
       
 29604     p->pDirtyTail = pPage->pDirtyPrev;
       
 29605   }
       
 29606   if( pPage->pDirtyPrev ){
       
 29607     pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
       
 29608   }else{
       
 29609     assert( pPage==p->pDirty );
       
 29610     p->pDirty = pPage->pDirtyNext;
       
 29611   }
       
 29612   pPage->pDirtyNext = 0;
       
 29613   pPage->pDirtyPrev = 0;
       
 29614 
       
 29615   expensive_assert( pcacheCheckSynced(p) );
       
 29616 }
       
 29617 
       
 29618 /*
       
 29619 ** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
       
 29620 ** pPage).
       
 29621 */
       
 29622 static void pcacheAddToDirtyList(PgHdr *pPage){
       
 29623   PCache *p = pPage->pCache;
       
 29624 
       
 29625   assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
       
 29626 
       
 29627   pPage->pDirtyNext = p->pDirty;
       
 29628   if( pPage->pDirtyNext ){
       
 29629     assert( pPage->pDirtyNext->pDirtyPrev==0 );
       
 29630     pPage->pDirtyNext->pDirtyPrev = pPage;
       
 29631   }
       
 29632   p->pDirty = pPage;
       
 29633   if( !p->pDirtyTail ){
       
 29634     p->pDirtyTail = pPage;
       
 29635   }
       
 29636   if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
       
 29637     p->pSynced = pPage;
       
 29638   }
       
 29639   expensive_assert( pcacheCheckSynced(p) );
       
 29640 }
       
 29641 
       
 29642 /*
       
 29643 ** Wrapper around the pluggable caches xUnpin method. If the cache is
       
 29644 ** being used for an in-memory database, this function is a no-op.
       
 29645 */
       
 29646 static void pcacheUnpin(PgHdr *p){
       
 29647   PCache *pCache = p->pCache;
       
 29648   if( pCache->bPurgeable ){
       
 29649     if( p->pgno==1 ){
       
 29650       pCache->pPage1 = 0;
       
 29651     }
       
 29652     sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 0);
       
 29653   }
       
 29654 }
       
 29655 
       
 29656 /*************************************************** General Interfaces ******
       
 29657 **
       
 29658 ** Initialize and shutdown the page cache subsystem. Neither of these 
       
 29659 ** functions are threadsafe.
       
 29660 */
       
 29661 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
       
 29662   if( sqlite3GlobalConfig.pcache.xInit==0 ){
       
 29663     sqlite3PCacheSetDefault();
       
 29664   }
       
 29665   return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg);
       
 29666 }
       
 29667 SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
       
 29668   if( sqlite3GlobalConfig.pcache.xShutdown ){
       
 29669     sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg);
       
 29670   }
       
 29671 }
       
 29672 
       
 29673 /*
       
 29674 ** Return the size in bytes of a PCache object.
       
 29675 */
       
 29676 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
       
 29677 
       
 29678 /*
       
 29679 ** Create a new PCache object. Storage space to hold the object
       
 29680 ** has already been allocated and is passed in as the p pointer. 
       
 29681 ** The caller discovers how much space needs to be allocated by 
       
 29682 ** calling sqlite3PcacheSize().
       
 29683 */
       
 29684 SQLITE_PRIVATE void sqlite3PcacheOpen(
       
 29685   int szPage,                  /* Size of every page */
       
 29686   int szExtra,                 /* Extra space associated with each page */
       
 29687   int bPurgeable,              /* True if pages are on backing store */
       
 29688   int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
       
 29689   void *pStress,               /* Argument to xStress */
       
 29690   PCache *p                    /* Preallocated space for the PCache */
       
 29691 ){
       
 29692   memset(p, 0, sizeof(PCache));
       
 29693   p->szPage = szPage;
       
 29694   p->szExtra = szExtra;
       
 29695   p->bPurgeable = bPurgeable;
       
 29696   p->xStress = xStress;
       
 29697   p->pStress = pStress;
       
 29698   p->nMax = 100;
       
 29699 }
       
 29700 
       
 29701 /*
       
 29702 ** Change the page size for PCache object. The caller must ensure that there
       
 29703 ** are no outstanding page references when this function is called.
       
 29704 */
       
 29705 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
       
 29706   assert( pCache->nRef==0 && pCache->pDirty==0 );
       
 29707   if( pCache->pCache ){
       
 29708     sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
       
 29709     pCache->pCache = 0;
       
 29710   }
       
 29711   pCache->szPage = szPage;
       
 29712 }
       
 29713 
       
 29714 /*
       
 29715 ** Try to obtain a page from the cache.
       
 29716 */
       
 29717 SQLITE_PRIVATE int sqlite3PcacheFetch(
       
 29718   PCache *pCache,       /* Obtain the page from this cache */
       
 29719   Pgno pgno,            /* Page number to obtain */
       
 29720   int createFlag,       /* If true, create page if it does not exist already */
       
 29721   PgHdr **ppPage        /* Write the page here */
       
 29722 ){
       
 29723   PgHdr *pPage = 0;
       
 29724   int eCreate;
       
 29725 
       
 29726   assert( pCache!=0 );
       
 29727   assert( createFlag==1 || createFlag==0 );
       
 29728   assert( pgno>0 );
       
 29729 
       
 29730   /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
       
 29731   ** allocate it now.
       
 29732   */
       
 29733   if( !pCache->pCache && createFlag ){
       
 29734     sqlite3_pcache *p;
       
 29735     int nByte;
       
 29736     nByte = pCache->szPage + pCache->szExtra + sizeof(PgHdr);
       
 29737     p = sqlite3GlobalConfig.pcache.xCreate(nByte, pCache->bPurgeable);
       
 29738     if( !p ){
       
 29739       return SQLITE_NOMEM;
       
 29740     }
       
 29741     sqlite3GlobalConfig.pcache.xCachesize(p, pCache->nMax);
       
 29742     pCache->pCache = p;
       
 29743   }
       
 29744 
       
 29745   eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
       
 29746   if( pCache->pCache ){
       
 29747     pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, eCreate);
       
 29748   }
       
 29749 
       
 29750   if( !pPage && eCreate==1 ){
       
 29751     PgHdr *pPg;
       
 29752 
       
 29753     /* Find a dirty page to write-out and recycle. First try to find a 
       
 29754     ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
       
 29755     ** cleared), but if that is not possible settle for any other 
       
 29756     ** unreferenced dirty page.
       
 29757     */
       
 29758     expensive_assert( pcacheCheckSynced(pCache) );
       
 29759     for(pPg=pCache->pSynced; 
       
 29760         pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
       
 29761         pPg=pPg->pDirtyPrev
       
 29762     );
       
 29763     if( !pPg ){
       
 29764       for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
       
 29765     }
       
 29766     if( pPg ){
       
 29767       int rc;
       
 29768       rc = pCache->xStress(pCache->pStress, pPg);
       
 29769       if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
       
 29770         return rc;
       
 29771       }
       
 29772     }
       
 29773 
       
 29774     pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
       
 29775   }
       
 29776 
       
 29777   if( pPage ){
       
 29778     if( !pPage->pData ){
       
 29779       memset(pPage, 0, sizeof(PgHdr) + pCache->szExtra);
       
 29780       pPage->pExtra = (void*)&pPage[1];
       
 29781       pPage->pData = (void *)&((char *)pPage)[sizeof(PgHdr) + pCache->szExtra];
       
 29782       pPage->pCache = pCache;
       
 29783       pPage->pgno = pgno;
       
 29784     }
       
 29785     assert( pPage->pCache==pCache );
       
 29786     assert( pPage->pgno==pgno );
       
 29787     assert( pPage->pExtra==(void *)&pPage[1] );
       
 29788 
       
 29789     if( 0==pPage->nRef ){
       
 29790       pCache->nRef++;
       
 29791     }
       
 29792     pPage->nRef++;
       
 29793     if( pgno==1 ){
       
 29794       pCache->pPage1 = pPage;
       
 29795     }
       
 29796   }
       
 29797   *ppPage = pPage;
       
 29798   return (pPage==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
       
 29799 }
       
 29800 
       
 29801 /*
       
 29802 ** Decrement the reference count on a page. If the page is clean and the
       
 29803 ** reference count drops to 0, then it is made elible for recycling.
       
 29804 */
       
 29805 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
       
 29806   assert( p->nRef>0 );
       
 29807   p->nRef--;
       
 29808   if( p->nRef==0 ){
       
 29809     PCache *pCache = p->pCache;
       
 29810     pCache->nRef--;
       
 29811     if( (p->flags&PGHDR_DIRTY)==0 ){
       
 29812       pcacheUnpin(p);
       
 29813     }else{
       
 29814       /* Move the page to the head of the dirty list. */
       
 29815       pcacheRemoveFromDirtyList(p);
       
 29816       pcacheAddToDirtyList(p);
       
 29817     }
       
 29818   }
       
 29819 }
       
 29820 
       
 29821 /*
       
 29822 ** Increase the reference count of a supplied page by 1.
       
 29823 */
       
 29824 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
       
 29825   assert(p->nRef>0);
       
 29826   p->nRef++;
       
 29827 }
       
 29828 
       
 29829 /*
       
 29830 ** Drop a page from the cache. There must be exactly one reference to the
       
 29831 ** page. This function deletes that reference, so after it returns the
       
 29832 ** page pointed to by p is invalid.
       
 29833 */
       
 29834 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
       
 29835   PCache *pCache;
       
 29836   assert( p->nRef==1 );
       
 29837   if( p->flags&PGHDR_DIRTY ){
       
 29838     pcacheRemoveFromDirtyList(p);
       
 29839   }
       
 29840   pCache = p->pCache;
       
 29841   pCache->nRef--;
       
 29842   if( p->pgno==1 ){
       
 29843     pCache->pPage1 = 0;
       
 29844   }
       
 29845   sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 1);
       
 29846 }
       
 29847 
       
 29848 /*
       
 29849 ** Make sure the page is marked as dirty. If it isn't dirty already,
       
 29850 ** make it so.
       
 29851 */
       
 29852 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
       
 29853   p->flags &= ~PGHDR_DONT_WRITE;
       
 29854   assert( p->nRef>0 );
       
 29855   if( 0==(p->flags & PGHDR_DIRTY) ){
       
 29856     p->flags |= PGHDR_DIRTY;
       
 29857     pcacheAddToDirtyList( p);
       
 29858   }
       
 29859 }
       
 29860 
       
 29861 /*
       
 29862 ** Make sure the page is marked as clean. If it isn't clean already,
       
 29863 ** make it so.
       
 29864 */
       
 29865 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
       
 29866   if( (p->flags & PGHDR_DIRTY) ){
       
 29867     pcacheRemoveFromDirtyList(p);
       
 29868     p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
       
 29869     if( p->nRef==0 ){
       
 29870       pcacheUnpin(p);
       
 29871     }
       
 29872   }
       
 29873 }
       
 29874 
       
 29875 /*
       
 29876 ** Make every page in the cache clean.
       
 29877 */
       
 29878 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
       
 29879   PgHdr *p;
       
 29880   while( (p = pCache->pDirty)!=0 ){
       
 29881     sqlite3PcacheMakeClean(p);
       
 29882   }
       
 29883 }
       
 29884 
       
 29885 /*
       
 29886 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
       
 29887 */
       
 29888 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
       
 29889   PgHdr *p;
       
 29890   for(p=pCache->pDirty; p; p=p->pDirtyNext){
       
 29891     p->flags &= ~PGHDR_NEED_SYNC;
       
 29892   }
       
 29893   pCache->pSynced = pCache->pDirtyTail;
       
 29894 }
       
 29895 
       
 29896 /*
       
 29897 ** Change the page number of page p to newPgno. 
       
 29898 */
       
 29899 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
       
 29900   PCache *pCache = p->pCache;
       
 29901   assert( p->nRef>0 );
       
 29902   assert( newPgno>0 );
       
 29903   sqlite3GlobalConfig.pcache.xRekey(pCache->pCache, p, p->pgno, newPgno);
       
 29904   p->pgno = newPgno;
       
 29905   if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
       
 29906     pcacheRemoveFromDirtyList(p);
       
 29907     pcacheAddToDirtyList(p);
       
 29908   }
       
 29909 }
       
 29910 
       
 29911 /*
       
 29912 ** Drop every cache entry whose page number is greater than "pgno". The
       
 29913 ** caller must ensure that there are no outstanding references to any pages
       
 29914 ** other than page 1 with a page number greater than pgno.
       
 29915 **
       
 29916 ** If there is a reference to page 1 and the pgno parameter passed to this
       
 29917 ** function is 0, then the data area associated with page 1 is zeroed, but
       
 29918 ** the page object is not dropped.
       
 29919 */
       
 29920 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
       
 29921   if( pCache->pCache ){
       
 29922     PgHdr *p;
       
 29923     PgHdr *pNext;
       
 29924     for(p=pCache->pDirty; p; p=pNext){
       
 29925       pNext = p->pDirtyNext;
       
 29926       if( p->pgno>pgno ){
       
 29927         assert( p->flags&PGHDR_DIRTY );
       
 29928         sqlite3PcacheMakeClean(p);
       
 29929       }
       
 29930     }
       
 29931     if( pgno==0 && pCache->pPage1 ){
       
 29932       memset(pCache->pPage1->pData, 0, pCache->szPage);
       
 29933       pgno = 1;
       
 29934     }
       
 29935     sqlite3GlobalConfig.pcache.xTruncate(pCache->pCache, pgno+1);
       
 29936   }
       
 29937 }
       
 29938 
       
 29939 /*
       
 29940 ** Close a cache.
       
 29941 */
       
 29942 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
       
 29943   if( pCache->pCache ){
       
 29944     sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
       
 29945   }
       
 29946 }
       
 29947 
       
 29948 /* 
       
 29949 ** Discard the contents of the cache.
       
 29950 */
       
 29951 SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
       
 29952   sqlite3PcacheTruncate(pCache, 0);
       
 29953 }
       
 29954 
       
 29955 /*
       
 29956 ** Merge two lists of pages connected by pDirty and in pgno order.
       
 29957 ** Do not both fixing the pDirtyPrev pointers.
       
 29958 */
       
 29959 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
       
 29960   PgHdr result, *pTail;
       
 29961   pTail = &result;
       
 29962   while( pA && pB ){
       
 29963     if( pA->pgno<pB->pgno ){
       
 29964       pTail->pDirty = pA;
       
 29965       pTail = pA;
       
 29966       pA = pA->pDirty;
       
 29967     }else{
       
 29968       pTail->pDirty = pB;
       
 29969       pTail = pB;
       
 29970       pB = pB->pDirty;
       
 29971     }
       
 29972   }
       
 29973   if( pA ){
       
 29974     pTail->pDirty = pA;
       
 29975   }else if( pB ){
       
 29976     pTail->pDirty = pB;
       
 29977   }else{
       
 29978     pTail->pDirty = 0;
       
 29979   }
       
 29980   return result.pDirty;
       
 29981 }
       
 29982 
       
 29983 /*
       
 29984 ** Sort the list of pages in accending order by pgno.  Pages are
       
 29985 ** connected by pDirty pointers.  The pDirtyPrev pointers are
       
 29986 ** corrupted by this sort.
       
 29987 **
       
 29988 ** Since there cannot be more than 2^31 distinct pages in a database,
       
 29989 ** there cannot be more than 31 buckets required by the merge sorter.
       
 29990 ** One extra bucket is added to catch overflow in case something
       
 29991 ** ever changes to make the previous sentence incorrect.
       
 29992 */
       
 29993 #define N_SORT_BUCKET  32
       
 29994 static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
       
 29995   PgHdr *a[N_SORT_BUCKET], *p;
       
 29996   int i;
       
 29997   memset(a, 0, sizeof(a));
       
 29998   while( pIn ){
       
 29999     p = pIn;
       
 30000     pIn = p->pDirty;
       
 30001     p->pDirty = 0;
       
 30002     for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
       
 30003       if( a[i]==0 ){
       
 30004         a[i] = p;
       
 30005         break;
       
 30006       }else{
       
 30007         p = pcacheMergeDirtyList(a[i], p);
       
 30008         a[i] = 0;
       
 30009       }
       
 30010     }
       
 30011     if( NEVER(i==N_SORT_BUCKET-1) ){
       
 30012       /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
       
 30013       ** the input list.  But that is impossible.
       
 30014       */
       
 30015       a[i] = pcacheMergeDirtyList(a[i], p);
       
 30016     }
       
 30017   }
       
 30018   p = a[0];
       
 30019   for(i=1; i<N_SORT_BUCKET; i++){
       
 30020     p = pcacheMergeDirtyList(p, a[i]);
       
 30021   }
       
 30022   return p;
       
 30023 }
       
 30024 
       
 30025 /*
       
 30026 ** Return a list of all dirty pages in the cache, sorted by page number.
       
 30027 */
       
 30028 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
       
 30029   PgHdr *p;
       
 30030   for(p=pCache->pDirty; p; p=p->pDirtyNext){
       
 30031     p->pDirty = p->pDirtyNext;
       
 30032   }
       
 30033   return pcacheSortDirtyList(pCache->pDirty);
       
 30034 }
       
 30035 
       
 30036 /* 
       
 30037 ** Return the total number of referenced pages held by the cache.
       
 30038 */
       
 30039 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
       
 30040   return pCache->nRef;
       
 30041 }
       
 30042 
       
 30043 /*
       
 30044 ** Return the number of references to the page supplied as an argument.
       
 30045 */
       
 30046 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
       
 30047   return p->nRef;
       
 30048 }
       
 30049 
       
 30050 /* 
       
 30051 ** Return the total number of pages in the cache.
       
 30052 */
       
 30053 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
       
 30054   int nPage = 0;
       
 30055   if( pCache->pCache ){
       
 30056     nPage = sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache);
       
 30057   }
       
 30058   return nPage;
       
 30059 }
       
 30060 
       
 30061 #ifdef SQLITE_TEST
       
 30062 /*
       
 30063 ** Get the suggested cache-size value.
       
 30064 */
       
 30065 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
       
 30066   return pCache->nMax;
       
 30067 }
       
 30068 #endif
       
 30069 
       
 30070 /*
       
 30071 ** Set the suggested cache-size value.
       
 30072 */
       
 30073 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
       
 30074   pCache->nMax = mxPage;
       
 30075   if( pCache->pCache ){
       
 30076     sqlite3GlobalConfig.pcache.xCachesize(pCache->pCache, mxPage);
       
 30077   }
       
 30078 }
       
 30079 
       
 30080 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
       
 30081 /*
       
 30082 ** For all dirty pages currently in the cache, invoke the specified
       
 30083 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
       
 30084 ** defined.
       
 30085 */
       
 30086 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
       
 30087   PgHdr *pDirty;
       
 30088   for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
       
 30089     xIter(pDirty);
       
 30090   }
       
 30091 }
       
 30092 #endif
       
 30093 
       
 30094 /************** End of pcache.c **********************************************/
       
 30095 /************** Begin file pcache1.c *****************************************/
       
 30096 /*
       
 30097 ** 2008 November 05
       
 30098 **
       
 30099 ** The author disclaims copyright to this source code.  In place of
       
 30100 ** a legal notice, here is a blessing:
       
 30101 **
       
 30102 **    May you do good and not evil.
       
 30103 **    May you find forgiveness for yourself and forgive others.
       
 30104 **    May you share freely, never taking more than you give.
       
 30105 **
       
 30106 *************************************************************************
       
 30107 **
       
 30108 ** This file implements the default page cache implementation (the
       
 30109 ** sqlite3_pcache interface). It also contains part of the implementation
       
 30110 ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
       
 30111 ** If the default page cache implementation is overriden, then neither of
       
 30112 ** these two features are available.
       
 30113 **
       
 30114 ** @(#) $Id: pcache1.c,v 1.19 2009/07/17 11:44:07 drh Exp $
       
 30115 */
       
 30116 
       
 30117 
       
 30118 typedef struct PCache1 PCache1;
       
 30119 typedef struct PgHdr1 PgHdr1;
       
 30120 typedef struct PgFreeslot PgFreeslot;
       
 30121 
       
 30122 /* Pointers to structures of this type are cast and returned as 
       
 30123 ** opaque sqlite3_pcache* handles
       
 30124 */
       
 30125 struct PCache1 {
       
 30126   /* Cache configuration parameters. Page size (szPage) and the purgeable
       
 30127   ** flag (bPurgeable) are set when the cache is created. nMax may be 
       
 30128   ** modified at any time by a call to the pcache1CacheSize() method.
       
 30129   ** The global mutex must be held when accessing nMax.
       
 30130   */
       
 30131   int szPage;                         /* Size of allocated pages in bytes */
       
 30132   int bPurgeable;                     /* True if cache is purgeable */
       
 30133   unsigned int nMin;                  /* Minimum number of pages reserved */
       
 30134   unsigned int nMax;                  /* Configured "cache_size" value */
       
 30135 
       
 30136   /* Hash table of all pages. The following variables may only be accessed
       
 30137   ** when the accessor is holding the global mutex (see pcache1EnterMutex() 
       
 30138   ** and pcache1LeaveMutex()).
       
 30139   */
       
 30140   unsigned int nRecyclable;           /* Number of pages in the LRU list */
       
 30141   unsigned int nPage;                 /* Total number of pages in apHash */
       
 30142   unsigned int nHash;                 /* Number of slots in apHash[] */
       
 30143   PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
       
 30144 
       
 30145   unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
       
 30146 };
       
 30147 
       
 30148 /*
       
 30149 ** Each cache entry is represented by an instance of the following 
       
 30150 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated 
       
 30151 ** directly before this structure in memory (see the PGHDR1_TO_PAGE() 
       
 30152 ** macro below).
       
 30153 */
       
 30154 struct PgHdr1 {
       
 30155   unsigned int iKey;             /* Key value (page number) */
       
 30156   PgHdr1 *pNext;                 /* Next in hash table chain */
       
 30157   PCache1 *pCache;               /* Cache that currently owns this page */
       
 30158   PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
       
 30159   PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
       
 30160 };
       
 30161 
       
 30162 /*
       
 30163 ** Free slots in the allocator used to divide up the buffer provided using
       
 30164 ** the SQLITE_CONFIG_PAGECACHE mechanism.
       
 30165 */
       
 30166 struct PgFreeslot {
       
 30167   PgFreeslot *pNext;  /* Next free slot */
       
 30168 };
       
 30169 
       
 30170 /*
       
 30171 ** Global data used by this cache.
       
 30172 */
       
 30173 static SQLITE_WSD struct PCacheGlobal {
       
 30174   sqlite3_mutex *mutex;               /* static mutex MUTEX_STATIC_LRU */
       
 30175 
       
 30176   int nMaxPage;                       /* Sum of nMaxPage for purgeable caches */
       
 30177   int nMinPage;                       /* Sum of nMinPage for purgeable caches */
       
 30178   int nCurrentPage;                   /* Number of purgeable pages allocated */
       
 30179   PgHdr1 *pLruHead, *pLruTail;        /* LRU list of unpinned pages */
       
 30180 
       
 30181   /* Variables related to SQLITE_CONFIG_PAGECACHE settings. */
       
 30182   int szSlot;                         /* Size of each free slot */
       
 30183   void *pStart, *pEnd;                /* Bounds of pagecache malloc range */
       
 30184   PgFreeslot *pFree;                  /* Free page blocks */
       
 30185   int isInit;                         /* True if initialized */
       
 30186 } pcache1_g;
       
 30187 
       
 30188 /*
       
 30189 ** All code in this file should access the global structure above via the
       
 30190 ** alias "pcache1". This ensures that the WSD emulation is used when
       
 30191 ** compiling for systems that do not support real WSD.
       
 30192 */
       
 30193 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
       
 30194 
       
 30195 /*
       
 30196 ** When a PgHdr1 structure is allocated, the associated PCache1.szPage
       
 30197 ** bytes of data are located directly before it in memory (i.e. the total
       
 30198 ** size of the allocation is sizeof(PgHdr1)+PCache1.szPage byte). The
       
 30199 ** PGHDR1_TO_PAGE() macro takes a pointer to a PgHdr1 structure as
       
 30200 ** an argument and returns a pointer to the associated block of szPage
       
 30201 ** bytes. The PAGE_TO_PGHDR1() macro does the opposite: its argument is
       
 30202 ** a pointer to a block of szPage bytes of data and the return value is
       
 30203 ** a pointer to the associated PgHdr1 structure.
       
 30204 **
       
 30205 **   assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
       
 30206 */
       
 30207 #define PGHDR1_TO_PAGE(p)    (void*)(((char*)p) - p->pCache->szPage)
       
 30208 #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
       
 30209 
       
 30210 /*
       
 30211 ** Macros to enter and leave the global LRU mutex.
       
 30212 */
       
 30213 #define pcache1EnterMutex() sqlite3_mutex_enter(pcache1.mutex)
       
 30214 #define pcache1LeaveMutex() sqlite3_mutex_leave(pcache1.mutex)
       
 30215 
       
 30216 /******************************************************************************/
       
 30217 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
       
 30218 
       
 30219 /*
       
 30220 ** This function is called during initialization if a static buffer is 
       
 30221 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
       
 30222 ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
       
 30223 ** enough to contain 'n' buffers of 'sz' bytes each.
       
 30224 */
       
 30225 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
       
 30226   if( pcache1.isInit ){
       
 30227     PgFreeslot *p;
       
 30228     sz = ROUNDDOWN8(sz);
       
 30229     pcache1.szSlot = sz;
       
 30230     pcache1.pStart = pBuf;
       
 30231     pcache1.pFree = 0;
       
 30232     while( n-- ){
       
 30233       p = (PgFreeslot*)pBuf;
       
 30234       p->pNext = pcache1.pFree;
       
 30235       pcache1.pFree = p;
       
 30236       pBuf = (void*)&((char*)pBuf)[sz];
       
 30237     }
       
 30238     pcache1.pEnd = pBuf;
       
 30239   }
       
 30240 }
       
 30241 
       
 30242 /*
       
 30243 ** Malloc function used within this file to allocate space from the buffer
       
 30244 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no 
       
 30245 ** such buffer exists or there is no space left in it, this function falls 
       
 30246 ** back to sqlite3Malloc().
       
 30247 */
       
 30248 static void *pcache1Alloc(int nByte){
       
 30249   void *p;
       
 30250   assert( sqlite3_mutex_held(pcache1.mutex) );
       
 30251   if( nByte<=pcache1.szSlot && pcache1.pFree ){
       
 30252     assert( pcache1.isInit );
       
 30253     p = (PgHdr1 *)pcache1.pFree;
       
 30254     pcache1.pFree = pcache1.pFree->pNext;
       
 30255     sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
       
 30256     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
       
 30257   }else{
       
 30258 
       
 30259     /* Allocate a new buffer using sqlite3Malloc. Before doing so, exit the
       
 30260     ** global pcache mutex and unlock the pager-cache object pCache. This is 
       
 30261     ** so that if the attempt to allocate a new buffer causes the the 
       
 30262     ** configured soft-heap-limit to be breached, it will be possible to
       
 30263     ** reclaim memory from this pager-cache.
       
 30264     */
       
 30265     pcache1LeaveMutex();
       
 30266     p = sqlite3Malloc(nByte);
       
 30267     pcache1EnterMutex();
       
 30268     if( p ){
       
 30269       int sz = sqlite3MallocSize(p);
       
 30270       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
       
 30271     }
       
 30272   }
       
 30273   return p;
       
 30274 }
       
 30275 
       
 30276 /*
       
 30277 ** Free an allocated buffer obtained from pcache1Alloc().
       
 30278 */
       
 30279 static void pcache1Free(void *p){
       
 30280   assert( sqlite3_mutex_held(pcache1.mutex) );
       
 30281   if( p==0 ) return;
       
 30282   if( p>=pcache1.pStart && p<pcache1.pEnd ){
       
 30283     PgFreeslot *pSlot;
       
 30284     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
       
 30285     pSlot = (PgFreeslot*)p;
       
 30286     pSlot->pNext = pcache1.pFree;
       
 30287     pcache1.pFree = pSlot;
       
 30288   }else{
       
 30289     int iSize = sqlite3MallocSize(p);
       
 30290     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
       
 30291     sqlite3_free(p);
       
 30292   }
       
 30293 }
       
 30294 
       
 30295 /*
       
 30296 ** Allocate a new page object initially associated with cache pCache.
       
 30297 */
       
 30298 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
       
 30299   int nByte = sizeof(PgHdr1) + pCache->szPage;
       
 30300   void *pPg = pcache1Alloc(nByte);
       
 30301   PgHdr1 *p;
       
 30302   if( pPg ){
       
 30303     p = PAGE_TO_PGHDR1(pCache, pPg);
       
 30304     if( pCache->bPurgeable ){
       
 30305       pcache1.nCurrentPage++;
       
 30306     }
       
 30307   }else{
       
 30308     p = 0;
       
 30309   }
       
 30310   return p;
       
 30311 }
       
 30312 
       
 30313 /*
       
 30314 ** Free a page object allocated by pcache1AllocPage().
       
 30315 **
       
 30316 ** The pointer is allowed to be NULL, which is prudent.  But it turns out
       
 30317 ** that the current implementation happens to never call this routine
       
 30318 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
       
 30319 */
       
 30320 static void pcache1FreePage(PgHdr1 *p){
       
 30321   if( ALWAYS(p) ){
       
 30322     if( p->pCache->bPurgeable ){
       
 30323       pcache1.nCurrentPage--;
       
 30324     }
       
 30325     pcache1Free(PGHDR1_TO_PAGE(p));
       
 30326   }
       
 30327 }
       
 30328 
       
 30329 /*
       
 30330 ** Malloc function used by SQLite to obtain space from the buffer configured
       
 30331 ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
       
 30332 ** exists, this function falls back to sqlite3Malloc().
       
 30333 */
       
 30334 SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
       
 30335   void *p;
       
 30336   pcache1EnterMutex();
       
 30337   p = pcache1Alloc(sz);
       
 30338   pcache1LeaveMutex();
       
 30339   return p;
       
 30340 }
       
 30341 
       
 30342 /*
       
 30343 ** Free an allocated buffer obtained from sqlite3PageMalloc().
       
 30344 */
       
 30345 SQLITE_PRIVATE void sqlite3PageFree(void *p){
       
 30346   pcache1EnterMutex();
       
 30347   pcache1Free(p);
       
 30348   pcache1LeaveMutex();
       
 30349 }
       
 30350 
       
 30351 /******************************************************************************/
       
 30352 /******** General Implementation Functions ************************************/
       
 30353 
       
 30354 /*
       
 30355 ** This function is used to resize the hash table used by the cache passed
       
 30356 ** as the first argument.
       
 30357 **
       
 30358 ** The global mutex must be held when this function is called.
       
 30359 */
       
 30360 static int pcache1ResizeHash(PCache1 *p){
       
 30361   PgHdr1 **apNew;
       
 30362   unsigned int nNew;
       
 30363   unsigned int i;
       
 30364 
       
 30365   assert( sqlite3_mutex_held(pcache1.mutex) );
       
 30366 
       
 30367   nNew = p->nHash*2;
       
 30368   if( nNew<256 ){
       
 30369     nNew = 256;
       
 30370   }
       
 30371 
       
 30372   pcache1LeaveMutex();
       
 30373   if( p->nHash ){ sqlite3BeginBenignMalloc(); }
       
 30374   apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
       
 30375   if( p->nHash ){ sqlite3EndBenignMalloc(); }
       
 30376   pcache1EnterMutex();
       
 30377   if( apNew ){
       
 30378     memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
       
 30379     for(i=0; i<p->nHash; i++){
       
 30380       PgHdr1 *pPage;
       
 30381       PgHdr1 *pNext = p->apHash[i];
       
 30382       while( (pPage = pNext)!=0 ){
       
 30383         unsigned int h = pPage->iKey % nNew;
       
 30384         pNext = pPage->pNext;
       
 30385         pPage->pNext = apNew[h];
       
 30386         apNew[h] = pPage;
       
 30387       }
       
 30388     }
       
 30389     sqlite3_free(p->apHash);
       
 30390     p->apHash = apNew;
       
 30391     p->nHash = nNew;
       
 30392   }
       
 30393 
       
 30394   return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
       
 30395 }
       
 30396 
       
 30397 /*
       
 30398 ** This function is used internally to remove the page pPage from the 
       
 30399 ** global LRU list, if is part of it. If pPage is not part of the global
       
 30400 ** LRU list, then this function is a no-op.
       
 30401 **
       
 30402 ** The global mutex must be held when this function is called.
       
 30403 */
       
 30404 static void pcache1PinPage(PgHdr1 *pPage){
       
 30405   assert( sqlite3_mutex_held(pcache1.mutex) );
       
 30406   if( pPage && (pPage->pLruNext || pPage==pcache1.pLruTail) ){
       
 30407     if( pPage->pLruPrev ){
       
 30408       pPage->pLruPrev->pLruNext = pPage->pLruNext;
       
 30409     }
       
 30410     if( pPage->pLruNext ){
       
 30411       pPage->pLruNext->pLruPrev = pPage->pLruPrev;
       
 30412     }
       
 30413     if( pcache1.pLruHead==pPage ){
       
 30414       pcache1.pLruHead = pPage->pLruNext;
       
 30415     }
       
 30416     if( pcache1.pLruTail==pPage ){
       
 30417       pcache1.pLruTail = pPage->pLruPrev;
       
 30418     }
       
 30419     pPage->pLruNext = 0;
       
 30420     pPage->pLruPrev = 0;
       
 30421     pPage->pCache->nRecyclable--;
       
 30422   }
       
 30423 }
       
 30424 
       
 30425 
       
 30426 /*
       
 30427 ** Remove the page supplied as an argument from the hash table 
       
 30428 ** (PCache1.apHash structure) that it is currently stored in.
       
 30429 **
       
 30430 ** The global mutex must be held when this function is called.
       
 30431 */
       
 30432 static void pcache1RemoveFromHash(PgHdr1 *pPage){
       
 30433   unsigned int h;
       
 30434   PCache1 *pCache = pPage->pCache;
       
 30435   PgHdr1 **pp;
       
 30436 
       
 30437   h = pPage->iKey % pCache->nHash;
       
 30438   for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
       
 30439   *pp = (*pp)->pNext;
       
 30440 
       
 30441   pCache->nPage--;
       
 30442 }
       
 30443 
       
 30444 /*
       
 30445 ** If there are currently more than pcache.nMaxPage pages allocated, try
       
 30446 ** to recycle pages to reduce the number allocated to pcache.nMaxPage.
       
 30447 */
       
 30448 static void pcache1EnforceMaxPage(void){
       
 30449   assert( sqlite3_mutex_held(pcache1.mutex) );
       
 30450   while( pcache1.nCurrentPage>pcache1.nMaxPage && pcache1.pLruTail ){
       
 30451     PgHdr1 *p = pcache1.pLruTail;
       
 30452     pcache1PinPage(p);
       
 30453     pcache1RemoveFromHash(p);
       
 30454     pcache1FreePage(p);
       
 30455   }
       
 30456 }
       
 30457 
       
 30458 /*
       
 30459 ** Discard all pages from cache pCache with a page number (key value) 
       
 30460 ** greater than or equal to iLimit. Any pinned pages that meet this 
       
 30461 ** criteria are unpinned before they are discarded.
       
 30462 **
       
 30463 ** The global mutex must be held when this function is called.
       
 30464 */
       
 30465 static void pcache1TruncateUnsafe(
       
 30466   PCache1 *pCache, 
       
 30467   unsigned int iLimit 
       
 30468 ){
       
 30469   TESTONLY( unsigned int nPage = 0; )      /* Used to assert pCache->nPage is correct */
       
 30470   unsigned int h;
       
 30471   assert( sqlite3_mutex_held(pcache1.mutex) );
       
 30472   for(h=0; h<pCache->nHash; h++){
       
 30473     PgHdr1 **pp = &pCache->apHash[h]; 
       
 30474     PgHdr1 *pPage;
       
 30475     while( (pPage = *pp)!=0 ){
       
 30476       if( pPage->iKey>=iLimit ){
       
 30477         pCache->nPage--;
       
 30478         *pp = pPage->pNext;
       
 30479         pcache1PinPage(pPage);
       
 30480         pcache1FreePage(pPage);
       
 30481       }else{
       
 30482         pp = &pPage->pNext;
       
 30483         TESTONLY( nPage++; )
       
 30484       }
       
 30485     }
       
 30486   }
       
 30487   assert( pCache->nPage==nPage );
       
 30488 }
       
 30489 
       
 30490 /******************************************************************************/
       
 30491 /******** sqlite3_pcache Methods **********************************************/
       
 30492 
       
 30493 /*
       
 30494 ** Implementation of the sqlite3_pcache.xInit method.
       
 30495 */
       
 30496 static int pcache1Init(void *NotUsed){
       
 30497   UNUSED_PARAMETER(NotUsed);
       
 30498   assert( pcache1.isInit==0 );
       
 30499   memset(&pcache1, 0, sizeof(pcache1));
       
 30500   if( sqlite3GlobalConfig.bCoreMutex ){
       
 30501     pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
       
 30502   }
       
 30503   pcache1.isInit = 1;
       
 30504   return SQLITE_OK;
       
 30505 }
       
 30506 
       
 30507 /*
       
 30508 ** Implementation of the sqlite3_pcache.xShutdown method.
       
 30509 ** Note that the static mutex allocated in xInit does 
       
 30510 ** not need to be freed.
       
 30511 */
       
 30512 static void pcache1Shutdown(void *NotUsed){
       
 30513   UNUSED_PARAMETER(NotUsed);
       
 30514   assert( pcache1.isInit!=0 );
       
 30515   memset(&pcache1, 0, sizeof(pcache1));
       
 30516 }
       
 30517 
       
 30518 /*
       
 30519 ** Implementation of the sqlite3_pcache.xCreate method.
       
 30520 **
       
 30521 ** Allocate a new cache.
       
 30522 */
       
 30523 static sqlite3_pcache *pcache1Create(int szPage, int bPurgeable){
       
 30524   PCache1 *pCache;
       
 30525 
       
 30526   pCache = (PCache1 *)sqlite3_malloc(sizeof(PCache1));
       
 30527   if( pCache ){
       
 30528     memset(pCache, 0, sizeof(PCache1));
       
 30529     pCache->szPage = szPage;
       
 30530     pCache->bPurgeable = (bPurgeable ? 1 : 0);
       
 30531     if( bPurgeable ){
       
 30532       pCache->nMin = 10;
       
 30533       pcache1EnterMutex();
       
 30534       pcache1.nMinPage += pCache->nMin;
       
 30535       pcache1LeaveMutex();
       
 30536     }
       
 30537   }
       
 30538   return (sqlite3_pcache *)pCache;
       
 30539 }
       
 30540 
       
 30541 /*
       
 30542 ** Implementation of the sqlite3_pcache.xCachesize method. 
       
 30543 **
       
 30544 ** Configure the cache_size limit for a cache.
       
 30545 */
       
 30546 static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
       
 30547   PCache1 *pCache = (PCache1 *)p;
       
 30548   if( pCache->bPurgeable ){
       
 30549     pcache1EnterMutex();
       
 30550     pcache1.nMaxPage += (nMax - pCache->nMax);
       
 30551     pCache->nMax = nMax;
       
 30552     pcache1EnforceMaxPage();
       
 30553     pcache1LeaveMutex();
       
 30554   }
       
 30555 }
       
 30556 
       
 30557 /*
       
 30558 ** Implementation of the sqlite3_pcache.xPagecount method. 
       
 30559 */
       
 30560 static int pcache1Pagecount(sqlite3_pcache *p){
       
 30561   int n;
       
 30562   pcache1EnterMutex();
       
 30563   n = ((PCache1 *)p)->nPage;
       
 30564   pcache1LeaveMutex();
       
 30565   return n;
       
 30566 }
       
 30567 
       
 30568 /*
       
 30569 ** Implementation of the sqlite3_pcache.xFetch method. 
       
 30570 **
       
 30571 ** Fetch a page by key value.
       
 30572 **
       
 30573 ** Whether or not a new page may be allocated by this function depends on
       
 30574 ** the value of the createFlag argument.  0 means do not allocate a new
       
 30575 ** page.  1 means allocate a new page if space is easily available.  2 
       
 30576 ** means to try really hard to allocate a new page.
       
 30577 **
       
 30578 ** For a non-purgeable cache (a cache used as the storage for an in-memory
       
 30579 ** database) there is really no difference between createFlag 1 and 2.  So
       
 30580 ** the calling function (pcache.c) will never have a createFlag of 1 on
       
 30581 ** a non-purgable cache.
       
 30582 **
       
 30583 ** There are three different approaches to obtaining space for a page,
       
 30584 ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
       
 30585 **
       
 30586 **   1. Regardless of the value of createFlag, the cache is searched for a 
       
 30587 **      copy of the requested page. If one is found, it is returned.
       
 30588 **
       
 30589 **   2. If createFlag==0 and the page is not already in the cache, NULL is
       
 30590 **      returned.
       
 30591 **
       
 30592 **   3. If createFlag is 1, and the page is not already in the cache,
       
 30593 **      and if either of the following are true, return NULL:
       
 30594 **
       
 30595 **       (a) the number of pages pinned by the cache is greater than
       
 30596 **           PCache1.nMax, or
       
 30597 **       (b) the number of pages pinned by the cache is greater than
       
 30598 **           the sum of nMax for all purgeable caches, less the sum of 
       
 30599 **           nMin for all other purgeable caches. 
       
 30600 **
       
 30601 **   4. If none of the first three conditions apply and the cache is marked
       
 30602 **      as purgeable, and if one of the following is true:
       
 30603 **
       
 30604 **       (a) The number of pages allocated for the cache is already 
       
 30605 **           PCache1.nMax, or
       
 30606 **
       
 30607 **       (b) The number of pages allocated for all purgeable caches is
       
 30608 **           already equal to or greater than the sum of nMax for all
       
 30609 **           purgeable caches,
       
 30610 **
       
 30611 **      then attempt to recycle a page from the LRU list. If it is the right
       
 30612 **      size, return the recycled buffer. Otherwise, free the buffer and
       
 30613 **      proceed to step 5. 
       
 30614 **
       
 30615 **   5. Otherwise, allocate and return a new page buffer.
       
 30616 */
       
 30617 static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
       
 30618   unsigned int nPinned;
       
 30619   PCache1 *pCache = (PCache1 *)p;
       
 30620   PgHdr1 *pPage = 0;
       
 30621 
       
 30622   assert( pCache->bPurgeable || createFlag!=1 );
       
 30623   pcache1EnterMutex();
       
 30624   if( createFlag==1 ) sqlite3BeginBenignMalloc();
       
 30625 
       
 30626   /* Search the hash table for an existing entry. */
       
 30627   if( pCache->nHash>0 ){
       
 30628     unsigned int h = iKey % pCache->nHash;
       
 30629     for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
       
 30630   }
       
 30631 
       
 30632   if( pPage || createFlag==0 ){
       
 30633     pcache1PinPage(pPage);
       
 30634     goto fetch_out;
       
 30635   }
       
 30636 
       
 30637   /* Step 3 of header comment. */
       
 30638   nPinned = pCache->nPage - pCache->nRecyclable;
       
 30639   if( createFlag==1 && (
       
 30640         nPinned>=(pcache1.nMaxPage+pCache->nMin-pcache1.nMinPage)
       
 30641      || nPinned>=(pCache->nMax * 9 / 10)
       
 30642   )){
       
 30643     goto fetch_out;
       
 30644   }
       
 30645 
       
 30646   if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
       
 30647     goto fetch_out;
       
 30648   }
       
 30649 
       
 30650   /* Step 4. Try to recycle a page buffer if appropriate. */
       
 30651   if( pCache->bPurgeable && pcache1.pLruTail && (
       
 30652      (pCache->nPage+1>=pCache->nMax) || pcache1.nCurrentPage>=pcache1.nMaxPage
       
 30653   )){
       
 30654     pPage = pcache1.pLruTail;
       
 30655     pcache1RemoveFromHash(pPage);
       
 30656     pcache1PinPage(pPage);
       
 30657     if( pPage->pCache->szPage!=pCache->szPage ){
       
 30658       pcache1FreePage(pPage);
       
 30659       pPage = 0;
       
 30660     }else{
       
 30661       pcache1.nCurrentPage -= (pPage->pCache->bPurgeable - pCache->bPurgeable);
       
 30662     }
       
 30663   }
       
 30664 
       
 30665   /* Step 5. If a usable page buffer has still not been found, 
       
 30666   ** attempt to allocate a new one. 
       
 30667   */
       
 30668   if( !pPage ){
       
 30669     pPage = pcache1AllocPage(pCache);
       
 30670   }
       
 30671 
       
 30672   if( pPage ){
       
 30673     unsigned int h = iKey % pCache->nHash;
       
 30674     pCache->nPage++;
       
 30675     pPage->iKey = iKey;
       
 30676     pPage->pNext = pCache->apHash[h];
       
 30677     pPage->pCache = pCache;
       
 30678     pPage->pLruPrev = 0;
       
 30679     pPage->pLruNext = 0;
       
 30680     *(void **)(PGHDR1_TO_PAGE(pPage)) = 0;
       
 30681     pCache->apHash[h] = pPage;
       
 30682   }
       
 30683 
       
 30684 fetch_out:
       
 30685   if( pPage && iKey>pCache->iMaxKey ){
       
 30686     pCache->iMaxKey = iKey;
       
 30687   }
       
 30688   if( createFlag==1 ) sqlite3EndBenignMalloc();
       
 30689   pcache1LeaveMutex();
       
 30690   return (pPage ? PGHDR1_TO_PAGE(pPage) : 0);
       
 30691 }
       
 30692 
       
 30693 
       
 30694 /*
       
 30695 ** Implementation of the sqlite3_pcache.xUnpin method.
       
 30696 **
       
 30697 ** Mark a page as unpinned (eligible for asynchronous recycling).
       
 30698 */
       
 30699 static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){
       
 30700   PCache1 *pCache = (PCache1 *)p;
       
 30701   PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
       
 30702  
       
 30703   assert( pPage->pCache==pCache );
       
 30704   pcache1EnterMutex();
       
 30705 
       
 30706   /* It is an error to call this function if the page is already 
       
 30707   ** part of the global LRU list.
       
 30708   */
       
 30709   assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
       
 30710   assert( pcache1.pLruHead!=pPage && pcache1.pLruTail!=pPage );
       
 30711 
       
 30712   if( reuseUnlikely || pcache1.nCurrentPage>pcache1.nMaxPage ){
       
 30713     pcache1RemoveFromHash(pPage);
       
 30714     pcache1FreePage(pPage);
       
 30715   }else{
       
 30716     /* Add the page to the global LRU list. Normally, the page is added to
       
 30717     ** the head of the list (last page to be recycled). However, if the 
       
 30718     ** reuseUnlikely flag passed to this function is true, the page is added
       
 30719     ** to the tail of the list (first page to be recycled).
       
 30720     */
       
 30721     if( pcache1.pLruHead ){
       
 30722       pcache1.pLruHead->pLruPrev = pPage;
       
 30723       pPage->pLruNext = pcache1.pLruHead;
       
 30724       pcache1.pLruHead = pPage;
       
 30725     }else{
       
 30726       pcache1.pLruTail = pPage;
       
 30727       pcache1.pLruHead = pPage;
       
 30728     }
       
 30729     pCache->nRecyclable++;
       
 30730   }
       
 30731 
       
 30732   pcache1LeaveMutex();
       
 30733 }
       
 30734 
       
 30735 /*
       
 30736 ** Implementation of the sqlite3_pcache.xRekey method. 
       
 30737 */
       
 30738 static void pcache1Rekey(
       
 30739   sqlite3_pcache *p,
       
 30740   void *pPg,
       
 30741   unsigned int iOld,
       
 30742   unsigned int iNew
       
 30743 ){
       
 30744   PCache1 *pCache = (PCache1 *)p;
       
 30745   PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
       
 30746   PgHdr1 **pp;
       
 30747   unsigned int h; 
       
 30748   assert( pPage->iKey==iOld );
       
 30749   assert( pPage->pCache==pCache );
       
 30750 
       
 30751   pcache1EnterMutex();
       
 30752 
       
 30753   h = iOld%pCache->nHash;
       
 30754   pp = &pCache->apHash[h];
       
 30755   while( (*pp)!=pPage ){
       
 30756     pp = &(*pp)->pNext;
       
 30757   }
       
 30758   *pp = pPage->pNext;
       
 30759 
       
 30760   h = iNew%pCache->nHash;
       
 30761   pPage->iKey = iNew;
       
 30762   pPage->pNext = pCache->apHash[h];
       
 30763   pCache->apHash[h] = pPage;
       
 30764 
       
 30765   /* The xRekey() interface is only used to move pages earlier in the
       
 30766   ** database file (in order to move all free pages to the end of the
       
 30767   ** file where they can be truncated off.)  Hence, it is not possible
       
 30768   ** for the new page number to be greater than the largest previously
       
 30769   ** fetched page.  But we retain the following test in case xRekey()
       
 30770   ** begins to be used in different ways in the future.
       
 30771   */
       
 30772   if( NEVER(iNew>pCache->iMaxKey) ){
       
 30773     pCache->iMaxKey = iNew;
       
 30774   }
       
 30775 
       
 30776   pcache1LeaveMutex();
       
 30777 }
       
 30778 
       
 30779 /*
       
 30780 ** Implementation of the sqlite3_pcache.xTruncate method. 
       
 30781 **
       
 30782 ** Discard all unpinned pages in the cache with a page number equal to
       
 30783 ** or greater than parameter iLimit. Any pinned pages with a page number
       
 30784 ** equal to or greater than iLimit are implicitly unpinned.
       
 30785 */
       
 30786 static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
       
 30787   PCache1 *pCache = (PCache1 *)p;
       
 30788   pcache1EnterMutex();
       
 30789   if( iLimit<=pCache->iMaxKey ){
       
 30790     pcache1TruncateUnsafe(pCache, iLimit);
       
 30791     pCache->iMaxKey = iLimit-1;
       
 30792   }
       
 30793   pcache1LeaveMutex();
       
 30794 }
       
 30795 
       
 30796 /*
       
 30797 ** Implementation of the sqlite3_pcache.xDestroy method. 
       
 30798 **
       
 30799 ** Destroy a cache allocated using pcache1Create().
       
 30800 */
       
 30801 static void pcache1Destroy(sqlite3_pcache *p){
       
 30802   PCache1 *pCache = (PCache1 *)p;
       
 30803   pcache1EnterMutex();
       
 30804   pcache1TruncateUnsafe(pCache, 0);
       
 30805   pcache1.nMaxPage -= pCache->nMax;
       
 30806   pcache1.nMinPage -= pCache->nMin;
       
 30807   pcache1EnforceMaxPage();
       
 30808   pcache1LeaveMutex();
       
 30809   sqlite3_free(pCache->apHash);
       
 30810   sqlite3_free(pCache);
       
 30811 }
       
 30812 
       
 30813 /*
       
 30814 ** This function is called during initialization (sqlite3_initialize()) to
       
 30815 ** install the default pluggable cache module, assuming the user has not
       
 30816 ** already provided an alternative.
       
 30817 */
       
 30818 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
       
 30819   static sqlite3_pcache_methods defaultMethods = {
       
 30820     0,                       /* pArg */
       
 30821     pcache1Init,             /* xInit */
       
 30822     pcache1Shutdown,         /* xShutdown */
       
 30823     pcache1Create,           /* xCreate */
       
 30824     pcache1Cachesize,        /* xCachesize */
       
 30825     pcache1Pagecount,        /* xPagecount */
       
 30826     pcache1Fetch,            /* xFetch */
       
 30827     pcache1Unpin,            /* xUnpin */
       
 30828     pcache1Rekey,            /* xRekey */
       
 30829     pcache1Truncate,         /* xTruncate */
       
 30830     pcache1Destroy           /* xDestroy */
       
 30831   };
       
 30832   sqlite3_config(SQLITE_CONFIG_PCACHE, &defaultMethods);
       
 30833 }
       
 30834 
       
 30835 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
       
 30836 /*
       
 30837 ** This function is called to free superfluous dynamically allocated memory
       
 30838 ** held by the pager system. Memory in use by any SQLite pager allocated
       
 30839 ** by the current thread may be sqlite3_free()ed.
       
 30840 **
       
 30841 ** nReq is the number of bytes of memory required. Once this much has
       
 30842 ** been released, the function returns. The return value is the total number 
       
 30843 ** of bytes of memory released.
       
 30844 */
       
 30845 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
       
 30846   int nFree = 0;
       
 30847   if( pcache1.pStart==0 ){
       
 30848     PgHdr1 *p;
       
 30849     pcache1EnterMutex();
       
 30850     while( (nReq<0 || nFree<nReq) && (p=pcache1.pLruTail) ){
       
 30851       nFree += sqlite3MallocSize(PGHDR1_TO_PAGE(p));
       
 30852       pcache1PinPage(p);
       
 30853       pcache1RemoveFromHash(p);
       
 30854       pcache1FreePage(p);
       
 30855     }
       
 30856     pcache1LeaveMutex();
       
 30857   }
       
 30858   return nFree;
       
 30859 }
       
 30860 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
       
 30861 
       
 30862 #ifdef SQLITE_TEST
       
 30863 /*
       
 30864 ** This function is used by test procedures to inspect the internal state
       
 30865 ** of the global cache.
       
 30866 */
       
 30867 SQLITE_PRIVATE void sqlite3PcacheStats(
       
 30868   int *pnCurrent,      /* OUT: Total number of pages cached */
       
 30869   int *pnMax,          /* OUT: Global maximum cache size */
       
 30870   int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
       
 30871   int *pnRecyclable    /* OUT: Total number of pages available for recycling */
       
 30872 ){
       
 30873   PgHdr1 *p;
       
 30874   int nRecyclable = 0;
       
 30875   for(p=pcache1.pLruHead; p; p=p->pLruNext){
       
 30876     nRecyclable++;
       
 30877   }
       
 30878   *pnCurrent = pcache1.nCurrentPage;
       
 30879   *pnMax = pcache1.nMaxPage;
       
 30880   *pnMin = pcache1.nMinPage;
       
 30881   *pnRecyclable = nRecyclable;
       
 30882 }
       
 30883 #endif
       
 30884 
       
 30885 /************** End of pcache1.c *********************************************/
       
 30886 /************** Begin file rowset.c ******************************************/
       
 30887 /*
       
 30888 ** 2008 December 3
       
 30889 **
       
 30890 ** The author disclaims copyright to this source code.  In place of
       
 30891 ** a legal notice, here is a blessing:
       
 30892 **
       
 30893 **    May you do good and not evil.
       
 30894 **    May you find forgiveness for yourself and forgive others.
       
 30895 **    May you share freely, never taking more than you give.
       
 30896 **
       
 30897 *************************************************************************
       
 30898 **
       
 30899 ** This module implements an object we call a "RowSet".
       
 30900 **
       
 30901 ** The RowSet object is a collection of rowids.  Rowids
       
 30902 ** are inserted into the RowSet in an arbitrary order.  Inserts
       
 30903 ** can be intermixed with tests to see if a given rowid has been
       
 30904 ** previously inserted into the RowSet.
       
 30905 **
       
 30906 ** After all inserts are finished, it is possible to extract the
       
 30907 ** elements of the RowSet in sorted order.  Once this extraction
       
 30908 ** process has started, no new elements may be inserted.
       
 30909 **
       
 30910 ** Hence, the primitive operations for a RowSet are:
       
 30911 **
       
 30912 **    CREATE
       
 30913 **    INSERT
       
 30914 **    TEST
       
 30915 **    SMALLEST
       
 30916 **    DESTROY
       
 30917 **
       
 30918 ** The CREATE and DESTROY primitives are the constructor and destructor,
       
 30919 ** obviously.  The INSERT primitive adds a new element to the RowSet.
       
 30920 ** TEST checks to see if an element is already in the RowSet.  SMALLEST
       
 30921 ** extracts the least value from the RowSet.
       
 30922 **
       
 30923 ** The INSERT primitive might allocate additional memory.  Memory is
       
 30924 ** allocated in chunks so most INSERTs do no allocation.  There is an 
       
 30925 ** upper bound on the size of allocated memory.  No memory is freed
       
 30926 ** until DESTROY.
       
 30927 **
       
 30928 ** The TEST primitive includes a "batch" number.  The TEST primitive
       
 30929 ** will only see elements that were inserted before the last change
       
 30930 ** in the batch number.  In other words, if an INSERT occurs between
       
 30931 ** two TESTs where the TESTs have the same batch nubmer, then the
       
 30932 ** value added by the INSERT will not be visible to the second TEST.
       
 30933 ** The initial batch number is zero, so if the very first TEST contains
       
 30934 ** a non-zero batch number, it will see all prior INSERTs.
       
 30935 **
       
 30936 ** No INSERTs may occurs after a SMALLEST.  An assertion will fail if
       
 30937 ** that is attempted.
       
 30938 **
       
 30939 ** The cost of an INSERT is roughly constant.  (Sometime new memory
       
 30940 ** has to be allocated on an INSERT.)  The cost of a TEST with a new
       
 30941 ** batch number is O(NlogN) where N is the number of elements in the RowSet.
       
 30942 ** The cost of a TEST using the same batch number is O(logN).  The cost
       
 30943 ** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
       
 30944 ** primitives are constant time.  The cost of DESTROY is O(N).
       
 30945 **
       
 30946 ** There is an added cost of O(N) when switching between TEST and
       
 30947 ** SMALLEST primitives.
       
 30948 **
       
 30949 ** $Id: rowset.c,v 1.7 2009/05/22 01:00:13 drh Exp $
       
 30950 */
       
 30951 
       
 30952 
       
 30953 /*
       
 30954 ** Target size for allocation chunks.
       
 30955 */
       
 30956 #define ROWSET_ALLOCATION_SIZE 1024
       
 30957 
       
 30958 /*
       
 30959 ** The number of rowset entries per allocation chunk.
       
 30960 */
       
 30961 #define ROWSET_ENTRY_PER_CHUNK  \
       
 30962                        ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
       
 30963 
       
 30964 /*
       
 30965 ** Each entry in a RowSet is an instance of the following object.
       
 30966 */
       
 30967 struct RowSetEntry {            
       
 30968   i64 v;                        /* ROWID value for this entry */
       
 30969   struct RowSetEntry *pRight;   /* Right subtree (larger entries) or list */
       
 30970   struct RowSetEntry *pLeft;    /* Left subtree (smaller entries) */
       
 30971 };
       
 30972 
       
 30973 /*
       
 30974 ** RowSetEntry objects are allocated in large chunks (instances of the
       
 30975 ** following structure) to reduce memory allocation overhead.  The
       
 30976 ** chunks are kept on a linked list so that they can be deallocated
       
 30977 ** when the RowSet is destroyed.
       
 30978 */
       
 30979 struct RowSetChunk {
       
 30980   struct RowSetChunk *pNextChunk;        /* Next chunk on list of them all */
       
 30981   struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
       
 30982 };
       
 30983 
       
 30984 /*
       
 30985 ** A RowSet in an instance of the following structure.
       
 30986 **
       
 30987 ** A typedef of this structure if found in sqliteInt.h.
       
 30988 */
       
 30989 struct RowSet {
       
 30990   struct RowSetChunk *pChunk;    /* List of all chunk allocations */
       
 30991   sqlite3 *db;                   /* The database connection */
       
 30992   struct RowSetEntry *pEntry;    /* List of entries using pRight */
       
 30993   struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
       
 30994   struct RowSetEntry *pFresh;    /* Source of new entry objects */
       
 30995   struct RowSetEntry *pTree;     /* Binary tree of entries */
       
 30996   u16 nFresh;                    /* Number of objects on pFresh */
       
 30997   u8 isSorted;                   /* True if pEntry is sorted */
       
 30998   u8 iBatch;                     /* Current insert batch */
       
 30999 };
       
 31000 
       
 31001 /*
       
 31002 ** Turn bulk memory into a RowSet object.  N bytes of memory
       
 31003 ** are available at pSpace.  The db pointer is used as a memory context
       
 31004 ** for any subsequent allocations that need to occur.
       
 31005 ** Return a pointer to the new RowSet object.
       
 31006 **
       
 31007 ** It must be the case that N is sufficient to make a Rowset.  If not
       
 31008 ** an assertion fault occurs.
       
 31009 ** 
       
 31010 ** If N is larger than the minimum, use the surplus as an initial
       
 31011 ** allocation of entries available to be filled.
       
 31012 */
       
 31013 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
       
 31014   RowSet *p;
       
 31015   assert( N >= ROUND8(sizeof(*p)) );
       
 31016   p = pSpace;
       
 31017   p->pChunk = 0;
       
 31018   p->db = db;
       
 31019   p->pEntry = 0;
       
 31020   p->pLast = 0;
       
 31021   p->pTree = 0;
       
 31022   p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
       
 31023   p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
       
 31024   p->isSorted = 1;
       
 31025   p->iBatch = 0;
       
 31026   return p;
       
 31027 }
       
 31028 
       
 31029 /*
       
 31030 ** Deallocate all chunks from a RowSet.  This frees all memory that
       
 31031 ** the RowSet has allocated over its lifetime.  This routine is
       
 31032 ** the destructor for the RowSet.
       
 31033 */
       
 31034 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
       
 31035   struct RowSetChunk *pChunk, *pNextChunk;
       
 31036   for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
       
 31037     pNextChunk = pChunk->pNextChunk;
       
 31038     sqlite3DbFree(p->db, pChunk);
       
 31039   }
       
 31040   p->pChunk = 0;
       
 31041   p->nFresh = 0;
       
 31042   p->pEntry = 0;
       
 31043   p->pLast = 0;
       
 31044   p->pTree = 0;
       
 31045   p->isSorted = 1;
       
 31046 }
       
 31047 
       
 31048 /*
       
 31049 ** Insert a new value into a RowSet.
       
 31050 **
       
 31051 ** The mallocFailed flag of the database connection is set if a
       
 31052 ** memory allocation fails.
       
 31053 */
       
 31054 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
       
 31055   struct RowSetEntry *pEntry;  /* The new entry */
       
 31056   struct RowSetEntry *pLast;   /* The last prior entry */
       
 31057   assert( p!=0 );
       
 31058   if( p->nFresh==0 ){
       
 31059     struct RowSetChunk *pNew;
       
 31060     pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
       
 31061     if( pNew==0 ){
       
 31062       return;
       
 31063     }
       
 31064     pNew->pNextChunk = p->pChunk;
       
 31065     p->pChunk = pNew;
       
 31066     p->pFresh = pNew->aEntry;
       
 31067     p->nFresh = ROWSET_ENTRY_PER_CHUNK;
       
 31068   }
       
 31069   pEntry = p->pFresh++;
       
 31070   p->nFresh--;
       
 31071   pEntry->v = rowid;
       
 31072   pEntry->pRight = 0;
       
 31073   pLast = p->pLast;
       
 31074   if( pLast ){
       
 31075     if( p->isSorted && rowid<=pLast->v ){
       
 31076       p->isSorted = 0;
       
 31077     }
       
 31078     pLast->pRight = pEntry;
       
 31079   }else{
       
 31080     assert( p->pEntry==0 ); /* Fires if INSERT after SMALLEST */
       
 31081     p->pEntry = pEntry;
       
 31082   }
       
 31083   p->pLast = pEntry;
       
 31084 }
       
 31085 
       
 31086 /*
       
 31087 ** Merge two lists of RowSetEntry objects.  Remove duplicates.
       
 31088 **
       
 31089 ** The input lists are connected via pRight pointers and are 
       
 31090 ** assumed to each already be in sorted order.
       
 31091 */
       
 31092 static struct RowSetEntry *rowSetMerge(
       
 31093   struct RowSetEntry *pA,    /* First sorted list to be merged */
       
 31094   struct RowSetEntry *pB     /* Second sorted list to be merged */
       
 31095 ){
       
 31096   struct RowSetEntry head;
       
 31097   struct RowSetEntry *pTail;
       
 31098 
       
 31099   pTail = &head;
       
 31100   while( pA && pB ){
       
 31101     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
       
 31102     assert( pB->pRight==0 || pB->v<=pB->pRight->v );
       
 31103     if( pA->v<pB->v ){
       
 31104       pTail->pRight = pA;
       
 31105       pA = pA->pRight;
       
 31106       pTail = pTail->pRight;
       
 31107     }else if( pB->v<pA->v ){
       
 31108       pTail->pRight = pB;
       
 31109       pB = pB->pRight;
       
 31110       pTail = pTail->pRight;
       
 31111     }else{
       
 31112       pA = pA->pRight;
       
 31113     }
       
 31114   }
       
 31115   if( pA ){
       
 31116     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
       
 31117     pTail->pRight = pA;
       
 31118   }else{
       
 31119     assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
       
 31120     pTail->pRight = pB;
       
 31121   }
       
 31122   return head.pRight;
       
 31123 }
       
 31124 
       
 31125 /*
       
 31126 ** Sort all elements on the pEntry list of the RowSet into ascending order.
       
 31127 */ 
       
 31128 static void rowSetSort(RowSet *p){
       
 31129   unsigned int i;
       
 31130   struct RowSetEntry *pEntry;
       
 31131   struct RowSetEntry *aBucket[40];
       
 31132 
       
 31133   assert( p->isSorted==0 );
       
 31134   memset(aBucket, 0, sizeof(aBucket));
       
 31135   while( p->pEntry ){
       
 31136     pEntry = p->pEntry;
       
 31137     p->pEntry = pEntry->pRight;
       
 31138     pEntry->pRight = 0;
       
 31139     for(i=0; aBucket[i]; i++){
       
 31140       pEntry = rowSetMerge(aBucket[i], pEntry);
       
 31141       aBucket[i] = 0;
       
 31142     }
       
 31143     aBucket[i] = pEntry;
       
 31144   }
       
 31145   pEntry = 0;
       
 31146   for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
       
 31147     pEntry = rowSetMerge(pEntry, aBucket[i]);
       
 31148   }
       
 31149   p->pEntry = pEntry;
       
 31150   p->pLast = 0;
       
 31151   p->isSorted = 1;
       
 31152 }
       
 31153 
       
 31154 
       
 31155 /*
       
 31156 ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
       
 31157 ** Convert this tree into a linked list connected by the pRight pointers
       
 31158 ** and return pointers to the first and last elements of the new list.
       
 31159 */
       
 31160 static void rowSetTreeToList(
       
 31161   struct RowSetEntry *pIn,         /* Root of the input tree */
       
 31162   struct RowSetEntry **ppFirst,    /* Write head of the output list here */
       
 31163   struct RowSetEntry **ppLast      /* Write tail of the output list here */
       
 31164 ){
       
 31165   assert( pIn!=0 );
       
 31166   if( pIn->pLeft ){
       
 31167     struct RowSetEntry *p;
       
 31168     rowSetTreeToList(pIn->pLeft, ppFirst, &p);
       
 31169     p->pRight = pIn;
       
 31170   }else{
       
 31171     *ppFirst = pIn;
       
 31172   }
       
 31173   if( pIn->pRight ){
       
 31174     rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
       
 31175   }else{
       
 31176     *ppLast = pIn;
       
 31177   }
       
 31178   assert( (*ppLast)->pRight==0 );
       
 31179 }
       
 31180 
       
 31181 
       
 31182 /*
       
 31183 ** Convert a sorted list of elements (connected by pRight) into a binary
       
 31184 ** tree with depth of iDepth.  A depth of 1 means the tree contains a single
       
 31185 ** node taken from the head of *ppList.  A depth of 2 means a tree with
       
 31186 ** three nodes.  And so forth.
       
 31187 **
       
 31188 ** Use as many entries from the input list as required and update the
       
 31189 ** *ppList to point to the unused elements of the list.  If the input
       
 31190 ** list contains too few elements, then construct an incomplete tree
       
 31191 ** and leave *ppList set to NULL.
       
 31192 **
       
 31193 ** Return a pointer to the root of the constructed binary tree.
       
 31194 */
       
 31195 static struct RowSetEntry *rowSetNDeepTree(
       
 31196   struct RowSetEntry **ppList,
       
 31197   int iDepth
       
 31198 ){
       
 31199   struct RowSetEntry *p;         /* Root of the new tree */
       
 31200   struct RowSetEntry *pLeft;     /* Left subtree */
       
 31201   if( *ppList==0 ){
       
 31202     return 0;
       
 31203   }
       
 31204   if( iDepth==1 ){
       
 31205     p = *ppList;
       
 31206     *ppList = p->pRight;
       
 31207     p->pLeft = p->pRight = 0;
       
 31208     return p;
       
 31209   }
       
 31210   pLeft = rowSetNDeepTree(ppList, iDepth-1);
       
 31211   p = *ppList;
       
 31212   if( p==0 ){
       
 31213     return pLeft;
       
 31214   }
       
 31215   p->pLeft = pLeft;
       
 31216   *ppList = p->pRight;
       
 31217   p->pRight = rowSetNDeepTree(ppList, iDepth-1);
       
 31218   return p;
       
 31219 }
       
 31220 
       
 31221 /*
       
 31222 ** Convert a sorted list of elements into a binary tree. Make the tree
       
 31223 ** as deep as it needs to be in order to contain the entire list.
       
 31224 */
       
 31225 static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
       
 31226   int iDepth;           /* Depth of the tree so far */
       
 31227   struct RowSetEntry *p;       /* Current tree root */
       
 31228   struct RowSetEntry *pLeft;   /* Left subtree */
       
 31229 
       
 31230   assert( pList!=0 );
       
 31231   p = pList;
       
 31232   pList = p->pRight;
       
 31233   p->pLeft = p->pRight = 0;
       
 31234   for(iDepth=1; pList; iDepth++){
       
 31235     pLeft = p;
       
 31236     p = pList;
       
 31237     pList = p->pRight;
       
 31238     p->pLeft = pLeft;
       
 31239     p->pRight = rowSetNDeepTree(&pList, iDepth);
       
 31240   }
       
 31241   return p;
       
 31242 }
       
 31243 
       
 31244 /*
       
 31245 ** Convert the list in p->pEntry into a sorted list if it is not
       
 31246 ** sorted already.  If there is a binary tree on p->pTree, then
       
 31247 ** convert it into a list too and merge it into the p->pEntry list.
       
 31248 */
       
 31249 static void rowSetToList(RowSet *p){
       
 31250   if( !p->isSorted ){
       
 31251     rowSetSort(p);
       
 31252   }
       
 31253   if( p->pTree ){
       
 31254     struct RowSetEntry *pHead, *pTail;
       
 31255     rowSetTreeToList(p->pTree, &pHead, &pTail);
       
 31256     p->pTree = 0;
       
 31257     p->pEntry = rowSetMerge(p->pEntry, pHead);
       
 31258   }
       
 31259 }
       
 31260 
       
 31261 /*
       
 31262 ** Extract the smallest element from the RowSet.
       
 31263 ** Write the element into *pRowid.  Return 1 on success.  Return
       
 31264 ** 0 if the RowSet is already empty.
       
 31265 **
       
 31266 ** After this routine has been called, the sqlite3RowSetInsert()
       
 31267 ** routine may not be called again.  
       
 31268 */
       
 31269 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
       
 31270   rowSetToList(p);
       
 31271   if( p->pEntry ){
       
 31272     *pRowid = p->pEntry->v;
       
 31273     p->pEntry = p->pEntry->pRight;
       
 31274     if( p->pEntry==0 ){
       
 31275       sqlite3RowSetClear(p);
       
 31276     }
       
 31277     return 1;
       
 31278   }else{
       
 31279     return 0;
       
 31280   }
       
 31281 }
       
 31282 
       
 31283 /*
       
 31284 ** Check to see if element iRowid was inserted into the the rowset as
       
 31285 ** part of any insert batch prior to iBatch.  Return 1 or 0.
       
 31286 */
       
 31287 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
       
 31288   struct RowSetEntry *p;
       
 31289   if( iBatch!=pRowSet->iBatch ){
       
 31290     if( pRowSet->pEntry ){
       
 31291       rowSetToList(pRowSet);
       
 31292       pRowSet->pTree = rowSetListToTree(pRowSet->pEntry);
       
 31293       pRowSet->pEntry = 0;
       
 31294       pRowSet->pLast = 0;
       
 31295     }
       
 31296     pRowSet->iBatch = iBatch;
       
 31297   }
       
 31298   p = pRowSet->pTree;
       
 31299   while( p ){
       
 31300     if( p->v<iRowid ){
       
 31301       p = p->pRight;
       
 31302     }else if( p->v>iRowid ){
       
 31303       p = p->pLeft;
       
 31304     }else{
       
 31305       return 1;
       
 31306     }
       
 31307   }
       
 31308   return 0;
       
 31309 }
       
 31310 
       
 31311 /************** End of rowset.c **********************************************/
       
 31312 /************** Begin file pager.c *******************************************/
       
 31313 /*
       
 31314 ** 2001 September 15
       
 31315 **
       
 31316 ** The author disclaims copyright to this source code.  In place of
       
 31317 ** a legal notice, here is a blessing:
       
 31318 **
       
 31319 **    May you do good and not evil.
       
 31320 **    May you find forgiveness for yourself and forgive others.
       
 31321 **    May you share freely, never taking more than you give.
       
 31322 **
       
 31323 *************************************************************************
       
 31324 ** This is the implementation of the page cache subsystem or "pager".
       
 31325 ** 
       
 31326 ** The pager is used to access a database disk file.  It implements
       
 31327 ** atomic commit and rollback through the use of a journal file that
       
 31328 ** is separate from the database file.  The pager also implements file
       
 31329 ** locking to prevent two processes from writing the same database
       
 31330 ** file simultaneously, or one process from reading the database while
       
 31331 ** another is writing.
       
 31332 **
       
 31333 ** @(#) $Id: pager.c,v 1.629 2009/08/10 17:48:57 drh Exp $
       
 31334 */
       
 31335 #ifndef SQLITE_OMIT_DISKIO
       
 31336 
       
 31337 /*
       
 31338 ** Macros for troubleshooting.  Normally turned off
       
 31339 */
       
 31340 #if 0
       
 31341 int sqlite3PagerTrace=1;  /* True to enable tracing */
       
 31342 #define sqlite3DebugPrintf printf
       
 31343 #define PAGERTRACE(X)     if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
       
 31344 #else
       
 31345 #define PAGERTRACE(X)
       
 31346 #endif
       
 31347 
       
 31348 /*
       
 31349 ** The following two macros are used within the PAGERTRACE() macros above
       
 31350 ** to print out file-descriptors. 
       
 31351 **
       
 31352 ** PAGERID() takes a pointer to a Pager struct as its argument. The
       
 31353 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
       
 31354 ** struct as its argument.
       
 31355 */
       
 31356 #define PAGERID(p) ((int)(p->fd))
       
 31357 #define FILEHANDLEID(fd) ((int)fd)
       
 31358 
       
 31359 /*
       
 31360 ** The page cache as a whole is always in one of the following
       
 31361 ** states:
       
 31362 **
       
 31363 **   PAGER_UNLOCK        The page cache is not currently reading or 
       
 31364 **                       writing the database file.  There is no
       
 31365 **                       data held in memory.  This is the initial
       
 31366 **                       state.
       
 31367 **
       
 31368 **   PAGER_SHARED        The page cache is reading the database.
       
 31369 **                       Writing is not permitted.  There can be
       
 31370 **                       multiple readers accessing the same database
       
 31371 **                       file at the same time.
       
 31372 **
       
 31373 **   PAGER_RESERVED      This process has reserved the database for writing
       
 31374 **                       but has not yet made any changes.  Only one process
       
 31375 **                       at a time can reserve the database.  The original
       
 31376 **                       database file has not been modified so other
       
 31377 **                       processes may still be reading the on-disk
       
 31378 **                       database file.
       
 31379 **
       
 31380 **   PAGER_EXCLUSIVE     The page cache is writing the database.
       
 31381 **                       Access is exclusive.  No other processes or
       
 31382 **                       threads can be reading or writing while one
       
 31383 **                       process is writing.
       
 31384 **
       
 31385 **   PAGER_SYNCED        The pager moves to this state from PAGER_EXCLUSIVE
       
 31386 **                       after all dirty pages have been written to the
       
 31387 **                       database file and the file has been synced to
       
 31388 **                       disk. All that remains to do is to remove or
       
 31389 **                       truncate the journal file and the transaction 
       
 31390 **                       will be committed.
       
 31391 **
       
 31392 ** The page cache comes up in PAGER_UNLOCK.  The first time a
       
 31393 ** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
       
 31394 ** After all pages have been released using sqlite_page_unref(),
       
 31395 ** the state transitions back to PAGER_UNLOCK.  The first time
       
 31396 ** that sqlite3PagerWrite() is called, the state transitions to
       
 31397 ** PAGER_RESERVED.  (Note that sqlite3PagerWrite() can only be
       
 31398 ** called on an outstanding page which means that the pager must
       
 31399 ** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
       
 31400 ** PAGER_RESERVED means that there is an open rollback journal.
       
 31401 ** The transition to PAGER_EXCLUSIVE occurs before any changes
       
 31402 ** are made to the database file, though writes to the rollback
       
 31403 ** journal occurs with just PAGER_RESERVED.  After an sqlite3PagerRollback()
       
 31404 ** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
       
 31405 ** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
       
 31406 */
       
 31407 #define PAGER_UNLOCK      0
       
 31408 #define PAGER_SHARED      1   /* same as SHARED_LOCK */
       
 31409 #define PAGER_RESERVED    2   /* same as RESERVED_LOCK */
       
 31410 #define PAGER_EXCLUSIVE   4   /* same as EXCLUSIVE_LOCK */
       
 31411 #define PAGER_SYNCED      5
       
 31412 
       
 31413 /*
       
 31414 ** A macro used for invoking the codec if there is one
       
 31415 */
       
 31416 #ifdef SQLITE_HAS_CODEC
       
 31417 # define CODEC1(P,D,N,X,E) \
       
 31418     if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
       
 31419 # define CODEC2(P,D,N,X,E,O) \
       
 31420     if( P->xCodec==0 ){ O=(char*)D; }else \
       
 31421     if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
       
 31422 #else
       
 31423 # define CODEC1(P,D,N,X,E)   /* NO-OP */
       
 31424 # define CODEC2(P,D,N,X,E,O) O=(char*)D
       
 31425 #endif
       
 31426 
       
 31427 /*
       
 31428 ** The maximum allowed sector size. 64KiB. If the xSectorsize() method 
       
 31429 ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
       
 31430 ** This could conceivably cause corruption following a power failure on
       
 31431 ** such a system. This is currently an undocumented limit.
       
 31432 */
       
 31433 #define MAX_SECTOR_SIZE 0x10000
       
 31434 
       
 31435 /*
       
 31436 ** An instance of the following structure is allocated for each active
       
 31437 ** savepoint and statement transaction in the system. All such structures
       
 31438 ** are stored in the Pager.aSavepoint[] array, which is allocated and
       
 31439 ** resized using sqlite3Realloc().
       
 31440 **
       
 31441 ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
       
 31442 ** set to 0. If a journal-header is written into the main journal while
       
 31443 ** the savepoint is active, then iHdrOffset is set to the byte offset 
       
 31444 ** immediately following the last journal record written into the main
       
 31445 ** journal before the journal-header. This is required during savepoint
       
 31446 ** rollback (see pagerPlaybackSavepoint()).
       
 31447 */
       
 31448 typedef struct PagerSavepoint PagerSavepoint;
       
 31449 struct PagerSavepoint {
       
 31450   i64 iOffset;                 /* Starting offset in main journal */
       
 31451   i64 iHdrOffset;              /* See above */
       
 31452   Bitvec *pInSavepoint;        /* Set of pages in this savepoint */
       
 31453   Pgno nOrig;                  /* Original number of pages in file */
       
 31454   Pgno iSubRec;                /* Index of first record in sub-journal */
       
 31455 };
       
 31456 
       
 31457 /*
       
 31458 ** A open page cache is an instance of the following structure.
       
 31459 **
       
 31460 ** errCode
       
 31461 **
       
 31462 **   Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
       
 31463 **   or SQLITE_FULL. Once one of the first three errors occurs, it persists
       
 31464 **   and is returned as the result of every major pager API call.  The
       
 31465 **   SQLITE_FULL return code is slightly different. It persists only until the
       
 31466 **   next successful rollback is performed on the pager cache. Also,
       
 31467 **   SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
       
 31468 **   APIs, they may still be used successfully.
       
 31469 **
       
 31470 ** dbSizeValid, dbSize, dbOrigSize, dbFileSize
       
 31471 **
       
 31472 **   Managing the size of the database file in pages is a little complicated.
       
 31473 **   The variable Pager.dbSize contains the number of pages that the database
       
 31474 **   image currently contains. As the database image grows or shrinks this
       
 31475 **   variable is updated. The variable Pager.dbFileSize contains the number
       
 31476 **   of pages in the database file. This may be different from Pager.dbSize
       
 31477 **   if some pages have been appended to the database image but not yet written
       
 31478 **   out from the cache to the actual file on disk. Or if the image has been
       
 31479 **   truncated by an incremental-vacuum operation. The Pager.dbOrigSize variable
       
 31480 **   contains the number of pages in the database image when the current
       
 31481 **   transaction was opened. The contents of all three of these variables is
       
 31482 **   only guaranteed to be correct if the boolean Pager.dbSizeValid is true.
       
 31483 **
       
 31484 **   TODO: Under what conditions is dbSizeValid set? Cleared?
       
 31485 **
       
 31486 ** changeCountDone
       
 31487 **
       
 31488 **   This boolean variable is used to make sure that the change-counter 
       
 31489 **   (the 4-byte header field at byte offset 24 of the database file) is 
       
 31490 **   not updated more often than necessary. 
       
 31491 **
       
 31492 **   It is set to true when the change-counter field is updated, which 
       
 31493 **   can only happen if an exclusive lock is held on the database file.
       
 31494 **   It is cleared (set to false) whenever an exclusive lock is 
       
 31495 **   relinquished on the database file. Each time a transaction is committed,
       
 31496 **   The changeCountDone flag is inspected. If it is true, the work of
       
 31497 **   updating the change-counter is omitted for the current transaction.
       
 31498 **
       
 31499 **   This mechanism means that when running in exclusive mode, a connection 
       
 31500 **   need only update the change-counter once, for the first transaction
       
 31501 **   committed.
       
 31502 **
       
 31503 ** dbModified
       
 31504 **
       
 31505 **   The dbModified flag is set whenever a database page is dirtied.
       
 31506 **   It is cleared at the end of each transaction.
       
 31507 **
       
 31508 **   It is used when committing or otherwise ending a transaction. If
       
 31509 **   the dbModified flag is clear then less work has to be done.
       
 31510 **
       
 31511 ** journalStarted
       
 31512 **
       
 31513 **   This flag is set whenever the the main journal is synced. 
       
 31514 **
       
 31515 **   The point of this flag is that it must be set after the 
       
 31516 **   first journal header in a journal file has been synced to disk.
       
 31517 **   After this has happened, new pages appended to the database 
       
 31518 **   do not need the PGHDR_NEED_SYNC flag set, as they do not need
       
 31519 **   to wait for a journal sync before they can be written out to
       
 31520 **   the database file (see function pager_write()).
       
 31521 **   
       
 31522 ** setMaster
       
 31523 **
       
 31524 **   This variable is used to ensure that the master journal file name
       
 31525 **   (if any) is only written into the journal file once.
       
 31526 **
       
 31527 **   When committing a transaction, the master journal file name (if any)
       
 31528 **   may be written into the journal file while the pager is still in
       
 31529 **   PAGER_RESERVED state (see CommitPhaseOne() for the action). It
       
 31530 **   then attempts to upgrade to an exclusive lock. If this attempt
       
 31531 **   fails, then SQLITE_BUSY may be returned to the user and the user
       
 31532 **   may attempt to commit the transaction again later (calling
       
 31533 **   CommitPhaseOne() again). This flag is used to ensure that the 
       
 31534 **   master journal name is only written to the journal file the first
       
 31535 **   time CommitPhaseOne() is called.
       
 31536 **
       
 31537 ** doNotSync
       
 31538 **
       
 31539 **   This variable is set and cleared by sqlite3PagerWrite().
       
 31540 **
       
 31541 ** needSync
       
 31542 **
       
 31543 **   TODO: It might be easier to set this variable in writeJournalHdr()
       
 31544 **   and writeMasterJournal() only. Change its meaning to "unsynced data
       
 31545 **   has been written to the journal".
       
 31546 **
       
 31547 ** subjInMemory
       
 31548 **
       
 31549 **   This is a boolean variable. If true, then any required sub-journal
       
 31550 **   is opened as an in-memory journal file. If false, then in-memory
       
 31551 **   sub-journals are only used for in-memory pager files.
       
 31552 */
       
 31553 struct Pager {
       
 31554   sqlite3_vfs *pVfs;          /* OS functions to use for IO */
       
 31555   u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
       
 31556   u8 journalMode;             /* On of the PAGER_JOURNALMODE_* values */
       
 31557   u8 useJournal;              /* Use a rollback journal on this file */
       
 31558   u8 noReadlock;              /* Do not bother to obtain readlocks */
       
 31559   u8 noSync;                  /* Do not sync the journal if true */
       
 31560   u8 fullSync;                /* Do extra syncs of the journal for robustness */
       
 31561   u8 sync_flags;              /* One of SYNC_NORMAL or SYNC_FULL */
       
 31562   u8 tempFile;                /* zFilename is a temporary file */
       
 31563   u8 readOnly;                /* True for a read-only database */
       
 31564   u8 memDb;                   /* True to inhibit all file I/O */
       
 31565 
       
 31566   /* The following block contains those class members that are dynamically
       
 31567   ** modified during normal operations. The other variables in this structure
       
 31568   ** are either constant throughout the lifetime of the pager, or else
       
 31569   ** used to store configuration parameters that affect the way the pager 
       
 31570   ** operates.
       
 31571   **
       
 31572   ** The 'state' variable is described in more detail along with the
       
 31573   ** descriptions of the values it may take - PAGER_UNLOCK etc. Many of the
       
 31574   ** other variables in this block are described in the comment directly 
       
 31575   ** above this class definition.
       
 31576   */
       
 31577   u8 state;                   /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
       
 31578   u8 dbModified;              /* True if there are any changes to the Db */
       
 31579   u8 needSync;                /* True if an fsync() is needed on the journal */
       
 31580   u8 journalStarted;          /* True if header of journal is synced */
       
 31581   u8 changeCountDone;         /* Set after incrementing the change-counter */
       
 31582   u8 setMaster;               /* True if a m-j name has been written to jrnl */
       
 31583   u8 doNotSync;               /* Boolean. While true, do not spill the cache */
       
 31584   u8 dbSizeValid;             /* Set when dbSize is correct */
       
 31585   u8 subjInMemory;            /* True to use in-memory sub-journals */
       
 31586   Pgno dbSize;                /* Number of pages in the database */
       
 31587   Pgno dbOrigSize;            /* dbSize before the current transaction */
       
 31588   Pgno dbFileSize;            /* Number of pages in the database file */
       
 31589   int errCode;                /* One of several kinds of errors */
       
 31590   int nRec;                   /* Pages journalled since last j-header written */
       
 31591   u32 cksumInit;              /* Quasi-random value added to every checksum */
       
 31592   u32 nSubRec;                /* Number of records written to sub-journal */
       
 31593   Bitvec *pInJournal;         /* One bit for each page in the database file */
       
 31594   sqlite3_file *fd;           /* File descriptor for database */
       
 31595   sqlite3_file *jfd;          /* File descriptor for main journal */
       
 31596   sqlite3_file *sjfd;         /* File descriptor for sub-journal */
       
 31597   i64 journalOff;             /* Current write offset in the journal file */
       
 31598   i64 journalHdr;             /* Byte offset to previous journal header */
       
 31599   PagerSavepoint *aSavepoint; /* Array of active savepoints */
       
 31600   int nSavepoint;             /* Number of elements in aSavepoint[] */
       
 31601   char dbFileVers[16];        /* Changes whenever database file changes */
       
 31602   u32 sectorSize;             /* Assumed sector size during rollback */
       
 31603 
       
 31604   u16 nExtra;                 /* Add this many bytes to each in-memory page */
       
 31605   i16 nReserve;               /* Number of unused bytes at end of each page */
       
 31606   u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
       
 31607   int pageSize;               /* Number of bytes in a page */
       
 31608   Pgno mxPgno;                /* Maximum allowed size of the database */
       
 31609   char *zFilename;            /* Name of the database file */
       
 31610   char *zJournal;             /* Name of the journal file */
       
 31611   int (*xBusyHandler)(void*); /* Function to call when busy */
       
 31612   void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
       
 31613 #ifdef SQLITE_TEST
       
 31614   int nHit, nMiss;            /* Cache hits and missing */
       
 31615   int nRead, nWrite;          /* Database pages read/written */
       
 31616 #endif
       
 31617   void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
       
 31618 #ifdef SQLITE_HAS_CODEC
       
 31619   void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
       
 31620   void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
       
 31621   void (*xCodecFree)(void*);             /* Destructor for the codec */
       
 31622   void *pCodec;               /* First argument to xCodec... methods */
       
 31623 #endif
       
 31624   char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
       
 31625   i64 journalSizeLimit;       /* Size limit for persistent journal files */
       
 31626   PCache *pPCache;            /* Pointer to page cache object */
       
 31627   sqlite3_backup *pBackup;    /* Pointer to list of ongoing backup processes */
       
 31628 };
       
 31629 
       
 31630 /*
       
 31631 ** The following global variables hold counters used for
       
 31632 ** testing purposes only.  These variables do not exist in
       
 31633 ** a non-testing build.  These variables are not thread-safe.
       
 31634 */
       
 31635 #ifdef SQLITE_TEST
       
 31636 SQLITE_API int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
       
 31637 SQLITE_API int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
       
 31638 SQLITE_API int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
       
 31639 # define PAGER_INCR(v)  v++
       
 31640 #else
       
 31641 # define PAGER_INCR(v)
       
 31642 #endif
       
 31643 
       
 31644 
       
 31645 
       
 31646 /*
       
 31647 ** Journal files begin with the following magic string.  The data
       
 31648 ** was obtained from /dev/random.  It is used only as a sanity check.
       
 31649 **
       
 31650 ** Since version 2.8.0, the journal format contains additional sanity
       
 31651 ** checking information.  If the power fails while the journal is being
       
 31652 ** written, semi-random garbage data might appear in the journal
       
 31653 ** file after power is restored.  If an attempt is then made
       
 31654 ** to roll the journal back, the database could be corrupted.  The additional
       
 31655 ** sanity checking data is an attempt to discover the garbage in the
       
 31656 ** journal and ignore it.
       
 31657 **
       
 31658 ** The sanity checking information for the new journal format consists
       
 31659 ** of a 32-bit checksum on each page of data.  The checksum covers both
       
 31660 ** the page number and the pPager->pageSize bytes of data for the page.
       
 31661 ** This cksum is initialized to a 32-bit random value that appears in the
       
 31662 ** journal file right after the header.  The random initializer is important,
       
 31663 ** because garbage data that appears at the end of a journal is likely
       
 31664 ** data that was once in other files that have now been deleted.  If the
       
 31665 ** garbage data came from an obsolete journal file, the checksums might
       
 31666 ** be correct.  But by initializing the checksum to random value which
       
 31667 ** is different for every journal, we minimize that risk.
       
 31668 */
       
 31669 static const unsigned char aJournalMagic[] = {
       
 31670   0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
       
 31671 };
       
 31672 
       
 31673 /*
       
 31674 ** The size of the of each page record in the journal is given by
       
 31675 ** the following macro.
       
 31676 */
       
 31677 #define JOURNAL_PG_SZ(pPager)  ((pPager->pageSize) + 8)
       
 31678 
       
 31679 /*
       
 31680 ** The journal header size for this pager. This is usually the same 
       
 31681 ** size as a single disk sector. See also setSectorSize().
       
 31682 */
       
 31683 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
       
 31684 
       
 31685 /*
       
 31686 ** The macro MEMDB is true if we are dealing with an in-memory database.
       
 31687 ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
       
 31688 ** the value of MEMDB will be a constant and the compiler will optimize
       
 31689 ** out code that would never execute.
       
 31690 */
       
 31691 #ifdef SQLITE_OMIT_MEMORYDB
       
 31692 # define MEMDB 0
       
 31693 #else
       
 31694 # define MEMDB pPager->memDb
       
 31695 #endif
       
 31696 
       
 31697 /*
       
 31698 ** The maximum legal page number is (2^31 - 1).
       
 31699 */
       
 31700 #define PAGER_MAX_PGNO 2147483647
       
 31701 
       
 31702 #ifndef NDEBUG 
       
 31703 /*
       
 31704 ** Usage:
       
 31705 **
       
 31706 **   assert( assert_pager_state(pPager) );
       
 31707 */
       
 31708 static int assert_pager_state(Pager *pPager){
       
 31709 
       
 31710   /* A temp-file is always in PAGER_EXCLUSIVE or PAGER_SYNCED state. */
       
 31711   assert( pPager->tempFile==0 || pPager->state>=PAGER_EXCLUSIVE );
       
 31712 
       
 31713   /* The changeCountDone flag is always set for temp-files */
       
 31714   assert( pPager->tempFile==0 || pPager->changeCountDone );
       
 31715 
       
 31716   return 1;
       
 31717 }
       
 31718 #endif
       
 31719 
       
 31720 /*
       
 31721 ** Return true if it is necessary to write page *pPg into the sub-journal.
       
 31722 ** A page needs to be written into the sub-journal if there exists one
       
 31723 ** or more open savepoints for which:
       
 31724 **
       
 31725 **   * The page-number is less than or equal to PagerSavepoint.nOrig, and
       
 31726 **   * The bit corresponding to the page-number is not set in
       
 31727 **     PagerSavepoint.pInSavepoint.
       
 31728 */
       
 31729 static int subjRequiresPage(PgHdr *pPg){
       
 31730   Pgno pgno = pPg->pgno;
       
 31731   Pager *pPager = pPg->pPager;
       
 31732   int i;
       
 31733   for(i=0; i<pPager->nSavepoint; i++){
       
 31734     PagerSavepoint *p = &pPager->aSavepoint[i];
       
 31735     if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
       
 31736       return 1;
       
 31737     }
       
 31738   }
       
 31739   return 0;
       
 31740 }
       
 31741 
       
 31742 /*
       
 31743 ** Return true if the page is already in the journal file.
       
 31744 */
       
 31745 static int pageInJournal(PgHdr *pPg){
       
 31746   return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
       
 31747 }
       
 31748 
       
 31749 /*
       
 31750 ** Read a 32-bit integer from the given file descriptor.  Store the integer
       
 31751 ** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
       
 31752 ** error code is something goes wrong.
       
 31753 **
       
 31754 ** All values are stored on disk as big-endian.
       
 31755 */
       
 31756 static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
       
 31757   unsigned char ac[4];
       
 31758   int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
       
 31759   if( rc==SQLITE_OK ){
       
 31760     *pRes = sqlite3Get4byte(ac);
       
 31761   }
       
 31762   return rc;
       
 31763 }
       
 31764 
       
 31765 /*
       
 31766 ** Write a 32-bit integer into a string buffer in big-endian byte order.
       
 31767 */
       
 31768 #define put32bits(A,B)  sqlite3Put4byte((u8*)A,B)
       
 31769 
       
 31770 /*
       
 31771 ** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
       
 31772 ** on success or an error code is something goes wrong.
       
 31773 */
       
 31774 static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
       
 31775   char ac[4];
       
 31776   put32bits(ac, val);
       
 31777   return sqlite3OsWrite(fd, ac, 4, offset);
       
 31778 }
       
 31779 
       
 31780 /*
       
 31781 ** The argument to this macro is a file descriptor (type sqlite3_file*).
       
 31782 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
       
 31783 **
       
 31784 ** This is so that expressions can be written as:
       
 31785 **
       
 31786 **   if( isOpen(pPager->jfd) ){ ...
       
 31787 **
       
 31788 ** instead of
       
 31789 **
       
 31790 **   if( pPager->jfd->pMethods ){ ...
       
 31791 */
       
 31792 #define isOpen(pFd) ((pFd)->pMethods)
       
 31793 
       
 31794 /*
       
 31795 ** If file pFd is open, call sqlite3OsUnlock() on it.
       
 31796 */
       
 31797 static int osUnlock(sqlite3_file *pFd, int eLock){
       
 31798   if( !isOpen(pFd) ){
       
 31799     return SQLITE_OK;
       
 31800   }
       
 31801   return sqlite3OsUnlock(pFd, eLock);
       
 31802 }
       
 31803 
       
 31804 /*
       
 31805 ** This function determines whether or not the atomic-write optimization
       
 31806 ** can be used with this pager. The optimization can be used if:
       
 31807 **
       
 31808 **  (a) the value returned by OsDeviceCharacteristics() indicates that
       
 31809 **      a database page may be written atomically, and
       
 31810 **  (b) the value returned by OsSectorSize() is less than or equal
       
 31811 **      to the page size.
       
 31812 **
       
 31813 ** The optimization is also always enabled for temporary files. It is
       
 31814 ** an error to call this function if pPager is opened on an in-memory
       
 31815 ** database.
       
 31816 **
       
 31817 ** If the optimization cannot be used, 0 is returned. If it can be used,
       
 31818 ** then the value returned is the size of the journal file when it
       
 31819 ** contains rollback data for exactly one page.
       
 31820 */
       
 31821 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
       
 31822 static int jrnlBufferSize(Pager *pPager){
       
 31823   assert( !MEMDB );
       
 31824   if( !pPager->tempFile ){
       
 31825     int dc;                           /* Device characteristics */
       
 31826     int nSector;                      /* Sector size */
       
 31827     int szPage;                       /* Page size */
       
 31828 
       
 31829     assert( isOpen(pPager->fd) );
       
 31830     dc = sqlite3OsDeviceCharacteristics(pPager->fd);
       
 31831     nSector = pPager->sectorSize;
       
 31832     szPage = pPager->pageSize;
       
 31833 
       
 31834     assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
       
 31835     assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
       
 31836     if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
       
 31837       return 0;
       
 31838     }
       
 31839   }
       
 31840 
       
 31841   return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
       
 31842 }
       
 31843 #endif
       
 31844 
       
 31845 /*
       
 31846 ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
       
 31847 ** on the cache using a hash function.  This is used for testing
       
 31848 ** and debugging only.
       
 31849 */
       
 31850 #ifdef SQLITE_CHECK_PAGES
       
 31851 /*
       
 31852 ** Return a 32-bit hash of the page data for pPage.
       
 31853 */
       
 31854 static u32 pager_datahash(int nByte, unsigned char *pData){
       
 31855   u32 hash = 0;
       
 31856   int i;
       
 31857   for(i=0; i<nByte; i++){
       
 31858     hash = (hash*1039) + pData[i];
       
 31859   }
       
 31860   return hash;
       
 31861 }
       
 31862 static u32 pager_pagehash(PgHdr *pPage){
       
 31863   return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
       
 31864 }
       
 31865 static void pager_set_pagehash(PgHdr *pPage){
       
 31866   pPage->pageHash = pager_pagehash(pPage);
       
 31867 }
       
 31868 
       
 31869 /*
       
 31870 ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
       
 31871 ** is defined, and NDEBUG is not defined, an assert() statement checks
       
 31872 ** that the page is either dirty or still matches the calculated page-hash.
       
 31873 */
       
 31874 #define CHECK_PAGE(x) checkPage(x)
       
 31875 static void checkPage(PgHdr *pPg){
       
 31876   Pager *pPager = pPg->pPager;
       
 31877   assert( !pPg->pageHash || pPager->errCode
       
 31878       || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
       
 31879 }
       
 31880 
       
 31881 #else
       
 31882 #define pager_datahash(X,Y)  0
       
 31883 #define pager_pagehash(X)  0
       
 31884 #define CHECK_PAGE(x)
       
 31885 #endif  /* SQLITE_CHECK_PAGES */
       
 31886 
       
 31887 /*
       
 31888 ** When this is called the journal file for pager pPager must be open.
       
 31889 ** This function attempts to read a master journal file name from the 
       
 31890 ** end of the file and, if successful, copies it into memory supplied 
       
 31891 ** by the caller. See comments above writeMasterJournal() for the format
       
 31892 ** used to store a master journal file name at the end of a journal file.
       
 31893 **
       
 31894 ** zMaster must point to a buffer of at least nMaster bytes allocated by
       
 31895 ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
       
 31896 ** enough space to write the master journal name). If the master journal
       
 31897 ** name in the journal is longer than nMaster bytes (including a
       
 31898 ** nul-terminator), then this is handled as if no master journal name
       
 31899 ** were present in the journal.
       
 31900 **
       
 31901 ** If a master journal file name is present at the end of the journal
       
 31902 ** file, then it is copied into the buffer pointed to by zMaster. A
       
 31903 ** nul-terminator byte is appended to the buffer following the master
       
 31904 ** journal file name.
       
 31905 **
       
 31906 ** If it is determined that no master journal file name is present 
       
 31907 ** zMaster[0] is set to 0 and SQLITE_OK returned.
       
 31908 **
       
 31909 ** If an error occurs while reading from the journal file, an SQLite
       
 31910 ** error code is returned.
       
 31911 */
       
 31912 static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
       
 31913   int rc;                    /* Return code */
       
 31914   u32 len;                   /* Length in bytes of master journal name */
       
 31915   i64 szJ;                   /* Total size in bytes of journal file pJrnl */
       
 31916   u32 cksum;                 /* MJ checksum value read from journal */
       
 31917   u32 u;                     /* Unsigned loop counter */
       
 31918   unsigned char aMagic[8];   /* A buffer to hold the magic header */
       
 31919   zMaster[0] = '\0';
       
 31920 
       
 31921   if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
       
 31922    || szJ<16
       
 31923    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
       
 31924    || len>=nMaster 
       
 31925    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
       
 31926    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
       
 31927    || memcmp(aMagic, aJournalMagic, 8)
       
 31928    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
       
 31929   ){
       
 31930     return rc;
       
 31931   }
       
 31932 
       
 31933   /* See if the checksum matches the master journal name */
       
 31934   for(u=0; u<len; u++){
       
 31935     cksum -= zMaster[u];
       
 31936   }
       
 31937   if( cksum ){
       
 31938     /* If the checksum doesn't add up, then one or more of the disk sectors
       
 31939     ** containing the master journal filename is corrupted. This means
       
 31940     ** definitely roll back, so just return SQLITE_OK and report a (nul)
       
 31941     ** master-journal filename.
       
 31942     */
       
 31943     len = 0;
       
 31944   }
       
 31945   zMaster[len] = '\0';
       
 31946    
       
 31947   return SQLITE_OK;
       
 31948 }
       
 31949 
       
 31950 /*
       
 31951 ** Return the offset of the sector boundary at or immediately 
       
 31952 ** following the value in pPager->journalOff, assuming a sector 
       
 31953 ** size of pPager->sectorSize bytes.
       
 31954 **
       
 31955 ** i.e for a sector size of 512:
       
 31956 **
       
 31957 **   Pager.journalOff          Return value
       
 31958 **   ---------------------------------------
       
 31959 **   0                         0
       
 31960 **   512                       512
       
 31961 **   100                       512
       
 31962 **   2000                      2048
       
 31963 ** 
       
 31964 */
       
 31965 static i64 journalHdrOffset(Pager *pPager){
       
 31966   i64 offset = 0;
       
 31967   i64 c = pPager->journalOff;
       
 31968   if( c ){
       
 31969     offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
       
 31970   }
       
 31971   assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
       
 31972   assert( offset>=c );
       
 31973   assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
       
 31974   return offset;
       
 31975 }
       
 31976 
       
 31977 /*
       
 31978 ** The journal file must be open when this function is called.
       
 31979 **
       
 31980 ** This function is a no-op if the journal file has not been written to
       
 31981 ** within the current transaction (i.e. if Pager.journalOff==0).
       
 31982 **
       
 31983 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
       
 31984 ** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
       
 31985 ** zero the 28-byte header at the start of the journal file. In either case, 
       
 31986 ** if the pager is not in no-sync mode, sync the journal file immediately 
       
 31987 ** after writing or truncating it.
       
 31988 **
       
 31989 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
       
 31990 ** following the truncation or zeroing described above the size of the 
       
 31991 ** journal file in bytes is larger than this value, then truncate the
       
 31992 ** journal file to Pager.journalSizeLimit bytes. The journal file does
       
 31993 ** not need to be synced following this operation.
       
 31994 **
       
 31995 ** If an IO error occurs, abandon processing and return the IO error code.
       
 31996 ** Otherwise, return SQLITE_OK.
       
 31997 */
       
 31998 static int zeroJournalHdr(Pager *pPager, int doTruncate){
       
 31999   int rc = SQLITE_OK;                               /* Return code */
       
 32000   assert( isOpen(pPager->jfd) );
       
 32001   if( pPager->journalOff ){
       
 32002     const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
       
 32003 
       
 32004     IOTRACE(("JZEROHDR %p\n", pPager))
       
 32005     if( doTruncate || iLimit==0 ){
       
 32006       rc = sqlite3OsTruncate(pPager->jfd, 0);
       
 32007     }else{
       
 32008       static const char zeroHdr[28] = {0};
       
 32009       rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
       
 32010     }
       
 32011     if( rc==SQLITE_OK && !pPager->noSync ){
       
 32012       rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->sync_flags);
       
 32013     }
       
 32014 
       
 32015     /* At this point the transaction is committed but the write lock 
       
 32016     ** is still held on the file. If there is a size limit configured for 
       
 32017     ** the persistent journal and the journal file currently consumes more
       
 32018     ** space than that limit allows for, truncate it now. There is no need
       
 32019     ** to sync the file following this operation.
       
 32020     */
       
 32021     if( rc==SQLITE_OK && iLimit>0 ){
       
 32022       i64 sz;
       
 32023       rc = sqlite3OsFileSize(pPager->jfd, &sz);
       
 32024       if( rc==SQLITE_OK && sz>iLimit ){
       
 32025         rc = sqlite3OsTruncate(pPager->jfd, iLimit);
       
 32026       }
       
 32027     }
       
 32028   }
       
 32029   return rc;
       
 32030 }
       
 32031 
       
 32032 /*
       
 32033 ** The journal file must be open when this routine is called. A journal
       
 32034 ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
       
 32035 ** current location.
       
 32036 **
       
 32037 ** The format for the journal header is as follows:
       
 32038 ** - 8 bytes: Magic identifying journal format.
       
 32039 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
       
 32040 ** - 4 bytes: Random number used for page hash.
       
 32041 ** - 4 bytes: Initial database page count.
       
 32042 ** - 4 bytes: Sector size used by the process that wrote this journal.
       
 32043 ** - 4 bytes: Database page size.
       
 32044 ** 
       
 32045 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
       
 32046 */
       
 32047 static int writeJournalHdr(Pager *pPager){
       
 32048   int rc = SQLITE_OK;                 /* Return code */
       
 32049   char *zHeader = pPager->pTmpSpace;  /* Temporary space used to build header */
       
 32050   u32 nHeader = pPager->pageSize;     /* Size of buffer pointed to by zHeader */
       
 32051   u32 nWrite;                         /* Bytes of header sector written */
       
 32052   int ii;                             /* Loop counter */
       
 32053 
       
 32054   assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
       
 32055 
       
 32056   if( nHeader>JOURNAL_HDR_SZ(pPager) ){
       
 32057     nHeader = JOURNAL_HDR_SZ(pPager);
       
 32058   }
       
 32059 
       
 32060   /* If there are active savepoints and any of them were created 
       
 32061   ** since the most recent journal header was written, update the 
       
 32062   ** PagerSavepoint.iHdrOffset fields now.
       
 32063   */
       
 32064   for(ii=0; ii<pPager->nSavepoint; ii++){
       
 32065     if( pPager->aSavepoint[ii].iHdrOffset==0 ){
       
 32066       pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
       
 32067     }
       
 32068   }
       
 32069 
       
 32070   pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
       
 32071 
       
 32072   /* 
       
 32073   ** Write the nRec Field - the number of page records that follow this
       
 32074   ** journal header. Normally, zero is written to this value at this time.
       
 32075   ** After the records are added to the journal (and the journal synced, 
       
 32076   ** if in full-sync mode), the zero is overwritten with the true number
       
 32077   ** of records (see syncJournal()).
       
 32078   **
       
 32079   ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
       
 32080   ** reading the journal this value tells SQLite to assume that the
       
 32081   ** rest of the journal file contains valid page records. This assumption
       
 32082   ** is dangerous, as if a failure occurred whilst writing to the journal
       
 32083   ** file it may contain some garbage data. There are two scenarios
       
 32084   ** where this risk can be ignored:
       
 32085   **
       
 32086   **   * When the pager is in no-sync mode. Corruption can follow a
       
 32087   **     power failure in this case anyway.
       
 32088   **
       
 32089   **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
       
 32090   **     that garbage data is never appended to the journal file.
       
 32091   */
       
 32092   assert( isOpen(pPager->fd) || pPager->noSync );
       
 32093   if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
       
 32094    || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) 
       
 32095   ){
       
 32096     memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
       
 32097     put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
       
 32098   }else{
       
 32099     memset(zHeader, 0, sizeof(aJournalMagic)+4);
       
 32100   }
       
 32101 
       
 32102   /* The random check-hash initialiser */ 
       
 32103   sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
       
 32104   put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
       
 32105   /* The initial database size */
       
 32106   put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
       
 32107   /* The assumed sector size for this process */
       
 32108   put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
       
 32109 
       
 32110   /* The page size */
       
 32111   put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
       
 32112 
       
 32113   /* Initializing the tail of the buffer is not necessary.  Everything
       
 32114   ** works find if the following memset() is omitted.  But initializing
       
 32115   ** the memory prevents valgrind from complaining, so we are willing to
       
 32116   ** take the performance hit.
       
 32117   */
       
 32118   memset(&zHeader[sizeof(aJournalMagic)+20], 0,
       
 32119          nHeader-(sizeof(aJournalMagic)+20));
       
 32120 
       
 32121   /* In theory, it is only necessary to write the 28 bytes that the 
       
 32122   ** journal header consumes to the journal file here. Then increment the 
       
 32123   ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next 
       
 32124   ** record is written to the following sector (leaving a gap in the file
       
 32125   ** that will be implicitly filled in by the OS).
       
 32126   **
       
 32127   ** However it has been discovered that on some systems this pattern can 
       
 32128   ** be significantly slower than contiguously writing data to the file,
       
 32129   ** even if that means explicitly writing data to the block of 
       
 32130   ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
       
 32131   ** is done. 
       
 32132   **
       
 32133   ** The loop is required here in case the sector-size is larger than the 
       
 32134   ** database page size. Since the zHeader buffer is only Pager.pageSize
       
 32135   ** bytes in size, more than one call to sqlite3OsWrite() may be required
       
 32136   ** to populate the entire journal header sector.
       
 32137   */ 
       
 32138   for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
       
 32139     IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
       
 32140     rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
       
 32141     pPager->journalOff += nHeader;
       
 32142   }
       
 32143 
       
 32144   return rc;
       
 32145 }
       
 32146 
       
 32147 /*
       
 32148 ** The journal file must be open when this is called. A journal header file
       
 32149 ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
       
 32150 ** file. The current location in the journal file is given by
       
 32151 ** pPager->journalOff. See comments above function writeJournalHdr() for
       
 32152 ** a description of the journal header format.
       
 32153 **
       
 32154 ** If the header is read successfully, *pNRec is set to the number of
       
 32155 ** page records following this header and *pDbSize is set to the size of the
       
 32156 ** database before the transaction began, in pages. Also, pPager->cksumInit
       
 32157 ** is set to the value read from the journal header. SQLITE_OK is returned
       
 32158 ** in this case.
       
 32159 **
       
 32160 ** If the journal header file appears to be corrupted, SQLITE_DONE is
       
 32161 ** returned and *pNRec and *PDbSize are undefined.  If JOURNAL_HDR_SZ bytes
       
 32162 ** cannot be read from the journal file an error code is returned.
       
 32163 */
       
 32164 static int readJournalHdr(
       
 32165   Pager *pPager,               /* Pager object */
       
 32166   int isHot,
       
 32167   i64 journalSize,             /* Size of the open journal file in bytes */
       
 32168   u32 *pNRec,                  /* OUT: Value read from the nRec field */
       
 32169   u32 *pDbSize                 /* OUT: Value of original database size field */
       
 32170 ){
       
 32171   int rc;                      /* Return code */
       
 32172   unsigned char aMagic[8];     /* A buffer to hold the magic header */
       
 32173   i64 iHdrOff;                 /* Offset of journal header being read */
       
 32174 
       
 32175   assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
       
 32176 
       
 32177   /* Advance Pager.journalOff to the start of the next sector. If the
       
 32178   ** journal file is too small for there to be a header stored at this
       
 32179   ** point, return SQLITE_DONE.
       
 32180   */
       
 32181   pPager->journalOff = journalHdrOffset(pPager);
       
 32182   if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
       
 32183     return SQLITE_DONE;
       
 32184   }
       
 32185   iHdrOff = pPager->journalOff;
       
 32186 
       
 32187   /* Read in the first 8 bytes of the journal header. If they do not match
       
 32188   ** the  magic string found at the start of each journal header, return
       
 32189   ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
       
 32190   ** proceed.
       
 32191   */
       
 32192   if( isHot || iHdrOff!=pPager->journalHdr ){
       
 32193     rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
       
 32194     if( rc ){
       
 32195       return rc;
       
 32196     }
       
 32197     if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
       
 32198       return SQLITE_DONE;
       
 32199     }
       
 32200   }
       
 32201 
       
 32202   /* Read the first three 32-bit fields of the journal header: The nRec
       
 32203   ** field, the checksum-initializer and the database size at the start
       
 32204   ** of the transaction. Return an error code if anything goes wrong.
       
 32205   */
       
 32206   if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
       
 32207    || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
       
 32208    || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
       
 32209   ){
       
 32210     return rc;
       
 32211   }
       
 32212 
       
 32213   if( pPager->journalOff==0 ){
       
 32214     u32 iPageSize;               /* Page-size field of journal header */
       
 32215     u32 iSectorSize;             /* Sector-size field of journal header */
       
 32216     u16 iPageSize16;             /* Copy of iPageSize in 16-bit variable */
       
 32217 
       
 32218     /* Read the page-size and sector-size journal header fields. */
       
 32219     if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
       
 32220      || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
       
 32221     ){
       
 32222       return rc;
       
 32223     }
       
 32224 
       
 32225     /* Check that the values read from the page-size and sector-size fields
       
 32226     ** are within range. To be 'in range', both values need to be a power
       
 32227     ** of two greater than or equal to 512, and not greater than their 
       
 32228     ** respective compile time maximum limits.
       
 32229     */
       
 32230     if( iPageSize<512                  || iSectorSize<512
       
 32231      || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
       
 32232      || ((iPageSize-1)&iPageSize)!=0   || ((iSectorSize-1)&iSectorSize)!=0 
       
 32233     ){
       
 32234       /* If the either the page-size or sector-size in the journal-header is 
       
 32235       ** invalid, then the process that wrote the journal-header must have 
       
 32236       ** crashed before the header was synced. In this case stop reading 
       
 32237       ** the journal file here.
       
 32238       */
       
 32239       return SQLITE_DONE;
       
 32240     }
       
 32241 
       
 32242     /* Update the page-size to match the value read from the journal. 
       
 32243     ** Use a testcase() macro to make sure that malloc failure within 
       
 32244     ** PagerSetPagesize() is tested.
       
 32245     */
       
 32246     iPageSize16 = (u16)iPageSize;
       
 32247     rc = sqlite3PagerSetPagesize(pPager, &iPageSize16, -1);
       
 32248     testcase( rc!=SQLITE_OK );
       
 32249     assert( rc!=SQLITE_OK || iPageSize16==(u16)iPageSize );
       
 32250 
       
 32251     /* Update the assumed sector-size to match the value used by 
       
 32252     ** the process that created this journal. If this journal was
       
 32253     ** created by a process other than this one, then this routine
       
 32254     ** is being called from within pager_playback(). The local value
       
 32255     ** of Pager.sectorSize is restored at the end of that routine.
       
 32256     */
       
 32257     pPager->sectorSize = iSectorSize;
       
 32258   }
       
 32259 
       
 32260   pPager->journalOff += JOURNAL_HDR_SZ(pPager);
       
 32261   return rc;
       
 32262 }
       
 32263 
       
 32264 
       
 32265 /*
       
 32266 ** Write the supplied master journal name into the journal file for pager
       
 32267 ** pPager at the current location. The master journal name must be the last
       
 32268 ** thing written to a journal file. If the pager is in full-sync mode, the
       
 32269 ** journal file descriptor is advanced to the next sector boundary before
       
 32270 ** anything is written. The format is:
       
 32271 **
       
 32272 **   + 4 bytes: PAGER_MJ_PGNO.
       
 32273 **   + N bytes: Master journal filename in utf-8.
       
 32274 **   + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
       
 32275 **   + 4 bytes: Master journal name checksum.
       
 32276 **   + 8 bytes: aJournalMagic[].
       
 32277 **
       
 32278 ** The master journal page checksum is the sum of the bytes in the master
       
 32279 ** journal name, where each byte is interpreted as a signed 8-bit integer.
       
 32280 **
       
 32281 ** If zMaster is a NULL pointer (occurs for a single database transaction), 
       
 32282 ** this call is a no-op.
       
 32283 */
       
 32284 static int writeMasterJournal(Pager *pPager, const char *zMaster){
       
 32285   int rc;                          /* Return code */
       
 32286   int nMaster;                     /* Length of string zMaster */
       
 32287   i64 iHdrOff;                     /* Offset of header in journal file */
       
 32288   i64 jrnlSize;                    /* Size of journal file on disk */
       
 32289   u32 cksum = 0;                   /* Checksum of string zMaster */
       
 32290 
       
 32291   if( !zMaster || pPager->setMaster
       
 32292    || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
       
 32293    || pPager->journalMode==PAGER_JOURNALMODE_OFF 
       
 32294   ){
       
 32295     return SQLITE_OK;
       
 32296   }
       
 32297   pPager->setMaster = 1;
       
 32298   assert( isOpen(pPager->jfd) );
       
 32299 
       
 32300   /* Calculate the length in bytes and the checksum of zMaster */
       
 32301   for(nMaster=0; zMaster[nMaster]; nMaster++){
       
 32302     cksum += zMaster[nMaster];
       
 32303   }
       
 32304 
       
 32305   /* If in full-sync mode, advance to the next disk sector before writing
       
 32306   ** the master journal name. This is in case the previous page written to
       
 32307   ** the journal has already been synced.
       
 32308   */
       
 32309   if( pPager->fullSync ){
       
 32310     pPager->journalOff = journalHdrOffset(pPager);
       
 32311   }
       
 32312   iHdrOff = pPager->journalOff;
       
 32313 
       
 32314   /* Write the master journal data to the end of the journal file. If
       
 32315   ** an error occurs, return the error code to the caller.
       
 32316   */
       
 32317   if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
       
 32318    || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
       
 32319    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
       
 32320    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
       
 32321    || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
       
 32322   ){
       
 32323     return rc;
       
 32324   }
       
 32325   pPager->journalOff += (nMaster+20);
       
 32326   pPager->needSync = !pPager->noSync;
       
 32327 
       
 32328   /* If the pager is in peristent-journal mode, then the physical 
       
 32329   ** journal-file may extend past the end of the master-journal name
       
 32330   ** and 8 bytes of magic data just written to the file. This is 
       
 32331   ** dangerous because the code to rollback a hot-journal file
       
 32332   ** will not be able to find the master-journal name to determine 
       
 32333   ** whether or not the journal is hot. 
       
 32334   **
       
 32335   ** Easiest thing to do in this scenario is to truncate the journal 
       
 32336   ** file to the required size.
       
 32337   */ 
       
 32338   if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
       
 32339    && jrnlSize>pPager->journalOff
       
 32340   ){
       
 32341     rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
       
 32342   }
       
 32343   return rc;
       
 32344 }
       
 32345 
       
 32346 /*
       
 32347 ** Find a page in the hash table given its page number. Return
       
 32348 ** a pointer to the page or NULL if the requested page is not 
       
 32349 ** already in memory.
       
 32350 */
       
 32351 static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
       
 32352   PgHdr *p;                         /* Return value */
       
 32353 
       
 32354   /* It is not possible for a call to PcacheFetch() with createFlag==0 to
       
 32355   ** fail, since no attempt to allocate dynamic memory will be made.
       
 32356   */
       
 32357   (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
       
 32358   return p;
       
 32359 }
       
 32360 
       
 32361 /*
       
 32362 ** Unless the pager is in error-state, discard all in-memory pages. If
       
 32363 ** the pager is in error-state, then this call is a no-op.
       
 32364 **
       
 32365 ** TODO: Why can we not reset the pager while in error state?
       
 32366 */
       
 32367 static void pager_reset(Pager *pPager){
       
 32368   if( SQLITE_OK==pPager->errCode ){
       
 32369     sqlite3BackupRestart(pPager->pBackup);
       
 32370     sqlite3PcacheClear(pPager->pPCache);
       
 32371     pPager->dbSizeValid = 0;
       
 32372   }
       
 32373 }
       
 32374 
       
 32375 /*
       
 32376 ** Free all structures in the Pager.aSavepoint[] array and set both
       
 32377 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
       
 32378 ** if it is open and the pager is not in exclusive mode.
       
 32379 */
       
 32380 static void releaseAllSavepoints(Pager *pPager){
       
 32381   int ii;               /* Iterator for looping through Pager.aSavepoint */
       
 32382   for(ii=0; ii<pPager->nSavepoint; ii++){
       
 32383     sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
       
 32384   }
       
 32385   if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
       
 32386     sqlite3OsClose(pPager->sjfd);
       
 32387   }
       
 32388   sqlite3_free(pPager->aSavepoint);
       
 32389   pPager->aSavepoint = 0;
       
 32390   pPager->nSavepoint = 0;
       
 32391   pPager->nSubRec = 0;
       
 32392 }
       
 32393 
       
 32394 /*
       
 32395 ** Set the bit number pgno in the PagerSavepoint.pInSavepoint 
       
 32396 ** bitvecs of all open savepoints. Return SQLITE_OK if successful
       
 32397 ** or SQLITE_NOMEM if a malloc failure occurs.
       
 32398 */
       
 32399 static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
       
 32400   int ii;                   /* Loop counter */
       
 32401   int rc = SQLITE_OK;       /* Result code */
       
 32402 
       
 32403   for(ii=0; ii<pPager->nSavepoint; ii++){
       
 32404     PagerSavepoint *p = &pPager->aSavepoint[ii];
       
 32405     if( pgno<=p->nOrig ){
       
 32406       rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
       
 32407       testcase( rc==SQLITE_NOMEM );
       
 32408       assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
       
 32409     }
       
 32410   }
       
 32411   return rc;
       
 32412 }
       
 32413 
       
 32414 /*
       
 32415 ** Unlock the database file. This function is a no-op if the pager
       
 32416 ** is in exclusive mode.
       
 32417 **
       
 32418 ** If the pager is currently in error state, discard the contents of 
       
 32419 ** the cache and reset the Pager structure internal state. If there is
       
 32420 ** an open journal-file, then the next time a shared-lock is obtained
       
 32421 ** on the pager file (by this or any other process), it will be
       
 32422 ** treated as a hot-journal and rolled back.
       
 32423 */
       
 32424 static void pager_unlock(Pager *pPager){
       
 32425   if( !pPager->exclusiveMode ){
       
 32426     int rc;                      /* Return code */
       
 32427 
       
 32428     /* Always close the journal file when dropping the database lock.
       
 32429     ** Otherwise, another connection with journal_mode=delete might
       
 32430     ** delete the file out from under us.
       
 32431     */
       
 32432     sqlite3OsClose(pPager->jfd);
       
 32433     sqlite3BitvecDestroy(pPager->pInJournal);
       
 32434     pPager->pInJournal = 0;
       
 32435     releaseAllSavepoints(pPager);
       
 32436 
       
 32437     /* If the file is unlocked, somebody else might change it. The
       
 32438     ** values stored in Pager.dbSize etc. might become invalid if
       
 32439     ** this happens. TODO: Really, this doesn't need to be cleared
       
 32440     ** until the change-counter check fails in PagerSharedLock().
       
 32441     */
       
 32442     pPager->dbSizeValid = 0;
       
 32443 
       
 32444     rc = osUnlock(pPager->fd, NO_LOCK);
       
 32445     if( rc ){
       
 32446       pPager->errCode = rc;
       
 32447     }
       
 32448     IOTRACE(("UNLOCK %p\n", pPager))
       
 32449 
       
 32450     /* If Pager.errCode is set, the contents of the pager cache cannot be
       
 32451     ** trusted. Now that the pager file is unlocked, the contents of the
       
 32452     ** cache can be discarded and the error code safely cleared.
       
 32453     */
       
 32454     if( pPager->errCode ){
       
 32455       if( rc==SQLITE_OK ){
       
 32456         pPager->errCode = SQLITE_OK;
       
 32457       }
       
 32458       pager_reset(pPager);
       
 32459     }
       
 32460 
       
 32461     pPager->changeCountDone = 0;
       
 32462     pPager->state = PAGER_UNLOCK;
       
 32463   }
       
 32464 }
       
 32465 
       
 32466 /*
       
 32467 ** This function should be called when an IOERR, CORRUPT or FULL error
       
 32468 ** may have occurred. The first argument is a pointer to the pager 
       
 32469 ** structure, the second the error-code about to be returned by a pager 
       
 32470 ** API function. The value returned is a copy of the second argument 
       
 32471 ** to this function. 
       
 32472 **
       
 32473 ** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
       
 32474 ** the error becomes persistent. Until the persisten error is cleared,
       
 32475 ** subsequent API calls on this Pager will immediately return the same 
       
 32476 ** error code.
       
 32477 **
       
 32478 ** A persistent error indicates that the contents of the pager-cache 
       
 32479 ** cannot be trusted. This state can be cleared by completely discarding 
       
 32480 ** the contents of the pager-cache. If a transaction was active when
       
 32481 ** the persistent error occurred, then the rollback journal may need
       
 32482 ** to be replayed to restore the contents of the database file (as if
       
 32483 ** it were a hot-journal).
       
 32484 */
       
 32485 static int pager_error(Pager *pPager, int rc){
       
 32486   int rc2 = rc & 0xff;
       
 32487   assert( rc==SQLITE_OK || !MEMDB );
       
 32488   assert(
       
 32489        pPager->errCode==SQLITE_FULL ||
       
 32490        pPager->errCode==SQLITE_OK ||
       
 32491        (pPager->errCode & 0xff)==SQLITE_IOERR
       
 32492   );
       
 32493   if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
       
 32494     pPager->errCode = rc;
       
 32495   }
       
 32496   return rc;
       
 32497 }
       
 32498 
       
 32499 /*
       
 32500 ** Execute a rollback if a transaction is active and unlock the 
       
 32501 ** database file. 
       
 32502 **
       
 32503 ** If the pager has already entered the error state, do not attempt 
       
 32504 ** the rollback at this time. Instead, pager_unlock() is called. The
       
 32505 ** call to pager_unlock() will discard all in-memory pages, unlock
       
 32506 ** the database file and clear the error state. If this means that
       
 32507 ** there is a hot-journal left in the file-system, the next connection
       
 32508 ** to obtain a shared lock on the pager (which may be this one) will
       
 32509 ** roll it back.
       
 32510 **
       
 32511 ** If the pager has not already entered the error state, but an IO or
       
 32512 ** malloc error occurs during a rollback, then this will itself cause 
       
 32513 ** the pager to enter the error state. Which will be cleared by the
       
 32514 ** call to pager_unlock(), as described above.
       
 32515 */
       
 32516 static void pagerUnlockAndRollback(Pager *pPager){
       
 32517   if( pPager->errCode==SQLITE_OK && pPager->state>=PAGER_RESERVED ){
       
 32518     sqlite3BeginBenignMalloc();
       
 32519     sqlite3PagerRollback(pPager);
       
 32520     sqlite3EndBenignMalloc();
       
 32521   }
       
 32522   pager_unlock(pPager);
       
 32523 }
       
 32524 
       
 32525 /*
       
 32526 ** This routine ends a transaction. A transaction is usually ended by 
       
 32527 ** either a COMMIT or a ROLLBACK operation. This routine may be called 
       
 32528 ** after rollback of a hot-journal, or if an error occurs while opening
       
 32529 ** the journal file or writing the very first journal-header of a
       
 32530 ** database transaction.
       
 32531 ** 
       
 32532 ** If the pager is in PAGER_SHARED or PAGER_UNLOCK state when this
       
 32533 ** routine is called, it is a no-op (returns SQLITE_OK).
       
 32534 **
       
 32535 ** Otherwise, any active savepoints are released.
       
 32536 **
       
 32537 ** If the journal file is open, then it is "finalized". Once a journal 
       
 32538 ** file has been finalized it is not possible to use it to roll back a 
       
 32539 ** transaction. Nor will it be considered to be a hot-journal by this
       
 32540 ** or any other database connection. Exactly how a journal is finalized
       
 32541 ** depends on whether or not the pager is running in exclusive mode and
       
 32542 ** the current journal-mode (Pager.journalMode value), as follows:
       
 32543 **
       
 32544 **   journalMode==MEMORY
       
 32545 **     Journal file descriptor is simply closed. This destroys an 
       
 32546 **     in-memory journal.
       
 32547 **
       
 32548 **   journalMode==TRUNCATE
       
 32549 **     Journal file is truncated to zero bytes in size.
       
 32550 **
       
 32551 **   journalMode==PERSIST
       
 32552 **     The first 28 bytes of the journal file are zeroed. This invalidates
       
 32553 **     the first journal header in the file, and hence the entire journal
       
 32554 **     file. An invalid journal file cannot be rolled back.
       
 32555 **
       
 32556 **   journalMode==DELETE
       
 32557 **     The journal file is closed and deleted using sqlite3OsDelete().
       
 32558 **
       
 32559 **     If the pager is running in exclusive mode, this method of finalizing
       
 32560 **     the journal file is never used. Instead, if the journalMode is
       
 32561 **     DELETE and the pager is in exclusive mode, the method described under
       
 32562 **     journalMode==PERSIST is used instead.
       
 32563 **
       
 32564 ** After the journal is finalized, if running in non-exclusive mode, the
       
 32565 ** pager moves to PAGER_SHARED state (and downgrades the lock on the
       
 32566 ** database file accordingly).
       
 32567 **
       
 32568 ** If the pager is running in exclusive mode and is in PAGER_SYNCED state,
       
 32569 ** it moves to PAGER_EXCLUSIVE. No locks are downgraded when running in
       
 32570 ** exclusive mode.
       
 32571 **
       
 32572 ** SQLITE_OK is returned if no error occurs. If an error occurs during
       
 32573 ** any of the IO operations to finalize the journal file or unlock the
       
 32574 ** database then the IO error code is returned to the user. If the 
       
 32575 ** operation to finalize the journal file fails, then the code still
       
 32576 ** tries to unlock the database file if not in exclusive mode. If the
       
 32577 ** unlock operation fails as well, then the first error code related
       
 32578 ** to the first error encountered (the journal finalization one) is
       
 32579 ** returned.
       
 32580 */
       
 32581 static int pager_end_transaction(Pager *pPager, int hasMaster){
       
 32582   int rc = SQLITE_OK;      /* Error code from journal finalization operation */
       
 32583   int rc2 = SQLITE_OK;     /* Error code from db file unlock operation */
       
 32584 
       
 32585   if( pPager->state<PAGER_RESERVED ){
       
 32586     return SQLITE_OK;
       
 32587   }
       
 32588   releaseAllSavepoints(pPager);
       
 32589 
       
 32590   assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
       
 32591   if( isOpen(pPager->jfd) ){
       
 32592 
       
 32593     /* Finalize the journal file. */
       
 32594     if( sqlite3IsMemJournal(pPager->jfd) ){
       
 32595       assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
       
 32596       sqlite3OsClose(pPager->jfd);
       
 32597     }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
       
 32598       if( pPager->journalOff==0 ){
       
 32599         rc = SQLITE_OK;
       
 32600       }else{
       
 32601         rc = sqlite3OsTruncate(pPager->jfd, 0);
       
 32602       }
       
 32603       pPager->journalOff = 0;
       
 32604       pPager->journalStarted = 0;
       
 32605     }else if( pPager->exclusiveMode 
       
 32606      || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
       
 32607     ){
       
 32608       rc = zeroJournalHdr(pPager, hasMaster);
       
 32609       pager_error(pPager, rc);
       
 32610       pPager->journalOff = 0;
       
 32611       pPager->journalStarted = 0;
       
 32612     }else{
       
 32613       /* This branch may be executed with Pager.journalMode==MEMORY if
       
 32614       ** a hot-journal was just rolled back. In this case the journal
       
 32615       ** file should be closed and deleted. If this connection writes to
       
 32616       ** the database file, it will do so using an in-memory journal.  */
       
 32617       assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE 
       
 32618            || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
       
 32619       );
       
 32620       sqlite3OsClose(pPager->jfd);
       
 32621       if( !pPager->tempFile ){
       
 32622         rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
       
 32623       }
       
 32624     }
       
 32625 
       
 32626 #ifdef SQLITE_CHECK_PAGES
       
 32627     sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
       
 32628 #endif
       
 32629 
       
 32630     sqlite3PcacheCleanAll(pPager->pPCache);
       
 32631     sqlite3BitvecDestroy(pPager->pInJournal);
       
 32632     pPager->pInJournal = 0;
       
 32633     pPager->nRec = 0;
       
 32634   }
       
 32635 
       
 32636   if( !pPager->exclusiveMode ){
       
 32637     rc2 = osUnlock(pPager->fd, SHARED_LOCK);
       
 32638     pPager->state = PAGER_SHARED;
       
 32639     pPager->changeCountDone = 0;
       
 32640   }else if( pPager->state==PAGER_SYNCED ){
       
 32641     pPager->state = PAGER_EXCLUSIVE;
       
 32642   }
       
 32643   pPager->setMaster = 0;
       
 32644   pPager->needSync = 0;
       
 32645   pPager->dbModified = 0;
       
 32646 
       
 32647   /* TODO: Is this optimal? Why is the db size invalidated here 
       
 32648   ** when the database file is not unlocked? */
       
 32649   pPager->dbOrigSize = 0;
       
 32650   sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
       
 32651   if( !MEMDB ){
       
 32652     pPager->dbSizeValid = 0;
       
 32653   }
       
 32654 
       
 32655   return (rc==SQLITE_OK?rc2:rc);
       
 32656 }
       
 32657 
       
 32658 /*
       
 32659 ** Parameter aData must point to a buffer of pPager->pageSize bytes
       
 32660 ** of data. Compute and return a checksum based ont the contents of the 
       
 32661 ** page of data and the current value of pPager->cksumInit.
       
 32662 **
       
 32663 ** This is not a real checksum. It is really just the sum of the 
       
 32664 ** random initial value (pPager->cksumInit) and every 200th byte
       
 32665 ** of the page data, starting with byte offset (pPager->pageSize%200).
       
 32666 ** Each byte is interpreted as an 8-bit unsigned integer.
       
 32667 **
       
 32668 ** Changing the formula used to compute this checksum results in an
       
 32669 ** incompatible journal file format.
       
 32670 **
       
 32671 ** If journal corruption occurs due to a power failure, the most likely 
       
 32672 ** scenario is that one end or the other of the record will be changed. 
       
 32673 ** It is much less likely that the two ends of the journal record will be
       
 32674 ** correct and the middle be corrupt.  Thus, this "checksum" scheme,
       
 32675 ** though fast and simple, catches the mostly likely kind of corruption.
       
 32676 */
       
 32677 static u32 pager_cksum(Pager *pPager, const u8 *aData){
       
 32678   u32 cksum = pPager->cksumInit;         /* Checksum value to return */
       
 32679   int i = pPager->pageSize-200;          /* Loop counter */
       
 32680   while( i>0 ){
       
 32681     cksum += aData[i];
       
 32682     i -= 200;
       
 32683   }
       
 32684   return cksum;
       
 32685 }
       
 32686 
       
 32687 /*
       
 32688 ** Read a single page from either the journal file (if isMainJrnl==1) or
       
 32689 ** from the sub-journal (if isMainJrnl==0) and playback that page.
       
 32690 ** The page begins at offset *pOffset into the file. The *pOffset
       
 32691 ** value is increased to the start of the next page in the journal.
       
 32692 **
       
 32693 ** The isMainJrnl flag is true if this is the main rollback journal and
       
 32694 ** false for the statement journal.  The main rollback journal uses
       
 32695 ** checksums - the statement journal does not.
       
 32696 **
       
 32697 ** If the page number of the page record read from the (sub-)journal file
       
 32698 ** is greater than the current value of Pager.dbSize, then playback is
       
 32699 ** skipped and SQLITE_OK is returned.
       
 32700 **
       
 32701 ** If pDone is not NULL, then it is a record of pages that have already
       
 32702 ** been played back.  If the page at *pOffset has already been played back
       
 32703 ** (if the corresponding pDone bit is set) then skip the playback.
       
 32704 ** Make sure the pDone bit corresponding to the *pOffset page is set
       
 32705 ** prior to returning.
       
 32706 **
       
 32707 ** If the page record is successfully read from the (sub-)journal file
       
 32708 ** and played back, then SQLITE_OK is returned. If an IO error occurs
       
 32709 ** while reading the record from the (sub-)journal file or while writing
       
 32710 ** to the database file, then the IO error code is returned. If data
       
 32711 ** is successfully read from the (sub-)journal file but appears to be
       
 32712 ** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
       
 32713 ** two circumstances:
       
 32714 ** 
       
 32715 **   * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
       
 32716 **   * If the record is being rolled back from the main journal file
       
 32717 **     and the checksum field does not match the record content.
       
 32718 **
       
 32719 ** Neither of these two scenarios are possible during a savepoint rollback.
       
 32720 **
       
 32721 ** If this is a savepoint rollback, then memory may have to be dynamically
       
 32722 ** allocated by this function. If this is the case and an allocation fails,
       
 32723 ** SQLITE_NOMEM is returned.
       
 32724 */
       
 32725 static int pager_playback_one_page(
       
 32726   Pager *pPager,                /* The pager being played back */
       
 32727   int isMainJrnl,               /* 1 -> main journal. 0 -> sub-journal. */
       
 32728   int isUnsync,                 /* True if reading from unsynced main journal */
       
 32729   i64 *pOffset,                 /* Offset of record to playback */
       
 32730   int isSavepnt,                /* True for a savepoint rollback */
       
 32731   Bitvec *pDone                 /* Bitvec of pages already played back */
       
 32732 ){
       
 32733   int rc;
       
 32734   PgHdr *pPg;                   /* An existing page in the cache */
       
 32735   Pgno pgno;                    /* The page number of a page in journal */
       
 32736   u32 cksum;                    /* Checksum used for sanity checking */
       
 32737   u8 *aData;                    /* Temporary storage for the page */
       
 32738   sqlite3_file *jfd;            /* The file descriptor for the journal file */
       
 32739 
       
 32740   assert( (isMainJrnl&~1)==0 );      /* isMainJrnl is 0 or 1 */
       
 32741   assert( (isSavepnt&~1)==0 );       /* isSavepnt is 0 or 1 */
       
 32742   assert( isMainJrnl || pDone );     /* pDone always used on sub-journals */
       
 32743   assert( isSavepnt || pDone==0 );   /* pDone never used on non-savepoint */
       
 32744 
       
 32745   aData = (u8*)pPager->pTmpSpace;
       
 32746   assert( aData );         /* Temp storage must have already been allocated */
       
 32747 
       
 32748   /* Read the page number and page data from the journal or sub-journal
       
 32749   ** file. Return an error code to the caller if an IO error occurs.
       
 32750   */
       
 32751   jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
       
 32752   rc = read32bits(jfd, *pOffset, &pgno);
       
 32753   if( rc!=SQLITE_OK ) return rc;
       
 32754   rc = sqlite3OsRead(jfd, aData, pPager->pageSize, (*pOffset)+4);
       
 32755   if( rc!=SQLITE_OK ) return rc;
       
 32756   *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
       
 32757 
       
 32758   /* Sanity checking on the page.  This is more important that I originally
       
 32759   ** thought.  If a power failure occurs while the journal is being written,
       
 32760   ** it could cause invalid data to be written into the journal.  We need to
       
 32761   ** detect this invalid data (with high probability) and ignore it.
       
 32762   */
       
 32763   if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
       
 32764     assert( !isSavepnt );
       
 32765     return SQLITE_DONE;
       
 32766   }
       
 32767   if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
       
 32768     return SQLITE_OK;
       
 32769   }
       
 32770   if( isMainJrnl ){
       
 32771     rc = read32bits(jfd, (*pOffset)-4, &cksum);
       
 32772     if( rc ) return rc;
       
 32773     if( !isSavepnt && pager_cksum(pPager, aData)!=cksum ){
       
 32774       return SQLITE_DONE;
       
 32775     }
       
 32776   }
       
 32777 
       
 32778   if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
       
 32779     return rc;
       
 32780   }
       
 32781 
       
 32782   assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
       
 32783 
       
 32784   /* If the pager is in RESERVED state, then there must be a copy of this
       
 32785   ** page in the pager cache. In this case just update the pager cache,
       
 32786   ** not the database file. The page is left marked dirty in this case.
       
 32787   **
       
 32788   ** An exception to the above rule: If the database is in no-sync mode
       
 32789   ** and a page is moved during an incremental vacuum then the page may
       
 32790   ** not be in the pager cache. Later: if a malloc() or IO error occurs
       
 32791   ** during a Movepage() call, then the page may not be in the cache
       
 32792   ** either. So the condition described in the above paragraph is not
       
 32793   ** assert()able.
       
 32794   **
       
 32795   ** If in EXCLUSIVE state, then we update the pager cache if it exists
       
 32796   ** and the main file. The page is then marked not dirty.
       
 32797   **
       
 32798   ** Ticket #1171:  The statement journal might contain page content that is
       
 32799   ** different from the page content at the start of the transaction.
       
 32800   ** This occurs when a page is changed prior to the start of a statement
       
 32801   ** then changed again within the statement.  When rolling back such a
       
 32802   ** statement we must not write to the original database unless we know
       
 32803   ** for certain that original page contents are synced into the main rollback
       
 32804   ** journal.  Otherwise, a power loss might leave modified data in the
       
 32805   ** database file without an entry in the rollback journal that can
       
 32806   ** restore the database to its original form.  Two conditions must be
       
 32807   ** met before writing to the database files. (1) the database must be
       
 32808   ** locked.  (2) we know that the original page content is fully synced
       
 32809   ** in the main journal either because the page is not in cache or else
       
 32810   ** the page is marked as needSync==0.
       
 32811   **
       
 32812   ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
       
 32813   ** is possible to fail a statement on a database that does not yet exist.
       
 32814   ** Do not attempt to write if database file has never been opened.
       
 32815   */
       
 32816   pPg = pager_lookup(pPager, pgno);
       
 32817   assert( pPg || !MEMDB );
       
 32818   PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
       
 32819                PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData),
       
 32820                (isMainJrnl?"main-journal":"sub-journal")
       
 32821   ));
       
 32822   if( (pPager->state>=PAGER_EXCLUSIVE)
       
 32823    && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC))
       
 32824    && isOpen(pPager->fd)
       
 32825    && !isUnsync
       
 32826   ){
       
 32827     i64 ofst = (pgno-1)*(i64)pPager->pageSize;
       
 32828     rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst);
       
 32829     if( pgno>pPager->dbFileSize ){
       
 32830       pPager->dbFileSize = pgno;
       
 32831     }
       
 32832     if( pPager->pBackup ){
       
 32833       CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
       
 32834       sqlite3BackupUpdate(pPager->pBackup, pgno, aData);
       
 32835       CODEC1(pPager, aData, pgno, 0, rc=SQLITE_NOMEM);
       
 32836     }
       
 32837   }else if( !isMainJrnl && pPg==0 ){
       
 32838     /* If this is a rollback of a savepoint and data was not written to
       
 32839     ** the database and the page is not in-memory, there is a potential
       
 32840     ** problem. When the page is next fetched by the b-tree layer, it 
       
 32841     ** will be read from the database file, which may or may not be 
       
 32842     ** current. 
       
 32843     **
       
 32844     ** There are a couple of different ways this can happen. All are quite
       
 32845     ** obscure. When running in synchronous mode, this can only happen 
       
 32846     ** if the page is on the free-list at the start of the transaction, then
       
 32847     ** populated, then moved using sqlite3PagerMovepage().
       
 32848     **
       
 32849     ** The solution is to add an in-memory page to the cache containing
       
 32850     ** the data just read from the sub-journal. Mark the page as dirty 
       
 32851     ** and if the pager requires a journal-sync, then mark the page as 
       
 32852     ** requiring a journal-sync before it is written.
       
 32853     */
       
 32854     assert( isSavepnt );
       
 32855     if( (rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1))!=SQLITE_OK ){
       
 32856       return rc;
       
 32857     }
       
 32858     pPg->flags &= ~PGHDR_NEED_READ;
       
 32859     sqlite3PcacheMakeDirty(pPg);
       
 32860   }
       
 32861   if( pPg ){
       
 32862     /* No page should ever be explicitly rolled back that is in use, except
       
 32863     ** for page 1 which is held in use in order to keep the lock on the
       
 32864     ** database active. However such a page may be rolled back as a result
       
 32865     ** of an internal error resulting in an automatic call to
       
 32866     ** sqlite3PagerRollback().
       
 32867     */
       
 32868     void *pData;
       
 32869     pData = pPg->pData;
       
 32870     memcpy(pData, aData, pPager->pageSize);
       
 32871     pPager->xReiniter(pPg);
       
 32872     if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
       
 32873       /* If the contents of this page were just restored from the main 
       
 32874       ** journal file, then its content must be as they were when the 
       
 32875       ** transaction was first opened. In this case we can mark the page
       
 32876       ** as clean, since there will be no need to write it out to the.
       
 32877       **
       
 32878       ** There is one exception to this rule. If the page is being rolled
       
 32879       ** back as part of a savepoint (or statement) rollback from an 
       
 32880       ** unsynced portion of the main journal file, then it is not safe
       
 32881       ** to mark the page as clean. This is because marking the page as
       
 32882       ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
       
 32883       ** already in the journal file (recorded in Pager.pInJournal) and
       
 32884       ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
       
 32885       ** again within this transaction, it will be marked as dirty but
       
 32886       ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
       
 32887       ** be written out into the database file before its journal file
       
 32888       ** segment is synced. If a crash occurs during or following this,
       
 32889       ** database corruption may ensue.
       
 32890       */
       
 32891       sqlite3PcacheMakeClean(pPg);
       
 32892     }
       
 32893 #ifdef SQLITE_CHECK_PAGES
       
 32894     pPg->pageHash = pager_pagehash(pPg);
       
 32895 #endif
       
 32896     /* If this was page 1, then restore the value of Pager.dbFileVers.
       
 32897     ** Do this before any decoding. */
       
 32898     if( pgno==1 ){
       
 32899       memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
       
 32900     }
       
 32901 
       
 32902     /* Decode the page just read from disk */
       
 32903     CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
       
 32904     sqlite3PcacheRelease(pPg);
       
 32905   }
       
 32906   return rc;
       
 32907 }
       
 32908 
       
 32909 /*
       
 32910 ** Parameter zMaster is the name of a master journal file. A single journal
       
 32911 ** file that referred to the master journal file has just been rolled back.
       
 32912 ** This routine checks if it is possible to delete the master journal file,
       
 32913 ** and does so if it is.
       
 32914 **
       
 32915 ** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not 
       
 32916 ** available for use within this function.
       
 32917 **
       
 32918 ** When a master journal file is created, it is populated with the names 
       
 32919 ** of all of its child journals, one after another, formatted as utf-8 
       
 32920 ** encoded text. The end of each child journal file is marked with a 
       
 32921 ** nul-terminator byte (0x00). i.e. the entire contents of a master journal
       
 32922 ** file for a transaction involving two databases might be:
       
 32923 **
       
 32924 **   "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
       
 32925 **
       
 32926 ** A master journal file may only be deleted once all of its child 
       
 32927 ** journals have been rolled back.
       
 32928 **
       
 32929 ** This function reads the contents of the master-journal file into 
       
 32930 ** memory and loops through each of the child journal names. For
       
 32931 ** each child journal, it checks if:
       
 32932 **
       
 32933 **   * if the child journal exists, and if so
       
 32934 **   * if the child journal contains a reference to master journal 
       
 32935 **     file zMaster
       
 32936 **
       
 32937 ** If a child journal can be found that matches both of the criteria
       
 32938 ** above, this function returns without doing anything. Otherwise, if
       
 32939 ** no such child journal can be found, file zMaster is deleted from
       
 32940 ** the file-system using sqlite3OsDelete().
       
 32941 **
       
 32942 ** If an IO error within this function, an error code is returned. This
       
 32943 ** function allocates memory by calling sqlite3Malloc(). If an allocation
       
 32944 ** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors 
       
 32945 ** occur, SQLITE_OK is returned.
       
 32946 **
       
 32947 ** TODO: This function allocates a single block of memory to load
       
 32948 ** the entire contents of the master journal file. This could be
       
 32949 ** a couple of kilobytes or so - potentially larger than the page 
       
 32950 ** size.
       
 32951 */
       
 32952 static int pager_delmaster(Pager *pPager, const char *zMaster){
       
 32953   sqlite3_vfs *pVfs = pPager->pVfs;
       
 32954   int rc;                   /* Return code */
       
 32955   sqlite3_file *pMaster;    /* Malloc'd master-journal file descriptor */
       
 32956   sqlite3_file *pJournal;   /* Malloc'd child-journal file descriptor */
       
 32957   char *zMasterJournal = 0; /* Contents of master journal file */
       
 32958   i64 nMasterJournal;       /* Size of master journal file */
       
 32959 
       
 32960   /* Allocate space for both the pJournal and pMaster file descriptors.
       
 32961   ** If successful, open the master journal file for reading.
       
 32962   */
       
 32963   pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
       
 32964   pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
       
 32965   if( !pMaster ){
       
 32966     rc = SQLITE_NOMEM;
       
 32967   }else{
       
 32968     const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
       
 32969     rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
       
 32970   }
       
 32971   if( rc!=SQLITE_OK ) goto delmaster_out;
       
 32972 
       
 32973   rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
       
 32974   if( rc!=SQLITE_OK ) goto delmaster_out;
       
 32975 
       
 32976   if( nMasterJournal>0 ){
       
 32977     char *zJournal;
       
 32978     char *zMasterPtr = 0;
       
 32979     int nMasterPtr = pVfs->mxPathname+1;
       
 32980 
       
 32981     /* Load the entire master journal file into space obtained from
       
 32982     ** sqlite3_malloc() and pointed to by zMasterJournal. 
       
 32983     */
       
 32984     zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
       
 32985     if( !zMasterJournal ){
       
 32986       rc = SQLITE_NOMEM;
       
 32987       goto delmaster_out;
       
 32988     }
       
 32989     zMasterPtr = &zMasterJournal[nMasterJournal+1];
       
 32990     rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
       
 32991     if( rc!=SQLITE_OK ) goto delmaster_out;
       
 32992     zMasterJournal[nMasterJournal] = 0;
       
 32993 
       
 32994     zJournal = zMasterJournal;
       
 32995     while( (zJournal-zMasterJournal)<nMasterJournal ){
       
 32996       int exists;
       
 32997       rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
       
 32998       if( rc!=SQLITE_OK ){
       
 32999         goto delmaster_out;
       
 33000       }
       
 33001       if( exists ){
       
 33002         /* One of the journals pointed to by the master journal exists.
       
 33003         ** Open it and check if it points at the master journal. If
       
 33004         ** so, return without deleting the master journal file.
       
 33005         */
       
 33006         int c;
       
 33007         int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
       
 33008         rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
       
 33009         if( rc!=SQLITE_OK ){
       
 33010           goto delmaster_out;
       
 33011         }
       
 33012 
       
 33013         rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
       
 33014         sqlite3OsClose(pJournal);
       
 33015         if( rc!=SQLITE_OK ){
       
 33016           goto delmaster_out;
       
 33017         }
       
 33018 
       
 33019         c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
       
 33020         if( c ){
       
 33021           /* We have a match. Do not delete the master journal file. */
       
 33022           goto delmaster_out;
       
 33023         }
       
 33024       }
       
 33025       zJournal += (sqlite3Strlen30(zJournal)+1);
       
 33026     }
       
 33027   }
       
 33028   
       
 33029   rc = sqlite3OsDelete(pVfs, zMaster, 0);
       
 33030 
       
 33031 delmaster_out:
       
 33032   if( zMasterJournal ){
       
 33033     sqlite3_free(zMasterJournal);
       
 33034   }  
       
 33035   if( pMaster ){
       
 33036     sqlite3OsClose(pMaster);
       
 33037     assert( !isOpen(pJournal) );
       
 33038   }
       
 33039   sqlite3_free(pMaster);
       
 33040   return rc;
       
 33041 }
       
 33042 
       
 33043 
       
 33044 /*
       
 33045 ** This function is used to change the actual size of the database 
       
 33046 ** file in the file-system. This only happens when committing a transaction,
       
 33047 ** or rolling back a transaction (including rolling back a hot-journal).
       
 33048 **
       
 33049 ** If the main database file is not open, or an exclusive lock is not
       
 33050 ** held, this function is a no-op. Otherwise, the size of the file is
       
 33051 ** changed to nPage pages (nPage*pPager->pageSize bytes). If the file
       
 33052 ** on disk is currently larger than nPage pages, then use the VFS
       
 33053 ** xTruncate() method to truncate it.
       
 33054 **
       
 33055 ** Or, it might might be the case that the file on disk is smaller than 
       
 33056 ** nPage pages. Some operating system implementations can get confused if 
       
 33057 ** you try to truncate a file to some size that is larger than it 
       
 33058 ** currently is, so detect this case and write a single zero byte to 
       
 33059 ** the end of the new file instead.
       
 33060 **
       
 33061 ** If successful, return SQLITE_OK. If an IO error occurs while modifying
       
 33062 ** the database file, return the error code to the caller.
       
 33063 */
       
 33064 static int pager_truncate(Pager *pPager, Pgno nPage){
       
 33065   int rc = SQLITE_OK;
       
 33066   if( pPager->state>=PAGER_EXCLUSIVE && isOpen(pPager->fd) ){
       
 33067     i64 currentSize, newSize;
       
 33068     /* TODO: Is it safe to use Pager.dbFileSize here? */
       
 33069     rc = sqlite3OsFileSize(pPager->fd, &currentSize);
       
 33070     newSize = pPager->pageSize*(i64)nPage;
       
 33071     if( rc==SQLITE_OK && currentSize!=newSize ){
       
 33072       if( currentSize>newSize ){
       
 33073         rc = sqlite3OsTruncate(pPager->fd, newSize);
       
 33074       }else{
       
 33075         rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
       
 33076       }
       
 33077       if( rc==SQLITE_OK ){
       
 33078         pPager->dbFileSize = nPage;
       
 33079       }
       
 33080     }
       
 33081   }
       
 33082   return rc;
       
 33083 }
       
 33084 
       
 33085 /*
       
 33086 ** Set the value of the Pager.sectorSize variable for the given
       
 33087 ** pager based on the value returned by the xSectorSize method
       
 33088 ** of the open database file. The sector size will be used used 
       
 33089 ** to determine the size and alignment of journal header and 
       
 33090 ** master journal pointers within created journal files.
       
 33091 **
       
 33092 ** For temporary files the effective sector size is always 512 bytes.
       
 33093 **
       
 33094 ** Otherwise, for non-temporary files, the effective sector size is
       
 33095 ** the value returned by the xSectorSize() method rounded up to 512 if
       
 33096 ** it is less than 512, or rounded down to MAX_SECTOR_SIZE if it
       
 33097 ** is greater than MAX_SECTOR_SIZE.
       
 33098 */
       
 33099 static void setSectorSize(Pager *pPager){
       
 33100   assert( isOpen(pPager->fd) || pPager->tempFile );
       
 33101 
       
 33102   if( !pPager->tempFile ){
       
 33103     /* Sector size doesn't matter for temporary files. Also, the file
       
 33104     ** may not have been opened yet, in which case the OsSectorSize()
       
 33105     ** call will segfault.
       
 33106     */
       
 33107     pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
       
 33108   }
       
 33109   if( pPager->sectorSize<512 ){
       
 33110     pPager->sectorSize = 512;
       
 33111   }
       
 33112   if( pPager->sectorSize>MAX_SECTOR_SIZE ){
       
 33113     assert( MAX_SECTOR_SIZE>=512 );
       
 33114     pPager->sectorSize = MAX_SECTOR_SIZE;
       
 33115   }
       
 33116 }
       
 33117 
       
 33118 /*
       
 33119 ** Playback the journal and thus restore the database file to
       
 33120 ** the state it was in before we started making changes.  
       
 33121 **
       
 33122 ** The journal file format is as follows: 
       
 33123 **
       
 33124 **  (1)  8 byte prefix.  A copy of aJournalMagic[].
       
 33125 **  (2)  4 byte big-endian integer which is the number of valid page records
       
 33126 **       in the journal.  If this value is 0xffffffff, then compute the
       
 33127 **       number of page records from the journal size.
       
 33128 **  (3)  4 byte big-endian integer which is the initial value for the 
       
 33129 **       sanity checksum.
       
 33130 **  (4)  4 byte integer which is the number of pages to truncate the
       
 33131 **       database to during a rollback.
       
 33132 **  (5)  4 byte big-endian integer which is the sector size.  The header
       
 33133 **       is this many bytes in size.
       
 33134 **  (6)  4 byte big-endian integer which is the page case.
       
 33135 **  (7)  4 byte integer which is the number of bytes in the master journal
       
 33136 **       name.  The value may be zero (indicate that there is no master
       
 33137 **       journal.)
       
 33138 **  (8)  N bytes of the master journal name.  The name will be nul-terminated
       
 33139 **       and might be shorter than the value read from (5).  If the first byte
       
 33140 **       of the name is \000 then there is no master journal.  The master
       
 33141 **       journal name is stored in UTF-8.
       
 33142 **  (9)  Zero or more pages instances, each as follows:
       
 33143 **        +  4 byte page number.
       
 33144 **        +  pPager->pageSize bytes of data.
       
 33145 **        +  4 byte checksum
       
 33146 **
       
 33147 ** When we speak of the journal header, we mean the first 8 items above.
       
 33148 ** Each entry in the journal is an instance of the 9th item.
       
 33149 **
       
 33150 ** Call the value from the second bullet "nRec".  nRec is the number of
       
 33151 ** valid page entries in the journal.  In most cases, you can compute the
       
 33152 ** value of nRec from the size of the journal file.  But if a power
       
 33153 ** failure occurred while the journal was being written, it could be the
       
 33154 ** case that the size of the journal file had already been increased but
       
 33155 ** the extra entries had not yet made it safely to disk.  In such a case,
       
 33156 ** the value of nRec computed from the file size would be too large.  For
       
 33157 ** that reason, we always use the nRec value in the header.
       
 33158 **
       
 33159 ** If the nRec value is 0xffffffff it means that nRec should be computed
       
 33160 ** from the file size.  This value is used when the user selects the
       
 33161 ** no-sync option for the journal.  A power failure could lead to corruption
       
 33162 ** in this case.  But for things like temporary table (which will be
       
 33163 ** deleted when the power is restored) we don't care.  
       
 33164 **
       
 33165 ** If the file opened as the journal file is not a well-formed
       
 33166 ** journal file then all pages up to the first corrupted page are rolled
       
 33167 ** back (or no pages if the journal header is corrupted). The journal file
       
 33168 ** is then deleted and SQLITE_OK returned, just as if no corruption had
       
 33169 ** been encountered.
       
 33170 **
       
 33171 ** If an I/O or malloc() error occurs, the journal-file is not deleted
       
 33172 ** and an error code is returned.
       
 33173 **
       
 33174 ** The isHot parameter indicates that we are trying to rollback a journal
       
 33175 ** that might be a hot journal.  Or, it could be that the journal is 
       
 33176 ** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
       
 33177 ** If the journal really is hot, reset the pager cache prior rolling
       
 33178 ** back any content.  If the journal is merely persistent, no reset is
       
 33179 ** needed.
       
 33180 */
       
 33181 static int pager_playback(Pager *pPager, int isHot){
       
 33182   sqlite3_vfs *pVfs = pPager->pVfs;
       
 33183   i64 szJ;                 /* Size of the journal file in bytes */
       
 33184   u32 nRec;                /* Number of Records in the journal */
       
 33185   u32 u;                   /* Unsigned loop counter */
       
 33186   Pgno mxPg = 0;           /* Size of the original file in pages */
       
 33187   int rc;                  /* Result code of a subroutine */
       
 33188   int res = 1;             /* Value returned by sqlite3OsAccess() */
       
 33189   char *zMaster = 0;       /* Name of master journal file if any */
       
 33190   int needPagerReset;      /* True to reset page prior to first page rollback */
       
 33191 
       
 33192   /* Figure out how many records are in the journal.  Abort early if
       
 33193   ** the journal is empty.
       
 33194   */
       
 33195   assert( isOpen(pPager->jfd) );
       
 33196   rc = sqlite3OsFileSize(pPager->jfd, &szJ);
       
 33197   if( rc!=SQLITE_OK || szJ==0 ){
       
 33198     goto end_playback;
       
 33199   }
       
 33200 
       
 33201   /* Read the master journal name from the journal, if it is present.
       
 33202   ** If a master journal file name is specified, but the file is not
       
 33203   ** present on disk, then the journal is not hot and does not need to be
       
 33204   ** played back.
       
 33205   **
       
 33206   ** TODO: Technically the following is an error because it assumes that
       
 33207   ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
       
 33208   ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
       
 33209   **  mxPathname is 512, which is the same as the minimum allowable value
       
 33210   ** for pageSize.
       
 33211   */
       
 33212   zMaster = pPager->pTmpSpace;
       
 33213   rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
       
 33214   if( rc==SQLITE_OK && zMaster[0] ){
       
 33215     rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
       
 33216   }
       
 33217   zMaster = 0;
       
 33218   if( rc!=SQLITE_OK || !res ){
       
 33219     goto end_playback;
       
 33220   }
       
 33221   pPager->journalOff = 0;
       
 33222   needPagerReset = isHot;
       
 33223 
       
 33224   /* This loop terminates either when a readJournalHdr() or 
       
 33225   ** pager_playback_one_page() call returns SQLITE_DONE or an IO error 
       
 33226   ** occurs. 
       
 33227   */
       
 33228   while( 1 ){
       
 33229     int isUnsync = 0;
       
 33230 
       
 33231     /* Read the next journal header from the journal file.  If there are
       
 33232     ** not enough bytes left in the journal file for a complete header, or
       
 33233     ** it is corrupted, then a process must of failed while writing it.
       
 33234     ** This indicates nothing more needs to be rolled back.
       
 33235     */
       
 33236     rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
       
 33237     if( rc!=SQLITE_OK ){ 
       
 33238       if( rc==SQLITE_DONE ){
       
 33239         rc = SQLITE_OK;
       
 33240       }
       
 33241       goto end_playback;
       
 33242     }
       
 33243 
       
 33244     /* If nRec is 0xffffffff, then this journal was created by a process
       
 33245     ** working in no-sync mode. This means that the rest of the journal
       
 33246     ** file consists of pages, there are no more journal headers. Compute
       
 33247     ** the value of nRec based on this assumption.
       
 33248     */
       
 33249     if( nRec==0xffffffff ){
       
 33250       assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
       
 33251       nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
       
 33252     }
       
 33253 
       
 33254     /* If nRec is 0 and this rollback is of a transaction created by this
       
 33255     ** process and if this is the final header in the journal, then it means
       
 33256     ** that this part of the journal was being filled but has not yet been
       
 33257     ** synced to disk.  Compute the number of pages based on the remaining
       
 33258     ** size of the file.
       
 33259     **
       
 33260     ** The third term of the test was added to fix ticket #2565.
       
 33261     ** When rolling back a hot journal, nRec==0 always means that the next
       
 33262     ** chunk of the journal contains zero pages to be rolled back.  But
       
 33263     ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
       
 33264     ** the journal, it means that the journal might contain additional
       
 33265     ** pages that need to be rolled back and that the number of pages 
       
 33266     ** should be computed based on the journal file size.
       
 33267     */
       
 33268     if( nRec==0 && !isHot &&
       
 33269         pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
       
 33270       nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
       
 33271       isUnsync = 1;
       
 33272     }
       
 33273 
       
 33274     /* If this is the first header read from the journal, truncate the
       
 33275     ** database file back to its original size.
       
 33276     */
       
 33277     if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
       
 33278       rc = pager_truncate(pPager, mxPg);
       
 33279       if( rc!=SQLITE_OK ){
       
 33280         goto end_playback;
       
 33281       }
       
 33282       pPager->dbSize = mxPg;
       
 33283     }
       
 33284 
       
 33285     /* Copy original pages out of the journal and back into the 
       
 33286     ** database file and/or page cache.
       
 33287     */
       
 33288     for(u=0; u<nRec; u++){
       
 33289       if( needPagerReset ){
       
 33290         pager_reset(pPager);
       
 33291         needPagerReset = 0;
       
 33292       }
       
 33293       rc = pager_playback_one_page(pPager,1,isUnsync,&pPager->journalOff,0,0);
       
 33294       if( rc!=SQLITE_OK ){
       
 33295         if( rc==SQLITE_DONE ){
       
 33296           rc = SQLITE_OK;
       
 33297           pPager->journalOff = szJ;
       
 33298           break;
       
 33299         }else{
       
 33300           /* If we are unable to rollback, quit and return the error
       
 33301           ** code.  This will cause the pager to enter the error state
       
 33302           ** so that no further harm will be done.  Perhaps the next
       
 33303           ** process to come along will be able to rollback the database.
       
 33304           */
       
 33305           goto end_playback;
       
 33306         }
       
 33307       }
       
 33308     }
       
 33309   }
       
 33310   /*NOTREACHED*/
       
 33311   assert( 0 );
       
 33312 
       
 33313 end_playback:
       
 33314   /* Following a rollback, the database file should be back in its original
       
 33315   ** state prior to the start of the transaction, so invoke the
       
 33316   ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
       
 33317   ** assertion that the transaction counter was modified.
       
 33318   */
       
 33319   assert(
       
 33320     pPager->fd->pMethods==0 ||
       
 33321     sqlite3OsFileControl(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0)>=SQLITE_OK
       
 33322   );
       
 33323 
       
 33324   /* If this playback is happening automatically as a result of an IO or 
       
 33325   ** malloc error that occurred after the change-counter was updated but 
       
 33326   ** before the transaction was committed, then the change-counter 
       
 33327   ** modification may just have been reverted. If this happens in exclusive 
       
 33328   ** mode, then subsequent transactions performed by the connection will not
       
 33329   ** update the change-counter at all. This may lead to cache inconsistency
       
 33330   ** problems for other processes at some point in the future. So, just
       
 33331   ** in case this has happened, clear the changeCountDone flag now.
       
 33332   */
       
 33333   pPager->changeCountDone = pPager->tempFile;
       
 33334 
       
 33335   if( rc==SQLITE_OK ){
       
 33336     zMaster = pPager->pTmpSpace;
       
 33337     rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
       
 33338     testcase( rc!=SQLITE_OK );
       
 33339   }
       
 33340   if( rc==SQLITE_OK ){
       
 33341     rc = pager_end_transaction(pPager, zMaster[0]!='\0');
       
 33342     testcase( rc!=SQLITE_OK );
       
 33343   }
       
 33344   if( rc==SQLITE_OK && zMaster[0] && res ){
       
 33345     /* If there was a master journal and this routine will return success,
       
 33346     ** see if it is possible to delete the master journal.
       
 33347     */
       
 33348     rc = pager_delmaster(pPager, zMaster);
       
 33349     testcase( rc!=SQLITE_OK );
       
 33350   }
       
 33351 
       
 33352   /* The Pager.sectorSize variable may have been updated while rolling
       
 33353   ** back a journal created by a process with a different sector size
       
 33354   ** value. Reset it to the correct value for this process.
       
 33355   */
       
 33356   setSectorSize(pPager);
       
 33357   return rc;
       
 33358 }
       
 33359 
       
 33360 /*
       
 33361 ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
       
 33362 ** the entire master journal file. The case pSavepoint==NULL occurs when 
       
 33363 ** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction 
       
 33364 ** savepoint.
       
 33365 **
       
 33366 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is 
       
 33367 ** being rolled back), then the rollback consists of up to three stages,
       
 33368 ** performed in the order specified:
       
 33369 **
       
 33370 **   * Pages are played back from the main journal starting at byte
       
 33371 **     offset PagerSavepoint.iOffset and continuing to 
       
 33372 **     PagerSavepoint.iHdrOffset, or to the end of the main journal
       
 33373 **     file if PagerSavepoint.iHdrOffset is zero.
       
 33374 **
       
 33375 **   * If PagerSavepoint.iHdrOffset is not zero, then pages are played
       
 33376 **     back starting from the journal header immediately following 
       
 33377 **     PagerSavepoint.iHdrOffset to the end of the main journal file.
       
 33378 **
       
 33379 **   * Pages are then played back from the sub-journal file, starting
       
 33380 **     with the PagerSavepoint.iSubRec and continuing to the end of
       
 33381 **     the journal file.
       
 33382 **
       
 33383 ** Throughout the rollback process, each time a page is rolled back, the
       
 33384 ** corresponding bit is set in a bitvec structure (variable pDone in the
       
 33385 ** implementation below). This is used to ensure that a page is only
       
 33386 ** rolled back the first time it is encountered in either journal.
       
 33387 **
       
 33388 ** If pSavepoint is NULL, then pages are only played back from the main
       
 33389 ** journal file. There is no need for a bitvec in this case.
       
 33390 **
       
 33391 ** In either case, before playback commences the Pager.dbSize variable
       
 33392 ** is reset to the value that it held at the start of the savepoint 
       
 33393 ** (or transaction). No page with a page-number greater than this value
       
 33394 ** is played back. If one is encountered it is simply skipped.
       
 33395 */
       
 33396 static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
       
 33397   i64 szJ;                 /* Effective size of the main journal */
       
 33398   i64 iHdrOff;             /* End of first segment of main-journal records */
       
 33399   int rc = SQLITE_OK;      /* Return code */
       
 33400   Bitvec *pDone = 0;       /* Bitvec to ensure pages played back only once */
       
 33401 
       
 33402   assert( pPager->state>=PAGER_SHARED );
       
 33403 
       
 33404   /* Allocate a bitvec to use to store the set of pages rolled back */
       
 33405   if( pSavepoint ){
       
 33406     pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
       
 33407     if( !pDone ){
       
 33408       return SQLITE_NOMEM;
       
 33409     }
       
 33410   }
       
 33411 
       
 33412   /* Set the database size back to the value it was before the savepoint 
       
 33413   ** being reverted was opened.
       
 33414   */
       
 33415   pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
       
 33416 
       
 33417   /* Use pPager->journalOff as the effective size of the main rollback
       
 33418   ** journal.  The actual file might be larger than this in
       
 33419   ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST.  But anything
       
 33420   ** past pPager->journalOff is off-limits to us.
       
 33421   */
       
 33422   szJ = pPager->journalOff;
       
 33423 
       
 33424   /* Begin by rolling back records from the main journal starting at
       
 33425   ** PagerSavepoint.iOffset and continuing to the next journal header.
       
 33426   ** There might be records in the main journal that have a page number
       
 33427   ** greater than the current database size (pPager->dbSize) but those
       
 33428   ** will be skipped automatically.  Pages are added to pDone as they
       
 33429   ** are played back.
       
 33430   */
       
 33431   if( pSavepoint ){
       
 33432     iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
       
 33433     pPager->journalOff = pSavepoint->iOffset;
       
 33434     while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
       
 33435       rc = pager_playback_one_page(pPager, 1, 0, &pPager->journalOff, 1, pDone);
       
 33436     }
       
 33437     assert( rc!=SQLITE_DONE );
       
 33438   }else{
       
 33439     pPager->journalOff = 0;
       
 33440   }
       
 33441 
       
 33442   /* Continue rolling back records out of the main journal starting at
       
 33443   ** the first journal header seen and continuing until the effective end
       
 33444   ** of the main journal file.  Continue to skip out-of-range pages and
       
 33445   ** continue adding pages rolled back to pDone.
       
 33446   */
       
 33447   while( rc==SQLITE_OK && pPager->journalOff<szJ ){
       
 33448     u32 ii;            /* Loop counter */
       
 33449     u32 nJRec = 0;     /* Number of Journal Records */
       
 33450     u32 dummy;
       
 33451     rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
       
 33452     assert( rc!=SQLITE_DONE );
       
 33453 
       
 33454     /*
       
 33455     ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
       
 33456     ** test is related to ticket #2565.  See the discussion in the
       
 33457     ** pager_playback() function for additional information.
       
 33458     */
       
 33459     if( nJRec==0 
       
 33460      && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
       
 33461     ){
       
 33462       nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
       
 33463     }
       
 33464     for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
       
 33465       rc = pager_playback_one_page(pPager, 1, 0, &pPager->journalOff, 1, pDone);
       
 33466     }
       
 33467     assert( rc!=SQLITE_DONE );
       
 33468   }
       
 33469   assert( rc!=SQLITE_OK || pPager->journalOff==szJ );
       
 33470 
       
 33471   /* Finally,  rollback pages from the sub-journal.  Page that were
       
 33472   ** previously rolled back out of the main journal (and are hence in pDone)
       
 33473   ** will be skipped.  Out-of-range pages are also skipped.
       
 33474   */
       
 33475   if( pSavepoint ){
       
 33476     u32 ii;            /* Loop counter */
       
 33477     i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
       
 33478     for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
       
 33479       assert( offset==ii*(4+pPager->pageSize) );
       
 33480       rc = pager_playback_one_page(pPager, 0, 0, &offset, 1, pDone);
       
 33481     }
       
 33482     assert( rc!=SQLITE_DONE );
       
 33483   }
       
 33484 
       
 33485   sqlite3BitvecDestroy(pDone);
       
 33486   if( rc==SQLITE_OK ){
       
 33487     pPager->journalOff = szJ;
       
 33488   }
       
 33489   return rc;
       
 33490 }
       
 33491 
       
 33492 /*
       
 33493 ** Change the maximum number of in-memory pages that are allowed.
       
 33494 */
       
 33495 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
       
 33496   sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
       
 33497 }
       
 33498 
       
 33499 /*
       
 33500 ** Adjust the robustness of the database to damage due to OS crashes
       
 33501 ** or power failures by changing the number of syncs()s when writing
       
 33502 ** the rollback journal.  There are three levels:
       
 33503 **
       
 33504 **    OFF       sqlite3OsSync() is never called.  This is the default
       
 33505 **              for temporary and transient files.
       
 33506 **
       
 33507 **    NORMAL    The journal is synced once before writes begin on the
       
 33508 **              database.  This is normally adequate protection, but
       
 33509 **              it is theoretically possible, though very unlikely,
       
 33510 **              that an inopertune power failure could leave the journal
       
 33511 **              in a state which would cause damage to the database
       
 33512 **              when it is rolled back.
       
 33513 **
       
 33514 **    FULL      The journal is synced twice before writes begin on the
       
 33515 **              database (with some additional information - the nRec field
       
 33516 **              of the journal header - being written in between the two
       
 33517 **              syncs).  If we assume that writing a
       
 33518 **              single disk sector is atomic, then this mode provides
       
 33519 **              assurance that the journal will not be corrupted to the
       
 33520 **              point of causing damage to the database during rollback.
       
 33521 **
       
 33522 ** Numeric values associated with these states are OFF==1, NORMAL=2,
       
 33523 ** and FULL=3.
       
 33524 */
       
 33525 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
       
 33526 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){
       
 33527   pPager->noSync =  (level==1 || pPager->tempFile) ?1:0;
       
 33528   pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
       
 33529   pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
       
 33530   if( pPager->noSync ) pPager->needSync = 0;
       
 33531 }
       
 33532 #endif
       
 33533 
       
 33534 /*
       
 33535 ** The following global variable is incremented whenever the library
       
 33536 ** attempts to open a temporary file.  This information is used for
       
 33537 ** testing and analysis only.  
       
 33538 */
       
 33539 #ifdef SQLITE_TEST
       
 33540 SQLITE_API int sqlite3_opentemp_count = 0;
       
 33541 #endif
       
 33542 
       
 33543 /*
       
 33544 ** Open a temporary file.
       
 33545 **
       
 33546 ** Write the file descriptor into *pFile. Return SQLITE_OK on success 
       
 33547 ** or some other error code if we fail. The OS will automatically 
       
 33548 ** delete the temporary file when it is closed.
       
 33549 **
       
 33550 ** The flags passed to the VFS layer xOpen() call are those specified
       
 33551 ** by parameter vfsFlags ORed with the following:
       
 33552 **
       
 33553 **     SQLITE_OPEN_READWRITE
       
 33554 **     SQLITE_OPEN_CREATE
       
 33555 **     SQLITE_OPEN_EXCLUSIVE
       
 33556 **     SQLITE_OPEN_DELETEONCLOSE
       
 33557 */
       
 33558 static int pagerOpentemp(
       
 33559   Pager *pPager,        /* The pager object */
       
 33560   sqlite3_file *pFile,  /* Write the file descriptor here */
       
 33561   int vfsFlags          /* Flags passed through to the VFS */
       
 33562 ){
       
 33563   int rc;               /* Return code */
       
 33564 
       
 33565 #ifdef SQLITE_TEST
       
 33566   sqlite3_opentemp_count++;  /* Used for testing and analysis only */
       
 33567 #endif
       
 33568 
       
 33569   vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
       
 33570             SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
       
 33571   rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
       
 33572   assert( rc!=SQLITE_OK || isOpen(pFile) );
       
 33573   return rc;
       
 33574 }
       
 33575 
       
 33576 /*
       
 33577 ** Set the busy handler function.
       
 33578 **
       
 33579 ** The pager invokes the busy-handler if sqlite3OsLock() returns 
       
 33580 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
       
 33581 ** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE 
       
 33582 ** lock. It does *not* invoke the busy handler when upgrading from
       
 33583 ** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
       
 33584 ** (which occurs during hot-journal rollback). Summary:
       
 33585 **
       
 33586 **   Transition                        | Invokes xBusyHandler
       
 33587 **   --------------------------------------------------------
       
 33588 **   NO_LOCK       -> SHARED_LOCK      | Yes
       
 33589 **   SHARED_LOCK   -> RESERVED_LOCK    | No
       
 33590 **   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
       
 33591 **   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
       
 33592 **
       
 33593 ** If the busy-handler callback returns non-zero, the lock is 
       
 33594 ** retried. If it returns zero, then the SQLITE_BUSY error is
       
 33595 ** returned to the caller of the pager API function.
       
 33596 */
       
 33597 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
       
 33598   Pager *pPager,                       /* Pager object */
       
 33599   int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
       
 33600   void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
       
 33601 ){  
       
 33602   pPager->xBusyHandler = xBusyHandler;
       
 33603   pPager->pBusyHandlerArg = pBusyHandlerArg;
       
 33604 }
       
 33605 
       
 33606 /*
       
 33607 ** Report the current page size and number of reserved bytes back
       
 33608 ** to the codec.
       
 33609 */
       
 33610 #ifdef SQLITE_HAS_CODEC
       
 33611 static void pagerReportSize(Pager *pPager){
       
 33612   if( pPager->xCodecSizeChng ){
       
 33613     pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
       
 33614                            (int)pPager->nReserve);
       
 33615   }
       
 33616 }
       
 33617 #else
       
 33618 # define pagerReportSize(X)     /* No-op if we do not support a codec */
       
 33619 #endif
       
 33620 
       
 33621 /*
       
 33622 ** Change the page size used by the Pager object. The new page size 
       
 33623 ** is passed in *pPageSize.
       
 33624 **
       
 33625 ** If the pager is in the error state when this function is called, it
       
 33626 ** is a no-op. The value returned is the error state error code (i.e. 
       
 33627 ** one of SQLITE_IOERR, SQLITE_CORRUPT or SQLITE_FULL).
       
 33628 **
       
 33629 ** Otherwise, if all of the following are true:
       
 33630 **
       
 33631 **   * the new page size (value of *pPageSize) is valid (a power 
       
 33632 **     of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
       
 33633 **
       
 33634 **   * there are no outstanding page references, and
       
 33635 **
       
 33636 **   * the database is either not an in-memory database or it is
       
 33637 **     an in-memory database that currently consists of zero pages.
       
 33638 **
       
 33639 ** then the pager object page size is set to *pPageSize.
       
 33640 **
       
 33641 ** If the page size is changed, then this function uses sqlite3PagerMalloc() 
       
 33642 ** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt 
       
 33643 ** fails, SQLITE_NOMEM is returned and the page size remains unchanged. 
       
 33644 ** In all other cases, SQLITE_OK is returned.
       
 33645 **
       
 33646 ** If the page size is not changed, either because one of the enumerated
       
 33647 ** conditions above is not true, the pager was in error state when this
       
 33648 ** function was called, or because the memory allocation attempt failed, 
       
 33649 ** then *pPageSize is set to the old, retained page size before returning.
       
 33650 */
       
 33651 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize, int nReserve){
       
 33652   int rc = pPager->errCode;
       
 33653 
       
 33654   if( rc==SQLITE_OK ){
       
 33655     u16 pageSize = *pPageSize;
       
 33656     assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
       
 33657     if( (pPager->memDb==0 || pPager->dbSize==0)
       
 33658      && sqlite3PcacheRefCount(pPager->pPCache)==0 
       
 33659      && pageSize && pageSize!=pPager->pageSize 
       
 33660     ){
       
 33661       char *pNew = (char *)sqlite3PageMalloc(pageSize);
       
 33662       if( !pNew ){
       
 33663         rc = SQLITE_NOMEM;
       
 33664       }else{
       
 33665         pager_reset(pPager);
       
 33666         pPager->pageSize = pageSize;
       
 33667         sqlite3PageFree(pPager->pTmpSpace);
       
 33668         pPager->pTmpSpace = pNew;
       
 33669         sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
       
 33670       }
       
 33671     }
       
 33672     *pPageSize = (u16)pPager->pageSize;
       
 33673     if( nReserve<0 ) nReserve = pPager->nReserve;
       
 33674     assert( nReserve>=0 && nReserve<1000 );
       
 33675     pPager->nReserve = (i16)nReserve;
       
 33676     pagerReportSize(pPager);
       
 33677   }
       
 33678   return rc;
       
 33679 }
       
 33680 
       
 33681 /*
       
 33682 ** Return a pointer to the "temporary page" buffer held internally
       
 33683 ** by the pager.  This is a buffer that is big enough to hold the
       
 33684 ** entire content of a database page.  This buffer is used internally
       
 33685 ** during rollback and will be overwritten whenever a rollback
       
 33686 ** occurs.  But other modules are free to use it too, as long as
       
 33687 ** no rollbacks are happening.
       
 33688 */
       
 33689 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
       
 33690   return pPager->pTmpSpace;
       
 33691 }
       
 33692 
       
 33693 /*
       
 33694 ** Attempt to set the maximum database page count if mxPage is positive. 
       
 33695 ** Make no changes if mxPage is zero or negative.  And never reduce the
       
 33696 ** maximum page count below the current size of the database.
       
 33697 **
       
 33698 ** Regardless of mxPage, return the current maximum page count.
       
 33699 */
       
 33700 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
       
 33701   if( mxPage>0 ){
       
 33702     pPager->mxPgno = mxPage;
       
 33703   }
       
 33704   sqlite3PagerPagecount(pPager, 0);
       
 33705   return pPager->mxPgno;
       
 33706 }
       
 33707 
       
 33708 /*
       
 33709 ** The following set of routines are used to disable the simulated
       
 33710 ** I/O error mechanism.  These routines are used to avoid simulated
       
 33711 ** errors in places where we do not care about errors.
       
 33712 **
       
 33713 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
       
 33714 ** and generate no code.
       
 33715 */
       
 33716 #ifdef SQLITE_TEST
       
 33717 SQLITE_API extern int sqlite3_io_error_pending;
       
 33718 SQLITE_API extern int sqlite3_io_error_hit;
       
 33719 static int saved_cnt;
       
 33720 void disable_simulated_io_errors(void){
       
 33721   saved_cnt = sqlite3_io_error_pending;
       
 33722   sqlite3_io_error_pending = -1;
       
 33723 }
       
 33724 void enable_simulated_io_errors(void){
       
 33725   sqlite3_io_error_pending = saved_cnt;
       
 33726 }
       
 33727 #else
       
 33728 # define disable_simulated_io_errors()
       
 33729 # define enable_simulated_io_errors()
       
 33730 #endif
       
 33731 
       
 33732 /*
       
 33733 ** Read the first N bytes from the beginning of the file into memory
       
 33734 ** that pDest points to. 
       
 33735 **
       
 33736 ** If the pager was opened on a transient file (zFilename==""), or
       
 33737 ** opened on a file less than N bytes in size, the output buffer is
       
 33738 ** zeroed and SQLITE_OK returned. The rationale for this is that this 
       
 33739 ** function is used to read database headers, and a new transient or
       
 33740 ** zero sized database has a header than consists entirely of zeroes.
       
 33741 **
       
 33742 ** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
       
 33743 ** the error code is returned to the caller and the contents of the
       
 33744 ** output buffer undefined.
       
 33745 */
       
 33746 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
       
 33747   int rc = SQLITE_OK;
       
 33748   memset(pDest, 0, N);
       
 33749   assert( isOpen(pPager->fd) || pPager->tempFile );
       
 33750   if( isOpen(pPager->fd) ){
       
 33751     IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
       
 33752     rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
       
 33753     if( rc==SQLITE_IOERR_SHORT_READ ){
       
 33754       rc = SQLITE_OK;
       
 33755     }
       
 33756   }
       
 33757   return rc;
       
 33758 }
       
 33759 
       
 33760 /*
       
 33761 ** Return the total number of pages in the database file associated 
       
 33762 ** with pPager. Normally, this is calculated as (<db file size>/<page-size>).
       
 33763 ** However, if the file is between 1 and <page-size> bytes in size, then 
       
 33764 ** this is considered a 1 page file.
       
 33765 **
       
 33766 ** If the pager is in error state when this function is called, then the
       
 33767 ** error state error code is returned and *pnPage left unchanged. Or,
       
 33768 ** if the file system has to be queried for the size of the file and
       
 33769 ** the query attempt returns an IO error, the IO error code is returned
       
 33770 ** and *pnPage is left unchanged.
       
 33771 **
       
 33772 ** Otherwise, if everything is successful, then SQLITE_OK is returned
       
 33773 ** and *pnPage is set to the number of pages in the database.
       
 33774 */
       
 33775 SQLITE_PRIVATE int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
       
 33776   Pgno nPage;               /* Value to return via *pnPage */
       
 33777 
       
 33778   /* If the pager is already in the error state, return the error code. */
       
 33779   if( pPager->errCode ){
       
 33780     return pPager->errCode;
       
 33781   }
       
 33782 
       
 33783   /* Determine the number of pages in the file. Store this in nPage. */
       
 33784   if( pPager->dbSizeValid ){
       
 33785     nPage = pPager->dbSize;
       
 33786   }else{
       
 33787     int rc;                 /* Error returned by OsFileSize() */
       
 33788     i64 n = 0;              /* File size in bytes returned by OsFileSize() */
       
 33789 
       
 33790     assert( isOpen(pPager->fd) || pPager->tempFile );
       
 33791     if( isOpen(pPager->fd) && (0 != (rc = sqlite3OsFileSize(pPager->fd, &n))) ){
       
 33792       pager_error(pPager, rc);
       
 33793       return rc;
       
 33794     }
       
 33795     if( n>0 && n<pPager->pageSize ){
       
 33796       nPage = 1;
       
 33797     }else{
       
 33798       nPage = (Pgno)(n / pPager->pageSize);
       
 33799     }
       
 33800     if( pPager->state!=PAGER_UNLOCK ){
       
 33801       pPager->dbSize = nPage;
       
 33802       pPager->dbFileSize = nPage;
       
 33803       pPager->dbSizeValid = 1;
       
 33804     }
       
 33805   }
       
 33806 
       
 33807   /* If the current number of pages in the file is greater than the 
       
 33808   ** configured maximum pager number, increase the allowed limit so
       
 33809   ** that the file can be read.
       
 33810   */
       
 33811   if( nPage>pPager->mxPgno ){
       
 33812     pPager->mxPgno = (Pgno)nPage;
       
 33813   }
       
 33814 
       
 33815   /* Set the output variable and return SQLITE_OK */
       
 33816   if( pnPage ){
       
 33817     *pnPage = nPage;
       
 33818   }
       
 33819   return SQLITE_OK;
       
 33820 }
       
 33821 
       
 33822 
       
 33823 /*
       
 33824 ** Try to obtain a lock of type locktype on the database file. If
       
 33825 ** a similar or greater lock is already held, this function is a no-op
       
 33826 ** (returning SQLITE_OK immediately).
       
 33827 **
       
 33828 ** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke 
       
 33829 ** the busy callback if the lock is currently not available. Repeat 
       
 33830 ** until the busy callback returns false or until the attempt to 
       
 33831 ** obtain the lock succeeds.
       
 33832 **
       
 33833 ** Return SQLITE_OK on success and an error code if we cannot obtain
       
 33834 ** the lock. If the lock is obtained successfully, set the Pager.state 
       
 33835 ** variable to locktype before returning.
       
 33836 */
       
 33837 static int pager_wait_on_lock(Pager *pPager, int locktype){
       
 33838   int rc;                              /* Return code */
       
 33839 
       
 33840   /* The OS lock values must be the same as the Pager lock values */
       
 33841   assert( PAGER_SHARED==SHARED_LOCK );
       
 33842   assert( PAGER_RESERVED==RESERVED_LOCK );
       
 33843   assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
       
 33844 
       
 33845   /* If the file is currently unlocked then the size must be unknown */
       
 33846   assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 );
       
 33847 
       
 33848   /* Check that this is either a no-op (because the requested lock is 
       
 33849   ** already held, or one of the transistions that the busy-handler
       
 33850   ** may be invoked during, according to the comment above
       
 33851   ** sqlite3PagerSetBusyhandler().
       
 33852   */
       
 33853   assert( (pPager->state>=locktype)
       
 33854        || (pPager->state==PAGER_UNLOCK && locktype==PAGER_SHARED)
       
 33855        || (pPager->state==PAGER_RESERVED && locktype==PAGER_EXCLUSIVE)
       
 33856   );
       
 33857 
       
 33858   if( pPager->state>=locktype ){
       
 33859     rc = SQLITE_OK;
       
 33860   }else{
       
 33861     do {
       
 33862       rc = sqlite3OsLock(pPager->fd, locktype);
       
 33863     }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
       
 33864     if( rc==SQLITE_OK ){
       
 33865       pPager->state = (u8)locktype;
       
 33866       IOTRACE(("LOCK %p %d\n", pPager, locktype))
       
 33867     }
       
 33868   }
       
 33869   return rc;
       
 33870 }
       
 33871 
       
 33872 /*
       
 33873 ** Function assertTruncateConstraint(pPager) checks that one of the 
       
 33874 ** following is true for all dirty pages currently in the page-cache:
       
 33875 **
       
 33876 **   a) The page number is less than or equal to the size of the 
       
 33877 **      current database image, in pages, OR
       
 33878 **
       
 33879 **   b) if the page content were written at this time, it would not
       
 33880 **      be necessary to write the current content out to the sub-journal
       
 33881 **      (as determined by function subjRequiresPage()).
       
 33882 **
       
 33883 ** If the condition asserted by this function were not true, and the
       
 33884 ** dirty page were to be discarded from the cache via the pagerStress()
       
 33885 ** routine, pagerStress() would not write the current page content to
       
 33886 ** the database file. If a savepoint transaction were rolled back after
       
 33887 ** this happened, the correct behaviour would be to restore the current
       
 33888 ** content of the page. However, since this content is not present in either
       
 33889 ** the database file or the portion of the rollback journal and 
       
 33890 ** sub-journal rolled back the content could not be restored and the
       
 33891 ** database image would become corrupt. It is therefore fortunate that 
       
 33892 ** this circumstance cannot arise.
       
 33893 */
       
 33894 #if defined(SQLITE_DEBUG)
       
 33895 static void assertTruncateConstraintCb(PgHdr *pPg){
       
 33896   assert( pPg->flags&PGHDR_DIRTY );
       
 33897   assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
       
 33898 }
       
 33899 static void assertTruncateConstraint(Pager *pPager){
       
 33900   sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
       
 33901 }
       
 33902 #else
       
 33903 # define assertTruncateConstraint(pPager)
       
 33904 #endif
       
 33905 
       
 33906 /*
       
 33907 ** Truncate the in-memory database file image to nPage pages. This 
       
 33908 ** function does not actually modify the database file on disk. It 
       
 33909 ** just sets the internal state of the pager object so that the 
       
 33910 ** truncation will be done when the current transaction is committed.
       
 33911 */
       
 33912 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
       
 33913   assert( pPager->dbSizeValid );
       
 33914   assert( pPager->dbSize>=nPage );
       
 33915   assert( pPager->state>=PAGER_RESERVED );
       
 33916   pPager->dbSize = nPage;
       
 33917   assertTruncateConstraint(pPager);
       
 33918 }
       
 33919 
       
 33920 /*
       
 33921 ** Shutdown the page cache.  Free all memory and close all files.
       
 33922 **
       
 33923 ** If a transaction was in progress when this routine is called, that
       
 33924 ** transaction is rolled back.  All outstanding pages are invalidated
       
 33925 ** and their memory is freed.  Any attempt to use a page associated
       
 33926 ** with this page cache after this function returns will likely
       
 33927 ** result in a coredump.
       
 33928 **
       
 33929 ** This function always succeeds. If a transaction is active an attempt
       
 33930 ** is made to roll it back. If an error occurs during the rollback 
       
 33931 ** a hot journal may be left in the filesystem but no error is returned
       
 33932 ** to the caller.
       
 33933 */
       
 33934 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
       
 33935   disable_simulated_io_errors();
       
 33936   sqlite3BeginBenignMalloc();
       
 33937   pPager->errCode = 0;
       
 33938   pPager->exclusiveMode = 0;
       
 33939   pager_reset(pPager);
       
 33940   if( MEMDB ){
       
 33941     pager_unlock(pPager);
       
 33942   }else{
       
 33943     /* Set Pager.journalHdr to -1 for the benefit of the pager_playback() 
       
 33944     ** call which may be made from within pagerUnlockAndRollback(). If it
       
 33945     ** is not -1, then the unsynced portion of an open journal file may
       
 33946     ** be played back into the database. If a power failure occurs while
       
 33947     ** this is happening, the database may become corrupt.
       
 33948     */
       
 33949     pPager->journalHdr = -1;
       
 33950     pagerUnlockAndRollback(pPager);
       
 33951   }
       
 33952   sqlite3EndBenignMalloc();
       
 33953   enable_simulated_io_errors();
       
 33954   PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
       
 33955   IOTRACE(("CLOSE %p\n", pPager))
       
 33956   sqlite3OsClose(pPager->fd);
       
 33957   sqlite3PageFree(pPager->pTmpSpace);
       
 33958   sqlite3PcacheClose(pPager->pPCache);
       
 33959 
       
 33960 #ifdef SQLITE_HAS_CODEC
       
 33961   if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
       
 33962 #endif
       
 33963 
       
 33964   assert( !pPager->aSavepoint && !pPager->pInJournal );
       
 33965   assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
       
 33966 
       
 33967   sqlite3_free(pPager);
       
 33968   return SQLITE_OK;
       
 33969 }
       
 33970 
       
 33971 #if !defined(NDEBUG) || defined(SQLITE_TEST)
       
 33972 /*
       
 33973 ** Return the page number for page pPg.
       
 33974 */
       
 33975 SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
       
 33976   return pPg->pgno;
       
 33977 }
       
 33978 #endif
       
 33979 
       
 33980 /*
       
 33981 ** Increment the reference count for page pPg.
       
 33982 */
       
 33983 SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
       
 33984   sqlite3PcacheRef(pPg);
       
 33985 }
       
 33986 
       
 33987 /*
       
 33988 ** Sync the journal. In other words, make sure all the pages that have
       
 33989 ** been written to the journal have actually reached the surface of the
       
 33990 ** disk and can be restored in the event of a hot-journal rollback.
       
 33991 **
       
 33992 ** If the Pager.needSync flag is not set, then this function is a
       
 33993 ** no-op. Otherwise, the actions required depend on the journal-mode
       
 33994 ** and the device characteristics of the the file-system, as follows:
       
 33995 **
       
 33996 **   * If the journal file is an in-memory journal file, no action need
       
 33997 **     be taken.
       
 33998 **
       
 33999 **   * Otherwise, if the device does not support the SAFE_APPEND property,
       
 34000 **     then the nRec field of the most recently written journal header
       
 34001 **     is updated to contain the number of journal records that have
       
 34002 **     been written following it. If the pager is operating in full-sync
       
 34003 **     mode, then the journal file is synced before this field is updated.
       
 34004 **
       
 34005 **   * If the device does not support the SEQUENTIAL property, then 
       
 34006 **     journal file is synced.
       
 34007 **
       
 34008 ** Or, in pseudo-code:
       
 34009 **
       
 34010 **   if( NOT <in-memory journal> ){
       
 34011 **     if( NOT SAFE_APPEND ){
       
 34012 **       if( <full-sync mode> ) xSync(<journal file>);
       
 34013 **       <update nRec field>
       
 34014 **     } 
       
 34015 **     if( NOT SEQUENTIAL ) xSync(<journal file>);
       
 34016 **   }
       
 34017 **
       
 34018 ** The Pager.needSync flag is never be set for temporary files, or any
       
 34019 ** file operating in no-sync mode (Pager.noSync set to non-zero).
       
 34020 **
       
 34021 ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every 
       
 34022 ** page currently held in memory before returning SQLITE_OK. If an IO
       
 34023 ** error is encountered, then the IO error code is returned to the caller.
       
 34024 */
       
 34025 static int syncJournal(Pager *pPager){
       
 34026   if( pPager->needSync ){
       
 34027     assert( !pPager->tempFile );
       
 34028     if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
       
 34029       int rc;                              /* Return code */
       
 34030       const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
       
 34031       assert( isOpen(pPager->jfd) );
       
 34032 
       
 34033       if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
       
 34034         /* This block deals with an obscure problem. If the last connection
       
 34035         ** that wrote to this database was operating in persistent-journal
       
 34036         ** mode, then the journal file may at this point actually be larger
       
 34037         ** than Pager.journalOff bytes. If the next thing in the journal
       
 34038         ** file happens to be a journal-header (written as part of the
       
 34039         ** previous connections transaction), and a crash or power-failure 
       
 34040         ** occurs after nRec is updated but before this connection writes 
       
 34041         ** anything else to the journal file (or commits/rolls back its 
       
 34042         ** transaction), then SQLite may become confused when doing the 
       
 34043         ** hot-journal rollback following recovery. It may roll back all
       
 34044         ** of this connections data, then proceed to rolling back the old,
       
 34045         ** out-of-date data that follows it. Database corruption.
       
 34046         **
       
 34047         ** To work around this, if the journal file does appear to contain
       
 34048         ** a valid header following Pager.journalOff, then write a 0x00
       
 34049         ** byte to the start of it to prevent it from being recognized.
       
 34050         **
       
 34051         ** Variable iNextHdrOffset is set to the offset at which this
       
 34052         ** problematic header will occur, if it exists. aMagic is used 
       
 34053         ** as a temporary buffer to inspect the first couple of bytes of
       
 34054         ** the potential journal header.
       
 34055         */
       
 34056         i64 iNextHdrOffset;
       
 34057         u8 aMagic[8];
       
 34058 	u8 zHeader[sizeof(aJournalMagic)+4];
       
 34059 
       
 34060 	memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
       
 34061 	put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
       
 34062 
       
 34063         iNextHdrOffset = journalHdrOffset(pPager);
       
 34064         rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
       
 34065         if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
       
 34066           static const u8 zerobyte = 0;
       
 34067           rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
       
 34068         }
       
 34069         if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
       
 34070           return rc;
       
 34071         }
       
 34072 
       
 34073         /* Write the nRec value into the journal file header. If in
       
 34074         ** full-synchronous mode, sync the journal first. This ensures that
       
 34075         ** all data has really hit the disk before nRec is updated to mark
       
 34076         ** it as a candidate for rollback.
       
 34077         **
       
 34078         ** This is not required if the persistent media supports the
       
 34079         ** SAFE_APPEND property. Because in this case it is not possible 
       
 34080         ** for garbage data to be appended to the file, the nRec field
       
 34081         ** is populated with 0xFFFFFFFF when the journal header is written
       
 34082         ** and never needs to be updated.
       
 34083         */
       
 34084         if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
       
 34085           PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
       
 34086           IOTRACE(("JSYNC %p\n", pPager))
       
 34087           rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
       
 34088           if( rc!=SQLITE_OK ) return rc;
       
 34089         }
       
 34090         IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
       
 34091         rc = sqlite3OsWrite(
       
 34092             pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
       
 34093 	);
       
 34094         if( rc!=SQLITE_OK ) return rc;
       
 34095       }
       
 34096       if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
       
 34097         PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
       
 34098         IOTRACE(("JSYNC %p\n", pPager))
       
 34099         rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags| 
       
 34100           (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
       
 34101         );
       
 34102         if( rc!=SQLITE_OK ) return rc;
       
 34103       }
       
 34104     }
       
 34105 
       
 34106     /* The journal file was just successfully synced. Set Pager.needSync 
       
 34107     ** to zero and clear the PGHDR_NEED_SYNC flag on all pagess.
       
 34108     */
       
 34109     pPager->needSync = 0;
       
 34110     pPager->journalStarted = 1;
       
 34111     sqlite3PcacheClearSyncFlags(pPager->pPCache);
       
 34112   }
       
 34113 
       
 34114   return SQLITE_OK;
       
 34115 }
       
 34116 
       
 34117 /*
       
 34118 ** The argument is the first in a linked list of dirty pages connected
       
 34119 ** by the PgHdr.pDirty pointer. This function writes each one of the
       
 34120 ** in-memory pages in the list to the database file. The argument may
       
 34121 ** be NULL, representing an empty list. In this case this function is
       
 34122 ** a no-op.
       
 34123 **
       
 34124 ** The pager must hold at least a RESERVED lock when this function
       
 34125 ** is called. Before writing anything to the database file, this lock
       
 34126 ** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
       
 34127 ** SQLITE_BUSY is returned and no data is written to the database file.
       
 34128 ** 
       
 34129 ** If the pager is a temp-file pager and the actual file-system file
       
 34130 ** is not yet open, it is created and opened before any data is 
       
 34131 ** written out.
       
 34132 **
       
 34133 ** Once the lock has been upgraded and, if necessary, the file opened,
       
 34134 ** the pages are written out to the database file in list order. Writing
       
 34135 ** a page is skipped if it meets either of the following criteria:
       
 34136 **
       
 34137 **   * The page number is greater than Pager.dbSize, or
       
 34138 **   * The PGHDR_DONT_WRITE flag is set on the page.
       
 34139 **
       
 34140 ** If writing out a page causes the database file to grow, Pager.dbFileSize
       
 34141 ** is updated accordingly. If page 1 is written out, then the value cached
       
 34142 ** in Pager.dbFileVers[] is updated to match the new value stored in
       
 34143 ** the database file.
       
 34144 **
       
 34145 ** If everything is successful, SQLITE_OK is returned. If an IO error 
       
 34146 ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
       
 34147 ** be obtained, SQLITE_BUSY is returned.
       
 34148 */
       
 34149 static int pager_write_pagelist(PgHdr *pList){
       
 34150   Pager *pPager;                       /* Pager object */
       
 34151   int rc;                              /* Return code */
       
 34152 
       
 34153   if( NEVER(pList==0) ) return SQLITE_OK;
       
 34154   pPager = pList->pPager;
       
 34155 
       
 34156   /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
       
 34157   ** database file. If there is already an EXCLUSIVE lock, the following
       
 34158   ** call is a no-op.
       
 34159   **
       
 34160   ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
       
 34161   ** through an intermediate state PENDING.   A PENDING lock prevents new
       
 34162   ** readers from attaching to the database but is unsufficient for us to
       
 34163   ** write.  The idea of a PENDING lock is to prevent new readers from
       
 34164   ** coming in while we wait for existing readers to clear.
       
 34165   **
       
 34166   ** While the pager is in the RESERVED state, the original database file
       
 34167   ** is unchanged and we can rollback without having to playback the
       
 34168   ** journal into the original database file.  Once we transition to
       
 34169   ** EXCLUSIVE, it means the database file has been changed and any rollback
       
 34170   ** will require a journal playback.
       
 34171   */
       
 34172   assert( pPager->state>=PAGER_RESERVED );
       
 34173   rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
       
 34174 
       
 34175   /* If the file is a temp-file has not yet been opened, open it now. It
       
 34176   ** is not possible for rc to be other than SQLITE_OK if this branch
       
 34177   ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
       
 34178   */
       
 34179   if( !isOpen(pPager->fd) ){
       
 34180     assert( pPager->tempFile && rc==SQLITE_OK );
       
 34181     rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
       
 34182   }
       
 34183 
       
 34184   while( rc==SQLITE_OK && pList ){
       
 34185     Pgno pgno = pList->pgno;
       
 34186 
       
 34187     /* If there are dirty pages in the page cache with page numbers greater
       
 34188     ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
       
 34189     ** make the file smaller (presumably by auto-vacuum code). Do not write
       
 34190     ** any such pages to the file.
       
 34191     **
       
 34192     ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
       
 34193     ** set (set by sqlite3PagerDontWrite()).
       
 34194     */
       
 34195     if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
       
 34196       i64 offset = (pgno-1)*(i64)pPager->pageSize;   /* Offset to write */
       
 34197       char *pData;                                   /* Data to write */    
       
 34198 
       
 34199       /* Encode the database */
       
 34200       CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
       
 34201 
       
 34202       /* Write out the page data. */
       
 34203       rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
       
 34204 
       
 34205       /* If page 1 was just written, update Pager.dbFileVers to match
       
 34206       ** the value now stored in the database file. If writing this 
       
 34207       ** page caused the database file to grow, update dbFileSize. 
       
 34208       */
       
 34209       if( pgno==1 ){
       
 34210         memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
       
 34211       }
       
 34212       if( pgno>pPager->dbFileSize ){
       
 34213         pPager->dbFileSize = pgno;
       
 34214       }
       
 34215 
       
 34216       /* Update any backup objects copying the contents of this pager. */
       
 34217       sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
       
 34218 
       
 34219       PAGERTRACE(("STORE %d page %d hash(%08x)\n",
       
 34220                    PAGERID(pPager), pgno, pager_pagehash(pList)));
       
 34221       IOTRACE(("PGOUT %p %d\n", pPager, pgno));
       
 34222       PAGER_INCR(sqlite3_pager_writedb_count);
       
 34223       PAGER_INCR(pPager->nWrite);
       
 34224     }else{
       
 34225       PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
       
 34226     }
       
 34227 #ifdef SQLITE_CHECK_PAGES
       
 34228     pList->pageHash = pager_pagehash(pList);
       
 34229 #endif
       
 34230     pList = pList->pDirty;
       
 34231   }
       
 34232 
       
 34233   return rc;
       
 34234 }
       
 34235 
       
 34236 /*
       
 34237 ** Append a record of the current state of page pPg to the sub-journal. 
       
 34238 ** It is the callers responsibility to use subjRequiresPage() to check 
       
 34239 ** that it is really required before calling this function.
       
 34240 **
       
 34241 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
       
 34242 ** for all open savepoints before returning.
       
 34243 **
       
 34244 ** This function returns SQLITE_OK if everything is successful, an IO
       
 34245 ** error code if the attempt to write to the sub-journal fails, or 
       
 34246 ** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
       
 34247 ** bitvec.
       
 34248 */
       
 34249 static int subjournalPage(PgHdr *pPg){
       
 34250   int rc = SQLITE_OK;
       
 34251   Pager *pPager = pPg->pPager;
       
 34252   if( isOpen(pPager->sjfd) ){
       
 34253     void *pData = pPg->pData;
       
 34254     i64 offset = pPager->nSubRec*(4+pPager->pageSize);
       
 34255     char *pData2;
       
 34256 
       
 34257     CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
       
 34258     PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
       
 34259   
       
 34260     assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
       
 34261     rc = write32bits(pPager->sjfd, offset, pPg->pgno);
       
 34262     if( rc==SQLITE_OK ){
       
 34263       rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
       
 34264     }
       
 34265   }
       
 34266   if( rc==SQLITE_OK ){
       
 34267     pPager->nSubRec++;
       
 34268     assert( pPager->nSavepoint>0 );
       
 34269     rc = addToSavepointBitvecs(pPager, pPg->pgno);
       
 34270   }
       
 34271   return rc;
       
 34272 }
       
 34273 
       
 34274 
       
 34275 /*
       
 34276 ** This function is called by the pcache layer when it has reached some
       
 34277 ** soft memory limit. The first argument is a pointer to a Pager object
       
 34278 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
       
 34279 ** database). The second argument is a reference to a page that is 
       
 34280 ** currently dirty but has no outstanding references. The page
       
 34281 ** is always associated with the Pager object passed as the first 
       
 34282 ** argument.
       
 34283 **
       
 34284 ** The job of this function is to make pPg clean by writing its contents
       
 34285 ** out to the database file, if possible. This may involve syncing the
       
 34286 ** journal file. 
       
 34287 **
       
 34288 ** If successful, sqlite3PcacheMakeClean() is called on the page and
       
 34289 ** SQLITE_OK returned. If an IO error occurs while trying to make the
       
 34290 ** page clean, the IO error code is returned. If the page cannot be
       
 34291 ** made clean for some other reason, but no error occurs, then SQLITE_OK
       
 34292 ** is returned by sqlite3PcacheMakeClean() is not called.
       
 34293 */
       
 34294 static int pagerStress(void *p, PgHdr *pPg){
       
 34295   Pager *pPager = (Pager *)p;
       
 34296   int rc = SQLITE_OK;
       
 34297 
       
 34298   assert( pPg->pPager==pPager );
       
 34299   assert( pPg->flags&PGHDR_DIRTY );
       
 34300 
       
 34301   /* The doNotSync flag is set by the sqlite3PagerWrite() function while it
       
 34302   ** is journalling a set of two or more database pages that are stored
       
 34303   ** on the same disk sector. Syncing the journal is not allowed while
       
 34304   ** this is happening as it is important that all members of such a
       
 34305   ** set of pages are synced to disk together. So, if the page this function
       
 34306   ** is trying to make clean will require a journal sync and the doNotSync
       
 34307   ** flag is set, return without doing anything. The pcache layer will
       
 34308   ** just have to go ahead and allocate a new page buffer instead of
       
 34309   ** reusing pPg.
       
 34310   **
       
 34311   ** Similarly, if the pager has already entered the error state, do not
       
 34312   ** try to write the contents of pPg to disk.
       
 34313   */
       
 34314   if( NEVER(pPager->errCode)
       
 34315    || (pPager->doNotSync && pPg->flags&PGHDR_NEED_SYNC)
       
 34316   ){
       
 34317     return SQLITE_OK;
       
 34318   }
       
 34319 
       
 34320   /* Sync the journal file if required. */
       
 34321   if( pPg->flags&PGHDR_NEED_SYNC ){
       
 34322     rc = syncJournal(pPager);
       
 34323     if( rc==SQLITE_OK && pPager->fullSync && 
       
 34324       !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
       
 34325       !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
       
 34326     ){
       
 34327       pPager->nRec = 0;
       
 34328       rc = writeJournalHdr(pPager);
       
 34329     }
       
 34330   }
       
 34331 
       
 34332   /* If the page number of this page is larger than the current size of
       
 34333   ** the database image, it may need to be written to the sub-journal.
       
 34334   ** This is because the call to pager_write_pagelist() below will not
       
 34335   ** actually write data to the file in this case.
       
 34336   **
       
 34337   ** Consider the following sequence of events:
       
 34338   **
       
 34339   **   BEGIN;
       
 34340   **     <journal page X>
       
 34341   **     <modify page X>
       
 34342   **     SAVEPOINT sp;
       
 34343   **       <shrink database file to Y pages>
       
 34344   **       pagerStress(page X)
       
 34345   **     ROLLBACK TO sp;
       
 34346   **
       
 34347   ** If (X>Y), then when pagerStress is called page X will not be written
       
 34348   ** out to the database file, but will be dropped from the cache. Then,
       
 34349   ** following the "ROLLBACK TO sp" statement, reading page X will read
       
 34350   ** data from the database file. This will be the copy of page X as it
       
 34351   ** was when the transaction started, not as it was when "SAVEPOINT sp"
       
 34352   ** was executed.
       
 34353   **
       
 34354   ** The solution is to write the current data for page X into the 
       
 34355   ** sub-journal file now (if it is not already there), so that it will
       
 34356   ** be restored to its current value when the "ROLLBACK TO sp" is 
       
 34357   ** executed.
       
 34358   */
       
 34359   if( NEVER(
       
 34360       rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
       
 34361   ) ){
       
 34362     rc = subjournalPage(pPg);
       
 34363   }
       
 34364 
       
 34365   /* Write the contents of the page out to the database file. */
       
 34366   if( rc==SQLITE_OK ){
       
 34367     pPg->pDirty = 0;
       
 34368     rc = pager_write_pagelist(pPg);
       
 34369   }
       
 34370 
       
 34371   /* Mark the page as clean. */
       
 34372   if( rc==SQLITE_OK ){
       
 34373     PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
       
 34374     sqlite3PcacheMakeClean(pPg);
       
 34375   }
       
 34376 
       
 34377   return pager_error(pPager, rc);
       
 34378 }
       
 34379 
       
 34380 
       
 34381 /*
       
 34382 ** Allocate and initialize a new Pager object and put a pointer to it
       
 34383 ** in *ppPager. The pager should eventually be freed by passing it
       
 34384 ** to sqlite3PagerClose().
       
 34385 **
       
 34386 ** The zFilename argument is the path to the database file to open.
       
 34387 ** If zFilename is NULL then a randomly-named temporary file is created
       
 34388 ** and used as the file to be cached. Temporary files are be deleted
       
 34389 ** automatically when they are closed. If zFilename is ":memory:" then 
       
 34390 ** all information is held in cache. It is never written to disk. 
       
 34391 ** This can be used to implement an in-memory database.
       
 34392 **
       
 34393 ** The nExtra parameter specifies the number of bytes of space allocated
       
 34394 ** along with each page reference. This space is available to the user
       
 34395 ** via the sqlite3PagerGetExtra() API.
       
 34396 **
       
 34397 ** The flags argument is used to specify properties that affect the
       
 34398 ** operation of the pager. It should be passed some bitwise combination
       
 34399 ** of the PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK flags.
       
 34400 **
       
 34401 ** The vfsFlags parameter is a bitmask to pass to the flags parameter
       
 34402 ** of the xOpen() method of the supplied VFS when opening files. 
       
 34403 **
       
 34404 ** If the pager object is allocated and the specified file opened 
       
 34405 ** successfully, SQLITE_OK is returned and *ppPager set to point to
       
 34406 ** the new pager object. If an error occurs, *ppPager is set to NULL
       
 34407 ** and error code returned. This function may return SQLITE_NOMEM
       
 34408 ** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or 
       
 34409 ** various SQLITE_IO_XXX errors.
       
 34410 */
       
 34411 SQLITE_PRIVATE int sqlite3PagerOpen(
       
 34412   sqlite3_vfs *pVfs,       /* The virtual file system to use */
       
 34413   Pager **ppPager,         /* OUT: Return the Pager structure here */
       
 34414   const char *zFilename,   /* Name of the database file to open */
       
 34415   int nExtra,              /* Extra bytes append to each in-memory page */
       
 34416   int flags,               /* flags controlling this file */
       
 34417   int vfsFlags,            /* flags passed through to sqlite3_vfs.xOpen() */
       
 34418   void (*xReinit)(DbPage*) /* Function to reinitialize pages */
       
 34419 ){
       
 34420   u8 *pPtr;
       
 34421   Pager *pPager = 0;       /* Pager object to allocate and return */
       
 34422   int rc = SQLITE_OK;      /* Return code */
       
 34423   int tempFile = 0;        /* True for temp files (incl. in-memory files) */
       
 34424   int memDb = 0;           /* True if this is an in-memory file */
       
 34425   int readOnly = 0;        /* True if this is a read-only file */
       
 34426   int journalFileSize;     /* Bytes to allocate for each journal fd */
       
 34427   char *zPathname = 0;     /* Full path to database file */
       
 34428   int nPathname = 0;       /* Number of bytes in zPathname */
       
 34429   int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
       
 34430   int noReadlock = (flags & PAGER_NO_READLOCK)!=0;  /* True to omit read-lock */
       
 34431   int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
       
 34432   u16 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
       
 34433 
       
 34434   /* Figure out how much space is required for each journal file-handle
       
 34435   ** (there are two of them, the main journal and the sub-journal). This
       
 34436   ** is the maximum space required for an in-memory journal file handle 
       
 34437   ** and a regular journal file-handle. Note that a "regular journal-handle"
       
 34438   ** may be a wrapper capable of caching the first portion of the journal
       
 34439   ** file in memory to implement the atomic-write optimization (see 
       
 34440   ** source file journal.c).
       
 34441   */
       
 34442   if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
       
 34443     journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
       
 34444   }else{
       
 34445     journalFileSize = ROUND8(sqlite3MemJournalSize());
       
 34446   }
       
 34447 
       
 34448   /* Set the output variable to NULL in case an error occurs. */
       
 34449   *ppPager = 0;
       
 34450 
       
 34451   /* Compute and store the full pathname in an allocated buffer pointed
       
 34452   ** to by zPathname, length nPathname. Or, if this is a temporary file,
       
 34453   ** leave both nPathname and zPathname set to 0.
       
 34454   */
       
 34455   if( zFilename && zFilename[0] ){
       
 34456     nPathname = pVfs->mxPathname+1;
       
 34457     zPathname = sqlite3Malloc(nPathname*2);
       
 34458     if( zPathname==0 ){
       
 34459       return SQLITE_NOMEM;
       
 34460     }
       
 34461 #ifndef SQLITE_OMIT_MEMORYDB
       
 34462     if( strcmp(zFilename,":memory:")==0 ){
       
 34463       memDb = 1;
       
 34464       zPathname[0] = 0;
       
 34465     }else
       
 34466 #endif
       
 34467     {
       
 34468       zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
       
 34469       rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
       
 34470     }
       
 34471 
       
 34472     nPathname = sqlite3Strlen30(zPathname);
       
 34473     if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
       
 34474       /* This branch is taken when the journal path required by
       
 34475       ** the database being opened will be more than pVfs->mxPathname
       
 34476       ** bytes in length. This means the database cannot be opened,
       
 34477       ** as it will not be possible to open the journal file or even
       
 34478       ** check for a hot-journal before reading.
       
 34479       */
       
 34480       rc = SQLITE_CANTOPEN;
       
 34481     }
       
 34482     if( rc!=SQLITE_OK ){
       
 34483       sqlite3_free(zPathname);
       
 34484       return rc;
       
 34485     }
       
 34486   }
       
 34487 
       
 34488   /* Allocate memory for the Pager structure, PCache object, the
       
 34489   ** three file descriptors, the database file name and the journal 
       
 34490   ** file name. The layout in memory is as follows:
       
 34491   **
       
 34492   **     Pager object                    (sizeof(Pager) bytes)
       
 34493   **     PCache object                   (sqlite3PcacheSize() bytes)
       
 34494   **     Database file handle            (pVfs->szOsFile bytes)
       
 34495   **     Sub-journal file handle         (journalFileSize bytes)
       
 34496   **     Main journal file handle        (journalFileSize bytes)
       
 34497   **     Database file name              (nPathname+1 bytes)
       
 34498   **     Journal file name               (nPathname+8+1 bytes)
       
 34499   */
       
 34500   pPtr = (u8 *)sqlite3MallocZero(
       
 34501     ROUND8(sizeof(*pPager)) +      /* Pager structure */
       
 34502     ROUND8(pcacheSize) +           /* PCache object */
       
 34503     ROUND8(pVfs->szOsFile) +       /* The main db file */
       
 34504     journalFileSize * 2 +          /* The two journal files */ 
       
 34505     nPathname + 1 +                /* zFilename */
       
 34506     nPathname + 8 + 1              /* zJournal */
       
 34507   );
       
 34508   assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
       
 34509   if( !pPtr ){
       
 34510     sqlite3_free(zPathname);
       
 34511     return SQLITE_NOMEM;
       
 34512   }
       
 34513   pPager =              (Pager*)(pPtr);
       
 34514   pPager->pPCache =    (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
       
 34515   pPager->fd =   (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
       
 34516   pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
       
 34517   pPager->jfd =  (sqlite3_file*)(pPtr += journalFileSize);
       
 34518   pPager->zFilename =    (char*)(pPtr += journalFileSize);
       
 34519   assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
       
 34520 
       
 34521   /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
       
 34522   if( zPathname ){
       
 34523     pPager->zJournal =   (char*)(pPtr += nPathname + 1);
       
 34524     memcpy(pPager->zFilename, zPathname, nPathname);
       
 34525     memcpy(pPager->zJournal, zPathname, nPathname);
       
 34526     memcpy(&pPager->zJournal[nPathname], "-journal", 8);
       
 34527     if( pPager->zFilename[0]==0 ) pPager->zJournal[0] = 0;
       
 34528     sqlite3_free(zPathname);
       
 34529   }
       
 34530   pPager->pVfs = pVfs;
       
 34531   pPager->vfsFlags = vfsFlags;
       
 34532 
       
 34533   /* Open the pager file.
       
 34534   */
       
 34535   if( zFilename && zFilename[0] && !memDb ){
       
 34536     int fout = 0;                    /* VFS flags returned by xOpen() */
       
 34537     rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
       
 34538     readOnly = (fout&SQLITE_OPEN_READONLY);
       
 34539 
       
 34540     /* If the file was successfully opened for read/write access,
       
 34541     ** choose a default page size in case we have to create the
       
 34542     ** database file. The default page size is the maximum of:
       
 34543     **
       
 34544     **    + SQLITE_DEFAULT_PAGE_SIZE,
       
 34545     **    + The value returned by sqlite3OsSectorSize()
       
 34546     **    + The largest page size that can be written atomically.
       
 34547     */
       
 34548     if( rc==SQLITE_OK && !readOnly ){
       
 34549       setSectorSize(pPager);
       
 34550       assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
       
 34551       if( szPageDflt<pPager->sectorSize ){
       
 34552         if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
       
 34553           szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
       
 34554         }else{
       
 34555           szPageDflt = (u16)pPager->sectorSize;
       
 34556         }
       
 34557       }
       
 34558 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
       
 34559       {
       
 34560         int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
       
 34561         int ii;
       
 34562         assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
       
 34563         assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
       
 34564         assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
       
 34565         for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
       
 34566           if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
       
 34567             szPageDflt = ii;
       
 34568           }
       
 34569         }
       
 34570       }
       
 34571 #endif
       
 34572     }
       
 34573   }else{
       
 34574     /* If a temporary file is requested, it is not opened immediately.
       
 34575     ** In this case we accept the default page size and delay actually
       
 34576     ** opening the file until the first call to OsWrite().
       
 34577     **
       
 34578     ** This branch is also run for an in-memory database. An in-memory
       
 34579     ** database is the same as a temp-file that is never written out to
       
 34580     ** disk and uses an in-memory rollback journal.
       
 34581     */ 
       
 34582     tempFile = 1;
       
 34583     pPager->state = PAGER_EXCLUSIVE;
       
 34584     readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
       
 34585   }
       
 34586 
       
 34587   /* The following call to PagerSetPagesize() serves to set the value of 
       
 34588   ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
       
 34589   */
       
 34590   if( rc==SQLITE_OK ){
       
 34591     assert( pPager->memDb==0 );
       
 34592     rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
       
 34593     testcase( rc!=SQLITE_OK );
       
 34594   }
       
 34595 
       
 34596   /* If an error occurred in either of the blocks above, free the 
       
 34597   ** Pager structure and close the file.
       
 34598   */
       
 34599   if( rc!=SQLITE_OK ){
       
 34600     assert( !pPager->pTmpSpace );
       
 34601     sqlite3OsClose(pPager->fd);
       
 34602     sqlite3_free(pPager);
       
 34603     return rc;
       
 34604   }
       
 34605 
       
 34606   /* Initialize the PCache object. */
       
 34607   assert( nExtra<1000 );
       
 34608   nExtra = ROUND8(nExtra);
       
 34609   sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
       
 34610                     !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
       
 34611 
       
 34612   PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
       
 34613   IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
       
 34614 
       
 34615   pPager->useJournal = (u8)useJournal;
       
 34616   pPager->noReadlock = (noReadlock && readOnly) ?1:0;
       
 34617   /* pPager->stmtOpen = 0; */
       
 34618   /* pPager->stmtInUse = 0; */
       
 34619   /* pPager->nRef = 0; */
       
 34620   pPager->dbSizeValid = (u8)memDb;
       
 34621   /* pPager->stmtSize = 0; */
       
 34622   /* pPager->stmtJSize = 0; */
       
 34623   /* pPager->nPage = 0; */
       
 34624   pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
       
 34625   /* pPager->state = PAGER_UNLOCK; */
       
 34626   assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
       
 34627   /* pPager->errMask = 0; */
       
 34628   pPager->tempFile = (u8)tempFile;
       
 34629   assert( tempFile==PAGER_LOCKINGMODE_NORMAL 
       
 34630           || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
       
 34631   assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
       
 34632   pPager->exclusiveMode = (u8)tempFile; 
       
 34633   pPager->changeCountDone = pPager->tempFile;
       
 34634   pPager->memDb = (u8)memDb;
       
 34635   pPager->readOnly = (u8)readOnly;
       
 34636   /* pPager->needSync = 0; */
       
 34637   assert( useJournal || pPager->tempFile );
       
 34638   pPager->noSync = pPager->tempFile;
       
 34639   pPager->fullSync = pPager->noSync ?0:1;
       
 34640   pPager->sync_flags = SQLITE_SYNC_NORMAL;
       
 34641   /* pPager->pFirst = 0; */
       
 34642   /* pPager->pFirstSynced = 0; */
       
 34643   /* pPager->pLast = 0; */
       
 34644   pPager->nExtra = (u16)nExtra;
       
 34645   pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
       
 34646   assert( isOpen(pPager->fd) || tempFile );
       
 34647   setSectorSize(pPager);
       
 34648   if( !useJournal ){
       
 34649     pPager->journalMode = PAGER_JOURNALMODE_OFF;
       
 34650   }else if( memDb ){
       
 34651     pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
       
 34652   }
       
 34653   /* pPager->xBusyHandler = 0; */
       
 34654   /* pPager->pBusyHandlerArg = 0; */
       
 34655   pPager->xReiniter = xReinit;
       
 34656   /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
       
 34657   *ppPager = pPager;
       
 34658   return SQLITE_OK;
       
 34659 }
       
 34660 
       
 34661 
       
 34662 
       
 34663 /*
       
 34664 ** This function is called after transitioning from PAGER_UNLOCK to
       
 34665 ** PAGER_SHARED state. It tests if there is a hot journal present in
       
 34666 ** the file-system for the given pager. A hot journal is one that 
       
 34667 ** needs to be played back. According to this function, a hot-journal
       
 34668 ** file exists if the following criteria are met:
       
 34669 **
       
 34670 **   * The journal file exists in the file system, and
       
 34671 **   * No process holds a RESERVED or greater lock on the database file, and
       
 34672 **   * The database file itself is greater than 0 bytes in size, and
       
 34673 **   * The first byte of the journal file exists and is not 0x00.
       
 34674 **
       
 34675 ** If the current size of the database file is 0 but a journal file
       
 34676 ** exists, that is probably an old journal left over from a prior
       
 34677 ** database with the same name. In this case the journal file is
       
 34678 ** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
       
 34679 ** is returned.
       
 34680 **
       
 34681 ** This routine does not check if there is a master journal filename
       
 34682 ** at the end of the file. If there is, and that master journal file
       
 34683 ** does not exist, then the journal file is not really hot. In this
       
 34684 ** case this routine will return a false-positive. The pager_playback()
       
 34685 ** routine will discover that the journal file is not really hot and 
       
 34686 ** will not roll it back. 
       
 34687 **
       
 34688 ** If a hot-journal file is found to exist, *pExists is set to 1 and 
       
 34689 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
       
 34690 ** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
       
 34691 ** to determine whether or not a hot-journal file exists, the IO error
       
 34692 ** code is returned and the value of *pExists is undefined.
       
 34693 */
       
 34694 static int hasHotJournal(Pager *pPager, int *pExists){
       
 34695   sqlite3_vfs * const pVfs = pPager->pVfs;
       
 34696   int rc;                       /* Return code */
       
 34697   int exists;                   /* True if a journal file is present */
       
 34698 
       
 34699   assert( pPager!=0 );
       
 34700   assert( pPager->useJournal );
       
 34701   assert( isOpen(pPager->fd) );
       
 34702   assert( !isOpen(pPager->jfd) );
       
 34703   assert( pPager->state <= PAGER_SHARED );
       
 34704 
       
 34705   *pExists = 0;
       
 34706   rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
       
 34707   if( rc==SQLITE_OK && exists ){
       
 34708     int locked;                 /* True if some process holds a RESERVED lock */
       
 34709 
       
 34710     /* Race condition here:  Another process might have been holding the
       
 34711     ** the RESERVED lock and have a journal open at the sqlite3OsAccess() 
       
 34712     ** call above, but then delete the journal and drop the lock before
       
 34713     ** we get to the following sqlite3OsCheckReservedLock() call.  If that
       
 34714     ** is the case, this routine might think there is a hot journal when
       
 34715     ** in fact there is none.  This results in a false-positive which will
       
 34716     ** be dealt with by the playback routine.  Ticket #3883.
       
 34717     */
       
 34718     rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
       
 34719     if( rc==SQLITE_OK && !locked ){
       
 34720       int nPage;
       
 34721 
       
 34722       /* Check the size of the database file. If it consists of 0 pages,
       
 34723       ** then delete the journal file. See the header comment above for 
       
 34724       ** the reasoning here.  Delete the obsolete journal file under
       
 34725       ** a RESERVED lock to avoid race conditions and to avoid violating
       
 34726       ** [H33020].
       
 34727       */
       
 34728       rc = sqlite3PagerPagecount(pPager, &nPage);
       
 34729       if( rc==SQLITE_OK ){
       
 34730         if( nPage==0 ){
       
 34731           sqlite3BeginBenignMalloc();
       
 34732           if( sqlite3OsLock(pPager->fd, RESERVED_LOCK)==SQLITE_OK ){
       
 34733             sqlite3OsDelete(pVfs, pPager->zJournal, 0);
       
 34734             sqlite3OsUnlock(pPager->fd, SHARED_LOCK);
       
 34735           }
       
 34736           sqlite3EndBenignMalloc();
       
 34737         }else{
       
 34738           /* The journal file exists and no other connection has a reserved
       
 34739           ** or greater lock on the database file. Now check that there is
       
 34740           ** at least one non-zero bytes at the start of the journal file.
       
 34741           ** If there is, then we consider this journal to be hot. If not, 
       
 34742           ** it can be ignored.
       
 34743           */
       
 34744           int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
       
 34745           rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
       
 34746           if( rc==SQLITE_OK ){
       
 34747             u8 first = 0;
       
 34748             rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
       
 34749             if( rc==SQLITE_IOERR_SHORT_READ ){
       
 34750               rc = SQLITE_OK;
       
 34751             }
       
 34752             sqlite3OsClose(pPager->jfd);
       
 34753             *pExists = (first!=0);
       
 34754           }else if( rc==SQLITE_CANTOPEN ){
       
 34755             /* If we cannot open the rollback journal file in order to see if
       
 34756             ** its has a zero header, that might be due to an I/O error, or
       
 34757             ** it might be due to the race condition described above and in
       
 34758             ** ticket #3883.  Either way, assume that the journal is hot.
       
 34759             ** This might be a false positive.  But if it is, then the
       
 34760             ** automatic journal playback and recovery mechanism will deal
       
 34761             ** with it under an EXCLUSIVE lock where we do not need to
       
 34762             ** worry so much with race conditions.
       
 34763             */
       
 34764             *pExists = 1;
       
 34765             rc = SQLITE_OK;
       
 34766           }
       
 34767         }
       
 34768       }
       
 34769     }
       
 34770   }
       
 34771 
       
 34772   return rc;
       
 34773 }
       
 34774 
       
 34775 /*
       
 34776 ** Read the content for page pPg out of the database file and into 
       
 34777 ** pPg->pData. A shared lock or greater must be held on the database
       
 34778 ** file before this function is called.
       
 34779 **
       
 34780 ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
       
 34781 ** the value read from the database file.
       
 34782 **
       
 34783 ** If an IO error occurs, then the IO error is returned to the caller.
       
 34784 ** Otherwise, SQLITE_OK is returned.
       
 34785 */
       
 34786 static int readDbPage(PgHdr *pPg){
       
 34787   Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
       
 34788   Pgno pgno = pPg->pgno;       /* Page number to read */
       
 34789   int rc;                      /* Return code */
       
 34790   i64 iOffset;                 /* Byte offset of file to read from */
       
 34791 
       
 34792   assert( pPager->state>=PAGER_SHARED && !MEMDB );
       
 34793   assert( isOpen(pPager->fd) );
       
 34794 
       
 34795   if( NEVER(!isOpen(pPager->fd)) ){
       
 34796     assert( pPager->tempFile );
       
 34797     memset(pPg->pData, 0, pPager->pageSize);
       
 34798     return SQLITE_OK;
       
 34799   }
       
 34800   iOffset = (pgno-1)*(i64)pPager->pageSize;
       
 34801   rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
       
 34802   if( rc==SQLITE_IOERR_SHORT_READ ){
       
 34803     rc = SQLITE_OK;
       
 34804   }
       
 34805   if( pgno==1 ){
       
 34806     u8 *dbFileVers = &((u8*)pPg->pData)[24];
       
 34807     memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
       
 34808   }
       
 34809   CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
       
 34810 
       
 34811   PAGER_INCR(sqlite3_pager_readdb_count);
       
 34812   PAGER_INCR(pPager->nRead);
       
 34813   IOTRACE(("PGIN %p %d\n", pPager, pgno));
       
 34814   PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
       
 34815                PAGERID(pPager), pgno, pager_pagehash(pPg)));
       
 34816 
       
 34817   return rc;
       
 34818 }
       
 34819 
       
 34820 /*
       
 34821 ** This function is called to obtain a shared lock on the database file.
       
 34822 ** It is illegal to call sqlite3PagerAcquire() until after this function
       
 34823 ** has been successfully called. If a shared-lock is already held when
       
 34824 ** this function is called, it is a no-op.
       
 34825 **
       
 34826 ** The following operations are also performed by this function.
       
 34827 **
       
 34828 **   1) If the pager is currently in PAGER_UNLOCK state (no lock held
       
 34829 **      on the database file), then an attempt is made to obtain a
       
 34830 **      SHARED lock on the database file. Immediately after obtaining
       
 34831 **      the SHARED lock, the file-system is checked for a hot-journal,
       
 34832 **      which is played back if present. Following any hot-journal 
       
 34833 **      rollback, the contents of the cache are validated by checking
       
 34834 **      the 'change-counter' field of the database file header and
       
 34835 **      discarded if they are found to be invalid.
       
 34836 **
       
 34837 **   2) If the pager is running in exclusive-mode, and there are currently
       
 34838 **      no outstanding references to any pages, and is in the error state,
       
 34839 **      then an attempt is made to clear the error state by discarding
       
 34840 **      the contents of the page cache and rolling back any open journal
       
 34841 **      file.
       
 34842 **
       
 34843 ** If the operation described by (2) above is not attempted, and if the
       
 34844 ** pager is in an error state other than SQLITE_FULL when this is called,
       
 34845 ** the error state error code is returned. It is permitted to read the
       
 34846 ** database when in SQLITE_FULL error state.
       
 34847 **
       
 34848 ** Otherwise, if everything is successful, SQLITE_OK is returned. If an
       
 34849 ** IO error occurs while locking the database, checking for a hot-journal
       
 34850 ** file or rolling back a journal file, the IO error code is returned.
       
 34851 */
       
 34852 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
       
 34853   int rc = SQLITE_OK;                /* Return code */
       
 34854   int isErrorReset = 0;              /* True if recovering from error state */
       
 34855 
       
 34856   /* This routine is only called from b-tree and only when there are no
       
 34857   ** outstanding pages */
       
 34858   assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
       
 34859   if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
       
 34860 
       
 34861   /* If this database is in an error-state, now is a chance to clear
       
 34862   ** the error. Discard the contents of the pager-cache and rollback
       
 34863   ** any hot journal in the file-system.
       
 34864   */
       
 34865   if( pPager->errCode ){
       
 34866     if( isOpen(pPager->jfd) || pPager->zJournal ){
       
 34867       isErrorReset = 1;
       
 34868     }
       
 34869     pPager->errCode = SQLITE_OK;
       
 34870     pager_reset(pPager);
       
 34871   }
       
 34872 
       
 34873   if( pPager->state==PAGER_UNLOCK || isErrorReset ){
       
 34874     sqlite3_vfs * const pVfs = pPager->pVfs;
       
 34875     int isHotJournal = 0;
       
 34876     assert( !MEMDB );
       
 34877     assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
       
 34878     if( pPager->noReadlock ){
       
 34879       assert( pPager->readOnly );
       
 34880       pPager->state = PAGER_SHARED;
       
 34881     }else{
       
 34882       rc = pager_wait_on_lock(pPager, SHARED_LOCK);
       
 34883       if( rc!=SQLITE_OK ){
       
 34884         assert( pPager->state==PAGER_UNLOCK );
       
 34885         return pager_error(pPager, rc);
       
 34886       }
       
 34887     }
       
 34888     assert( pPager->state>=SHARED_LOCK );
       
 34889 
       
 34890     /* If a journal file exists, and there is no RESERVED lock on the
       
 34891     ** database file, then it either needs to be played back or deleted.
       
 34892     */
       
 34893     if( !isErrorReset ){
       
 34894       assert( pPager->state <= PAGER_SHARED );
       
 34895       rc = hasHotJournal(pPager, &isHotJournal);
       
 34896       if( rc!=SQLITE_OK ){
       
 34897         goto failed;
       
 34898       }
       
 34899     }
       
 34900     if( isErrorReset || isHotJournal ){
       
 34901       /* Get an EXCLUSIVE lock on the database file. At this point it is
       
 34902       ** important that a RESERVED lock is not obtained on the way to the
       
 34903       ** EXCLUSIVE lock. If it were, another process might open the
       
 34904       ** database file, detect the RESERVED lock, and conclude that the
       
 34905       ** database is safe to read while this process is still rolling the 
       
 34906       ** hot-journal back.
       
 34907       ** 
       
 34908       ** Because the intermediate RESERVED lock is not requested, any
       
 34909       ** other process attempting to access the database file will get to 
       
 34910       ** this point in the code and fail to obtain its own EXCLUSIVE lock 
       
 34911       ** on the database file.
       
 34912       */
       
 34913       if( pPager->state<EXCLUSIVE_LOCK ){
       
 34914         rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
       
 34915         if( rc!=SQLITE_OK ){
       
 34916           rc = pager_error(pPager, rc);
       
 34917           goto failed;
       
 34918         }
       
 34919         pPager->state = PAGER_EXCLUSIVE;
       
 34920       }
       
 34921  
       
 34922       /* Open the journal for read/write access. This is because in 
       
 34923       ** exclusive-access mode the file descriptor will be kept open and
       
 34924       ** possibly used for a transaction later on. On some systems, the
       
 34925       ** OsTruncate() call used in exclusive-access mode also requires
       
 34926       ** a read/write file handle.
       
 34927       */
       
 34928       if( !isOpen(pPager->jfd) ){
       
 34929         int res;
       
 34930         rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
       
 34931         if( rc==SQLITE_OK ){
       
 34932           if( res ){
       
 34933             int fout = 0;
       
 34934             int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
       
 34935             assert( !pPager->tempFile );
       
 34936             rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
       
 34937             assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
       
 34938             if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
       
 34939               rc = SQLITE_CANTOPEN;
       
 34940               sqlite3OsClose(pPager->jfd);
       
 34941             }
       
 34942           }else{
       
 34943             /* If the journal does not exist, it usually means that some 
       
 34944             ** other connection managed to get in and roll it back before 
       
 34945             ** this connection obtained the exclusive lock above. Or, it 
       
 34946             ** may mean that the pager was in the error-state when this
       
 34947             ** function was called and the journal file does not exist.  */
       
 34948             rc = pager_end_transaction(pPager, 0);
       
 34949           }
       
 34950         }
       
 34951       }
       
 34952       if( rc!=SQLITE_OK ){
       
 34953         goto failed;
       
 34954       }
       
 34955 
       
 34956       /* TODO: Why are these cleared here? Is it necessary? */
       
 34957       pPager->journalStarted = 0;
       
 34958       pPager->journalOff = 0;
       
 34959       pPager->setMaster = 0;
       
 34960       pPager->journalHdr = 0;
       
 34961  
       
 34962       /* Playback and delete the journal.  Drop the database write
       
 34963       ** lock and reacquire the read lock. Purge the cache before
       
 34964       ** playing back the hot-journal so that we don't end up with
       
 34965       ** an inconsistent cache.
       
 34966       */
       
 34967       if( isOpen(pPager->jfd) ){
       
 34968         rc = pager_playback(pPager, 1);
       
 34969         if( rc!=SQLITE_OK ){
       
 34970           rc = pager_error(pPager, rc);
       
 34971           goto failed;
       
 34972         }
       
 34973       }
       
 34974       assert( (pPager->state==PAGER_SHARED)
       
 34975            || (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
       
 34976       );
       
 34977     }
       
 34978 
       
 34979     if( pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0 ){
       
 34980       /* The shared-lock has just been acquired on the database file
       
 34981       ** and there are already pages in the cache (from a previous
       
 34982       ** read or write transaction).  Check to see if the database
       
 34983       ** has been modified.  If the database has changed, flush the
       
 34984       ** cache.
       
 34985       **
       
 34986       ** Database changes is detected by looking at 15 bytes beginning
       
 34987       ** at offset 24 into the file.  The first 4 of these 16 bytes are
       
 34988       ** a 32-bit counter that is incremented with each change.  The
       
 34989       ** other bytes change randomly with each file change when
       
 34990       ** a codec is in use.
       
 34991       ** 
       
 34992       ** There is a vanishingly small chance that a change will not be 
       
 34993       ** detected.  The chance of an undetected change is so small that
       
 34994       ** it can be neglected.
       
 34995       */
       
 34996       char dbFileVers[sizeof(pPager->dbFileVers)];
       
 34997       sqlite3PagerPagecount(pPager, 0);
       
 34998 
       
 34999       if( pPager->errCode ){
       
 35000         rc = pPager->errCode;
       
 35001         goto failed;
       
 35002       }
       
 35003 
       
 35004       assert( pPager->dbSizeValid );
       
 35005       if( pPager->dbSize>0 ){
       
 35006         IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
       
 35007         rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
       
 35008         if( rc!=SQLITE_OK ){
       
 35009           goto failed;
       
 35010         }
       
 35011       }else{
       
 35012         memset(dbFileVers, 0, sizeof(dbFileVers));
       
 35013       }
       
 35014 
       
 35015       if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
       
 35016         pager_reset(pPager);
       
 35017       }
       
 35018     }
       
 35019     assert( pPager->exclusiveMode || pPager->state==PAGER_SHARED );
       
 35020   }
       
 35021 
       
 35022  failed:
       
 35023   if( rc!=SQLITE_OK ){
       
 35024     /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
       
 35025     pager_unlock(pPager);
       
 35026   }
       
 35027   return rc;
       
 35028 }
       
 35029 
       
 35030 /*
       
 35031 ** If the reference count has reached zero, rollback any active
       
 35032 ** transaction and unlock the pager.
       
 35033 **
       
 35034 ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
       
 35035 ** the rollback journal, the unlock is not performed and there is
       
 35036 ** nothing to rollback, so this routine is a no-op.
       
 35037 */ 
       
 35038 static void pagerUnlockIfUnused(Pager *pPager){
       
 35039   if( (sqlite3PcacheRefCount(pPager->pPCache)==0)
       
 35040    && (!pPager->exclusiveMode || pPager->journalOff>0) 
       
 35041   ){
       
 35042     pagerUnlockAndRollback(pPager);
       
 35043   }
       
 35044 }
       
 35045 
       
 35046 /*
       
 35047 ** Acquire a reference to page number pgno in pager pPager (a page
       
 35048 ** reference has type DbPage*). If the requested reference is 
       
 35049 ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
       
 35050 **
       
 35051 ** If the requested page is already in the cache, it is returned. 
       
 35052 ** Otherwise, a new page object is allocated and populated with data
       
 35053 ** read from the database file. In some cases, the pcache module may
       
 35054 ** choose not to allocate a new page object and may reuse an existing
       
 35055 ** object with no outstanding references.
       
 35056 **
       
 35057 ** The extra data appended to a page is always initialized to zeros the 
       
 35058 ** first time a page is loaded into memory. If the page requested is 
       
 35059 ** already in the cache when this function is called, then the extra
       
 35060 ** data is left as it was when the page object was last used.
       
 35061 **
       
 35062 ** If the database image is smaller than the requested page or if a 
       
 35063 ** non-zero value is passed as the noContent parameter and the 
       
 35064 ** requested page is not already stored in the cache, then no 
       
 35065 ** actual disk read occurs. In this case the memory image of the 
       
 35066 ** page is initialized to all zeros. 
       
 35067 **
       
 35068 ** If noContent is true, it means that we do not care about the contents
       
 35069 ** of the page. This occurs in two seperate scenarios:
       
 35070 **
       
 35071 **   a) When reading a free-list leaf page from the database, and
       
 35072 **
       
 35073 **   b) When a savepoint is being rolled back and we need to load
       
 35074 **      a new page into the cache to populate with the data read
       
 35075 **      from the savepoint journal.
       
 35076 **
       
 35077 ** If noContent is true, then the data returned is zeroed instead of
       
 35078 ** being read from the database. Additionally, the bits corresponding
       
 35079 ** to pgno in Pager.pInJournal (bitvec of pages already written to the
       
 35080 ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
       
 35081 ** savepoints are set. This means if the page is made writable at any
       
 35082 ** point in the future, using a call to sqlite3PagerWrite(), its contents
       
 35083 ** will not be journaled. This saves IO.
       
 35084 **
       
 35085 ** The acquisition might fail for several reasons.  In all cases,
       
 35086 ** an appropriate error code is returned and *ppPage is set to NULL.
       
 35087 **
       
 35088 ** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
       
 35089 ** to find a page in the in-memory cache first.  If the page is not already
       
 35090 ** in memory, this routine goes to disk to read it in whereas Lookup()
       
 35091 ** just returns 0.  This routine acquires a read-lock the first time it
       
 35092 ** has to go to disk, and could also playback an old journal if necessary.
       
 35093 ** Since Lookup() never goes to disk, it never has to deal with locks
       
 35094 ** or journal files.
       
 35095 */
       
 35096 SQLITE_PRIVATE int sqlite3PagerAcquire(
       
 35097   Pager *pPager,      /* The pager open on the database file */
       
 35098   Pgno pgno,          /* Page number to fetch */
       
 35099   DbPage **ppPage,    /* Write a pointer to the page here */
       
 35100   int noContent       /* Do not bother reading content from disk if true */
       
 35101 ){
       
 35102   int rc;
       
 35103   PgHdr *pPg;
       
 35104 
       
 35105   assert( assert_pager_state(pPager) );
       
 35106   assert( pPager->state>PAGER_UNLOCK );
       
 35107 
       
 35108   if( pgno==0 ){
       
 35109     return SQLITE_CORRUPT_BKPT;
       
 35110   }
       
 35111 
       
 35112   /* If the pager is in the error state, return an error immediately. 
       
 35113   ** Otherwise, request the page from the PCache layer. */
       
 35114   if( pPager->errCode!=SQLITE_OK && pPager->errCode!=SQLITE_FULL ){
       
 35115     rc = pPager->errCode;
       
 35116   }else{
       
 35117     rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
       
 35118   }
       
 35119 
       
 35120   if( rc!=SQLITE_OK ){
       
 35121     /* Either the call to sqlite3PcacheFetch() returned an error or the
       
 35122     ** pager was already in the error-state when this function was called.
       
 35123     ** Set pPg to 0 and jump to the exception handler.  */
       
 35124     pPg = 0;
       
 35125     goto pager_acquire_err;
       
 35126   }
       
 35127   assert( (*ppPage)->pgno==pgno );
       
 35128   assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
       
 35129 
       
 35130   if( (*ppPage)->pPager ){
       
 35131     /* In this case the pcache already contains an initialized copy of
       
 35132     ** the page. Return without further ado.  */
       
 35133     assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
       
 35134     PAGER_INCR(pPager->nHit);
       
 35135     return SQLITE_OK;
       
 35136 
       
 35137   }else{
       
 35138     /* The pager cache has created a new page. Its content needs to 
       
 35139     ** be initialized.  */
       
 35140     int nMax;
       
 35141 
       
 35142     PAGER_INCR(pPager->nMiss);
       
 35143     pPg = *ppPage;
       
 35144     pPg->pPager = pPager;
       
 35145 
       
 35146     /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
       
 35147     ** number greater than this, or the unused locking-page, is requested. */
       
 35148     if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
       
 35149       rc = SQLITE_CORRUPT_BKPT;
       
 35150       goto pager_acquire_err;
       
 35151     }
       
 35152 
       
 35153     rc = sqlite3PagerPagecount(pPager, &nMax);
       
 35154     if( rc!=SQLITE_OK ){
       
 35155       goto pager_acquire_err;
       
 35156     }
       
 35157 
       
 35158     if( nMax<(int)pgno || MEMDB || noContent ){
       
 35159       if( pgno>pPager->mxPgno ){
       
 35160 	rc = SQLITE_FULL;
       
 35161 	goto pager_acquire_err;
       
 35162       }
       
 35163       if( noContent ){
       
 35164         /* Failure to set the bits in the InJournal bit-vectors is benign.
       
 35165         ** It merely means that we might do some extra work to journal a 
       
 35166         ** page that does not need to be journaled.  Nevertheless, be sure 
       
 35167         ** to test the case where a malloc error occurs while trying to set 
       
 35168         ** a bit in a bit vector.
       
 35169         */
       
 35170         sqlite3BeginBenignMalloc();
       
 35171         if( pgno<=pPager->dbOrigSize ){
       
 35172           TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
       
 35173           testcase( rc==SQLITE_NOMEM );
       
 35174         }
       
 35175         TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
       
 35176         testcase( rc==SQLITE_NOMEM );
       
 35177         sqlite3EndBenignMalloc();
       
 35178       }else{
       
 35179         memset(pPg->pData, 0, pPager->pageSize);
       
 35180       }
       
 35181       IOTRACE(("ZERO %p %d\n", pPager, pgno));
       
 35182     }else{
       
 35183       assert( pPg->pPager==pPager );
       
 35184       rc = readDbPage(pPg);
       
 35185       if( rc!=SQLITE_OK ){
       
 35186         goto pager_acquire_err;
       
 35187       }
       
 35188     }
       
 35189 #ifdef SQLITE_CHECK_PAGES
       
 35190     pPg->pageHash = pager_pagehash(pPg);
       
 35191 #endif
       
 35192   }
       
 35193 
       
 35194   return SQLITE_OK;
       
 35195 
       
 35196 pager_acquire_err:
       
 35197   assert( rc!=SQLITE_OK );
       
 35198   if( pPg ){
       
 35199     sqlite3PcacheDrop(pPg);
       
 35200   }
       
 35201   pagerUnlockIfUnused(pPager);
       
 35202 
       
 35203   *ppPage = 0;
       
 35204   return rc;
       
 35205 }
       
 35206 
       
 35207 /*
       
 35208 ** Acquire a page if it is already in the in-memory cache.  Do
       
 35209 ** not read the page from disk.  Return a pointer to the page,
       
 35210 ** or 0 if the page is not in cache. Also, return 0 if the 
       
 35211 ** pager is in PAGER_UNLOCK state when this function is called,
       
 35212 ** or if the pager is in an error state other than SQLITE_FULL.
       
 35213 **
       
 35214 ** See also sqlite3PagerGet().  The difference between this routine
       
 35215 ** and sqlite3PagerGet() is that _get() will go to the disk and read
       
 35216 ** in the page if the page is not already in cache.  This routine
       
 35217 ** returns NULL if the page is not in cache or if a disk I/O error 
       
 35218 ** has ever happened.
       
 35219 */
       
 35220 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
       
 35221   PgHdr *pPg = 0;
       
 35222   assert( pPager!=0 );
       
 35223   assert( pgno!=0 );
       
 35224   assert( pPager->pPCache!=0 );
       
 35225   assert( pPager->state > PAGER_UNLOCK );
       
 35226   sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
       
 35227   return pPg;
       
 35228 }
       
 35229 
       
 35230 /*
       
 35231 ** Release a page reference.
       
 35232 **
       
 35233 ** If the number of references to the page drop to zero, then the
       
 35234 ** page is added to the LRU list.  When all references to all pages
       
 35235 ** are released, a rollback occurs and the lock on the database is
       
 35236 ** removed.
       
 35237 */
       
 35238 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
       
 35239   if( pPg ){
       
 35240     Pager *pPager = pPg->pPager;
       
 35241     sqlite3PcacheRelease(pPg);
       
 35242     pagerUnlockIfUnused(pPager);
       
 35243   }
       
 35244 }
       
 35245 
       
 35246 /*
       
 35247 ** If the main journal file has already been opened, ensure that the
       
 35248 ** sub-journal file is open too. If the main journal is not open,
       
 35249 ** this function is a no-op.
       
 35250 **
       
 35251 ** SQLITE_OK is returned if everything goes according to plan. 
       
 35252 ** An SQLITE_IOERR_XXX error code is returned if a call to 
       
 35253 ** sqlite3OsOpen() fails.
       
 35254 */
       
 35255 static int openSubJournal(Pager *pPager){
       
 35256   int rc = SQLITE_OK;
       
 35257   if( isOpen(pPager->jfd) && !isOpen(pPager->sjfd) ){
       
 35258     if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
       
 35259       sqlite3MemJournalOpen(pPager->sjfd);
       
 35260     }else{
       
 35261       rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
       
 35262     }
       
 35263   }
       
 35264   return rc;
       
 35265 }
       
 35266 
       
 35267 /*
       
 35268 ** This function is called at the start of every write transaction.
       
 35269 ** There must already be a RESERVED or EXCLUSIVE lock on the database 
       
 35270 ** file when this routine is called.
       
 35271 **
       
 35272 ** Open the journal file for pager pPager and write a journal header
       
 35273 ** to the start of it. If there are active savepoints, open the sub-journal
       
 35274 ** as well. This function is only used when the journal file is being 
       
 35275 ** opened to write a rollback log for a transaction. It is not used 
       
 35276 ** when opening a hot journal file to roll it back.
       
 35277 **
       
 35278 ** If the journal file is already open (as it may be in exclusive mode),
       
 35279 ** then this function just writes a journal header to the start of the
       
 35280 ** already open file. 
       
 35281 **
       
 35282 ** Whether or not the journal file is opened by this function, the
       
 35283 ** Pager.pInJournal bitvec structure is allocated.
       
 35284 **
       
 35285 ** Return SQLITE_OK if everything is successful. Otherwise, return 
       
 35286 ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or 
       
 35287 ** an IO error code if opening or writing the journal file fails.
       
 35288 */
       
 35289 static int pager_open_journal(Pager *pPager){
       
 35290   int rc = SQLITE_OK;                        /* Return code */
       
 35291   sqlite3_vfs * const pVfs = pPager->pVfs;   /* Local cache of vfs pointer */
       
 35292 
       
 35293   assert( pPager->state>=PAGER_RESERVED );
       
 35294   assert( pPager->useJournal );
       
 35295   assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF );
       
 35296   assert( pPager->pInJournal==0 );
       
 35297   
       
 35298   /* If already in the error state, this function is a no-op.  But on
       
 35299   ** the other hand, this routine is never called if we are already in
       
 35300   ** an error state. */
       
 35301   if( NEVER(pPager->errCode) ) return pPager->errCode;
       
 35302 
       
 35303   /* TODO: Is it really possible to get here with dbSizeValid==0? If not,
       
 35304   ** the call to PagerPagecount() can be removed.
       
 35305   */
       
 35306   testcase( pPager->dbSizeValid==0 );
       
 35307   sqlite3PagerPagecount(pPager, 0);
       
 35308 
       
 35309   pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
       
 35310   if( pPager->pInJournal==0 ){
       
 35311     return SQLITE_NOMEM;
       
 35312   }
       
 35313 
       
 35314   /* Open the journal file if it is not already open. */
       
 35315   if( !isOpen(pPager->jfd) ){
       
 35316     if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
       
 35317       sqlite3MemJournalOpen(pPager->jfd);
       
 35318     }else{
       
 35319       const int flags =                   /* VFS flags to open journal file */
       
 35320         SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
       
 35321         (pPager->tempFile ? 
       
 35322           (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
       
 35323           (SQLITE_OPEN_MAIN_JOURNAL)
       
 35324         );
       
 35325 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
       
 35326       rc = sqlite3JournalOpen(
       
 35327           pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
       
 35328       );
       
 35329 #else
       
 35330       rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
       
 35331 #endif
       
 35332     }
       
 35333     assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
       
 35334   }
       
 35335 
       
 35336 
       
 35337   /* Write the first journal header to the journal file and open 
       
 35338   ** the sub-journal if necessary.
       
 35339   */
       
 35340   if( rc==SQLITE_OK ){
       
 35341     /* TODO: Check if all of these are really required. */
       
 35342     pPager->dbOrigSize = pPager->dbSize;
       
 35343     pPager->journalStarted = 0;
       
 35344     pPager->needSync = 0;
       
 35345     pPager->nRec = 0;
       
 35346     pPager->journalOff = 0;
       
 35347     pPager->setMaster = 0;
       
 35348     pPager->journalHdr = 0;
       
 35349     rc = writeJournalHdr(pPager);
       
 35350   }
       
 35351   if( rc==SQLITE_OK && pPager->nSavepoint ){
       
 35352     rc = openSubJournal(pPager);
       
 35353   }
       
 35354 
       
 35355   if( rc!=SQLITE_OK ){
       
 35356     sqlite3BitvecDestroy(pPager->pInJournal);
       
 35357     pPager->pInJournal = 0;
       
 35358   }
       
 35359   return rc;
       
 35360 }
       
 35361 
       
 35362 /*
       
 35363 ** Begin a write-transaction on the specified pager object. If a 
       
 35364 ** write-transaction has already been opened, this function is a no-op.
       
 35365 **
       
 35366 ** If the exFlag argument is false, then acquire at least a RESERVED
       
 35367 ** lock on the database file. If exFlag is true, then acquire at least
       
 35368 ** an EXCLUSIVE lock. If such a lock is already held, no locking 
       
 35369 ** functions need be called.
       
 35370 **
       
 35371 ** If this is not a temporary or in-memory file and, the journal file is 
       
 35372 ** opened if it has not been already. For a temporary file, the opening 
       
 35373 ** of the journal file is deferred until there is an actual need to 
       
 35374 ** write to the journal. TODO: Why handle temporary files differently?
       
 35375 **
       
 35376 ** If the journal file is opened (or if it is already open), then a
       
 35377 ** journal-header is written to the start of it.
       
 35378 **
       
 35379 ** If the subjInMemory argument is non-zero, then any sub-journal opened
       
 35380 ** within this transaction will be opened as an in-memory file. This
       
 35381 ** has no effect if the sub-journal is already opened (as it may be when
       
 35382 ** running in exclusive mode) or if the transaction does not require a
       
 35383 ** sub-journal. If the subjInMemory argument is zero, then any required
       
 35384 ** sub-journal is implemented in-memory if pPager is an in-memory database, 
       
 35385 ** or using a temporary file otherwise.
       
 35386 */
       
 35387 SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
       
 35388   int rc = SQLITE_OK;
       
 35389   assert( pPager->state!=PAGER_UNLOCK );
       
 35390   pPager->subjInMemory = (u8)subjInMemory;
       
 35391   if( pPager->state==PAGER_SHARED ){
       
 35392     assert( pPager->pInJournal==0 );
       
 35393     assert( !MEMDB && !pPager->tempFile );
       
 35394 
       
 35395     /* Obtain a RESERVED lock on the database file. If the exFlag parameter
       
 35396     ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
       
 35397     ** busy-handler callback can be used when upgrading to the EXCLUSIVE
       
 35398     ** lock, but not when obtaining the RESERVED lock.
       
 35399     */
       
 35400     rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
       
 35401     if( rc==SQLITE_OK ){
       
 35402       pPager->state = PAGER_RESERVED;
       
 35403       if( exFlag ){
       
 35404         rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
       
 35405       }
       
 35406     }
       
 35407 
       
 35408     /* If the required locks were successfully obtained, open the journal
       
 35409     ** file and write the first journal-header to it.
       
 35410     */
       
 35411     if( rc==SQLITE_OK && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
       
 35412       rc = pager_open_journal(pPager);
       
 35413     }
       
 35414   }else if( isOpen(pPager->jfd) && pPager->journalOff==0 ){
       
 35415     /* This happens when the pager was in exclusive-access mode the last
       
 35416     ** time a (read or write) transaction was successfully concluded
       
 35417     ** by this connection. Instead of deleting the journal file it was 
       
 35418     ** kept open and either was truncated to 0 bytes or its header was
       
 35419     ** overwritten with zeros.
       
 35420     */
       
 35421     assert( pPager->nRec==0 );
       
 35422     assert( pPager->dbOrigSize==0 );
       
 35423     assert( pPager->pInJournal==0 );
       
 35424     rc = pager_open_journal(pPager);
       
 35425   }
       
 35426 
       
 35427   PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
       
 35428   assert( !isOpen(pPager->jfd) || pPager->journalOff>0 || rc!=SQLITE_OK );
       
 35429   if( rc!=SQLITE_OK ){
       
 35430     assert( !pPager->dbModified );
       
 35431     /* Ignore any IO error that occurs within pager_end_transaction(). The
       
 35432     ** purpose of this call is to reset the internal state of the pager
       
 35433     ** sub-system. It doesn't matter if the journal-file is not properly
       
 35434     ** finalized at this point (since it is not a valid journal file anyway).
       
 35435     */
       
 35436     pager_end_transaction(pPager, 0);
       
 35437   }
       
 35438   return rc;
       
 35439 }
       
 35440 
       
 35441 /*
       
 35442 ** Mark a single data page as writeable. The page is written into the 
       
 35443 ** main journal or sub-journal as required. If the page is written into
       
 35444 ** one of the journals, the corresponding bit is set in the 
       
 35445 ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
       
 35446 ** of any open savepoints as appropriate.
       
 35447 */
       
 35448 static int pager_write(PgHdr *pPg){
       
 35449   void *pData = pPg->pData;
       
 35450   Pager *pPager = pPg->pPager;
       
 35451   int rc = SQLITE_OK;
       
 35452 
       
 35453   /* This routine is not called unless a transaction has already been
       
 35454   ** started.
       
 35455   */
       
 35456   assert( pPager->state>=PAGER_RESERVED );
       
 35457 
       
 35458   /* If an error has been previously detected, we should not be
       
 35459   ** calling this routine.  Repeat the error for robustness.
       
 35460   */
       
 35461   if( NEVER(pPager->errCode) )  return pPager->errCode;
       
 35462 
       
 35463   /* Higher-level routines never call this function if database is not
       
 35464   ** writable.  But check anyway, just for robustness. */
       
 35465   if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
       
 35466 
       
 35467   assert( !pPager->setMaster );
       
 35468 
       
 35469   CHECK_PAGE(pPg);
       
 35470 
       
 35471   /* Mark the page as dirty.  If the page has already been written
       
 35472   ** to the journal then we can return right away.
       
 35473   */
       
 35474   sqlite3PcacheMakeDirty(pPg);
       
 35475   if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
       
 35476     pPager->dbModified = 1;
       
 35477   }else{
       
 35478 
       
 35479     /* If we get this far, it means that the page needs to be
       
 35480     ** written to the transaction journal or the ckeckpoint journal
       
 35481     ** or both.
       
 35482     **
       
 35483     ** Higher level routines should have already started a transaction,
       
 35484     ** which means they have acquired the necessary locks and opened
       
 35485     ** a rollback journal.  Double-check to makes sure this is the case.
       
 35486     */
       
 35487     rc = sqlite3PagerBegin(pPager, 0, pPager->subjInMemory);
       
 35488     if( NEVER(rc!=SQLITE_OK) ){
       
 35489       return rc;
       
 35490     }
       
 35491     if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
       
 35492       assert( pPager->useJournal );
       
 35493       rc = pager_open_journal(pPager);
       
 35494       if( rc!=SQLITE_OK ) return rc;
       
 35495     }
       
 35496     pPager->dbModified = 1;
       
 35497   
       
 35498     /* The transaction journal now exists and we have a RESERVED or an
       
 35499     ** EXCLUSIVE lock on the main database file.  Write the current page to
       
 35500     ** the transaction journal if it is not there already.
       
 35501     */
       
 35502     if( !pageInJournal(pPg) && isOpen(pPager->jfd) ){
       
 35503       if( pPg->pgno<=pPager->dbOrigSize ){
       
 35504         u32 cksum;
       
 35505         char *pData2;
       
 35506 
       
 35507         /* We should never write to the journal file the page that
       
 35508         ** contains the database locks.  The following assert verifies
       
 35509         ** that we do not. */
       
 35510         assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
       
 35511         CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
       
 35512         cksum = pager_cksum(pPager, (u8*)pData2);
       
 35513         rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
       
 35514         if( rc==SQLITE_OK ){
       
 35515           rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
       
 35516                               pPager->journalOff + 4);
       
 35517           pPager->journalOff += pPager->pageSize+4;
       
 35518         }
       
 35519         if( rc==SQLITE_OK ){
       
 35520           rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
       
 35521           pPager->journalOff += 4;
       
 35522         }
       
 35523         IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, 
       
 35524                  pPager->journalOff, pPager->pageSize));
       
 35525         PAGER_INCR(sqlite3_pager_writej_count);
       
 35526         PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
       
 35527              PAGERID(pPager), pPg->pgno, 
       
 35528              ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
       
 35529 
       
 35530         /* Even if an IO or diskfull error occurred while journalling the
       
 35531         ** page in the block above, set the need-sync flag for the page.
       
 35532         ** Otherwise, when the transaction is rolled back, the logic in
       
 35533         ** playback_one_page() will think that the page needs to be restored
       
 35534         ** in the database file. And if an IO error occurs while doing so,
       
 35535         ** then corruption may follow.
       
 35536         */
       
 35537         if( !pPager->noSync ){
       
 35538           pPg->flags |= PGHDR_NEED_SYNC;
       
 35539           pPager->needSync = 1;
       
 35540         }
       
 35541 
       
 35542         /* An error has occurred writing to the journal file. The 
       
 35543         ** transaction will be rolled back by the layer above.
       
 35544         */
       
 35545         if( rc!=SQLITE_OK ){
       
 35546           return rc;
       
 35547         }
       
 35548 
       
 35549         pPager->nRec++;
       
 35550         assert( pPager->pInJournal!=0 );
       
 35551         rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
       
 35552         testcase( rc==SQLITE_NOMEM );
       
 35553         assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
       
 35554         rc |= addToSavepointBitvecs(pPager, pPg->pgno);
       
 35555         if( rc!=SQLITE_OK ){
       
 35556           assert( rc==SQLITE_NOMEM );
       
 35557           return rc;
       
 35558         }
       
 35559       }else{
       
 35560         if( !pPager->journalStarted && !pPager->noSync ){
       
 35561           pPg->flags |= PGHDR_NEED_SYNC;
       
 35562           pPager->needSync = 1;
       
 35563         }
       
 35564         PAGERTRACE(("APPEND %d page %d needSync=%d\n",
       
 35565                 PAGERID(pPager), pPg->pgno,
       
 35566                ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
       
 35567       }
       
 35568     }
       
 35569   
       
 35570     /* If the statement journal is open and the page is not in it,
       
 35571     ** then write the current page to the statement journal.  Note that
       
 35572     ** the statement journal format differs from the standard journal format
       
 35573     ** in that it omits the checksums and the header.
       
 35574     */
       
 35575     if( subjRequiresPage(pPg) ){
       
 35576       rc = subjournalPage(pPg);
       
 35577     }
       
 35578   }
       
 35579 
       
 35580   /* Update the database size and return.
       
 35581   */
       
 35582   assert( pPager->state>=PAGER_SHARED );
       
 35583   if( pPager->dbSize<pPg->pgno ){
       
 35584     pPager->dbSize = pPg->pgno;
       
 35585   }
       
 35586   return rc;
       
 35587 }
       
 35588 
       
 35589 /*
       
 35590 ** Mark a data page as writeable. This routine must be called before 
       
 35591 ** making changes to a page. The caller must check the return value 
       
 35592 ** of this function and be careful not to change any page data unless 
       
 35593 ** this routine returns SQLITE_OK.
       
 35594 **
       
 35595 ** The difference between this function and pager_write() is that this
       
 35596 ** function also deals with the special case where 2 or more pages
       
 35597 ** fit on a single disk sector. In this case all co-resident pages
       
 35598 ** must have been written to the journal file before returning.
       
 35599 **
       
 35600 ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
       
 35601 ** as appropriate. Otherwise, SQLITE_OK.
       
 35602 */
       
 35603 SQLITE_PRIVATE int sqlite3PagerWrite(DbPage *pDbPage){
       
 35604   int rc = SQLITE_OK;
       
 35605 
       
 35606   PgHdr *pPg = pDbPage;
       
 35607   Pager *pPager = pPg->pPager;
       
 35608   Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
       
 35609 
       
 35610   if( nPagePerSector>1 ){
       
 35611     Pgno nPageCount;          /* Total number of pages in database file */
       
 35612     Pgno pg1;                 /* First page of the sector pPg is located on. */
       
 35613     int nPage;                /* Number of pages starting at pg1 to journal */
       
 35614     int ii;                   /* Loop counter */
       
 35615     int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */
       
 35616 
       
 35617     /* Set the doNotSync flag to 1. This is because we cannot allow a journal
       
 35618     ** header to be written between the pages journaled by this function.
       
 35619     */
       
 35620     assert( !MEMDB );
       
 35621     assert( pPager->doNotSync==0 );
       
 35622     pPager->doNotSync = 1;
       
 35623 
       
 35624     /* This trick assumes that both the page-size and sector-size are
       
 35625     ** an integer power of 2. It sets variable pg1 to the identifier
       
 35626     ** of the first page of the sector pPg is located on.
       
 35627     */
       
 35628     pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
       
 35629 
       
 35630     sqlite3PagerPagecount(pPager, (int *)&nPageCount);
       
 35631     if( pPg->pgno>nPageCount ){
       
 35632       nPage = (pPg->pgno - pg1)+1;
       
 35633     }else if( (pg1+nPagePerSector-1)>nPageCount ){
       
 35634       nPage = nPageCount+1-pg1;
       
 35635     }else{
       
 35636       nPage = nPagePerSector;
       
 35637     }
       
 35638     assert(nPage>0);
       
 35639     assert(pg1<=pPg->pgno);
       
 35640     assert((pg1+nPage)>pPg->pgno);
       
 35641 
       
 35642     for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
       
 35643       Pgno pg = pg1+ii;
       
 35644       PgHdr *pPage;
       
 35645       if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
       
 35646         if( pg!=PAGER_MJ_PGNO(pPager) ){
       
 35647           rc = sqlite3PagerGet(pPager, pg, &pPage);
       
 35648           if( rc==SQLITE_OK ){
       
 35649             rc = pager_write(pPage);
       
 35650             if( pPage->flags&PGHDR_NEED_SYNC ){
       
 35651               needSync = 1;
       
 35652               assert(pPager->needSync);
       
 35653             }
       
 35654             sqlite3PagerUnref(pPage);
       
 35655           }
       
 35656         }
       
 35657       }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
       
 35658         if( pPage->flags&PGHDR_NEED_SYNC ){
       
 35659           needSync = 1;
       
 35660         }
       
 35661         sqlite3PagerUnref(pPage);
       
 35662       }
       
 35663     }
       
 35664 
       
 35665     /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages 
       
 35666     ** starting at pg1, then it needs to be set for all of them. Because
       
 35667     ** writing to any of these nPage pages may damage the others, the
       
 35668     ** journal file must contain sync()ed copies of all of them
       
 35669     ** before any of them can be written out to the database file.
       
 35670     */
       
 35671     if( rc==SQLITE_OK && needSync ){
       
 35672       assert( !MEMDB && pPager->noSync==0 );
       
 35673       for(ii=0; ii<nPage; ii++){
       
 35674         PgHdr *pPage = pager_lookup(pPager, pg1+ii);
       
 35675         if( pPage ){
       
 35676           pPage->flags |= PGHDR_NEED_SYNC;
       
 35677           sqlite3PagerUnref(pPage);
       
 35678         }
       
 35679       }
       
 35680       assert(pPager->needSync);
       
 35681     }
       
 35682 
       
 35683     assert( pPager->doNotSync==1 );
       
 35684     pPager->doNotSync = 0;
       
 35685   }else{
       
 35686     rc = pager_write(pDbPage);
       
 35687   }
       
 35688   return rc;
       
 35689 }
       
 35690 
       
 35691 /*
       
 35692 ** Return TRUE if the page given in the argument was previously passed
       
 35693 ** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
       
 35694 ** to change the content of the page.
       
 35695 */
       
 35696 #ifndef NDEBUG
       
 35697 SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
       
 35698   return pPg->flags&PGHDR_DIRTY;
       
 35699 }
       
 35700 #endif
       
 35701 
       
 35702 /*
       
 35703 ** A call to this routine tells the pager that it is not necessary to
       
 35704 ** write the information on page pPg back to the disk, even though
       
 35705 ** that page might be marked as dirty.  This happens, for example, when
       
 35706 ** the page has been added as a leaf of the freelist and so its
       
 35707 ** content no longer matters.
       
 35708 **
       
 35709 ** The overlying software layer calls this routine when all of the data
       
 35710 ** on the given page is unused. The pager marks the page as clean so
       
 35711 ** that it does not get written to disk.
       
 35712 **
       
 35713 ** Tests show that this optimization can quadruple the speed of large 
       
 35714 ** DELETE operations.
       
 35715 */
       
 35716 SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
       
 35717   Pager *pPager = pPg->pPager;
       
 35718   if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
       
 35719     PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
       
 35720     IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
       
 35721     pPg->flags |= PGHDR_DONT_WRITE;
       
 35722 #ifdef SQLITE_CHECK_PAGES
       
 35723     pPg->pageHash = pager_pagehash(pPg);
       
 35724 #endif
       
 35725   }
       
 35726 }
       
 35727 
       
 35728 /*
       
 35729 ** This routine is called to increment the value of the database file 
       
 35730 ** change-counter, stored as a 4-byte big-endian integer starting at 
       
 35731 ** byte offset 24 of the pager file.
       
 35732 **
       
 35733 ** If the isDirectMode flag is zero, then this is done by calling 
       
 35734 ** sqlite3PagerWrite() on page 1, then modifying the contents of the
       
 35735 ** page data. In this case the file will be updated when the current
       
 35736 ** transaction is committed.
       
 35737 **
       
 35738 ** The isDirectMode flag may only be non-zero if the library was compiled
       
 35739 ** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
       
 35740 ** if isDirect is non-zero, then the database file is updated directly
       
 35741 ** by writing an updated version of page 1 using a call to the 
       
 35742 ** sqlite3OsWrite() function.
       
 35743 */
       
 35744 static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
       
 35745   int rc = SQLITE_OK;
       
 35746 
       
 35747   /* Declare and initialize constant integer 'isDirect'. If the
       
 35748   ** atomic-write optimization is enabled in this build, then isDirect
       
 35749   ** is initialized to the value passed as the isDirectMode parameter
       
 35750   ** to this function. Otherwise, it is always set to zero.
       
 35751   **
       
 35752   ** The idea is that if the atomic-write optimization is not
       
 35753   ** enabled at compile time, the compiler can omit the tests of
       
 35754   ** 'isDirect' below, as well as the block enclosed in the
       
 35755   ** "if( isDirect )" condition.
       
 35756   */
       
 35757 #ifndef SQLITE_ENABLE_ATOMIC_WRITE
       
 35758 # define DIRECT_MODE 0
       
 35759   assert( isDirectMode==0 );
       
 35760   UNUSED_PARAMETER(isDirectMode);
       
 35761 #else
       
 35762 # define DIRECT_MODE isDirectMode
       
 35763 #endif
       
 35764 
       
 35765   assert( pPager->state>=PAGER_RESERVED );
       
 35766   if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
       
 35767     PgHdr *pPgHdr;                /* Reference to page 1 */
       
 35768     u32 change_counter;           /* Initial value of change-counter field */
       
 35769 
       
 35770     assert( !pPager->tempFile && isOpen(pPager->fd) );
       
 35771 
       
 35772     /* Open page 1 of the file for writing. */
       
 35773     rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
       
 35774     assert( pPgHdr==0 || rc==SQLITE_OK );
       
 35775 
       
 35776     /* If page one was fetched successfully, and this function is not
       
 35777     ** operating in direct-mode, make page 1 writable.  When not in 
       
 35778     ** direct mode, page 1 is always held in cache and hence the PagerGet()
       
 35779     ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
       
 35780     */
       
 35781     if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
       
 35782       rc = sqlite3PagerWrite(pPgHdr);
       
 35783     }
       
 35784 
       
 35785     if( rc==SQLITE_OK ){
       
 35786       /* Increment the value just read and write it back to byte 24. */
       
 35787       change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
       
 35788       change_counter++;
       
 35789       put32bits(((char*)pPgHdr->pData)+24, change_counter);
       
 35790 
       
 35791       /* If running in direct mode, write the contents of page 1 to the file. */
       
 35792       if( DIRECT_MODE ){
       
 35793         const void *zBuf = pPgHdr->pData;
       
 35794         assert( pPager->dbFileSize>0 );
       
 35795         rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
       
 35796         if( rc==SQLITE_OK ){
       
 35797           pPager->changeCountDone = 1;
       
 35798         }
       
 35799       }else{
       
 35800         pPager->changeCountDone = 1;
       
 35801       }
       
 35802     }
       
 35803 
       
 35804     /* Release the page reference. */
       
 35805     sqlite3PagerUnref(pPgHdr);
       
 35806   }
       
 35807   return rc;
       
 35808 }
       
 35809 
       
 35810 /*
       
 35811 ** Sync the pager file to disk. This is a no-op for in-memory files
       
 35812 ** or pages with the Pager.noSync flag set.
       
 35813 **
       
 35814 ** If successful, or called on a pager for which it is a no-op, this
       
 35815 ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
       
 35816 */
       
 35817 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
       
 35818   int rc;                              /* Return code */
       
 35819   assert( !MEMDB );
       
 35820   if( pPager->noSync ){
       
 35821     rc = SQLITE_OK;
       
 35822   }else{
       
 35823     rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
       
 35824   }
       
 35825   return rc;
       
 35826 }
       
 35827 
       
 35828 /*
       
 35829 ** Sync the database file for the pager pPager. zMaster points to the name
       
 35830 ** of a master journal file that should be written into the individual
       
 35831 ** journal file. zMaster may be NULL, which is interpreted as no master
       
 35832 ** journal (a single database transaction).
       
 35833 **
       
 35834 ** This routine ensures that:
       
 35835 **
       
 35836 **   * The database file change-counter is updated,
       
 35837 **   * the journal is synced (unless the atomic-write optimization is used),
       
 35838 **   * all dirty pages are written to the database file, 
       
 35839 **   * the database file is truncated (if required), and
       
 35840 **   * the database file synced. 
       
 35841 **
       
 35842 ** The only thing that remains to commit the transaction is to finalize 
       
 35843 ** (delete, truncate or zero the first part of) the journal file (or 
       
 35844 ** delete the master journal file if specified).
       
 35845 **
       
 35846 ** Note that if zMaster==NULL, this does not overwrite a previous value
       
 35847 ** passed to an sqlite3PagerCommitPhaseOne() call.
       
 35848 **
       
 35849 ** If the final parameter - noSync - is true, then the database file itself
       
 35850 ** is not synced. The caller must call sqlite3PagerSync() directly to
       
 35851 ** sync the database file before calling CommitPhaseTwo() to delete the
       
 35852 ** journal file in this case.
       
 35853 */
       
 35854 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
       
 35855   Pager *pPager,                  /* Pager object */
       
 35856   const char *zMaster,            /* If not NULL, the master journal name */
       
 35857   int noSync                      /* True to omit the xSync on the db file */
       
 35858 ){
       
 35859   int rc = SQLITE_OK;             /* Return code */
       
 35860 
       
 35861   /* The dbOrigSize is never set if journal_mode=OFF */
       
 35862   assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 );
       
 35863 
       
 35864   /* If a prior error occurred, this routine should not be called.  ROLLBACK
       
 35865   ** is the appropriate response to an error, not COMMIT.  Guard against
       
 35866   ** coding errors by repeating the prior error. */
       
 35867   if( NEVER(pPager->errCode) ) return pPager->errCode;
       
 35868 
       
 35869   PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", 
       
 35870       pPager->zFilename, zMaster, pPager->dbSize));
       
 35871 
       
 35872   if( MEMDB && pPager->dbModified ){
       
 35873     /* If this is an in-memory db, or no pages have been written to, or this
       
 35874     ** function has already been called, it is mostly a no-op.  However, any
       
 35875     ** backup in progress needs to be restarted.
       
 35876     */
       
 35877     sqlite3BackupRestart(pPager->pBackup);
       
 35878   }else if( pPager->state!=PAGER_SYNCED && pPager->dbModified ){
       
 35879 
       
 35880     /* The following block updates the change-counter. Exactly how it
       
 35881     ** does this depends on whether or not the atomic-update optimization
       
 35882     ** was enabled at compile time, and if this transaction meets the 
       
 35883     ** runtime criteria to use the operation: 
       
 35884     **
       
 35885     **    * The file-system supports the atomic-write property for
       
 35886     **      blocks of size page-size, and 
       
 35887     **    * This commit is not part of a multi-file transaction, and
       
 35888     **    * Exactly one page has been modified and store in the journal file.
       
 35889     **
       
 35890     ** If the optimization was not enabled at compile time, then the
       
 35891     ** pager_incr_changecounter() function is called to update the change
       
 35892     ** counter in 'indirect-mode'. If the optimization is compiled in but
       
 35893     ** is not applicable to this transaction, call sqlite3JournalCreate()
       
 35894     ** to make sure the journal file has actually been created, then call
       
 35895     ** pager_incr_changecounter() to update the change-counter in indirect
       
 35896     ** mode. 
       
 35897     **
       
 35898     ** Otherwise, if the optimization is both enabled and applicable,
       
 35899     ** then call pager_incr_changecounter() to update the change-counter
       
 35900     ** in 'direct' mode. In this case the journal file will never be
       
 35901     ** created for this transaction.
       
 35902     */
       
 35903 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
       
 35904     PgHdr *pPg;
       
 35905     assert( isOpen(pPager->jfd) || pPager->journalMode==PAGER_JOURNALMODE_OFF );
       
 35906     if( !zMaster && isOpen(pPager->jfd) 
       
 35907      && pPager->journalOff==jrnlBufferSize(pPager) 
       
 35908      && pPager->dbSize>=pPager->dbFileSize
       
 35909      && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
       
 35910     ){
       
 35911       /* Update the db file change counter via the direct-write method. The 
       
 35912       ** following call will modify the in-memory representation of page 1 
       
 35913       ** to include the updated change counter and then write page 1 
       
 35914       ** directly to the database file. Because of the atomic-write 
       
 35915       ** property of the host file-system, this is safe.
       
 35916       */
       
 35917       rc = pager_incr_changecounter(pPager, 1);
       
 35918     }else{
       
 35919       rc = sqlite3JournalCreate(pPager->jfd);
       
 35920       if( rc==SQLITE_OK ){
       
 35921         rc = pager_incr_changecounter(pPager, 0);
       
 35922       }
       
 35923     }
       
 35924 #else
       
 35925     rc = pager_incr_changecounter(pPager, 0);
       
 35926 #endif
       
 35927     if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
       
 35928 
       
 35929     /* If this transaction has made the database smaller, then all pages
       
 35930     ** being discarded by the truncation must be written to the journal
       
 35931     ** file. This can only happen in auto-vacuum mode.
       
 35932     **
       
 35933     ** Before reading the pages with page numbers larger than the 
       
 35934     ** current value of Pager.dbSize, set dbSize back to the value
       
 35935     ** that it took at the start of the transaction. Otherwise, the
       
 35936     ** calls to sqlite3PagerGet() return zeroed pages instead of 
       
 35937     ** reading data from the database file.
       
 35938     **
       
 35939     ** When journal_mode==OFF the dbOrigSize is always zero, so this
       
 35940     ** block never runs if journal_mode=OFF.
       
 35941     */
       
 35942 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 35943     if( pPager->dbSize<pPager->dbOrigSize 
       
 35944      && ALWAYS(pPager->journalMode!=PAGER_JOURNALMODE_OFF)
       
 35945     ){
       
 35946       Pgno i;                                   /* Iterator variable */
       
 35947       const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
       
 35948       const Pgno dbSize = pPager->dbSize;       /* Database image size */ 
       
 35949       pPager->dbSize = pPager->dbOrigSize;
       
 35950       for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
       
 35951         if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
       
 35952           PgHdr *pPage;             /* Page to journal */
       
 35953           rc = sqlite3PagerGet(pPager, i, &pPage);
       
 35954           if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
       
 35955           rc = sqlite3PagerWrite(pPage);
       
 35956           sqlite3PagerUnref(pPage);
       
 35957           if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
       
 35958         }
       
 35959       } 
       
 35960       pPager->dbSize = dbSize;
       
 35961     }
       
 35962 #endif
       
 35963 
       
 35964     /* Write the master journal name into the journal file. If a master 
       
 35965     ** journal file name has already been written to the journal file, 
       
 35966     ** or if zMaster is NULL (no master journal), then this call is a no-op.
       
 35967     */
       
 35968     rc = writeMasterJournal(pPager, zMaster);
       
 35969     if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
       
 35970 
       
 35971     /* Sync the journal file. If the atomic-update optimization is being
       
 35972     ** used, this call will not create the journal file or perform any
       
 35973     ** real IO.
       
 35974     */
       
 35975     rc = syncJournal(pPager);
       
 35976     if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
       
 35977 
       
 35978     /* Write all dirty pages to the database file. */
       
 35979     rc = pager_write_pagelist(sqlite3PcacheDirtyList(pPager->pPCache));
       
 35980     if( rc!=SQLITE_OK ){
       
 35981       assert( rc!=SQLITE_IOERR_BLOCKED );
       
 35982       goto commit_phase_one_exit;
       
 35983     }
       
 35984     sqlite3PcacheCleanAll(pPager->pPCache);
       
 35985 
       
 35986     /* If the file on disk is not the same size as the database image,
       
 35987     ** then use pager_truncate to grow or shrink the file here.
       
 35988     */
       
 35989     if( pPager->dbSize!=pPager->dbFileSize ){
       
 35990       Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
       
 35991       assert( pPager->state>=PAGER_EXCLUSIVE );
       
 35992       rc = pager_truncate(pPager, nNew);
       
 35993       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
       
 35994     }
       
 35995 
       
 35996     /* Finally, sync the database file. */
       
 35997     if( !pPager->noSync && !noSync ){
       
 35998       rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
       
 35999     }
       
 36000     IOTRACE(("DBSYNC %p\n", pPager))
       
 36001 
       
 36002     pPager->state = PAGER_SYNCED;
       
 36003   }
       
 36004 
       
 36005 commit_phase_one_exit:
       
 36006   return rc;
       
 36007 }
       
 36008 
       
 36009 
       
 36010 /*
       
 36011 ** When this function is called, the database file has been completely
       
 36012 ** updated to reflect the changes made by the current transaction and
       
 36013 ** synced to disk. The journal file still exists in the file-system 
       
 36014 ** though, and if a failure occurs at this point it will eventually
       
 36015 ** be used as a hot-journal and the current transaction rolled back.
       
 36016 **
       
 36017 ** This function finalizes the journal file, either by deleting, 
       
 36018 ** truncating or partially zeroing it, so that it cannot be used 
       
 36019 ** for hot-journal rollback. Once this is done the transaction is
       
 36020 ** irrevocably committed.
       
 36021 **
       
 36022 ** If an error occurs, an IO error code is returned and the pager
       
 36023 ** moves into the error state. Otherwise, SQLITE_OK is returned.
       
 36024 */
       
 36025 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
       
 36026   int rc = SQLITE_OK;                  /* Return code */
       
 36027 
       
 36028   /* This routine should not be called if a prior error has occurred.
       
 36029   ** But if (due to a coding error elsewhere in the system) it does get
       
 36030   ** called, just return the same error code without doing anything. */
       
 36031   if( NEVER(pPager->errCode) ) return pPager->errCode;
       
 36032 
       
 36033   /* This function should not be called if the pager is not in at least
       
 36034   ** PAGER_RESERVED state. And indeed SQLite never does this. But it is
       
 36035   ** nice to have this defensive test here anyway.
       
 36036   */
       
 36037   if( NEVER(pPager->state<PAGER_RESERVED) ) return SQLITE_ERROR;
       
 36038 
       
 36039   /* An optimization. If the database was not actually modified during
       
 36040   ** this transaction, the pager is running in exclusive-mode and is
       
 36041   ** using persistent journals, then this function is a no-op.
       
 36042   **
       
 36043   ** The start of the journal file currently contains a single journal 
       
 36044   ** header with the nRec field set to 0. If such a journal is used as
       
 36045   ** a hot-journal during hot-journal rollback, 0 changes will be made
       
 36046   ** to the database file. So there is no need to zero the journal 
       
 36047   ** header. Since the pager is in exclusive mode, there is no need
       
 36048   ** to drop any locks either.
       
 36049   */
       
 36050   if( pPager->dbModified==0 && pPager->exclusiveMode 
       
 36051    && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
       
 36052   ){
       
 36053     assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
       
 36054     return SQLITE_OK;
       
 36055   }
       
 36056 
       
 36057   PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
       
 36058   assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dbModified );
       
 36059   rc = pager_end_transaction(pPager, pPager->setMaster);
       
 36060   return pager_error(pPager, rc);
       
 36061 }
       
 36062 
       
 36063 /*
       
 36064 ** Rollback all changes. The database falls back to PAGER_SHARED mode.
       
 36065 **
       
 36066 ** This function performs two tasks:
       
 36067 **
       
 36068 **   1) It rolls back the journal file, restoring all database file and 
       
 36069 **      in-memory cache pages to the state they were in when the transaction
       
 36070 **      was opened, and
       
 36071 **   2) It finalizes the journal file, so that it is not used for hot
       
 36072 **      rollback at any point in the future.
       
 36073 **
       
 36074 ** subject to the following qualifications:
       
 36075 **
       
 36076 ** * If the journal file is not yet open when this function is called,
       
 36077 **   then only (2) is performed. In this case there is no journal file
       
 36078 **   to roll back.
       
 36079 **
       
 36080 ** * If in an error state other than SQLITE_FULL, then task (1) is 
       
 36081 **   performed. If successful, task (2). Regardless of the outcome
       
 36082 **   of either, the error state error code is returned to the caller
       
 36083 **   (i.e. either SQLITE_IOERR or SQLITE_CORRUPT).
       
 36084 **
       
 36085 ** * If the pager is in PAGER_RESERVED state, then attempt (1). Whether
       
 36086 **   or not (1) is succussful, also attempt (2). If successful, return
       
 36087 **   SQLITE_OK. Otherwise, enter the error state and return the first 
       
 36088 **   error code encountered. 
       
 36089 **
       
 36090 **   In this case there is no chance that the database was written to. 
       
 36091 **   So is safe to finalize the journal file even if the playback 
       
 36092 **   (operation 1) failed. However the pager must enter the error state
       
 36093 **   as the contents of the in-memory cache are now suspect.
       
 36094 **
       
 36095 ** * Finally, if in PAGER_EXCLUSIVE state, then attempt (1). Only
       
 36096 **   attempt (2) if (1) is successful. Return SQLITE_OK if successful,
       
 36097 **   otherwise enter the error state and return the error code from the 
       
 36098 **   failing operation.
       
 36099 **
       
 36100 **   In this case the database file may have been written to. So if the
       
 36101 **   playback operation did not succeed it would not be safe to finalize
       
 36102 **   the journal file. It needs to be left in the file-system so that
       
 36103 **   some other process can use it to restore the database state (by
       
 36104 **   hot-journal rollback).
       
 36105 */
       
 36106 SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
       
 36107   int rc = SQLITE_OK;                  /* Return code */
       
 36108   PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
       
 36109   if( !pPager->dbModified || !isOpen(pPager->jfd) ){
       
 36110     rc = pager_end_transaction(pPager, pPager->setMaster);
       
 36111   }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
       
 36112     if( pPager->state>=PAGER_EXCLUSIVE ){
       
 36113       pager_playback(pPager, 0);
       
 36114     }
       
 36115     rc = pPager->errCode;
       
 36116   }else{
       
 36117     if( pPager->state==PAGER_RESERVED ){
       
 36118       int rc2;
       
 36119       rc = pager_playback(pPager, 0);
       
 36120       rc2 = pager_end_transaction(pPager, pPager->setMaster);
       
 36121       if( rc==SQLITE_OK ){
       
 36122         rc = rc2;
       
 36123       }
       
 36124     }else{
       
 36125       rc = pager_playback(pPager, 0);
       
 36126     }
       
 36127 
       
 36128     if( !MEMDB ){
       
 36129       pPager->dbSizeValid = 0;
       
 36130     }
       
 36131 
       
 36132     /* If an error occurs during a ROLLBACK, we can no longer trust the pager
       
 36133     ** cache. So call pager_error() on the way out to make any error 
       
 36134     ** persistent.
       
 36135     */
       
 36136     rc = pager_error(pPager, rc);
       
 36137   }
       
 36138   return rc;
       
 36139 }
       
 36140 
       
 36141 /*
       
 36142 ** Return TRUE if the database file is opened read-only.  Return FALSE
       
 36143 ** if the database is (in theory) writable.
       
 36144 */
       
 36145 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
       
 36146   return pPager->readOnly;
       
 36147 }
       
 36148 
       
 36149 /*
       
 36150 ** Return the number of references to the pager.
       
 36151 */
       
 36152 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
       
 36153   return sqlite3PcacheRefCount(pPager->pPCache);
       
 36154 }
       
 36155 
       
 36156 /*
       
 36157 ** Return the number of references to the specified page.
       
 36158 */
       
 36159 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
       
 36160   return sqlite3PcachePageRefcount(pPage);
       
 36161 }
       
 36162 
       
 36163 #ifdef SQLITE_TEST
       
 36164 /*
       
 36165 ** This routine is used for testing and analysis only.
       
 36166 */
       
 36167 SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
       
 36168   static int a[11];
       
 36169   a[0] = sqlite3PcacheRefCount(pPager->pPCache);
       
 36170   a[1] = sqlite3PcachePagecount(pPager->pPCache);
       
 36171   a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
       
 36172   a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1;
       
 36173   a[4] = pPager->state;
       
 36174   a[5] = pPager->errCode;
       
 36175   a[6] = pPager->nHit;
       
 36176   a[7] = pPager->nMiss;
       
 36177   a[8] = 0;  /* Used to be pPager->nOvfl */
       
 36178   a[9] = pPager->nRead;
       
 36179   a[10] = pPager->nWrite;
       
 36180   return a;
       
 36181 }
       
 36182 #endif
       
 36183 
       
 36184 /*
       
 36185 ** Return true if this is an in-memory pager.
       
 36186 */
       
 36187 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
       
 36188   return MEMDB;
       
 36189 }
       
 36190 
       
 36191 /*
       
 36192 ** Check that there are at least nSavepoint savepoints open. If there are
       
 36193 ** currently less than nSavepoints open, then open one or more savepoints
       
 36194 ** to make up the difference. If the number of savepoints is already
       
 36195 ** equal to nSavepoint, then this function is a no-op.
       
 36196 **
       
 36197 ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error 
       
 36198 ** occurs while opening the sub-journal file, then an IO error code is
       
 36199 ** returned. Otherwise, SQLITE_OK.
       
 36200 */
       
 36201 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
       
 36202   int rc = SQLITE_OK;                       /* Return code */
       
 36203   int nCurrent = pPager->nSavepoint;        /* Current number of savepoints */
       
 36204 
       
 36205   if( nSavepoint>nCurrent && pPager->useJournal ){
       
 36206     int ii;                                 /* Iterator variable */
       
 36207     PagerSavepoint *aNew;                   /* New Pager.aSavepoint array */
       
 36208 
       
 36209     /* Either there is no active journal or the sub-journal is open or 
       
 36210     ** the journal is always stored in memory */
       
 36211     assert( pPager->nSavepoint==0 || isOpen(pPager->sjfd) ||
       
 36212             pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
       
 36213 
       
 36214     /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
       
 36215     ** if the allocation fails. Otherwise, zero the new portion in case a 
       
 36216     ** malloc failure occurs while populating it in the for(...) loop below.
       
 36217     */
       
 36218     aNew = (PagerSavepoint *)sqlite3Realloc(
       
 36219         pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
       
 36220     );
       
 36221     if( !aNew ){
       
 36222       return SQLITE_NOMEM;
       
 36223     }
       
 36224     memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
       
 36225     pPager->aSavepoint = aNew;
       
 36226     pPager->nSavepoint = nSavepoint;
       
 36227 
       
 36228     /* Populate the PagerSavepoint structures just allocated. */
       
 36229     for(ii=nCurrent; ii<nSavepoint; ii++){
       
 36230       assert( pPager->dbSizeValid );
       
 36231       aNew[ii].nOrig = pPager->dbSize;
       
 36232       if( isOpen(pPager->jfd) && ALWAYS(pPager->journalOff>0) ){
       
 36233         aNew[ii].iOffset = pPager->journalOff;
       
 36234       }else{
       
 36235         aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
       
 36236       }
       
 36237       aNew[ii].iSubRec = pPager->nSubRec;
       
 36238       aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
       
 36239       if( !aNew[ii].pInSavepoint ){
       
 36240         return SQLITE_NOMEM;
       
 36241       }
       
 36242     }
       
 36243 
       
 36244     /* Open the sub-journal, if it is not already opened. */
       
 36245     rc = openSubJournal(pPager);
       
 36246     assertTruncateConstraint(pPager);
       
 36247   }
       
 36248 
       
 36249   return rc;
       
 36250 }
       
 36251 
       
 36252 /*
       
 36253 ** This function is called to rollback or release (commit) a savepoint.
       
 36254 ** The savepoint to release or rollback need not be the most recently 
       
 36255 ** created savepoint.
       
 36256 **
       
 36257 ** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
       
 36258 ** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
       
 36259 ** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
       
 36260 ** that have occurred since the specified savepoint was created.
       
 36261 **
       
 36262 ** The savepoint to rollback or release is identified by parameter 
       
 36263 ** iSavepoint. A value of 0 means to operate on the outermost savepoint
       
 36264 ** (the first created). A value of (Pager.nSavepoint-1) means operate
       
 36265 ** on the most recently created savepoint. If iSavepoint is greater than
       
 36266 ** (Pager.nSavepoint-1), then this function is a no-op.
       
 36267 **
       
 36268 ** If a negative value is passed to this function, then the current
       
 36269 ** transaction is rolled back. This is different to calling 
       
 36270 ** sqlite3PagerRollback() because this function does not terminate
       
 36271 ** the transaction or unlock the database, it just restores the 
       
 36272 ** contents of the database to its original state. 
       
 36273 **
       
 36274 ** In any case, all savepoints with an index greater than iSavepoint 
       
 36275 ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
       
 36276 ** then savepoint iSavepoint is also destroyed.
       
 36277 **
       
 36278 ** This function may return SQLITE_NOMEM if a memory allocation fails,
       
 36279 ** or an IO error code if an IO error occurs while rolling back a 
       
 36280 ** savepoint. If no errors occur, SQLITE_OK is returned.
       
 36281 */ 
       
 36282 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
       
 36283   int rc = SQLITE_OK;
       
 36284 
       
 36285   assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
       
 36286   assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
       
 36287 
       
 36288   if( iSavepoint<pPager->nSavepoint ){
       
 36289     int ii;            /* Iterator variable */
       
 36290     int nNew;          /* Number of remaining savepoints after this op. */
       
 36291 
       
 36292     /* Figure out how many savepoints will still be active after this
       
 36293     ** operation. Store this value in nNew. Then free resources associated 
       
 36294     ** with any savepoints that are destroyed by this operation.
       
 36295     */
       
 36296     nNew = iSavepoint + (op==SAVEPOINT_ROLLBACK);
       
 36297     for(ii=nNew; ii<pPager->nSavepoint; ii++){
       
 36298       sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
       
 36299     }
       
 36300     pPager->nSavepoint = nNew;
       
 36301 
       
 36302     /* If this is a rollback operation, playback the specified savepoint.
       
 36303     ** If this is a temp-file, it is possible that the journal file has
       
 36304     ** not yet been opened. In this case there have been no changes to
       
 36305     ** the database file, so the playback operation can be skipped.
       
 36306     */
       
 36307     if( op==SAVEPOINT_ROLLBACK && isOpen(pPager->jfd) ){
       
 36308       PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
       
 36309       rc = pagerPlaybackSavepoint(pPager, pSavepoint);
       
 36310       assert(rc!=SQLITE_DONE);
       
 36311     }
       
 36312   
       
 36313     /* If this is a release of the outermost savepoint, truncate 
       
 36314     ** the sub-journal to zero bytes in size. */
       
 36315     if( nNew==0 && op==SAVEPOINT_RELEASE && isOpen(pPager->sjfd) ){
       
 36316       assert( rc==SQLITE_OK );
       
 36317       rc = sqlite3OsTruncate(pPager->sjfd, 0);
       
 36318       pPager->nSubRec = 0;
       
 36319     }
       
 36320   }
       
 36321   return rc;
       
 36322 }
       
 36323 
       
 36324 /*
       
 36325 ** Return the full pathname of the database file.
       
 36326 */
       
 36327 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager){
       
 36328   return pPager->zFilename;
       
 36329 }
       
 36330 
       
 36331 /*
       
 36332 ** Return the VFS structure for the pager.
       
 36333 */
       
 36334 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
       
 36335   return pPager->pVfs;
       
 36336 }
       
 36337 
       
 36338 /*
       
 36339 ** Return the file handle for the database file associated
       
 36340 ** with the pager.  This might return NULL if the file has
       
 36341 ** not yet been opened.
       
 36342 */
       
 36343 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
       
 36344   return pPager->fd;
       
 36345 }
       
 36346 
       
 36347 /*
       
 36348 ** Return the full pathname of the journal file.
       
 36349 */
       
 36350 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
       
 36351   return pPager->zJournal;
       
 36352 }
       
 36353 
       
 36354 /*
       
 36355 ** Return true if fsync() calls are disabled for this pager.  Return FALSE
       
 36356 ** if fsync()s are executed normally.
       
 36357 */
       
 36358 SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
       
 36359   return pPager->noSync;
       
 36360 }
       
 36361 
       
 36362 #ifdef SQLITE_HAS_CODEC
       
 36363 /*
       
 36364 ** Set or retrieve the codec for this pager
       
 36365 */
       
 36366 static void sqlite3PagerSetCodec(
       
 36367   Pager *pPager,
       
 36368   void *(*xCodec)(void*,void*,Pgno,int),
       
 36369   void (*xCodecSizeChng)(void*,int,int),
       
 36370   void (*xCodecFree)(void*),
       
 36371   void *pCodec
       
 36372 ){
       
 36373   if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
       
 36374   pPager->xCodec = xCodec;
       
 36375   pPager->xCodecSizeChng = xCodecSizeChng;
       
 36376   pPager->xCodecFree = xCodecFree;
       
 36377   pPager->pCodec = pCodec;
       
 36378   pagerReportSize(pPager);
       
 36379 }
       
 36380 static void *sqlite3PagerGetCodec(Pager *pPager){
       
 36381   return pPager->pCodec;
       
 36382 }
       
 36383 #endif
       
 36384 
       
 36385 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 36386 /*
       
 36387 ** Move the page pPg to location pgno in the file.
       
 36388 **
       
 36389 ** There must be no references to the page previously located at
       
 36390 ** pgno (which we call pPgOld) though that page is allowed to be
       
 36391 ** in cache.  If the page previously located at pgno is not already
       
 36392 ** in the rollback journal, it is not put there by by this routine.
       
 36393 **
       
 36394 ** References to the page pPg remain valid. Updating any
       
 36395 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
       
 36396 ** allocated along with the page) is the responsibility of the caller.
       
 36397 **
       
 36398 ** A transaction must be active when this routine is called. It used to be
       
 36399 ** required that a statement transaction was not active, but this restriction
       
 36400 ** has been removed (CREATE INDEX needs to move a page when a statement
       
 36401 ** transaction is active).
       
 36402 **
       
 36403 ** If the fourth argument, isCommit, is non-zero, then this page is being
       
 36404 ** moved as part of a database reorganization just before the transaction 
       
 36405 ** is being committed. In this case, it is guaranteed that the database page 
       
 36406 ** pPg refers to will not be written to again within this transaction.
       
 36407 **
       
 36408 ** This function may return SQLITE_NOMEM or an IO error code if an error
       
 36409 ** occurs. Otherwise, it returns SQLITE_OK.
       
 36410 */
       
 36411 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
       
 36412   PgHdr *pPgOld;               /* The page being overwritten. */
       
 36413   Pgno needSyncPgno = 0;       /* Old value of pPg->pgno, if sync is required */
       
 36414   int rc;                      /* Return code */
       
 36415   Pgno origPgno;               /* The original page number */
       
 36416 
       
 36417   assert( pPg->nRef>0 );
       
 36418 
       
 36419   /* If the page being moved is dirty and has not been saved by the latest
       
 36420   ** savepoint, then save the current contents of the page into the 
       
 36421   ** sub-journal now. This is required to handle the following scenario:
       
 36422   **
       
 36423   **   BEGIN;
       
 36424   **     <journal page X, then modify it in memory>
       
 36425   **     SAVEPOINT one;
       
 36426   **       <Move page X to location Y>
       
 36427   **     ROLLBACK TO one;
       
 36428   **
       
 36429   ** If page X were not written to the sub-journal here, it would not
       
 36430   ** be possible to restore its contents when the "ROLLBACK TO one"
       
 36431   ** statement were is processed.
       
 36432   **
       
 36433   ** subjournalPage() may need to allocate space to store pPg->pgno into
       
 36434   ** one or more savepoint bitvecs. This is the reason this function
       
 36435   ** may return SQLITE_NOMEM.
       
 36436   */
       
 36437   if( pPg->flags&PGHDR_DIRTY 
       
 36438    && subjRequiresPage(pPg)
       
 36439    && SQLITE_OK!=(rc = subjournalPage(pPg))
       
 36440   ){
       
 36441     return rc;
       
 36442   }
       
 36443 
       
 36444   PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n", 
       
 36445       PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
       
 36446   IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
       
 36447 
       
 36448   /* If the journal needs to be sync()ed before page pPg->pgno can
       
 36449   ** be written to, store pPg->pgno in local variable needSyncPgno.
       
 36450   **
       
 36451   ** If the isCommit flag is set, there is no need to remember that
       
 36452   ** the journal needs to be sync()ed before database page pPg->pgno 
       
 36453   ** can be written to. The caller has already promised not to write to it.
       
 36454   */
       
 36455   if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
       
 36456     needSyncPgno = pPg->pgno;
       
 36457     assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
       
 36458     assert( pPg->flags&PGHDR_DIRTY );
       
 36459     assert( pPager->needSync );
       
 36460   }
       
 36461 
       
 36462   /* If the cache contains a page with page-number pgno, remove it
       
 36463   ** from its hash chain. Also, if the PgHdr.needSync was set for 
       
 36464   ** page pgno before the 'move' operation, it needs to be retained 
       
 36465   ** for the page moved there.
       
 36466   */
       
 36467   pPg->flags &= ~PGHDR_NEED_SYNC;
       
 36468   pPgOld = pager_lookup(pPager, pgno);
       
 36469   assert( !pPgOld || pPgOld->nRef==1 );
       
 36470   if( pPgOld ){
       
 36471     pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
       
 36472     sqlite3PcacheDrop(pPgOld);
       
 36473   }
       
 36474 
       
 36475   origPgno = pPg->pgno;
       
 36476   sqlite3PcacheMove(pPg, pgno);
       
 36477   sqlite3PcacheMakeDirty(pPg);
       
 36478   pPager->dbModified = 1;
       
 36479 
       
 36480   if( needSyncPgno ){
       
 36481     /* If needSyncPgno is non-zero, then the journal file needs to be 
       
 36482     ** sync()ed before any data is written to database file page needSyncPgno.
       
 36483     ** Currently, no such page exists in the page-cache and the 
       
 36484     ** "is journaled" bitvec flag has been set. This needs to be remedied by
       
 36485     ** loading the page into the pager-cache and setting the PgHdr.needSync 
       
 36486     ** flag.
       
 36487     **
       
 36488     ** If the attempt to load the page into the page-cache fails, (due
       
 36489     ** to a malloc() or IO failure), clear the bit in the pInJournal[]
       
 36490     ** array. Otherwise, if the page is loaded and written again in
       
 36491     ** this transaction, it may be written to the database file before
       
 36492     ** it is synced into the journal file. This way, it may end up in
       
 36493     ** the journal file twice, but that is not a problem.
       
 36494     **
       
 36495     ** The sqlite3PagerGet() call may cause the journal to sync. So make
       
 36496     ** sure the Pager.needSync flag is set too.
       
 36497     */
       
 36498     PgHdr *pPgHdr;
       
 36499     assert( pPager->needSync );
       
 36500     rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
       
 36501     if( rc!=SQLITE_OK ){
       
 36502       if( needSyncPgno<=pPager->dbOrigSize ){
       
 36503         assert( pPager->pTmpSpace!=0 );
       
 36504         sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
       
 36505       }
       
 36506       return rc;
       
 36507     }
       
 36508     pPager->needSync = 1;
       
 36509     assert( pPager->noSync==0 && !MEMDB );
       
 36510     pPgHdr->flags |= PGHDR_NEED_SYNC;
       
 36511     sqlite3PcacheMakeDirty(pPgHdr);
       
 36512     sqlite3PagerUnref(pPgHdr);
       
 36513   }
       
 36514 
       
 36515   /*
       
 36516   ** For an in-memory database, make sure the original page continues
       
 36517   ** to exist, in case the transaction needs to roll back.  We allocate
       
 36518   ** the page now, instead of at rollback, because we can better deal
       
 36519   ** with an out-of-memory error now.  Ticket #3761.
       
 36520   */
       
 36521   if( MEMDB ){
       
 36522     DbPage *pNew;
       
 36523     rc = sqlite3PagerAcquire(pPager, origPgno, &pNew, 1);
       
 36524     if( rc!=SQLITE_OK ){
       
 36525       sqlite3PcacheMove(pPg, origPgno);
       
 36526       return rc;
       
 36527     }
       
 36528     sqlite3PagerUnref(pNew);
       
 36529   }
       
 36530 
       
 36531   return SQLITE_OK;
       
 36532 }
       
 36533 #endif
       
 36534 
       
 36535 /*
       
 36536 ** Return a pointer to the data for the specified page.
       
 36537 */
       
 36538 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
       
 36539   assert( pPg->nRef>0 || pPg->pPager->memDb );
       
 36540   return pPg->pData;
       
 36541 }
       
 36542 
       
 36543 /*
       
 36544 ** Return a pointer to the Pager.nExtra bytes of "extra" space 
       
 36545 ** allocated along with the specified page.
       
 36546 */
       
 36547 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
       
 36548   return pPg->pExtra;
       
 36549 }
       
 36550 
       
 36551 /*
       
 36552 ** Get/set the locking-mode for this pager. Parameter eMode must be one
       
 36553 ** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or 
       
 36554 ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
       
 36555 ** the locking-mode is set to the value specified.
       
 36556 **
       
 36557 ** The returned value is either PAGER_LOCKINGMODE_NORMAL or
       
 36558 ** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
       
 36559 ** locking-mode.
       
 36560 */
       
 36561 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
       
 36562   assert( eMode==PAGER_LOCKINGMODE_QUERY
       
 36563             || eMode==PAGER_LOCKINGMODE_NORMAL
       
 36564             || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
       
 36565   assert( PAGER_LOCKINGMODE_QUERY<0 );
       
 36566   assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
       
 36567   if( eMode>=0 && !pPager->tempFile ){
       
 36568     pPager->exclusiveMode = (u8)eMode;
       
 36569   }
       
 36570   return (int)pPager->exclusiveMode;
       
 36571 }
       
 36572 
       
 36573 /*
       
 36574 ** Get/set the journal-mode for this pager. Parameter eMode must be one of:
       
 36575 **
       
 36576 **    PAGER_JOURNALMODE_QUERY
       
 36577 **    PAGER_JOURNALMODE_DELETE
       
 36578 **    PAGER_JOURNALMODE_TRUNCATE
       
 36579 **    PAGER_JOURNALMODE_PERSIST
       
 36580 **    PAGER_JOURNALMODE_OFF
       
 36581 **    PAGER_JOURNALMODE_MEMORY
       
 36582 **
       
 36583 ** If the parameter is not _QUERY, then the journal_mode is set to the
       
 36584 ** value specified if the change is allowed.  The change is disallowed
       
 36585 ** for the following reasons:
       
 36586 **
       
 36587 **   *  An in-memory database can only have its journal_mode set to _OFF
       
 36588 **      or _MEMORY.
       
 36589 **
       
 36590 **   *  The journal mode may not be changed while a transaction is active.
       
 36591 **
       
 36592 ** The returned indicate the current (possibly updated) journal-mode.
       
 36593 */
       
 36594 SQLITE_PRIVATE int sqlite3PagerJournalMode(Pager *pPager, int eMode){
       
 36595   assert( eMode==PAGER_JOURNALMODE_QUERY
       
 36596             || eMode==PAGER_JOURNALMODE_DELETE
       
 36597             || eMode==PAGER_JOURNALMODE_TRUNCATE
       
 36598             || eMode==PAGER_JOURNALMODE_PERSIST
       
 36599             || eMode==PAGER_JOURNALMODE_OFF 
       
 36600             || eMode==PAGER_JOURNALMODE_MEMORY );
       
 36601   assert( PAGER_JOURNALMODE_QUERY<0 );
       
 36602   if( eMode>=0
       
 36603    && (!MEMDB || eMode==PAGER_JOURNALMODE_MEMORY 
       
 36604               || eMode==PAGER_JOURNALMODE_OFF)
       
 36605    && !pPager->dbModified
       
 36606    && (!isOpen(pPager->jfd) || 0==pPager->journalOff)
       
 36607   ){
       
 36608     if( isOpen(pPager->jfd) ){
       
 36609       sqlite3OsClose(pPager->jfd);
       
 36610     }
       
 36611     pPager->journalMode = (u8)eMode;
       
 36612   }
       
 36613   return (int)pPager->journalMode;
       
 36614 }
       
 36615 
       
 36616 /*
       
 36617 ** Get/set the size-limit used for persistent journal files.
       
 36618 **
       
 36619 ** Setting the size limit to -1 means no limit is enforced.
       
 36620 ** An attempt to set a limit smaller than -1 is a no-op.
       
 36621 */
       
 36622 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
       
 36623   if( iLimit>=-1 ){
       
 36624     pPager->journalSizeLimit = iLimit;
       
 36625   }
       
 36626   return pPager->journalSizeLimit;
       
 36627 }
       
 36628 
       
 36629 /*
       
 36630 ** Return a pointer to the pPager->pBackup variable. The backup module
       
 36631 ** in backup.c maintains the content of this variable. This module
       
 36632 ** uses it opaquely as an argument to sqlite3BackupRestart() and
       
 36633 ** sqlite3BackupUpdate() only.
       
 36634 */
       
 36635 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
       
 36636   return &pPager->pBackup;
       
 36637 }
       
 36638 
       
 36639 #endif /* SQLITE_OMIT_DISKIO */
       
 36640 
       
 36641 /************** End of pager.c ***********************************************/
       
 36642 /************** Begin file btmutex.c *****************************************/
       
 36643 /*
       
 36644 ** 2007 August 27
       
 36645 **
       
 36646 ** The author disclaims copyright to this source code.  In place of
       
 36647 ** a legal notice, here is a blessing:
       
 36648 **
       
 36649 **    May you do good and not evil.
       
 36650 **    May you find forgiveness for yourself and forgive others.
       
 36651 **    May you share freely, never taking more than you give.
       
 36652 **
       
 36653 *************************************************************************
       
 36654 **
       
 36655 ** $Id: btmutex.c,v 1.17 2009/07/20 12:33:33 drh Exp $
       
 36656 **
       
 36657 ** This file contains code used to implement mutexes on Btree objects.
       
 36658 ** This code really belongs in btree.c.  But btree.c is getting too
       
 36659 ** big and we want to break it down some.  This packaged seemed like
       
 36660 ** a good breakout.
       
 36661 */
       
 36662 /************** Include btreeInt.h in the middle of btmutex.c ****************/
       
 36663 /************** Begin file btreeInt.h ****************************************/
       
 36664 /*
       
 36665 ** 2004 April 6
       
 36666 **
       
 36667 ** The author disclaims copyright to this source code.  In place of
       
 36668 ** a legal notice, here is a blessing:
       
 36669 **
       
 36670 **    May you do good and not evil.
       
 36671 **    May you find forgiveness for yourself and forgive others.
       
 36672 **    May you share freely, never taking more than you give.
       
 36673 **
       
 36674 *************************************************************************
       
 36675 ** $Id: btreeInt.h,v 1.52 2009/07/15 17:25:46 drh Exp $
       
 36676 **
       
 36677 ** This file implements a external (disk-based) database using BTrees.
       
 36678 ** For a detailed discussion of BTrees, refer to
       
 36679 **
       
 36680 **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
       
 36681 **     "Sorting And Searching", pages 473-480. Addison-Wesley
       
 36682 **     Publishing Company, Reading, Massachusetts.
       
 36683 **
       
 36684 ** The basic idea is that each page of the file contains N database
       
 36685 ** entries and N+1 pointers to subpages.
       
 36686 **
       
 36687 **   ----------------------------------------------------------------
       
 36688 **   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
       
 36689 **   ----------------------------------------------------------------
       
 36690 **
       
 36691 ** All of the keys on the page that Ptr(0) points to have values less
       
 36692 ** than Key(0).  All of the keys on page Ptr(1) and its subpages have
       
 36693 ** values greater than Key(0) and less than Key(1).  All of the keys
       
 36694 ** on Ptr(N) and its subpages have values greater than Key(N-1).  And
       
 36695 ** so forth.
       
 36696 **
       
 36697 ** Finding a particular key requires reading O(log(M)) pages from the 
       
 36698 ** disk where M is the number of entries in the tree.
       
 36699 **
       
 36700 ** In this implementation, a single file can hold one or more separate 
       
 36701 ** BTrees.  Each BTree is identified by the index of its root page.  The
       
 36702 ** key and data for any entry are combined to form the "payload".  A
       
 36703 ** fixed amount of payload can be carried directly on the database
       
 36704 ** page.  If the payload is larger than the preset amount then surplus
       
 36705 ** bytes are stored on overflow pages.  The payload for an entry
       
 36706 ** and the preceding pointer are combined to form a "Cell".  Each 
       
 36707 ** page has a small header which contains the Ptr(N) pointer and other
       
 36708 ** information such as the size of key and data.
       
 36709 **
       
 36710 ** FORMAT DETAILS
       
 36711 **
       
 36712 ** The file is divided into pages.  The first page is called page 1,
       
 36713 ** the second is page 2, and so forth.  A page number of zero indicates
       
 36714 ** "no such page".  The page size can be anything between 512 and 65536.
       
 36715 ** Each page can be either a btree page, a freelist page or an overflow
       
 36716 ** page.
       
 36717 **
       
 36718 ** The first page is always a btree page.  The first 100 bytes of the first
       
 36719 ** page contain a special header (the "file header") that describes the file.
       
 36720 ** The format of the file header is as follows:
       
 36721 **
       
 36722 **   OFFSET   SIZE    DESCRIPTION
       
 36723 **      0      16     Header string: "SQLite format 3\000"
       
 36724 **     16       2     Page size in bytes.  
       
 36725 **     18       1     File format write version
       
 36726 **     19       1     File format read version
       
 36727 **     20       1     Bytes of unused space at the end of each page
       
 36728 **     21       1     Max embedded payload fraction
       
 36729 **     22       1     Min embedded payload fraction
       
 36730 **     23       1     Min leaf payload fraction
       
 36731 **     24       4     File change counter
       
 36732 **     28       4     Reserved for future use
       
 36733 **     32       4     First freelist page
       
 36734 **     36       4     Number of freelist pages in the file
       
 36735 **     40      60     15 4-byte meta values passed to higher layers
       
 36736 **
       
 36737 **     40       4     Schema cookie
       
 36738 **     44       4     File format of schema layer
       
 36739 **     48       4     Size of page cache
       
 36740 **     52       4     Largest root-page (auto/incr_vacuum)
       
 36741 **     56       4     1=UTF-8 2=UTF16le 3=UTF16be
       
 36742 **     60       4     User version
       
 36743 **     64       4     Incremental vacuum mode
       
 36744 **     68       4     unused
       
 36745 **     72       4     unused
       
 36746 **     76       4     unused
       
 36747 **
       
 36748 ** All of the integer values are big-endian (most significant byte first).
       
 36749 **
       
 36750 ** The file change counter is incremented when the database is changed
       
 36751 ** This counter allows other processes to know when the file has changed
       
 36752 ** and thus when they need to flush their cache.
       
 36753 **
       
 36754 ** The max embedded payload fraction is the amount of the total usable
       
 36755 ** space in a page that can be consumed by a single cell for standard
       
 36756 ** B-tree (non-LEAFDATA) tables.  A value of 255 means 100%.  The default
       
 36757 ** is to limit the maximum cell size so that at least 4 cells will fit
       
 36758 ** on one page.  Thus the default max embedded payload fraction is 64.
       
 36759 **
       
 36760 ** If the payload for a cell is larger than the max payload, then extra
       
 36761 ** payload is spilled to overflow pages.  Once an overflow page is allocated,
       
 36762 ** as many bytes as possible are moved into the overflow pages without letting
       
 36763 ** the cell size drop below the min embedded payload fraction.
       
 36764 **
       
 36765 ** The min leaf payload fraction is like the min embedded payload fraction
       
 36766 ** except that it applies to leaf nodes in a LEAFDATA tree.  The maximum
       
 36767 ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
       
 36768 ** not specified in the header.
       
 36769 **
       
 36770 ** Each btree pages is divided into three sections:  The header, the
       
 36771 ** cell pointer array, and the cell content area.  Page 1 also has a 100-byte
       
 36772 ** file header that occurs before the page header.
       
 36773 **
       
 36774 **      |----------------|
       
 36775 **      | file header    |   100 bytes.  Page 1 only.
       
 36776 **      |----------------|
       
 36777 **      | page header    |   8 bytes for leaves.  12 bytes for interior nodes
       
 36778 **      |----------------|
       
 36779 **      | cell pointer   |   |  2 bytes per cell.  Sorted order.
       
 36780 **      | array          |   |  Grows downward
       
 36781 **      |                |   v
       
 36782 **      |----------------|
       
 36783 **      | unallocated    |
       
 36784 **      | space          |
       
 36785 **      |----------------|   ^  Grows upwards
       
 36786 **      | cell content   |   |  Arbitrary order interspersed with freeblocks.
       
 36787 **      | area           |   |  and free space fragments.
       
 36788 **      |----------------|
       
 36789 **
       
 36790 ** The page headers looks like this:
       
 36791 **
       
 36792 **   OFFSET   SIZE     DESCRIPTION
       
 36793 **      0       1      Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
       
 36794 **      1       2      byte offset to the first freeblock
       
 36795 **      3       2      number of cells on this page
       
 36796 **      5       2      first byte of the cell content area
       
 36797 **      7       1      number of fragmented free bytes
       
 36798 **      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
       
 36799 **
       
 36800 ** The flags define the format of this btree page.  The leaf flag means that
       
 36801 ** this page has no children.  The zerodata flag means that this page carries
       
 36802 ** only keys and no data.  The intkey flag means that the key is a integer
       
 36803 ** which is stored in the key size entry of the cell header rather than in
       
 36804 ** the payload area.
       
 36805 **
       
 36806 ** The cell pointer array begins on the first byte after the page header.
       
 36807 ** The cell pointer array contains zero or more 2-byte numbers which are
       
 36808 ** offsets from the beginning of the page to the cell content in the cell
       
 36809 ** content area.  The cell pointers occur in sorted order.  The system strives
       
 36810 ** to keep free space after the last cell pointer so that new cells can
       
 36811 ** be easily added without having to defragment the page.
       
 36812 **
       
 36813 ** Cell content is stored at the very end of the page and grows toward the
       
 36814 ** beginning of the page.
       
 36815 **
       
 36816 ** Unused space within the cell content area is collected into a linked list of
       
 36817 ** freeblocks.  Each freeblock is at least 4 bytes in size.  The byte offset
       
 36818 ** to the first freeblock is given in the header.  Freeblocks occur in
       
 36819 ** increasing order.  Because a freeblock must be at least 4 bytes in size,
       
 36820 ** any group of 3 or fewer unused bytes in the cell content area cannot
       
 36821 ** exist on the freeblock chain.  A group of 3 or fewer free bytes is called
       
 36822 ** a fragment.  The total number of bytes in all fragments is recorded.
       
 36823 ** in the page header at offset 7.
       
 36824 **
       
 36825 **    SIZE    DESCRIPTION
       
 36826 **      2     Byte offset of the next freeblock
       
 36827 **      2     Bytes in this freeblock
       
 36828 **
       
 36829 ** Cells are of variable length.  Cells are stored in the cell content area at
       
 36830 ** the end of the page.  Pointers to the cells are in the cell pointer array
       
 36831 ** that immediately follows the page header.  Cells is not necessarily
       
 36832 ** contiguous or in order, but cell pointers are contiguous and in order.
       
 36833 **
       
 36834 ** Cell content makes use of variable length integers.  A variable
       
 36835 ** length integer is 1 to 9 bytes where the lower 7 bits of each 
       
 36836 ** byte are used.  The integer consists of all bytes that have bit 8 set and
       
 36837 ** the first byte with bit 8 clear.  The most significant byte of the integer
       
 36838 ** appears first.  A variable-length integer may not be more than 9 bytes long.
       
 36839 ** As a special case, all 8 bytes of the 9th byte are used as data.  This
       
 36840 ** allows a 64-bit integer to be encoded in 9 bytes.
       
 36841 **
       
 36842 **    0x00                      becomes  0x00000000
       
 36843 **    0x7f                      becomes  0x0000007f
       
 36844 **    0x81 0x00                 becomes  0x00000080
       
 36845 **    0x82 0x00                 becomes  0x00000100
       
 36846 **    0x80 0x7f                 becomes  0x0000007f
       
 36847 **    0x8a 0x91 0xd1 0xac 0x78  becomes  0x12345678
       
 36848 **    0x81 0x81 0x81 0x81 0x01  becomes  0x10204081
       
 36849 **
       
 36850 ** Variable length integers are used for rowids and to hold the number of
       
 36851 ** bytes of key and data in a btree cell.
       
 36852 **
       
 36853 ** The content of a cell looks like this:
       
 36854 **
       
 36855 **    SIZE    DESCRIPTION
       
 36856 **      4     Page number of the left child. Omitted if leaf flag is set.
       
 36857 **     var    Number of bytes of data. Omitted if the zerodata flag is set.
       
 36858 **     var    Number of bytes of key. Or the key itself if intkey flag is set.
       
 36859 **      *     Payload
       
 36860 **      4     First page of the overflow chain.  Omitted if no overflow
       
 36861 **
       
 36862 ** Overflow pages form a linked list.  Each page except the last is completely
       
 36863 ** filled with data (pagesize - 4 bytes).  The last page can have as little
       
 36864 ** as 1 byte of data.
       
 36865 **
       
 36866 **    SIZE    DESCRIPTION
       
 36867 **      4     Page number of next overflow page
       
 36868 **      *     Data
       
 36869 **
       
 36870 ** Freelist pages come in two subtypes: trunk pages and leaf pages.  The
       
 36871 ** file header points to the first in a linked list of trunk page.  Each trunk
       
 36872 ** page points to multiple leaf pages.  The content of a leaf page is
       
 36873 ** unspecified.  A trunk page looks like this:
       
 36874 **
       
 36875 **    SIZE    DESCRIPTION
       
 36876 **      4     Page number of next trunk page
       
 36877 **      4     Number of leaf pointers on this page
       
 36878 **      *     zero or more pages numbers of leaves
       
 36879 */
       
 36880 
       
 36881 
       
 36882 /* The following value is the maximum cell size assuming a maximum page
       
 36883 ** size give above.
       
 36884 */
       
 36885 #define MX_CELL_SIZE(pBt)  (pBt->pageSize-8)
       
 36886 
       
 36887 /* The maximum number of cells on a single page of the database.  This
       
 36888 ** assumes a minimum cell size of 6 bytes  (4 bytes for the cell itself
       
 36889 ** plus 2 bytes for the index to the cell in the page header).  Such
       
 36890 ** small cells will be rare, but they are possible.
       
 36891 */
       
 36892 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
       
 36893 
       
 36894 /* Forward declarations */
       
 36895 typedef struct MemPage MemPage;
       
 36896 typedef struct BtLock BtLock;
       
 36897 
       
 36898 /*
       
 36899 ** This is a magic string that appears at the beginning of every
       
 36900 ** SQLite database in order to identify the file as a real database.
       
 36901 **
       
 36902 ** You can change this value at compile-time by specifying a
       
 36903 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
       
 36904 ** header must be exactly 16 bytes including the zero-terminator so
       
 36905 ** the string itself should be 15 characters long.  If you change
       
 36906 ** the header, then your custom library will not be able to read 
       
 36907 ** databases generated by the standard tools and the standard tools
       
 36908 ** will not be able to read databases created by your custom library.
       
 36909 */
       
 36910 #ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
       
 36911 #  define SQLITE_FILE_HEADER "SQLite format 3"
       
 36912 #endif
       
 36913 
       
 36914 /*
       
 36915 ** Page type flags.  An ORed combination of these flags appear as the
       
 36916 ** first byte of on-disk image of every BTree page.
       
 36917 */
       
 36918 #define PTF_INTKEY    0x01
       
 36919 #define PTF_ZERODATA  0x02
       
 36920 #define PTF_LEAFDATA  0x04
       
 36921 #define PTF_LEAF      0x08
       
 36922 
       
 36923 /*
       
 36924 ** As each page of the file is loaded into memory, an instance of the following
       
 36925 ** structure is appended and initialized to zero.  This structure stores
       
 36926 ** information about the page that is decoded from the raw file page.
       
 36927 **
       
 36928 ** The pParent field points back to the parent page.  This allows us to
       
 36929 ** walk up the BTree from any leaf to the root.  Care must be taken to
       
 36930 ** unref() the parent page pointer when this page is no longer referenced.
       
 36931 ** The pageDestructor() routine handles that chore.
       
 36932 **
       
 36933 ** Access to all fields of this structure is controlled by the mutex
       
 36934 ** stored in MemPage.pBt->mutex.
       
 36935 */
       
 36936 struct MemPage {
       
 36937   u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
       
 36938   u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
       
 36939   u8 intKey;           /* True if intkey flag is set */
       
 36940   u8 leaf;             /* True if leaf flag is set */
       
 36941   u8 hasData;          /* True if this page stores data */
       
 36942   u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
       
 36943   u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
       
 36944   u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
       
 36945   u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
       
 36946   u16 cellOffset;      /* Index in aData of first cell pointer */
       
 36947   u16 nFree;           /* Number of free bytes on the page */
       
 36948   u16 nCell;           /* Number of cells on this page, local and ovfl */
       
 36949   u16 maskPage;        /* Mask for page offset */
       
 36950   struct _OvflCell {   /* Cells that will not fit on aData[] */
       
 36951     u8 *pCell;          /* Pointers to the body of the overflow cell */
       
 36952     u16 idx;            /* Insert this cell before idx-th non-overflow cell */
       
 36953   } aOvfl[5];
       
 36954   BtShared *pBt;       /* Pointer to BtShared that this page is part of */
       
 36955   u8 *aData;           /* Pointer to disk image of the page data */
       
 36956   DbPage *pDbPage;     /* Pager page handle */
       
 36957   Pgno pgno;           /* Page number for this page */
       
 36958 };
       
 36959 
       
 36960 /*
       
 36961 ** The in-memory image of a disk page has the auxiliary information appended
       
 36962 ** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
       
 36963 ** that extra information.
       
 36964 */
       
 36965 #define EXTRA_SIZE sizeof(MemPage)
       
 36966 
       
 36967 /*
       
 36968 ** A linked list of the following structures is stored at BtShared.pLock.
       
 36969 ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor 
       
 36970 ** is opened on the table with root page BtShared.iTable. Locks are removed
       
 36971 ** from this list when a transaction is committed or rolled back, or when
       
 36972 ** a btree handle is closed.
       
 36973 */
       
 36974 struct BtLock {
       
 36975   Btree *pBtree;        /* Btree handle holding this lock */
       
 36976   Pgno iTable;          /* Root page of table */
       
 36977   u8 eLock;             /* READ_LOCK or WRITE_LOCK */
       
 36978   BtLock *pNext;        /* Next in BtShared.pLock list */
       
 36979 };
       
 36980 
       
 36981 /* Candidate values for BtLock.eLock */
       
 36982 #define READ_LOCK     1
       
 36983 #define WRITE_LOCK    2
       
 36984 
       
 36985 /* A Btree handle
       
 36986 **
       
 36987 ** A database connection contains a pointer to an instance of
       
 36988 ** this object for every database file that it has open.  This structure
       
 36989 ** is opaque to the database connection.  The database connection cannot
       
 36990 ** see the internals of this structure and only deals with pointers to
       
 36991 ** this structure.
       
 36992 **
       
 36993 ** For some database files, the same underlying database cache might be 
       
 36994 ** shared between multiple connections.  In that case, each contection
       
 36995 ** has it own pointer to this object.  But each instance of this object
       
 36996 ** points to the same BtShared object.  The database cache and the
       
 36997 ** schema associated with the database file are all contained within
       
 36998 ** the BtShared object.
       
 36999 **
       
 37000 ** All fields in this structure are accessed under sqlite3.mutex.
       
 37001 ** The pBt pointer itself may not be changed while there exists cursors 
       
 37002 ** in the referenced BtShared that point back to this Btree since those
       
 37003 ** cursors have to do go through this Btree to find their BtShared and
       
 37004 ** they often do so without holding sqlite3.mutex.
       
 37005 */
       
 37006 struct Btree {
       
 37007   sqlite3 *db;       /* The database connection holding this btree */
       
 37008   BtShared *pBt;     /* Sharable content of this btree */
       
 37009   u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
       
 37010   u8 sharable;       /* True if we can share pBt with another db */
       
 37011   u8 locked;         /* True if db currently has pBt locked */
       
 37012   int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
       
 37013   int nBackup;       /* Number of backup operations reading this btree */
       
 37014   Btree *pNext;      /* List of other sharable Btrees from the same db */
       
 37015   Btree *pPrev;      /* Back pointer of the same list */
       
 37016 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 37017   BtLock lock;       /* Object used to lock page 1 */
       
 37018 #endif
       
 37019 };
       
 37020 
       
 37021 /*
       
 37022 ** Btree.inTrans may take one of the following values.
       
 37023 **
       
 37024 ** If the shared-data extension is enabled, there may be multiple users
       
 37025 ** of the Btree structure. At most one of these may open a write transaction,
       
 37026 ** but any number may have active read transactions.
       
 37027 */
       
 37028 #define TRANS_NONE  0
       
 37029 #define TRANS_READ  1
       
 37030 #define TRANS_WRITE 2
       
 37031 
       
 37032 /*
       
 37033 ** An instance of this object represents a single database file.
       
 37034 ** 
       
 37035 ** A single database file can be in use as the same time by two
       
 37036 ** or more database connections.  When two or more connections are
       
 37037 ** sharing the same database file, each connection has it own
       
 37038 ** private Btree object for the file and each of those Btrees points
       
 37039 ** to this one BtShared object.  BtShared.nRef is the number of
       
 37040 ** connections currently sharing this database file.
       
 37041 **
       
 37042 ** Fields in this structure are accessed under the BtShared.mutex
       
 37043 ** mutex, except for nRef and pNext which are accessed under the
       
 37044 ** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
       
 37045 ** may not be modified once it is initially set as long as nRef>0.
       
 37046 ** The pSchema field may be set once under BtShared.mutex and
       
 37047 ** thereafter is unchanged as long as nRef>0.
       
 37048 **
       
 37049 ** isPending:
       
 37050 **
       
 37051 **   If a BtShared client fails to obtain a write-lock on a database
       
 37052 **   table (because there exists one or more read-locks on the table),
       
 37053 **   the shared-cache enters 'pending-lock' state and isPending is
       
 37054 **   set to true.
       
 37055 **
       
 37056 **   The shared-cache leaves the 'pending lock' state when either of
       
 37057 **   the following occur:
       
 37058 **
       
 37059 **     1) The current writer (BtShared.pWriter) concludes its transaction, OR
       
 37060 **     2) The number of locks held by other connections drops to zero.
       
 37061 **
       
 37062 **   while in the 'pending-lock' state, no connection may start a new
       
 37063 **   transaction.
       
 37064 **
       
 37065 **   This feature is included to help prevent writer-starvation.
       
 37066 */
       
 37067 struct BtShared {
       
 37068   Pager *pPager;        /* The page cache */
       
 37069   sqlite3 *db;          /* Database connection currently using this Btree */
       
 37070   BtCursor *pCursor;    /* A list of all open cursors */
       
 37071   MemPage *pPage1;      /* First page of the database */
       
 37072   u8 readOnly;          /* True if the underlying file is readonly */
       
 37073   u8 pageSizeFixed;     /* True if the page size can no longer be changed */
       
 37074 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 37075   u8 autoVacuum;        /* True if auto-vacuum is enabled */
       
 37076   u8 incrVacuum;        /* True if incr-vacuum is enabled */
       
 37077 #endif
       
 37078   u16 pageSize;         /* Total number of bytes on a page */
       
 37079   u16 usableSize;       /* Number of usable bytes on each page */
       
 37080   u16 maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
       
 37081   u16 minLocal;         /* Minimum local payload in non-LEAFDATA tables */
       
 37082   u16 maxLeaf;          /* Maximum local payload in a LEAFDATA table */
       
 37083   u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
       
 37084   u8 inTransaction;     /* Transaction state */
       
 37085   int nTransaction;     /* Number of open transactions (read + write) */
       
 37086   void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
       
 37087   void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
       
 37088   sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
       
 37089   Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
       
 37090 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 37091   int nRef;             /* Number of references to this structure */
       
 37092   BtShared *pNext;      /* Next on a list of sharable BtShared structs */
       
 37093   BtLock *pLock;        /* List of locks held on this shared-btree struct */
       
 37094   Btree *pWriter;       /* Btree with currently open write transaction */
       
 37095   u8 isExclusive;       /* True if pWriter has an EXCLUSIVE lock on the db */
       
 37096   u8 isPending;         /* If waiting for read-locks to clear */
       
 37097 #endif
       
 37098   u8 *pTmpSpace;        /* BtShared.pageSize bytes of space for tmp use */
       
 37099 };
       
 37100 
       
 37101 /*
       
 37102 ** An instance of the following structure is used to hold information
       
 37103 ** about a cell.  The parseCellPtr() function fills in this structure
       
 37104 ** based on information extract from the raw disk page.
       
 37105 */
       
 37106 typedef struct CellInfo CellInfo;
       
 37107 struct CellInfo {
       
 37108   u8 *pCell;     /* Pointer to the start of cell content */
       
 37109   i64 nKey;      /* The key for INTKEY tables, or number of bytes in key */
       
 37110   u32 nData;     /* Number of bytes of data */
       
 37111   u32 nPayload;  /* Total amount of payload */
       
 37112   u16 nHeader;   /* Size of the cell content header in bytes */
       
 37113   u16 nLocal;    /* Amount of payload held locally */
       
 37114   u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
       
 37115   u16 nSize;     /* Size of the cell content on the main b-tree page */
       
 37116 };
       
 37117 
       
 37118 /*
       
 37119 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
       
 37120 ** this will be declared corrupt. This value is calculated based on a
       
 37121 ** maximum database size of 2^31 pages a minimum fanout of 2 for a
       
 37122 ** root-node and 3 for all other internal nodes.
       
 37123 **
       
 37124 ** If a tree that appears to be taller than this is encountered, it is
       
 37125 ** assumed that the database is corrupt.
       
 37126 */
       
 37127 #define BTCURSOR_MAX_DEPTH 20
       
 37128 
       
 37129 /*
       
 37130 ** A cursor is a pointer to a particular entry within a particular
       
 37131 ** b-tree within a database file.
       
 37132 **
       
 37133 ** The entry is identified by its MemPage and the index in
       
 37134 ** MemPage.aCell[] of the entry.
       
 37135 **
       
 37136 ** When a single database file can shared by two more database connections,
       
 37137 ** but cursors cannot be shared.  Each cursor is associated with a
       
 37138 ** particular database connection identified BtCursor.pBtree.db.
       
 37139 **
       
 37140 ** Fields in this structure are accessed under the BtShared.mutex
       
 37141 ** found at self->pBt->mutex. 
       
 37142 */
       
 37143 struct BtCursor {
       
 37144   Btree *pBtree;            /* The Btree to which this cursor belongs */
       
 37145   BtShared *pBt;            /* The BtShared this cursor points to */
       
 37146   BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
       
 37147   struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
       
 37148   Pgno pgnoRoot;            /* The root page of this tree */
       
 37149   sqlite3_int64 cachedRowid; /* Next rowid cache.  0 means not valid */
       
 37150   CellInfo info;            /* A parse of the cell we are pointing at */
       
 37151   u8 wrFlag;                /* True if writable */
       
 37152   u8 atLast;                /* Cursor pointing to the last entry */
       
 37153   u8 validNKey;             /* True if info.nKey is valid */
       
 37154   u8 eState;                /* One of the CURSOR_XXX constants (see below) */
       
 37155   void *pKey;      /* Saved key that was cursor's last known position */
       
 37156   i64 nKey;        /* Size of pKey, or last integer key */
       
 37157   int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
       
 37158 #ifndef SQLITE_OMIT_INCRBLOB
       
 37159   u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
       
 37160   Pgno *aOverflow;          /* Cache of overflow page locations */
       
 37161 #endif
       
 37162   i16 iPage;                            /* Index of current page in apPage */
       
 37163   MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
       
 37164   u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
       
 37165 };
       
 37166 
       
 37167 /*
       
 37168 ** Potential values for BtCursor.eState.
       
 37169 **
       
 37170 ** CURSOR_VALID:
       
 37171 **   Cursor points to a valid entry. getPayload() etc. may be called.
       
 37172 **
       
 37173 ** CURSOR_INVALID:
       
 37174 **   Cursor does not point to a valid entry. This can happen (for example) 
       
 37175 **   because the table is empty or because BtreeCursorFirst() has not been
       
 37176 **   called.
       
 37177 **
       
 37178 ** CURSOR_REQUIRESEEK:
       
 37179 **   The table that this cursor was opened on still exists, but has been 
       
 37180 **   modified since the cursor was last used. The cursor position is saved
       
 37181 **   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in 
       
 37182 **   this state, restoreCursorPosition() can be called to attempt to
       
 37183 **   seek the cursor to the saved position.
       
 37184 **
       
 37185 ** CURSOR_FAULT:
       
 37186 **   A unrecoverable error (an I/O error or a malloc failure) has occurred
       
 37187 **   on a different connection that shares the BtShared cache with this
       
 37188 **   cursor.  The error has left the cache in an inconsistent state.
       
 37189 **   Do nothing else with this cursor.  Any attempt to use the cursor
       
 37190 **   should return the error code stored in BtCursor.skip
       
 37191 */
       
 37192 #define CURSOR_INVALID           0
       
 37193 #define CURSOR_VALID             1
       
 37194 #define CURSOR_REQUIRESEEK       2
       
 37195 #define CURSOR_FAULT             3
       
 37196 
       
 37197 /* 
       
 37198 ** The database page the PENDING_BYTE occupies. This page is never used.
       
 37199 */
       
 37200 # define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
       
 37201 
       
 37202 /*
       
 37203 ** These macros define the location of the pointer-map entry for a 
       
 37204 ** database page. The first argument to each is the number of usable
       
 37205 ** bytes on each page of the database (often 1024). The second is the
       
 37206 ** page number to look up in the pointer map.
       
 37207 **
       
 37208 ** PTRMAP_PAGENO returns the database page number of the pointer-map
       
 37209 ** page that stores the required pointer. PTRMAP_PTROFFSET returns
       
 37210 ** the offset of the requested map entry.
       
 37211 **
       
 37212 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
       
 37213 ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
       
 37214 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
       
 37215 ** this test.
       
 37216 */
       
 37217 #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
       
 37218 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
       
 37219 #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
       
 37220 
       
 37221 /*
       
 37222 ** The pointer map is a lookup table that identifies the parent page for
       
 37223 ** each child page in the database file.  The parent page is the page that
       
 37224 ** contains a pointer to the child.  Every page in the database contains
       
 37225 ** 0 or 1 parent pages.  (In this context 'database page' refers
       
 37226 ** to any page that is not part of the pointer map itself.)  Each pointer map
       
 37227 ** entry consists of a single byte 'type' and a 4 byte parent page number.
       
 37228 ** The PTRMAP_XXX identifiers below are the valid types.
       
 37229 **
       
 37230 ** The purpose of the pointer map is to facility moving pages from one
       
 37231 ** position in the file to another as part of autovacuum.  When a page
       
 37232 ** is moved, the pointer in its parent must be updated to point to the
       
 37233 ** new location.  The pointer map is used to locate the parent page quickly.
       
 37234 **
       
 37235 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
       
 37236 **                  used in this case.
       
 37237 **
       
 37238 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number 
       
 37239 **                  is not used in this case.
       
 37240 **
       
 37241 ** PTRMAP_OVERFLOW1: The database page is the first page in a list of 
       
 37242 **                   overflow pages. The page number identifies the page that
       
 37243 **                   contains the cell with a pointer to this overflow page.
       
 37244 **
       
 37245 ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
       
 37246 **                   overflow pages. The page-number identifies the previous
       
 37247 **                   page in the overflow page list.
       
 37248 **
       
 37249 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
       
 37250 **               identifies the parent page in the btree.
       
 37251 */
       
 37252 #define PTRMAP_ROOTPAGE 1
       
 37253 #define PTRMAP_FREEPAGE 2
       
 37254 #define PTRMAP_OVERFLOW1 3
       
 37255 #define PTRMAP_OVERFLOW2 4
       
 37256 #define PTRMAP_BTREE 5
       
 37257 
       
 37258 /* A bunch of assert() statements to check the transaction state variables
       
 37259 ** of handle p (type Btree*) are internally consistent.
       
 37260 */
       
 37261 #define btreeIntegrity(p) \
       
 37262   assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
       
 37263   assert( p->pBt->inTransaction>=p->inTrans ); 
       
 37264 
       
 37265 
       
 37266 /*
       
 37267 ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
       
 37268 ** if the database supports auto-vacuum or not. Because it is used
       
 37269 ** within an expression that is an argument to another macro 
       
 37270 ** (sqliteMallocRaw), it is not possible to use conditional compilation.
       
 37271 ** So, this macro is defined instead.
       
 37272 */
       
 37273 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 37274 #define ISAUTOVACUUM (pBt->autoVacuum)
       
 37275 #else
       
 37276 #define ISAUTOVACUUM 0
       
 37277 #endif
       
 37278 
       
 37279 
       
 37280 /*
       
 37281 ** This structure is passed around through all the sanity checking routines
       
 37282 ** in order to keep track of some global state information.
       
 37283 */
       
 37284 typedef struct IntegrityCk IntegrityCk;
       
 37285 struct IntegrityCk {
       
 37286   BtShared *pBt;    /* The tree being checked out */
       
 37287   Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
       
 37288   Pgno nPage;       /* Number of pages in the database */
       
 37289   int *anRef;       /* Number of times each page is referenced */
       
 37290   int mxErr;        /* Stop accumulating errors when this reaches zero */
       
 37291   int nErr;         /* Number of messages written to zErrMsg so far */
       
 37292   int mallocFailed; /* A memory allocation error has occurred */
       
 37293   StrAccum errMsg;  /* Accumulate the error message text here */
       
 37294 };
       
 37295 
       
 37296 /*
       
 37297 ** Read or write a two- and four-byte big-endian integer values.
       
 37298 */
       
 37299 #define get2byte(x)   ((x)[0]<<8 | (x)[1])
       
 37300 #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
       
 37301 #define get4byte sqlite3Get4byte
       
 37302 #define put4byte sqlite3Put4byte
       
 37303 
       
 37304 /************** End of btreeInt.h ********************************************/
       
 37305 /************** Continuing where we left off in btmutex.c ********************/
       
 37306 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 37307 #if SQLITE_THREADSAFE
       
 37308 
       
 37309 /*
       
 37310 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
       
 37311 ** set BtShared.db to the database handle associated with p and the
       
 37312 ** p->locked boolean to true.
       
 37313 */
       
 37314 static void lockBtreeMutex(Btree *p){
       
 37315   assert( p->locked==0 );
       
 37316   assert( sqlite3_mutex_notheld(p->pBt->mutex) );
       
 37317   assert( sqlite3_mutex_held(p->db->mutex) );
       
 37318 
       
 37319   sqlite3_mutex_enter(p->pBt->mutex);
       
 37320   p->pBt->db = p->db;
       
 37321   p->locked = 1;
       
 37322 }
       
 37323 
       
 37324 /*
       
 37325 ** Release the BtShared mutex associated with B-Tree handle p and
       
 37326 ** clear the p->locked boolean.
       
 37327 */
       
 37328 static void unlockBtreeMutex(Btree *p){
       
 37329   assert( p->locked==1 );
       
 37330   assert( sqlite3_mutex_held(p->pBt->mutex) );
       
 37331   assert( sqlite3_mutex_held(p->db->mutex) );
       
 37332   assert( p->db==p->pBt->db );
       
 37333 
       
 37334   sqlite3_mutex_leave(p->pBt->mutex);
       
 37335   p->locked = 0;
       
 37336 }
       
 37337 
       
 37338 /*
       
 37339 ** Enter a mutex on the given BTree object.
       
 37340 **
       
 37341 ** If the object is not sharable, then no mutex is ever required
       
 37342 ** and this routine is a no-op.  The underlying mutex is non-recursive.
       
 37343 ** But we keep a reference count in Btree.wantToLock so the behavior
       
 37344 ** of this interface is recursive.
       
 37345 **
       
 37346 ** To avoid deadlocks, multiple Btrees are locked in the same order
       
 37347 ** by all database connections.  The p->pNext is a list of other
       
 37348 ** Btrees belonging to the same database connection as the p Btree
       
 37349 ** which need to be locked after p.  If we cannot get a lock on
       
 37350 ** p, then first unlock all of the others on p->pNext, then wait
       
 37351 ** for the lock to become available on p, then relock all of the
       
 37352 ** subsequent Btrees that desire a lock.
       
 37353 */
       
 37354 SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
       
 37355   Btree *pLater;
       
 37356 
       
 37357   /* Some basic sanity checking on the Btree.  The list of Btrees
       
 37358   ** connected by pNext and pPrev should be in sorted order by
       
 37359   ** Btree.pBt value. All elements of the list should belong to
       
 37360   ** the same connection. Only shared Btrees are on the list. */
       
 37361   assert( p->pNext==0 || p->pNext->pBt>p->pBt );
       
 37362   assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
       
 37363   assert( p->pNext==0 || p->pNext->db==p->db );
       
 37364   assert( p->pPrev==0 || p->pPrev->db==p->db );
       
 37365   assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
       
 37366 
       
 37367   /* Check for locking consistency */
       
 37368   assert( !p->locked || p->wantToLock>0 );
       
 37369   assert( p->sharable || p->wantToLock==0 );
       
 37370 
       
 37371   /* We should already hold a lock on the database connection */
       
 37372   assert( sqlite3_mutex_held(p->db->mutex) );
       
 37373 
       
 37374   /* Unless the database is sharable and unlocked, then BtShared.db
       
 37375   ** should already be set correctly. */
       
 37376   assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
       
 37377 
       
 37378   if( !p->sharable ) return;
       
 37379   p->wantToLock++;
       
 37380   if( p->locked ) return;
       
 37381 
       
 37382   /* In most cases, we should be able to acquire the lock we
       
 37383   ** want without having to go throught the ascending lock
       
 37384   ** procedure that follows.  Just be sure not to block.
       
 37385   */
       
 37386   if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
       
 37387     p->pBt->db = p->db;
       
 37388     p->locked = 1;
       
 37389     return;
       
 37390   }
       
 37391 
       
 37392   /* To avoid deadlock, first release all locks with a larger
       
 37393   ** BtShared address.  Then acquire our lock.  Then reacquire
       
 37394   ** the other BtShared locks that we used to hold in ascending
       
 37395   ** order.
       
 37396   */
       
 37397   for(pLater=p->pNext; pLater; pLater=pLater->pNext){
       
 37398     assert( pLater->sharable );
       
 37399     assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
       
 37400     assert( !pLater->locked || pLater->wantToLock>0 );
       
 37401     if( pLater->locked ){
       
 37402       unlockBtreeMutex(pLater);
       
 37403     }
       
 37404   }
       
 37405   lockBtreeMutex(p);
       
 37406   for(pLater=p->pNext; pLater; pLater=pLater->pNext){
       
 37407     if( pLater->wantToLock ){
       
 37408       lockBtreeMutex(pLater);
       
 37409     }
       
 37410   }
       
 37411 }
       
 37412 
       
 37413 /*
       
 37414 ** Exit the recursive mutex on a Btree.
       
 37415 */
       
 37416 SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
       
 37417   if( p->sharable ){
       
 37418     assert( p->wantToLock>0 );
       
 37419     p->wantToLock--;
       
 37420     if( p->wantToLock==0 ){
       
 37421       unlockBtreeMutex(p);
       
 37422     }
       
 37423   }
       
 37424 }
       
 37425 
       
 37426 #ifndef NDEBUG
       
 37427 /*
       
 37428 ** Return true if the BtShared mutex is held on the btree, or if the
       
 37429 ** B-Tree is not marked as sharable.
       
 37430 **
       
 37431 ** This routine is used only from within assert() statements.
       
 37432 */
       
 37433 SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
       
 37434   assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
       
 37435   assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
       
 37436   assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
       
 37437   assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
       
 37438 
       
 37439   return (p->sharable==0 || p->locked);
       
 37440 }
       
 37441 #endif
       
 37442 
       
 37443 
       
 37444 #ifndef SQLITE_OMIT_INCRBLOB
       
 37445 /*
       
 37446 ** Enter and leave a mutex on a Btree given a cursor owned by that
       
 37447 ** Btree.  These entry points are used by incremental I/O and can be
       
 37448 ** omitted if that module is not used.
       
 37449 */
       
 37450 SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
       
 37451   sqlite3BtreeEnter(pCur->pBtree);
       
 37452 }
       
 37453 SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
       
 37454   sqlite3BtreeLeave(pCur->pBtree);
       
 37455 }
       
 37456 #endif /* SQLITE_OMIT_INCRBLOB */
       
 37457 
       
 37458 
       
 37459 /*
       
 37460 ** Enter the mutex on every Btree associated with a database
       
 37461 ** connection.  This is needed (for example) prior to parsing
       
 37462 ** a statement since we will be comparing table and column names
       
 37463 ** against all schemas and we do not want those schemas being
       
 37464 ** reset out from under us.
       
 37465 **
       
 37466 ** There is a corresponding leave-all procedures.
       
 37467 **
       
 37468 ** Enter the mutexes in accending order by BtShared pointer address
       
 37469 ** to avoid the possibility of deadlock when two threads with
       
 37470 ** two or more btrees in common both try to lock all their btrees
       
 37471 ** at the same instant.
       
 37472 */
       
 37473 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
       
 37474   int i;
       
 37475   Btree *p, *pLater;
       
 37476   assert( sqlite3_mutex_held(db->mutex) );
       
 37477   for(i=0; i<db->nDb; i++){
       
 37478     p = db->aDb[i].pBt;
       
 37479     assert( !p || (p->locked==0 && p->sharable) || p->pBt->db==p->db );
       
 37480     if( p && p->sharable ){
       
 37481       p->wantToLock++;
       
 37482       if( !p->locked ){
       
 37483         assert( p->wantToLock==1 );
       
 37484         while( p->pPrev ) p = p->pPrev;
       
 37485         /* Reason for ALWAYS:  There must be at least on unlocked Btree in
       
 37486         ** the chain.  Otherwise the !p->locked test above would have failed */
       
 37487         while( p->locked && ALWAYS(p->pNext) ) p = p->pNext;
       
 37488         for(pLater = p->pNext; pLater; pLater=pLater->pNext){
       
 37489           if( pLater->locked ){
       
 37490             unlockBtreeMutex(pLater);
       
 37491           }
       
 37492         }
       
 37493         while( p ){
       
 37494           lockBtreeMutex(p);
       
 37495           p = p->pNext;
       
 37496         }
       
 37497       }
       
 37498     }
       
 37499   }
       
 37500 }
       
 37501 SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
       
 37502   int i;
       
 37503   Btree *p;
       
 37504   assert( sqlite3_mutex_held(db->mutex) );
       
 37505   for(i=0; i<db->nDb; i++){
       
 37506     p = db->aDb[i].pBt;
       
 37507     if( p && p->sharable ){
       
 37508       assert( p->wantToLock>0 );
       
 37509       p->wantToLock--;
       
 37510       if( p->wantToLock==0 ){
       
 37511         unlockBtreeMutex(p);
       
 37512       }
       
 37513     }
       
 37514   }
       
 37515 }
       
 37516 
       
 37517 #ifndef NDEBUG
       
 37518 /*
       
 37519 ** Return true if the current thread holds the database connection
       
 37520 ** mutex and all required BtShared mutexes.
       
 37521 **
       
 37522 ** This routine is used inside assert() statements only.
       
 37523 */
       
 37524 SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
       
 37525   int i;
       
 37526   if( !sqlite3_mutex_held(db->mutex) ){
       
 37527     return 0;
       
 37528   }
       
 37529   for(i=0; i<db->nDb; i++){
       
 37530     Btree *p;
       
 37531     p = db->aDb[i].pBt;
       
 37532     if( p && p->sharable &&
       
 37533          (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
       
 37534       return 0;
       
 37535     }
       
 37536   }
       
 37537   return 1;
       
 37538 }
       
 37539 #endif /* NDEBUG */
       
 37540 
       
 37541 /*
       
 37542 ** Add a new Btree pointer to a BtreeMutexArray. 
       
 37543 ** if the pointer can possibly be shared with
       
 37544 ** another database connection.
       
 37545 **
       
 37546 ** The pointers are kept in sorted order by pBtree->pBt.  That
       
 37547 ** way when we go to enter all the mutexes, we can enter them
       
 37548 ** in order without every having to backup and retry and without
       
 37549 ** worrying about deadlock.
       
 37550 **
       
 37551 ** The number of shared btrees will always be small (usually 0 or 1)
       
 37552 ** so an insertion sort is an adequate algorithm here.
       
 37553 */
       
 37554 SQLITE_PRIVATE void sqlite3BtreeMutexArrayInsert(BtreeMutexArray *pArray, Btree *pBtree){
       
 37555   int i, j;
       
 37556   BtShared *pBt;
       
 37557   if( pBtree==0 || pBtree->sharable==0 ) return;
       
 37558 #ifndef NDEBUG
       
 37559   {
       
 37560     for(i=0; i<pArray->nMutex; i++){
       
 37561       assert( pArray->aBtree[i]!=pBtree );
       
 37562     }
       
 37563   }
       
 37564 #endif
       
 37565   assert( pArray->nMutex>=0 );
       
 37566   assert( pArray->nMutex<ArraySize(pArray->aBtree)-1 );
       
 37567   pBt = pBtree->pBt;
       
 37568   for(i=0; i<pArray->nMutex; i++){
       
 37569     assert( pArray->aBtree[i]!=pBtree );
       
 37570     if( pArray->aBtree[i]->pBt>pBt ){
       
 37571       for(j=pArray->nMutex; j>i; j--){
       
 37572         pArray->aBtree[j] = pArray->aBtree[j-1];
       
 37573       }
       
 37574       pArray->aBtree[i] = pBtree;
       
 37575       pArray->nMutex++;
       
 37576       return;
       
 37577     }
       
 37578   }
       
 37579   pArray->aBtree[pArray->nMutex++] = pBtree;
       
 37580 }
       
 37581 
       
 37582 /*
       
 37583 ** Enter the mutex of every btree in the array.  This routine is
       
 37584 ** called at the beginning of sqlite3VdbeExec().  The mutexes are
       
 37585 ** exited at the end of the same function.
       
 37586 */
       
 37587 SQLITE_PRIVATE void sqlite3BtreeMutexArrayEnter(BtreeMutexArray *pArray){
       
 37588   int i;
       
 37589   for(i=0; i<pArray->nMutex; i++){
       
 37590     Btree *p = pArray->aBtree[i];
       
 37591     /* Some basic sanity checking */
       
 37592     assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
       
 37593     assert( !p->locked || p->wantToLock>0 );
       
 37594 
       
 37595     /* We should already hold a lock on the database connection */
       
 37596     assert( sqlite3_mutex_held(p->db->mutex) );
       
 37597 
       
 37598     /* The Btree is sharable because only sharable Btrees are entered
       
 37599     ** into the array in the first place. */
       
 37600     assert( p->sharable );
       
 37601 
       
 37602     p->wantToLock++;
       
 37603     if( !p->locked ){
       
 37604       lockBtreeMutex(p);
       
 37605     }
       
 37606   }
       
 37607 }
       
 37608 
       
 37609 /*
       
 37610 ** Leave the mutex of every btree in the group.
       
 37611 */
       
 37612 SQLITE_PRIVATE void sqlite3BtreeMutexArrayLeave(BtreeMutexArray *pArray){
       
 37613   int i;
       
 37614   for(i=0; i<pArray->nMutex; i++){
       
 37615     Btree *p = pArray->aBtree[i];
       
 37616     /* Some basic sanity checking */
       
 37617     assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
       
 37618     assert( p->locked );
       
 37619     assert( p->wantToLock>0 );
       
 37620 
       
 37621     /* We should already hold a lock on the database connection */
       
 37622     assert( sqlite3_mutex_held(p->db->mutex) );
       
 37623 
       
 37624     p->wantToLock--;
       
 37625     if( p->wantToLock==0 ){
       
 37626       unlockBtreeMutex(p);
       
 37627     }
       
 37628   }
       
 37629 }
       
 37630 
       
 37631 #else
       
 37632 SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
       
 37633   p->pBt->db = p->db;
       
 37634 }
       
 37635 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
       
 37636   int i;
       
 37637   for(i=0; i<db->nDb; i++){
       
 37638     Btree *p = db->aDb[i].pBt;
       
 37639     if( p ){
       
 37640       p->pBt->db = p->db;
       
 37641     }
       
 37642   }
       
 37643 }
       
 37644 #endif /* if SQLITE_THREADSAFE */
       
 37645 #endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
       
 37646 
       
 37647 /************** End of btmutex.c *********************************************/
       
 37648 /************** Begin file btree.c *******************************************/
       
 37649 /*
       
 37650 ** 2004 April 6
       
 37651 **
       
 37652 ** The author disclaims copyright to this source code.  In place of
       
 37653 ** a legal notice, here is a blessing:
       
 37654 **
       
 37655 **    May you do good and not evil.
       
 37656 **    May you find forgiveness for yourself and forgive others.
       
 37657 **    May you share freely, never taking more than you give.
       
 37658 **
       
 37659 *************************************************************************
       
 37660 ** $Id: btree.c,v 1.705 2009/08/10 03:57:58 shane Exp $
       
 37661 **
       
 37662 ** This file implements a external (disk-based) database using BTrees.
       
 37663 ** See the header comment on "btreeInt.h" for additional information.
       
 37664 ** Including a description of file format and an overview of operation.
       
 37665 */
       
 37666 
       
 37667 /*
       
 37668 ** The header string that appears at the beginning of every
       
 37669 ** SQLite database.
       
 37670 */
       
 37671 static const char zMagicHeader[] = SQLITE_FILE_HEADER;
       
 37672 
       
 37673 /*
       
 37674 ** Set this global variable to 1 to enable tracing using the TRACE
       
 37675 ** macro.
       
 37676 */
       
 37677 #if 0
       
 37678 int sqlite3BtreeTrace=1;  /* True to enable tracing */
       
 37679 # define TRACE(X)  if(sqlite3BtreeTrace){printf X;fflush(stdout);}
       
 37680 #else
       
 37681 # define TRACE(X)
       
 37682 #endif
       
 37683 
       
 37684 
       
 37685 
       
 37686 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 37687 /*
       
 37688 ** A list of BtShared objects that are eligible for participation
       
 37689 ** in shared cache.  This variable has file scope during normal builds,
       
 37690 ** but the test harness needs to access it so we make it global for 
       
 37691 ** test builds.
       
 37692 **
       
 37693 ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
       
 37694 */
       
 37695 #ifdef SQLITE_TEST
       
 37696 SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
       
 37697 #else
       
 37698 static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
       
 37699 #endif
       
 37700 #endif /* SQLITE_OMIT_SHARED_CACHE */
       
 37701 
       
 37702 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 37703 /*
       
 37704 ** Enable or disable the shared pager and schema features.
       
 37705 **
       
 37706 ** This routine has no effect on existing database connections.
       
 37707 ** The shared cache setting effects only future calls to
       
 37708 ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
       
 37709 */
       
 37710 SQLITE_API int sqlite3_enable_shared_cache(int enable){
       
 37711   sqlite3GlobalConfig.sharedCacheEnabled = enable;
       
 37712   return SQLITE_OK;
       
 37713 }
       
 37714 #endif
       
 37715 
       
 37716 
       
 37717 
       
 37718 #ifdef SQLITE_OMIT_SHARED_CACHE
       
 37719   /*
       
 37720   ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
       
 37721   ** and clearAllSharedCacheTableLocks()
       
 37722   ** manipulate entries in the BtShared.pLock linked list used to store
       
 37723   ** shared-cache table level locks. If the library is compiled with the
       
 37724   ** shared-cache feature disabled, then there is only ever one user
       
 37725   ** of each BtShared structure and so this locking is not necessary. 
       
 37726   ** So define the lock related functions as no-ops.
       
 37727   */
       
 37728   #define querySharedCacheTableLock(a,b,c) SQLITE_OK
       
 37729   #define setSharedCacheTableLock(a,b,c) SQLITE_OK
       
 37730   #define clearAllSharedCacheTableLocks(a)
       
 37731   #define downgradeAllSharedCacheTableLocks(a)
       
 37732   #define hasSharedCacheTableLock(a,b,c,d) 1
       
 37733   #define hasReadConflicts(a, b) 0
       
 37734 #endif
       
 37735 
       
 37736 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 37737 
       
 37738 #ifdef SQLITE_DEBUG
       
 37739 /*
       
 37740 ** This function is only used as part of an assert() statement. It checks
       
 37741 ** that connection p holds the required locks to read or write to the 
       
 37742 ** b-tree with root page iRoot. If so, true is returned. Otherwise, false. 
       
 37743 ** For example, when writing to a table b-tree with root-page iRoot via 
       
 37744 ** Btree connection pBtree:
       
 37745 **
       
 37746 **    assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
       
 37747 **
       
 37748 ** When writing to an index b-tree that resides in a sharable database, the 
       
 37749 ** caller should have first obtained a lock specifying the root page of
       
 37750 ** the corresponding table b-tree. This makes things a bit more complicated,
       
 37751 ** as this module treats each b-tree as a separate structure. To determine
       
 37752 ** the table b-tree corresponding to the index b-tree being written, this
       
 37753 ** function has to search through the database schema.
       
 37754 **
       
 37755 ** Instead of a lock on the b-tree rooted at page iRoot, the caller may
       
 37756 ** hold a write-lock on the schema table (root page 1). This is also
       
 37757 ** acceptable.
       
 37758 */
       
 37759 static int hasSharedCacheTableLock(
       
 37760   Btree *pBtree,         /* Handle that must hold lock */
       
 37761   Pgno iRoot,            /* Root page of b-tree */
       
 37762   int isIndex,           /* True if iRoot is the root of an index b-tree */
       
 37763   int eLockType          /* Required lock type (READ_LOCK or WRITE_LOCK) */
       
 37764 ){
       
 37765   Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
       
 37766   Pgno iTab = 0;
       
 37767   BtLock *pLock;
       
 37768 
       
 37769   /* If this b-tree database is not shareable, or if the client is reading
       
 37770   ** and has the read-uncommitted flag set, then no lock is required. 
       
 37771   ** In these cases return true immediately.  If the client is reading 
       
 37772   ** or writing an index b-tree, but the schema is not loaded, then return
       
 37773   ** true also. In this case the lock is required, but it is too difficult
       
 37774   ** to check if the client actually holds it. This doesn't happen very
       
 37775   ** often.  */
       
 37776   if( (pBtree->sharable==0)
       
 37777    || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
       
 37778    || (isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0 ))
       
 37779   ){
       
 37780     return 1;
       
 37781   }
       
 37782 
       
 37783   /* Figure out the root-page that the lock should be held on. For table
       
 37784   ** b-trees, this is just the root page of the b-tree being read or
       
 37785   ** written. For index b-trees, it is the root page of the associated
       
 37786   ** table.  */
       
 37787   if( isIndex ){
       
 37788     HashElem *p;
       
 37789     for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
       
 37790       Index *pIdx = (Index *)sqliteHashData(p);
       
 37791       if( pIdx->tnum==(int)iRoot ){
       
 37792         iTab = pIdx->pTable->tnum;
       
 37793       }
       
 37794     }
       
 37795   }else{
       
 37796     iTab = iRoot;
       
 37797   }
       
 37798 
       
 37799   /* Search for the required lock. Either a write-lock on root-page iTab, a 
       
 37800   ** write-lock on the schema table, or (if the client is reading) a
       
 37801   ** read-lock on iTab will suffice. Return 1 if any of these are found.  */
       
 37802   for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
       
 37803     if( pLock->pBtree==pBtree 
       
 37804      && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
       
 37805      && pLock->eLock>=eLockType 
       
 37806     ){
       
 37807       return 1;
       
 37808     }
       
 37809   }
       
 37810 
       
 37811   /* Failed to find the required lock. */
       
 37812   return 0;
       
 37813 }
       
 37814 
       
 37815 /*
       
 37816 ** This function is also used as part of assert() statements only. It 
       
 37817 ** returns true if there exist one or more cursors open on the table 
       
 37818 ** with root page iRoot that do not belong to either connection pBtree 
       
 37819 ** or some other connection that has the read-uncommitted flag set.
       
 37820 **
       
 37821 ** For example, before writing to page iRoot:
       
 37822 **
       
 37823 **    assert( !hasReadConflicts(pBtree, iRoot) );
       
 37824 */
       
 37825 static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
       
 37826   BtCursor *p;
       
 37827   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
       
 37828     if( p->pgnoRoot==iRoot 
       
 37829      && p->pBtree!=pBtree
       
 37830      && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
       
 37831     ){
       
 37832       return 1;
       
 37833     }
       
 37834   }
       
 37835   return 0;
       
 37836 }
       
 37837 #endif    /* #ifdef SQLITE_DEBUG */
       
 37838 
       
 37839 /*
       
 37840 ** Query to see if btree handle p may obtain a lock of type eLock 
       
 37841 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
       
 37842 ** SQLITE_OK if the lock may be obtained (by calling
       
 37843 ** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
       
 37844 */
       
 37845 static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
       
 37846   BtShared *pBt = p->pBt;
       
 37847   BtLock *pIter;
       
 37848 
       
 37849   assert( sqlite3BtreeHoldsMutex(p) );
       
 37850   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
       
 37851   assert( p->db!=0 );
       
 37852   assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
       
 37853   
       
 37854   /* If requesting a write-lock, then the Btree must have an open write
       
 37855   ** transaction on this file. And, obviously, for this to be so there 
       
 37856   ** must be an open write transaction on the file itself.
       
 37857   */
       
 37858   assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
       
 37859   assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
       
 37860   
       
 37861   /* This is a no-op if the shared-cache is not enabled */
       
 37862   if( !p->sharable ){
       
 37863     return SQLITE_OK;
       
 37864   }
       
 37865 
       
 37866   /* If some other connection is holding an exclusive lock, the
       
 37867   ** requested lock may not be obtained.
       
 37868   */
       
 37869   if( pBt->pWriter!=p && pBt->isExclusive ){
       
 37870     sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
       
 37871     return SQLITE_LOCKED_SHAREDCACHE;
       
 37872   }
       
 37873 
       
 37874   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
       
 37875     /* The condition (pIter->eLock!=eLock) in the following if(...) 
       
 37876     ** statement is a simplification of:
       
 37877     **
       
 37878     **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
       
 37879     **
       
 37880     ** since we know that if eLock==WRITE_LOCK, then no other connection
       
 37881     ** may hold a WRITE_LOCK on any table in this file (since there can
       
 37882     ** only be a single writer).
       
 37883     */
       
 37884     assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
       
 37885     assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
       
 37886     if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
       
 37887       sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
       
 37888       if( eLock==WRITE_LOCK ){
       
 37889         assert( p==pBt->pWriter );
       
 37890         pBt->isPending = 1;
       
 37891       }
       
 37892       return SQLITE_LOCKED_SHAREDCACHE;
       
 37893     }
       
 37894   }
       
 37895   return SQLITE_OK;
       
 37896 }
       
 37897 #endif /* !SQLITE_OMIT_SHARED_CACHE */
       
 37898 
       
 37899 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 37900 /*
       
 37901 ** Add a lock on the table with root-page iTable to the shared-btree used
       
 37902 ** by Btree handle p. Parameter eLock must be either READ_LOCK or 
       
 37903 ** WRITE_LOCK.
       
 37904 **
       
 37905 ** This function assumes the following:
       
 37906 **
       
 37907 **   (a) The specified b-tree connection handle is connected to a sharable
       
 37908 **       b-tree database (one with the BtShared.sharable) flag set, and
       
 37909 **
       
 37910 **   (b) No other b-tree connection handle holds a lock that conflicts
       
 37911 **       with the requested lock (i.e. querySharedCacheTableLock() has
       
 37912 **       already been called and returned SQLITE_OK).
       
 37913 **
       
 37914 ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM 
       
 37915 ** is returned if a malloc attempt fails.
       
 37916 */
       
 37917 static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
       
 37918   BtShared *pBt = p->pBt;
       
 37919   BtLock *pLock = 0;
       
 37920   BtLock *pIter;
       
 37921 
       
 37922   assert( sqlite3BtreeHoldsMutex(p) );
       
 37923   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
       
 37924   assert( p->db!=0 );
       
 37925 
       
 37926   /* A connection with the read-uncommitted flag set will never try to
       
 37927   ** obtain a read-lock using this function. The only read-lock obtained
       
 37928   ** by a connection in read-uncommitted mode is on the sqlite_master 
       
 37929   ** table, and that lock is obtained in BtreeBeginTrans().  */
       
 37930   assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
       
 37931 
       
 37932   /* This function should only be called on a sharable b-tree after it 
       
 37933   ** has been determined that no other b-tree holds a conflicting lock.  */
       
 37934   assert( p->sharable );
       
 37935   assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
       
 37936 
       
 37937   /* First search the list for an existing lock on this table. */
       
 37938   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
       
 37939     if( pIter->iTable==iTable && pIter->pBtree==p ){
       
 37940       pLock = pIter;
       
 37941       break;
       
 37942     }
       
 37943   }
       
 37944 
       
 37945   /* If the above search did not find a BtLock struct associating Btree p
       
 37946   ** with table iTable, allocate one and link it into the list.
       
 37947   */
       
 37948   if( !pLock ){
       
 37949     pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
       
 37950     if( !pLock ){
       
 37951       return SQLITE_NOMEM;
       
 37952     }
       
 37953     pLock->iTable = iTable;
       
 37954     pLock->pBtree = p;
       
 37955     pLock->pNext = pBt->pLock;
       
 37956     pBt->pLock = pLock;
       
 37957   }
       
 37958 
       
 37959   /* Set the BtLock.eLock variable to the maximum of the current lock
       
 37960   ** and the requested lock. This means if a write-lock was already held
       
 37961   ** and a read-lock requested, we don't incorrectly downgrade the lock.
       
 37962   */
       
 37963   assert( WRITE_LOCK>READ_LOCK );
       
 37964   if( eLock>pLock->eLock ){
       
 37965     pLock->eLock = eLock;
       
 37966   }
       
 37967 
       
 37968   return SQLITE_OK;
       
 37969 }
       
 37970 #endif /* !SQLITE_OMIT_SHARED_CACHE */
       
 37971 
       
 37972 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 37973 /*
       
 37974 ** Release all the table locks (locks obtained via calls to
       
 37975 ** the setSharedCacheTableLock() procedure) held by Btree handle p.
       
 37976 **
       
 37977 ** This function assumes that handle p has an open read or write 
       
 37978 ** transaction. If it does not, then the BtShared.isPending variable
       
 37979 ** may be incorrectly cleared.
       
 37980 */
       
 37981 static void clearAllSharedCacheTableLocks(Btree *p){
       
 37982   BtShared *pBt = p->pBt;
       
 37983   BtLock **ppIter = &pBt->pLock;
       
 37984 
       
 37985   assert( sqlite3BtreeHoldsMutex(p) );
       
 37986   assert( p->sharable || 0==*ppIter );
       
 37987   assert( p->inTrans>0 );
       
 37988 
       
 37989   while( *ppIter ){
       
 37990     BtLock *pLock = *ppIter;
       
 37991     assert( pBt->isExclusive==0 || pBt->pWriter==pLock->pBtree );
       
 37992     assert( pLock->pBtree->inTrans>=pLock->eLock );
       
 37993     if( pLock->pBtree==p ){
       
 37994       *ppIter = pLock->pNext;
       
 37995       assert( pLock->iTable!=1 || pLock==&p->lock );
       
 37996       if( pLock->iTable!=1 ){
       
 37997         sqlite3_free(pLock);
       
 37998       }
       
 37999     }else{
       
 38000       ppIter = &pLock->pNext;
       
 38001     }
       
 38002   }
       
 38003 
       
 38004   assert( pBt->isPending==0 || pBt->pWriter );
       
 38005   if( pBt->pWriter==p ){
       
 38006     pBt->pWriter = 0;
       
 38007     pBt->isExclusive = 0;
       
 38008     pBt->isPending = 0;
       
 38009   }else if( pBt->nTransaction==2 ){
       
 38010     /* This function is called when connection p is concluding its 
       
 38011     ** transaction. If there currently exists a writer, and p is not
       
 38012     ** that writer, then the number of locks held by connections other
       
 38013     ** than the writer must be about to drop to zero. In this case
       
 38014     ** set the isPending flag to 0.
       
 38015     **
       
 38016     ** If there is not currently a writer, then BtShared.isPending must
       
 38017     ** be zero already. So this next line is harmless in that case.
       
 38018     */
       
 38019     pBt->isPending = 0;
       
 38020   }
       
 38021 }
       
 38022 
       
 38023 /*
       
 38024 ** This function changes all write-locks held by connection p to read-locks.
       
 38025 */
       
 38026 static void downgradeAllSharedCacheTableLocks(Btree *p){
       
 38027   BtShared *pBt = p->pBt;
       
 38028   if( pBt->pWriter==p ){
       
 38029     BtLock *pLock;
       
 38030     pBt->pWriter = 0;
       
 38031     pBt->isExclusive = 0;
       
 38032     pBt->isPending = 0;
       
 38033     for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
       
 38034       assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
       
 38035       pLock->eLock = READ_LOCK;
       
 38036     }
       
 38037   }
       
 38038 }
       
 38039 
       
 38040 #endif /* SQLITE_OMIT_SHARED_CACHE */
       
 38041 
       
 38042 static void releasePage(MemPage *pPage);  /* Forward reference */
       
 38043 
       
 38044 /*
       
 38045 ** Verify that the cursor holds a mutex on the BtShared
       
 38046 */
       
 38047 #ifndef NDEBUG
       
 38048 static int cursorHoldsMutex(BtCursor *p){
       
 38049   return sqlite3_mutex_held(p->pBt->mutex);
       
 38050 }
       
 38051 #endif
       
 38052 
       
 38053 
       
 38054 #ifndef SQLITE_OMIT_INCRBLOB
       
 38055 /*
       
 38056 ** Invalidate the overflow page-list cache for cursor pCur, if any.
       
 38057 */
       
 38058 static void invalidateOverflowCache(BtCursor *pCur){
       
 38059   assert( cursorHoldsMutex(pCur) );
       
 38060   sqlite3_free(pCur->aOverflow);
       
 38061   pCur->aOverflow = 0;
       
 38062 }
       
 38063 
       
 38064 /*
       
 38065 ** Invalidate the overflow page-list cache for all cursors opened
       
 38066 ** on the shared btree structure pBt.
       
 38067 */
       
 38068 static void invalidateAllOverflowCache(BtShared *pBt){
       
 38069   BtCursor *p;
       
 38070   assert( sqlite3_mutex_held(pBt->mutex) );
       
 38071   for(p=pBt->pCursor; p; p=p->pNext){
       
 38072     invalidateOverflowCache(p);
       
 38073   }
       
 38074 }
       
 38075 
       
 38076 /*
       
 38077 ** This function is called before modifying the contents of a table
       
 38078 ** b-tree to invalidate any incrblob cursors that are open on the
       
 38079 ** row or one of the rows being modified.
       
 38080 **
       
 38081 ** If argument isClearTable is true, then the entire contents of the
       
 38082 ** table is about to be deleted. In this case invalidate all incrblob
       
 38083 ** cursors open on any row within the table with root-page pgnoRoot.
       
 38084 **
       
 38085 ** Otherwise, if argument isClearTable is false, then the row with
       
 38086 ** rowid iRow is being replaced or deleted. In this case invalidate
       
 38087 ** only those incrblob cursors open on this specific row.
       
 38088 */
       
 38089 static void invalidateIncrblobCursors(
       
 38090   Btree *pBtree,          /* The database file to check */
       
 38091   i64 iRow,               /* The rowid that might be changing */
       
 38092   int isClearTable        /* True if all rows are being deleted */
       
 38093 ){
       
 38094   BtCursor *p;
       
 38095   BtShared *pBt = pBtree->pBt;
       
 38096   assert( sqlite3BtreeHoldsMutex(pBtree) );
       
 38097   for(p=pBt->pCursor; p; p=p->pNext){
       
 38098     if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
       
 38099       p->eState = CURSOR_INVALID;
       
 38100     }
       
 38101   }
       
 38102 }
       
 38103 
       
 38104 #else
       
 38105   #define invalidateOverflowCache(x)
       
 38106   #define invalidateAllOverflowCache(x)
       
 38107   #define invalidateIncrblobCursors(x,y,z)
       
 38108 #endif
       
 38109 
       
 38110 /*
       
 38111 ** Set bit pgno of the BtShared.pHasContent bitvec. This is called 
       
 38112 ** when a page that previously contained data becomes a free-list leaf 
       
 38113 ** page.
       
 38114 **
       
 38115 ** The BtShared.pHasContent bitvec exists to work around an obscure
       
 38116 ** bug caused by the interaction of two useful IO optimizations surrounding
       
 38117 ** free-list leaf pages:
       
 38118 **
       
 38119 **   1) When all data is deleted from a page and the page becomes
       
 38120 **      a free-list leaf page, the page is not written to the database
       
 38121 **      (as free-list leaf pages contain no meaningful data). Sometimes
       
 38122 **      such a page is not even journalled (as it will not be modified,
       
 38123 **      why bother journalling it?).
       
 38124 **
       
 38125 **   2) When a free-list leaf page is reused, its content is not read
       
 38126 **      from the database or written to the journal file (why should it
       
 38127 **      be, if it is not at all meaningful?).
       
 38128 **
       
 38129 ** By themselves, these optimizations work fine and provide a handy
       
 38130 ** performance boost to bulk delete or insert operations. However, if
       
 38131 ** a page is moved to the free-list and then reused within the same
       
 38132 ** transaction, a problem comes up. If the page is not journalled when
       
 38133 ** it is moved to the free-list and it is also not journalled when it
       
 38134 ** is extracted from the free-list and reused, then the original data
       
 38135 ** may be lost. In the event of a rollback, it may not be possible
       
 38136 ** to restore the database to its original configuration.
       
 38137 **
       
 38138 ** The solution is the BtShared.pHasContent bitvec. Whenever a page is 
       
 38139 ** moved to become a free-list leaf page, the corresponding bit is
       
 38140 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
       
 38141 ** optimization 2 above is ommitted if the corresponding bit is already
       
 38142 ** set in BtShared.pHasContent. The contents of the bitvec are cleared
       
 38143 ** at the end of every transaction.
       
 38144 */
       
 38145 static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
       
 38146   int rc = SQLITE_OK;
       
 38147   if( !pBt->pHasContent ){
       
 38148     int nPage = 100;
       
 38149     sqlite3PagerPagecount(pBt->pPager, &nPage);
       
 38150     /* If sqlite3PagerPagecount() fails there is no harm because the
       
 38151     ** nPage variable is unchanged from its default value of 100 */
       
 38152     pBt->pHasContent = sqlite3BitvecCreate((u32)nPage);
       
 38153     if( !pBt->pHasContent ){
       
 38154       rc = SQLITE_NOMEM;
       
 38155     }
       
 38156   }
       
 38157   if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
       
 38158     rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
       
 38159   }
       
 38160   return rc;
       
 38161 }
       
 38162 
       
 38163 /*
       
 38164 ** Query the BtShared.pHasContent vector.
       
 38165 **
       
 38166 ** This function is called when a free-list leaf page is removed from the
       
 38167 ** free-list for reuse. It returns false if it is safe to retrieve the
       
 38168 ** page from the pager layer with the 'no-content' flag set. True otherwise.
       
 38169 */
       
 38170 static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
       
 38171   Bitvec *p = pBt->pHasContent;
       
 38172   return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
       
 38173 }
       
 38174 
       
 38175 /*
       
 38176 ** Clear (destroy) the BtShared.pHasContent bitvec. This should be
       
 38177 ** invoked at the conclusion of each write-transaction.
       
 38178 */
       
 38179 static void btreeClearHasContent(BtShared *pBt){
       
 38180   sqlite3BitvecDestroy(pBt->pHasContent);
       
 38181   pBt->pHasContent = 0;
       
 38182 }
       
 38183 
       
 38184 /*
       
 38185 ** Save the current cursor position in the variables BtCursor.nKey 
       
 38186 ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
       
 38187 **
       
 38188 ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
       
 38189 ** prior to calling this routine.  
       
 38190 */
       
 38191 static int saveCursorPosition(BtCursor *pCur){
       
 38192   int rc;
       
 38193 
       
 38194   assert( CURSOR_VALID==pCur->eState );
       
 38195   assert( 0==pCur->pKey );
       
 38196   assert( cursorHoldsMutex(pCur) );
       
 38197 
       
 38198   rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
       
 38199   assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
       
 38200 
       
 38201   /* If this is an intKey table, then the above call to BtreeKeySize()
       
 38202   ** stores the integer key in pCur->nKey. In this case this value is
       
 38203   ** all that is required. Otherwise, if pCur is not open on an intKey
       
 38204   ** table, then malloc space for and store the pCur->nKey bytes of key 
       
 38205   ** data.
       
 38206   */
       
 38207   if( 0==pCur->apPage[0]->intKey ){
       
 38208     void *pKey = sqlite3Malloc( (int)pCur->nKey );
       
 38209     if( pKey ){
       
 38210       rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
       
 38211       if( rc==SQLITE_OK ){
       
 38212         pCur->pKey = pKey;
       
 38213       }else{
       
 38214         sqlite3_free(pKey);
       
 38215       }
       
 38216     }else{
       
 38217       rc = SQLITE_NOMEM;
       
 38218     }
       
 38219   }
       
 38220   assert( !pCur->apPage[0]->intKey || !pCur->pKey );
       
 38221 
       
 38222   if( rc==SQLITE_OK ){
       
 38223     int i;
       
 38224     for(i=0; i<=pCur->iPage; i++){
       
 38225       releasePage(pCur->apPage[i]);
       
 38226       pCur->apPage[i] = 0;
       
 38227     }
       
 38228     pCur->iPage = -1;
       
 38229     pCur->eState = CURSOR_REQUIRESEEK;
       
 38230   }
       
 38231 
       
 38232   invalidateOverflowCache(pCur);
       
 38233   return rc;
       
 38234 }
       
 38235 
       
 38236 /*
       
 38237 ** Save the positions of all cursors except pExcept open on the table 
       
 38238 ** with root-page iRoot. Usually, this is called just before cursor
       
 38239 ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
       
 38240 */
       
 38241 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
       
 38242   BtCursor *p;
       
 38243   assert( sqlite3_mutex_held(pBt->mutex) );
       
 38244   assert( pExcept==0 || pExcept->pBt==pBt );
       
 38245   for(p=pBt->pCursor; p; p=p->pNext){
       
 38246     if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && 
       
 38247         p->eState==CURSOR_VALID ){
       
 38248       int rc = saveCursorPosition(p);
       
 38249       if( SQLITE_OK!=rc ){
       
 38250         return rc;
       
 38251       }
       
 38252     }
       
 38253   }
       
 38254   return SQLITE_OK;
       
 38255 }
       
 38256 
       
 38257 /*
       
 38258 ** Clear the current cursor position.
       
 38259 */
       
 38260 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
       
 38261   assert( cursorHoldsMutex(pCur) );
       
 38262   sqlite3_free(pCur->pKey);
       
 38263   pCur->pKey = 0;
       
 38264   pCur->eState = CURSOR_INVALID;
       
 38265 }
       
 38266 
       
 38267 /*
       
 38268 ** In this version of BtreeMoveto, pKey is a packed index record
       
 38269 ** such as is generated by the OP_MakeRecord opcode.  Unpack the
       
 38270 ** record and then call BtreeMovetoUnpacked() to do the work.
       
 38271 */
       
 38272 static int btreeMoveto(
       
 38273   BtCursor *pCur,     /* Cursor open on the btree to be searched */
       
 38274   const void *pKey,   /* Packed key if the btree is an index */
       
 38275   i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
       
 38276   int bias,           /* Bias search to the high end */
       
 38277   int *pRes           /* Write search results here */
       
 38278 ){
       
 38279   int rc;                    /* Status code */
       
 38280   UnpackedRecord *pIdxKey;   /* Unpacked index key */
       
 38281   char aSpace[150];          /* Temp space for pIdxKey - to avoid a malloc */
       
 38282 
       
 38283   if( pKey ){
       
 38284     assert( nKey==(i64)(int)nKey );
       
 38285     pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey,
       
 38286                                       aSpace, sizeof(aSpace));
       
 38287     if( pIdxKey==0 ) return SQLITE_NOMEM;
       
 38288   }else{
       
 38289     pIdxKey = 0;
       
 38290   }
       
 38291   rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
       
 38292   if( pKey ){
       
 38293     sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
       
 38294   }
       
 38295   return rc;
       
 38296 }
       
 38297 
       
 38298 /*
       
 38299 ** Restore the cursor to the position it was in (or as close to as possible)
       
 38300 ** when saveCursorPosition() was called. Note that this call deletes the 
       
 38301 ** saved position info stored by saveCursorPosition(), so there can be
       
 38302 ** at most one effective restoreCursorPosition() call after each 
       
 38303 ** saveCursorPosition().
       
 38304 */
       
 38305 static int btreeRestoreCursorPosition(BtCursor *pCur){
       
 38306   int rc;
       
 38307   assert( cursorHoldsMutex(pCur) );
       
 38308   assert( pCur->eState>=CURSOR_REQUIRESEEK );
       
 38309   if( pCur->eState==CURSOR_FAULT ){
       
 38310     return pCur->skipNext;
       
 38311   }
       
 38312   pCur->eState = CURSOR_INVALID;
       
 38313   rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
       
 38314   if( rc==SQLITE_OK ){
       
 38315     sqlite3_free(pCur->pKey);
       
 38316     pCur->pKey = 0;
       
 38317     assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
       
 38318   }
       
 38319   return rc;
       
 38320 }
       
 38321 
       
 38322 #define restoreCursorPosition(p) \
       
 38323   (p->eState>=CURSOR_REQUIRESEEK ? \
       
 38324          btreeRestoreCursorPosition(p) : \
       
 38325          SQLITE_OK)
       
 38326 
       
 38327 /*
       
 38328 ** Determine whether or not a cursor has moved from the position it
       
 38329 ** was last placed at.  Cursors can move when the row they are pointing
       
 38330 ** at is deleted out from under them.
       
 38331 **
       
 38332 ** This routine returns an error code if something goes wrong.  The
       
 38333 ** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
       
 38334 */
       
 38335 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
       
 38336   int rc;
       
 38337 
       
 38338   rc = restoreCursorPosition(pCur);
       
 38339   if( rc ){
       
 38340     *pHasMoved = 1;
       
 38341     return rc;
       
 38342   }
       
 38343   if( pCur->eState!=CURSOR_VALID || pCur->skipNext!=0 ){
       
 38344     *pHasMoved = 1;
       
 38345   }else{
       
 38346     *pHasMoved = 0;
       
 38347   }
       
 38348   return SQLITE_OK;
       
 38349 }
       
 38350 
       
 38351 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 38352 /*
       
 38353 ** Given a page number of a regular database page, return the page
       
 38354 ** number for the pointer-map page that contains the entry for the
       
 38355 ** input page number.
       
 38356 */
       
 38357 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
       
 38358   int nPagesPerMapPage;
       
 38359   Pgno iPtrMap, ret;
       
 38360   assert( sqlite3_mutex_held(pBt->mutex) );
       
 38361   nPagesPerMapPage = (pBt->usableSize/5)+1;
       
 38362   iPtrMap = (pgno-2)/nPagesPerMapPage;
       
 38363   ret = (iPtrMap*nPagesPerMapPage) + 2; 
       
 38364   if( ret==PENDING_BYTE_PAGE(pBt) ){
       
 38365     ret++;
       
 38366   }
       
 38367   return ret;
       
 38368 }
       
 38369 
       
 38370 /*
       
 38371 ** Write an entry into the pointer map.
       
 38372 **
       
 38373 ** This routine updates the pointer map entry for page number 'key'
       
 38374 ** so that it maps to type 'eType' and parent page number 'pgno'.
       
 38375 **
       
 38376 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
       
 38377 ** a no-op.  If an error occurs, the appropriate error code is written
       
 38378 ** into *pRC.
       
 38379 */
       
 38380 static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
       
 38381   DbPage *pDbPage;  /* The pointer map page */
       
 38382   u8 *pPtrmap;      /* The pointer map data */
       
 38383   Pgno iPtrmap;     /* The pointer map page number */
       
 38384   int offset;       /* Offset in pointer map page */
       
 38385   int rc;           /* Return code from subfunctions */
       
 38386 
       
 38387   if( *pRC ) return;
       
 38388 
       
 38389   assert( sqlite3_mutex_held(pBt->mutex) );
       
 38390   /* The master-journal page number must never be used as a pointer map page */
       
 38391   assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
       
 38392 
       
 38393   assert( pBt->autoVacuum );
       
 38394   if( key==0 ){
       
 38395     *pRC = SQLITE_CORRUPT_BKPT;
       
 38396     return;
       
 38397   }
       
 38398   iPtrmap = PTRMAP_PAGENO(pBt, key);
       
 38399   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
       
 38400   if( rc!=SQLITE_OK ){
       
 38401     *pRC = rc;
       
 38402     return;
       
 38403   }
       
 38404   offset = PTRMAP_PTROFFSET(iPtrmap, key);
       
 38405   if( offset<0 ){
       
 38406     *pRC = SQLITE_CORRUPT_BKPT;
       
 38407     goto ptrmap_exit;
       
 38408   }
       
 38409   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
       
 38410 
       
 38411   if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
       
 38412     TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
       
 38413     *pRC= rc = sqlite3PagerWrite(pDbPage);
       
 38414     if( rc==SQLITE_OK ){
       
 38415       pPtrmap[offset] = eType;
       
 38416       put4byte(&pPtrmap[offset+1], parent);
       
 38417     }
       
 38418   }
       
 38419 
       
 38420 ptrmap_exit:
       
 38421   sqlite3PagerUnref(pDbPage);
       
 38422 }
       
 38423 
       
 38424 /*
       
 38425 ** Read an entry from the pointer map.
       
 38426 **
       
 38427 ** This routine retrieves the pointer map entry for page 'key', writing
       
 38428 ** the type and parent page number to *pEType and *pPgno respectively.
       
 38429 ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
       
 38430 */
       
 38431 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
       
 38432   DbPage *pDbPage;   /* The pointer map page */
       
 38433   int iPtrmap;       /* Pointer map page index */
       
 38434   u8 *pPtrmap;       /* Pointer map page data */
       
 38435   int offset;        /* Offset of entry in pointer map */
       
 38436   int rc;
       
 38437 
       
 38438   assert( sqlite3_mutex_held(pBt->mutex) );
       
 38439 
       
 38440   iPtrmap = PTRMAP_PAGENO(pBt, key);
       
 38441   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
       
 38442   if( rc!=0 ){
       
 38443     return rc;
       
 38444   }
       
 38445   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
       
 38446 
       
 38447   offset = PTRMAP_PTROFFSET(iPtrmap, key);
       
 38448   assert( pEType!=0 );
       
 38449   *pEType = pPtrmap[offset];
       
 38450   if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
       
 38451 
       
 38452   sqlite3PagerUnref(pDbPage);
       
 38453   if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
       
 38454   return SQLITE_OK;
       
 38455 }
       
 38456 
       
 38457 #else /* if defined SQLITE_OMIT_AUTOVACUUM */
       
 38458   #define ptrmapPut(w,x,y,z,rc)
       
 38459   #define ptrmapGet(w,x,y,z) SQLITE_OK
       
 38460   #define ptrmapPutOvflPtr(x, y, rc)
       
 38461 #endif
       
 38462 
       
 38463 /*
       
 38464 ** Given a btree page and a cell index (0 means the first cell on
       
 38465 ** the page, 1 means the second cell, and so forth) return a pointer
       
 38466 ** to the cell content.
       
 38467 **
       
 38468 ** This routine works only for pages that do not contain overflow cells.
       
 38469 */
       
 38470 #define findCell(P,I) \
       
 38471   ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)])))
       
 38472 
       
 38473 /*
       
 38474 ** This a more complex version of findCell() that works for
       
 38475 ** pages that do contain overflow cells.
       
 38476 */
       
 38477 static u8 *findOverflowCell(MemPage *pPage, int iCell){
       
 38478   int i;
       
 38479   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 38480   for(i=pPage->nOverflow-1; i>=0; i--){
       
 38481     int k;
       
 38482     struct _OvflCell *pOvfl;
       
 38483     pOvfl = &pPage->aOvfl[i];
       
 38484     k = pOvfl->idx;
       
 38485     if( k<=iCell ){
       
 38486       if( k==iCell ){
       
 38487         return pOvfl->pCell;
       
 38488       }
       
 38489       iCell--;
       
 38490     }
       
 38491   }
       
 38492   return findCell(pPage, iCell);
       
 38493 }
       
 38494 
       
 38495 /*
       
 38496 ** Parse a cell content block and fill in the CellInfo structure.  There
       
 38497 ** are two versions of this function.  btreeParseCell() takes a 
       
 38498 ** cell index as the second argument and btreeParseCellPtr() 
       
 38499 ** takes a pointer to the body of the cell as its second argument.
       
 38500 **
       
 38501 ** Within this file, the parseCell() macro can be called instead of
       
 38502 ** btreeParseCellPtr(). Using some compilers, this will be faster.
       
 38503 */
       
 38504 static void btreeParseCellPtr(
       
 38505   MemPage *pPage,         /* Page containing the cell */
       
 38506   u8 *pCell,              /* Pointer to the cell text. */
       
 38507   CellInfo *pInfo         /* Fill in this structure */
       
 38508 ){
       
 38509   u16 n;                  /* Number bytes in cell content header */
       
 38510   u32 nPayload;           /* Number of bytes of cell payload */
       
 38511 
       
 38512   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 38513 
       
 38514   pInfo->pCell = pCell;
       
 38515   assert( pPage->leaf==0 || pPage->leaf==1 );
       
 38516   n = pPage->childPtrSize;
       
 38517   assert( n==4-4*pPage->leaf );
       
 38518   if( pPage->intKey ){
       
 38519     if( pPage->hasData ){
       
 38520       n += getVarint32(&pCell[n], nPayload);
       
 38521     }else{
       
 38522       nPayload = 0;
       
 38523     }
       
 38524     n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
       
 38525     pInfo->nData = nPayload;
       
 38526   }else{
       
 38527     pInfo->nData = 0;
       
 38528     n += getVarint32(&pCell[n], nPayload);
       
 38529     pInfo->nKey = nPayload;
       
 38530   }
       
 38531   pInfo->nPayload = nPayload;
       
 38532   pInfo->nHeader = n;
       
 38533   testcase( nPayload==pPage->maxLocal );
       
 38534   testcase( nPayload==pPage->maxLocal+1 );
       
 38535   if( likely(nPayload<=pPage->maxLocal) ){
       
 38536     /* This is the (easy) common case where the entire payload fits
       
 38537     ** on the local page.  No overflow is required.
       
 38538     */
       
 38539     int nSize;          /* Total size of cell content in bytes */
       
 38540     nSize = nPayload + n;
       
 38541     pInfo->nLocal = (u16)nPayload;
       
 38542     pInfo->iOverflow = 0;
       
 38543     if( (nSize & ~3)==0 ){
       
 38544       nSize = 4;        /* Minimum cell size is 4 */
       
 38545     }
       
 38546     pInfo->nSize = (u16)nSize;
       
 38547   }else{
       
 38548     /* If the payload will not fit completely on the local page, we have
       
 38549     ** to decide how much to store locally and how much to spill onto
       
 38550     ** overflow pages.  The strategy is to minimize the amount of unused
       
 38551     ** space on overflow pages while keeping the amount of local storage
       
 38552     ** in between minLocal and maxLocal.
       
 38553     **
       
 38554     ** Warning:  changing the way overflow payload is distributed in any
       
 38555     ** way will result in an incompatible file format.
       
 38556     */
       
 38557     int minLocal;  /* Minimum amount of payload held locally */
       
 38558     int maxLocal;  /* Maximum amount of payload held locally */
       
 38559     int surplus;   /* Overflow payload available for local storage */
       
 38560 
       
 38561     minLocal = pPage->minLocal;
       
 38562     maxLocal = pPage->maxLocal;
       
 38563     surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
       
 38564     testcase( surplus==maxLocal );
       
 38565     testcase( surplus==maxLocal+1 );
       
 38566     if( surplus <= maxLocal ){
       
 38567       pInfo->nLocal = (u16)surplus;
       
 38568     }else{
       
 38569       pInfo->nLocal = (u16)minLocal;
       
 38570     }
       
 38571     pInfo->iOverflow = (u16)(pInfo->nLocal + n);
       
 38572     pInfo->nSize = pInfo->iOverflow + 4;
       
 38573   }
       
 38574 }
       
 38575 #define parseCell(pPage, iCell, pInfo) \
       
 38576   btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
       
 38577 static void btreeParseCell(
       
 38578   MemPage *pPage,         /* Page containing the cell */
       
 38579   int iCell,              /* The cell index.  First cell is 0 */
       
 38580   CellInfo *pInfo         /* Fill in this structure */
       
 38581 ){
       
 38582   parseCell(pPage, iCell, pInfo);
       
 38583 }
       
 38584 
       
 38585 /*
       
 38586 ** Compute the total number of bytes that a Cell needs in the cell
       
 38587 ** data area of the btree-page.  The return number includes the cell
       
 38588 ** data header and the local payload, but not any overflow page or
       
 38589 ** the space used by the cell pointer.
       
 38590 */
       
 38591 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
       
 38592   u8 *pIter = &pCell[pPage->childPtrSize];
       
 38593   u32 nSize;
       
 38594 
       
 38595 #ifdef SQLITE_DEBUG
       
 38596   /* The value returned by this function should always be the same as
       
 38597   ** the (CellInfo.nSize) value found by doing a full parse of the
       
 38598   ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
       
 38599   ** this function verifies that this invariant is not violated. */
       
 38600   CellInfo debuginfo;
       
 38601   btreeParseCellPtr(pPage, pCell, &debuginfo);
       
 38602 #endif
       
 38603 
       
 38604   if( pPage->intKey ){
       
 38605     u8 *pEnd;
       
 38606     if( pPage->hasData ){
       
 38607       pIter += getVarint32(pIter, nSize);
       
 38608     }else{
       
 38609       nSize = 0;
       
 38610     }
       
 38611 
       
 38612     /* pIter now points at the 64-bit integer key value, a variable length 
       
 38613     ** integer. The following block moves pIter to point at the first byte
       
 38614     ** past the end of the key value. */
       
 38615     pEnd = &pIter[9];
       
 38616     while( (*pIter++)&0x80 && pIter<pEnd );
       
 38617   }else{
       
 38618     pIter += getVarint32(pIter, nSize);
       
 38619   }
       
 38620 
       
 38621   testcase( nSize==pPage->maxLocal );
       
 38622   testcase( nSize==pPage->maxLocal+1 );
       
 38623   if( nSize>pPage->maxLocal ){
       
 38624     int minLocal = pPage->minLocal;
       
 38625     nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
       
 38626     testcase( nSize==pPage->maxLocal );
       
 38627     testcase( nSize==pPage->maxLocal+1 );
       
 38628     if( nSize>pPage->maxLocal ){
       
 38629       nSize = minLocal;
       
 38630     }
       
 38631     nSize += 4;
       
 38632   }
       
 38633   nSize += (u32)(pIter - pCell);
       
 38634 
       
 38635   /* The minimum size of any cell is 4 bytes. */
       
 38636   if( nSize<4 ){
       
 38637     nSize = 4;
       
 38638   }
       
 38639 
       
 38640   assert( nSize==debuginfo.nSize );
       
 38641   return (u16)nSize;
       
 38642 }
       
 38643 #ifndef NDEBUG
       
 38644 static u16 cellSize(MemPage *pPage, int iCell){
       
 38645   return cellSizePtr(pPage, findCell(pPage, iCell));
       
 38646 }
       
 38647 #endif
       
 38648 
       
 38649 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 38650 /*
       
 38651 ** If the cell pCell, part of page pPage contains a pointer
       
 38652 ** to an overflow page, insert an entry into the pointer-map
       
 38653 ** for the overflow page.
       
 38654 */
       
 38655 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
       
 38656   CellInfo info;
       
 38657   if( *pRC ) return;
       
 38658   assert( pCell!=0 );
       
 38659   btreeParseCellPtr(pPage, pCell, &info);
       
 38660   assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
       
 38661   if( info.iOverflow ){
       
 38662     Pgno ovfl = get4byte(&pCell[info.iOverflow]);
       
 38663     ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
       
 38664   }
       
 38665 }
       
 38666 #endif
       
 38667 
       
 38668 
       
 38669 /*
       
 38670 ** Defragment the page given.  All Cells are moved to the
       
 38671 ** end of the page and all free space is collected into one
       
 38672 ** big FreeBlk that occurs in between the header and cell
       
 38673 ** pointer array and the cell content area.
       
 38674 */
       
 38675 static int defragmentPage(MemPage *pPage){
       
 38676   int i;                     /* Loop counter */
       
 38677   int pc;                    /* Address of a i-th cell */
       
 38678   int hdr;                   /* Offset to the page header */
       
 38679   int size;                  /* Size of a cell */
       
 38680   int usableSize;            /* Number of usable bytes on a page */
       
 38681   int cellOffset;            /* Offset to the cell pointer array */
       
 38682   int cbrk;                  /* Offset to the cell content area */
       
 38683   int nCell;                 /* Number of cells on the page */
       
 38684   unsigned char *data;       /* The page data */
       
 38685   unsigned char *temp;       /* Temp area for cell content */
       
 38686   int iCellFirst;            /* First allowable cell index */
       
 38687   int iCellLast;             /* Last possible cell index */
       
 38688 
       
 38689 
       
 38690   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
 38691   assert( pPage->pBt!=0 );
       
 38692   assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
       
 38693   assert( pPage->nOverflow==0 );
       
 38694   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 38695   temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
       
 38696   data = pPage->aData;
       
 38697   hdr = pPage->hdrOffset;
       
 38698   cellOffset = pPage->cellOffset;
       
 38699   nCell = pPage->nCell;
       
 38700   assert( nCell==get2byte(&data[hdr+3]) );
       
 38701   usableSize = pPage->pBt->usableSize;
       
 38702   cbrk = get2byte(&data[hdr+5]);
       
 38703   memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
       
 38704   cbrk = usableSize;
       
 38705   iCellFirst = cellOffset + 2*nCell;
       
 38706   iCellLast = usableSize - 4;
       
 38707   for(i=0; i<nCell; i++){
       
 38708     u8 *pAddr;     /* The i-th cell pointer */
       
 38709     pAddr = &data[cellOffset + i*2];
       
 38710     pc = get2byte(pAddr);
       
 38711     testcase( pc==iCellFirst );
       
 38712     testcase( pc==iCellLast );
       
 38713 #if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
       
 38714     /* These conditions have already been verified in btreeInitPage()
       
 38715     ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined 
       
 38716     */
       
 38717     if( pc<iCellFirst || pc>iCellLast ){
       
 38718       return SQLITE_CORRUPT_BKPT;
       
 38719     }
       
 38720 #endif
       
 38721     assert( pc>=iCellFirst && pc<=iCellLast );
       
 38722     size = cellSizePtr(pPage, &temp[pc]);
       
 38723     cbrk -= size;
       
 38724 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
       
 38725     if( cbrk<iCellFirst ){
       
 38726       return SQLITE_CORRUPT_BKPT;
       
 38727     }
       
 38728 #else
       
 38729     if( cbrk<iCellFirst || pc+size>usableSize ){
       
 38730       return SQLITE_CORRUPT_BKPT;
       
 38731     }
       
 38732 #endif
       
 38733     assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
       
 38734     testcase( cbrk+size==usableSize );
       
 38735     testcase( pc+size==usableSize );
       
 38736     memcpy(&data[cbrk], &temp[pc], size);
       
 38737     put2byte(pAddr, cbrk);
       
 38738   }
       
 38739   assert( cbrk>=iCellFirst );
       
 38740   put2byte(&data[hdr+5], cbrk);
       
 38741   data[hdr+1] = 0;
       
 38742   data[hdr+2] = 0;
       
 38743   data[hdr+7] = 0;
       
 38744   memset(&data[iCellFirst], 0, cbrk-iCellFirst);
       
 38745   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
 38746   if( cbrk-iCellFirst!=pPage->nFree ){
       
 38747     return SQLITE_CORRUPT_BKPT;
       
 38748   }
       
 38749   return SQLITE_OK;
       
 38750 }
       
 38751 
       
 38752 /*
       
 38753 ** Allocate nByte bytes of space from within the B-Tree page passed
       
 38754 ** as the first argument. Write into *pIdx the index into pPage->aData[]
       
 38755 ** of the first byte of allocated space. Return either SQLITE_OK or
       
 38756 ** an error code (usually SQLITE_CORRUPT).
       
 38757 **
       
 38758 ** The caller guarantees that there is sufficient space to make the
       
 38759 ** allocation.  This routine might need to defragment in order to bring
       
 38760 ** all the space together, however.  This routine will avoid using
       
 38761 ** the first two bytes past the cell pointer area since presumably this
       
 38762 ** allocation is being made in order to insert a new cell, so we will
       
 38763 ** also end up needing a new cell pointer.
       
 38764 */
       
 38765 static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
       
 38766   const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
       
 38767   u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
       
 38768   int nFrag;                           /* Number of fragmented bytes on pPage */
       
 38769   int top;                             /* First byte of cell content area */
       
 38770   int gap;        /* First byte of gap between cell pointers and cell content */
       
 38771   int rc;         /* Integer return code */
       
 38772   
       
 38773   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
 38774   assert( pPage->pBt );
       
 38775   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 38776   assert( nByte>=0 );  /* Minimum cell size is 4 */
       
 38777   assert( pPage->nFree>=nByte );
       
 38778   assert( pPage->nOverflow==0 );
       
 38779   assert( nByte<pPage->pBt->usableSize-8 );
       
 38780 
       
 38781   nFrag = data[hdr+7];
       
 38782   assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
       
 38783   gap = pPage->cellOffset + 2*pPage->nCell;
       
 38784   top = get2byte(&data[hdr+5]);
       
 38785   if( gap>top ) return SQLITE_CORRUPT_BKPT;
       
 38786   testcase( gap+2==top );
       
 38787   testcase( gap+1==top );
       
 38788   testcase( gap==top );
       
 38789 
       
 38790   if( nFrag>=60 ){
       
 38791     /* Always defragment highly fragmented pages */
       
 38792     rc = defragmentPage(pPage);
       
 38793     if( rc ) return rc;
       
 38794     top = get2byte(&data[hdr+5]);
       
 38795   }else if( gap+2<=top ){
       
 38796     /* Search the freelist looking for a free slot big enough to satisfy 
       
 38797     ** the request. The allocation is made from the first free slot in 
       
 38798     ** the list that is large enough to accomadate it.
       
 38799     */
       
 38800     int pc, addr;
       
 38801     for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
       
 38802       int size = get2byte(&data[pc+2]);     /* Size of free slot */
       
 38803       if( size>=nByte ){
       
 38804         int x = size - nByte;
       
 38805         testcase( x==4 );
       
 38806         testcase( x==3 );
       
 38807         if( x<4 ){
       
 38808           /* Remove the slot from the free-list. Update the number of
       
 38809           ** fragmented bytes within the page. */
       
 38810           memcpy(&data[addr], &data[pc], 2);
       
 38811           data[hdr+7] = (u8)(nFrag + x);
       
 38812         }else{
       
 38813           /* The slot remains on the free-list. Reduce its size to account
       
 38814           ** for the portion used by the new allocation. */
       
 38815           put2byte(&data[pc+2], x);
       
 38816         }
       
 38817         *pIdx = pc + x;
       
 38818         return SQLITE_OK;
       
 38819       }
       
 38820     }
       
 38821   }
       
 38822 
       
 38823   /* Check to make sure there is enough space in the gap to satisfy
       
 38824   ** the allocation.  If not, defragment.
       
 38825   */
       
 38826   testcase( gap+2+nByte==top );
       
 38827   if( gap+2+nByte>top ){
       
 38828     rc = defragmentPage(pPage);
       
 38829     if( rc ) return rc;
       
 38830     top = get2byte(&data[hdr+5]);
       
 38831     assert( gap+nByte<=top );
       
 38832   }
       
 38833 
       
 38834 
       
 38835   /* Allocate memory from the gap in between the cell pointer array
       
 38836   ** and the cell content area.  The btreeInitPage() call has already
       
 38837   ** validated the freelist.  Given that the freelist is valid, there
       
 38838   ** is no way that the allocation can extend off the end of the page.
       
 38839   ** The assert() below verifies the previous sentence.
       
 38840   */
       
 38841   top -= nByte;
       
 38842   put2byte(&data[hdr+5], top);
       
 38843   assert( top+nByte <= pPage->pBt->usableSize );
       
 38844   *pIdx = top;
       
 38845   return SQLITE_OK;
       
 38846 }
       
 38847 
       
 38848 /*
       
 38849 ** Return a section of the pPage->aData to the freelist.
       
 38850 ** The first byte of the new free block is pPage->aDisk[start]
       
 38851 ** and the size of the block is "size" bytes.
       
 38852 **
       
 38853 ** Most of the effort here is involved in coalesing adjacent
       
 38854 ** free blocks into a single big free block.
       
 38855 */
       
 38856 static int freeSpace(MemPage *pPage, int start, int size){
       
 38857   int addr, pbegin, hdr;
       
 38858   int iLast;                        /* Largest possible freeblock offset */
       
 38859   unsigned char *data = pPage->aData;
       
 38860 
       
 38861   assert( pPage->pBt!=0 );
       
 38862   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
 38863   assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
       
 38864   assert( (start + size)<=pPage->pBt->usableSize );
       
 38865   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 38866   assert( size>=0 );   /* Minimum cell size is 4 */
       
 38867 
       
 38868 #ifdef SQLITE_SECURE_DELETE
       
 38869   /* Overwrite deleted information with zeros when the SECURE_DELETE 
       
 38870   ** option is enabled at compile-time */
       
 38871   memset(&data[start], 0, size);
       
 38872 #endif
       
 38873 
       
 38874   /* Add the space back into the linked list of freeblocks.  Note that
       
 38875   ** even though the freeblock list was checked by btreeInitPage(),
       
 38876   ** btreeInitPage() did not detect overlapping cells or
       
 38877   ** freeblocks that overlapped cells.   Nor does it detect when the
       
 38878   ** cell content area exceeds the value in the page header.  If these
       
 38879   ** situations arise, then subsequent insert operations might corrupt
       
 38880   ** the freelist.  So we do need to check for corruption while scanning
       
 38881   ** the freelist.
       
 38882   */
       
 38883   hdr = pPage->hdrOffset;
       
 38884   addr = hdr + 1;
       
 38885   iLast = pPage->pBt->usableSize - 4;
       
 38886   assert( start<=iLast );
       
 38887   while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
       
 38888     if( pbegin<addr+4 ){
       
 38889       return SQLITE_CORRUPT_BKPT;
       
 38890     }
       
 38891     addr = pbegin;
       
 38892   }
       
 38893   if( pbegin>iLast ){
       
 38894     return SQLITE_CORRUPT_BKPT;
       
 38895   }
       
 38896   assert( pbegin>addr || pbegin==0 );
       
 38897   put2byte(&data[addr], start);
       
 38898   put2byte(&data[start], pbegin);
       
 38899   put2byte(&data[start+2], size);
       
 38900   pPage->nFree = pPage->nFree + (u16)size;
       
 38901 
       
 38902   /* Coalesce adjacent free blocks */
       
 38903   addr = hdr + 1;
       
 38904   while( (pbegin = get2byte(&data[addr]))>0 ){
       
 38905     int pnext, psize, x;
       
 38906     assert( pbegin>addr );
       
 38907     assert( pbegin<=pPage->pBt->usableSize-4 );
       
 38908     pnext = get2byte(&data[pbegin]);
       
 38909     psize = get2byte(&data[pbegin+2]);
       
 38910     if( pbegin + psize + 3 >= pnext && pnext>0 ){
       
 38911       int frag = pnext - (pbegin+psize);
       
 38912       if( (frag<0) || (frag>(int)data[hdr+7]) ){
       
 38913         return SQLITE_CORRUPT_BKPT;
       
 38914       }
       
 38915       data[hdr+7] -= (u8)frag;
       
 38916       x = get2byte(&data[pnext]);
       
 38917       put2byte(&data[pbegin], x);
       
 38918       x = pnext + get2byte(&data[pnext+2]) - pbegin;
       
 38919       put2byte(&data[pbegin+2], x);
       
 38920     }else{
       
 38921       addr = pbegin;
       
 38922     }
       
 38923   }
       
 38924 
       
 38925   /* If the cell content area begins with a freeblock, remove it. */
       
 38926   if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
       
 38927     int top;
       
 38928     pbegin = get2byte(&data[hdr+1]);
       
 38929     memcpy(&data[hdr+1], &data[pbegin], 2);
       
 38930     top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
       
 38931     put2byte(&data[hdr+5], top);
       
 38932   }
       
 38933   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
 38934   return SQLITE_OK;
       
 38935 }
       
 38936 
       
 38937 /*
       
 38938 ** Decode the flags byte (the first byte of the header) for a page
       
 38939 ** and initialize fields of the MemPage structure accordingly.
       
 38940 **
       
 38941 ** Only the following combinations are supported.  Anything different
       
 38942 ** indicates a corrupt database files:
       
 38943 **
       
 38944 **         PTF_ZERODATA
       
 38945 **         PTF_ZERODATA | PTF_LEAF
       
 38946 **         PTF_LEAFDATA | PTF_INTKEY
       
 38947 **         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
       
 38948 */
       
 38949 static int decodeFlags(MemPage *pPage, int flagByte){
       
 38950   BtShared *pBt;     /* A copy of pPage->pBt */
       
 38951 
       
 38952   assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
       
 38953   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 38954   pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
       
 38955   flagByte &= ~PTF_LEAF;
       
 38956   pPage->childPtrSize = 4-4*pPage->leaf;
       
 38957   pBt = pPage->pBt;
       
 38958   if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
       
 38959     pPage->intKey = 1;
       
 38960     pPage->hasData = pPage->leaf;
       
 38961     pPage->maxLocal = pBt->maxLeaf;
       
 38962     pPage->minLocal = pBt->minLeaf;
       
 38963   }else if( flagByte==PTF_ZERODATA ){
       
 38964     pPage->intKey = 0;
       
 38965     pPage->hasData = 0;
       
 38966     pPage->maxLocal = pBt->maxLocal;
       
 38967     pPage->minLocal = pBt->minLocal;
       
 38968   }else{
       
 38969     return SQLITE_CORRUPT_BKPT;
       
 38970   }
       
 38971   return SQLITE_OK;
       
 38972 }
       
 38973 
       
 38974 /*
       
 38975 ** Initialize the auxiliary information for a disk block.
       
 38976 **
       
 38977 ** Return SQLITE_OK on success.  If we see that the page does
       
 38978 ** not contain a well-formed database page, then return 
       
 38979 ** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
       
 38980 ** guarantee that the page is well-formed.  It only shows that
       
 38981 ** we failed to detect any corruption.
       
 38982 */
       
 38983 static int btreeInitPage(MemPage *pPage){
       
 38984 
       
 38985   assert( pPage->pBt!=0 );
       
 38986   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 38987   assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
       
 38988   assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
       
 38989   assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
       
 38990 
       
 38991   if( !pPage->isInit ){
       
 38992     u16 pc;            /* Address of a freeblock within pPage->aData[] */
       
 38993     u8 hdr;            /* Offset to beginning of page header */
       
 38994     u8 *data;          /* Equal to pPage->aData */
       
 38995     BtShared *pBt;        /* The main btree structure */
       
 38996     u16 usableSize;    /* Amount of usable space on each page */
       
 38997     u16 cellOffset;    /* Offset from start of page to first cell pointer */
       
 38998     u16 nFree;         /* Number of unused bytes on the page */
       
 38999     u16 top;           /* First byte of the cell content area */
       
 39000     int iCellFirst;    /* First allowable cell or freeblock offset */
       
 39001     int iCellLast;     /* Last possible cell or freeblock offset */
       
 39002 
       
 39003     pBt = pPage->pBt;
       
 39004 
       
 39005     hdr = pPage->hdrOffset;
       
 39006     data = pPage->aData;
       
 39007     if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
       
 39008     assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );
       
 39009     pPage->maskPage = pBt->pageSize - 1;
       
 39010     pPage->nOverflow = 0;
       
 39011     usableSize = pBt->usableSize;
       
 39012     pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
       
 39013     top = get2byte(&data[hdr+5]);
       
 39014     pPage->nCell = get2byte(&data[hdr+3]);
       
 39015     if( pPage->nCell>MX_CELL(pBt) ){
       
 39016       /* To many cells for a single page.  The page must be corrupt */
       
 39017       return SQLITE_CORRUPT_BKPT;
       
 39018     }
       
 39019     testcase( pPage->nCell==MX_CELL(pBt) );
       
 39020 
       
 39021     /* A malformed database page might cause us to read past the end
       
 39022     ** of page when parsing a cell.  
       
 39023     **
       
 39024     ** The following block of code checks early to see if a cell extends
       
 39025     ** past the end of a page boundary and causes SQLITE_CORRUPT to be 
       
 39026     ** returned if it does.
       
 39027     */
       
 39028     iCellFirst = cellOffset + 2*pPage->nCell;
       
 39029     iCellLast = usableSize - 4;
       
 39030 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
       
 39031     {
       
 39032       int i;            /* Index into the cell pointer array */
       
 39033       int sz;           /* Size of a cell */
       
 39034 
       
 39035       if( !pPage->leaf ) iCellLast--;
       
 39036       for(i=0; i<pPage->nCell; i++){
       
 39037         pc = get2byte(&data[cellOffset+i*2]);
       
 39038         testcase( pc==iCellFirst );
       
 39039         testcase( pc==iCellLast );
       
 39040         if( pc<iCellFirst || pc>iCellLast ){
       
 39041           return SQLITE_CORRUPT_BKPT;
       
 39042         }
       
 39043         sz = cellSizePtr(pPage, &data[pc]);
       
 39044         testcase( pc+sz==usableSize );
       
 39045         if( pc+sz>usableSize ){
       
 39046           return SQLITE_CORRUPT_BKPT;
       
 39047         }
       
 39048       }
       
 39049       if( !pPage->leaf ) iCellLast++;
       
 39050     }  
       
 39051 #endif
       
 39052 
       
 39053     /* Compute the total free space on the page */
       
 39054     pc = get2byte(&data[hdr+1]);
       
 39055     nFree = data[hdr+7] + top;
       
 39056     while( pc>0 ){
       
 39057       u16 next, size;
       
 39058       if( pc<iCellFirst || pc>iCellLast ){
       
 39059         /* Start of free block is off the page */
       
 39060         return SQLITE_CORRUPT_BKPT; 
       
 39061       }
       
 39062       next = get2byte(&data[pc]);
       
 39063       size = get2byte(&data[pc+2]);
       
 39064       if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
       
 39065         /* Free blocks must be in ascending order. And the last byte of
       
 39066 	** the free-block must lie on the database page.  */
       
 39067         return SQLITE_CORRUPT_BKPT; 
       
 39068       }
       
 39069       nFree = nFree + size;
       
 39070       pc = next;
       
 39071     }
       
 39072 
       
 39073     /* At this point, nFree contains the sum of the offset to the start
       
 39074     ** of the cell-content area plus the number of free bytes within
       
 39075     ** the cell-content area. If this is greater than the usable-size
       
 39076     ** of the page, then the page must be corrupted. This check also
       
 39077     ** serves to verify that the offset to the start of the cell-content
       
 39078     ** area, according to the page header, lies within the page.
       
 39079     */
       
 39080     if( nFree>usableSize ){
       
 39081       return SQLITE_CORRUPT_BKPT; 
       
 39082     }
       
 39083     pPage->nFree = (u16)(nFree - iCellFirst);
       
 39084     pPage->isInit = 1;
       
 39085   }
       
 39086   return SQLITE_OK;
       
 39087 }
       
 39088 
       
 39089 /*
       
 39090 ** Set up a raw page so that it looks like a database page holding
       
 39091 ** no entries.
       
 39092 */
       
 39093 static void zeroPage(MemPage *pPage, int flags){
       
 39094   unsigned char *data = pPage->aData;
       
 39095   BtShared *pBt = pPage->pBt;
       
 39096   u8 hdr = pPage->hdrOffset;
       
 39097   u16 first;
       
 39098 
       
 39099   assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
       
 39100   assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
       
 39101   assert( sqlite3PagerGetData(pPage->pDbPage) == data );
       
 39102   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
 39103   assert( sqlite3_mutex_held(pBt->mutex) );
       
 39104   /*memset(&data[hdr], 0, pBt->usableSize - hdr);*/
       
 39105   data[hdr] = (char)flags;
       
 39106   first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
       
 39107   memset(&data[hdr+1], 0, 4);
       
 39108   data[hdr+7] = 0;
       
 39109   put2byte(&data[hdr+5], pBt->usableSize);
       
 39110   pPage->nFree = pBt->usableSize - first;
       
 39111   decodeFlags(pPage, flags);
       
 39112   pPage->hdrOffset = hdr;
       
 39113   pPage->cellOffset = first;
       
 39114   pPage->nOverflow = 0;
       
 39115   assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );
       
 39116   pPage->maskPage = pBt->pageSize - 1;
       
 39117   pPage->nCell = 0;
       
 39118   pPage->isInit = 1;
       
 39119 }
       
 39120 
       
 39121 
       
 39122 /*
       
 39123 ** Convert a DbPage obtained from the pager into a MemPage used by
       
 39124 ** the btree layer.
       
 39125 */
       
 39126 static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
       
 39127   MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
       
 39128   pPage->aData = sqlite3PagerGetData(pDbPage);
       
 39129   pPage->pDbPage = pDbPage;
       
 39130   pPage->pBt = pBt;
       
 39131   pPage->pgno = pgno;
       
 39132   pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
       
 39133   return pPage; 
       
 39134 }
       
 39135 
       
 39136 /*
       
 39137 ** Get a page from the pager.  Initialize the MemPage.pBt and
       
 39138 ** MemPage.aData elements if needed.
       
 39139 **
       
 39140 ** If the noContent flag is set, it means that we do not care about
       
 39141 ** the content of the page at this time.  So do not go to the disk
       
 39142 ** to fetch the content.  Just fill in the content with zeros for now.
       
 39143 ** If in the future we call sqlite3PagerWrite() on this page, that
       
 39144 ** means we have started to be concerned about content and the disk
       
 39145 ** read should occur at that point.
       
 39146 */
       
 39147 static int btreeGetPage(
       
 39148   BtShared *pBt,       /* The btree */
       
 39149   Pgno pgno,           /* Number of the page to fetch */
       
 39150   MemPage **ppPage,    /* Return the page in this parameter */
       
 39151   int noContent        /* Do not load page content if true */
       
 39152 ){
       
 39153   int rc;
       
 39154   DbPage *pDbPage;
       
 39155 
       
 39156   assert( sqlite3_mutex_held(pBt->mutex) );
       
 39157   rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
       
 39158   if( rc ) return rc;
       
 39159   *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
       
 39160   return SQLITE_OK;
       
 39161 }
       
 39162 
       
 39163 /*
       
 39164 ** Retrieve a page from the pager cache. If the requested page is not
       
 39165 ** already in the pager cache return NULL. Initialize the MemPage.pBt and
       
 39166 ** MemPage.aData elements if needed.
       
 39167 */
       
 39168 static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
       
 39169   DbPage *pDbPage;
       
 39170   assert( sqlite3_mutex_held(pBt->mutex) );
       
 39171   pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
       
 39172   if( pDbPage ){
       
 39173     return btreePageFromDbPage(pDbPage, pgno, pBt);
       
 39174   }
       
 39175   return 0;
       
 39176 }
       
 39177 
       
 39178 /*
       
 39179 ** Return the size of the database file in pages. If there is any kind of
       
 39180 ** error, return ((unsigned int)-1).
       
 39181 */
       
 39182 static Pgno pagerPagecount(BtShared *pBt){
       
 39183   int nPage = -1;
       
 39184   int rc;
       
 39185   assert( pBt->pPage1 );
       
 39186   rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
       
 39187   assert( rc==SQLITE_OK || nPage==-1 );
       
 39188   return (Pgno)nPage;
       
 39189 }
       
 39190 
       
 39191 /*
       
 39192 ** Get a page from the pager and initialize it.  This routine is just a
       
 39193 ** convenience wrapper around separate calls to btreeGetPage() and 
       
 39194 ** btreeInitPage().
       
 39195 **
       
 39196 ** If an error occurs, then the value *ppPage is set to is undefined. It
       
 39197 ** may remain unchanged, or it may be set to an invalid value.
       
 39198 */
       
 39199 static int getAndInitPage(
       
 39200   BtShared *pBt,          /* The database file */
       
 39201   Pgno pgno,           /* Number of the page to get */
       
 39202   MemPage **ppPage     /* Write the page pointer here */
       
 39203 ){
       
 39204   int rc;
       
 39205   TESTONLY( Pgno iLastPg = pagerPagecount(pBt); )
       
 39206   assert( sqlite3_mutex_held(pBt->mutex) );
       
 39207 
       
 39208   rc = btreeGetPage(pBt, pgno, ppPage, 0);
       
 39209   if( rc==SQLITE_OK ){
       
 39210     rc = btreeInitPage(*ppPage);
       
 39211     if( rc!=SQLITE_OK ){
       
 39212       releasePage(*ppPage);
       
 39213     }
       
 39214   }
       
 39215 
       
 39216   /* If the requested page number was either 0 or greater than the page
       
 39217   ** number of the last page in the database, this function should return
       
 39218   ** SQLITE_CORRUPT or some other error (i.e. SQLITE_FULL). Check that this
       
 39219   ** is the case.  */
       
 39220   assert( (pgno>0 && pgno<=iLastPg) || rc!=SQLITE_OK );
       
 39221   testcase( pgno==0 );
       
 39222   testcase( pgno==iLastPg );
       
 39223 
       
 39224   return rc;
       
 39225 }
       
 39226 
       
 39227 /*
       
 39228 ** Release a MemPage.  This should be called once for each prior
       
 39229 ** call to btreeGetPage.
       
 39230 */
       
 39231 static void releasePage(MemPage *pPage){
       
 39232   if( pPage ){
       
 39233     assert( pPage->nOverflow==0 || sqlite3PagerPageRefcount(pPage->pDbPage)>1 );
       
 39234     assert( pPage->aData );
       
 39235     assert( pPage->pBt );
       
 39236     assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
       
 39237     assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
       
 39238     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 39239     sqlite3PagerUnref(pPage->pDbPage);
       
 39240   }
       
 39241 }
       
 39242 
       
 39243 /*
       
 39244 ** During a rollback, when the pager reloads information into the cache
       
 39245 ** so that the cache is restored to its original state at the start of
       
 39246 ** the transaction, for each page restored this routine is called.
       
 39247 **
       
 39248 ** This routine needs to reset the extra data section at the end of the
       
 39249 ** page to agree with the restored data.
       
 39250 */
       
 39251 static void pageReinit(DbPage *pData){
       
 39252   MemPage *pPage;
       
 39253   pPage = (MemPage *)sqlite3PagerGetExtra(pData);
       
 39254   assert( sqlite3PagerPageRefcount(pData)>0 );
       
 39255   if( pPage->isInit ){
       
 39256     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 39257     pPage->isInit = 0;
       
 39258     if( sqlite3PagerPageRefcount(pData)>1 ){
       
 39259       /* pPage might not be a btree page;  it might be an overflow page
       
 39260       ** or ptrmap page or a free page.  In those cases, the following
       
 39261       ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
       
 39262       ** But no harm is done by this.  And it is very important that
       
 39263       ** btreeInitPage() be called on every btree page so we make
       
 39264       ** the call for every page that comes in for re-initing. */
       
 39265       btreeInitPage(pPage);
       
 39266     }
       
 39267   }
       
 39268 }
       
 39269 
       
 39270 /*
       
 39271 ** Invoke the busy handler for a btree.
       
 39272 */
       
 39273 static int btreeInvokeBusyHandler(void *pArg){
       
 39274   BtShared *pBt = (BtShared*)pArg;
       
 39275   assert( pBt->db );
       
 39276   assert( sqlite3_mutex_held(pBt->db->mutex) );
       
 39277   return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
       
 39278 }
       
 39279 
       
 39280 /*
       
 39281 ** Open a database file.
       
 39282 ** 
       
 39283 ** zFilename is the name of the database file.  If zFilename is NULL
       
 39284 ** a new database with a random name is created.  This randomly named
       
 39285 ** database file will be deleted when sqlite3BtreeClose() is called.
       
 39286 ** If zFilename is ":memory:" then an in-memory database is created
       
 39287 ** that is automatically destroyed when it is closed.
       
 39288 **
       
 39289 ** If the database is already opened in the same database connection
       
 39290 ** and we are in shared cache mode, then the open will fail with an
       
 39291 ** SQLITE_CONSTRAINT error.  We cannot allow two or more BtShared
       
 39292 ** objects in the same database connection since doing so will lead
       
 39293 ** to problems with locking.
       
 39294 */
       
 39295 SQLITE_PRIVATE int sqlite3BtreeOpen(
       
 39296   const char *zFilename,  /* Name of the file containing the BTree database */
       
 39297   sqlite3 *db,            /* Associated database handle */
       
 39298   Btree **ppBtree,        /* Pointer to new Btree object written here */
       
 39299   int flags,              /* Options */
       
 39300   int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
       
 39301 ){
       
 39302   sqlite3_vfs *pVfs;             /* The VFS to use for this btree */
       
 39303   BtShared *pBt = 0;             /* Shared part of btree structure */
       
 39304   Btree *p;                      /* Handle to return */
       
 39305   sqlite3_mutex *mutexOpen = 0;  /* Prevents a race condition. Ticket #3537 */
       
 39306   int rc = SQLITE_OK;            /* Result code from this function */
       
 39307   u8 nReserve;                   /* Byte of unused space on each page */
       
 39308   unsigned char zDbHeader[100];  /* Database header content */
       
 39309 
       
 39310   /* Set the variable isMemdb to true for an in-memory database, or 
       
 39311   ** false for a file-based database. This symbol is only required if
       
 39312   ** either of the shared-data or autovacuum features are compiled 
       
 39313   ** into the library.
       
 39314   */
       
 39315 #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM)
       
 39316   #ifdef SQLITE_OMIT_MEMORYDB
       
 39317     const int isMemdb = 0;
       
 39318   #else
       
 39319     const int isMemdb = zFilename && !strcmp(zFilename, ":memory:");
       
 39320   #endif
       
 39321 #endif
       
 39322 
       
 39323   assert( db!=0 );
       
 39324   assert( sqlite3_mutex_held(db->mutex) );
       
 39325 
       
 39326   pVfs = db->pVfs;
       
 39327   p = sqlite3MallocZero(sizeof(Btree));
       
 39328   if( !p ){
       
 39329     return SQLITE_NOMEM;
       
 39330   }
       
 39331   p->inTrans = TRANS_NONE;
       
 39332   p->db = db;
       
 39333 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 39334   p->lock.pBtree = p;
       
 39335   p->lock.iTable = 1;
       
 39336 #endif
       
 39337 
       
 39338 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
       
 39339   /*
       
 39340   ** If this Btree is a candidate for shared cache, try to find an
       
 39341   ** existing BtShared object that we can share with
       
 39342   */
       
 39343   if( isMemdb==0 && zFilename && zFilename[0] ){
       
 39344     if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
       
 39345       int nFullPathname = pVfs->mxPathname+1;
       
 39346       char *zFullPathname = sqlite3Malloc(nFullPathname);
       
 39347       sqlite3_mutex *mutexShared;
       
 39348       p->sharable = 1;
       
 39349       if( !zFullPathname ){
       
 39350         sqlite3_free(p);
       
 39351         return SQLITE_NOMEM;
       
 39352       }
       
 39353       sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
       
 39354       mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
       
 39355       sqlite3_mutex_enter(mutexOpen);
       
 39356       mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
       
 39357       sqlite3_mutex_enter(mutexShared);
       
 39358       for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
       
 39359         assert( pBt->nRef>0 );
       
 39360         if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
       
 39361                  && sqlite3PagerVfs(pBt->pPager)==pVfs ){
       
 39362           int iDb;
       
 39363           for(iDb=db->nDb-1; iDb>=0; iDb--){
       
 39364             Btree *pExisting = db->aDb[iDb].pBt;
       
 39365             if( pExisting && pExisting->pBt==pBt ){
       
 39366               sqlite3_mutex_leave(mutexShared);
       
 39367               sqlite3_mutex_leave(mutexOpen);
       
 39368               sqlite3_free(zFullPathname);
       
 39369               sqlite3_free(p);
       
 39370               return SQLITE_CONSTRAINT;
       
 39371             }
       
 39372           }
       
 39373           p->pBt = pBt;
       
 39374           pBt->nRef++;
       
 39375           break;
       
 39376         }
       
 39377       }
       
 39378       sqlite3_mutex_leave(mutexShared);
       
 39379       sqlite3_free(zFullPathname);
       
 39380     }
       
 39381 #ifdef SQLITE_DEBUG
       
 39382     else{
       
 39383       /* In debug mode, we mark all persistent databases as sharable
       
 39384       ** even when they are not.  This exercises the locking code and
       
 39385       ** gives more opportunity for asserts(sqlite3_mutex_held())
       
 39386       ** statements to find locking problems.
       
 39387       */
       
 39388       p->sharable = 1;
       
 39389     }
       
 39390 #endif
       
 39391   }
       
 39392 #endif
       
 39393   if( pBt==0 ){
       
 39394     /*
       
 39395     ** The following asserts make sure that structures used by the btree are
       
 39396     ** the right size.  This is to guard against size changes that result
       
 39397     ** when compiling on a different architecture.
       
 39398     */
       
 39399     assert( sizeof(i64)==8 || sizeof(i64)==4 );
       
 39400     assert( sizeof(u64)==8 || sizeof(u64)==4 );
       
 39401     assert( sizeof(u32)==4 );
       
 39402     assert( sizeof(u16)==2 );
       
 39403     assert( sizeof(Pgno)==4 );
       
 39404   
       
 39405     pBt = sqlite3MallocZero( sizeof(*pBt) );
       
 39406     if( pBt==0 ){
       
 39407       rc = SQLITE_NOMEM;
       
 39408       goto btree_open_out;
       
 39409     }
       
 39410     rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
       
 39411                           EXTRA_SIZE, flags, vfsFlags, pageReinit);
       
 39412     if( rc==SQLITE_OK ){
       
 39413       rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
       
 39414     }
       
 39415     if( rc!=SQLITE_OK ){
       
 39416       goto btree_open_out;
       
 39417     }
       
 39418     pBt->db = db;
       
 39419     sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
       
 39420     p->pBt = pBt;
       
 39421   
       
 39422     pBt->pCursor = 0;
       
 39423     pBt->pPage1 = 0;
       
 39424     pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
       
 39425     pBt->pageSize = get2byte(&zDbHeader[16]);
       
 39426     if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
       
 39427          || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
       
 39428       pBt->pageSize = 0;
       
 39429 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 39430       /* If the magic name ":memory:" will create an in-memory database, then
       
 39431       ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
       
 39432       ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
       
 39433       ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
       
 39434       ** regular file-name. In this case the auto-vacuum applies as per normal.
       
 39435       */
       
 39436       if( zFilename && !isMemdb ){
       
 39437         pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
       
 39438         pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
       
 39439       }
       
 39440 #endif
       
 39441       nReserve = 0;
       
 39442     }else{
       
 39443       nReserve = zDbHeader[20];
       
 39444       pBt->pageSizeFixed = 1;
       
 39445 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 39446       pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
       
 39447       pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
       
 39448 #endif
       
 39449     }
       
 39450     rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
       
 39451     if( rc ) goto btree_open_out;
       
 39452     pBt->usableSize = pBt->pageSize - nReserve;
       
 39453     assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
       
 39454    
       
 39455 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
       
 39456     /* Add the new BtShared object to the linked list sharable BtShareds.
       
 39457     */
       
 39458     if( p->sharable ){
       
 39459       sqlite3_mutex *mutexShared;
       
 39460       pBt->nRef = 1;
       
 39461       mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
       
 39462       if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
       
 39463         pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
       
 39464         if( pBt->mutex==0 ){
       
 39465           rc = SQLITE_NOMEM;
       
 39466           db->mallocFailed = 0;
       
 39467           goto btree_open_out;
       
 39468         }
       
 39469       }
       
 39470       sqlite3_mutex_enter(mutexShared);
       
 39471       pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
       
 39472       GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
       
 39473       sqlite3_mutex_leave(mutexShared);
       
 39474     }
       
 39475 #endif
       
 39476   }
       
 39477 
       
 39478 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
       
 39479   /* If the new Btree uses a sharable pBtShared, then link the new
       
 39480   ** Btree into the list of all sharable Btrees for the same connection.
       
 39481   ** The list is kept in ascending order by pBt address.
       
 39482   */
       
 39483   if( p->sharable ){
       
 39484     int i;
       
 39485     Btree *pSib;
       
 39486     for(i=0; i<db->nDb; i++){
       
 39487       if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
       
 39488         while( pSib->pPrev ){ pSib = pSib->pPrev; }
       
 39489         if( p->pBt<pSib->pBt ){
       
 39490           p->pNext = pSib;
       
 39491           p->pPrev = 0;
       
 39492           pSib->pPrev = p;
       
 39493         }else{
       
 39494           while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
       
 39495             pSib = pSib->pNext;
       
 39496           }
       
 39497           p->pNext = pSib->pNext;
       
 39498           p->pPrev = pSib;
       
 39499           if( p->pNext ){
       
 39500             p->pNext->pPrev = p;
       
 39501           }
       
 39502           pSib->pNext = p;
       
 39503         }
       
 39504         break;
       
 39505       }
       
 39506     }
       
 39507   }
       
 39508 #endif
       
 39509   *ppBtree = p;
       
 39510 
       
 39511 btree_open_out:
       
 39512   if( rc!=SQLITE_OK ){
       
 39513     if( pBt && pBt->pPager ){
       
 39514       sqlite3PagerClose(pBt->pPager);
       
 39515     }
       
 39516     sqlite3_free(pBt);
       
 39517     sqlite3_free(p);
       
 39518     *ppBtree = 0;
       
 39519   }
       
 39520   if( mutexOpen ){
       
 39521     assert( sqlite3_mutex_held(mutexOpen) );
       
 39522     sqlite3_mutex_leave(mutexOpen);
       
 39523   }
       
 39524   return rc;
       
 39525 }
       
 39526 
       
 39527 /*
       
 39528 ** Decrement the BtShared.nRef counter.  When it reaches zero,
       
 39529 ** remove the BtShared structure from the sharing list.  Return
       
 39530 ** true if the BtShared.nRef counter reaches zero and return
       
 39531 ** false if it is still positive.
       
 39532 */
       
 39533 static int removeFromSharingList(BtShared *pBt){
       
 39534 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 39535   sqlite3_mutex *pMaster;
       
 39536   BtShared *pList;
       
 39537   int removed = 0;
       
 39538 
       
 39539   assert( sqlite3_mutex_notheld(pBt->mutex) );
       
 39540   pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
       
 39541   sqlite3_mutex_enter(pMaster);
       
 39542   pBt->nRef--;
       
 39543   if( pBt->nRef<=0 ){
       
 39544     if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
       
 39545       GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
       
 39546     }else{
       
 39547       pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
       
 39548       while( ALWAYS(pList) && pList->pNext!=pBt ){
       
 39549         pList=pList->pNext;
       
 39550       }
       
 39551       if( ALWAYS(pList) ){
       
 39552         pList->pNext = pBt->pNext;
       
 39553       }
       
 39554     }
       
 39555     if( SQLITE_THREADSAFE ){
       
 39556       sqlite3_mutex_free(pBt->mutex);
       
 39557     }
       
 39558     removed = 1;
       
 39559   }
       
 39560   sqlite3_mutex_leave(pMaster);
       
 39561   return removed;
       
 39562 #else
       
 39563   return 1;
       
 39564 #endif
       
 39565 }
       
 39566 
       
 39567 /*
       
 39568 ** Make sure pBt->pTmpSpace points to an allocation of 
       
 39569 ** MX_CELL_SIZE(pBt) bytes.
       
 39570 */
       
 39571 static void allocateTempSpace(BtShared *pBt){
       
 39572   if( !pBt->pTmpSpace ){
       
 39573     pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
       
 39574   }
       
 39575 }
       
 39576 
       
 39577 /*
       
 39578 ** Free the pBt->pTmpSpace allocation
       
 39579 */
       
 39580 static void freeTempSpace(BtShared *pBt){
       
 39581   sqlite3PageFree( pBt->pTmpSpace);
       
 39582   pBt->pTmpSpace = 0;
       
 39583 }
       
 39584 
       
 39585 /*
       
 39586 ** Close an open database and invalidate all cursors.
       
 39587 */
       
 39588 SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
       
 39589   BtShared *pBt = p->pBt;
       
 39590   BtCursor *pCur;
       
 39591 
       
 39592   /* Close all cursors opened via this handle.  */
       
 39593   assert( sqlite3_mutex_held(p->db->mutex) );
       
 39594   sqlite3BtreeEnter(p);
       
 39595   pCur = pBt->pCursor;
       
 39596   while( pCur ){
       
 39597     BtCursor *pTmp = pCur;
       
 39598     pCur = pCur->pNext;
       
 39599     if( pTmp->pBtree==p ){
       
 39600       sqlite3BtreeCloseCursor(pTmp);
       
 39601     }
       
 39602   }
       
 39603 
       
 39604   /* Rollback any active transaction and free the handle structure.
       
 39605   ** The call to sqlite3BtreeRollback() drops any table-locks held by
       
 39606   ** this handle.
       
 39607   */
       
 39608   sqlite3BtreeRollback(p);
       
 39609   sqlite3BtreeLeave(p);
       
 39610 
       
 39611   /* If there are still other outstanding references to the shared-btree
       
 39612   ** structure, return now. The remainder of this procedure cleans 
       
 39613   ** up the shared-btree.
       
 39614   */
       
 39615   assert( p->wantToLock==0 && p->locked==0 );
       
 39616   if( !p->sharable || removeFromSharingList(pBt) ){
       
 39617     /* The pBt is no longer on the sharing list, so we can access
       
 39618     ** it without having to hold the mutex.
       
 39619     **
       
 39620     ** Clean out and delete the BtShared object.
       
 39621     */
       
 39622     assert( !pBt->pCursor );
       
 39623     sqlite3PagerClose(pBt->pPager);
       
 39624     if( pBt->xFreeSchema && pBt->pSchema ){
       
 39625       pBt->xFreeSchema(pBt->pSchema);
       
 39626     }
       
 39627     sqlite3_free(pBt->pSchema);
       
 39628     freeTempSpace(pBt);
       
 39629     sqlite3_free(pBt);
       
 39630   }
       
 39631 
       
 39632 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 39633   assert( p->wantToLock==0 );
       
 39634   assert( p->locked==0 );
       
 39635   if( p->pPrev ) p->pPrev->pNext = p->pNext;
       
 39636   if( p->pNext ) p->pNext->pPrev = p->pPrev;
       
 39637 #endif
       
 39638 
       
 39639   sqlite3_free(p);
       
 39640   return SQLITE_OK;
       
 39641 }
       
 39642 
       
 39643 /*
       
 39644 ** Change the limit on the number of pages allowed in the cache.
       
 39645 **
       
 39646 ** The maximum number of cache pages is set to the absolute
       
 39647 ** value of mxPage.  If mxPage is negative, the pager will
       
 39648 ** operate asynchronously - it will not stop to do fsync()s
       
 39649 ** to insure data is written to the disk surface before
       
 39650 ** continuing.  Transactions still work if synchronous is off,
       
 39651 ** and the database cannot be corrupted if this program
       
 39652 ** crashes.  But if the operating system crashes or there is
       
 39653 ** an abrupt power failure when synchronous is off, the database
       
 39654 ** could be left in an inconsistent and unrecoverable state.
       
 39655 ** Synchronous is on by default so database corruption is not
       
 39656 ** normally a worry.
       
 39657 */
       
 39658 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
       
 39659   BtShared *pBt = p->pBt;
       
 39660   assert( sqlite3_mutex_held(p->db->mutex) );
       
 39661   sqlite3BtreeEnter(p);
       
 39662   sqlite3PagerSetCachesize(pBt->pPager, mxPage);
       
 39663   sqlite3BtreeLeave(p);
       
 39664   return SQLITE_OK;
       
 39665 }
       
 39666 
       
 39667 /*
       
 39668 ** Change the way data is synced to disk in order to increase or decrease
       
 39669 ** how well the database resists damage due to OS crashes and power
       
 39670 ** failures.  Level 1 is the same as asynchronous (no syncs() occur and
       
 39671 ** there is a high probability of damage)  Level 2 is the default.  There
       
 39672 ** is a very low but non-zero probability of damage.  Level 3 reduces the
       
 39673 ** probability of damage to near zero but with a write performance reduction.
       
 39674 */
       
 39675 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
       
 39676 SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){
       
 39677   BtShared *pBt = p->pBt;
       
 39678   assert( sqlite3_mutex_held(p->db->mutex) );
       
 39679   sqlite3BtreeEnter(p);
       
 39680   sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync);
       
 39681   sqlite3BtreeLeave(p);
       
 39682   return SQLITE_OK;
       
 39683 }
       
 39684 #endif
       
 39685 
       
 39686 /*
       
 39687 ** Return TRUE if the given btree is set to safety level 1.  In other
       
 39688 ** words, return TRUE if no sync() occurs on the disk files.
       
 39689 */
       
 39690 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
       
 39691   BtShared *pBt = p->pBt;
       
 39692   int rc;
       
 39693   assert( sqlite3_mutex_held(p->db->mutex) );  
       
 39694   sqlite3BtreeEnter(p);
       
 39695   assert( pBt && pBt->pPager );
       
 39696   rc = sqlite3PagerNosync(pBt->pPager);
       
 39697   sqlite3BtreeLeave(p);
       
 39698   return rc;
       
 39699 }
       
 39700 
       
 39701 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
       
 39702 /*
       
 39703 ** Change the default pages size and the number of reserved bytes per page.
       
 39704 ** Or, if the page size has already been fixed, return SQLITE_READONLY 
       
 39705 ** without changing anything.
       
 39706 **
       
 39707 ** The page size must be a power of 2 between 512 and 65536.  If the page
       
 39708 ** size supplied does not meet this constraint then the page size is not
       
 39709 ** changed.
       
 39710 **
       
 39711 ** Page sizes are constrained to be a power of two so that the region
       
 39712 ** of the database file used for locking (beginning at PENDING_BYTE,
       
 39713 ** the first byte past the 1GB boundary, 0x40000000) needs to occur
       
 39714 ** at the beginning of a page.
       
 39715 **
       
 39716 ** If parameter nReserve is less than zero, then the number of reserved
       
 39717 ** bytes per page is left unchanged.
       
 39718 **
       
 39719 ** If the iFix!=0 then the pageSizeFixed flag is set so that the page size
       
 39720 ** and autovacuum mode can no longer be changed.
       
 39721 */
       
 39722 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
       
 39723   int rc = SQLITE_OK;
       
 39724   BtShared *pBt = p->pBt;
       
 39725   assert( nReserve>=-1 && nReserve<=255 );
       
 39726   sqlite3BtreeEnter(p);
       
 39727   if( pBt->pageSizeFixed ){
       
 39728     sqlite3BtreeLeave(p);
       
 39729     return SQLITE_READONLY;
       
 39730   }
       
 39731   if( nReserve<0 ){
       
 39732     nReserve = pBt->pageSize - pBt->usableSize;
       
 39733   }
       
 39734   assert( nReserve>=0 && nReserve<=255 );
       
 39735   if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
       
 39736         ((pageSize-1)&pageSize)==0 ){
       
 39737     assert( (pageSize & 7)==0 );
       
 39738     assert( !pBt->pPage1 && !pBt->pCursor );
       
 39739     pBt->pageSize = (u16)pageSize;
       
 39740     freeTempSpace(pBt);
       
 39741   }
       
 39742   rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
       
 39743   pBt->usableSize = pBt->pageSize - (u16)nReserve;
       
 39744   if( iFix ) pBt->pageSizeFixed = 1;
       
 39745   sqlite3BtreeLeave(p);
       
 39746   return rc;
       
 39747 }
       
 39748 
       
 39749 /*
       
 39750 ** Return the currently defined page size
       
 39751 */
       
 39752 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
       
 39753   return p->pBt->pageSize;
       
 39754 }
       
 39755 
       
 39756 /*
       
 39757 ** Return the number of bytes of space at the end of every page that
       
 39758 ** are intentually left unused.  This is the "reserved" space that is
       
 39759 ** sometimes used by extensions.
       
 39760 */
       
 39761 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree *p){
       
 39762   int n;
       
 39763   sqlite3BtreeEnter(p);
       
 39764   n = p->pBt->pageSize - p->pBt->usableSize;
       
 39765   sqlite3BtreeLeave(p);
       
 39766   return n;
       
 39767 }
       
 39768 
       
 39769 /*
       
 39770 ** Set the maximum page count for a database if mxPage is positive.
       
 39771 ** No changes are made if mxPage is 0 or negative.
       
 39772 ** Regardless of the value of mxPage, return the maximum page count.
       
 39773 */
       
 39774 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
       
 39775   int n;
       
 39776   sqlite3BtreeEnter(p);
       
 39777   n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
       
 39778   sqlite3BtreeLeave(p);
       
 39779   return n;
       
 39780 }
       
 39781 #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
       
 39782 
       
 39783 /*
       
 39784 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
       
 39785 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
       
 39786 ** is disabled. The default value for the auto-vacuum property is 
       
 39787 ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
       
 39788 */
       
 39789 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
       
 39790 #ifdef SQLITE_OMIT_AUTOVACUUM
       
 39791   return SQLITE_READONLY;
       
 39792 #else
       
 39793   BtShared *pBt = p->pBt;
       
 39794   int rc = SQLITE_OK;
       
 39795   u8 av = (u8)autoVacuum;
       
 39796 
       
 39797   sqlite3BtreeEnter(p);
       
 39798   if( pBt->pageSizeFixed && (av ?1:0)!=pBt->autoVacuum ){
       
 39799     rc = SQLITE_READONLY;
       
 39800   }else{
       
 39801     pBt->autoVacuum = av ?1:0;
       
 39802     pBt->incrVacuum = av==2 ?1:0;
       
 39803   }
       
 39804   sqlite3BtreeLeave(p);
       
 39805   return rc;
       
 39806 #endif
       
 39807 }
       
 39808 
       
 39809 /*
       
 39810 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is 
       
 39811 ** enabled 1 is returned. Otherwise 0.
       
 39812 */
       
 39813 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
       
 39814 #ifdef SQLITE_OMIT_AUTOVACUUM
       
 39815   return BTREE_AUTOVACUUM_NONE;
       
 39816 #else
       
 39817   int rc;
       
 39818   sqlite3BtreeEnter(p);
       
 39819   rc = (
       
 39820     (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
       
 39821     (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
       
 39822     BTREE_AUTOVACUUM_INCR
       
 39823   );
       
 39824   sqlite3BtreeLeave(p);
       
 39825   return rc;
       
 39826 #endif
       
 39827 }
       
 39828 
       
 39829 
       
 39830 /*
       
 39831 ** Get a reference to pPage1 of the database file.  This will
       
 39832 ** also acquire a readlock on that file.
       
 39833 **
       
 39834 ** SQLITE_OK is returned on success.  If the file is not a
       
 39835 ** well-formed database file, then SQLITE_CORRUPT is returned.
       
 39836 ** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
       
 39837 ** is returned if we run out of memory. 
       
 39838 */
       
 39839 static int lockBtree(BtShared *pBt){
       
 39840   int rc;
       
 39841   MemPage *pPage1;
       
 39842   int nPage;
       
 39843 
       
 39844   assert( sqlite3_mutex_held(pBt->mutex) );
       
 39845   assert( pBt->pPage1==0 );
       
 39846   rc = sqlite3PagerSharedLock(pBt->pPager);
       
 39847   if( rc!=SQLITE_OK ) return rc;
       
 39848   rc = btreeGetPage(pBt, 1, &pPage1, 0);
       
 39849   if( rc!=SQLITE_OK ) return rc;
       
 39850 
       
 39851   /* Do some checking to help insure the file we opened really is
       
 39852   ** a valid database file. 
       
 39853   */
       
 39854   rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
       
 39855   if( rc!=SQLITE_OK ){
       
 39856     goto page1_init_failed;
       
 39857   }else if( nPage>0 ){
       
 39858     int pageSize;
       
 39859     int usableSize;
       
 39860     u8 *page1 = pPage1->aData;
       
 39861     rc = SQLITE_NOTADB;
       
 39862     if( memcmp(page1, zMagicHeader, 16)!=0 ){
       
 39863       goto page1_init_failed;
       
 39864     }
       
 39865     if( page1[18]>1 ){
       
 39866       pBt->readOnly = 1;
       
 39867     }
       
 39868     if( page1[19]>1 ){
       
 39869       goto page1_init_failed;
       
 39870     }
       
 39871 
       
 39872     /* The maximum embedded fraction must be exactly 25%.  And the minimum
       
 39873     ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
       
 39874     ** The original design allowed these amounts to vary, but as of
       
 39875     ** version 3.6.0, we require them to be fixed.
       
 39876     */
       
 39877     if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
       
 39878       goto page1_init_failed;
       
 39879     }
       
 39880     pageSize = get2byte(&page1[16]);
       
 39881     if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ||
       
 39882         (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE)
       
 39883     ){
       
 39884       goto page1_init_failed;
       
 39885     }
       
 39886     assert( (pageSize & 7)==0 );
       
 39887     usableSize = pageSize - page1[20];
       
 39888     if( pageSize!=pBt->pageSize ){
       
 39889       /* After reading the first page of the database assuming a page size
       
 39890       ** of BtShared.pageSize, we have discovered that the page-size is
       
 39891       ** actually pageSize. Unlock the database, leave pBt->pPage1 at
       
 39892       ** zero and return SQLITE_OK. The caller will call this function
       
 39893       ** again with the correct page-size.
       
 39894       */
       
 39895       releasePage(pPage1);
       
 39896       pBt->usableSize = (u16)usableSize;
       
 39897       pBt->pageSize = (u16)pageSize;
       
 39898       freeTempSpace(pBt);
       
 39899       rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
       
 39900                                    pageSize-usableSize);
       
 39901       return rc;
       
 39902     }
       
 39903     if( usableSize<480 ){
       
 39904       goto page1_init_failed;
       
 39905     }
       
 39906     pBt->pageSize = (u16)pageSize;
       
 39907     pBt->usableSize = (u16)usableSize;
       
 39908 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 39909     pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
       
 39910     pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
       
 39911 #endif
       
 39912   }
       
 39913 
       
 39914   /* maxLocal is the maximum amount of payload to store locally for
       
 39915   ** a cell.  Make sure it is small enough so that at least minFanout
       
 39916   ** cells can will fit on one page.  We assume a 10-byte page header.
       
 39917   ** Besides the payload, the cell must store:
       
 39918   **     2-byte pointer to the cell
       
 39919   **     4-byte child pointer
       
 39920   **     9-byte nKey value
       
 39921   **     4-byte nData value
       
 39922   **     4-byte overflow page pointer
       
 39923   ** So a cell consists of a 2-byte poiner, a header which is as much as
       
 39924   ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
       
 39925   ** page pointer.
       
 39926   */
       
 39927   pBt->maxLocal = (pBt->usableSize-12)*64/255 - 23;
       
 39928   pBt->minLocal = (pBt->usableSize-12)*32/255 - 23;
       
 39929   pBt->maxLeaf = pBt->usableSize - 35;
       
 39930   pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23;
       
 39931   assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
       
 39932   pBt->pPage1 = pPage1;
       
 39933   return SQLITE_OK;
       
 39934 
       
 39935 page1_init_failed:
       
 39936   releasePage(pPage1);
       
 39937   pBt->pPage1 = 0;
       
 39938   return rc;
       
 39939 }
       
 39940 
       
 39941 /*
       
 39942 ** If there are no outstanding cursors and we are not in the middle
       
 39943 ** of a transaction but there is a read lock on the database, then
       
 39944 ** this routine unrefs the first page of the database file which 
       
 39945 ** has the effect of releasing the read lock.
       
 39946 **
       
 39947 ** If there is a transaction in progress, this routine is a no-op.
       
 39948 */
       
 39949 static void unlockBtreeIfUnused(BtShared *pBt){
       
 39950   assert( sqlite3_mutex_held(pBt->mutex) );
       
 39951   assert( pBt->pCursor==0 || pBt->inTransaction>TRANS_NONE );
       
 39952   if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
       
 39953     assert( pBt->pPage1->aData );
       
 39954     assert( sqlite3PagerRefcount(pBt->pPager)==1 );
       
 39955     assert( pBt->pPage1->aData );
       
 39956     releasePage(pBt->pPage1);
       
 39957     pBt->pPage1 = 0;
       
 39958   }
       
 39959 }
       
 39960 
       
 39961 /*
       
 39962 ** If pBt points to an empty file then convert that empty file
       
 39963 ** into a new empty database by initializing the first page of
       
 39964 ** the database.
       
 39965 */
       
 39966 static int newDatabase(BtShared *pBt){
       
 39967   MemPage *pP1;
       
 39968   unsigned char *data;
       
 39969   int rc;
       
 39970   int nPage;
       
 39971 
       
 39972   assert( sqlite3_mutex_held(pBt->mutex) );
       
 39973   /* The database size has already been measured and cached, so failure
       
 39974   ** is impossible here.  If the original size measurement failed, then
       
 39975   ** processing aborts before entering this routine. */
       
 39976   rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
       
 39977   if( NEVER(rc!=SQLITE_OK) || nPage>0 ){
       
 39978     return rc;
       
 39979   }
       
 39980   pP1 = pBt->pPage1;
       
 39981   assert( pP1!=0 );
       
 39982   data = pP1->aData;
       
 39983   rc = sqlite3PagerWrite(pP1->pDbPage);
       
 39984   if( rc ) return rc;
       
 39985   memcpy(data, zMagicHeader, sizeof(zMagicHeader));
       
 39986   assert( sizeof(zMagicHeader)==16 );
       
 39987   put2byte(&data[16], pBt->pageSize);
       
 39988   data[18] = 1;
       
 39989   data[19] = 1;
       
 39990   assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
       
 39991   data[20] = (u8)(pBt->pageSize - pBt->usableSize);
       
 39992   data[21] = 64;
       
 39993   data[22] = 32;
       
 39994   data[23] = 32;
       
 39995   memset(&data[24], 0, 100-24);
       
 39996   zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
       
 39997   pBt->pageSizeFixed = 1;
       
 39998 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 39999   assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
       
 40000   assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
       
 40001   put4byte(&data[36 + 4*4], pBt->autoVacuum);
       
 40002   put4byte(&data[36 + 7*4], pBt->incrVacuum);
       
 40003 #endif
       
 40004   return SQLITE_OK;
       
 40005 }
       
 40006 
       
 40007 /*
       
 40008 ** Attempt to start a new transaction. A write-transaction
       
 40009 ** is started if the second argument is nonzero, otherwise a read-
       
 40010 ** transaction.  If the second argument is 2 or more and exclusive
       
 40011 ** transaction is started, meaning that no other process is allowed
       
 40012 ** to access the database.  A preexisting transaction may not be
       
 40013 ** upgraded to exclusive by calling this routine a second time - the
       
 40014 ** exclusivity flag only works for a new transaction.
       
 40015 **
       
 40016 ** A write-transaction must be started before attempting any 
       
 40017 ** changes to the database.  None of the following routines 
       
 40018 ** will work unless a transaction is started first:
       
 40019 **
       
 40020 **      sqlite3BtreeCreateTable()
       
 40021 **      sqlite3BtreeCreateIndex()
       
 40022 **      sqlite3BtreeClearTable()
       
 40023 **      sqlite3BtreeDropTable()
       
 40024 **      sqlite3BtreeInsert()
       
 40025 **      sqlite3BtreeDelete()
       
 40026 **      sqlite3BtreeUpdateMeta()
       
 40027 **
       
 40028 ** If an initial attempt to acquire the lock fails because of lock contention
       
 40029 ** and the database was previously unlocked, then invoke the busy handler
       
 40030 ** if there is one.  But if there was previously a read-lock, do not
       
 40031 ** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is 
       
 40032 ** returned when there is already a read-lock in order to avoid a deadlock.
       
 40033 **
       
 40034 ** Suppose there are two processes A and B.  A has a read lock and B has
       
 40035 ** a reserved lock.  B tries to promote to exclusive but is blocked because
       
 40036 ** of A's read lock.  A tries to promote to reserved but is blocked by B.
       
 40037 ** One or the other of the two processes must give way or there can be
       
 40038 ** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
       
 40039 ** when A already has a read lock, we encourage A to give up and let B
       
 40040 ** proceed.
       
 40041 */
       
 40042 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
       
 40043   sqlite3 *pBlock = 0;
       
 40044   BtShared *pBt = p->pBt;
       
 40045   int rc = SQLITE_OK;
       
 40046 
       
 40047   sqlite3BtreeEnter(p);
       
 40048   btreeIntegrity(p);
       
 40049 
       
 40050   /* If the btree is already in a write-transaction, or it
       
 40051   ** is already in a read-transaction and a read-transaction
       
 40052   ** is requested, this is a no-op.
       
 40053   */
       
 40054   if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
       
 40055     goto trans_begun;
       
 40056   }
       
 40057 
       
 40058   /* Write transactions are not possible on a read-only database */
       
 40059   if( pBt->readOnly && wrflag ){
       
 40060     rc = SQLITE_READONLY;
       
 40061     goto trans_begun;
       
 40062   }
       
 40063 
       
 40064 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 40065   /* If another database handle has already opened a write transaction 
       
 40066   ** on this shared-btree structure and a second write transaction is
       
 40067   ** requested, return SQLITE_LOCKED.
       
 40068   */
       
 40069   if( (wrflag && pBt->inTransaction==TRANS_WRITE) || pBt->isPending ){
       
 40070     pBlock = pBt->pWriter->db;
       
 40071   }else if( wrflag>1 ){
       
 40072     BtLock *pIter;
       
 40073     for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
       
 40074       if( pIter->pBtree!=p ){
       
 40075         pBlock = pIter->pBtree->db;
       
 40076         break;
       
 40077       }
       
 40078     }
       
 40079   }
       
 40080   if( pBlock ){
       
 40081     sqlite3ConnectionBlocked(p->db, pBlock);
       
 40082     rc = SQLITE_LOCKED_SHAREDCACHE;
       
 40083     goto trans_begun;
       
 40084   }
       
 40085 #endif
       
 40086 
       
 40087   /* Any read-only or read-write transaction implies a read-lock on 
       
 40088   ** page 1. So if some other shared-cache client already has a write-lock 
       
 40089   ** on page 1, the transaction cannot be opened. */
       
 40090   rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
       
 40091   if( SQLITE_OK!=rc ) goto trans_begun;
       
 40092 
       
 40093   do {
       
 40094     /* Call lockBtree() until either pBt->pPage1 is populated or
       
 40095     ** lockBtree() returns something other than SQLITE_OK. lockBtree()
       
 40096     ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
       
 40097     ** reading page 1 it discovers that the page-size of the database 
       
 40098     ** file is not pBt->pageSize. In this case lockBtree() will update
       
 40099     ** pBt->pageSize to the page-size of the file on disk.
       
 40100     */
       
 40101     while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
       
 40102 
       
 40103     if( rc==SQLITE_OK && wrflag ){
       
 40104       if( pBt->readOnly ){
       
 40105         rc = SQLITE_READONLY;
       
 40106       }else{
       
 40107         rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
       
 40108         if( rc==SQLITE_OK ){
       
 40109           rc = newDatabase(pBt);
       
 40110         }
       
 40111       }
       
 40112     }
       
 40113   
       
 40114     if( rc!=SQLITE_OK ){
       
 40115       unlockBtreeIfUnused(pBt);
       
 40116     }
       
 40117   }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
       
 40118           btreeInvokeBusyHandler(pBt) );
       
 40119 
       
 40120   if( rc==SQLITE_OK ){
       
 40121     if( p->inTrans==TRANS_NONE ){
       
 40122       pBt->nTransaction++;
       
 40123 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 40124       if( p->sharable ){
       
 40125 	assert( p->lock.pBtree==p && p->lock.iTable==1 );
       
 40126         p->lock.eLock = READ_LOCK;
       
 40127         p->lock.pNext = pBt->pLock;
       
 40128         pBt->pLock = &p->lock;
       
 40129       }
       
 40130 #endif
       
 40131     }
       
 40132     p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
       
 40133     if( p->inTrans>pBt->inTransaction ){
       
 40134       pBt->inTransaction = p->inTrans;
       
 40135     }
       
 40136 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 40137     if( wrflag ){
       
 40138       assert( !pBt->pWriter );
       
 40139       pBt->pWriter = p;
       
 40140       pBt->isExclusive = (u8)(wrflag>1);
       
 40141     }
       
 40142 #endif
       
 40143   }
       
 40144 
       
 40145 
       
 40146 trans_begun:
       
 40147   if( rc==SQLITE_OK && wrflag ){
       
 40148     /* This call makes sure that the pager has the correct number of
       
 40149     ** open savepoints. If the second parameter is greater than 0 and
       
 40150     ** the sub-journal is not already open, then it will be opened here.
       
 40151     */
       
 40152     rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
       
 40153   }
       
 40154 
       
 40155   btreeIntegrity(p);
       
 40156   sqlite3BtreeLeave(p);
       
 40157   return rc;
       
 40158 }
       
 40159 
       
 40160 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 40161 
       
 40162 /*
       
 40163 ** Set the pointer-map entries for all children of page pPage. Also, if
       
 40164 ** pPage contains cells that point to overflow pages, set the pointer
       
 40165 ** map entries for the overflow pages as well.
       
 40166 */
       
 40167 static int setChildPtrmaps(MemPage *pPage){
       
 40168   int i;                             /* Counter variable */
       
 40169   int nCell;                         /* Number of cells in page pPage */
       
 40170   int rc;                            /* Return code */
       
 40171   BtShared *pBt = pPage->pBt;
       
 40172   u8 isInitOrig = pPage->isInit;
       
 40173   Pgno pgno = pPage->pgno;
       
 40174 
       
 40175   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 40176   rc = btreeInitPage(pPage);
       
 40177   if( rc!=SQLITE_OK ){
       
 40178     goto set_child_ptrmaps_out;
       
 40179   }
       
 40180   nCell = pPage->nCell;
       
 40181 
       
 40182   for(i=0; i<nCell; i++){
       
 40183     u8 *pCell = findCell(pPage, i);
       
 40184 
       
 40185     ptrmapPutOvflPtr(pPage, pCell, &rc);
       
 40186 
       
 40187     if( !pPage->leaf ){
       
 40188       Pgno childPgno = get4byte(pCell);
       
 40189       ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
       
 40190     }
       
 40191   }
       
 40192 
       
 40193   if( !pPage->leaf ){
       
 40194     Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
       
 40195     ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
       
 40196   }
       
 40197 
       
 40198 set_child_ptrmaps_out:
       
 40199   pPage->isInit = isInitOrig;
       
 40200   return rc;
       
 40201 }
       
 40202 
       
 40203 /*
       
 40204 ** Somewhere on pPage is a pointer to page iFrom.  Modify this pointer so
       
 40205 ** that it points to iTo. Parameter eType describes the type of pointer to
       
 40206 ** be modified, as  follows:
       
 40207 **
       
 40208 ** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child 
       
 40209 **                   page of pPage.
       
 40210 **
       
 40211 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
       
 40212 **                   page pointed to by one of the cells on pPage.
       
 40213 **
       
 40214 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
       
 40215 **                   overflow page in the list.
       
 40216 */
       
 40217 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
       
 40218   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 40219   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
 40220   if( eType==PTRMAP_OVERFLOW2 ){
       
 40221     /* The pointer is always the first 4 bytes of the page in this case.  */
       
 40222     if( get4byte(pPage->aData)!=iFrom ){
       
 40223       return SQLITE_CORRUPT_BKPT;
       
 40224     }
       
 40225     put4byte(pPage->aData, iTo);
       
 40226   }else{
       
 40227     u8 isInitOrig = pPage->isInit;
       
 40228     int i;
       
 40229     int nCell;
       
 40230 
       
 40231     btreeInitPage(pPage);
       
 40232     nCell = pPage->nCell;
       
 40233 
       
 40234     for(i=0; i<nCell; i++){
       
 40235       u8 *pCell = findCell(pPage, i);
       
 40236       if( eType==PTRMAP_OVERFLOW1 ){
       
 40237         CellInfo info;
       
 40238         btreeParseCellPtr(pPage, pCell, &info);
       
 40239         if( info.iOverflow ){
       
 40240           if( iFrom==get4byte(&pCell[info.iOverflow]) ){
       
 40241             put4byte(&pCell[info.iOverflow], iTo);
       
 40242             break;
       
 40243           }
       
 40244         }
       
 40245       }else{
       
 40246         if( get4byte(pCell)==iFrom ){
       
 40247           put4byte(pCell, iTo);
       
 40248           break;
       
 40249         }
       
 40250       }
       
 40251     }
       
 40252   
       
 40253     if( i==nCell ){
       
 40254       if( eType!=PTRMAP_BTREE || 
       
 40255           get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
       
 40256         return SQLITE_CORRUPT_BKPT;
       
 40257       }
       
 40258       put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
       
 40259     }
       
 40260 
       
 40261     pPage->isInit = isInitOrig;
       
 40262   }
       
 40263   return SQLITE_OK;
       
 40264 }
       
 40265 
       
 40266 
       
 40267 /*
       
 40268 ** Move the open database page pDbPage to location iFreePage in the 
       
 40269 ** database. The pDbPage reference remains valid.
       
 40270 **
       
 40271 ** The isCommit flag indicates that there is no need to remember that
       
 40272 ** the journal needs to be sync()ed before database page pDbPage->pgno 
       
 40273 ** can be written to. The caller has already promised not to write to that
       
 40274 ** page.
       
 40275 */
       
 40276 static int relocatePage(
       
 40277   BtShared *pBt,           /* Btree */
       
 40278   MemPage *pDbPage,        /* Open page to move */
       
 40279   u8 eType,                /* Pointer map 'type' entry for pDbPage */
       
 40280   Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
       
 40281   Pgno iFreePage,          /* The location to move pDbPage to */
       
 40282   int isCommit             /* isCommit flag passed to sqlite3PagerMovepage */
       
 40283 ){
       
 40284   MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
       
 40285   Pgno iDbPage = pDbPage->pgno;
       
 40286   Pager *pPager = pBt->pPager;
       
 40287   int rc;
       
 40288 
       
 40289   assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || 
       
 40290       eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
       
 40291   assert( sqlite3_mutex_held(pBt->mutex) );
       
 40292   assert( pDbPage->pBt==pBt );
       
 40293 
       
 40294   /* Move page iDbPage from its current location to page number iFreePage */
       
 40295   TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", 
       
 40296       iDbPage, iFreePage, iPtrPage, eType));
       
 40297   rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
       
 40298   if( rc!=SQLITE_OK ){
       
 40299     return rc;
       
 40300   }
       
 40301   pDbPage->pgno = iFreePage;
       
 40302 
       
 40303   /* If pDbPage was a btree-page, then it may have child pages and/or cells
       
 40304   ** that point to overflow pages. The pointer map entries for all these
       
 40305   ** pages need to be changed.
       
 40306   **
       
 40307   ** If pDbPage is an overflow page, then the first 4 bytes may store a
       
 40308   ** pointer to a subsequent overflow page. If this is the case, then
       
 40309   ** the pointer map needs to be updated for the subsequent overflow page.
       
 40310   */
       
 40311   if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
       
 40312     rc = setChildPtrmaps(pDbPage);
       
 40313     if( rc!=SQLITE_OK ){
       
 40314       return rc;
       
 40315     }
       
 40316   }else{
       
 40317     Pgno nextOvfl = get4byte(pDbPage->aData);
       
 40318     if( nextOvfl!=0 ){
       
 40319       ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
       
 40320       if( rc!=SQLITE_OK ){
       
 40321         return rc;
       
 40322       }
       
 40323     }
       
 40324   }
       
 40325 
       
 40326   /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
       
 40327   ** that it points at iFreePage. Also fix the pointer map entry for
       
 40328   ** iPtrPage.
       
 40329   */
       
 40330   if( eType!=PTRMAP_ROOTPAGE ){
       
 40331     rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
       
 40332     if( rc!=SQLITE_OK ){
       
 40333       return rc;
       
 40334     }
       
 40335     rc = sqlite3PagerWrite(pPtrPage->pDbPage);
       
 40336     if( rc!=SQLITE_OK ){
       
 40337       releasePage(pPtrPage);
       
 40338       return rc;
       
 40339     }
       
 40340     rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
       
 40341     releasePage(pPtrPage);
       
 40342     if( rc==SQLITE_OK ){
       
 40343       ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
       
 40344     }
       
 40345   }
       
 40346   return rc;
       
 40347 }
       
 40348 
       
 40349 /* Forward declaration required by incrVacuumStep(). */
       
 40350 static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
       
 40351 
       
 40352 /*
       
 40353 ** Perform a single step of an incremental-vacuum. If successful,
       
 40354 ** return SQLITE_OK. If there is no work to do (and therefore no
       
 40355 ** point in calling this function again), return SQLITE_DONE.
       
 40356 **
       
 40357 ** More specificly, this function attempts to re-organize the 
       
 40358 ** database so that the last page of the file currently in use
       
 40359 ** is no longer in use.
       
 40360 **
       
 40361 ** If the nFin parameter is non-zero, this function assumes
       
 40362 ** that the caller will keep calling incrVacuumStep() until
       
 40363 ** it returns SQLITE_DONE or an error, and that nFin is the
       
 40364 ** number of pages the database file will contain after this 
       
 40365 ** process is complete.  If nFin is zero, it is assumed that
       
 40366 ** incrVacuumStep() will be called a finite amount of times
       
 40367 ** which may or may not empty the freelist.  A full autovacuum
       
 40368 ** has nFin>0.  A "PRAGMA incremental_vacuum" has nFin==0.
       
 40369 */
       
 40370 static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
       
 40371   Pgno nFreeList;           /* Number of pages still on the free-list */
       
 40372 
       
 40373   assert( sqlite3_mutex_held(pBt->mutex) );
       
 40374   assert( iLastPg>nFin );
       
 40375 
       
 40376   if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
       
 40377     int rc;
       
 40378     u8 eType;
       
 40379     Pgno iPtrPage;
       
 40380 
       
 40381     nFreeList = get4byte(&pBt->pPage1->aData[36]);
       
 40382     if( nFreeList==0 ){
       
 40383       return SQLITE_DONE;
       
 40384     }
       
 40385 
       
 40386     rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
       
 40387     if( rc!=SQLITE_OK ){
       
 40388       return rc;
       
 40389     }
       
 40390     if( eType==PTRMAP_ROOTPAGE ){
       
 40391       return SQLITE_CORRUPT_BKPT;
       
 40392     }
       
 40393 
       
 40394     if( eType==PTRMAP_FREEPAGE ){
       
 40395       if( nFin==0 ){
       
 40396         /* Remove the page from the files free-list. This is not required
       
 40397         ** if nFin is non-zero. In that case, the free-list will be
       
 40398         ** truncated to zero after this function returns, so it doesn't 
       
 40399         ** matter if it still contains some garbage entries.
       
 40400         */
       
 40401         Pgno iFreePg;
       
 40402         MemPage *pFreePg;
       
 40403         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
       
 40404         if( rc!=SQLITE_OK ){
       
 40405           return rc;
       
 40406         }
       
 40407         assert( iFreePg==iLastPg );
       
 40408         releasePage(pFreePg);
       
 40409       }
       
 40410     } else {
       
 40411       Pgno iFreePg;             /* Index of free page to move pLastPg to */
       
 40412       MemPage *pLastPg;
       
 40413 
       
 40414       rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
       
 40415       if( rc!=SQLITE_OK ){
       
 40416         return rc;
       
 40417       }
       
 40418 
       
 40419       /* If nFin is zero, this loop runs exactly once and page pLastPg
       
 40420       ** is swapped with the first free page pulled off the free list.
       
 40421       **
       
 40422       ** On the other hand, if nFin is greater than zero, then keep
       
 40423       ** looping until a free-page located within the first nFin pages
       
 40424       ** of the file is found.
       
 40425       */
       
 40426       do {
       
 40427         MemPage *pFreePg;
       
 40428         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
       
 40429         if( rc!=SQLITE_OK ){
       
 40430           releasePage(pLastPg);
       
 40431           return rc;
       
 40432         }
       
 40433         releasePage(pFreePg);
       
 40434       }while( nFin!=0 && iFreePg>nFin );
       
 40435       assert( iFreePg<iLastPg );
       
 40436       
       
 40437       rc = sqlite3PagerWrite(pLastPg->pDbPage);
       
 40438       if( rc==SQLITE_OK ){
       
 40439         rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0);
       
 40440       }
       
 40441       releasePage(pLastPg);
       
 40442       if( rc!=SQLITE_OK ){
       
 40443         return rc;
       
 40444       }
       
 40445     }
       
 40446   }
       
 40447 
       
 40448   if( nFin==0 ){
       
 40449     iLastPg--;
       
 40450     while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){
       
 40451       if( PTRMAP_ISPAGE(pBt, iLastPg) ){
       
 40452         MemPage *pPg;
       
 40453         int rc = btreeGetPage(pBt, iLastPg, &pPg, 0);
       
 40454         if( rc!=SQLITE_OK ){
       
 40455           return rc;
       
 40456         }
       
 40457         rc = sqlite3PagerWrite(pPg->pDbPage);
       
 40458         releasePage(pPg);
       
 40459         if( rc!=SQLITE_OK ){
       
 40460           return rc;
       
 40461         }
       
 40462       }
       
 40463       iLastPg--;
       
 40464     }
       
 40465     sqlite3PagerTruncateImage(pBt->pPager, iLastPg);
       
 40466   }
       
 40467   return SQLITE_OK;
       
 40468 }
       
 40469 
       
 40470 /*
       
 40471 ** A write-transaction must be opened before calling this function.
       
 40472 ** It performs a single unit of work towards an incremental vacuum.
       
 40473 **
       
 40474 ** If the incremental vacuum is finished after this function has run,
       
 40475 ** SQLITE_DONE is returned. If it is not finished, but no error occurred,
       
 40476 ** SQLITE_OK is returned. Otherwise an SQLite error code. 
       
 40477 */
       
 40478 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
       
 40479   int rc;
       
 40480   BtShared *pBt = p->pBt;
       
 40481 
       
 40482   sqlite3BtreeEnter(p);
       
 40483   assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
       
 40484   if( !pBt->autoVacuum ){
       
 40485     rc = SQLITE_DONE;
       
 40486   }else{
       
 40487     invalidateAllOverflowCache(pBt);
       
 40488     rc = incrVacuumStep(pBt, 0, pagerPagecount(pBt));
       
 40489   }
       
 40490   sqlite3BtreeLeave(p);
       
 40491   return rc;
       
 40492 }
       
 40493 
       
 40494 /*
       
 40495 ** This routine is called prior to sqlite3PagerCommit when a transaction
       
 40496 ** is commited for an auto-vacuum database.
       
 40497 **
       
 40498 ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
       
 40499 ** the database file should be truncated to during the commit process. 
       
 40500 ** i.e. the database has been reorganized so that only the first *pnTrunc
       
 40501 ** pages are in use.
       
 40502 */
       
 40503 static int autoVacuumCommit(BtShared *pBt){
       
 40504   int rc = SQLITE_OK;
       
 40505   Pager *pPager = pBt->pPager;
       
 40506   VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
       
 40507 
       
 40508   assert( sqlite3_mutex_held(pBt->mutex) );
       
 40509   invalidateAllOverflowCache(pBt);
       
 40510   assert(pBt->autoVacuum);
       
 40511   if( !pBt->incrVacuum ){
       
 40512     Pgno nFin;         /* Number of pages in database after autovacuuming */
       
 40513     Pgno nFree;        /* Number of pages on the freelist initially */
       
 40514     Pgno nPtrmap;      /* Number of PtrMap pages to be freed */
       
 40515     Pgno iFree;        /* The next page to be freed */
       
 40516     int nEntry;        /* Number of entries on one ptrmap page */
       
 40517     Pgno nOrig;        /* Database size before freeing */
       
 40518 
       
 40519     nOrig = pagerPagecount(pBt);
       
 40520     if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
       
 40521       /* It is not possible to create a database for which the final page
       
 40522       ** is either a pointer-map page or the pending-byte page. If one
       
 40523       ** is encountered, this indicates corruption.
       
 40524       */
       
 40525       return SQLITE_CORRUPT_BKPT;
       
 40526     }
       
 40527 
       
 40528     nFree = get4byte(&pBt->pPage1->aData[36]);
       
 40529     nEntry = pBt->usableSize/5;
       
 40530     nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
       
 40531     nFin = nOrig - nFree - nPtrmap;
       
 40532     if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
       
 40533       nFin--;
       
 40534     }
       
 40535     while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
       
 40536       nFin--;
       
 40537     }
       
 40538     if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
       
 40539 
       
 40540     for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
       
 40541       rc = incrVacuumStep(pBt, nFin, iFree);
       
 40542     }
       
 40543     if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
       
 40544       rc = SQLITE_OK;
       
 40545       rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
       
 40546       put4byte(&pBt->pPage1->aData[32], 0);
       
 40547       put4byte(&pBt->pPage1->aData[36], 0);
       
 40548       sqlite3PagerTruncateImage(pBt->pPager, nFin);
       
 40549     }
       
 40550     if( rc!=SQLITE_OK ){
       
 40551       sqlite3PagerRollback(pPager);
       
 40552     }
       
 40553   }
       
 40554 
       
 40555   assert( nRef==sqlite3PagerRefcount(pPager) );
       
 40556   return rc;
       
 40557 }
       
 40558 
       
 40559 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
       
 40560 # define setChildPtrmaps(x) SQLITE_OK
       
 40561 #endif
       
 40562 
       
 40563 /*
       
 40564 ** This routine does the first phase of a two-phase commit.  This routine
       
 40565 ** causes a rollback journal to be created (if it does not already exist)
       
 40566 ** and populated with enough information so that if a power loss occurs
       
 40567 ** the database can be restored to its original state by playing back
       
 40568 ** the journal.  Then the contents of the journal are flushed out to
       
 40569 ** the disk.  After the journal is safely on oxide, the changes to the
       
 40570 ** database are written into the database file and flushed to oxide.
       
 40571 ** At the end of this call, the rollback journal still exists on the
       
 40572 ** disk and we are still holding all locks, so the transaction has not
       
 40573 ** committed.  See sqlite3BtreeCommitPhaseTwo() for the second phase of the
       
 40574 ** commit process.
       
 40575 **
       
 40576 ** This call is a no-op if no write-transaction is currently active on pBt.
       
 40577 **
       
 40578 ** Otherwise, sync the database file for the btree pBt. zMaster points to
       
 40579 ** the name of a master journal file that should be written into the
       
 40580 ** individual journal file, or is NULL, indicating no master journal file 
       
 40581 ** (single database transaction).
       
 40582 **
       
 40583 ** When this is called, the master journal should already have been
       
 40584 ** created, populated with this journal pointer and synced to disk.
       
 40585 **
       
 40586 ** Once this is routine has returned, the only thing required to commit
       
 40587 ** the write-transaction for this database file is to delete the journal.
       
 40588 */
       
 40589 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
       
 40590   int rc = SQLITE_OK;
       
 40591   if( p->inTrans==TRANS_WRITE ){
       
 40592     BtShared *pBt = p->pBt;
       
 40593     sqlite3BtreeEnter(p);
       
 40594 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 40595     if( pBt->autoVacuum ){
       
 40596       rc = autoVacuumCommit(pBt);
       
 40597       if( rc!=SQLITE_OK ){
       
 40598         sqlite3BtreeLeave(p);
       
 40599         return rc;
       
 40600       }
       
 40601     }
       
 40602 #endif
       
 40603     rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
       
 40604     sqlite3BtreeLeave(p);
       
 40605   }
       
 40606   return rc;
       
 40607 }
       
 40608 
       
 40609 /*
       
 40610 ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
       
 40611 ** at the conclusion of a transaction.
       
 40612 */
       
 40613 static void btreeEndTransaction(Btree *p){
       
 40614   BtShared *pBt = p->pBt;
       
 40615   BtCursor *pCsr;
       
 40616   assert( sqlite3BtreeHoldsMutex(p) );
       
 40617 
       
 40618   /* Search for a cursor held open by this b-tree connection. If one exists,
       
 40619   ** then the transaction will be downgraded to a read-only transaction
       
 40620   ** instead of actually concluded. A subsequent call to CommitPhaseTwo() 
       
 40621   ** or Rollback() will finish the transaction and unlock the database.  */
       
 40622   for(pCsr=pBt->pCursor; pCsr && pCsr->pBtree!=p; pCsr=pCsr->pNext);
       
 40623   assert( pCsr==0 || p->inTrans>TRANS_NONE );
       
 40624 
       
 40625   btreeClearHasContent(pBt);
       
 40626   if( pCsr ){
       
 40627     downgradeAllSharedCacheTableLocks(p);
       
 40628     p->inTrans = TRANS_READ;
       
 40629   }else{
       
 40630     /* If the handle had any kind of transaction open, decrement the 
       
 40631     ** transaction count of the shared btree. If the transaction count 
       
 40632     ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
       
 40633     ** call below will unlock the pager.  */
       
 40634     if( p->inTrans!=TRANS_NONE ){
       
 40635       clearAllSharedCacheTableLocks(p);
       
 40636       pBt->nTransaction--;
       
 40637       if( 0==pBt->nTransaction ){
       
 40638         pBt->inTransaction = TRANS_NONE;
       
 40639       }
       
 40640     }
       
 40641 
       
 40642     /* Set the current transaction state to TRANS_NONE and unlock the 
       
 40643     ** pager if this call closed the only read or write transaction.  */
       
 40644     p->inTrans = TRANS_NONE;
       
 40645     unlockBtreeIfUnused(pBt);
       
 40646   }
       
 40647 
       
 40648   btreeIntegrity(p);
       
 40649 }
       
 40650 
       
 40651 /*
       
 40652 ** Commit the transaction currently in progress.
       
 40653 **
       
 40654 ** This routine implements the second phase of a 2-phase commit.  The
       
 40655 ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
       
 40656 ** be invoked prior to calling this routine.  The sqlite3BtreeCommitPhaseOne()
       
 40657 ** routine did all the work of writing information out to disk and flushing the
       
 40658 ** contents so that they are written onto the disk platter.  All this
       
 40659 ** routine has to do is delete or truncate or zero the header in the
       
 40660 ** the rollback journal (which causes the transaction to commit) and
       
 40661 ** drop locks.
       
 40662 **
       
 40663 ** This will release the write lock on the database file.  If there
       
 40664 ** are no active cursors, it also releases the read lock.
       
 40665 */
       
 40666 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p){
       
 40667   BtShared *pBt = p->pBt;
       
 40668 
       
 40669   sqlite3BtreeEnter(p);
       
 40670   btreeIntegrity(p);
       
 40671 
       
 40672   /* If the handle has a write-transaction open, commit the shared-btrees 
       
 40673   ** transaction and set the shared state to TRANS_READ.
       
 40674   */
       
 40675   if( p->inTrans==TRANS_WRITE ){
       
 40676     int rc;
       
 40677     assert( pBt->inTransaction==TRANS_WRITE );
       
 40678     assert( pBt->nTransaction>0 );
       
 40679     rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
       
 40680     if( rc!=SQLITE_OK ){
       
 40681       sqlite3BtreeLeave(p);
       
 40682       return rc;
       
 40683     }
       
 40684     pBt->inTransaction = TRANS_READ;
       
 40685   }
       
 40686 
       
 40687   btreeEndTransaction(p);
       
 40688   sqlite3BtreeLeave(p);
       
 40689   return SQLITE_OK;
       
 40690 }
       
 40691 
       
 40692 /*
       
 40693 ** Do both phases of a commit.
       
 40694 */
       
 40695 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
       
 40696   int rc;
       
 40697   sqlite3BtreeEnter(p);
       
 40698   rc = sqlite3BtreeCommitPhaseOne(p, 0);
       
 40699   if( rc==SQLITE_OK ){
       
 40700     rc = sqlite3BtreeCommitPhaseTwo(p);
       
 40701   }
       
 40702   sqlite3BtreeLeave(p);
       
 40703   return rc;
       
 40704 }
       
 40705 
       
 40706 #ifndef NDEBUG
       
 40707 /*
       
 40708 ** Return the number of write-cursors open on this handle. This is for use
       
 40709 ** in assert() expressions, so it is only compiled if NDEBUG is not
       
 40710 ** defined.
       
 40711 **
       
 40712 ** For the purposes of this routine, a write-cursor is any cursor that
       
 40713 ** is capable of writing to the databse.  That means the cursor was
       
 40714 ** originally opened for writing and the cursor has not be disabled
       
 40715 ** by having its state changed to CURSOR_FAULT.
       
 40716 */
       
 40717 static int countWriteCursors(BtShared *pBt){
       
 40718   BtCursor *pCur;
       
 40719   int r = 0;
       
 40720   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
       
 40721     if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; 
       
 40722   }
       
 40723   return r;
       
 40724 }
       
 40725 #endif
       
 40726 
       
 40727 /*
       
 40728 ** This routine sets the state to CURSOR_FAULT and the error
       
 40729 ** code to errCode for every cursor on BtShared that pBtree
       
 40730 ** references.
       
 40731 **
       
 40732 ** Every cursor is tripped, including cursors that belong
       
 40733 ** to other database connections that happen to be sharing
       
 40734 ** the cache with pBtree.
       
 40735 **
       
 40736 ** This routine gets called when a rollback occurs.
       
 40737 ** All cursors using the same cache must be tripped
       
 40738 ** to prevent them from trying to use the btree after
       
 40739 ** the rollback.  The rollback may have deleted tables
       
 40740 ** or moved root pages, so it is not sufficient to
       
 40741 ** save the state of the cursor.  The cursor must be
       
 40742 ** invalidated.
       
 40743 */
       
 40744 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
       
 40745   BtCursor *p;
       
 40746   sqlite3BtreeEnter(pBtree);
       
 40747   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
       
 40748     int i;
       
 40749     sqlite3BtreeClearCursor(p);
       
 40750     p->eState = CURSOR_FAULT;
       
 40751     p->skipNext = errCode;
       
 40752     for(i=0; i<=p->iPage; i++){
       
 40753       releasePage(p->apPage[i]);
       
 40754       p->apPage[i] = 0;
       
 40755     }
       
 40756   }
       
 40757   sqlite3BtreeLeave(pBtree);
       
 40758 }
       
 40759 
       
 40760 /*
       
 40761 ** Rollback the transaction in progress.  All cursors will be
       
 40762 ** invalided by this operation.  Any attempt to use a cursor
       
 40763 ** that was open at the beginning of this operation will result
       
 40764 ** in an error.
       
 40765 **
       
 40766 ** This will release the write lock on the database file.  If there
       
 40767 ** are no active cursors, it also releases the read lock.
       
 40768 */
       
 40769 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p){
       
 40770   int rc;
       
 40771   BtShared *pBt = p->pBt;
       
 40772   MemPage *pPage1;
       
 40773 
       
 40774   sqlite3BtreeEnter(p);
       
 40775   rc = saveAllCursors(pBt, 0, 0);
       
 40776 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 40777   if( rc!=SQLITE_OK ){
       
 40778     /* This is a horrible situation. An IO or malloc() error occurred whilst
       
 40779     ** trying to save cursor positions. If this is an automatic rollback (as
       
 40780     ** the result of a constraint, malloc() failure or IO error) then 
       
 40781     ** the cache may be internally inconsistent (not contain valid trees) so
       
 40782     ** we cannot simply return the error to the caller. Instead, abort 
       
 40783     ** all queries that may be using any of the cursors that failed to save.
       
 40784     */
       
 40785     sqlite3BtreeTripAllCursors(p, rc);
       
 40786   }
       
 40787 #endif
       
 40788   btreeIntegrity(p);
       
 40789 
       
 40790   if( p->inTrans==TRANS_WRITE ){
       
 40791     int rc2;
       
 40792 
       
 40793     assert( TRANS_WRITE==pBt->inTransaction );
       
 40794     rc2 = sqlite3PagerRollback(pBt->pPager);
       
 40795     if( rc2!=SQLITE_OK ){
       
 40796       rc = rc2;
       
 40797     }
       
 40798 
       
 40799     /* The rollback may have destroyed the pPage1->aData value.  So
       
 40800     ** call btreeGetPage() on page 1 again to make
       
 40801     ** sure pPage1->aData is set correctly. */
       
 40802     if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
       
 40803       releasePage(pPage1);
       
 40804     }
       
 40805     assert( countWriteCursors(pBt)==0 );
       
 40806     pBt->inTransaction = TRANS_READ;
       
 40807   }
       
 40808 
       
 40809   btreeEndTransaction(p);
       
 40810   sqlite3BtreeLeave(p);
       
 40811   return rc;
       
 40812 }
       
 40813 
       
 40814 /*
       
 40815 ** Start a statement subtransaction. The subtransaction can can be rolled
       
 40816 ** back independently of the main transaction. You must start a transaction 
       
 40817 ** before starting a subtransaction. The subtransaction is ended automatically 
       
 40818 ** if the main transaction commits or rolls back.
       
 40819 **
       
 40820 ** Statement subtransactions are used around individual SQL statements
       
 40821 ** that are contained within a BEGIN...COMMIT block.  If a constraint
       
 40822 ** error occurs within the statement, the effect of that one statement
       
 40823 ** can be rolled back without having to rollback the entire transaction.
       
 40824 **
       
 40825 ** A statement sub-transaction is implemented as an anonymous savepoint. The
       
 40826 ** value passed as the second parameter is the total number of savepoints,
       
 40827 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
       
 40828 ** are no active savepoints and no other statement-transactions open,
       
 40829 ** iStatement is 1. This anonymous savepoint can be released or rolled back
       
 40830 ** using the sqlite3BtreeSavepoint() function.
       
 40831 */
       
 40832 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
       
 40833   int rc;
       
 40834   BtShared *pBt = p->pBt;
       
 40835   sqlite3BtreeEnter(p);
       
 40836   assert( p->inTrans==TRANS_WRITE );
       
 40837   assert( pBt->readOnly==0 );
       
 40838   assert( iStatement>0 );
       
 40839   assert( iStatement>p->db->nSavepoint );
       
 40840   if( NEVER(p->inTrans!=TRANS_WRITE || pBt->readOnly) ){
       
 40841     rc = SQLITE_INTERNAL;
       
 40842   }else{
       
 40843     assert( pBt->inTransaction==TRANS_WRITE );
       
 40844     /* At the pager level, a statement transaction is a savepoint with
       
 40845     ** an index greater than all savepoints created explicitly using
       
 40846     ** SQL statements. It is illegal to open, release or rollback any
       
 40847     ** such savepoints while the statement transaction savepoint is active.
       
 40848     */
       
 40849     rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
       
 40850   }
       
 40851   sqlite3BtreeLeave(p);
       
 40852   return rc;
       
 40853 }
       
 40854 
       
 40855 /*
       
 40856 ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
       
 40857 ** or SAVEPOINT_RELEASE. This function either releases or rolls back the
       
 40858 ** savepoint identified by parameter iSavepoint, depending on the value 
       
 40859 ** of op.
       
 40860 **
       
 40861 ** Normally, iSavepoint is greater than or equal to zero. However, if op is
       
 40862 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the 
       
 40863 ** contents of the entire transaction are rolled back. This is different
       
 40864 ** from a normal transaction rollback, as no locks are released and the
       
 40865 ** transaction remains open.
       
 40866 */
       
 40867 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
       
 40868   int rc = SQLITE_OK;
       
 40869   if( p && p->inTrans==TRANS_WRITE ){
       
 40870     BtShared *pBt = p->pBt;
       
 40871     assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
       
 40872     assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
       
 40873     sqlite3BtreeEnter(p);
       
 40874     rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
       
 40875     if( rc==SQLITE_OK ){
       
 40876       rc = newDatabase(pBt);
       
 40877     }
       
 40878     sqlite3BtreeLeave(p);
       
 40879   }
       
 40880   return rc;
       
 40881 }
       
 40882 
       
 40883 /*
       
 40884 ** Create a new cursor for the BTree whose root is on the page
       
 40885 ** iTable. If a read-only cursor is requested, it is assumed that
       
 40886 ** the caller already has at least a read-only transaction open
       
 40887 ** on the database already. If a write-cursor is requested, then
       
 40888 ** the caller is assumed to have an open write transaction.
       
 40889 **
       
 40890 ** If wrFlag==0, then the cursor can only be used for reading.
       
 40891 ** If wrFlag==1, then the cursor can be used for reading or for
       
 40892 ** writing if other conditions for writing are also met.  These
       
 40893 ** are the conditions that must be met in order for writing to
       
 40894 ** be allowed:
       
 40895 **
       
 40896 ** 1:  The cursor must have been opened with wrFlag==1
       
 40897 **
       
 40898 ** 2:  Other database connections that share the same pager cache
       
 40899 **     but which are not in the READ_UNCOMMITTED state may not have
       
 40900 **     cursors open with wrFlag==0 on the same table.  Otherwise
       
 40901 **     the changes made by this write cursor would be visible to
       
 40902 **     the read cursors in the other database connection.
       
 40903 **
       
 40904 ** 3:  The database must be writable (not on read-only media)
       
 40905 **
       
 40906 ** 4:  There must be an active transaction.
       
 40907 **
       
 40908 ** No checking is done to make sure that page iTable really is the
       
 40909 ** root page of a b-tree.  If it is not, then the cursor acquired
       
 40910 ** will not work correctly.
       
 40911 **
       
 40912 ** It is assumed that the sqlite3BtreeCursorSize() bytes of memory 
       
 40913 ** pointed to by pCur have been zeroed by the caller.
       
 40914 */
       
 40915 static int btreeCursor(
       
 40916   Btree *p,                              /* The btree */
       
 40917   int iTable,                            /* Root page of table to open */
       
 40918   int wrFlag,                            /* 1 to write. 0 read-only */
       
 40919   struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
       
 40920   BtCursor *pCur                         /* Space for new cursor */
       
 40921 ){
       
 40922   BtShared *pBt = p->pBt;                /* Shared b-tree handle */
       
 40923 
       
 40924   assert( sqlite3BtreeHoldsMutex(p) );
       
 40925   assert( wrFlag==0 || wrFlag==1 );
       
 40926 
       
 40927   /* The following assert statements verify that if this is a sharable 
       
 40928   ** b-tree database, the connection is holding the required table locks, 
       
 40929   ** and that no other connection has any open cursor that conflicts with 
       
 40930   ** this lock.  */
       
 40931   assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) );
       
 40932   assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
       
 40933 
       
 40934   /* Assert that the caller has opened the required transaction. */
       
 40935   assert( p->inTrans>TRANS_NONE );
       
 40936   assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
       
 40937   assert( pBt->pPage1 && pBt->pPage1->aData );
       
 40938 
       
 40939   if( NEVER(wrFlag && pBt->readOnly) ){
       
 40940     return SQLITE_READONLY;
       
 40941   }
       
 40942   if( iTable==1 && pagerPagecount(pBt)==0 ){
       
 40943     return SQLITE_EMPTY;
       
 40944   }
       
 40945 
       
 40946   /* Now that no other errors can occur, finish filling in the BtCursor
       
 40947   ** variables and link the cursor into the BtShared list.  */
       
 40948   pCur->pgnoRoot = (Pgno)iTable;
       
 40949   pCur->iPage = -1;
       
 40950   pCur->pKeyInfo = pKeyInfo;
       
 40951   pCur->pBtree = p;
       
 40952   pCur->pBt = pBt;
       
 40953   pCur->wrFlag = (u8)wrFlag;
       
 40954   pCur->pNext = pBt->pCursor;
       
 40955   if( pCur->pNext ){
       
 40956     pCur->pNext->pPrev = pCur;
       
 40957   }
       
 40958   pBt->pCursor = pCur;
       
 40959   pCur->eState = CURSOR_INVALID;
       
 40960   pCur->cachedRowid = 0;
       
 40961   return SQLITE_OK;
       
 40962 }
       
 40963 SQLITE_PRIVATE int sqlite3BtreeCursor(
       
 40964   Btree *p,                                   /* The btree */
       
 40965   int iTable,                                 /* Root page of table to open */
       
 40966   int wrFlag,                                 /* 1 to write. 0 read-only */
       
 40967   struct KeyInfo *pKeyInfo,                   /* First arg to xCompare() */
       
 40968   BtCursor *pCur                              /* Write new cursor here */
       
 40969 ){
       
 40970   int rc;
       
 40971   sqlite3BtreeEnter(p);
       
 40972   rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
       
 40973   sqlite3BtreeLeave(p);
       
 40974   return rc;
       
 40975 }
       
 40976 
       
 40977 /*
       
 40978 ** Return the size of a BtCursor object in bytes.
       
 40979 **
       
 40980 ** This interfaces is needed so that users of cursors can preallocate
       
 40981 ** sufficient storage to hold a cursor.  The BtCursor object is opaque
       
 40982 ** to users so they cannot do the sizeof() themselves - they must call
       
 40983 ** this routine.
       
 40984 */
       
 40985 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
       
 40986   return sizeof(BtCursor);
       
 40987 }
       
 40988 
       
 40989 /*
       
 40990 ** Set the cached rowid value of every cursor in the same database file
       
 40991 ** as pCur and having the same root page number as pCur.  The value is
       
 40992 ** set to iRowid.
       
 40993 **
       
 40994 ** Only positive rowid values are considered valid for this cache.
       
 40995 ** The cache is initialized to zero, indicating an invalid cache.
       
 40996 ** A btree will work fine with zero or negative rowids.  We just cannot
       
 40997 ** cache zero or negative rowids, which means tables that use zero or
       
 40998 ** negative rowids might run a little slower.  But in practice, zero
       
 40999 ** or negative rowids are very uncommon so this should not be a problem.
       
 41000 */
       
 41001 SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){
       
 41002   BtCursor *p;
       
 41003   for(p=pCur->pBt->pCursor; p; p=p->pNext){
       
 41004     if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid;
       
 41005   }
       
 41006   assert( pCur->cachedRowid==iRowid );
       
 41007 }
       
 41008 
       
 41009 /*
       
 41010 ** Return the cached rowid for the given cursor.  A negative or zero
       
 41011 ** return value indicates that the rowid cache is invalid and should be
       
 41012 ** ignored.  If the rowid cache has never before been set, then a
       
 41013 ** zero is returned.
       
 41014 */
       
 41015 SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){
       
 41016   return pCur->cachedRowid;
       
 41017 }
       
 41018 
       
 41019 /*
       
 41020 ** Close a cursor.  The read lock on the database file is released
       
 41021 ** when the last cursor is closed.
       
 41022 */
       
 41023 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
       
 41024   Btree *pBtree = pCur->pBtree;
       
 41025   if( pBtree ){
       
 41026     int i;
       
 41027     BtShared *pBt = pCur->pBt;
       
 41028     sqlite3BtreeEnter(pBtree);
       
 41029     sqlite3BtreeClearCursor(pCur);
       
 41030     if( pCur->pPrev ){
       
 41031       pCur->pPrev->pNext = pCur->pNext;
       
 41032     }else{
       
 41033       pBt->pCursor = pCur->pNext;
       
 41034     }
       
 41035     if( pCur->pNext ){
       
 41036       pCur->pNext->pPrev = pCur->pPrev;
       
 41037     }
       
 41038     for(i=0; i<=pCur->iPage; i++){
       
 41039       releasePage(pCur->apPage[i]);
       
 41040     }
       
 41041     unlockBtreeIfUnused(pBt);
       
 41042     invalidateOverflowCache(pCur);
       
 41043     /* sqlite3_free(pCur); */
       
 41044     sqlite3BtreeLeave(pBtree);
       
 41045   }
       
 41046   return SQLITE_OK;
       
 41047 }
       
 41048 
       
 41049 /*
       
 41050 ** Make sure the BtCursor* given in the argument has a valid
       
 41051 ** BtCursor.info structure.  If it is not already valid, call
       
 41052 ** btreeParseCell() to fill it in.
       
 41053 **
       
 41054 ** BtCursor.info is a cache of the information in the current cell.
       
 41055 ** Using this cache reduces the number of calls to btreeParseCell().
       
 41056 **
       
 41057 ** 2007-06-25:  There is a bug in some versions of MSVC that cause the
       
 41058 ** compiler to crash when getCellInfo() is implemented as a macro.
       
 41059 ** But there is a measureable speed advantage to using the macro on gcc
       
 41060 ** (when less compiler optimizations like -Os or -O0 are used and the
       
 41061 ** compiler is not doing agressive inlining.)  So we use a real function
       
 41062 ** for MSVC and a macro for everything else.  Ticket #2457.
       
 41063 */
       
 41064 #ifndef NDEBUG
       
 41065   static void assertCellInfo(BtCursor *pCur){
       
 41066     CellInfo info;
       
 41067     int iPage = pCur->iPage;
       
 41068     memset(&info, 0, sizeof(info));
       
 41069     btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
       
 41070     assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
       
 41071   }
       
 41072 #else
       
 41073   #define assertCellInfo(x)
       
 41074 #endif
       
 41075 #ifdef _MSC_VER
       
 41076   /* Use a real function in MSVC to work around bugs in that compiler. */
       
 41077   static void getCellInfo(BtCursor *pCur){
       
 41078     if( pCur->info.nSize==0 ){
       
 41079       int iPage = pCur->iPage;
       
 41080       btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
       
 41081       pCur->validNKey = 1;
       
 41082     }else{
       
 41083       assertCellInfo(pCur);
       
 41084     }
       
 41085   }
       
 41086 #else /* if not _MSC_VER */
       
 41087   /* Use a macro in all other compilers so that the function is inlined */
       
 41088 #define getCellInfo(pCur)                                                      \
       
 41089   if( pCur->info.nSize==0 ){                                                   \
       
 41090     int iPage = pCur->iPage;                                                   \
       
 41091     btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
       
 41092     pCur->validNKey = 1;                                                       \
       
 41093   }else{                                                                       \
       
 41094     assertCellInfo(pCur);                                                      \
       
 41095   }
       
 41096 #endif /* _MSC_VER */
       
 41097 
       
 41098 #ifndef NDEBUG  /* The next routine used only within assert() statements */
       
 41099 /*
       
 41100 ** Return true if the given BtCursor is valid.  A valid cursor is one
       
 41101 ** that is currently pointing to a row in a (non-empty) table.
       
 41102 ** This is a verification routine is used only within assert() statements.
       
 41103 */
       
 41104 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
       
 41105   return pCur && pCur->eState==CURSOR_VALID;
       
 41106 }
       
 41107 #endif /* NDEBUG */
       
 41108 
       
 41109 /*
       
 41110 ** Set *pSize to the size of the buffer needed to hold the value of
       
 41111 ** the key for the current entry.  If the cursor is not pointing
       
 41112 ** to a valid entry, *pSize is set to 0. 
       
 41113 **
       
 41114 ** For a table with the INTKEY flag set, this routine returns the key
       
 41115 ** itself, not the number of bytes in the key.
       
 41116 **
       
 41117 ** The caller must position the cursor prior to invoking this routine.
       
 41118 ** 
       
 41119 ** This routine cannot fail.  It always returns SQLITE_OK.  
       
 41120 */
       
 41121 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
       
 41122   assert( cursorHoldsMutex(pCur) );
       
 41123   assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
       
 41124   if( pCur->eState!=CURSOR_VALID ){
       
 41125     *pSize = 0;
       
 41126   }else{
       
 41127     getCellInfo(pCur);
       
 41128     *pSize = pCur->info.nKey;
       
 41129   }
       
 41130   return SQLITE_OK;
       
 41131 }
       
 41132 
       
 41133 /*
       
 41134 ** Set *pSize to the number of bytes of data in the entry the
       
 41135 ** cursor currently points to.
       
 41136 **
       
 41137 ** The caller must guarantee that the cursor is pointing to a non-NULL
       
 41138 ** valid entry.  In other words, the calling procedure must guarantee
       
 41139 ** that the cursor has Cursor.eState==CURSOR_VALID.
       
 41140 **
       
 41141 ** Failure is not possible.  This function always returns SQLITE_OK.
       
 41142 ** It might just as well be a procedure (returning void) but we continue
       
 41143 ** to return an integer result code for historical reasons.
       
 41144 */
       
 41145 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
       
 41146   assert( cursorHoldsMutex(pCur) );
       
 41147   assert( pCur->eState==CURSOR_VALID );
       
 41148   getCellInfo(pCur);
       
 41149   *pSize = pCur->info.nData;
       
 41150   return SQLITE_OK;
       
 41151 }
       
 41152 
       
 41153 /*
       
 41154 ** Given the page number of an overflow page in the database (parameter
       
 41155 ** ovfl), this function finds the page number of the next page in the 
       
 41156 ** linked list of overflow pages. If possible, it uses the auto-vacuum
       
 41157 ** pointer-map data instead of reading the content of page ovfl to do so. 
       
 41158 **
       
 41159 ** If an error occurs an SQLite error code is returned. Otherwise:
       
 41160 **
       
 41161 ** The page number of the next overflow page in the linked list is 
       
 41162 ** written to *pPgnoNext. If page ovfl is the last page in its linked 
       
 41163 ** list, *pPgnoNext is set to zero. 
       
 41164 **
       
 41165 ** If ppPage is not NULL, and a reference to the MemPage object corresponding
       
 41166 ** to page number pOvfl was obtained, then *ppPage is set to point to that
       
 41167 ** reference. It is the responsibility of the caller to call releasePage()
       
 41168 ** on *ppPage to free the reference. In no reference was obtained (because
       
 41169 ** the pointer-map was used to obtain the value for *pPgnoNext), then
       
 41170 ** *ppPage is set to zero.
       
 41171 */
       
 41172 static int getOverflowPage(
       
 41173   BtShared *pBt,               /* The database file */
       
 41174   Pgno ovfl,                   /* Current overflow page number */
       
 41175   MemPage **ppPage,            /* OUT: MemPage handle (may be NULL) */
       
 41176   Pgno *pPgnoNext              /* OUT: Next overflow page number */
       
 41177 ){
       
 41178   Pgno next = 0;
       
 41179   MemPage *pPage = 0;
       
 41180   int rc = SQLITE_OK;
       
 41181 
       
 41182   assert( sqlite3_mutex_held(pBt->mutex) );
       
 41183   assert(pPgnoNext);
       
 41184 
       
 41185 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 41186   /* Try to find the next page in the overflow list using the
       
 41187   ** autovacuum pointer-map pages. Guess that the next page in 
       
 41188   ** the overflow list is page number (ovfl+1). If that guess turns 
       
 41189   ** out to be wrong, fall back to loading the data of page 
       
 41190   ** number ovfl to determine the next page number.
       
 41191   */
       
 41192   if( pBt->autoVacuum ){
       
 41193     Pgno pgno;
       
 41194     Pgno iGuess = ovfl+1;
       
 41195     u8 eType;
       
 41196 
       
 41197     while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
       
 41198       iGuess++;
       
 41199     }
       
 41200 
       
 41201     if( iGuess<=pagerPagecount(pBt) ){
       
 41202       rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
       
 41203       if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
       
 41204         next = iGuess;
       
 41205         rc = SQLITE_DONE;
       
 41206       }
       
 41207     }
       
 41208   }
       
 41209 #endif
       
 41210 
       
 41211   assert( next==0 || rc==SQLITE_DONE );
       
 41212   if( rc==SQLITE_OK ){
       
 41213     rc = btreeGetPage(pBt, ovfl, &pPage, 0);
       
 41214     assert( rc==SQLITE_OK || pPage==0 );
       
 41215     if( rc==SQLITE_OK ){
       
 41216       next = get4byte(pPage->aData);
       
 41217     }
       
 41218   }
       
 41219 
       
 41220   *pPgnoNext = next;
       
 41221   if( ppPage ){
       
 41222     *ppPage = pPage;
       
 41223   }else{
       
 41224     releasePage(pPage);
       
 41225   }
       
 41226   return (rc==SQLITE_DONE ? SQLITE_OK : rc);
       
 41227 }
       
 41228 
       
 41229 /*
       
 41230 ** Copy data from a buffer to a page, or from a page to a buffer.
       
 41231 **
       
 41232 ** pPayload is a pointer to data stored on database page pDbPage.
       
 41233 ** If argument eOp is false, then nByte bytes of data are copied
       
 41234 ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
       
 41235 ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
       
 41236 ** of data are copied from the buffer pBuf to pPayload.
       
 41237 **
       
 41238 ** SQLITE_OK is returned on success, otherwise an error code.
       
 41239 */
       
 41240 static int copyPayload(
       
 41241   void *pPayload,           /* Pointer to page data */
       
 41242   void *pBuf,               /* Pointer to buffer */
       
 41243   int nByte,                /* Number of bytes to copy */
       
 41244   int eOp,                  /* 0 -> copy from page, 1 -> copy to page */
       
 41245   DbPage *pDbPage           /* Page containing pPayload */
       
 41246 ){
       
 41247   if( eOp ){
       
 41248     /* Copy data from buffer to page (a write operation) */
       
 41249     int rc = sqlite3PagerWrite(pDbPage);
       
 41250     if( rc!=SQLITE_OK ){
       
 41251       return rc;
       
 41252     }
       
 41253     memcpy(pPayload, pBuf, nByte);
       
 41254   }else{
       
 41255     /* Copy data from page to buffer (a read operation) */
       
 41256     memcpy(pBuf, pPayload, nByte);
       
 41257   }
       
 41258   return SQLITE_OK;
       
 41259 }
       
 41260 
       
 41261 /*
       
 41262 ** This function is used to read or overwrite payload information
       
 41263 ** for the entry that the pCur cursor is pointing to. If the eOp
       
 41264 ** parameter is 0, this is a read operation (data copied into
       
 41265 ** buffer pBuf). If it is non-zero, a write (data copied from
       
 41266 ** buffer pBuf).
       
 41267 **
       
 41268 ** A total of "amt" bytes are read or written beginning at "offset".
       
 41269 ** Data is read to or from the buffer pBuf.
       
 41270 **
       
 41271 ** The content being read or written might appear on the main page
       
 41272 ** or be scattered out on multiple overflow pages.
       
 41273 **
       
 41274 ** If the BtCursor.isIncrblobHandle flag is set, and the current
       
 41275 ** cursor entry uses one or more overflow pages, this function
       
 41276 ** allocates space for and lazily popluates the overflow page-list 
       
 41277 ** cache array (BtCursor.aOverflow). Subsequent calls use this
       
 41278 ** cache to make seeking to the supplied offset more efficient.
       
 41279 **
       
 41280 ** Once an overflow page-list cache has been allocated, it may be
       
 41281 ** invalidated if some other cursor writes to the same table, or if
       
 41282 ** the cursor is moved to a different row. Additionally, in auto-vacuum
       
 41283 ** mode, the following events may invalidate an overflow page-list cache.
       
 41284 **
       
 41285 **   * An incremental vacuum,
       
 41286 **   * A commit in auto_vacuum="full" mode,
       
 41287 **   * Creating a table (may require moving an overflow page).
       
 41288 */
       
 41289 static int accessPayload(
       
 41290   BtCursor *pCur,      /* Cursor pointing to entry to read from */
       
 41291   u32 offset,          /* Begin reading this far into payload */
       
 41292   u32 amt,             /* Read this many bytes */
       
 41293   unsigned char *pBuf, /* Write the bytes into this buffer */ 
       
 41294   int eOp              /* zero to read. non-zero to write. */
       
 41295 ){
       
 41296   unsigned char *aPayload;
       
 41297   int rc = SQLITE_OK;
       
 41298   u32 nKey;
       
 41299   int iIdx = 0;
       
 41300   MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
       
 41301   BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
       
 41302 
       
 41303   assert( pPage );
       
 41304   assert( pCur->eState==CURSOR_VALID );
       
 41305   assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
       
 41306   assert( cursorHoldsMutex(pCur) );
       
 41307 
       
 41308   getCellInfo(pCur);
       
 41309   aPayload = pCur->info.pCell + pCur->info.nHeader;
       
 41310   nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
       
 41311 
       
 41312   if( NEVER(offset+amt > nKey+pCur->info.nData) 
       
 41313    || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
       
 41314   ){
       
 41315     /* Trying to read or write past the end of the data is an error */
       
 41316     return SQLITE_CORRUPT_BKPT;
       
 41317   }
       
 41318 
       
 41319   /* Check if data must be read/written to/from the btree page itself. */
       
 41320   if( offset<pCur->info.nLocal ){
       
 41321     int a = amt;
       
 41322     if( a+offset>pCur->info.nLocal ){
       
 41323       a = pCur->info.nLocal - offset;
       
 41324     }
       
 41325     rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
       
 41326     offset = 0;
       
 41327     pBuf += a;
       
 41328     amt -= a;
       
 41329   }else{
       
 41330     offset -= pCur->info.nLocal;
       
 41331   }
       
 41332 
       
 41333   if( rc==SQLITE_OK && amt>0 ){
       
 41334     const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
       
 41335     Pgno nextPage;
       
 41336 
       
 41337     nextPage = get4byte(&aPayload[pCur->info.nLocal]);
       
 41338 
       
 41339 #ifndef SQLITE_OMIT_INCRBLOB
       
 41340     /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
       
 41341     ** has not been allocated, allocate it now. The array is sized at
       
 41342     ** one entry for each overflow page in the overflow chain. The
       
 41343     ** page number of the first overflow page is stored in aOverflow[0],
       
 41344     ** etc. A value of 0 in the aOverflow[] array means "not yet known"
       
 41345     ** (the cache is lazily populated).
       
 41346     */
       
 41347     if( pCur->isIncrblobHandle && !pCur->aOverflow ){
       
 41348       int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
       
 41349       pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
       
 41350       /* nOvfl is always positive.  If it were zero, fetchPayload would have
       
 41351       ** been used instead of this routine. */
       
 41352       if( ALWAYS(nOvfl) && !pCur->aOverflow ){
       
 41353         rc = SQLITE_NOMEM;
       
 41354       }
       
 41355     }
       
 41356 
       
 41357     /* If the overflow page-list cache has been allocated and the
       
 41358     ** entry for the first required overflow page is valid, skip
       
 41359     ** directly to it.
       
 41360     */
       
 41361     if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
       
 41362       iIdx = (offset/ovflSize);
       
 41363       nextPage = pCur->aOverflow[iIdx];
       
 41364       offset = (offset%ovflSize);
       
 41365     }
       
 41366 #endif
       
 41367 
       
 41368     for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
       
 41369 
       
 41370 #ifndef SQLITE_OMIT_INCRBLOB
       
 41371       /* If required, populate the overflow page-list cache. */
       
 41372       if( pCur->aOverflow ){
       
 41373         assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
       
 41374         pCur->aOverflow[iIdx] = nextPage;
       
 41375       }
       
 41376 #endif
       
 41377 
       
 41378       if( offset>=ovflSize ){
       
 41379         /* The only reason to read this page is to obtain the page
       
 41380         ** number for the next page in the overflow chain. The page
       
 41381         ** data is not required. So first try to lookup the overflow
       
 41382         ** page-list cache, if any, then fall back to the getOverflowPage()
       
 41383         ** function.
       
 41384         */
       
 41385 #ifndef SQLITE_OMIT_INCRBLOB
       
 41386         if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
       
 41387           nextPage = pCur->aOverflow[iIdx+1];
       
 41388         } else 
       
 41389 #endif
       
 41390           rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
       
 41391         offset -= ovflSize;
       
 41392       }else{
       
 41393         /* Need to read this page properly. It contains some of the
       
 41394         ** range of data that is being read (eOp==0) or written (eOp!=0).
       
 41395         */
       
 41396         DbPage *pDbPage;
       
 41397         int a = amt;
       
 41398         rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
       
 41399         if( rc==SQLITE_OK ){
       
 41400           aPayload = sqlite3PagerGetData(pDbPage);
       
 41401           nextPage = get4byte(aPayload);
       
 41402           if( a + offset > ovflSize ){
       
 41403             a = ovflSize - offset;
       
 41404           }
       
 41405           rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
       
 41406           sqlite3PagerUnref(pDbPage);
       
 41407           offset = 0;
       
 41408           amt -= a;
       
 41409           pBuf += a;
       
 41410         }
       
 41411       }
       
 41412     }
       
 41413   }
       
 41414 
       
 41415   if( rc==SQLITE_OK && amt>0 ){
       
 41416     return SQLITE_CORRUPT_BKPT;
       
 41417   }
       
 41418   return rc;
       
 41419 }
       
 41420 
       
 41421 /*
       
 41422 ** Read part of the key associated with cursor pCur.  Exactly
       
 41423 ** "amt" bytes will be transfered into pBuf[].  The transfer
       
 41424 ** begins at "offset".
       
 41425 **
       
 41426 ** The caller must ensure that pCur is pointing to a valid row
       
 41427 ** in the table.
       
 41428 **
       
 41429 ** Return SQLITE_OK on success or an error code if anything goes
       
 41430 ** wrong.  An error is returned if "offset+amt" is larger than
       
 41431 ** the available payload.
       
 41432 */
       
 41433 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
       
 41434   assert( cursorHoldsMutex(pCur) );
       
 41435   assert( pCur->eState==CURSOR_VALID );
       
 41436   assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
       
 41437   assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
       
 41438   return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
       
 41439 }
       
 41440 
       
 41441 /*
       
 41442 ** Read part of the data associated with cursor pCur.  Exactly
       
 41443 ** "amt" bytes will be transfered into pBuf[].  The transfer
       
 41444 ** begins at "offset".
       
 41445 **
       
 41446 ** Return SQLITE_OK on success or an error code if anything goes
       
 41447 ** wrong.  An error is returned if "offset+amt" is larger than
       
 41448 ** the available payload.
       
 41449 */
       
 41450 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
       
 41451   int rc;
       
 41452 
       
 41453 #ifndef SQLITE_OMIT_INCRBLOB
       
 41454   if ( pCur->eState==CURSOR_INVALID ){
       
 41455     return SQLITE_ABORT;
       
 41456   }
       
 41457 #endif
       
 41458 
       
 41459   assert( cursorHoldsMutex(pCur) );
       
 41460   rc = restoreCursorPosition(pCur);
       
 41461   if( rc==SQLITE_OK ){
       
 41462     assert( pCur->eState==CURSOR_VALID );
       
 41463     assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
       
 41464     assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
       
 41465     rc = accessPayload(pCur, offset, amt, pBuf, 0);
       
 41466   }
       
 41467   return rc;
       
 41468 }
       
 41469 
       
 41470 /*
       
 41471 ** Return a pointer to payload information from the entry that the 
       
 41472 ** pCur cursor is pointing to.  The pointer is to the beginning of
       
 41473 ** the key if skipKey==0 and it points to the beginning of data if
       
 41474 ** skipKey==1.  The number of bytes of available key/data is written
       
 41475 ** into *pAmt.  If *pAmt==0, then the value returned will not be
       
 41476 ** a valid pointer.
       
 41477 **
       
 41478 ** This routine is an optimization.  It is common for the entire key
       
 41479 ** and data to fit on the local page and for there to be no overflow
       
 41480 ** pages.  When that is so, this routine can be used to access the
       
 41481 ** key and data without making a copy.  If the key and/or data spills
       
 41482 ** onto overflow pages, then accessPayload() must be used to reassemble
       
 41483 ** the key/data and copy it into a preallocated buffer.
       
 41484 **
       
 41485 ** The pointer returned by this routine looks directly into the cached
       
 41486 ** page of the database.  The data might change or move the next time
       
 41487 ** any btree routine is called.
       
 41488 */
       
 41489 static const unsigned char *fetchPayload(
       
 41490   BtCursor *pCur,      /* Cursor pointing to entry to read from */
       
 41491   int *pAmt,           /* Write the number of available bytes here */
       
 41492   int skipKey          /* read beginning at data if this is true */
       
 41493 ){
       
 41494   unsigned char *aPayload;
       
 41495   MemPage *pPage;
       
 41496   u32 nKey;
       
 41497   u32 nLocal;
       
 41498 
       
 41499   assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
       
 41500   assert( pCur->eState==CURSOR_VALID );
       
 41501   assert( cursorHoldsMutex(pCur) );
       
 41502   pPage = pCur->apPage[pCur->iPage];
       
 41503   assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
       
 41504   if( NEVER(pCur->info.nSize==0) ){
       
 41505     btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
       
 41506                    &pCur->info);
       
 41507   }
       
 41508   aPayload = pCur->info.pCell;
       
 41509   aPayload += pCur->info.nHeader;
       
 41510   if( pPage->intKey ){
       
 41511     nKey = 0;
       
 41512   }else{
       
 41513     nKey = (int)pCur->info.nKey;
       
 41514   }
       
 41515   if( skipKey ){
       
 41516     aPayload += nKey;
       
 41517     nLocal = pCur->info.nLocal - nKey;
       
 41518   }else{
       
 41519     nLocal = pCur->info.nLocal;
       
 41520     assert( nLocal<=nKey );
       
 41521   }
       
 41522   *pAmt = nLocal;
       
 41523   return aPayload;
       
 41524 }
       
 41525 
       
 41526 
       
 41527 /*
       
 41528 ** For the entry that cursor pCur is point to, return as
       
 41529 ** many bytes of the key or data as are available on the local
       
 41530 ** b-tree page.  Write the number of available bytes into *pAmt.
       
 41531 **
       
 41532 ** The pointer returned is ephemeral.  The key/data may move
       
 41533 ** or be destroyed on the next call to any Btree routine,
       
 41534 ** including calls from other threads against the same cache.
       
 41535 ** Hence, a mutex on the BtShared should be held prior to calling
       
 41536 ** this routine.
       
 41537 **
       
 41538 ** These routines is used to get quick access to key and data
       
 41539 ** in the common case where no overflow pages are used.
       
 41540 */
       
 41541 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
       
 41542   const void *p = 0;
       
 41543   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
       
 41544   assert( cursorHoldsMutex(pCur) );
       
 41545   if( ALWAYS(pCur->eState==CURSOR_VALID) ){
       
 41546     p = (const void*)fetchPayload(pCur, pAmt, 0);
       
 41547   }
       
 41548   return p;
       
 41549 }
       
 41550 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
       
 41551   const void *p = 0;
       
 41552   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
       
 41553   assert( cursorHoldsMutex(pCur) );
       
 41554   if( ALWAYS(pCur->eState==CURSOR_VALID) ){
       
 41555     p = (const void*)fetchPayload(pCur, pAmt, 1);
       
 41556   }
       
 41557   return p;
       
 41558 }
       
 41559 
       
 41560 
       
 41561 /*
       
 41562 ** Move the cursor down to a new child page.  The newPgno argument is the
       
 41563 ** page number of the child page to move to.
       
 41564 **
       
 41565 ** This function returns SQLITE_CORRUPT if the page-header flags field of
       
 41566 ** the new child page does not match the flags field of the parent (i.e.
       
 41567 ** if an intkey page appears to be the parent of a non-intkey page, or
       
 41568 ** vice-versa).
       
 41569 */
       
 41570 static int moveToChild(BtCursor *pCur, u32 newPgno){
       
 41571   int rc;
       
 41572   int i = pCur->iPage;
       
 41573   MemPage *pNewPage;
       
 41574   BtShared *pBt = pCur->pBt;
       
 41575 
       
 41576   assert( cursorHoldsMutex(pCur) );
       
 41577   assert( pCur->eState==CURSOR_VALID );
       
 41578   assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
       
 41579   if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
       
 41580     return SQLITE_CORRUPT_BKPT;
       
 41581   }
       
 41582   rc = getAndInitPage(pBt, newPgno, &pNewPage);
       
 41583   if( rc ) return rc;
       
 41584   pCur->apPage[i+1] = pNewPage;
       
 41585   pCur->aiIdx[i+1] = 0;
       
 41586   pCur->iPage++;
       
 41587 
       
 41588   pCur->info.nSize = 0;
       
 41589   pCur->validNKey = 0;
       
 41590   if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
       
 41591     return SQLITE_CORRUPT_BKPT;
       
 41592   }
       
 41593   return SQLITE_OK;
       
 41594 }
       
 41595 
       
 41596 #ifndef NDEBUG
       
 41597 /*
       
 41598 ** Page pParent is an internal (non-leaf) tree page. This function 
       
 41599 ** asserts that page number iChild is the left-child if the iIdx'th
       
 41600 ** cell in page pParent. Or, if iIdx is equal to the total number of
       
 41601 ** cells in pParent, that page number iChild is the right-child of
       
 41602 ** the page.
       
 41603 */
       
 41604 static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
       
 41605   assert( iIdx<=pParent->nCell );
       
 41606   if( iIdx==pParent->nCell ){
       
 41607     assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
       
 41608   }else{
       
 41609     assert( get4byte(findCell(pParent, iIdx))==iChild );
       
 41610   }
       
 41611 }
       
 41612 #else
       
 41613 #  define assertParentIndex(x,y,z) 
       
 41614 #endif
       
 41615 
       
 41616 /*
       
 41617 ** Move the cursor up to the parent page.
       
 41618 **
       
 41619 ** pCur->idx is set to the cell index that contains the pointer
       
 41620 ** to the page we are coming from.  If we are coming from the
       
 41621 ** right-most child page then pCur->idx is set to one more than
       
 41622 ** the largest cell index.
       
 41623 */
       
 41624 static void moveToParent(BtCursor *pCur){
       
 41625   assert( cursorHoldsMutex(pCur) );
       
 41626   assert( pCur->eState==CURSOR_VALID );
       
 41627   assert( pCur->iPage>0 );
       
 41628   assert( pCur->apPage[pCur->iPage] );
       
 41629   assertParentIndex(
       
 41630     pCur->apPage[pCur->iPage-1], 
       
 41631     pCur->aiIdx[pCur->iPage-1], 
       
 41632     pCur->apPage[pCur->iPage]->pgno
       
 41633   );
       
 41634   releasePage(pCur->apPage[pCur->iPage]);
       
 41635   pCur->iPage--;
       
 41636   pCur->info.nSize = 0;
       
 41637   pCur->validNKey = 0;
       
 41638 }
       
 41639 
       
 41640 /*
       
 41641 ** Move the cursor to point to the root page of its b-tree structure.
       
 41642 **
       
 41643 ** If the table has a virtual root page, then the cursor is moved to point
       
 41644 ** to the virtual root page instead of the actual root page. A table has a
       
 41645 ** virtual root page when the actual root page contains no cells and a 
       
 41646 ** single child page. This can only happen with the table rooted at page 1.
       
 41647 **
       
 41648 ** If the b-tree structure is empty, the cursor state is set to 
       
 41649 ** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
       
 41650 ** cell located on the root (or virtual root) page and the cursor state
       
 41651 ** is set to CURSOR_VALID.
       
 41652 **
       
 41653 ** If this function returns successfully, it may be assumed that the
       
 41654 ** page-header flags indicate that the [virtual] root-page is the expected 
       
 41655 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
       
 41656 ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
       
 41657 ** indicating a table b-tree, or if the caller did specify a KeyInfo 
       
 41658 ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
       
 41659 ** b-tree).
       
 41660 */
       
 41661 static int moveToRoot(BtCursor *pCur){
       
 41662   MemPage *pRoot;
       
 41663   int rc = SQLITE_OK;
       
 41664   Btree *p = pCur->pBtree;
       
 41665   BtShared *pBt = p->pBt;
       
 41666 
       
 41667   assert( cursorHoldsMutex(pCur) );
       
 41668   assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
       
 41669   assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
       
 41670   assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
       
 41671   if( pCur->eState>=CURSOR_REQUIRESEEK ){
       
 41672     if( pCur->eState==CURSOR_FAULT ){
       
 41673       assert( pCur->skipNext!=SQLITE_OK );
       
 41674       return pCur->skipNext;
       
 41675     }
       
 41676     sqlite3BtreeClearCursor(pCur);
       
 41677   }
       
 41678 
       
 41679   if( pCur->iPage>=0 ){
       
 41680     int i;
       
 41681     for(i=1; i<=pCur->iPage; i++){
       
 41682       releasePage(pCur->apPage[i]);
       
 41683     }
       
 41684     pCur->iPage = 0;
       
 41685   }else{
       
 41686     rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
       
 41687     if( rc!=SQLITE_OK ){
       
 41688       pCur->eState = CURSOR_INVALID;
       
 41689       return rc;
       
 41690     }
       
 41691     pCur->iPage = 0;
       
 41692 
       
 41693     /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
       
 41694     ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
       
 41695     ** NULL, the caller expects a table b-tree. If this is not the case,
       
 41696     ** return an SQLITE_CORRUPT error.  */
       
 41697     assert( pCur->apPage[0]->intKey==1 || pCur->apPage[0]->intKey==0 );
       
 41698     if( (pCur->pKeyInfo==0)!=pCur->apPage[0]->intKey ){
       
 41699       return SQLITE_CORRUPT_BKPT;
       
 41700     }
       
 41701   }
       
 41702 
       
 41703   /* Assert that the root page is of the correct type. This must be the
       
 41704   ** case as the call to this function that loaded the root-page (either
       
 41705   ** this call or a previous invocation) would have detected corruption 
       
 41706   ** if the assumption were not true, and it is not possible for the flags 
       
 41707   ** byte to have been modified while this cursor is holding a reference
       
 41708   ** to the page.  */
       
 41709   pRoot = pCur->apPage[0];
       
 41710   assert( pRoot->pgno==pCur->pgnoRoot );
       
 41711   assert( pRoot->isInit && (pCur->pKeyInfo==0)==pRoot->intKey );
       
 41712 
       
 41713   pCur->aiIdx[0] = 0;
       
 41714   pCur->info.nSize = 0;
       
 41715   pCur->atLast = 0;
       
 41716   pCur->validNKey = 0;
       
 41717 
       
 41718   if( pRoot->nCell==0 && !pRoot->leaf ){
       
 41719     Pgno subpage;
       
 41720     if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
       
 41721     subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
       
 41722     pCur->eState = CURSOR_VALID;
       
 41723     rc = moveToChild(pCur, subpage);
       
 41724   }else{
       
 41725     pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
       
 41726   }
       
 41727   return rc;
       
 41728 }
       
 41729 
       
 41730 /*
       
 41731 ** Move the cursor down to the left-most leaf entry beneath the
       
 41732 ** entry to which it is currently pointing.
       
 41733 **
       
 41734 ** The left-most leaf is the one with the smallest key - the first
       
 41735 ** in ascending order.
       
 41736 */
       
 41737 static int moveToLeftmost(BtCursor *pCur){
       
 41738   Pgno pgno;
       
 41739   int rc = SQLITE_OK;
       
 41740   MemPage *pPage;
       
 41741 
       
 41742   assert( cursorHoldsMutex(pCur) );
       
 41743   assert( pCur->eState==CURSOR_VALID );
       
 41744   while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
       
 41745     assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
       
 41746     pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
       
 41747     rc = moveToChild(pCur, pgno);
       
 41748   }
       
 41749   return rc;
       
 41750 }
       
 41751 
       
 41752 /*
       
 41753 ** Move the cursor down to the right-most leaf entry beneath the
       
 41754 ** page to which it is currently pointing.  Notice the difference
       
 41755 ** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
       
 41756 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
       
 41757 ** finds the right-most entry beneath the *page*.
       
 41758 **
       
 41759 ** The right-most entry is the one with the largest key - the last
       
 41760 ** key in ascending order.
       
 41761 */
       
 41762 static int moveToRightmost(BtCursor *pCur){
       
 41763   Pgno pgno;
       
 41764   int rc = SQLITE_OK;
       
 41765   MemPage *pPage = 0;
       
 41766 
       
 41767   assert( cursorHoldsMutex(pCur) );
       
 41768   assert( pCur->eState==CURSOR_VALID );
       
 41769   while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
       
 41770     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
       
 41771     pCur->aiIdx[pCur->iPage] = pPage->nCell;
       
 41772     rc = moveToChild(pCur, pgno);
       
 41773   }
       
 41774   if( rc==SQLITE_OK ){
       
 41775     pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
       
 41776     pCur->info.nSize = 0;
       
 41777     pCur->validNKey = 0;
       
 41778   }
       
 41779   return rc;
       
 41780 }
       
 41781 
       
 41782 /* Move the cursor to the first entry in the table.  Return SQLITE_OK
       
 41783 ** on success.  Set *pRes to 0 if the cursor actually points to something
       
 41784 ** or set *pRes to 1 if the table is empty.
       
 41785 */
       
 41786 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
       
 41787   int rc;
       
 41788 
       
 41789   assert( cursorHoldsMutex(pCur) );
       
 41790   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
       
 41791   rc = moveToRoot(pCur);
       
 41792   if( rc==SQLITE_OK ){
       
 41793     if( pCur->eState==CURSOR_INVALID ){
       
 41794       assert( pCur->apPage[pCur->iPage]->nCell==0 );
       
 41795       *pRes = 1;
       
 41796       rc = SQLITE_OK;
       
 41797     }else{
       
 41798       assert( pCur->apPage[pCur->iPage]->nCell>0 );
       
 41799       *pRes = 0;
       
 41800       rc = moveToLeftmost(pCur);
       
 41801     }
       
 41802   }
       
 41803   return rc;
       
 41804 }
       
 41805 
       
 41806 /* Move the cursor to the last entry in the table.  Return SQLITE_OK
       
 41807 ** on success.  Set *pRes to 0 if the cursor actually points to something
       
 41808 ** or set *pRes to 1 if the table is empty.
       
 41809 */
       
 41810 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
       
 41811   int rc;
       
 41812  
       
 41813   assert( cursorHoldsMutex(pCur) );
       
 41814   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
       
 41815 
       
 41816   /* If the cursor already points to the last entry, this is a no-op. */
       
 41817   if( CURSOR_VALID==pCur->eState && pCur->atLast ){
       
 41818 #ifdef SQLITE_DEBUG
       
 41819     /* This block serves to assert() that the cursor really does point 
       
 41820     ** to the last entry in the b-tree. */
       
 41821     int ii;
       
 41822     for(ii=0; ii<pCur->iPage; ii++){
       
 41823       assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
       
 41824     }
       
 41825     assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
       
 41826     assert( pCur->apPage[pCur->iPage]->leaf );
       
 41827 #endif
       
 41828     return SQLITE_OK;
       
 41829   }
       
 41830 
       
 41831   rc = moveToRoot(pCur);
       
 41832   if( rc==SQLITE_OK ){
       
 41833     if( CURSOR_INVALID==pCur->eState ){
       
 41834       assert( pCur->apPage[pCur->iPage]->nCell==0 );
       
 41835       *pRes = 1;
       
 41836     }else{
       
 41837       assert( pCur->eState==CURSOR_VALID );
       
 41838       *pRes = 0;
       
 41839       rc = moveToRightmost(pCur);
       
 41840       pCur->atLast = rc==SQLITE_OK ?1:0;
       
 41841     }
       
 41842   }
       
 41843   return rc;
       
 41844 }
       
 41845 
       
 41846 /* Move the cursor so that it points to an entry near the key 
       
 41847 ** specified by pIdxKey or intKey.   Return a success code.
       
 41848 **
       
 41849 ** For INTKEY tables, the intKey parameter is used.  pIdxKey 
       
 41850 ** must be NULL.  For index tables, pIdxKey is used and intKey
       
 41851 ** is ignored.
       
 41852 **
       
 41853 ** If an exact match is not found, then the cursor is always
       
 41854 ** left pointing at a leaf page which would hold the entry if it
       
 41855 ** were present.  The cursor might point to an entry that comes
       
 41856 ** before or after the key.
       
 41857 **
       
 41858 ** An integer is written into *pRes which is the result of
       
 41859 ** comparing the key with the entry to which the cursor is 
       
 41860 ** pointing.  The meaning of the integer written into
       
 41861 ** *pRes is as follows:
       
 41862 **
       
 41863 **     *pRes<0      The cursor is left pointing at an entry that
       
 41864 **                  is smaller than intKey/pIdxKey or if the table is empty
       
 41865 **                  and the cursor is therefore left point to nothing.
       
 41866 **
       
 41867 **     *pRes==0     The cursor is left pointing at an entry that
       
 41868 **                  exactly matches intKey/pIdxKey.
       
 41869 **
       
 41870 **     *pRes>0      The cursor is left pointing at an entry that
       
 41871 **                  is larger than intKey/pIdxKey.
       
 41872 **
       
 41873 */
       
 41874 SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
       
 41875   BtCursor *pCur,          /* The cursor to be moved */
       
 41876   UnpackedRecord *pIdxKey, /* Unpacked index key */
       
 41877   i64 intKey,              /* The table key */
       
 41878   int biasRight,           /* If true, bias the search to the high end */
       
 41879   int *pRes                /* Write search results here */
       
 41880 ){
       
 41881   int rc;
       
 41882 
       
 41883   assert( cursorHoldsMutex(pCur) );
       
 41884   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
       
 41885   assert( pRes );
       
 41886   assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
       
 41887 
       
 41888   /* If the cursor is already positioned at the point we are trying
       
 41889   ** to move to, then just return without doing any work */
       
 41890   if( pCur->eState==CURSOR_VALID && pCur->validNKey 
       
 41891    && pCur->apPage[0]->intKey 
       
 41892   ){
       
 41893     if( pCur->info.nKey==intKey ){
       
 41894       *pRes = 0;
       
 41895       return SQLITE_OK;
       
 41896     }
       
 41897     if( pCur->atLast && pCur->info.nKey<intKey ){
       
 41898       *pRes = -1;
       
 41899       return SQLITE_OK;
       
 41900     }
       
 41901   }
       
 41902 
       
 41903   rc = moveToRoot(pCur);
       
 41904   if( rc ){
       
 41905     return rc;
       
 41906   }
       
 41907   assert( pCur->apPage[pCur->iPage] );
       
 41908   assert( pCur->apPage[pCur->iPage]->isInit );
       
 41909   assert( pCur->apPage[pCur->iPage]->nCell>0 || pCur->eState==CURSOR_INVALID );
       
 41910   if( pCur->eState==CURSOR_INVALID ){
       
 41911     *pRes = -1;
       
 41912     assert( pCur->apPage[pCur->iPage]->nCell==0 );
       
 41913     return SQLITE_OK;
       
 41914   }
       
 41915   assert( pCur->apPage[0]->intKey || pIdxKey );
       
 41916   for(;;){
       
 41917     int lwr, upr;
       
 41918     Pgno chldPg;
       
 41919     MemPage *pPage = pCur->apPage[pCur->iPage];
       
 41920     int c;
       
 41921 
       
 41922     /* pPage->nCell must be greater than zero. If this is the root-page
       
 41923     ** the cursor would have been INVALID above and this for(;;) loop
       
 41924     ** not run. If this is not the root-page, then the moveToChild() routine
       
 41925     ** would have already detected db corruption. Similarly, pPage must
       
 41926     ** be the right kind (index or table) of b-tree page. Otherwise
       
 41927     ** a moveToChild() or moveToRoot() call would have detected corruption.  */
       
 41928     assert( pPage->nCell>0 );
       
 41929     assert( pPage->intKey==(pIdxKey==0) );
       
 41930     lwr = 0;
       
 41931     upr = pPage->nCell-1;
       
 41932     if( biasRight ){
       
 41933       pCur->aiIdx[pCur->iPage] = (u16)upr;
       
 41934     }else{
       
 41935       pCur->aiIdx[pCur->iPage] = (u16)((upr+lwr)/2);
       
 41936     }
       
 41937     for(;;){
       
 41938       int idx = pCur->aiIdx[pCur->iPage]; /* Index of current cell in pPage */
       
 41939       u8 *pCell;                          /* Pointer to current cell in pPage */
       
 41940 
       
 41941       pCur->info.nSize = 0;
       
 41942       pCell = findCell(pPage, idx) + pPage->childPtrSize;
       
 41943       if( pPage->intKey ){
       
 41944         i64 nCellKey;
       
 41945         if( pPage->hasData ){
       
 41946           u32 dummy;
       
 41947           pCell += getVarint32(pCell, dummy);
       
 41948         }
       
 41949         getVarint(pCell, (u64*)&nCellKey);
       
 41950         if( nCellKey==intKey ){
       
 41951           c = 0;
       
 41952         }else if( nCellKey<intKey ){
       
 41953           c = -1;
       
 41954         }else{
       
 41955           assert( nCellKey>intKey );
       
 41956           c = +1;
       
 41957         }
       
 41958         pCur->validNKey = 1;
       
 41959         pCur->info.nKey = nCellKey;
       
 41960       }else{
       
 41961         /* The maximum supported page-size is 32768 bytes. This means that
       
 41962         ** the maximum number of record bytes stored on an index B-Tree
       
 41963         ** page is at most 8198 bytes, which may be stored as a 2-byte
       
 41964         ** varint. This information is used to attempt to avoid parsing 
       
 41965         ** the entire cell by checking for the cases where the record is 
       
 41966         ** stored entirely within the b-tree page by inspecting the first 
       
 41967         ** 2 bytes of the cell.
       
 41968         */
       
 41969         int nCell = pCell[0];
       
 41970         if( !(nCell & 0x80) && nCell<=pPage->maxLocal ){
       
 41971           /* This branch runs if the record-size field of the cell is a
       
 41972           ** single byte varint and the record fits entirely on the main
       
 41973           ** b-tree page.  */
       
 41974           c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
       
 41975         }else if( !(pCell[1] & 0x80) 
       
 41976           && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
       
 41977         ){
       
 41978           /* The record-size field is a 2 byte varint and the record 
       
 41979           ** fits entirely on the main b-tree page.  */
       
 41980           c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
       
 41981         }else{
       
 41982           /* The record flows over onto one or more overflow pages. In
       
 41983           ** this case the whole cell needs to be parsed, a buffer allocated
       
 41984           ** and accessPayload() used to retrieve the record into the
       
 41985           ** buffer before VdbeRecordCompare() can be called. */
       
 41986           void *pCellKey;
       
 41987           u8 * const pCellBody = pCell - pPage->childPtrSize;
       
 41988           btreeParseCellPtr(pPage, pCellBody, &pCur->info);
       
 41989           nCell = (int)pCur->info.nKey;
       
 41990           pCellKey = sqlite3Malloc( nCell );
       
 41991           if( pCellKey==0 ){
       
 41992             rc = SQLITE_NOMEM;
       
 41993             goto moveto_finish;
       
 41994           }
       
 41995           rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
       
 41996           if( rc ){
       
 41997             sqlite3_free(pCellKey);
       
 41998             goto moveto_finish;
       
 41999           }
       
 42000           c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
       
 42001           sqlite3_free(pCellKey);
       
 42002         }
       
 42003       }
       
 42004       if( c==0 ){
       
 42005         if( pPage->intKey && !pPage->leaf ){
       
 42006           lwr = idx;
       
 42007           upr = lwr - 1;
       
 42008           break;
       
 42009         }else{
       
 42010           *pRes = 0;
       
 42011           rc = SQLITE_OK;
       
 42012           goto moveto_finish;
       
 42013         }
       
 42014       }
       
 42015       if( c<0 ){
       
 42016         lwr = idx+1;
       
 42017       }else{
       
 42018         upr = idx-1;
       
 42019       }
       
 42020       if( lwr>upr ){
       
 42021         break;
       
 42022       }
       
 42023       pCur->aiIdx[pCur->iPage] = (u16)((lwr+upr)/2);
       
 42024     }
       
 42025     assert( lwr==upr+1 );
       
 42026     assert( pPage->isInit );
       
 42027     if( pPage->leaf ){
       
 42028       chldPg = 0;
       
 42029     }else if( lwr>=pPage->nCell ){
       
 42030       chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
       
 42031     }else{
       
 42032       chldPg = get4byte(findCell(pPage, lwr));
       
 42033     }
       
 42034     if( chldPg==0 ){
       
 42035       assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
       
 42036       *pRes = c;
       
 42037       rc = SQLITE_OK;
       
 42038       goto moveto_finish;
       
 42039     }
       
 42040     pCur->aiIdx[pCur->iPage] = (u16)lwr;
       
 42041     pCur->info.nSize = 0;
       
 42042     pCur->validNKey = 0;
       
 42043     rc = moveToChild(pCur, chldPg);
       
 42044     if( rc ) goto moveto_finish;
       
 42045   }
       
 42046 moveto_finish:
       
 42047   return rc;
       
 42048 }
       
 42049 
       
 42050 
       
 42051 /*
       
 42052 ** Return TRUE if the cursor is not pointing at an entry of the table.
       
 42053 **
       
 42054 ** TRUE will be returned after a call to sqlite3BtreeNext() moves
       
 42055 ** past the last entry in the table or sqlite3BtreePrev() moves past
       
 42056 ** the first entry.  TRUE is also returned if the table is empty.
       
 42057 */
       
 42058 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
       
 42059   /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
       
 42060   ** have been deleted? This API will need to change to return an error code
       
 42061   ** as well as the boolean result value.
       
 42062   */
       
 42063   return (CURSOR_VALID!=pCur->eState);
       
 42064 }
       
 42065 
       
 42066 /*
       
 42067 ** Advance the cursor to the next entry in the database.  If
       
 42068 ** successful then set *pRes=0.  If the cursor
       
 42069 ** was already pointing to the last entry in the database before
       
 42070 ** this routine was called, then set *pRes=1.
       
 42071 */
       
 42072 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
       
 42073   int rc;
       
 42074   int idx;
       
 42075   MemPage *pPage;
       
 42076 
       
 42077   assert( cursorHoldsMutex(pCur) );
       
 42078   rc = restoreCursorPosition(pCur);
       
 42079   if( rc!=SQLITE_OK ){
       
 42080     return rc;
       
 42081   }
       
 42082   assert( pRes!=0 );
       
 42083   if( CURSOR_INVALID==pCur->eState ){
       
 42084     *pRes = 1;
       
 42085     return SQLITE_OK;
       
 42086   }
       
 42087   if( pCur->skipNext>0 ){
       
 42088     pCur->skipNext = 0;
       
 42089     *pRes = 0;
       
 42090     return SQLITE_OK;
       
 42091   }
       
 42092   pCur->skipNext = 0;
       
 42093 
       
 42094   pPage = pCur->apPage[pCur->iPage];
       
 42095   idx = ++pCur->aiIdx[pCur->iPage];
       
 42096   assert( pPage->isInit );
       
 42097   assert( idx<=pPage->nCell );
       
 42098 
       
 42099   pCur->info.nSize = 0;
       
 42100   pCur->validNKey = 0;
       
 42101   if( idx>=pPage->nCell ){
       
 42102     if( !pPage->leaf ){
       
 42103       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
       
 42104       if( rc ) return rc;
       
 42105       rc = moveToLeftmost(pCur);
       
 42106       *pRes = 0;
       
 42107       return rc;
       
 42108     }
       
 42109     do{
       
 42110       if( pCur->iPage==0 ){
       
 42111         *pRes = 1;
       
 42112         pCur->eState = CURSOR_INVALID;
       
 42113         return SQLITE_OK;
       
 42114       }
       
 42115       moveToParent(pCur);
       
 42116       pPage = pCur->apPage[pCur->iPage];
       
 42117     }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
       
 42118     *pRes = 0;
       
 42119     if( pPage->intKey ){
       
 42120       rc = sqlite3BtreeNext(pCur, pRes);
       
 42121     }else{
       
 42122       rc = SQLITE_OK;
       
 42123     }
       
 42124     return rc;
       
 42125   }
       
 42126   *pRes = 0;
       
 42127   if( pPage->leaf ){
       
 42128     return SQLITE_OK;
       
 42129   }
       
 42130   rc = moveToLeftmost(pCur);
       
 42131   return rc;
       
 42132 }
       
 42133 
       
 42134 
       
 42135 /*
       
 42136 ** Step the cursor to the back to the previous entry in the database.  If
       
 42137 ** successful then set *pRes=0.  If the cursor
       
 42138 ** was already pointing to the first entry in the database before
       
 42139 ** this routine was called, then set *pRes=1.
       
 42140 */
       
 42141 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
       
 42142   int rc;
       
 42143   MemPage *pPage;
       
 42144 
       
 42145   assert( cursorHoldsMutex(pCur) );
       
 42146   rc = restoreCursorPosition(pCur);
       
 42147   if( rc!=SQLITE_OK ){
       
 42148     return rc;
       
 42149   }
       
 42150   pCur->atLast = 0;
       
 42151   if( CURSOR_INVALID==pCur->eState ){
       
 42152     *pRes = 1;
       
 42153     return SQLITE_OK;
       
 42154   }
       
 42155   if( pCur->skipNext<0 ){
       
 42156     pCur->skipNext = 0;
       
 42157     *pRes = 0;
       
 42158     return SQLITE_OK;
       
 42159   }
       
 42160   pCur->skipNext = 0;
       
 42161 
       
 42162   pPage = pCur->apPage[pCur->iPage];
       
 42163   assert( pPage->isInit );
       
 42164   if( !pPage->leaf ){
       
 42165     int idx = pCur->aiIdx[pCur->iPage];
       
 42166     rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
       
 42167     if( rc ){
       
 42168       return rc;
       
 42169     }
       
 42170     rc = moveToRightmost(pCur);
       
 42171   }else{
       
 42172     while( pCur->aiIdx[pCur->iPage]==0 ){
       
 42173       if( pCur->iPage==0 ){
       
 42174         pCur->eState = CURSOR_INVALID;
       
 42175         *pRes = 1;
       
 42176         return SQLITE_OK;
       
 42177       }
       
 42178       moveToParent(pCur);
       
 42179     }
       
 42180     pCur->info.nSize = 0;
       
 42181     pCur->validNKey = 0;
       
 42182 
       
 42183     pCur->aiIdx[pCur->iPage]--;
       
 42184     pPage = pCur->apPage[pCur->iPage];
       
 42185     if( pPage->intKey && !pPage->leaf ){
       
 42186       rc = sqlite3BtreePrevious(pCur, pRes);
       
 42187     }else{
       
 42188       rc = SQLITE_OK;
       
 42189     }
       
 42190   }
       
 42191   *pRes = 0;
       
 42192   return rc;
       
 42193 }
       
 42194 
       
 42195 /*
       
 42196 ** Allocate a new page from the database file.
       
 42197 **
       
 42198 ** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
       
 42199 ** has already been called on the new page.)  The new page has also
       
 42200 ** been referenced and the calling routine is responsible for calling
       
 42201 ** sqlite3PagerUnref() on the new page when it is done.
       
 42202 **
       
 42203 ** SQLITE_OK is returned on success.  Any other return value indicates
       
 42204 ** an error.  *ppPage and *pPgno are undefined in the event of an error.
       
 42205 ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
       
 42206 **
       
 42207 ** If the "nearby" parameter is not 0, then a (feeble) effort is made to 
       
 42208 ** locate a page close to the page number "nearby".  This can be used in an
       
 42209 ** attempt to keep related pages close to each other in the database file,
       
 42210 ** which in turn can make database access faster.
       
 42211 **
       
 42212 ** If the "exact" parameter is not 0, and the page-number nearby exists 
       
 42213 ** anywhere on the free-list, then it is guarenteed to be returned. This
       
 42214 ** is only used by auto-vacuum databases when allocating a new table.
       
 42215 */
       
 42216 static int allocateBtreePage(
       
 42217   BtShared *pBt, 
       
 42218   MemPage **ppPage, 
       
 42219   Pgno *pPgno, 
       
 42220   Pgno nearby,
       
 42221   u8 exact
       
 42222 ){
       
 42223   MemPage *pPage1;
       
 42224   int rc;
       
 42225   u32 n;     /* Number of pages on the freelist */
       
 42226   u32 k;     /* Number of leaves on the trunk of the freelist */
       
 42227   MemPage *pTrunk = 0;
       
 42228   MemPage *pPrevTrunk = 0;
       
 42229   Pgno mxPage;     /* Total size of the database file */
       
 42230 
       
 42231   assert( sqlite3_mutex_held(pBt->mutex) );
       
 42232   pPage1 = pBt->pPage1;
       
 42233   mxPage = pagerPagecount(pBt);
       
 42234   n = get4byte(&pPage1->aData[36]);
       
 42235   testcase( n==mxPage-1 );
       
 42236   if( n>=mxPage ){
       
 42237     return SQLITE_CORRUPT_BKPT;
       
 42238   }
       
 42239   if( n>0 ){
       
 42240     /* There are pages on the freelist.  Reuse one of those pages. */
       
 42241     Pgno iTrunk;
       
 42242     u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
       
 42243     
       
 42244     /* If the 'exact' parameter was true and a query of the pointer-map
       
 42245     ** shows that the page 'nearby' is somewhere on the free-list, then
       
 42246     ** the entire-list will be searched for that page.
       
 42247     */
       
 42248 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 42249     if( exact && nearby<=mxPage ){
       
 42250       u8 eType;
       
 42251       assert( nearby>0 );
       
 42252       assert( pBt->autoVacuum );
       
 42253       rc = ptrmapGet(pBt, nearby, &eType, 0);
       
 42254       if( rc ) return rc;
       
 42255       if( eType==PTRMAP_FREEPAGE ){
       
 42256         searchList = 1;
       
 42257       }
       
 42258       *pPgno = nearby;
       
 42259     }
       
 42260 #endif
       
 42261 
       
 42262     /* Decrement the free-list count by 1. Set iTrunk to the index of the
       
 42263     ** first free-list trunk page. iPrevTrunk is initially 1.
       
 42264     */
       
 42265     rc = sqlite3PagerWrite(pPage1->pDbPage);
       
 42266     if( rc ) return rc;
       
 42267     put4byte(&pPage1->aData[36], n-1);
       
 42268 
       
 42269     /* The code within this loop is run only once if the 'searchList' variable
       
 42270     ** is not true. Otherwise, it runs once for each trunk-page on the
       
 42271     ** free-list until the page 'nearby' is located.
       
 42272     */
       
 42273     do {
       
 42274       pPrevTrunk = pTrunk;
       
 42275       if( pPrevTrunk ){
       
 42276         iTrunk = get4byte(&pPrevTrunk->aData[0]);
       
 42277       }else{
       
 42278         iTrunk = get4byte(&pPage1->aData[32]);
       
 42279       }
       
 42280       testcase( iTrunk==mxPage );
       
 42281       if( iTrunk>mxPage ){
       
 42282         rc = SQLITE_CORRUPT_BKPT;
       
 42283       }else{
       
 42284         rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
       
 42285       }
       
 42286       if( rc ){
       
 42287         pTrunk = 0;
       
 42288         goto end_allocate_page;
       
 42289       }
       
 42290 
       
 42291       k = get4byte(&pTrunk->aData[4]);
       
 42292       if( k==0 && !searchList ){
       
 42293         /* The trunk has no leaves and the list is not being searched. 
       
 42294         ** So extract the trunk page itself and use it as the newly 
       
 42295         ** allocated page */
       
 42296         assert( pPrevTrunk==0 );
       
 42297         rc = sqlite3PagerWrite(pTrunk->pDbPage);
       
 42298         if( rc ){
       
 42299           goto end_allocate_page;
       
 42300         }
       
 42301         *pPgno = iTrunk;
       
 42302         memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
       
 42303         *ppPage = pTrunk;
       
 42304         pTrunk = 0;
       
 42305         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
       
 42306       }else if( k>(u32)(pBt->usableSize/4 - 2) ){
       
 42307         /* Value of k is out of range.  Database corruption */
       
 42308         rc = SQLITE_CORRUPT_BKPT;
       
 42309         goto end_allocate_page;
       
 42310 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 42311       }else if( searchList && nearby==iTrunk ){
       
 42312         /* The list is being searched and this trunk page is the page
       
 42313         ** to allocate, regardless of whether it has leaves.
       
 42314         */
       
 42315         assert( *pPgno==iTrunk );
       
 42316         *ppPage = pTrunk;
       
 42317         searchList = 0;
       
 42318         rc = sqlite3PagerWrite(pTrunk->pDbPage);
       
 42319         if( rc ){
       
 42320           goto end_allocate_page;
       
 42321         }
       
 42322         if( k==0 ){
       
 42323           if( !pPrevTrunk ){
       
 42324             memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
       
 42325           }else{
       
 42326             memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
       
 42327           }
       
 42328         }else{
       
 42329           /* The trunk page is required by the caller but it contains 
       
 42330           ** pointers to free-list leaves. The first leaf becomes a trunk
       
 42331           ** page in this case.
       
 42332           */
       
 42333           MemPage *pNewTrunk;
       
 42334           Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
       
 42335           if( iNewTrunk>mxPage ){ 
       
 42336             rc = SQLITE_CORRUPT_BKPT;
       
 42337             goto end_allocate_page;
       
 42338           }
       
 42339           testcase( iNewTrunk==mxPage );
       
 42340           rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
       
 42341           if( rc!=SQLITE_OK ){
       
 42342             goto end_allocate_page;
       
 42343           }
       
 42344           rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
       
 42345           if( rc!=SQLITE_OK ){
       
 42346             releasePage(pNewTrunk);
       
 42347             goto end_allocate_page;
       
 42348           }
       
 42349           memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
       
 42350           put4byte(&pNewTrunk->aData[4], k-1);
       
 42351           memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
       
 42352           releasePage(pNewTrunk);
       
 42353           if( !pPrevTrunk ){
       
 42354             assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
       
 42355             put4byte(&pPage1->aData[32], iNewTrunk);
       
 42356           }else{
       
 42357             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
       
 42358             if( rc ){
       
 42359               goto end_allocate_page;
       
 42360             }
       
 42361             put4byte(&pPrevTrunk->aData[0], iNewTrunk);
       
 42362           }
       
 42363         }
       
 42364         pTrunk = 0;
       
 42365         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
       
 42366 #endif
       
 42367       }else if( k>0 ){
       
 42368         /* Extract a leaf from the trunk */
       
 42369         u32 closest;
       
 42370         Pgno iPage;
       
 42371         unsigned char *aData = pTrunk->aData;
       
 42372         rc = sqlite3PagerWrite(pTrunk->pDbPage);
       
 42373         if( rc ){
       
 42374           goto end_allocate_page;
       
 42375         }
       
 42376         if( nearby>0 ){
       
 42377           u32 i;
       
 42378           int dist;
       
 42379           closest = 0;
       
 42380           dist = get4byte(&aData[8]) - nearby;
       
 42381           if( dist<0 ) dist = -dist;
       
 42382           for(i=1; i<k; i++){
       
 42383             int d2 = get4byte(&aData[8+i*4]) - nearby;
       
 42384             if( d2<0 ) d2 = -d2;
       
 42385             if( d2<dist ){
       
 42386               closest = i;
       
 42387               dist = d2;
       
 42388             }
       
 42389           }
       
 42390         }else{
       
 42391           closest = 0;
       
 42392         }
       
 42393 
       
 42394         iPage = get4byte(&aData[8+closest*4]);
       
 42395         testcase( iPage==mxPage );
       
 42396         if( iPage>mxPage ){
       
 42397           rc = SQLITE_CORRUPT_BKPT;
       
 42398           goto end_allocate_page;
       
 42399         }
       
 42400         testcase( iPage==mxPage );
       
 42401         if( !searchList || iPage==nearby ){
       
 42402           int noContent;
       
 42403           *pPgno = iPage;
       
 42404           TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
       
 42405                  ": %d more free pages\n",
       
 42406                  *pPgno, closest+1, k, pTrunk->pgno, n-1));
       
 42407           if( closest<k-1 ){
       
 42408             memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
       
 42409           }
       
 42410           put4byte(&aData[4], k-1);
       
 42411           assert( sqlite3PagerIswriteable(pTrunk->pDbPage) );
       
 42412           noContent = !btreeGetHasContent(pBt, *pPgno);
       
 42413           rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
       
 42414           if( rc==SQLITE_OK ){
       
 42415             rc = sqlite3PagerWrite((*ppPage)->pDbPage);
       
 42416             if( rc!=SQLITE_OK ){
       
 42417               releasePage(*ppPage);
       
 42418             }
       
 42419           }
       
 42420           searchList = 0;
       
 42421         }
       
 42422       }
       
 42423       releasePage(pPrevTrunk);
       
 42424       pPrevTrunk = 0;
       
 42425     }while( searchList );
       
 42426   }else{
       
 42427     /* There are no pages on the freelist, so create a new page at the
       
 42428     ** end of the file */
       
 42429     int nPage = pagerPagecount(pBt);
       
 42430     *pPgno = nPage + 1;
       
 42431 
       
 42432     if( *pPgno==PENDING_BYTE_PAGE(pBt) ){
       
 42433       (*pPgno)++;
       
 42434     }
       
 42435 
       
 42436 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 42437     if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){
       
 42438       /* If *pPgno refers to a pointer-map page, allocate two new pages
       
 42439       ** at the end of the file instead of one. The first allocated page
       
 42440       ** becomes a new pointer-map page, the second is used by the caller.
       
 42441       */
       
 42442       MemPage *pPg = 0;
       
 42443       TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
       
 42444       assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
       
 42445       rc = btreeGetPage(pBt, *pPgno, &pPg, 0);
       
 42446       if( rc==SQLITE_OK ){
       
 42447         rc = sqlite3PagerWrite(pPg->pDbPage);
       
 42448         releasePage(pPg);
       
 42449       }
       
 42450       if( rc ) return rc;
       
 42451       (*pPgno)++;
       
 42452       if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; }
       
 42453     }
       
 42454 #endif
       
 42455 
       
 42456     assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
       
 42457     rc = btreeGetPage(pBt, *pPgno, ppPage, 0);
       
 42458     if( rc ) return rc;
       
 42459     rc = sqlite3PagerWrite((*ppPage)->pDbPage);
       
 42460     if( rc!=SQLITE_OK ){
       
 42461       releasePage(*ppPage);
       
 42462     }
       
 42463     TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
       
 42464   }
       
 42465 
       
 42466   assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
       
 42467 
       
 42468 end_allocate_page:
       
 42469   releasePage(pTrunk);
       
 42470   releasePage(pPrevTrunk);
       
 42471   if( rc==SQLITE_OK ){
       
 42472     if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
       
 42473       releasePage(*ppPage);
       
 42474       return SQLITE_CORRUPT_BKPT;
       
 42475     }
       
 42476     (*ppPage)->isInit = 0;
       
 42477   }else{
       
 42478     *ppPage = 0;
       
 42479   }
       
 42480   return rc;
       
 42481 }
       
 42482 
       
 42483 /*
       
 42484 ** This function is used to add page iPage to the database file free-list. 
       
 42485 ** It is assumed that the page is not already a part of the free-list.
       
 42486 **
       
 42487 ** The value passed as the second argument to this function is optional.
       
 42488 ** If the caller happens to have a pointer to the MemPage object 
       
 42489 ** corresponding to page iPage handy, it may pass it as the second value. 
       
 42490 ** Otherwise, it may pass NULL.
       
 42491 **
       
 42492 ** If a pointer to a MemPage object is passed as the second argument,
       
 42493 ** its reference count is not altered by this function.
       
 42494 */
       
 42495 static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
       
 42496   MemPage *pTrunk = 0;                /* Free-list trunk page */
       
 42497   Pgno iTrunk = 0;                    /* Page number of free-list trunk page */ 
       
 42498   MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
       
 42499   MemPage *pPage;                     /* Page being freed. May be NULL. */
       
 42500   int rc;                             /* Return Code */
       
 42501   int nFree;                          /* Initial number of pages on free-list */
       
 42502 
       
 42503   assert( sqlite3_mutex_held(pBt->mutex) );
       
 42504   assert( iPage>1 );
       
 42505   assert( !pMemPage || pMemPage->pgno==iPage );
       
 42506 
       
 42507   if( pMemPage ){
       
 42508     pPage = pMemPage;
       
 42509     sqlite3PagerRef(pPage->pDbPage);
       
 42510   }else{
       
 42511     pPage = btreePageLookup(pBt, iPage);
       
 42512   }
       
 42513 
       
 42514   /* Increment the free page count on pPage1 */
       
 42515   rc = sqlite3PagerWrite(pPage1->pDbPage);
       
 42516   if( rc ) goto freepage_out;
       
 42517   nFree = get4byte(&pPage1->aData[36]);
       
 42518   put4byte(&pPage1->aData[36], nFree+1);
       
 42519 
       
 42520 #ifdef SQLITE_SECURE_DELETE
       
 42521   /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then
       
 42522   ** always fully overwrite deleted information with zeros.
       
 42523   */
       
 42524   if( (!pPage && (rc = btreeGetPage(pBt, iPage, &pPage, 0)))
       
 42525    ||            (rc = sqlite3PagerWrite(pPage->pDbPage))
       
 42526   ){
       
 42527     goto freepage_out;
       
 42528   }
       
 42529   memset(pPage->aData, 0, pPage->pBt->pageSize);
       
 42530 #endif
       
 42531 
       
 42532   /* If the database supports auto-vacuum, write an entry in the pointer-map
       
 42533   ** to indicate that the page is free.
       
 42534   */
       
 42535   if( ISAUTOVACUUM ){
       
 42536     ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
       
 42537     if( rc ) goto freepage_out;
       
 42538   }
       
 42539 
       
 42540   /* Now manipulate the actual database free-list structure. There are two
       
 42541   ** possibilities. If the free-list is currently empty, or if the first
       
 42542   ** trunk page in the free-list is full, then this page will become a
       
 42543   ** new free-list trunk page. Otherwise, it will become a leaf of the
       
 42544   ** first trunk page in the current free-list. This block tests if it
       
 42545   ** is possible to add the page as a new free-list leaf.
       
 42546   */
       
 42547   if( nFree!=0 ){
       
 42548     u32 nLeaf;                /* Initial number of leaf cells on trunk page */
       
 42549 
       
 42550     iTrunk = get4byte(&pPage1->aData[32]);
       
 42551     rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
       
 42552     if( rc!=SQLITE_OK ){
       
 42553       goto freepage_out;
       
 42554     }
       
 42555 
       
 42556     nLeaf = get4byte(&pTrunk->aData[4]);
       
 42557     assert( pBt->usableSize>32 );
       
 42558     if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
       
 42559       rc = SQLITE_CORRUPT_BKPT;
       
 42560       goto freepage_out;
       
 42561     }
       
 42562     if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
       
 42563       /* In this case there is room on the trunk page to insert the page
       
 42564       ** being freed as a new leaf.
       
 42565       **
       
 42566       ** Note that the trunk page is not really full until it contains
       
 42567       ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
       
 42568       ** coded.  But due to a coding error in versions of SQLite prior to
       
 42569       ** 3.6.0, databases with freelist trunk pages holding more than
       
 42570       ** usableSize/4 - 8 entries will be reported as corrupt.  In order
       
 42571       ** to maintain backwards compatibility with older versions of SQLite,
       
 42572       ** we will continue to restrict the number of entries to usableSize/4 - 8
       
 42573       ** for now.  At some point in the future (once everyone has upgraded
       
 42574       ** to 3.6.0 or later) we should consider fixing the conditional above
       
 42575       ** to read "usableSize/4-2" instead of "usableSize/4-8".
       
 42576       */
       
 42577       rc = sqlite3PagerWrite(pTrunk->pDbPage);
       
 42578       if( rc==SQLITE_OK ){
       
 42579         put4byte(&pTrunk->aData[4], nLeaf+1);
       
 42580         put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
       
 42581 #ifndef SQLITE_SECURE_DELETE
       
 42582         if( pPage ){
       
 42583           sqlite3PagerDontWrite(pPage->pDbPage);
       
 42584         }
       
 42585 #endif
       
 42586         rc = btreeSetHasContent(pBt, iPage);
       
 42587       }
       
 42588       TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
       
 42589       goto freepage_out;
       
 42590     }
       
 42591   }
       
 42592 
       
 42593   /* If control flows to this point, then it was not possible to add the
       
 42594   ** the page being freed as a leaf page of the first trunk in the free-list.
       
 42595   ** Possibly because the free-list is empty, or possibly because the 
       
 42596   ** first trunk in the free-list is full. Either way, the page being freed
       
 42597   ** will become the new first trunk page in the free-list.
       
 42598   */
       
 42599   if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
       
 42600     goto freepage_out;
       
 42601   }
       
 42602   rc = sqlite3PagerWrite(pPage->pDbPage);
       
 42603   if( rc!=SQLITE_OK ){
       
 42604     goto freepage_out;
       
 42605   }
       
 42606   put4byte(pPage->aData, iTrunk);
       
 42607   put4byte(&pPage->aData[4], 0);
       
 42608   put4byte(&pPage1->aData[32], iPage);
       
 42609   TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
       
 42610 
       
 42611 freepage_out:
       
 42612   if( pPage ){
       
 42613     pPage->isInit = 0;
       
 42614   }
       
 42615   releasePage(pPage);
       
 42616   releasePage(pTrunk);
       
 42617   return rc;
       
 42618 }
       
 42619 static void freePage(MemPage *pPage, int *pRC){
       
 42620   if( (*pRC)==SQLITE_OK ){
       
 42621     *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
       
 42622   }
       
 42623 }
       
 42624 
       
 42625 /*
       
 42626 ** Free any overflow pages associated with the given Cell.
       
 42627 */
       
 42628 static int clearCell(MemPage *pPage, unsigned char *pCell){
       
 42629   BtShared *pBt = pPage->pBt;
       
 42630   CellInfo info;
       
 42631   Pgno ovflPgno;
       
 42632   int rc;
       
 42633   int nOvfl;
       
 42634   u16 ovflPageSize;
       
 42635 
       
 42636   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 42637   btreeParseCellPtr(pPage, pCell, &info);
       
 42638   if( info.iOverflow==0 ){
       
 42639     return SQLITE_OK;  /* No overflow pages. Return without doing anything */
       
 42640   }
       
 42641   ovflPgno = get4byte(&pCell[info.iOverflow]);
       
 42642   assert( pBt->usableSize > 4 );
       
 42643   ovflPageSize = pBt->usableSize - 4;
       
 42644   nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
       
 42645   assert( ovflPgno==0 || nOvfl>0 );
       
 42646   while( nOvfl-- ){
       
 42647     Pgno iNext = 0;
       
 42648     MemPage *pOvfl = 0;
       
 42649     if( ovflPgno<2 || ovflPgno>pagerPagecount(pBt) ){
       
 42650       /* 0 is not a legal page number and page 1 cannot be an 
       
 42651       ** overflow page. Therefore if ovflPgno<2 or past the end of the 
       
 42652       ** file the database must be corrupt. */
       
 42653       return SQLITE_CORRUPT_BKPT;
       
 42654     }
       
 42655     if( nOvfl ){
       
 42656       rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
       
 42657       if( rc ) return rc;
       
 42658     }
       
 42659     rc = freePage2(pBt, pOvfl, ovflPgno);
       
 42660     if( pOvfl ){
       
 42661       sqlite3PagerUnref(pOvfl->pDbPage);
       
 42662     }
       
 42663     if( rc ) return rc;
       
 42664     ovflPgno = iNext;
       
 42665   }
       
 42666   return SQLITE_OK;
       
 42667 }
       
 42668 
       
 42669 /*
       
 42670 ** Create the byte sequence used to represent a cell on page pPage
       
 42671 ** and write that byte sequence into pCell[].  Overflow pages are
       
 42672 ** allocated and filled in as necessary.  The calling procedure
       
 42673 ** is responsible for making sure sufficient space has been allocated
       
 42674 ** for pCell[].
       
 42675 **
       
 42676 ** Note that pCell does not necessary need to point to the pPage->aData
       
 42677 ** area.  pCell might point to some temporary storage.  The cell will
       
 42678 ** be constructed in this temporary area then copied into pPage->aData
       
 42679 ** later.
       
 42680 */
       
 42681 static int fillInCell(
       
 42682   MemPage *pPage,                /* The page that contains the cell */
       
 42683   unsigned char *pCell,          /* Complete text of the cell */
       
 42684   const void *pKey, i64 nKey,    /* The key */
       
 42685   const void *pData,int nData,   /* The data */
       
 42686   int nZero,                     /* Extra zero bytes to append to pData */
       
 42687   int *pnSize                    /* Write cell size here */
       
 42688 ){
       
 42689   int nPayload;
       
 42690   const u8 *pSrc;
       
 42691   int nSrc, n, rc;
       
 42692   int spaceLeft;
       
 42693   MemPage *pOvfl = 0;
       
 42694   MemPage *pToRelease = 0;
       
 42695   unsigned char *pPrior;
       
 42696   unsigned char *pPayload;
       
 42697   BtShared *pBt = pPage->pBt;
       
 42698   Pgno pgnoOvfl = 0;
       
 42699   int nHeader;
       
 42700   CellInfo info;
       
 42701 
       
 42702   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 42703 
       
 42704   /* pPage is not necessarily writeable since pCell might be auxiliary
       
 42705   ** buffer space that is separate from the pPage buffer area */
       
 42706   assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
       
 42707             || sqlite3PagerIswriteable(pPage->pDbPage) );
       
 42708 
       
 42709   /* Fill in the header. */
       
 42710   nHeader = 0;
       
 42711   if( !pPage->leaf ){
       
 42712     nHeader += 4;
       
 42713   }
       
 42714   if( pPage->hasData ){
       
 42715     nHeader += putVarint(&pCell[nHeader], nData+nZero);
       
 42716   }else{
       
 42717     nData = nZero = 0;
       
 42718   }
       
 42719   nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
       
 42720   btreeParseCellPtr(pPage, pCell, &info);
       
 42721   assert( info.nHeader==nHeader );
       
 42722   assert( info.nKey==nKey );
       
 42723   assert( info.nData==(u32)(nData+nZero) );
       
 42724   
       
 42725   /* Fill in the payload */
       
 42726   nPayload = nData + nZero;
       
 42727   if( pPage->intKey ){
       
 42728     pSrc = pData;
       
 42729     nSrc = nData;
       
 42730     nData = 0;
       
 42731   }else{ 
       
 42732     if( NEVER(nKey>0x7fffffff || pKey==0) ){
       
 42733       return SQLITE_CORRUPT_BKPT;
       
 42734     }
       
 42735     nPayload += (int)nKey;
       
 42736     pSrc = pKey;
       
 42737     nSrc = (int)nKey;
       
 42738   }
       
 42739   *pnSize = info.nSize;
       
 42740   spaceLeft = info.nLocal;
       
 42741   pPayload = &pCell[nHeader];
       
 42742   pPrior = &pCell[info.iOverflow];
       
 42743 
       
 42744   while( nPayload>0 ){
       
 42745     if( spaceLeft==0 ){
       
 42746 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 42747       Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
       
 42748       if( pBt->autoVacuum ){
       
 42749         do{
       
 42750           pgnoOvfl++;
       
 42751         } while( 
       
 42752           PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) 
       
 42753         );
       
 42754       }
       
 42755 #endif
       
 42756       rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
       
 42757 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 42758       /* If the database supports auto-vacuum, and the second or subsequent
       
 42759       ** overflow page is being allocated, add an entry to the pointer-map
       
 42760       ** for that page now. 
       
 42761       **
       
 42762       ** If this is the first overflow page, then write a partial entry 
       
 42763       ** to the pointer-map. If we write nothing to this pointer-map slot,
       
 42764       ** then the optimistic overflow chain processing in clearCell()
       
 42765       ** may misinterpret the uninitialised values and delete the
       
 42766       ** wrong pages from the database.
       
 42767       */
       
 42768       if( pBt->autoVacuum && rc==SQLITE_OK ){
       
 42769         u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
       
 42770         ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
       
 42771         if( rc ){
       
 42772           releasePage(pOvfl);
       
 42773         }
       
 42774       }
       
 42775 #endif
       
 42776       if( rc ){
       
 42777         releasePage(pToRelease);
       
 42778         return rc;
       
 42779       }
       
 42780 
       
 42781       /* If pToRelease is not zero than pPrior points into the data area
       
 42782       ** of pToRelease.  Make sure pToRelease is still writeable. */
       
 42783       assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
       
 42784 
       
 42785       /* If pPrior is part of the data area of pPage, then make sure pPage
       
 42786       ** is still writeable */
       
 42787       assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
       
 42788             || sqlite3PagerIswriteable(pPage->pDbPage) );
       
 42789 
       
 42790       put4byte(pPrior, pgnoOvfl);
       
 42791       releasePage(pToRelease);
       
 42792       pToRelease = pOvfl;
       
 42793       pPrior = pOvfl->aData;
       
 42794       put4byte(pPrior, 0);
       
 42795       pPayload = &pOvfl->aData[4];
       
 42796       spaceLeft = pBt->usableSize - 4;
       
 42797     }
       
 42798     n = nPayload;
       
 42799     if( n>spaceLeft ) n = spaceLeft;
       
 42800 
       
 42801     /* If pToRelease is not zero than pPayload points into the data area
       
 42802     ** of pToRelease.  Make sure pToRelease is still writeable. */
       
 42803     assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
       
 42804 
       
 42805     /* If pPayload is part of the data area of pPage, then make sure pPage
       
 42806     ** is still writeable */
       
 42807     assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
       
 42808             || sqlite3PagerIswriteable(pPage->pDbPage) );
       
 42809 
       
 42810     if( nSrc>0 ){
       
 42811       if( n>nSrc ) n = nSrc;
       
 42812       assert( pSrc );
       
 42813       memcpy(pPayload, pSrc, n);
       
 42814     }else{
       
 42815       memset(pPayload, 0, n);
       
 42816     }
       
 42817     nPayload -= n;
       
 42818     pPayload += n;
       
 42819     pSrc += n;
       
 42820     nSrc -= n;
       
 42821     spaceLeft -= n;
       
 42822     if( nSrc==0 ){
       
 42823       nSrc = nData;
       
 42824       pSrc = pData;
       
 42825     }
       
 42826   }
       
 42827   releasePage(pToRelease);
       
 42828   return SQLITE_OK;
       
 42829 }
       
 42830 
       
 42831 /*
       
 42832 ** Remove the i-th cell from pPage.  This routine effects pPage only.
       
 42833 ** The cell content is not freed or deallocated.  It is assumed that
       
 42834 ** the cell content has been copied someplace else.  This routine just
       
 42835 ** removes the reference to the cell from pPage.
       
 42836 **
       
 42837 ** "sz" must be the number of bytes in the cell.
       
 42838 */
       
 42839 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
       
 42840   int i;          /* Loop counter */
       
 42841   int pc;         /* Offset to cell content of cell being deleted */
       
 42842   u8 *data;       /* pPage->aData */
       
 42843   u8 *ptr;        /* Used to move bytes around within data[] */
       
 42844   int rc;         /* The return code */
       
 42845   int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
       
 42846 
       
 42847   if( *pRC ) return;
       
 42848 
       
 42849   assert( idx>=0 && idx<pPage->nCell );
       
 42850   assert( sz==cellSize(pPage, idx) );
       
 42851   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
 42852   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 42853   data = pPage->aData;
       
 42854   ptr = &data[pPage->cellOffset + 2*idx];
       
 42855   pc = get2byte(ptr);
       
 42856   hdr = pPage->hdrOffset;
       
 42857   testcase( pc==get2byte(&data[hdr+5]) );
       
 42858   testcase( pc+sz==pPage->pBt->usableSize );
       
 42859   if( pc < get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
       
 42860     *pRC = SQLITE_CORRUPT_BKPT;
       
 42861     return;
       
 42862   }
       
 42863   rc = freeSpace(pPage, pc, sz);
       
 42864   if( rc ){
       
 42865     *pRC = rc;
       
 42866     return;
       
 42867   }
       
 42868   for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
       
 42869     ptr[0] = ptr[2];
       
 42870     ptr[1] = ptr[3];
       
 42871   }
       
 42872   pPage->nCell--;
       
 42873   put2byte(&data[hdr+3], pPage->nCell);
       
 42874   pPage->nFree += 2;
       
 42875 }
       
 42876 
       
 42877 /*
       
 42878 ** Insert a new cell on pPage at cell index "i".  pCell points to the
       
 42879 ** content of the cell.
       
 42880 **
       
 42881 ** If the cell content will fit on the page, then put it there.  If it
       
 42882 ** will not fit, then make a copy of the cell content into pTemp if
       
 42883 ** pTemp is not null.  Regardless of pTemp, allocate a new entry
       
 42884 ** in pPage->aOvfl[] and make it point to the cell content (either
       
 42885 ** in pTemp or the original pCell) and also record its index. 
       
 42886 ** Allocating a new entry in pPage->aCell[] implies that 
       
 42887 ** pPage->nOverflow is incremented.
       
 42888 **
       
 42889 ** If nSkip is non-zero, then do not copy the first nSkip bytes of the
       
 42890 ** cell. The caller will overwrite them after this function returns. If
       
 42891 ** nSkip is non-zero, then pCell may not point to an invalid memory location 
       
 42892 ** (but pCell+nSkip is always valid).
       
 42893 */
       
 42894 static void insertCell(
       
 42895   MemPage *pPage,   /* Page into which we are copying */
       
 42896   int i,            /* New cell becomes the i-th cell of the page */
       
 42897   u8 *pCell,        /* Content of the new cell */
       
 42898   int sz,           /* Bytes of content in pCell */
       
 42899   u8 *pTemp,        /* Temp storage space for pCell, if needed */
       
 42900   Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
       
 42901   int *pRC          /* Read and write return code from here */
       
 42902 ){
       
 42903   int idx;          /* Where to write new cell content in data[] */
       
 42904   int j;            /* Loop counter */
       
 42905   int end;          /* First byte past the last cell pointer in data[] */
       
 42906   int ins;          /* Index in data[] where new cell pointer is inserted */
       
 42907   int cellOffset;   /* Address of first cell pointer in data[] */
       
 42908   u8 *data;         /* The content of the whole page */
       
 42909   u8 *ptr;          /* Used for moving information around in data[] */
       
 42910 
       
 42911   int nSkip = (iChild ? 4 : 0);
       
 42912 
       
 42913   if( *pRC ) return;
       
 42914 
       
 42915   assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
       
 42916   assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 );
       
 42917   assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
       
 42918   assert( sz==cellSizePtr(pPage, pCell) );
       
 42919   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 42920   if( pPage->nOverflow || sz+2>pPage->nFree ){
       
 42921     if( pTemp ){
       
 42922       memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
       
 42923       pCell = pTemp;
       
 42924     }
       
 42925     if( iChild ){
       
 42926       put4byte(pCell, iChild);
       
 42927     }
       
 42928     j = pPage->nOverflow++;
       
 42929     assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) );
       
 42930     pPage->aOvfl[j].pCell = pCell;
       
 42931     pPage->aOvfl[j].idx = (u16)i;
       
 42932   }else{
       
 42933     int rc = sqlite3PagerWrite(pPage->pDbPage);
       
 42934     if( rc!=SQLITE_OK ){
       
 42935       *pRC = rc;
       
 42936       return;
       
 42937     }
       
 42938     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
 42939     data = pPage->aData;
       
 42940     cellOffset = pPage->cellOffset;
       
 42941     end = cellOffset + 2*pPage->nCell;
       
 42942     ins = cellOffset + 2*i;
       
 42943     rc = allocateSpace(pPage, sz, &idx);
       
 42944     if( rc ){ *pRC = rc; return; }
       
 42945     /* The allocateSpace() routine guarantees the following two properties
       
 42946     ** if it returns success */
       
 42947     assert( idx >= end+2 );
       
 42948     assert( idx+sz <= pPage->pBt->usableSize );
       
 42949     pPage->nCell++;
       
 42950     pPage->nFree -= (u16)(2 + sz);
       
 42951     memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
       
 42952     if( iChild ){
       
 42953       put4byte(&data[idx], iChild);
       
 42954     }
       
 42955     for(j=end, ptr=&data[j]; j>ins; j-=2, ptr-=2){
       
 42956       ptr[0] = ptr[-2];
       
 42957       ptr[1] = ptr[-1];
       
 42958     }
       
 42959     put2byte(&data[ins], idx);
       
 42960     put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
       
 42961 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 42962     if( pPage->pBt->autoVacuum ){
       
 42963       /* The cell may contain a pointer to an overflow page. If so, write
       
 42964       ** the entry for the overflow page into the pointer map.
       
 42965       */
       
 42966       ptrmapPutOvflPtr(pPage, pCell, pRC);
       
 42967     }
       
 42968 #endif
       
 42969   }
       
 42970 }
       
 42971 
       
 42972 /*
       
 42973 ** Add a list of cells to a page.  The page should be initially empty.
       
 42974 ** The cells are guaranteed to fit on the page.
       
 42975 */
       
 42976 static void assemblePage(
       
 42977   MemPage *pPage,   /* The page to be assemblied */
       
 42978   int nCell,        /* The number of cells to add to this page */
       
 42979   u8 **apCell,      /* Pointers to cell bodies */
       
 42980   u16 *aSize        /* Sizes of the cells */
       
 42981 ){
       
 42982   int i;            /* Loop counter */
       
 42983   u8 *pCellptr;     /* Address of next cell pointer */
       
 42984   int cellbody;     /* Address of next cell body */
       
 42985   u8 * const data = pPage->aData;             /* Pointer to data for pPage */
       
 42986   const int hdr = pPage->hdrOffset;           /* Offset of header on pPage */
       
 42987   const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
       
 42988 
       
 42989   assert( pPage->nOverflow==0 );
       
 42990   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 42991   assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 );
       
 42992   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
 42993 
       
 42994   /* Check that the page has just been zeroed by zeroPage() */
       
 42995   assert( pPage->nCell==0 );
       
 42996   assert( get2byte(&data[hdr+5])==nUsable );
       
 42997 
       
 42998   pCellptr = &data[pPage->cellOffset + nCell*2];
       
 42999   cellbody = nUsable;
       
 43000   for(i=nCell-1; i>=0; i--){
       
 43001     pCellptr -= 2;
       
 43002     cellbody -= aSize[i];
       
 43003     put2byte(pCellptr, cellbody);
       
 43004     memcpy(&data[cellbody], apCell[i], aSize[i]);
       
 43005   }
       
 43006   put2byte(&data[hdr+3], nCell);
       
 43007   put2byte(&data[hdr+5], cellbody);
       
 43008   pPage->nFree -= (nCell*2 + nUsable - cellbody);
       
 43009   pPage->nCell = (u16)nCell;
       
 43010 }
       
 43011 
       
 43012 /*
       
 43013 ** The following parameters determine how many adjacent pages get involved
       
 43014 ** in a balancing operation.  NN is the number of neighbors on either side
       
 43015 ** of the page that participate in the balancing operation.  NB is the
       
 43016 ** total number of pages that participate, including the target page and
       
 43017 ** NN neighbors on either side.
       
 43018 **
       
 43019 ** The minimum value of NN is 1 (of course).  Increasing NN above 1
       
 43020 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
       
 43021 ** in exchange for a larger degradation in INSERT and UPDATE performance.
       
 43022 ** The value of NN appears to give the best results overall.
       
 43023 */
       
 43024 #define NN 1             /* Number of neighbors on either side of pPage */
       
 43025 #define NB (NN*2+1)      /* Total pages involved in the balance */
       
 43026 
       
 43027 
       
 43028 #ifndef SQLITE_OMIT_QUICKBALANCE
       
 43029 /*
       
 43030 ** This version of balance() handles the common special case where
       
 43031 ** a new entry is being inserted on the extreme right-end of the
       
 43032 ** tree, in other words, when the new entry will become the largest
       
 43033 ** entry in the tree.
       
 43034 **
       
 43035 ** Instead of trying to balance the 3 right-most leaf pages, just add
       
 43036 ** a new page to the right-hand side and put the one new entry in
       
 43037 ** that page.  This leaves the right side of the tree somewhat
       
 43038 ** unbalanced.  But odds are that we will be inserting new entries
       
 43039 ** at the end soon afterwards so the nearly empty page will quickly
       
 43040 ** fill up.  On average.
       
 43041 **
       
 43042 ** pPage is the leaf page which is the right-most page in the tree.
       
 43043 ** pParent is its parent.  pPage must have a single overflow entry
       
 43044 ** which is also the right-most entry on the page.
       
 43045 **
       
 43046 ** The pSpace buffer is used to store a temporary copy of the divider
       
 43047 ** cell that will be inserted into pParent. Such a cell consists of a 4
       
 43048 ** byte page number followed by a variable length integer. In other
       
 43049 ** words, at most 13 bytes. Hence the pSpace buffer must be at
       
 43050 ** least 13 bytes in size.
       
 43051 */
       
 43052 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
       
 43053   BtShared *const pBt = pPage->pBt;    /* B-Tree Database */
       
 43054   MemPage *pNew;                       /* Newly allocated page */
       
 43055   int rc;                              /* Return Code */
       
 43056   Pgno pgnoNew;                        /* Page number of pNew */
       
 43057 
       
 43058   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
 43059   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
       
 43060   assert( pPage->nOverflow==1 );
       
 43061 
       
 43062   if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT;
       
 43063 
       
 43064   /* Allocate a new page. This page will become the right-sibling of 
       
 43065   ** pPage. Make the parent page writable, so that the new divider cell
       
 43066   ** may be inserted. If both these operations are successful, proceed.
       
 43067   */
       
 43068   rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
       
 43069 
       
 43070   if( rc==SQLITE_OK ){
       
 43071 
       
 43072     u8 *pOut = &pSpace[4];
       
 43073     u8 *pCell = pPage->aOvfl[0].pCell;
       
 43074     u16 szCell = cellSizePtr(pPage, pCell);
       
 43075     u8 *pStop;
       
 43076 
       
 43077     assert( sqlite3PagerIswriteable(pNew->pDbPage) );
       
 43078     assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
       
 43079     zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
       
 43080     assemblePage(pNew, 1, &pCell, &szCell);
       
 43081 
       
 43082     /* If this is an auto-vacuum database, update the pointer map
       
 43083     ** with entries for the new page, and any pointer from the 
       
 43084     ** cell on the page to an overflow page. If either of these
       
 43085     ** operations fails, the return code is set, but the contents
       
 43086     ** of the parent page are still manipulated by thh code below.
       
 43087     ** That is Ok, at this point the parent page is guaranteed to
       
 43088     ** be marked as dirty. Returning an error code will cause a
       
 43089     ** rollback, undoing any changes made to the parent page.
       
 43090     */
       
 43091     if( ISAUTOVACUUM ){
       
 43092       ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
       
 43093       if( szCell>pNew->minLocal ){
       
 43094         ptrmapPutOvflPtr(pNew, pCell, &rc);
       
 43095       }
       
 43096     }
       
 43097   
       
 43098     /* Create a divider cell to insert into pParent. The divider cell
       
 43099     ** consists of a 4-byte page number (the page number of pPage) and
       
 43100     ** a variable length key value (which must be the same value as the
       
 43101     ** largest key on pPage).
       
 43102     **
       
 43103     ** To find the largest key value on pPage, first find the right-most 
       
 43104     ** cell on pPage. The first two fields of this cell are the 
       
 43105     ** record-length (a variable length integer at most 32-bits in size)
       
 43106     ** and the key value (a variable length integer, may have any value).
       
 43107     ** The first of the while(...) loops below skips over the record-length
       
 43108     ** field. The second while(...) loop copies the key value from the
       
 43109     ** cell on pPage into the pSpace buffer.
       
 43110     */
       
 43111     pCell = findCell(pPage, pPage->nCell-1);
       
 43112     pStop = &pCell[9];
       
 43113     while( (*(pCell++)&0x80) && pCell<pStop );
       
 43114     pStop = &pCell[9];
       
 43115     while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
       
 43116 
       
 43117     /* Insert the new divider cell into pParent. */
       
 43118     insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
       
 43119                0, pPage->pgno, &rc);
       
 43120 
       
 43121     /* Set the right-child pointer of pParent to point to the new page. */
       
 43122     put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
       
 43123   
       
 43124     /* Release the reference to the new page. */
       
 43125     releasePage(pNew);
       
 43126   }
       
 43127 
       
 43128   return rc;
       
 43129 }
       
 43130 #endif /* SQLITE_OMIT_QUICKBALANCE */
       
 43131 
       
 43132 #if 0
       
 43133 /*
       
 43134 ** This function does not contribute anything to the operation of SQLite.
       
 43135 ** it is sometimes activated temporarily while debugging code responsible 
       
 43136 ** for setting pointer-map entries.
       
 43137 */
       
 43138 static int ptrmapCheckPages(MemPage **apPage, int nPage){
       
 43139   int i, j;
       
 43140   for(i=0; i<nPage; i++){
       
 43141     Pgno n;
       
 43142     u8 e;
       
 43143     MemPage *pPage = apPage[i];
       
 43144     BtShared *pBt = pPage->pBt;
       
 43145     assert( pPage->isInit );
       
 43146 
       
 43147     for(j=0; j<pPage->nCell; j++){
       
 43148       CellInfo info;
       
 43149       u8 *z;
       
 43150      
       
 43151       z = findCell(pPage, j);
       
 43152       btreeParseCellPtr(pPage, z, &info);
       
 43153       if( info.iOverflow ){
       
 43154         Pgno ovfl = get4byte(&z[info.iOverflow]);
       
 43155         ptrmapGet(pBt, ovfl, &e, &n);
       
 43156         assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
       
 43157       }
       
 43158       if( !pPage->leaf ){
       
 43159         Pgno child = get4byte(z);
       
 43160         ptrmapGet(pBt, child, &e, &n);
       
 43161         assert( n==pPage->pgno && e==PTRMAP_BTREE );
       
 43162       }
       
 43163     }
       
 43164     if( !pPage->leaf ){
       
 43165       Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
       
 43166       ptrmapGet(pBt, child, &e, &n);
       
 43167       assert( n==pPage->pgno && e==PTRMAP_BTREE );
       
 43168     }
       
 43169   }
       
 43170   return 1;
       
 43171 }
       
 43172 #endif
       
 43173 
       
 43174 /*
       
 43175 ** This function is used to copy the contents of the b-tree node stored 
       
 43176 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
       
 43177 ** the pointer-map entries for each child page are updated so that the
       
 43178 ** parent page stored in the pointer map is page pTo. If pFrom contained
       
 43179 ** any cells with overflow page pointers, then the corresponding pointer
       
 43180 ** map entries are also updated so that the parent page is page pTo.
       
 43181 **
       
 43182 ** If pFrom is currently carrying any overflow cells (entries in the
       
 43183 ** MemPage.aOvfl[] array), they are not copied to pTo. 
       
 43184 **
       
 43185 ** Before returning, page pTo is reinitialized using btreeInitPage().
       
 43186 **
       
 43187 ** The performance of this function is not critical. It is only used by 
       
 43188 ** the balance_shallower() and balance_deeper() procedures, neither of
       
 43189 ** which are called often under normal circumstances.
       
 43190 */
       
 43191 static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
       
 43192   if( (*pRC)==SQLITE_OK ){
       
 43193     BtShared * const pBt = pFrom->pBt;
       
 43194     u8 * const aFrom = pFrom->aData;
       
 43195     u8 * const aTo = pTo->aData;
       
 43196     int const iFromHdr = pFrom->hdrOffset;
       
 43197     int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
       
 43198     TESTONLY(int rc;)
       
 43199     int iData;
       
 43200   
       
 43201   
       
 43202     assert( pFrom->isInit );
       
 43203     assert( pFrom->nFree>=iToHdr );
       
 43204     assert( get2byte(&aFrom[iFromHdr+5])<=pBt->usableSize );
       
 43205   
       
 43206     /* Copy the b-tree node content from page pFrom to page pTo. */
       
 43207     iData = get2byte(&aFrom[iFromHdr+5]);
       
 43208     memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
       
 43209     memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
       
 43210   
       
 43211     /* Reinitialize page pTo so that the contents of the MemPage structure
       
 43212     ** match the new data. The initialization of pTo "cannot" fail, as the
       
 43213     ** data copied from pFrom is known to be valid.  */
       
 43214     pTo->isInit = 0;
       
 43215     TESTONLY(rc = ) btreeInitPage(pTo);
       
 43216     assert( rc==SQLITE_OK );
       
 43217   
       
 43218     /* If this is an auto-vacuum database, update the pointer-map entries
       
 43219     ** for any b-tree or overflow pages that pTo now contains the pointers to.
       
 43220     */
       
 43221     if( ISAUTOVACUUM ){
       
 43222       *pRC = setChildPtrmaps(pTo);
       
 43223     }
       
 43224   }
       
 43225 }
       
 43226 
       
 43227 /*
       
 43228 ** This routine redistributes cells on the iParentIdx'th child of pParent
       
 43229 ** (hereafter "the page") and up to 2 siblings so that all pages have about the
       
 43230 ** same amount of free space. Usually a single sibling on either side of the
       
 43231 ** page are used in the balancing, though both siblings might come from one
       
 43232 ** side if the page is the first or last child of its parent. If the page 
       
 43233 ** has fewer than 2 siblings (something which can only happen if the page
       
 43234 ** is a root page or a child of a root page) then all available siblings
       
 43235 ** participate in the balancing.
       
 43236 **
       
 43237 ** The number of siblings of the page might be increased or decreased by 
       
 43238 ** one or two in an effort to keep pages nearly full but not over full. 
       
 43239 **
       
 43240 ** Note that when this routine is called, some of the cells on the page
       
 43241 ** might not actually be stored in MemPage.aData[]. This can happen
       
 43242 ** if the page is overfull. This routine ensures that all cells allocated
       
 43243 ** to the page and its siblings fit into MemPage.aData[] before returning.
       
 43244 **
       
 43245 ** In the course of balancing the page and its siblings, cells may be
       
 43246 ** inserted into or removed from the parent page (pParent). Doing so
       
 43247 ** may cause the parent page to become overfull or underfull. If this
       
 43248 ** happens, it is the responsibility of the caller to invoke the correct
       
 43249 ** balancing routine to fix this problem (see the balance() routine). 
       
 43250 **
       
 43251 ** If this routine fails for any reason, it might leave the database
       
 43252 ** in a corrupted state. So if this routine fails, the database should
       
 43253 ** be rolled back.
       
 43254 **
       
 43255 ** The third argument to this function, aOvflSpace, is a pointer to a
       
 43256 ** buffer big enough to hold one page. If while inserting cells into the parent
       
 43257 ** page (pParent) the parent page becomes overfull, this buffer is
       
 43258 ** used to store the parent's overflow cells. Because this function inserts
       
 43259 ** a maximum of four divider cells into the parent page, and the maximum
       
 43260 ** size of a cell stored within an internal node is always less than 1/4
       
 43261 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
       
 43262 ** enough for all overflow cells.
       
 43263 **
       
 43264 ** If aOvflSpace is set to a null pointer, this function returns 
       
 43265 ** SQLITE_NOMEM.
       
 43266 */
       
 43267 static int balance_nonroot(
       
 43268   MemPage *pParent,               /* Parent page of siblings being balanced */
       
 43269   int iParentIdx,                 /* Index of "the page" in pParent */
       
 43270   u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
       
 43271   int isRoot                      /* True if pParent is a root-page */
       
 43272 ){
       
 43273   BtShared *pBt;               /* The whole database */
       
 43274   int nCell = 0;               /* Number of cells in apCell[] */
       
 43275   int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
       
 43276   int nNew = 0;                /* Number of pages in apNew[] */
       
 43277   int nOld;                    /* Number of pages in apOld[] */
       
 43278   int i, j, k;                 /* Loop counters */
       
 43279   int nxDiv;                   /* Next divider slot in pParent->aCell[] */
       
 43280   int rc = SQLITE_OK;          /* The return code */
       
 43281   u16 leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
       
 43282   int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
       
 43283   int usableSpace;             /* Bytes in pPage beyond the header */
       
 43284   int pageFlags;               /* Value of pPage->aData[0] */
       
 43285   int subtotal;                /* Subtotal of bytes in cells on one page */
       
 43286   int iSpace1 = 0;             /* First unused byte of aSpace1[] */
       
 43287   int iOvflSpace = 0;          /* First unused byte of aOvflSpace[] */
       
 43288   int szScratch;               /* Size of scratch memory requested */
       
 43289   MemPage *apOld[NB];          /* pPage and up to two siblings */
       
 43290   MemPage *apCopy[NB];         /* Private copies of apOld[] pages */
       
 43291   MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
       
 43292   u8 *pRight;                  /* Location in parent of right-sibling pointer */
       
 43293   u8 *apDiv[NB-1];             /* Divider cells in pParent */
       
 43294   int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
       
 43295   int szNew[NB+2];             /* Combined size of cells place on i-th page */
       
 43296   u8 **apCell = 0;             /* All cells begin balanced */
       
 43297   u16 *szCell;                 /* Local size of all cells in apCell[] */
       
 43298   u8 *aSpace1;                 /* Space for copies of dividers cells */
       
 43299   Pgno pgno;                   /* Temp var to store a page number in */
       
 43300 
       
 43301   pBt = pParent->pBt;
       
 43302   assert( sqlite3_mutex_held(pBt->mutex) );
       
 43303   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
       
 43304 
       
 43305 #if 0
       
 43306   TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
       
 43307 #endif
       
 43308 
       
 43309   /* At this point pParent may have at most one overflow cell. And if
       
 43310   ** this overflow cell is present, it must be the cell with 
       
 43311   ** index iParentIdx. This scenario comes about when this function
       
 43312   ** is called (indirectly) from sqlite3BtreeDelete().
       
 43313   */
       
 43314   assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
       
 43315   assert( pParent->nOverflow==0 || pParent->aOvfl[0].idx==iParentIdx );
       
 43316 
       
 43317   if( !aOvflSpace ){
       
 43318     return SQLITE_NOMEM;
       
 43319   }
       
 43320 
       
 43321   /* Find the sibling pages to balance. Also locate the cells in pParent 
       
 43322   ** that divide the siblings. An attempt is made to find NN siblings on 
       
 43323   ** either side of pPage. More siblings are taken from one side, however, 
       
 43324   ** if there are fewer than NN siblings on the other side. If pParent
       
 43325   ** has NB or fewer children then all children of pParent are taken.  
       
 43326   **
       
 43327   ** This loop also drops the divider cells from the parent page. This
       
 43328   ** way, the remainder of the function does not have to deal with any
       
 43329   ** overflow cells in the parent page, since if any existed they will
       
 43330   ** have already been removed.
       
 43331   */
       
 43332   i = pParent->nOverflow + pParent->nCell;
       
 43333   if( i<2 ){
       
 43334     nxDiv = 0;
       
 43335     nOld = i+1;
       
 43336   }else{
       
 43337     nOld = 3;
       
 43338     if( iParentIdx==0 ){                 
       
 43339       nxDiv = 0;
       
 43340     }else if( iParentIdx==i ){
       
 43341       nxDiv = i-2;
       
 43342     }else{
       
 43343       nxDiv = iParentIdx-1;
       
 43344     }
       
 43345     i = 2;
       
 43346   }
       
 43347   if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
       
 43348     pRight = &pParent->aData[pParent->hdrOffset+8];
       
 43349   }else{
       
 43350     pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
       
 43351   }
       
 43352   pgno = get4byte(pRight);
       
 43353   while( 1 ){
       
 43354     rc = getAndInitPage(pBt, pgno, &apOld[i]);
       
 43355     if( rc ){
       
 43356       memset(apOld, 0, (i+1)*sizeof(MemPage*));
       
 43357       goto balance_cleanup;
       
 43358     }
       
 43359     nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
       
 43360     if( (i--)==0 ) break;
       
 43361 
       
 43362     if( i+nxDiv==pParent->aOvfl[0].idx && pParent->nOverflow ){
       
 43363       apDiv[i] = pParent->aOvfl[0].pCell;
       
 43364       pgno = get4byte(apDiv[i]);
       
 43365       szNew[i] = cellSizePtr(pParent, apDiv[i]);
       
 43366       pParent->nOverflow = 0;
       
 43367     }else{
       
 43368       apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
       
 43369       pgno = get4byte(apDiv[i]);
       
 43370       szNew[i] = cellSizePtr(pParent, apDiv[i]);
       
 43371 
       
 43372       /* Drop the cell from the parent page. apDiv[i] still points to
       
 43373       ** the cell within the parent, even though it has been dropped.
       
 43374       ** This is safe because dropping a cell only overwrites the first
       
 43375       ** four bytes of it, and this function does not need the first
       
 43376       ** four bytes of the divider cell. So the pointer is safe to use
       
 43377       ** later on.  
       
 43378       **
       
 43379       ** Unless SQLite is compiled in secure-delete mode. In this case,
       
 43380       ** the dropCell() routine will overwrite the entire cell with zeroes.
       
 43381       ** In this case, temporarily copy the cell into the aOvflSpace[]
       
 43382       ** buffer. It will be copied out again as soon as the aSpace[] buffer
       
 43383       ** is allocated.  */
       
 43384 #ifdef SQLITE_SECURE_DELETE
       
 43385       memcpy(&aOvflSpace[apDiv[i]-pParent->aData], apDiv[i], szNew[i]);
       
 43386       apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
       
 43387 #endif
       
 43388       dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
       
 43389     }
       
 43390   }
       
 43391 
       
 43392   /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
       
 43393   ** alignment */
       
 43394   nMaxCells = (nMaxCells + 3)&~3;
       
 43395 
       
 43396   /*
       
 43397   ** Allocate space for memory structures
       
 43398   */
       
 43399   k = pBt->pageSize + ROUND8(sizeof(MemPage));
       
 43400   szScratch =
       
 43401        nMaxCells*sizeof(u8*)                       /* apCell */
       
 43402      + nMaxCells*sizeof(u16)                       /* szCell */
       
 43403      + pBt->pageSize                               /* aSpace1 */
       
 43404      + k*nOld;                                     /* Page copies (apCopy) */
       
 43405   apCell = sqlite3ScratchMalloc( szScratch ); 
       
 43406   if( apCell==0 ){
       
 43407     rc = SQLITE_NOMEM;
       
 43408     goto balance_cleanup;
       
 43409   }
       
 43410   szCell = (u16*)&apCell[nMaxCells];
       
 43411   aSpace1 = (u8*)&szCell[nMaxCells];
       
 43412   assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
       
 43413 
       
 43414   /*
       
 43415   ** Load pointers to all cells on sibling pages and the divider cells
       
 43416   ** into the local apCell[] array.  Make copies of the divider cells
       
 43417   ** into space obtained from aSpace1[] and remove the the divider Cells
       
 43418   ** from pParent.
       
 43419   **
       
 43420   ** If the siblings are on leaf pages, then the child pointers of the
       
 43421   ** divider cells are stripped from the cells before they are copied
       
 43422   ** into aSpace1[].  In this way, all cells in apCell[] are without
       
 43423   ** child pointers.  If siblings are not leaves, then all cell in
       
 43424   ** apCell[] include child pointers.  Either way, all cells in apCell[]
       
 43425   ** are alike.
       
 43426   **
       
 43427   ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
       
 43428   **       leafData:  1 if pPage holds key+data and pParent holds only keys.
       
 43429   */
       
 43430   leafCorrection = apOld[0]->leaf*4;
       
 43431   leafData = apOld[0]->hasData;
       
 43432   for(i=0; i<nOld; i++){
       
 43433     int limit;
       
 43434     
       
 43435     /* Before doing anything else, take a copy of the i'th original sibling
       
 43436     ** The rest of this function will use data from the copies rather
       
 43437     ** that the original pages since the original pages will be in the
       
 43438     ** process of being overwritten.  */
       
 43439     MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
       
 43440     memcpy(pOld, apOld[i], sizeof(MemPage));
       
 43441     pOld->aData = (void*)&pOld[1];
       
 43442     memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
       
 43443 
       
 43444     limit = pOld->nCell+pOld->nOverflow;
       
 43445     for(j=0; j<limit; j++){
       
 43446       assert( nCell<nMaxCells );
       
 43447       apCell[nCell] = findOverflowCell(pOld, j);
       
 43448       szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
       
 43449       nCell++;
       
 43450     }
       
 43451     if( i<nOld-1 && !leafData){
       
 43452       u16 sz = (u16)szNew[i];
       
 43453       u8 *pTemp;
       
 43454       assert( nCell<nMaxCells );
       
 43455       szCell[nCell] = sz;
       
 43456       pTemp = &aSpace1[iSpace1];
       
 43457       iSpace1 += sz;
       
 43458       assert( sz<=pBt->pageSize/4 );
       
 43459       assert( iSpace1<=pBt->pageSize );
       
 43460       memcpy(pTemp, apDiv[i], sz);
       
 43461       apCell[nCell] = pTemp+leafCorrection;
       
 43462       assert( leafCorrection==0 || leafCorrection==4 );
       
 43463       szCell[nCell] = szCell[nCell] - leafCorrection;
       
 43464       if( !pOld->leaf ){
       
 43465         assert( leafCorrection==0 );
       
 43466         assert( pOld->hdrOffset==0 );
       
 43467         /* The right pointer of the child page pOld becomes the left
       
 43468         ** pointer of the divider cell */
       
 43469         memcpy(apCell[nCell], &pOld->aData[8], 4);
       
 43470       }else{
       
 43471         assert( leafCorrection==4 );
       
 43472         if( szCell[nCell]<4 ){
       
 43473           /* Do not allow any cells smaller than 4 bytes. */
       
 43474           szCell[nCell] = 4;
       
 43475         }
       
 43476       }
       
 43477       nCell++;
       
 43478     }
       
 43479   }
       
 43480 
       
 43481   /*
       
 43482   ** Figure out the number of pages needed to hold all nCell cells.
       
 43483   ** Store this number in "k".  Also compute szNew[] which is the total
       
 43484   ** size of all cells on the i-th page and cntNew[] which is the index
       
 43485   ** in apCell[] of the cell that divides page i from page i+1.  
       
 43486   ** cntNew[k] should equal nCell.
       
 43487   **
       
 43488   ** Values computed by this block:
       
 43489   **
       
 43490   **           k: The total number of sibling pages
       
 43491   **    szNew[i]: Spaced used on the i-th sibling page.
       
 43492   **   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
       
 43493   **              the right of the i-th sibling page.
       
 43494   ** usableSpace: Number of bytes of space available on each sibling.
       
 43495   ** 
       
 43496   */
       
 43497   usableSpace = pBt->usableSize - 12 + leafCorrection;
       
 43498   for(subtotal=k=i=0; i<nCell; i++){
       
 43499     assert( i<nMaxCells );
       
 43500     subtotal += szCell[i] + 2;
       
 43501     if( subtotal > usableSpace ){
       
 43502       szNew[k] = subtotal - szCell[i];
       
 43503       cntNew[k] = i;
       
 43504       if( leafData ){ i--; }
       
 43505       subtotal = 0;
       
 43506       k++;
       
 43507       if( k>NB+1 ){ rc = SQLITE_CORRUPT; goto balance_cleanup; }
       
 43508     }
       
 43509   }
       
 43510   szNew[k] = subtotal;
       
 43511   cntNew[k] = nCell;
       
 43512   k++;
       
 43513 
       
 43514   /*
       
 43515   ** The packing computed by the previous block is biased toward the siblings
       
 43516   ** on the left side.  The left siblings are always nearly full, while the
       
 43517   ** right-most sibling might be nearly empty.  This block of code attempts
       
 43518   ** to adjust the packing of siblings to get a better balance.
       
 43519   **
       
 43520   ** This adjustment is more than an optimization.  The packing above might
       
 43521   ** be so out of balance as to be illegal.  For example, the right-most
       
 43522   ** sibling might be completely empty.  This adjustment is not optional.
       
 43523   */
       
 43524   for(i=k-1; i>0; i--){
       
 43525     int szRight = szNew[i];  /* Size of sibling on the right */
       
 43526     int szLeft = szNew[i-1]; /* Size of sibling on the left */
       
 43527     int r;              /* Index of right-most cell in left sibling */
       
 43528     int d;              /* Index of first cell to the left of right sibling */
       
 43529 
       
 43530     r = cntNew[i-1] - 1;
       
 43531     d = r + 1 - leafData;
       
 43532     assert( d<nMaxCells );
       
 43533     assert( r<nMaxCells );
       
 43534     while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
       
 43535       szRight += szCell[d] + 2;
       
 43536       szLeft -= szCell[r] + 2;
       
 43537       cntNew[i-1]--;
       
 43538       r = cntNew[i-1] - 1;
       
 43539       d = r + 1 - leafData;
       
 43540     }
       
 43541     szNew[i] = szRight;
       
 43542     szNew[i-1] = szLeft;
       
 43543   }
       
 43544 
       
 43545   /* Either we found one or more cells (cntnew[0])>0) or pPage is
       
 43546   ** a virtual root page.  A virtual root page is when the real root
       
 43547   ** page is page 1 and we are the only child of that page.
       
 43548   */
       
 43549   assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
       
 43550 
       
 43551   TRACE(("BALANCE: old: %d %d %d  ",
       
 43552     apOld[0]->pgno, 
       
 43553     nOld>=2 ? apOld[1]->pgno : 0,
       
 43554     nOld>=3 ? apOld[2]->pgno : 0
       
 43555   ));
       
 43556 
       
 43557   /*
       
 43558   ** Allocate k new pages.  Reuse old pages where possible.
       
 43559   */
       
 43560   if( apOld[0]->pgno<=1 ){
       
 43561     rc = SQLITE_CORRUPT;
       
 43562     goto balance_cleanup;
       
 43563   }
       
 43564   pageFlags = apOld[0]->aData[0];
       
 43565   for(i=0; i<k; i++){
       
 43566     MemPage *pNew;
       
 43567     if( i<nOld ){
       
 43568       pNew = apNew[i] = apOld[i];
       
 43569       apOld[i] = 0;
       
 43570       rc = sqlite3PagerWrite(pNew->pDbPage);
       
 43571       nNew++;
       
 43572       if( rc ) goto balance_cleanup;
       
 43573     }else{
       
 43574       assert( i>0 );
       
 43575       rc = allocateBtreePage(pBt, &pNew, &pgno, pgno, 0);
       
 43576       if( rc ) goto balance_cleanup;
       
 43577       apNew[i] = pNew;
       
 43578       nNew++;
       
 43579 
       
 43580       /* Set the pointer-map entry for the new sibling page. */
       
 43581       if( ISAUTOVACUUM ){
       
 43582         ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
       
 43583         if( rc!=SQLITE_OK ){
       
 43584           goto balance_cleanup;
       
 43585         }
       
 43586       }
       
 43587     }
       
 43588   }
       
 43589 
       
 43590   /* Free any old pages that were not reused as new pages.
       
 43591   */
       
 43592   while( i<nOld ){
       
 43593     freePage(apOld[i], &rc);
       
 43594     if( rc ) goto balance_cleanup;
       
 43595     releasePage(apOld[i]);
       
 43596     apOld[i] = 0;
       
 43597     i++;
       
 43598   }
       
 43599 
       
 43600   /*
       
 43601   ** Put the new pages in accending order.  This helps to
       
 43602   ** keep entries in the disk file in order so that a scan
       
 43603   ** of the table is a linear scan through the file.  That
       
 43604   ** in turn helps the operating system to deliver pages
       
 43605   ** from the disk more rapidly.
       
 43606   **
       
 43607   ** An O(n^2) insertion sort algorithm is used, but since
       
 43608   ** n is never more than NB (a small constant), that should
       
 43609   ** not be a problem.
       
 43610   **
       
 43611   ** When NB==3, this one optimization makes the database
       
 43612   ** about 25% faster for large insertions and deletions.
       
 43613   */
       
 43614   for(i=0; i<k-1; i++){
       
 43615     int minV = apNew[i]->pgno;
       
 43616     int minI = i;
       
 43617     for(j=i+1; j<k; j++){
       
 43618       if( apNew[j]->pgno<(unsigned)minV ){
       
 43619         minI = j;
       
 43620         minV = apNew[j]->pgno;
       
 43621       }
       
 43622     }
       
 43623     if( minI>i ){
       
 43624       int t;
       
 43625       MemPage *pT;
       
 43626       t = apNew[i]->pgno;
       
 43627       pT = apNew[i];
       
 43628       apNew[i] = apNew[minI];
       
 43629       apNew[minI] = pT;
       
 43630     }
       
 43631   }
       
 43632   TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
       
 43633     apNew[0]->pgno, szNew[0],
       
 43634     nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
       
 43635     nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
       
 43636     nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
       
 43637     nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
       
 43638 
       
 43639   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
       
 43640   put4byte(pRight, apNew[nNew-1]->pgno);
       
 43641 
       
 43642   /*
       
 43643   ** Evenly distribute the data in apCell[] across the new pages.
       
 43644   ** Insert divider cells into pParent as necessary.
       
 43645   */
       
 43646   j = 0;
       
 43647   for(i=0; i<nNew; i++){
       
 43648     /* Assemble the new sibling page. */
       
 43649     MemPage *pNew = apNew[i];
       
 43650     assert( j<nMaxCells );
       
 43651     zeroPage(pNew, pageFlags);
       
 43652     assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
       
 43653     assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
       
 43654     assert( pNew->nOverflow==0 );
       
 43655 
       
 43656     j = cntNew[i];
       
 43657 
       
 43658     /* If the sibling page assembled above was not the right-most sibling,
       
 43659     ** insert a divider cell into the parent page.
       
 43660     */
       
 43661     assert( i<nNew-1 || j==nCell );
       
 43662     if( j<nCell ){
       
 43663       u8 *pCell;
       
 43664       u8 *pTemp;
       
 43665       int sz;
       
 43666 
       
 43667       assert( j<nMaxCells );
       
 43668       pCell = apCell[j];
       
 43669       sz = szCell[j] + leafCorrection;
       
 43670       pTemp = &aOvflSpace[iOvflSpace];
       
 43671       if( !pNew->leaf ){
       
 43672         memcpy(&pNew->aData[8], pCell, 4);
       
 43673       }else if( leafData ){
       
 43674         /* If the tree is a leaf-data tree, and the siblings are leaves, 
       
 43675         ** then there is no divider cell in apCell[]. Instead, the divider 
       
 43676         ** cell consists of the integer key for the right-most cell of 
       
 43677         ** the sibling-page assembled above only.
       
 43678         */
       
 43679         CellInfo info;
       
 43680         j--;
       
 43681         btreeParseCellPtr(pNew, apCell[j], &info);
       
 43682         pCell = pTemp;
       
 43683         sz = 4 + putVarint(&pCell[4], info.nKey);
       
 43684         pTemp = 0;
       
 43685       }else{
       
 43686         pCell -= 4;
       
 43687         /* Obscure case for non-leaf-data trees: If the cell at pCell was
       
 43688         ** previously stored on a leaf node, and its reported size was 4
       
 43689         ** bytes, then it may actually be smaller than this 
       
 43690         ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
       
 43691         ** any cell). But it is important to pass the correct size to 
       
 43692         ** insertCell(), so reparse the cell now.
       
 43693         **
       
 43694         ** Note that this can never happen in an SQLite data file, as all
       
 43695         ** cells are at least 4 bytes. It only happens in b-trees used
       
 43696         ** to evaluate "IN (SELECT ...)" and similar clauses.
       
 43697         */
       
 43698         if( szCell[j]==4 ){
       
 43699           assert(leafCorrection==4);
       
 43700           sz = cellSizePtr(pParent, pCell);
       
 43701         }
       
 43702       }
       
 43703       iOvflSpace += sz;
       
 43704       assert( sz<=pBt->pageSize/4 );
       
 43705       assert( iOvflSpace<=pBt->pageSize );
       
 43706       insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc);
       
 43707       if( rc!=SQLITE_OK ) goto balance_cleanup;
       
 43708       assert( sqlite3PagerIswriteable(pParent->pDbPage) );
       
 43709 
       
 43710       j++;
       
 43711       nxDiv++;
       
 43712     }
       
 43713   }
       
 43714   assert( j==nCell );
       
 43715   assert( nOld>0 );
       
 43716   assert( nNew>0 );
       
 43717   if( (pageFlags & PTF_LEAF)==0 ){
       
 43718     u8 *zChild = &apCopy[nOld-1]->aData[8];
       
 43719     memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
       
 43720   }
       
 43721 
       
 43722   if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
       
 43723     /* The root page of the b-tree now contains no cells. The only sibling
       
 43724     ** page is the right-child of the parent. Copy the contents of the
       
 43725     ** child page into the parent, decreasing the overall height of the
       
 43726     ** b-tree structure by one. This is described as the "balance-shallower"
       
 43727     ** sub-algorithm in some documentation.
       
 43728     **
       
 43729     ** If this is an auto-vacuum database, the call to copyNodeContent() 
       
 43730     ** sets all pointer-map entries corresponding to database image pages 
       
 43731     ** for which the pointer is stored within the content being copied.
       
 43732     **
       
 43733     ** The second assert below verifies that the child page is defragmented
       
 43734     ** (it must be, as it was just reconstructed using assemblePage()). This
       
 43735     ** is important if the parent page happens to be page 1 of the database
       
 43736     ** image.  */
       
 43737     assert( nNew==1 );
       
 43738     assert( apNew[0]->nFree == 
       
 43739         (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) 
       
 43740     );
       
 43741     copyNodeContent(apNew[0], pParent, &rc);
       
 43742     freePage(apNew[0], &rc);
       
 43743   }else if( ISAUTOVACUUM ){
       
 43744     /* Fix the pointer-map entries for all the cells that were shifted around. 
       
 43745     ** There are several different types of pointer-map entries that need to
       
 43746     ** be dealt with by this routine. Some of these have been set already, but
       
 43747     ** many have not. The following is a summary:
       
 43748     **
       
 43749     **   1) The entries associated with new sibling pages that were not
       
 43750     **      siblings when this function was called. These have already
       
 43751     **      been set. We don't need to worry about old siblings that were
       
 43752     **      moved to the free-list - the freePage() code has taken care
       
 43753     **      of those.
       
 43754     **
       
 43755     **   2) The pointer-map entries associated with the first overflow
       
 43756     **      page in any overflow chains used by new divider cells. These 
       
 43757     **      have also already been taken care of by the insertCell() code.
       
 43758     **
       
 43759     **   3) If the sibling pages are not leaves, then the child pages of
       
 43760     **      cells stored on the sibling pages may need to be updated.
       
 43761     **
       
 43762     **   4) If the sibling pages are not internal intkey nodes, then any
       
 43763     **      overflow pages used by these cells may need to be updated
       
 43764     **      (internal intkey nodes never contain pointers to overflow pages).
       
 43765     **
       
 43766     **   5) If the sibling pages are not leaves, then the pointer-map
       
 43767     **      entries for the right-child pages of each sibling may need
       
 43768     **      to be updated.
       
 43769     **
       
 43770     ** Cases 1 and 2 are dealt with above by other code. The next
       
 43771     ** block deals with cases 3 and 4 and the one after that, case 5. Since
       
 43772     ** setting a pointer map entry is a relatively expensive operation, this
       
 43773     ** code only sets pointer map entries for child or overflow pages that have
       
 43774     ** actually moved between pages.  */
       
 43775     MemPage *pNew = apNew[0];
       
 43776     MemPage *pOld = apCopy[0];
       
 43777     int nOverflow = pOld->nOverflow;
       
 43778     int iNextOld = pOld->nCell + nOverflow;
       
 43779     int iOverflow = (nOverflow ? pOld->aOvfl[0].idx : -1);
       
 43780     j = 0;                             /* Current 'old' sibling page */
       
 43781     k = 0;                             /* Current 'new' sibling page */
       
 43782     for(i=0; i<nCell; i++){
       
 43783       int isDivider = 0;
       
 43784       while( i==iNextOld ){
       
 43785         /* Cell i is the cell immediately following the last cell on old
       
 43786         ** sibling page j. If the siblings are not leaf pages of an
       
 43787         ** intkey b-tree, then cell i was a divider cell. */
       
 43788         pOld = apCopy[++j];
       
 43789         iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
       
 43790         if( pOld->nOverflow ){
       
 43791           nOverflow = pOld->nOverflow;
       
 43792           iOverflow = i + !leafData + pOld->aOvfl[0].idx;
       
 43793         }
       
 43794         isDivider = !leafData;  
       
 43795       }
       
 43796 
       
 43797       assert(nOverflow>0 || iOverflow<i );
       
 43798       assert(nOverflow<2 || pOld->aOvfl[0].idx==pOld->aOvfl[1].idx-1);
       
 43799       assert(nOverflow<3 || pOld->aOvfl[1].idx==pOld->aOvfl[2].idx-1);
       
 43800       if( i==iOverflow ){
       
 43801         isDivider = 1;
       
 43802         if( (--nOverflow)>0 ){
       
 43803           iOverflow++;
       
 43804         }
       
 43805       }
       
 43806 
       
 43807       if( i==cntNew[k] ){
       
 43808         /* Cell i is the cell immediately following the last cell on new
       
 43809         ** sibling page k. If the siblings are not leaf pages of an
       
 43810         ** intkey b-tree, then cell i is a divider cell.  */
       
 43811         pNew = apNew[++k];
       
 43812         if( !leafData ) continue;
       
 43813       }
       
 43814       assert( j<nOld );
       
 43815       assert( k<nNew );
       
 43816 
       
 43817       /* If the cell was originally divider cell (and is not now) or
       
 43818       ** an overflow cell, or if the cell was located on a different sibling
       
 43819       ** page before the balancing, then the pointer map entries associated
       
 43820       ** with any child or overflow pages need to be updated.  */
       
 43821       if( isDivider || pOld->pgno!=pNew->pgno ){
       
 43822         if( !leafCorrection ){
       
 43823           ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc);
       
 43824         }
       
 43825         if( szCell[i]>pNew->minLocal ){
       
 43826           ptrmapPutOvflPtr(pNew, apCell[i], &rc);
       
 43827         }
       
 43828       }
       
 43829     }
       
 43830 
       
 43831     if( !leafCorrection ){
       
 43832       for(i=0; i<nNew; i++){
       
 43833         u32 key = get4byte(&apNew[i]->aData[8]);
       
 43834         ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
       
 43835       }
       
 43836     }
       
 43837 
       
 43838 #if 0
       
 43839     /* The ptrmapCheckPages() contains assert() statements that verify that
       
 43840     ** all pointer map pages are set correctly. This is helpful while 
       
 43841     ** debugging. This is usually disabled because a corrupt database may
       
 43842     ** cause an assert() statement to fail.  */
       
 43843     ptrmapCheckPages(apNew, nNew);
       
 43844     ptrmapCheckPages(&pParent, 1);
       
 43845 #endif
       
 43846   }
       
 43847 
       
 43848   assert( pParent->isInit );
       
 43849   TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
       
 43850           nOld, nNew, nCell));
       
 43851 
       
 43852   /*
       
 43853   ** Cleanup before returning.
       
 43854   */
       
 43855 balance_cleanup:
       
 43856   sqlite3ScratchFree(apCell);
       
 43857   for(i=0; i<nOld; i++){
       
 43858     releasePage(apOld[i]);
       
 43859   }
       
 43860   for(i=0; i<nNew; i++){
       
 43861     releasePage(apNew[i]);
       
 43862   }
       
 43863 
       
 43864   return rc;
       
 43865 }
       
 43866 
       
 43867 
       
 43868 /*
       
 43869 ** This function is called when the root page of a b-tree structure is
       
 43870 ** overfull (has one or more overflow pages).
       
 43871 **
       
 43872 ** A new child page is allocated and the contents of the current root
       
 43873 ** page, including overflow cells, are copied into the child. The root
       
 43874 ** page is then overwritten to make it an empty page with the right-child 
       
 43875 ** pointer pointing to the new page.
       
 43876 **
       
 43877 ** Before returning, all pointer-map entries corresponding to pages 
       
 43878 ** that the new child-page now contains pointers to are updated. The
       
 43879 ** entry corresponding to the new right-child pointer of the root
       
 43880 ** page is also updated.
       
 43881 **
       
 43882 ** If successful, *ppChild is set to contain a reference to the child 
       
 43883 ** page and SQLITE_OK is returned. In this case the caller is required
       
 43884 ** to call releasePage() on *ppChild exactly once. If an error occurs,
       
 43885 ** an error code is returned and *ppChild is set to 0.
       
 43886 */
       
 43887 static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
       
 43888   int rc;                        /* Return value from subprocedures */
       
 43889   MemPage *pChild = 0;           /* Pointer to a new child page */
       
 43890   Pgno pgnoChild = 0;            /* Page number of the new child page */
       
 43891   BtShared *pBt = pRoot->pBt;    /* The BTree */
       
 43892 
       
 43893   assert( pRoot->nOverflow>0 );
       
 43894   assert( sqlite3_mutex_held(pBt->mutex) );
       
 43895 
       
 43896   /* Make pRoot, the root page of the b-tree, writable. Allocate a new 
       
 43897   ** page that will become the new right-child of pPage. Copy the contents
       
 43898   ** of the node stored on pRoot into the new child page.
       
 43899   */
       
 43900   rc = sqlite3PagerWrite(pRoot->pDbPage);
       
 43901   if( rc==SQLITE_OK ){
       
 43902     rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
       
 43903     copyNodeContent(pRoot, pChild, &rc);
       
 43904     if( ISAUTOVACUUM ){
       
 43905       ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
       
 43906     }
       
 43907   }
       
 43908   if( rc ){
       
 43909     *ppChild = 0;
       
 43910     releasePage(pChild);
       
 43911     return rc;
       
 43912   }
       
 43913   assert( sqlite3PagerIswriteable(pChild->pDbPage) );
       
 43914   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
       
 43915   assert( pChild->nCell==pRoot->nCell );
       
 43916 
       
 43917   TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
       
 43918 
       
 43919   /* Copy the overflow cells from pRoot to pChild */
       
 43920   memcpy(pChild->aOvfl, pRoot->aOvfl, pRoot->nOverflow*sizeof(pRoot->aOvfl[0]));
       
 43921   pChild->nOverflow = pRoot->nOverflow;
       
 43922 
       
 43923   /* Zero the contents of pRoot. Then install pChild as the right-child. */
       
 43924   zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
       
 43925   put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
       
 43926 
       
 43927   *ppChild = pChild;
       
 43928   return SQLITE_OK;
       
 43929 }
       
 43930 
       
 43931 /*
       
 43932 ** The page that pCur currently points to has just been modified in
       
 43933 ** some way. This function figures out if this modification means the
       
 43934 ** tree needs to be balanced, and if so calls the appropriate balancing 
       
 43935 ** routine. Balancing routines are:
       
 43936 **
       
 43937 **   balance_quick()
       
 43938 **   balance_deeper()
       
 43939 **   balance_nonroot()
       
 43940 */
       
 43941 static int balance(BtCursor *pCur){
       
 43942   int rc = SQLITE_OK;
       
 43943   const int nMin = pCur->pBt->usableSize * 2 / 3;
       
 43944   u8 aBalanceQuickSpace[13];
       
 43945   u8 *pFree = 0;
       
 43946 
       
 43947   TESTONLY( int balance_quick_called = 0 );
       
 43948   TESTONLY( int balance_deeper_called = 0 );
       
 43949 
       
 43950   do {
       
 43951     int iPage = pCur->iPage;
       
 43952     MemPage *pPage = pCur->apPage[iPage];
       
 43953 
       
 43954     if( iPage==0 ){
       
 43955       if( pPage->nOverflow ){
       
 43956         /* The root page of the b-tree is overfull. In this case call the
       
 43957         ** balance_deeper() function to create a new child for the root-page
       
 43958         ** and copy the current contents of the root-page to it. The
       
 43959         ** next iteration of the do-loop will balance the child page.
       
 43960         */ 
       
 43961         assert( (balance_deeper_called++)==0 );
       
 43962         rc = balance_deeper(pPage, &pCur->apPage[1]);
       
 43963         if( rc==SQLITE_OK ){
       
 43964           pCur->iPage = 1;
       
 43965           pCur->aiIdx[0] = 0;
       
 43966           pCur->aiIdx[1] = 0;
       
 43967           assert( pCur->apPage[1]->nOverflow );
       
 43968         }
       
 43969       }else{
       
 43970         break;
       
 43971       }
       
 43972     }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
       
 43973       break;
       
 43974     }else{
       
 43975       MemPage * const pParent = pCur->apPage[iPage-1];
       
 43976       int const iIdx = pCur->aiIdx[iPage-1];
       
 43977 
       
 43978       rc = sqlite3PagerWrite(pParent->pDbPage);
       
 43979       if( rc==SQLITE_OK ){
       
 43980 #ifndef SQLITE_OMIT_QUICKBALANCE
       
 43981         if( pPage->hasData
       
 43982          && pPage->nOverflow==1
       
 43983          && pPage->aOvfl[0].idx==pPage->nCell
       
 43984          && pParent->pgno!=1
       
 43985          && pParent->nCell==iIdx
       
 43986         ){
       
 43987           /* Call balance_quick() to create a new sibling of pPage on which
       
 43988           ** to store the overflow cell. balance_quick() inserts a new cell
       
 43989           ** into pParent, which may cause pParent overflow. If this
       
 43990           ** happens, the next interation of the do-loop will balance pParent 
       
 43991           ** use either balance_nonroot() or balance_deeper(). Until this
       
 43992           ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
       
 43993           ** buffer. 
       
 43994           **
       
 43995           ** The purpose of the following assert() is to check that only a
       
 43996           ** single call to balance_quick() is made for each call to this
       
 43997           ** function. If this were not verified, a subtle bug involving reuse
       
 43998           ** of the aBalanceQuickSpace[] might sneak in.
       
 43999           */
       
 44000           assert( (balance_quick_called++)==0 );
       
 44001           rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
       
 44002         }else
       
 44003 #endif
       
 44004         {
       
 44005           /* In this case, call balance_nonroot() to redistribute cells
       
 44006           ** between pPage and up to 2 of its sibling pages. This involves
       
 44007           ** modifying the contents of pParent, which may cause pParent to
       
 44008           ** become overfull or underfull. The next iteration of the do-loop
       
 44009           ** will balance the parent page to correct this.
       
 44010           ** 
       
 44011           ** If the parent page becomes overfull, the overflow cell or cells
       
 44012           ** are stored in the pSpace buffer allocated immediately below. 
       
 44013           ** A subsequent iteration of the do-loop will deal with this by
       
 44014           ** calling balance_nonroot() (balance_deeper() may be called first,
       
 44015           ** but it doesn't deal with overflow cells - just moves them to a
       
 44016           ** different page). Once this subsequent call to balance_nonroot() 
       
 44017           ** has completed, it is safe to release the pSpace buffer used by
       
 44018           ** the previous call, as the overflow cell data will have been 
       
 44019           ** copied either into the body of a database page or into the new
       
 44020           ** pSpace buffer passed to the latter call to balance_nonroot().
       
 44021           */
       
 44022           u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
       
 44023           rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1);
       
 44024           if( pFree ){
       
 44025             /* If pFree is not NULL, it points to the pSpace buffer used 
       
 44026             ** by a previous call to balance_nonroot(). Its contents are
       
 44027             ** now stored either on real database pages or within the 
       
 44028             ** new pSpace buffer, so it may be safely freed here. */
       
 44029             sqlite3PageFree(pFree);
       
 44030           }
       
 44031 
       
 44032           /* The pSpace buffer will be freed after the next call to
       
 44033           ** balance_nonroot(), or just before this function returns, whichever
       
 44034           ** comes first. */
       
 44035           pFree = pSpace;
       
 44036         }
       
 44037       }
       
 44038 
       
 44039       pPage->nOverflow = 0;
       
 44040 
       
 44041       /* The next iteration of the do-loop balances the parent page. */
       
 44042       releasePage(pPage);
       
 44043       pCur->iPage--;
       
 44044     }
       
 44045   }while( rc==SQLITE_OK );
       
 44046 
       
 44047   if( pFree ){
       
 44048     sqlite3PageFree(pFree);
       
 44049   }
       
 44050   return rc;
       
 44051 }
       
 44052 
       
 44053 
       
 44054 /*
       
 44055 ** Insert a new record into the BTree.  The key is given by (pKey,nKey)
       
 44056 ** and the data is given by (pData,nData).  The cursor is used only to
       
 44057 ** define what table the record should be inserted into.  The cursor
       
 44058 ** is left pointing at a random location.
       
 44059 **
       
 44060 ** For an INTKEY table, only the nKey value of the key is used.  pKey is
       
 44061 ** ignored.  For a ZERODATA table, the pData and nData are both ignored.
       
 44062 **
       
 44063 ** If the seekResult parameter is non-zero, then a successful call to
       
 44064 ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
       
 44065 ** been performed. seekResult is the search result returned (a negative
       
 44066 ** number if pCur points at an entry that is smaller than (pKey, nKey), or
       
 44067 ** a positive value if pCur points at an etry that is larger than 
       
 44068 ** (pKey, nKey)). 
       
 44069 **
       
 44070 ** If the seekResult parameter is non-zero, then the caller guarantees that
       
 44071 ** cursor pCur is pointing at the existing copy of a row that is to be
       
 44072 ** overwritten.  If the seekResult parameter is 0, then cursor pCur may
       
 44073 ** point to any entry or to no entry at all and so this function has to seek
       
 44074 ** the cursor before the new key can be inserted.
       
 44075 */
       
 44076 SQLITE_PRIVATE int sqlite3BtreeInsert(
       
 44077   BtCursor *pCur,                /* Insert data into the table of this cursor */
       
 44078   const void *pKey, i64 nKey,    /* The key of the new record */
       
 44079   const void *pData, int nData,  /* The data of the new record */
       
 44080   int nZero,                     /* Number of extra 0 bytes to append to data */
       
 44081   int appendBias,                /* True if this is likely an append */
       
 44082   int seekResult                 /* Result of prior MovetoUnpacked() call */
       
 44083 ){
       
 44084   int rc;
       
 44085   int loc = seekResult;          /* -1: before desired location  +1: after */
       
 44086   int szNew;
       
 44087   int idx;
       
 44088   MemPage *pPage;
       
 44089   Btree *p = pCur->pBtree;
       
 44090   BtShared *pBt = p->pBt;
       
 44091   unsigned char *oldCell;
       
 44092   unsigned char *newCell = 0;
       
 44093 
       
 44094   if( pCur->eState==CURSOR_FAULT ){
       
 44095     assert( pCur->skipNext!=SQLITE_OK );
       
 44096     return pCur->skipNext;
       
 44097   }
       
 44098 
       
 44099   assert( cursorHoldsMutex(pCur) );
       
 44100   assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE && !pBt->readOnly );
       
 44101   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
       
 44102 
       
 44103   /* Assert that the caller has been consistent. If this cursor was opened
       
 44104   ** expecting an index b-tree, then the caller should be inserting blob
       
 44105   ** keys with no associated data. If the cursor was opened expecting an
       
 44106   ** intkey table, the caller should be inserting integer keys with a
       
 44107   ** blob of associated data.  */
       
 44108   assert( (pKey==0)==(pCur->pKeyInfo==0) );
       
 44109 
       
 44110   /* If this is an insert into a table b-tree, invalidate any incrblob 
       
 44111   ** cursors open on the row being replaced (assuming this is a replace
       
 44112   ** operation - if it is not, the following is a no-op).  */
       
 44113   if( pCur->pKeyInfo==0 ){
       
 44114     invalidateIncrblobCursors(p, nKey, 0);
       
 44115   }
       
 44116 
       
 44117   /* Save the positions of any other cursors open on this table.
       
 44118   **
       
 44119   ** In some cases, the call to btreeMoveto() below is a no-op. For
       
 44120   ** example, when inserting data into a table with auto-generated integer
       
 44121   ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the 
       
 44122   ** integer key to use. It then calls this function to actually insert the 
       
 44123   ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
       
 44124   ** that the cursor is already where it needs to be and returns without
       
 44125   ** doing any work. To avoid thwarting these optimizations, it is important
       
 44126   ** not to clear the cursor here.
       
 44127   */
       
 44128   rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
       
 44129   if( rc ) return rc;
       
 44130   if( !loc ){
       
 44131     rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
       
 44132     if( rc ) return rc;
       
 44133   }
       
 44134   assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
       
 44135 
       
 44136   pPage = pCur->apPage[pCur->iPage];
       
 44137   assert( pPage->intKey || nKey>=0 );
       
 44138   assert( pPage->leaf || !pPage->intKey );
       
 44139 
       
 44140   TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
       
 44141           pCur->pgnoRoot, nKey, nData, pPage->pgno,
       
 44142           loc==0 ? "overwrite" : "new entry"));
       
 44143   assert( pPage->isInit );
       
 44144   allocateTempSpace(pBt);
       
 44145   newCell = pBt->pTmpSpace;
       
 44146   if( newCell==0 ) return SQLITE_NOMEM;
       
 44147   rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
       
 44148   if( rc ) goto end_insert;
       
 44149   assert( szNew==cellSizePtr(pPage, newCell) );
       
 44150   assert( szNew<=MX_CELL_SIZE(pBt) );
       
 44151   idx = pCur->aiIdx[pCur->iPage];
       
 44152   if( loc==0 ){
       
 44153     u16 szOld;
       
 44154     assert( idx<pPage->nCell );
       
 44155     rc = sqlite3PagerWrite(pPage->pDbPage);
       
 44156     if( rc ){
       
 44157       goto end_insert;
       
 44158     }
       
 44159     oldCell = findCell(pPage, idx);
       
 44160     if( !pPage->leaf ){
       
 44161       memcpy(newCell, oldCell, 4);
       
 44162     }
       
 44163     szOld = cellSizePtr(pPage, oldCell);
       
 44164     rc = clearCell(pPage, oldCell);
       
 44165     dropCell(pPage, idx, szOld, &rc);
       
 44166     if( rc ) goto end_insert;
       
 44167   }else if( loc<0 && pPage->nCell>0 ){
       
 44168     assert( pPage->leaf );
       
 44169     idx = ++pCur->aiIdx[pCur->iPage];
       
 44170   }else{
       
 44171     assert( pPage->leaf );
       
 44172   }
       
 44173   insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
       
 44174   assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
       
 44175 
       
 44176   /* If no error has occured and pPage has an overflow cell, call balance() 
       
 44177   ** to redistribute the cells within the tree. Since balance() may move
       
 44178   ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
       
 44179   ** variables.
       
 44180   **
       
 44181   ** Previous versions of SQLite called moveToRoot() to move the cursor
       
 44182   ** back to the root page as balance() used to invalidate the contents
       
 44183   ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
       
 44184   ** set the cursor state to "invalid". This makes common insert operations
       
 44185   ** slightly faster.
       
 44186   **
       
 44187   ** There is a subtle but important optimization here too. When inserting
       
 44188   ** multiple records into an intkey b-tree using a single cursor (as can
       
 44189   ** happen while processing an "INSERT INTO ... SELECT" statement), it
       
 44190   ** is advantageous to leave the cursor pointing to the last entry in
       
 44191   ** the b-tree if possible. If the cursor is left pointing to the last
       
 44192   ** entry in the table, and the next row inserted has an integer key
       
 44193   ** larger than the largest existing key, it is possible to insert the
       
 44194   ** row without seeking the cursor. This can be a big performance boost.
       
 44195   */
       
 44196   pCur->info.nSize = 0;
       
 44197   pCur->validNKey = 0;
       
 44198   if( rc==SQLITE_OK && pPage->nOverflow ){
       
 44199     rc = balance(pCur);
       
 44200 
       
 44201     /* Must make sure nOverflow is reset to zero even if the balance()
       
 44202     ** fails. Internal data structure corruption will result otherwise. 
       
 44203     ** Also, set the cursor state to invalid. This stops saveCursorPosition()
       
 44204     ** from trying to save the current position of the cursor.  */
       
 44205     pCur->apPage[pCur->iPage]->nOverflow = 0;
       
 44206     pCur->eState = CURSOR_INVALID;
       
 44207   }
       
 44208   assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
       
 44209 
       
 44210 end_insert:
       
 44211   return rc;
       
 44212 }
       
 44213 
       
 44214 /*
       
 44215 ** Delete the entry that the cursor is pointing to.  The cursor
       
 44216 ** is left pointing at a arbitrary location.
       
 44217 */
       
 44218 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
       
 44219   Btree *p = pCur->pBtree;
       
 44220   BtShared *pBt = p->pBt;              
       
 44221   int rc;                              /* Return code */
       
 44222   MemPage *pPage;                      /* Page to delete cell from */
       
 44223   unsigned char *pCell;                /* Pointer to cell to delete */
       
 44224   int iCellIdx;                        /* Index of cell to delete */
       
 44225   int iCellDepth;                      /* Depth of node containing pCell */ 
       
 44226 
       
 44227   assert( cursorHoldsMutex(pCur) );
       
 44228   assert( pBt->inTransaction==TRANS_WRITE );
       
 44229   assert( !pBt->readOnly );
       
 44230   assert( pCur->wrFlag );
       
 44231   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
       
 44232   assert( !hasReadConflicts(p, pCur->pgnoRoot) );
       
 44233 
       
 44234   if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) 
       
 44235    || NEVER(pCur->eState!=CURSOR_VALID)
       
 44236   ){
       
 44237     return SQLITE_ERROR;  /* Something has gone awry. */
       
 44238   }
       
 44239 
       
 44240   /* If this is a delete operation to remove a row from a table b-tree,
       
 44241   ** invalidate any incrblob cursors open on the row being deleted.  */
       
 44242   if( pCur->pKeyInfo==0 ){
       
 44243     invalidateIncrblobCursors(p, pCur->info.nKey, 0);
       
 44244   }
       
 44245 
       
 44246   iCellDepth = pCur->iPage;
       
 44247   iCellIdx = pCur->aiIdx[iCellDepth];
       
 44248   pPage = pCur->apPage[iCellDepth];
       
 44249   pCell = findCell(pPage, iCellIdx);
       
 44250 
       
 44251   /* If the page containing the entry to delete is not a leaf page, move
       
 44252   ** the cursor to the largest entry in the tree that is smaller than
       
 44253   ** the entry being deleted. This cell will replace the cell being deleted
       
 44254   ** from the internal node. The 'previous' entry is used for this instead
       
 44255   ** of the 'next' entry, as the previous entry is always a part of the
       
 44256   ** sub-tree headed by the child page of the cell being deleted. This makes
       
 44257   ** balancing the tree following the delete operation easier.  */
       
 44258   if( !pPage->leaf ){
       
 44259     int notUsed;
       
 44260     rc = sqlite3BtreePrevious(pCur, &notUsed);
       
 44261     if( rc ) return rc;
       
 44262   }
       
 44263 
       
 44264   /* Save the positions of any other cursors open on this table before
       
 44265   ** making any modifications. Make the page containing the entry to be 
       
 44266   ** deleted writable. Then free any overflow pages associated with the 
       
 44267   ** entry and finally remove the cell itself from within the page.  
       
 44268   */
       
 44269   rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
       
 44270   if( rc ) return rc;
       
 44271   rc = sqlite3PagerWrite(pPage->pDbPage);
       
 44272   if( rc ) return rc;
       
 44273   rc = clearCell(pPage, pCell);
       
 44274   dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
       
 44275   if( rc ) return rc;
       
 44276 
       
 44277   /* If the cell deleted was not located on a leaf page, then the cursor
       
 44278   ** is currently pointing to the largest entry in the sub-tree headed
       
 44279   ** by the child-page of the cell that was just deleted from an internal
       
 44280   ** node. The cell from the leaf node needs to be moved to the internal
       
 44281   ** node to replace the deleted cell.  */
       
 44282   if( !pPage->leaf ){
       
 44283     MemPage *pLeaf = pCur->apPage[pCur->iPage];
       
 44284     int nCell;
       
 44285     Pgno n = pCur->apPage[iCellDepth+1]->pgno;
       
 44286     unsigned char *pTmp;
       
 44287 
       
 44288     pCell = findCell(pLeaf, pLeaf->nCell-1);
       
 44289     nCell = cellSizePtr(pLeaf, pCell);
       
 44290     assert( MX_CELL_SIZE(pBt)>=nCell );
       
 44291 
       
 44292     allocateTempSpace(pBt);
       
 44293     pTmp = pBt->pTmpSpace;
       
 44294 
       
 44295     rc = sqlite3PagerWrite(pLeaf->pDbPage);
       
 44296     insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
       
 44297     dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
       
 44298     if( rc ) return rc;
       
 44299   }
       
 44300 
       
 44301   /* Balance the tree. If the entry deleted was located on a leaf page,
       
 44302   ** then the cursor still points to that page. In this case the first
       
 44303   ** call to balance() repairs the tree, and the if(...) condition is
       
 44304   ** never true.
       
 44305   **
       
 44306   ** Otherwise, if the entry deleted was on an internal node page, then
       
 44307   ** pCur is pointing to the leaf page from which a cell was removed to
       
 44308   ** replace the cell deleted from the internal node. This is slightly
       
 44309   ** tricky as the leaf node may be underfull, and the internal node may
       
 44310   ** be either under or overfull. In this case run the balancing algorithm
       
 44311   ** on the leaf node first. If the balance proceeds far enough up the
       
 44312   ** tree that we can be sure that any problem in the internal node has
       
 44313   ** been corrected, so be it. Otherwise, after balancing the leaf node,
       
 44314   ** walk the cursor up the tree to the internal node and balance it as 
       
 44315   ** well.  */
       
 44316   rc = balance(pCur);
       
 44317   if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
       
 44318     while( pCur->iPage>iCellDepth ){
       
 44319       releasePage(pCur->apPage[pCur->iPage--]);
       
 44320     }
       
 44321     rc = balance(pCur);
       
 44322   }
       
 44323 
       
 44324   if( rc==SQLITE_OK ){
       
 44325     moveToRoot(pCur);
       
 44326   }
       
 44327   return rc;
       
 44328 }
       
 44329 
       
 44330 /*
       
 44331 ** Create a new BTree table.  Write into *piTable the page
       
 44332 ** number for the root page of the new table.
       
 44333 **
       
 44334 ** The type of type is determined by the flags parameter.  Only the
       
 44335 ** following values of flags are currently in use.  Other values for
       
 44336 ** flags might not work:
       
 44337 **
       
 44338 **     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
       
 44339 **     BTREE_ZERODATA                  Used for SQL indices
       
 44340 */
       
 44341 static int btreeCreateTable(Btree *p, int *piTable, int flags){
       
 44342   BtShared *pBt = p->pBt;
       
 44343   MemPage *pRoot;
       
 44344   Pgno pgnoRoot;
       
 44345   int rc;
       
 44346 
       
 44347   assert( sqlite3BtreeHoldsMutex(p) );
       
 44348   assert( pBt->inTransaction==TRANS_WRITE );
       
 44349   assert( !pBt->readOnly );
       
 44350 
       
 44351 #ifdef SQLITE_OMIT_AUTOVACUUM
       
 44352   rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
       
 44353   if( rc ){
       
 44354     return rc;
       
 44355   }
       
 44356 #else
       
 44357   if( pBt->autoVacuum ){
       
 44358     Pgno pgnoMove;      /* Move a page here to make room for the root-page */
       
 44359     MemPage *pPageMove; /* The page to move to. */
       
 44360 
       
 44361     /* Creating a new table may probably require moving an existing database
       
 44362     ** to make room for the new tables root page. In case this page turns
       
 44363     ** out to be an overflow page, delete all overflow page-map caches
       
 44364     ** held by open cursors.
       
 44365     */
       
 44366     invalidateAllOverflowCache(pBt);
       
 44367 
       
 44368     /* Read the value of meta[3] from the database to determine where the
       
 44369     ** root page of the new table should go. meta[3] is the largest root-page
       
 44370     ** created so far, so the new root-page is (meta[3]+1).
       
 44371     */
       
 44372     sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
       
 44373     pgnoRoot++;
       
 44374 
       
 44375     /* The new root-page may not be allocated on a pointer-map page, or the
       
 44376     ** PENDING_BYTE page.
       
 44377     */
       
 44378     while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
       
 44379         pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
       
 44380       pgnoRoot++;
       
 44381     }
       
 44382     assert( pgnoRoot>=3 );
       
 44383 
       
 44384     /* Allocate a page. The page that currently resides at pgnoRoot will
       
 44385     ** be moved to the allocated page (unless the allocated page happens
       
 44386     ** to reside at pgnoRoot).
       
 44387     */
       
 44388     rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
       
 44389     if( rc!=SQLITE_OK ){
       
 44390       return rc;
       
 44391     }
       
 44392 
       
 44393     if( pgnoMove!=pgnoRoot ){
       
 44394       /* pgnoRoot is the page that will be used for the root-page of
       
 44395       ** the new table (assuming an error did not occur). But we were
       
 44396       ** allocated pgnoMove. If required (i.e. if it was not allocated
       
 44397       ** by extending the file), the current page at position pgnoMove
       
 44398       ** is already journaled.
       
 44399       */
       
 44400       u8 eType = 0;
       
 44401       Pgno iPtrPage = 0;
       
 44402 
       
 44403       releasePage(pPageMove);
       
 44404 
       
 44405       /* Move the page currently at pgnoRoot to pgnoMove. */
       
 44406       rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
       
 44407       if( rc!=SQLITE_OK ){
       
 44408         return rc;
       
 44409       }
       
 44410       rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
       
 44411       if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
       
 44412         rc = SQLITE_CORRUPT_BKPT;
       
 44413       }
       
 44414       if( rc!=SQLITE_OK ){
       
 44415         releasePage(pRoot);
       
 44416         return rc;
       
 44417       }
       
 44418       assert( eType!=PTRMAP_ROOTPAGE );
       
 44419       assert( eType!=PTRMAP_FREEPAGE );
       
 44420       rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
       
 44421       releasePage(pRoot);
       
 44422 
       
 44423       /* Obtain the page at pgnoRoot */
       
 44424       if( rc!=SQLITE_OK ){
       
 44425         return rc;
       
 44426       }
       
 44427       rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
       
 44428       if( rc!=SQLITE_OK ){
       
 44429         return rc;
       
 44430       }
       
 44431       rc = sqlite3PagerWrite(pRoot->pDbPage);
       
 44432       if( rc!=SQLITE_OK ){
       
 44433         releasePage(pRoot);
       
 44434         return rc;
       
 44435       }
       
 44436     }else{
       
 44437       pRoot = pPageMove;
       
 44438     } 
       
 44439 
       
 44440     /* Update the pointer-map and meta-data with the new root-page number. */
       
 44441     ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
       
 44442     if( rc ){
       
 44443       releasePage(pRoot);
       
 44444       return rc;
       
 44445     }
       
 44446     rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
       
 44447     if( rc ){
       
 44448       releasePage(pRoot);
       
 44449       return rc;
       
 44450     }
       
 44451 
       
 44452   }else{
       
 44453     rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
       
 44454     if( rc ) return rc;
       
 44455   }
       
 44456 #endif
       
 44457   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
       
 44458   zeroPage(pRoot, flags | PTF_LEAF);
       
 44459   sqlite3PagerUnref(pRoot->pDbPage);
       
 44460   *piTable = (int)pgnoRoot;
       
 44461   return SQLITE_OK;
       
 44462 }
       
 44463 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
       
 44464   int rc;
       
 44465   sqlite3BtreeEnter(p);
       
 44466   rc = btreeCreateTable(p, piTable, flags);
       
 44467   sqlite3BtreeLeave(p);
       
 44468   return rc;
       
 44469 }
       
 44470 
       
 44471 /*
       
 44472 ** Erase the given database page and all its children.  Return
       
 44473 ** the page to the freelist.
       
 44474 */
       
 44475 static int clearDatabasePage(
       
 44476   BtShared *pBt,           /* The BTree that contains the table */
       
 44477   Pgno pgno,            /* Page number to clear */
       
 44478   int freePageFlag,     /* Deallocate page if true */
       
 44479   int *pnChange
       
 44480 ){
       
 44481   MemPage *pPage;
       
 44482   int rc;
       
 44483   unsigned char *pCell;
       
 44484   int i;
       
 44485 
       
 44486   assert( sqlite3_mutex_held(pBt->mutex) );
       
 44487   if( pgno>pagerPagecount(pBt) ){
       
 44488     return SQLITE_CORRUPT_BKPT;
       
 44489   }
       
 44490 
       
 44491   rc = getAndInitPage(pBt, pgno, &pPage);
       
 44492   if( rc ) return rc;
       
 44493   for(i=0; i<pPage->nCell; i++){
       
 44494     pCell = findCell(pPage, i);
       
 44495     if( !pPage->leaf ){
       
 44496       rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
       
 44497       if( rc ) goto cleardatabasepage_out;
       
 44498     }
       
 44499     rc = clearCell(pPage, pCell);
       
 44500     if( rc ) goto cleardatabasepage_out;
       
 44501   }
       
 44502   if( !pPage->leaf ){
       
 44503     rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
       
 44504     if( rc ) goto cleardatabasepage_out;
       
 44505   }else if( pnChange ){
       
 44506     assert( pPage->intKey );
       
 44507     *pnChange += pPage->nCell;
       
 44508   }
       
 44509   if( freePageFlag ){
       
 44510     freePage(pPage, &rc);
       
 44511   }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
       
 44512     zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
       
 44513   }
       
 44514 
       
 44515 cleardatabasepage_out:
       
 44516   releasePage(pPage);
       
 44517   return rc;
       
 44518 }
       
 44519 
       
 44520 /*
       
 44521 ** Delete all information from a single table in the database.  iTable is
       
 44522 ** the page number of the root of the table.  After this routine returns,
       
 44523 ** the root page is empty, but still exists.
       
 44524 **
       
 44525 ** This routine will fail with SQLITE_LOCKED if there are any open
       
 44526 ** read cursors on the table.  Open write cursors are moved to the
       
 44527 ** root of the table.
       
 44528 **
       
 44529 ** If pnChange is not NULL, then table iTable must be an intkey table. The
       
 44530 ** integer value pointed to by pnChange is incremented by the number of
       
 44531 ** entries in the table.
       
 44532 */
       
 44533 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
       
 44534   int rc;
       
 44535   BtShared *pBt = p->pBt;
       
 44536   sqlite3BtreeEnter(p);
       
 44537   assert( p->inTrans==TRANS_WRITE );
       
 44538 
       
 44539   /* Invalidate all incrblob cursors open on table iTable (assuming iTable
       
 44540   ** is the root of a table b-tree - if it is not, the following call is
       
 44541   ** a no-op).  */
       
 44542   invalidateIncrblobCursors(p, 0, 1);
       
 44543 
       
 44544   rc = saveAllCursors(pBt, (Pgno)iTable, 0);
       
 44545   if( SQLITE_OK==rc ){
       
 44546     rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
       
 44547   }
       
 44548   sqlite3BtreeLeave(p);
       
 44549   return rc;
       
 44550 }
       
 44551 
       
 44552 /*
       
 44553 ** Erase all information in a table and add the root of the table to
       
 44554 ** the freelist.  Except, the root of the principle table (the one on
       
 44555 ** page 1) is never added to the freelist.
       
 44556 **
       
 44557 ** This routine will fail with SQLITE_LOCKED if there are any open
       
 44558 ** cursors on the table.
       
 44559 **
       
 44560 ** If AUTOVACUUM is enabled and the page at iTable is not the last
       
 44561 ** root page in the database file, then the last root page 
       
 44562 ** in the database file is moved into the slot formerly occupied by
       
 44563 ** iTable and that last slot formerly occupied by the last root page
       
 44564 ** is added to the freelist instead of iTable.  In this say, all
       
 44565 ** root pages are kept at the beginning of the database file, which
       
 44566 ** is necessary for AUTOVACUUM to work right.  *piMoved is set to the 
       
 44567 ** page number that used to be the last root page in the file before
       
 44568 ** the move.  If no page gets moved, *piMoved is set to 0.
       
 44569 ** The last root page is recorded in meta[3] and the value of
       
 44570 ** meta[3] is updated by this procedure.
       
 44571 */
       
 44572 static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
       
 44573   int rc;
       
 44574   MemPage *pPage = 0;
       
 44575   BtShared *pBt = p->pBt;
       
 44576 
       
 44577   assert( sqlite3BtreeHoldsMutex(p) );
       
 44578   assert( p->inTrans==TRANS_WRITE );
       
 44579 
       
 44580   /* It is illegal to drop a table if any cursors are open on the
       
 44581   ** database. This is because in auto-vacuum mode the backend may
       
 44582   ** need to move another root-page to fill a gap left by the deleted
       
 44583   ** root page. If an open cursor was using this page a problem would 
       
 44584   ** occur.
       
 44585   **
       
 44586   ** This error is caught long before control reaches this point.
       
 44587   */
       
 44588   if( NEVER(pBt->pCursor) ){
       
 44589     sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
       
 44590     return SQLITE_LOCKED_SHAREDCACHE;
       
 44591   }
       
 44592 
       
 44593   rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
       
 44594   if( rc ) return rc;
       
 44595   rc = sqlite3BtreeClearTable(p, iTable, 0);
       
 44596   if( rc ){
       
 44597     releasePage(pPage);
       
 44598     return rc;
       
 44599   }
       
 44600 
       
 44601   *piMoved = 0;
       
 44602 
       
 44603   if( iTable>1 ){
       
 44604 #ifdef SQLITE_OMIT_AUTOVACUUM
       
 44605     freePage(pPage, &rc);
       
 44606     releasePage(pPage);
       
 44607 #else
       
 44608     if( pBt->autoVacuum ){
       
 44609       Pgno maxRootPgno;
       
 44610       sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
       
 44611 
       
 44612       if( iTable==maxRootPgno ){
       
 44613         /* If the table being dropped is the table with the largest root-page
       
 44614         ** number in the database, put the root page on the free list. 
       
 44615         */
       
 44616         freePage(pPage, &rc);
       
 44617         releasePage(pPage);
       
 44618         if( rc!=SQLITE_OK ){
       
 44619           return rc;
       
 44620         }
       
 44621       }else{
       
 44622         /* The table being dropped does not have the largest root-page
       
 44623         ** number in the database. So move the page that does into the 
       
 44624         ** gap left by the deleted root-page.
       
 44625         */
       
 44626         MemPage *pMove;
       
 44627         releasePage(pPage);
       
 44628         rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
       
 44629         if( rc!=SQLITE_OK ){
       
 44630           return rc;
       
 44631         }
       
 44632         rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
       
 44633         releasePage(pMove);
       
 44634         if( rc!=SQLITE_OK ){
       
 44635           return rc;
       
 44636         }
       
 44637         pMove = 0;
       
 44638         rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
       
 44639         freePage(pMove, &rc);
       
 44640         releasePage(pMove);
       
 44641         if( rc!=SQLITE_OK ){
       
 44642           return rc;
       
 44643         }
       
 44644         *piMoved = maxRootPgno;
       
 44645       }
       
 44646 
       
 44647       /* Set the new 'max-root-page' value in the database header. This
       
 44648       ** is the old value less one, less one more if that happens to
       
 44649       ** be a root-page number, less one again if that is the
       
 44650       ** PENDING_BYTE_PAGE.
       
 44651       */
       
 44652       maxRootPgno--;
       
 44653       while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
       
 44654              || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
       
 44655         maxRootPgno--;
       
 44656       }
       
 44657       assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
       
 44658 
       
 44659       rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
       
 44660     }else{
       
 44661       freePage(pPage, &rc);
       
 44662       releasePage(pPage);
       
 44663     }
       
 44664 #endif
       
 44665   }else{
       
 44666     /* If sqlite3BtreeDropTable was called on page 1.
       
 44667     ** This really never should happen except in a corrupt
       
 44668     ** database. 
       
 44669     */
       
 44670     zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
       
 44671     releasePage(pPage);
       
 44672   }
       
 44673   return rc;  
       
 44674 }
       
 44675 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
       
 44676   int rc;
       
 44677   sqlite3BtreeEnter(p);
       
 44678   rc = btreeDropTable(p, iTable, piMoved);
       
 44679   sqlite3BtreeLeave(p);
       
 44680   return rc;
       
 44681 }
       
 44682 
       
 44683 
       
 44684 /*
       
 44685 ** This function may only be called if the b-tree connection already
       
 44686 ** has a read or write transaction open on the database.
       
 44687 **
       
 44688 ** Read the meta-information out of a database file.  Meta[0]
       
 44689 ** is the number of free pages currently in the database.  Meta[1]
       
 44690 ** through meta[15] are available for use by higher layers.  Meta[0]
       
 44691 ** is read-only, the others are read/write.
       
 44692 ** 
       
 44693 ** The schema layer numbers meta values differently.  At the schema
       
 44694 ** layer (and the SetCookie and ReadCookie opcodes) the number of
       
 44695 ** free pages is not visible.  So Cookie[0] is the same as Meta[1].
       
 44696 */
       
 44697 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
       
 44698   BtShared *pBt = p->pBt;
       
 44699 
       
 44700   sqlite3BtreeEnter(p);
       
 44701   assert( p->inTrans>TRANS_NONE );
       
 44702   assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
       
 44703   assert( pBt->pPage1 );
       
 44704   assert( idx>=0 && idx<=15 );
       
 44705 
       
 44706   *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
       
 44707 
       
 44708   /* If auto-vacuum is disabled in this build and this is an auto-vacuum
       
 44709   ** database, mark the database as read-only.  */
       
 44710 #ifdef SQLITE_OMIT_AUTOVACUUM
       
 44711   if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ) pBt->readOnly = 1;
       
 44712 #endif
       
 44713 
       
 44714   sqlite3BtreeLeave(p);
       
 44715 }
       
 44716 
       
 44717 /*
       
 44718 ** Write meta-information back into the database.  Meta[0] is
       
 44719 ** read-only and may not be written.
       
 44720 */
       
 44721 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
       
 44722   BtShared *pBt = p->pBt;
       
 44723   unsigned char *pP1;
       
 44724   int rc;
       
 44725   assert( idx>=1 && idx<=15 );
       
 44726   sqlite3BtreeEnter(p);
       
 44727   assert( p->inTrans==TRANS_WRITE );
       
 44728   assert( pBt->pPage1!=0 );
       
 44729   pP1 = pBt->pPage1->aData;
       
 44730   rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
       
 44731   if( rc==SQLITE_OK ){
       
 44732     put4byte(&pP1[36 + idx*4], iMeta);
       
 44733 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 44734     if( idx==BTREE_INCR_VACUUM ){
       
 44735       assert( pBt->autoVacuum || iMeta==0 );
       
 44736       assert( iMeta==0 || iMeta==1 );
       
 44737       pBt->incrVacuum = (u8)iMeta;
       
 44738     }
       
 44739 #endif
       
 44740   }
       
 44741   sqlite3BtreeLeave(p);
       
 44742   return rc;
       
 44743 }
       
 44744 
       
 44745 #ifndef SQLITE_OMIT_BTREECOUNT
       
 44746 /*
       
 44747 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
       
 44748 ** number of entries in the b-tree and write the result to *pnEntry.
       
 44749 **
       
 44750 ** SQLITE_OK is returned if the operation is successfully executed. 
       
 44751 ** Otherwise, if an error is encountered (i.e. an IO error or database
       
 44752 ** corruption) an SQLite error code is returned.
       
 44753 */
       
 44754 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
       
 44755   i64 nEntry = 0;                      /* Value to return in *pnEntry */
       
 44756   int rc;                              /* Return code */
       
 44757   rc = moveToRoot(pCur);
       
 44758 
       
 44759   /* Unless an error occurs, the following loop runs one iteration for each
       
 44760   ** page in the B-Tree structure (not including overflow pages). 
       
 44761   */
       
 44762   while( rc==SQLITE_OK ){
       
 44763     int iIdx;                          /* Index of child node in parent */
       
 44764     MemPage *pPage;                    /* Current page of the b-tree */
       
 44765 
       
 44766     /* If this is a leaf page or the tree is not an int-key tree, then 
       
 44767     ** this page contains countable entries. Increment the entry counter
       
 44768     ** accordingly.
       
 44769     */
       
 44770     pPage = pCur->apPage[pCur->iPage];
       
 44771     if( pPage->leaf || !pPage->intKey ){
       
 44772       nEntry += pPage->nCell;
       
 44773     }
       
 44774 
       
 44775     /* pPage is a leaf node. This loop navigates the cursor so that it 
       
 44776     ** points to the first interior cell that it points to the parent of
       
 44777     ** the next page in the tree that has not yet been visited. The
       
 44778     ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
       
 44779     ** of the page, or to the number of cells in the page if the next page
       
 44780     ** to visit is the right-child of its parent.
       
 44781     **
       
 44782     ** If all pages in the tree have been visited, return SQLITE_OK to the
       
 44783     ** caller.
       
 44784     */
       
 44785     if( pPage->leaf ){
       
 44786       do {
       
 44787         if( pCur->iPage==0 ){
       
 44788           /* All pages of the b-tree have been visited. Return successfully. */
       
 44789           *pnEntry = nEntry;
       
 44790           return SQLITE_OK;
       
 44791         }
       
 44792         moveToParent(pCur);
       
 44793       }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
       
 44794 
       
 44795       pCur->aiIdx[pCur->iPage]++;
       
 44796       pPage = pCur->apPage[pCur->iPage];
       
 44797     }
       
 44798 
       
 44799     /* Descend to the child node of the cell that the cursor currently 
       
 44800     ** points at. This is the right-child if (iIdx==pPage->nCell).
       
 44801     */
       
 44802     iIdx = pCur->aiIdx[pCur->iPage];
       
 44803     if( iIdx==pPage->nCell ){
       
 44804       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
       
 44805     }else{
       
 44806       rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
       
 44807     }
       
 44808   }
       
 44809 
       
 44810   /* An error has occurred. Return an error code. */
       
 44811   return rc;
       
 44812 }
       
 44813 #endif
       
 44814 
       
 44815 /*
       
 44816 ** Return the pager associated with a BTree.  This routine is used for
       
 44817 ** testing and debugging only.
       
 44818 */
       
 44819 SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
       
 44820   return p->pBt->pPager;
       
 44821 }
       
 44822 
       
 44823 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
       
 44824 /*
       
 44825 ** Append a message to the error message string.
       
 44826 */
       
 44827 static void checkAppendMsg(
       
 44828   IntegrityCk *pCheck,
       
 44829   char *zMsg1,
       
 44830   const char *zFormat,
       
 44831   ...
       
 44832 ){
       
 44833   va_list ap;
       
 44834   if( !pCheck->mxErr ) return;
       
 44835   pCheck->mxErr--;
       
 44836   pCheck->nErr++;
       
 44837   va_start(ap, zFormat);
       
 44838   if( pCheck->errMsg.nChar ){
       
 44839     sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
       
 44840   }
       
 44841   if( zMsg1 ){
       
 44842     sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
       
 44843   }
       
 44844   sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
       
 44845   va_end(ap);
       
 44846   if( pCheck->errMsg.mallocFailed ){
       
 44847     pCheck->mallocFailed = 1;
       
 44848   }
       
 44849 }
       
 44850 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
       
 44851 
       
 44852 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
       
 44853 /*
       
 44854 ** Add 1 to the reference count for page iPage.  If this is the second
       
 44855 ** reference to the page, add an error message to pCheck->zErrMsg.
       
 44856 ** Return 1 if there are 2 ore more references to the page and 0 if
       
 44857 ** if this is the first reference to the page.
       
 44858 **
       
 44859 ** Also check that the page number is in bounds.
       
 44860 */
       
 44861 static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
       
 44862   if( iPage==0 ) return 1;
       
 44863   if( iPage>pCheck->nPage ){
       
 44864     checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
       
 44865     return 1;
       
 44866   }
       
 44867   if( pCheck->anRef[iPage]==1 ){
       
 44868     checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
       
 44869     return 1;
       
 44870   }
       
 44871   return  (pCheck->anRef[iPage]++)>1;
       
 44872 }
       
 44873 
       
 44874 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 44875 /*
       
 44876 ** Check that the entry in the pointer-map for page iChild maps to 
       
 44877 ** page iParent, pointer type ptrType. If not, append an error message
       
 44878 ** to pCheck.
       
 44879 */
       
 44880 static void checkPtrmap(
       
 44881   IntegrityCk *pCheck,   /* Integrity check context */
       
 44882   Pgno iChild,           /* Child page number */
       
 44883   u8 eType,              /* Expected pointer map type */
       
 44884   Pgno iParent,          /* Expected pointer map parent page number */
       
 44885   char *zContext         /* Context description (used for error msg) */
       
 44886 ){
       
 44887   int rc;
       
 44888   u8 ePtrmapType;
       
 44889   Pgno iPtrmapParent;
       
 44890 
       
 44891   rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
       
 44892   if( rc!=SQLITE_OK ){
       
 44893     if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
       
 44894     checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
       
 44895     return;
       
 44896   }
       
 44897 
       
 44898   if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
       
 44899     checkAppendMsg(pCheck, zContext, 
       
 44900       "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", 
       
 44901       iChild, eType, iParent, ePtrmapType, iPtrmapParent);
       
 44902   }
       
 44903 }
       
 44904 #endif
       
 44905 
       
 44906 /*
       
 44907 ** Check the integrity of the freelist or of an overflow page list.
       
 44908 ** Verify that the number of pages on the list is N.
       
 44909 */
       
 44910 static void checkList(
       
 44911   IntegrityCk *pCheck,  /* Integrity checking context */
       
 44912   int isFreeList,       /* True for a freelist.  False for overflow page list */
       
 44913   int iPage,            /* Page number for first page in the list */
       
 44914   int N,                /* Expected number of pages in the list */
       
 44915   char *zContext        /* Context for error messages */
       
 44916 ){
       
 44917   int i;
       
 44918   int expected = N;
       
 44919   int iFirst = iPage;
       
 44920   while( N-- > 0 && pCheck->mxErr ){
       
 44921     DbPage *pOvflPage;
       
 44922     unsigned char *pOvflData;
       
 44923     if( iPage<1 ){
       
 44924       checkAppendMsg(pCheck, zContext,
       
 44925          "%d of %d pages missing from overflow list starting at %d",
       
 44926           N+1, expected, iFirst);
       
 44927       break;
       
 44928     }
       
 44929     if( checkRef(pCheck, iPage, zContext) ) break;
       
 44930     if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
       
 44931       checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
       
 44932       break;
       
 44933     }
       
 44934     pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
       
 44935     if( isFreeList ){
       
 44936       int n = get4byte(&pOvflData[4]);
       
 44937 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 44938       if( pCheck->pBt->autoVacuum ){
       
 44939         checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
       
 44940       }
       
 44941 #endif
       
 44942       if( n>pCheck->pBt->usableSize/4-2 ){
       
 44943         checkAppendMsg(pCheck, zContext,
       
 44944            "freelist leaf count too big on page %d", iPage);
       
 44945         N--;
       
 44946       }else{
       
 44947         for(i=0; i<n; i++){
       
 44948           Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
       
 44949 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 44950           if( pCheck->pBt->autoVacuum ){
       
 44951             checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
       
 44952           }
       
 44953 #endif
       
 44954           checkRef(pCheck, iFreePage, zContext);
       
 44955         }
       
 44956         N -= n;
       
 44957       }
       
 44958     }
       
 44959 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 44960     else{
       
 44961       /* If this database supports auto-vacuum and iPage is not the last
       
 44962       ** page in this overflow list, check that the pointer-map entry for
       
 44963       ** the following page matches iPage.
       
 44964       */
       
 44965       if( pCheck->pBt->autoVacuum && N>0 ){
       
 44966         i = get4byte(pOvflData);
       
 44967         checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
       
 44968       }
       
 44969     }
       
 44970 #endif
       
 44971     iPage = get4byte(pOvflData);
       
 44972     sqlite3PagerUnref(pOvflPage);
       
 44973   }
       
 44974 }
       
 44975 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
       
 44976 
       
 44977 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
       
 44978 /*
       
 44979 ** Do various sanity checks on a single page of a tree.  Return
       
 44980 ** the tree depth.  Root pages return 0.  Parents of root pages
       
 44981 ** return 1, and so forth.
       
 44982 ** 
       
 44983 ** These checks are done:
       
 44984 **
       
 44985 **      1.  Make sure that cells and freeblocks do not overlap
       
 44986 **          but combine to completely cover the page.
       
 44987 **  NO  2.  Make sure cell keys are in order.
       
 44988 **  NO  3.  Make sure no key is less than or equal to zLowerBound.
       
 44989 **  NO  4.  Make sure no key is greater than or equal to zUpperBound.
       
 44990 **      5.  Check the integrity of overflow pages.
       
 44991 **      6.  Recursively call checkTreePage on all children.
       
 44992 **      7.  Verify that the depth of all children is the same.
       
 44993 **      8.  Make sure this page is at least 33% full or else it is
       
 44994 **          the root of the tree.
       
 44995 */
       
 44996 static int checkTreePage(
       
 44997   IntegrityCk *pCheck,  /* Context for the sanity check */
       
 44998   int iPage,            /* Page number of the page to check */
       
 44999   char *zParentContext  /* Parent context */
       
 45000 ){
       
 45001   MemPage *pPage;
       
 45002   int i, rc, depth, d2, pgno, cnt;
       
 45003   int hdr, cellStart;
       
 45004   int nCell;
       
 45005   u8 *data;
       
 45006   BtShared *pBt;
       
 45007   int usableSize;
       
 45008   char zContext[100];
       
 45009   char *hit = 0;
       
 45010 
       
 45011   sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
       
 45012 
       
 45013   /* Check that the page exists
       
 45014   */
       
 45015   pBt = pCheck->pBt;
       
 45016   usableSize = pBt->usableSize;
       
 45017   if( iPage==0 ) return 0;
       
 45018   if( checkRef(pCheck, iPage, zParentContext) ) return 0;
       
 45019   if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
       
 45020     checkAppendMsg(pCheck, zContext,
       
 45021        "unable to get the page. error code=%d", rc);
       
 45022     return 0;
       
 45023   }
       
 45024 
       
 45025   /* Clear MemPage.isInit to make sure the corruption detection code in
       
 45026   ** btreeInitPage() is executed.  */
       
 45027   pPage->isInit = 0;
       
 45028   if( (rc = btreeInitPage(pPage))!=0 ){
       
 45029     assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
       
 45030     checkAppendMsg(pCheck, zContext, 
       
 45031                    "btreeInitPage() returns error code %d", rc);
       
 45032     releasePage(pPage);
       
 45033     return 0;
       
 45034   }
       
 45035 
       
 45036   /* Check out all the cells.
       
 45037   */
       
 45038   depth = 0;
       
 45039   for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
       
 45040     u8 *pCell;
       
 45041     u32 sz;
       
 45042     CellInfo info;
       
 45043 
       
 45044     /* Check payload overflow pages
       
 45045     */
       
 45046     sqlite3_snprintf(sizeof(zContext), zContext,
       
 45047              "On tree page %d cell %d: ", iPage, i);
       
 45048     pCell = findCell(pPage,i);
       
 45049     btreeParseCellPtr(pPage, pCell, &info);
       
 45050     sz = info.nData;
       
 45051     if( !pPage->intKey ) sz += (int)info.nKey;
       
 45052     assert( sz==info.nPayload );
       
 45053     if( (sz>info.nLocal) 
       
 45054      && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
       
 45055     ){
       
 45056       int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
       
 45057       Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
       
 45058 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 45059       if( pBt->autoVacuum ){
       
 45060         checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
       
 45061       }
       
 45062 #endif
       
 45063       checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
       
 45064     }
       
 45065 
       
 45066     /* Check sanity of left child page.
       
 45067     */
       
 45068     if( !pPage->leaf ){
       
 45069       pgno = get4byte(pCell);
       
 45070 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 45071       if( pBt->autoVacuum ){
       
 45072         checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
       
 45073       }
       
 45074 #endif
       
 45075       d2 = checkTreePage(pCheck, pgno, zContext);
       
 45076       if( i>0 && d2!=depth ){
       
 45077         checkAppendMsg(pCheck, zContext, "Child page depth differs");
       
 45078       }
       
 45079       depth = d2;
       
 45080     }
       
 45081   }
       
 45082   if( !pPage->leaf ){
       
 45083     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
       
 45084     sqlite3_snprintf(sizeof(zContext), zContext, 
       
 45085                      "On page %d at right child: ", iPage);
       
 45086 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 45087     if( pBt->autoVacuum ){
       
 45088       checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
       
 45089     }
       
 45090 #endif
       
 45091     checkTreePage(pCheck, pgno, zContext);
       
 45092   }
       
 45093  
       
 45094   /* Check for complete coverage of the page
       
 45095   */
       
 45096   data = pPage->aData;
       
 45097   hdr = pPage->hdrOffset;
       
 45098   hit = sqlite3PageMalloc( pBt->pageSize );
       
 45099   if( hit==0 ){
       
 45100     pCheck->mallocFailed = 1;
       
 45101   }else{
       
 45102     u16 contentOffset = get2byte(&data[hdr+5]);
       
 45103     assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
       
 45104     memset(hit+contentOffset, 0, usableSize-contentOffset);
       
 45105     memset(hit, 1, contentOffset);
       
 45106     nCell = get2byte(&data[hdr+3]);
       
 45107     cellStart = hdr + 12 - 4*pPage->leaf;
       
 45108     for(i=0; i<nCell; i++){
       
 45109       int pc = get2byte(&data[cellStart+i*2]);
       
 45110       u16 size = 1024;
       
 45111       int j;
       
 45112       if( pc<=usableSize-4 ){
       
 45113         size = cellSizePtr(pPage, &data[pc]);
       
 45114       }
       
 45115       if( (pc+size-1)>=usableSize ){
       
 45116         checkAppendMsg(pCheck, 0, 
       
 45117             "Corruption detected in cell %d on page %d",i,iPage,0);
       
 45118       }else{
       
 45119         for(j=pc+size-1; j>=pc; j--) hit[j]++;
       
 45120       }
       
 45121     }
       
 45122     i = get2byte(&data[hdr+1]);
       
 45123     while( i>0 ){
       
 45124       int size, j;
       
 45125       assert( i<=usableSize-4 );     /* Enforced by btreeInitPage() */
       
 45126       size = get2byte(&data[i+2]);
       
 45127       assert( i+size<=usableSize );  /* Enforced by btreeInitPage() */
       
 45128       for(j=i+size-1; j>=i; j--) hit[j]++;
       
 45129       j = get2byte(&data[i]);
       
 45130       assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
       
 45131       assert( j<=usableSize-4 );   /* Enforced by btreeInitPage() */
       
 45132       i = j;
       
 45133     }
       
 45134     for(i=cnt=0; i<usableSize; i++){
       
 45135       if( hit[i]==0 ){
       
 45136         cnt++;
       
 45137       }else if( hit[i]>1 ){
       
 45138         checkAppendMsg(pCheck, 0,
       
 45139           "Multiple uses for byte %d of page %d", i, iPage);
       
 45140         break;
       
 45141       }
       
 45142     }
       
 45143     if( cnt!=data[hdr+7] ){
       
 45144       checkAppendMsg(pCheck, 0, 
       
 45145           "Fragmentation of %d bytes reported as %d on page %d",
       
 45146           cnt, data[hdr+7], iPage);
       
 45147     }
       
 45148   }
       
 45149   sqlite3PageFree(hit);
       
 45150   releasePage(pPage);
       
 45151   return depth+1;
       
 45152 }
       
 45153 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
       
 45154 
       
 45155 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
       
 45156 /*
       
 45157 ** This routine does a complete check of the given BTree file.  aRoot[] is
       
 45158 ** an array of pages numbers were each page number is the root page of
       
 45159 ** a table.  nRoot is the number of entries in aRoot.
       
 45160 **
       
 45161 ** A read-only or read-write transaction must be opened before calling
       
 45162 ** this function.
       
 45163 **
       
 45164 ** Write the number of error seen in *pnErr.  Except for some memory
       
 45165 ** allocation errors,  an error message held in memory obtained from
       
 45166 ** malloc is returned if *pnErr is non-zero.  If *pnErr==0 then NULL is
       
 45167 ** returned.  If a memory allocation error occurs, NULL is returned.
       
 45168 */
       
 45169 SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
       
 45170   Btree *p,     /* The btree to be checked */
       
 45171   int *aRoot,   /* An array of root pages numbers for individual trees */
       
 45172   int nRoot,    /* Number of entries in aRoot[] */
       
 45173   int mxErr,    /* Stop reporting errors after this many */
       
 45174   int *pnErr    /* Write number of errors seen to this variable */
       
 45175 ){
       
 45176   Pgno i;
       
 45177   int nRef;
       
 45178   IntegrityCk sCheck;
       
 45179   BtShared *pBt = p->pBt;
       
 45180   char zErr[100];
       
 45181 
       
 45182   sqlite3BtreeEnter(p);
       
 45183   assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
       
 45184   nRef = sqlite3PagerRefcount(pBt->pPager);
       
 45185   sCheck.pBt = pBt;
       
 45186   sCheck.pPager = pBt->pPager;
       
 45187   sCheck.nPage = pagerPagecount(sCheck.pBt);
       
 45188   sCheck.mxErr = mxErr;
       
 45189   sCheck.nErr = 0;
       
 45190   sCheck.mallocFailed = 0;
       
 45191   *pnErr = 0;
       
 45192   if( sCheck.nPage==0 ){
       
 45193     sqlite3BtreeLeave(p);
       
 45194     return 0;
       
 45195   }
       
 45196   sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
       
 45197   if( !sCheck.anRef ){
       
 45198     *pnErr = 1;
       
 45199     sqlite3BtreeLeave(p);
       
 45200     return 0;
       
 45201   }
       
 45202   for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
       
 45203   i = PENDING_BYTE_PAGE(pBt);
       
 45204   if( i<=sCheck.nPage ){
       
 45205     sCheck.anRef[i] = 1;
       
 45206   }
       
 45207   sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
       
 45208 
       
 45209   /* Check the integrity of the freelist
       
 45210   */
       
 45211   checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
       
 45212             get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
       
 45213 
       
 45214   /* Check all the tables.
       
 45215   */
       
 45216   for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
       
 45217     if( aRoot[i]==0 ) continue;
       
 45218 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 45219     if( pBt->autoVacuum && aRoot[i]>1 ){
       
 45220       checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
       
 45221     }
       
 45222 #endif
       
 45223     checkTreePage(&sCheck, aRoot[i], "List of tree roots: ");
       
 45224   }
       
 45225 
       
 45226   /* Make sure every page in the file is referenced
       
 45227   */
       
 45228   for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
       
 45229 #ifdef SQLITE_OMIT_AUTOVACUUM
       
 45230     if( sCheck.anRef[i]==0 ){
       
 45231       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
       
 45232     }
       
 45233 #else
       
 45234     /* If the database supports auto-vacuum, make sure no tables contain
       
 45235     ** references to pointer-map pages.
       
 45236     */
       
 45237     if( sCheck.anRef[i]==0 && 
       
 45238        (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
       
 45239       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
       
 45240     }
       
 45241     if( sCheck.anRef[i]!=0 && 
       
 45242        (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
       
 45243       checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
       
 45244     }
       
 45245 #endif
       
 45246   }
       
 45247 
       
 45248   /* Make sure this analysis did not leave any unref() pages.
       
 45249   ** This is an internal consistency check; an integrity check
       
 45250   ** of the integrity check.
       
 45251   */
       
 45252   if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
       
 45253     checkAppendMsg(&sCheck, 0, 
       
 45254       "Outstanding page count goes from %d to %d during this analysis",
       
 45255       nRef, sqlite3PagerRefcount(pBt->pPager)
       
 45256     );
       
 45257   }
       
 45258 
       
 45259   /* Clean  up and report errors.
       
 45260   */
       
 45261   sqlite3BtreeLeave(p);
       
 45262   sqlite3_free(sCheck.anRef);
       
 45263   if( sCheck.mallocFailed ){
       
 45264     sqlite3StrAccumReset(&sCheck.errMsg);
       
 45265     *pnErr = sCheck.nErr+1;
       
 45266     return 0;
       
 45267   }
       
 45268   *pnErr = sCheck.nErr;
       
 45269   if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
       
 45270   return sqlite3StrAccumFinish(&sCheck.errMsg);
       
 45271 }
       
 45272 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
       
 45273 
       
 45274 /*
       
 45275 ** Return the full pathname of the underlying database file.
       
 45276 **
       
 45277 ** The pager filename is invariant as long as the pager is
       
 45278 ** open so it is safe to access without the BtShared mutex.
       
 45279 */
       
 45280 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
       
 45281   assert( p->pBt->pPager!=0 );
       
 45282   return sqlite3PagerFilename(p->pBt->pPager);
       
 45283 }
       
 45284 
       
 45285 /*
       
 45286 ** Return the pathname of the journal file for this database. The return
       
 45287 ** value of this routine is the same regardless of whether the journal file
       
 45288 ** has been created or not.
       
 45289 **
       
 45290 ** The pager journal filename is invariant as long as the pager is
       
 45291 ** open so it is safe to access without the BtShared mutex.
       
 45292 */
       
 45293 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
       
 45294   assert( p->pBt->pPager!=0 );
       
 45295   return sqlite3PagerJournalname(p->pBt->pPager);
       
 45296 }
       
 45297 
       
 45298 /*
       
 45299 ** Return non-zero if a transaction is active.
       
 45300 */
       
 45301 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
       
 45302   assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
       
 45303   return (p && (p->inTrans==TRANS_WRITE));
       
 45304 }
       
 45305 
       
 45306 /*
       
 45307 ** Return non-zero if a read (or write) transaction is active.
       
 45308 */
       
 45309 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
       
 45310   assert( p );
       
 45311   assert( sqlite3_mutex_held(p->db->mutex) );
       
 45312   return p->inTrans!=TRANS_NONE;
       
 45313 }
       
 45314 
       
 45315 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
       
 45316   assert( p );
       
 45317   assert( sqlite3_mutex_held(p->db->mutex) );
       
 45318   return p->nBackup!=0;
       
 45319 }
       
 45320 
       
 45321 /*
       
 45322 ** This function returns a pointer to a blob of memory associated with
       
 45323 ** a single shared-btree. The memory is used by client code for its own
       
 45324 ** purposes (for example, to store a high-level schema associated with 
       
 45325 ** the shared-btree). The btree layer manages reference counting issues.
       
 45326 **
       
 45327 ** The first time this is called on a shared-btree, nBytes bytes of memory
       
 45328 ** are allocated, zeroed, and returned to the caller. For each subsequent 
       
 45329 ** call the nBytes parameter is ignored and a pointer to the same blob
       
 45330 ** of memory returned. 
       
 45331 **
       
 45332 ** If the nBytes parameter is 0 and the blob of memory has not yet been
       
 45333 ** allocated, a null pointer is returned. If the blob has already been
       
 45334 ** allocated, it is returned as normal.
       
 45335 **
       
 45336 ** Just before the shared-btree is closed, the function passed as the 
       
 45337 ** xFree argument when the memory allocation was made is invoked on the 
       
 45338 ** blob of allocated memory. This function should not call sqlite3_free()
       
 45339 ** on the memory, the btree layer does that.
       
 45340 */
       
 45341 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
       
 45342   BtShared *pBt = p->pBt;
       
 45343   sqlite3BtreeEnter(p);
       
 45344   if( !pBt->pSchema && nBytes ){
       
 45345     pBt->pSchema = sqlite3MallocZero(nBytes);
       
 45346     pBt->xFreeSchema = xFree;
       
 45347   }
       
 45348   sqlite3BtreeLeave(p);
       
 45349   return pBt->pSchema;
       
 45350 }
       
 45351 
       
 45352 /*
       
 45353 ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared 
       
 45354 ** btree as the argument handle holds an exclusive lock on the 
       
 45355 ** sqlite_master table. Otherwise SQLITE_OK.
       
 45356 */
       
 45357 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
       
 45358   int rc;
       
 45359   assert( sqlite3_mutex_held(p->db->mutex) );
       
 45360   sqlite3BtreeEnter(p);
       
 45361   rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
       
 45362   assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
       
 45363   sqlite3BtreeLeave(p);
       
 45364   return rc;
       
 45365 }
       
 45366 
       
 45367 
       
 45368 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 45369 /*
       
 45370 ** Obtain a lock on the table whose root page is iTab.  The
       
 45371 ** lock is a write lock if isWritelock is true or a read lock
       
 45372 ** if it is false.
       
 45373 */
       
 45374 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
       
 45375   int rc = SQLITE_OK;
       
 45376   assert( p->inTrans!=TRANS_NONE );
       
 45377   if( p->sharable ){
       
 45378     u8 lockType = READ_LOCK + isWriteLock;
       
 45379     assert( READ_LOCK+1==WRITE_LOCK );
       
 45380     assert( isWriteLock==0 || isWriteLock==1 );
       
 45381 
       
 45382     sqlite3BtreeEnter(p);
       
 45383     rc = querySharedCacheTableLock(p, iTab, lockType);
       
 45384     if( rc==SQLITE_OK ){
       
 45385       rc = setSharedCacheTableLock(p, iTab, lockType);
       
 45386     }
       
 45387     sqlite3BtreeLeave(p);
       
 45388   }
       
 45389   return rc;
       
 45390 }
       
 45391 #endif
       
 45392 
       
 45393 #ifndef SQLITE_OMIT_INCRBLOB
       
 45394 /*
       
 45395 ** Argument pCsr must be a cursor opened for writing on an 
       
 45396 ** INTKEY table currently pointing at a valid table entry. 
       
 45397 ** This function modifies the data stored as part of that entry.
       
 45398 **
       
 45399 ** Only the data content may only be modified, it is not possible to 
       
 45400 ** change the length of the data stored. If this function is called with
       
 45401 ** parameters that attempt to write past the end of the existing data,
       
 45402 ** no modifications are made and SQLITE_CORRUPT is returned.
       
 45403 */
       
 45404 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
       
 45405   int rc;
       
 45406   assert( cursorHoldsMutex(pCsr) );
       
 45407   assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
       
 45408   assert( pCsr->isIncrblobHandle );
       
 45409 
       
 45410   rc = restoreCursorPosition(pCsr);
       
 45411   if( rc!=SQLITE_OK ){
       
 45412     return rc;
       
 45413   }
       
 45414   assert( pCsr->eState!=CURSOR_REQUIRESEEK );
       
 45415   if( pCsr->eState!=CURSOR_VALID ){
       
 45416     return SQLITE_ABORT;
       
 45417   }
       
 45418 
       
 45419   /* Check some assumptions: 
       
 45420   **   (a) the cursor is open for writing,
       
 45421   **   (b) there is a read/write transaction open,
       
 45422   **   (c) the connection holds a write-lock on the table (if required),
       
 45423   **   (d) there are no conflicting read-locks, and
       
 45424   **   (e) the cursor points at a valid row of an intKey table.
       
 45425   */
       
 45426   if( !pCsr->wrFlag ){
       
 45427     return SQLITE_READONLY;
       
 45428   }
       
 45429   assert( !pCsr->pBt->readOnly && pCsr->pBt->inTransaction==TRANS_WRITE );
       
 45430   assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
       
 45431   assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
       
 45432   assert( pCsr->apPage[pCsr->iPage]->intKey );
       
 45433 
       
 45434   return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
       
 45435 }
       
 45436 
       
 45437 /* 
       
 45438 ** Set a flag on this cursor to cache the locations of pages from the 
       
 45439 ** overflow list for the current row. This is used by cursors opened
       
 45440 ** for incremental blob IO only.
       
 45441 **
       
 45442 ** This function sets a flag only. The actual page location cache
       
 45443 ** (stored in BtCursor.aOverflow[]) is allocated and used by function
       
 45444 ** accessPayload() (the worker function for sqlite3BtreeData() and
       
 45445 ** sqlite3BtreePutData()).
       
 45446 */
       
 45447 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
       
 45448   assert( cursorHoldsMutex(pCur) );
       
 45449   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
       
 45450   assert(!pCur->isIncrblobHandle);
       
 45451   assert(!pCur->aOverflow);
       
 45452   pCur->isIncrblobHandle = 1;
       
 45453 }
       
 45454 #endif
       
 45455 
       
 45456 /************** End of btree.c ***********************************************/
       
 45457 /************** Begin file backup.c ******************************************/
       
 45458 /*
       
 45459 ** 2009 January 28
       
 45460 **
       
 45461 ** The author disclaims copyright to this source code.  In place of
       
 45462 ** a legal notice, here is a blessing:
       
 45463 **
       
 45464 **    May you do good and not evil.
       
 45465 **    May you find forgiveness for yourself and forgive others.
       
 45466 **    May you share freely, never taking more than you give.
       
 45467 **
       
 45468 *************************************************************************
       
 45469 ** This file contains the implementation of the sqlite3_backup_XXX() 
       
 45470 ** API functions and the related features.
       
 45471 **
       
 45472 ** $Id: backup.c,v 1.19 2009/07/06 19:03:13 drh Exp $
       
 45473 */
       
 45474 
       
 45475 /* Macro to find the minimum of two numeric values.
       
 45476 */
       
 45477 #ifndef MIN
       
 45478 # define MIN(x,y) ((x)<(y)?(x):(y))
       
 45479 #endif
       
 45480 
       
 45481 /*
       
 45482 ** Structure allocated for each backup operation.
       
 45483 */
       
 45484 struct sqlite3_backup {
       
 45485   sqlite3* pDestDb;        /* Destination database handle */
       
 45486   Btree *pDest;            /* Destination b-tree file */
       
 45487   u32 iDestSchema;         /* Original schema cookie in destination */
       
 45488   int bDestLocked;         /* True once a write-transaction is open on pDest */
       
 45489 
       
 45490   Pgno iNext;              /* Page number of the next source page to copy */
       
 45491   sqlite3* pSrcDb;         /* Source database handle */
       
 45492   Btree *pSrc;             /* Source b-tree file */
       
 45493 
       
 45494   int rc;                  /* Backup process error code */
       
 45495 
       
 45496   /* These two variables are set by every call to backup_step(). They are
       
 45497   ** read by calls to backup_remaining() and backup_pagecount().
       
 45498   */
       
 45499   Pgno nRemaining;         /* Number of pages left to copy */
       
 45500   Pgno nPagecount;         /* Total number of pages to copy */
       
 45501 
       
 45502   int isAttached;          /* True once backup has been registered with pager */
       
 45503   sqlite3_backup *pNext;   /* Next backup associated with source pager */
       
 45504 };
       
 45505 
       
 45506 /*
       
 45507 ** THREAD SAFETY NOTES:
       
 45508 **
       
 45509 **   Once it has been created using backup_init(), a single sqlite3_backup
       
 45510 **   structure may be accessed via two groups of thread-safe entry points:
       
 45511 **
       
 45512 **     * Via the sqlite3_backup_XXX() API function backup_step() and 
       
 45513 **       backup_finish(). Both these functions obtain the source database
       
 45514 **       handle mutex and the mutex associated with the source BtShared 
       
 45515 **       structure, in that order.
       
 45516 **
       
 45517 **     * Via the BackupUpdate() and BackupRestart() functions, which are
       
 45518 **       invoked by the pager layer to report various state changes in
       
 45519 **       the page cache associated with the source database. The mutex
       
 45520 **       associated with the source database BtShared structure will always 
       
 45521 **       be held when either of these functions are invoked.
       
 45522 **
       
 45523 **   The other sqlite3_backup_XXX() API functions, backup_remaining() and
       
 45524 **   backup_pagecount() are not thread-safe functions. If they are called
       
 45525 **   while some other thread is calling backup_step() or backup_finish(),
       
 45526 **   the values returned may be invalid. There is no way for a call to
       
 45527 **   BackupUpdate() or BackupRestart() to interfere with backup_remaining()
       
 45528 **   or backup_pagecount().
       
 45529 **
       
 45530 **   Depending on the SQLite configuration, the database handles and/or
       
 45531 **   the Btree objects may have their own mutexes that require locking.
       
 45532 **   Non-sharable Btrees (in-memory databases for example), do not have
       
 45533 **   associated mutexes.
       
 45534 */
       
 45535 
       
 45536 /*
       
 45537 ** Return a pointer corresponding to database zDb (i.e. "main", "temp")
       
 45538 ** in connection handle pDb. If such a database cannot be found, return
       
 45539 ** a NULL pointer and write an error message to pErrorDb.
       
 45540 **
       
 45541 ** If the "temp" database is requested, it may need to be opened by this 
       
 45542 ** function. If an error occurs while doing so, return 0 and write an 
       
 45543 ** error message to pErrorDb.
       
 45544 */
       
 45545 static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
       
 45546   int i = sqlite3FindDbName(pDb, zDb);
       
 45547 
       
 45548   if( i==1 ){
       
 45549     Parse *pParse;
       
 45550     int rc = 0;
       
 45551     pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
       
 45552     if( pParse==0 ){
       
 45553       sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
       
 45554       rc = SQLITE_NOMEM;
       
 45555     }else{
       
 45556       pParse->db = pDb;
       
 45557       if( sqlite3OpenTempDatabase(pParse) ){
       
 45558         sqlite3ErrorClear(pParse);
       
 45559         sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
       
 45560         rc = SQLITE_ERROR;
       
 45561       }
       
 45562       sqlite3StackFree(pErrorDb, pParse);
       
 45563     }
       
 45564     if( rc ){
       
 45565       return 0;
       
 45566     }
       
 45567   }
       
 45568 
       
 45569   if( i<0 ){
       
 45570     sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
       
 45571     return 0;
       
 45572   }
       
 45573 
       
 45574   return pDb->aDb[i].pBt;
       
 45575 }
       
 45576 
       
 45577 /*
       
 45578 ** Create an sqlite3_backup process to copy the contents of zSrcDb from
       
 45579 ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
       
 45580 ** a pointer to the new sqlite3_backup object.
       
 45581 **
       
 45582 ** If an error occurs, NULL is returned and an error code and error message
       
 45583 ** stored in database handle pDestDb.
       
 45584 */
       
 45585 SQLITE_API sqlite3_backup *sqlite3_backup_init(
       
 45586   sqlite3* pDestDb,                     /* Database to write to */
       
 45587   const char *zDestDb,                  /* Name of database within pDestDb */
       
 45588   sqlite3* pSrcDb,                      /* Database connection to read from */
       
 45589   const char *zSrcDb                    /* Name of database within pSrcDb */
       
 45590 ){
       
 45591   sqlite3_backup *p;                    /* Value to return */
       
 45592 
       
 45593   /* Lock the source database handle. The destination database
       
 45594   ** handle is not locked in this routine, but it is locked in
       
 45595   ** sqlite3_backup_step(). The user is required to ensure that no
       
 45596   ** other thread accesses the destination handle for the duration
       
 45597   ** of the backup operation.  Any attempt to use the destination
       
 45598   ** database connection while a backup is in progress may cause
       
 45599   ** a malfunction or a deadlock.
       
 45600   */
       
 45601   sqlite3_mutex_enter(pSrcDb->mutex);
       
 45602   sqlite3_mutex_enter(pDestDb->mutex);
       
 45603 
       
 45604   if( pSrcDb==pDestDb ){
       
 45605     sqlite3Error(
       
 45606         pDestDb, SQLITE_ERROR, "source and destination must be distinct"
       
 45607     );
       
 45608     p = 0;
       
 45609   }else {
       
 45610     /* Allocate space for a new sqlite3_backup object */
       
 45611     p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup));
       
 45612     if( !p ){
       
 45613       sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
       
 45614     }
       
 45615   }
       
 45616 
       
 45617   /* If the allocation succeeded, populate the new object. */
       
 45618   if( p ){
       
 45619     memset(p, 0, sizeof(sqlite3_backup));
       
 45620     p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
       
 45621     p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
       
 45622     p->pDestDb = pDestDb;
       
 45623     p->pSrcDb = pSrcDb;
       
 45624     p->iNext = 1;
       
 45625     p->isAttached = 0;
       
 45626 
       
 45627     if( 0==p->pSrc || 0==p->pDest ){
       
 45628       /* One (or both) of the named databases did not exist. An error has
       
 45629       ** already been written into the pDestDb handle. All that is left
       
 45630       ** to do here is free the sqlite3_backup structure.
       
 45631       */
       
 45632       sqlite3_free(p);
       
 45633       p = 0;
       
 45634     }
       
 45635   }
       
 45636   if( p ){
       
 45637     p->pSrc->nBackup++;
       
 45638   }
       
 45639 
       
 45640   sqlite3_mutex_leave(pDestDb->mutex);
       
 45641   sqlite3_mutex_leave(pSrcDb->mutex);
       
 45642   return p;
       
 45643 }
       
 45644 
       
 45645 /*
       
 45646 ** Argument rc is an SQLite error code. Return true if this error is 
       
 45647 ** considered fatal if encountered during a backup operation. All errors
       
 45648 ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
       
 45649 */
       
 45650 static int isFatalError(int rc){
       
 45651   return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
       
 45652 }
       
 45653 
       
 45654 /*
       
 45655 ** Parameter zSrcData points to a buffer containing the data for 
       
 45656 ** page iSrcPg from the source database. Copy this data into the 
       
 45657 ** destination database.
       
 45658 */
       
 45659 static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){
       
 45660   Pager * const pDestPager = sqlite3BtreePager(p->pDest);
       
 45661   const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
       
 45662   int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
       
 45663   const int nCopy = MIN(nSrcPgsz, nDestPgsz);
       
 45664   const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
       
 45665 
       
 45666   int rc = SQLITE_OK;
       
 45667   i64 iOff;
       
 45668 
       
 45669   assert( p->bDestLocked );
       
 45670   assert( !isFatalError(p->rc) );
       
 45671   assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
       
 45672   assert( zSrcData );
       
 45673 
       
 45674   /* Catch the case where the destination is an in-memory database and the
       
 45675   ** page sizes of the source and destination differ. 
       
 45676   */
       
 45677   if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(sqlite3BtreePager(p->pDest)) ){
       
 45678     rc = SQLITE_READONLY;
       
 45679   }
       
 45680 
       
 45681   /* This loop runs once for each destination page spanned by the source 
       
 45682   ** page. For each iteration, variable iOff is set to the byte offset
       
 45683   ** of the destination page.
       
 45684   */
       
 45685   for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
       
 45686     DbPage *pDestPg = 0;
       
 45687     Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
       
 45688     if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
       
 45689     if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
       
 45690      && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
       
 45691     ){
       
 45692       const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
       
 45693       u8 *zDestData = sqlite3PagerGetData(pDestPg);
       
 45694       u8 *zOut = &zDestData[iOff%nDestPgsz];
       
 45695 
       
 45696       /* Copy the data from the source page into the destination page.
       
 45697       ** Then clear the Btree layer MemPage.isInit flag. Both this module
       
 45698       ** and the pager code use this trick (clearing the first byte
       
 45699       ** of the page 'extra' space to invalidate the Btree layers
       
 45700       ** cached parse of the page). MemPage.isInit is marked 
       
 45701       ** "MUST BE FIRST" for this purpose.
       
 45702       */
       
 45703       memcpy(zOut, zIn, nCopy);
       
 45704       ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
       
 45705     }
       
 45706     sqlite3PagerUnref(pDestPg);
       
 45707   }
       
 45708 
       
 45709   return rc;
       
 45710 }
       
 45711 
       
 45712 /*
       
 45713 ** If pFile is currently larger than iSize bytes, then truncate it to
       
 45714 ** exactly iSize bytes. If pFile is not larger than iSize bytes, then
       
 45715 ** this function is a no-op.
       
 45716 **
       
 45717 ** Return SQLITE_OK if everything is successful, or an SQLite error 
       
 45718 ** code if an error occurs.
       
 45719 */
       
 45720 static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
       
 45721   i64 iCurrent;
       
 45722   int rc = sqlite3OsFileSize(pFile, &iCurrent);
       
 45723   if( rc==SQLITE_OK && iCurrent>iSize ){
       
 45724     rc = sqlite3OsTruncate(pFile, iSize);
       
 45725   }
       
 45726   return rc;
       
 45727 }
       
 45728 
       
 45729 /*
       
 45730 ** Register this backup object with the associated source pager for
       
 45731 ** callbacks when pages are changed or the cache invalidated.
       
 45732 */
       
 45733 static void attachBackupObject(sqlite3_backup *p){
       
 45734   sqlite3_backup **pp;
       
 45735   assert( sqlite3BtreeHoldsMutex(p->pSrc) );
       
 45736   pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
       
 45737   p->pNext = *pp;
       
 45738   *pp = p;
       
 45739   p->isAttached = 1;
       
 45740 }
       
 45741 
       
 45742 /*
       
 45743 ** Copy nPage pages from the source b-tree to the destination.
       
 45744 */
       
 45745 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
       
 45746   int rc;
       
 45747 
       
 45748   sqlite3_mutex_enter(p->pSrcDb->mutex);
       
 45749   sqlite3BtreeEnter(p->pSrc);
       
 45750   if( p->pDestDb ){
       
 45751     sqlite3_mutex_enter(p->pDestDb->mutex);
       
 45752   }
       
 45753 
       
 45754   rc = p->rc;
       
 45755   if( !isFatalError(rc) ){
       
 45756     Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
       
 45757     Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
       
 45758     int ii;                            /* Iterator variable */
       
 45759     int nSrcPage = -1;                 /* Size of source db in pages */
       
 45760     int bCloseTrans = 0;               /* True if src db requires unlocking */
       
 45761 
       
 45762     /* If the source pager is currently in a write-transaction, return
       
 45763     ** SQLITE_BUSY immediately.
       
 45764     */
       
 45765     if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
       
 45766       rc = SQLITE_BUSY;
       
 45767     }else{
       
 45768       rc = SQLITE_OK;
       
 45769     }
       
 45770 
       
 45771     /* Lock the destination database, if it is not locked already. */
       
 45772     if( SQLITE_OK==rc && p->bDestLocked==0
       
 45773      && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 
       
 45774     ){
       
 45775       p->bDestLocked = 1;
       
 45776       sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
       
 45777     }
       
 45778 
       
 45779     /* If there is no open read-transaction on the source database, open
       
 45780     ** one now. If a transaction is opened here, then it will be closed
       
 45781     ** before this function exits.
       
 45782     */
       
 45783     if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
       
 45784       rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
       
 45785       bCloseTrans = 1;
       
 45786     }
       
 45787   
       
 45788     /* Now that there is a read-lock on the source database, query the
       
 45789     ** source pager for the number of pages in the database.
       
 45790     */
       
 45791     if( rc==SQLITE_OK ){
       
 45792       rc = sqlite3PagerPagecount(pSrcPager, &nSrcPage);
       
 45793     }
       
 45794     for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
       
 45795       const Pgno iSrcPg = p->iNext;                 /* Source page number */
       
 45796       if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
       
 45797         DbPage *pSrcPg;                             /* Source page object */
       
 45798         rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
       
 45799         if( rc==SQLITE_OK ){
       
 45800           rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg));
       
 45801           sqlite3PagerUnref(pSrcPg);
       
 45802         }
       
 45803       }
       
 45804       p->iNext++;
       
 45805     }
       
 45806     if( rc==SQLITE_OK ){
       
 45807       p->nPagecount = nSrcPage;
       
 45808       p->nRemaining = nSrcPage+1-p->iNext;
       
 45809       if( p->iNext>(Pgno)nSrcPage ){
       
 45810         rc = SQLITE_DONE;
       
 45811       }else if( !p->isAttached ){
       
 45812         attachBackupObject(p);
       
 45813       }
       
 45814     }
       
 45815   
       
 45816     /* Update the schema version field in the destination database. This
       
 45817     ** is to make sure that the schema-version really does change in
       
 45818     ** the case where the source and destination databases have the
       
 45819     ** same schema version.
       
 45820     */
       
 45821     if( rc==SQLITE_DONE 
       
 45822      && (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK
       
 45823     ){
       
 45824       const int nSrcPagesize = sqlite3BtreeGetPageSize(p->pSrc);
       
 45825       const int nDestPagesize = sqlite3BtreeGetPageSize(p->pDest);
       
 45826       int nDestTruncate;
       
 45827   
       
 45828       if( p->pDestDb ){
       
 45829         sqlite3ResetInternalSchema(p->pDestDb, 0);
       
 45830       }
       
 45831 
       
 45832       /* Set nDestTruncate to the final number of pages in the destination
       
 45833       ** database. The complication here is that the destination page
       
 45834       ** size may be different to the source page size. 
       
 45835       **
       
 45836       ** If the source page size is smaller than the destination page size, 
       
 45837       ** round up. In this case the call to sqlite3OsTruncate() below will
       
 45838       ** fix the size of the file. However it is important to call
       
 45839       ** sqlite3PagerTruncateImage() here so that any pages in the 
       
 45840       ** destination file that lie beyond the nDestTruncate page mark are
       
 45841       ** journalled by PagerCommitPhaseOne() before they are destroyed
       
 45842       ** by the file truncation.
       
 45843       */
       
 45844       if( nSrcPagesize<nDestPagesize ){
       
 45845         int ratio = nDestPagesize/nSrcPagesize;
       
 45846         nDestTruncate = (nSrcPage+ratio-1)/ratio;
       
 45847         if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
       
 45848           nDestTruncate--;
       
 45849         }
       
 45850       }else{
       
 45851         nDestTruncate = nSrcPage * (nSrcPagesize/nDestPagesize);
       
 45852       }
       
 45853       sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
       
 45854 
       
 45855       if( nSrcPagesize<nDestPagesize ){
       
 45856         /* If the source page-size is smaller than the destination page-size,
       
 45857         ** two extra things may need to happen:
       
 45858         **
       
 45859         **   * The destination may need to be truncated, and
       
 45860         **
       
 45861         **   * Data stored on the pages immediately following the 
       
 45862         **     pending-byte page in the source database may need to be
       
 45863         **     copied into the destination database.
       
 45864         */
       
 45865         const i64 iSize = (i64)nSrcPagesize * (i64)nSrcPage;
       
 45866         sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
       
 45867 
       
 45868         assert( pFile );
       
 45869         assert( (i64)nDestTruncate*(i64)nDestPagesize >= iSize || (
       
 45870               nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
       
 45871            && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+nDestPagesize
       
 45872         ));
       
 45873         if( SQLITE_OK==(rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1))
       
 45874          && SQLITE_OK==(rc = backupTruncateFile(pFile, iSize))
       
 45875          && SQLITE_OK==(rc = sqlite3PagerSync(pDestPager))
       
 45876         ){
       
 45877           i64 iOff;
       
 45878           i64 iEnd = MIN(PENDING_BYTE + nDestPagesize, iSize);
       
 45879           for(
       
 45880             iOff=PENDING_BYTE+nSrcPagesize; 
       
 45881             rc==SQLITE_OK && iOff<iEnd; 
       
 45882             iOff+=nSrcPagesize
       
 45883           ){
       
 45884             PgHdr *pSrcPg = 0;
       
 45885             const Pgno iSrcPg = (Pgno)((iOff/nSrcPagesize)+1);
       
 45886             rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
       
 45887             if( rc==SQLITE_OK ){
       
 45888               u8 *zData = sqlite3PagerGetData(pSrcPg);
       
 45889               rc = sqlite3OsWrite(pFile, zData, nSrcPagesize, iOff);
       
 45890             }
       
 45891             sqlite3PagerUnref(pSrcPg);
       
 45892           }
       
 45893         }
       
 45894       }else{
       
 45895         rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
       
 45896       }
       
 45897   
       
 45898       /* Finish committing the transaction to the destination database. */
       
 45899       if( SQLITE_OK==rc
       
 45900        && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest))
       
 45901       ){
       
 45902         rc = SQLITE_DONE;
       
 45903       }
       
 45904     }
       
 45905   
       
 45906     /* If bCloseTrans is true, then this function opened a read transaction
       
 45907     ** on the source database. Close the read transaction here. There is
       
 45908     ** no need to check the return values of the btree methods here, as
       
 45909     ** "committing" a read-only transaction cannot fail.
       
 45910     */
       
 45911     if( bCloseTrans ){
       
 45912       TESTONLY( int rc2 );
       
 45913       TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
       
 45914       TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc);
       
 45915       assert( rc2==SQLITE_OK );
       
 45916     }
       
 45917   
       
 45918     p->rc = rc;
       
 45919   }
       
 45920   if( p->pDestDb ){
       
 45921     sqlite3_mutex_leave(p->pDestDb->mutex);
       
 45922   }
       
 45923   sqlite3BtreeLeave(p->pSrc);
       
 45924   sqlite3_mutex_leave(p->pSrcDb->mutex);
       
 45925   return rc;
       
 45926 }
       
 45927 
       
 45928 /*
       
 45929 ** Release all resources associated with an sqlite3_backup* handle.
       
 45930 */
       
 45931 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
       
 45932   sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
       
 45933   sqlite3_mutex *mutex;                /* Mutex to protect source database */
       
 45934   int rc;                              /* Value to return */
       
 45935 
       
 45936   /* Enter the mutexes */
       
 45937   if( p==0 ) return SQLITE_OK;
       
 45938   sqlite3_mutex_enter(p->pSrcDb->mutex);
       
 45939   sqlite3BtreeEnter(p->pSrc);
       
 45940   mutex = p->pSrcDb->mutex;
       
 45941   if( p->pDestDb ){
       
 45942     sqlite3_mutex_enter(p->pDestDb->mutex);
       
 45943   }
       
 45944 
       
 45945   /* Detach this backup from the source pager. */
       
 45946   if( p->pDestDb ){
       
 45947     p->pSrc->nBackup--;
       
 45948   }
       
 45949   if( p->isAttached ){
       
 45950     pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
       
 45951     while( *pp!=p ){
       
 45952       pp = &(*pp)->pNext;
       
 45953     }
       
 45954     *pp = p->pNext;
       
 45955   }
       
 45956 
       
 45957   /* If a transaction is still open on the Btree, roll it back. */
       
 45958   sqlite3BtreeRollback(p->pDest);
       
 45959 
       
 45960   /* Set the error code of the destination database handle. */
       
 45961   rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
       
 45962   sqlite3Error(p->pDestDb, rc, 0);
       
 45963 
       
 45964   /* Exit the mutexes and free the backup context structure. */
       
 45965   if( p->pDestDb ){
       
 45966     sqlite3_mutex_leave(p->pDestDb->mutex);
       
 45967   }
       
 45968   sqlite3BtreeLeave(p->pSrc);
       
 45969   if( p->pDestDb ){
       
 45970     sqlite3_free(p);
       
 45971   }
       
 45972   sqlite3_mutex_leave(mutex);
       
 45973   return rc;
       
 45974 }
       
 45975 
       
 45976 /*
       
 45977 ** Return the number of pages still to be backed up as of the most recent
       
 45978 ** call to sqlite3_backup_step().
       
 45979 */
       
 45980 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
       
 45981   return p->nRemaining;
       
 45982 }
       
 45983 
       
 45984 /*
       
 45985 ** Return the total number of pages in the source database as of the most 
       
 45986 ** recent call to sqlite3_backup_step().
       
 45987 */
       
 45988 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
       
 45989   return p->nPagecount;
       
 45990 }
       
 45991 
       
 45992 /*
       
 45993 ** This function is called after the contents of page iPage of the
       
 45994 ** source database have been modified. If page iPage has already been 
       
 45995 ** copied into the destination database, then the data written to the
       
 45996 ** destination is now invalidated. The destination copy of iPage needs
       
 45997 ** to be updated with the new data before the backup operation is
       
 45998 ** complete.
       
 45999 **
       
 46000 ** It is assumed that the mutex associated with the BtShared object
       
 46001 ** corresponding to the source database is held when this function is
       
 46002 ** called.
       
 46003 */
       
 46004 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
       
 46005   sqlite3_backup *p;                   /* Iterator variable */
       
 46006   for(p=pBackup; p; p=p->pNext){
       
 46007     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
       
 46008     if( !isFatalError(p->rc) && iPage<p->iNext ){
       
 46009       /* The backup process p has already copied page iPage. But now it
       
 46010       ** has been modified by a transaction on the source pager. Copy
       
 46011       ** the new data into the backup.
       
 46012       */
       
 46013       int rc = backupOnePage(p, iPage, aData);
       
 46014       assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
       
 46015       if( rc!=SQLITE_OK ){
       
 46016         p->rc = rc;
       
 46017       }
       
 46018     }
       
 46019   }
       
 46020 }
       
 46021 
       
 46022 /*
       
 46023 ** Restart the backup process. This is called when the pager layer
       
 46024 ** detects that the database has been modified by an external database
       
 46025 ** connection. In this case there is no way of knowing which of the
       
 46026 ** pages that have been copied into the destination database are still 
       
 46027 ** valid and which are not, so the entire process needs to be restarted.
       
 46028 **
       
 46029 ** It is assumed that the mutex associated with the BtShared object
       
 46030 ** corresponding to the source database is held when this function is
       
 46031 ** called.
       
 46032 */
       
 46033 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
       
 46034   sqlite3_backup *p;                   /* Iterator variable */
       
 46035   for(p=pBackup; p; p=p->pNext){
       
 46036     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
       
 46037     p->iNext = 1;
       
 46038   }
       
 46039 }
       
 46040 
       
 46041 #ifndef SQLITE_OMIT_VACUUM
       
 46042 /*
       
 46043 ** Copy the complete content of pBtFrom into pBtTo.  A transaction
       
 46044 ** must be active for both files.
       
 46045 **
       
 46046 ** The size of file pTo may be reduced by this operation. If anything 
       
 46047 ** goes wrong, the transaction on pTo is rolled back. If successful, the 
       
 46048 ** transaction is committed before returning.
       
 46049 */
       
 46050 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
       
 46051   int rc;
       
 46052   sqlite3_backup b;
       
 46053   sqlite3BtreeEnter(pTo);
       
 46054   sqlite3BtreeEnter(pFrom);
       
 46055 
       
 46056   /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
       
 46057   ** to 0. This is used by the implementations of sqlite3_backup_step()
       
 46058   ** and sqlite3_backup_finish() to detect that they are being called
       
 46059   ** from this function, not directly by the user.
       
 46060   */
       
 46061   memset(&b, 0, sizeof(b));
       
 46062   b.pSrcDb = pFrom->db;
       
 46063   b.pSrc = pFrom;
       
 46064   b.pDest = pTo;
       
 46065   b.iNext = 1;
       
 46066 
       
 46067   /* 0x7FFFFFFF is the hard limit for the number of pages in a database
       
 46068   ** file. By passing this as the number of pages to copy to
       
 46069   ** sqlite3_backup_step(), we can guarantee that the copy finishes 
       
 46070   ** within a single call (unless an error occurs). The assert() statement
       
 46071   ** checks this assumption - (p->rc) should be set to either SQLITE_DONE 
       
 46072   ** or an error code.
       
 46073   */
       
 46074   sqlite3_backup_step(&b, 0x7FFFFFFF);
       
 46075   assert( b.rc!=SQLITE_OK );
       
 46076   rc = sqlite3_backup_finish(&b);
       
 46077   if( rc==SQLITE_OK ){
       
 46078     pTo->pBt->pageSizeFixed = 0;
       
 46079   }
       
 46080 
       
 46081   sqlite3BtreeLeave(pFrom);
       
 46082   sqlite3BtreeLeave(pTo);
       
 46083   return rc;
       
 46084 }
       
 46085 #endif /* SQLITE_OMIT_VACUUM */
       
 46086 
       
 46087 /************** End of backup.c **********************************************/
       
 46088 /************** Begin file vdbemem.c *****************************************/
       
 46089 /*
       
 46090 ** 2004 May 26
       
 46091 **
       
 46092 ** The author disclaims copyright to this source code.  In place of
       
 46093 ** a legal notice, here is a blessing:
       
 46094 **
       
 46095 **    May you do good and not evil.
       
 46096 **    May you find forgiveness for yourself and forgive others.
       
 46097 **    May you share freely, never taking more than you give.
       
 46098 **
       
 46099 *************************************************************************
       
 46100 **
       
 46101 ** This file contains code use to manipulate "Mem" structure.  A "Mem"
       
 46102 ** stores a single value in the VDBE.  Mem is an opaque structure visible
       
 46103 ** only within the VDBE.  Interface routines refer to a Mem using the
       
 46104 ** name sqlite_value
       
 46105 **
       
 46106 ** $Id: vdbemem.c,v 1.152 2009/07/22 18:07:41 drh Exp $
       
 46107 */
       
 46108 
       
 46109 /*
       
 46110 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
       
 46111 ** P if required.
       
 46112 */
       
 46113 #define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
       
 46114 
       
 46115 /*
       
 46116 ** If pMem is an object with a valid string representation, this routine
       
 46117 ** ensures the internal encoding for the string representation is
       
 46118 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
       
 46119 **
       
 46120 ** If pMem is not a string object, or the encoding of the string
       
 46121 ** representation is already stored using the requested encoding, then this
       
 46122 ** routine is a no-op.
       
 46123 **
       
 46124 ** SQLITE_OK is returned if the conversion is successful (or not required).
       
 46125 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
       
 46126 ** between formats.
       
 46127 */
       
 46128 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
       
 46129   int rc;
       
 46130   assert( (pMem->flags&MEM_RowSet)==0 );
       
 46131   assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
       
 46132            || desiredEnc==SQLITE_UTF16BE );
       
 46133   if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
       
 46134     return SQLITE_OK;
       
 46135   }
       
 46136   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
 46137 #ifdef SQLITE_OMIT_UTF16
       
 46138   return SQLITE_ERROR;
       
 46139 #else
       
 46140 
       
 46141   /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
       
 46142   ** then the encoding of the value may not have changed.
       
 46143   */
       
 46144   rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
       
 46145   assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
       
 46146   assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
       
 46147   assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
       
 46148   return rc;
       
 46149 #endif
       
 46150 }
       
 46151 
       
 46152 /*
       
 46153 ** Make sure pMem->z points to a writable allocation of at least 
       
 46154 ** n bytes.
       
 46155 **
       
 46156 ** If the memory cell currently contains string or blob data
       
 46157 ** and the third argument passed to this function is true, the 
       
 46158 ** current content of the cell is preserved. Otherwise, it may
       
 46159 ** be discarded.  
       
 46160 **
       
 46161 ** This function sets the MEM_Dyn flag and clears any xDel callback.
       
 46162 ** It also clears MEM_Ephem and MEM_Static. If the preserve flag is 
       
 46163 ** not set, Mem.n is zeroed.
       
 46164 */
       
 46165 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
       
 46166   assert( 1 >=
       
 46167     ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
       
 46168     (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + 
       
 46169     ((pMem->flags&MEM_Ephem) ? 1 : 0) + 
       
 46170     ((pMem->flags&MEM_Static) ? 1 : 0)
       
 46171   );
       
 46172   assert( (pMem->flags&MEM_RowSet)==0 );
       
 46173 
       
 46174   if( n<32 ) n = 32;
       
 46175   if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
       
 46176     if( preserve && pMem->z==pMem->zMalloc ){
       
 46177       pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
       
 46178       preserve = 0;
       
 46179     }else{
       
 46180       sqlite3DbFree(pMem->db, pMem->zMalloc);
       
 46181       pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
       
 46182     }
       
 46183   }
       
 46184 
       
 46185   if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
       
 46186     memcpy(pMem->zMalloc, pMem->z, pMem->n);
       
 46187   }
       
 46188   if( pMem->flags&MEM_Dyn && pMem->xDel ){
       
 46189     pMem->xDel((void *)(pMem->z));
       
 46190   }
       
 46191 
       
 46192   pMem->z = pMem->zMalloc;
       
 46193   if( pMem->z==0 ){
       
 46194     pMem->flags = MEM_Null;
       
 46195   }else{
       
 46196     pMem->flags &= ~(MEM_Ephem|MEM_Static);
       
 46197   }
       
 46198   pMem->xDel = 0;
       
 46199   return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
       
 46200 }
       
 46201 
       
 46202 /*
       
 46203 ** Make the given Mem object MEM_Dyn.  In other words, make it so
       
 46204 ** that any TEXT or BLOB content is stored in memory obtained from
       
 46205 ** malloc().  In this way, we know that the memory is safe to be
       
 46206 ** overwritten or altered.
       
 46207 **
       
 46208 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
       
 46209 */
       
 46210 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
       
 46211   int f;
       
 46212   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
 46213   assert( (pMem->flags&MEM_RowSet)==0 );
       
 46214   expandBlob(pMem);
       
 46215   f = pMem->flags;
       
 46216   if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
       
 46217     if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
       
 46218       return SQLITE_NOMEM;
       
 46219     }
       
 46220     pMem->z[pMem->n] = 0;
       
 46221     pMem->z[pMem->n+1] = 0;
       
 46222     pMem->flags |= MEM_Term;
       
 46223   }
       
 46224 
       
 46225   return SQLITE_OK;
       
 46226 }
       
 46227 
       
 46228 /*
       
 46229 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
       
 46230 ** blob stored in dynamically allocated space.
       
 46231 */
       
 46232 #ifndef SQLITE_OMIT_INCRBLOB
       
 46233 SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
       
 46234   if( pMem->flags & MEM_Zero ){
       
 46235     int nByte;
       
 46236     assert( pMem->flags&MEM_Blob );
       
 46237     assert( (pMem->flags&MEM_RowSet)==0 );
       
 46238     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
 46239 
       
 46240     /* Set nByte to the number of bytes required to store the expanded blob. */
       
 46241     nByte = pMem->n + pMem->u.nZero;
       
 46242     if( nByte<=0 ){
       
 46243       nByte = 1;
       
 46244     }
       
 46245     if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
       
 46246       return SQLITE_NOMEM;
       
 46247     }
       
 46248 
       
 46249     memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
       
 46250     pMem->n += pMem->u.nZero;
       
 46251     pMem->flags &= ~(MEM_Zero|MEM_Term);
       
 46252   }
       
 46253   return SQLITE_OK;
       
 46254 }
       
 46255 #endif
       
 46256 
       
 46257 
       
 46258 /*
       
 46259 ** Make sure the given Mem is \u0000 terminated.
       
 46260 */
       
 46261 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
       
 46262   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
 46263   if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
       
 46264     return SQLITE_OK;   /* Nothing to do */
       
 46265   }
       
 46266   if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
       
 46267     return SQLITE_NOMEM;
       
 46268   }
       
 46269   pMem->z[pMem->n] = 0;
       
 46270   pMem->z[pMem->n+1] = 0;
       
 46271   pMem->flags |= MEM_Term;
       
 46272   return SQLITE_OK;
       
 46273 }
       
 46274 
       
 46275 /*
       
 46276 ** Add MEM_Str to the set of representations for the given Mem.  Numbers
       
 46277 ** are converted using sqlite3_snprintf().  Converting a BLOB to a string
       
 46278 ** is a no-op.
       
 46279 **
       
 46280 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
       
 46281 **
       
 46282 ** A MEM_Null value will never be passed to this function. This function is
       
 46283 ** used for converting values to text for returning to the user (i.e. via
       
 46284 ** sqlite3_value_text()), or for ensuring that values to be used as btree
       
 46285 ** keys are strings. In the former case a NULL pointer is returned the
       
 46286 ** user and the later is an internal programming error.
       
 46287 */
       
 46288 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, int enc){
       
 46289   int rc = SQLITE_OK;
       
 46290   int fg = pMem->flags;
       
 46291   const int nByte = 32;
       
 46292 
       
 46293   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
 46294   assert( !(fg&MEM_Zero) );
       
 46295   assert( !(fg&(MEM_Str|MEM_Blob)) );
       
 46296   assert( fg&(MEM_Int|MEM_Real) );
       
 46297   assert( (pMem->flags&MEM_RowSet)==0 );
       
 46298   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
       
 46299 
       
 46300 
       
 46301   if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
       
 46302     return SQLITE_NOMEM;
       
 46303   }
       
 46304 
       
 46305   /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
       
 46306   ** string representation of the value. Then, if the required encoding
       
 46307   ** is UTF-16le or UTF-16be do a translation.
       
 46308   ** 
       
 46309   ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
       
 46310   */
       
 46311   if( fg & MEM_Int ){
       
 46312     sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
       
 46313   }else{
       
 46314     assert( fg & MEM_Real );
       
 46315     sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
       
 46316   }
       
 46317   pMem->n = sqlite3Strlen30(pMem->z);
       
 46318   pMem->enc = SQLITE_UTF8;
       
 46319   pMem->flags |= MEM_Str|MEM_Term;
       
 46320   sqlite3VdbeChangeEncoding(pMem, enc);
       
 46321   return rc;
       
 46322 }
       
 46323 
       
 46324 /*
       
 46325 ** Memory cell pMem contains the context of an aggregate function.
       
 46326 ** This routine calls the finalize method for that function.  The
       
 46327 ** result of the aggregate is stored back into pMem.
       
 46328 **
       
 46329 ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
       
 46330 ** otherwise.
       
 46331 */
       
 46332 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
       
 46333   int rc = SQLITE_OK;
       
 46334   if( ALWAYS(pFunc && pFunc->xFinalize) ){
       
 46335     sqlite3_context ctx;
       
 46336     assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
       
 46337     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
 46338     memset(&ctx, 0, sizeof(ctx));
       
 46339     ctx.s.flags = MEM_Null;
       
 46340     ctx.s.db = pMem->db;
       
 46341     ctx.pMem = pMem;
       
 46342     ctx.pFunc = pFunc;
       
 46343     pFunc->xFinalize(&ctx);
       
 46344     assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
       
 46345     sqlite3DbFree(pMem->db, pMem->zMalloc);
       
 46346     memcpy(pMem, &ctx.s, sizeof(ctx.s));
       
 46347     rc = ctx.isError;
       
 46348   }
       
 46349   return rc;
       
 46350 }
       
 46351 
       
 46352 /*
       
 46353 ** If the memory cell contains a string value that must be freed by
       
 46354 ** invoking an external callback, free it now. Calling this function
       
 46355 ** does not free any Mem.zMalloc buffer.
       
 46356 */
       
 46357 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
       
 46358   assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
       
 46359   testcase( p->flags & MEM_Agg );
       
 46360   testcase( p->flags & MEM_Dyn );
       
 46361   testcase( p->flags & MEM_RowSet );
       
 46362   testcase( p->flags & MEM_Frame );
       
 46363   if( p->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame) ){
       
 46364     if( p->flags&MEM_Agg ){
       
 46365       sqlite3VdbeMemFinalize(p, p->u.pDef);
       
 46366       assert( (p->flags & MEM_Agg)==0 );
       
 46367       sqlite3VdbeMemRelease(p);
       
 46368     }else if( p->flags&MEM_Dyn && p->xDel ){
       
 46369       assert( (p->flags&MEM_RowSet)==0 );
       
 46370       p->xDel((void *)p->z);
       
 46371       p->xDel = 0;
       
 46372     }else if( p->flags&MEM_RowSet ){
       
 46373       sqlite3RowSetClear(p->u.pRowSet);
       
 46374     }else if( p->flags&MEM_Frame ){
       
 46375       sqlite3VdbeMemSetNull(p);
       
 46376     }
       
 46377   }
       
 46378 }
       
 46379 
       
 46380 /*
       
 46381 ** Release any memory held by the Mem. This may leave the Mem in an
       
 46382 ** inconsistent state, for example with (Mem.z==0) and
       
 46383 ** (Mem.type==SQLITE_TEXT).
       
 46384 */
       
 46385 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
       
 46386   sqlite3VdbeMemReleaseExternal(p);
       
 46387   sqlite3DbFree(p->db, p->zMalloc);
       
 46388   p->z = 0;
       
 46389   p->zMalloc = 0;
       
 46390   p->xDel = 0;
       
 46391 }
       
 46392 
       
 46393 /*
       
 46394 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
       
 46395 ** If the double is too large, return 0x8000000000000000.
       
 46396 **
       
 46397 ** Most systems appear to do this simply by assigning
       
 46398 ** variables and without the extra range tests.  But
       
 46399 ** there are reports that windows throws an expection
       
 46400 ** if the floating point value is out of range. (See ticket #2880.)
       
 46401 ** Because we do not completely understand the problem, we will
       
 46402 ** take the conservative approach and always do range tests
       
 46403 ** before attempting the conversion.
       
 46404 */
       
 46405 static i64 doubleToInt64(double r){
       
 46406   /*
       
 46407   ** Many compilers we encounter do not define constants for the
       
 46408   ** minimum and maximum 64-bit integers, or they define them
       
 46409   ** inconsistently.  And many do not understand the "LL" notation.
       
 46410   ** So we define our own static constants here using nothing
       
 46411   ** larger than a 32-bit integer constant.
       
 46412   */
       
 46413   static const i64 maxInt = LARGEST_INT64;
       
 46414   static const i64 minInt = SMALLEST_INT64;
       
 46415 
       
 46416   if( r<(double)minInt ){
       
 46417     return minInt;
       
 46418   }else if( r>(double)maxInt ){
       
 46419     /* minInt is correct here - not maxInt.  It turns out that assigning
       
 46420     ** a very large positive number to an integer results in a very large
       
 46421     ** negative integer.  This makes no sense, but it is what x86 hardware
       
 46422     ** does so for compatibility we will do the same in software. */
       
 46423     return minInt;
       
 46424   }else{
       
 46425     return (i64)r;
       
 46426   }
       
 46427 }
       
 46428 
       
 46429 /*
       
 46430 ** Return some kind of integer value which is the best we can do
       
 46431 ** at representing the value that *pMem describes as an integer.
       
 46432 ** If pMem is an integer, then the value is exact.  If pMem is
       
 46433 ** a floating-point then the value returned is the integer part.
       
 46434 ** If pMem is a string or blob, then we make an attempt to convert
       
 46435 ** it into a integer and return that.  If pMem represents an
       
 46436 ** an SQL-NULL value, return 0.
       
 46437 **
       
 46438 ** If pMem represents a string value, its encoding might be changed.
       
 46439 */
       
 46440 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
       
 46441   int flags;
       
 46442   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
 46443   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
       
 46444   flags = pMem->flags;
       
 46445   if( flags & MEM_Int ){
       
 46446     return pMem->u.i;
       
 46447   }else if( flags & MEM_Real ){
       
 46448     return doubleToInt64(pMem->r);
       
 46449   }else if( flags & (MEM_Str|MEM_Blob) ){
       
 46450     i64 value;
       
 46451     pMem->flags |= MEM_Str;
       
 46452     if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
       
 46453        || sqlite3VdbeMemNulTerminate(pMem) ){
       
 46454       return 0;
       
 46455     }
       
 46456     assert( pMem->z );
       
 46457     sqlite3Atoi64(pMem->z, &value);
       
 46458     return value;
       
 46459   }else{
       
 46460     return 0;
       
 46461   }
       
 46462 }
       
 46463 
       
 46464 /*
       
 46465 ** Return the best representation of pMem that we can get into a
       
 46466 ** double.  If pMem is already a double or an integer, return its
       
 46467 ** value.  If it is a string or blob, try to convert it to a double.
       
 46468 ** If it is a NULL, return 0.0.
       
 46469 */
       
 46470 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
       
 46471   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
 46472   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
       
 46473   if( pMem->flags & MEM_Real ){
       
 46474     return pMem->r;
       
 46475   }else if( pMem->flags & MEM_Int ){
       
 46476     return (double)pMem->u.i;
       
 46477   }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
       
 46478     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
       
 46479     double val = (double)0;
       
 46480     pMem->flags |= MEM_Str;
       
 46481     if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
       
 46482        || sqlite3VdbeMemNulTerminate(pMem) ){
       
 46483       /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
       
 46484       return (double)0;
       
 46485     }
       
 46486     assert( pMem->z );
       
 46487     sqlite3AtoF(pMem->z, &val);
       
 46488     return val;
       
 46489   }else{
       
 46490     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
       
 46491     return (double)0;
       
 46492   }
       
 46493 }
       
 46494 
       
 46495 /*
       
 46496 ** The MEM structure is already a MEM_Real.  Try to also make it a
       
 46497 ** MEM_Int if we can.
       
 46498 */
       
 46499 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
       
 46500   assert( pMem->flags & MEM_Real );
       
 46501   assert( (pMem->flags & MEM_RowSet)==0 );
       
 46502   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
 46503   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
       
 46504 
       
 46505   pMem->u.i = doubleToInt64(pMem->r);
       
 46506 
       
 46507   /* Only mark the value as an integer if
       
 46508   **
       
 46509   **    (1) the round-trip conversion real->int->real is a no-op, and
       
 46510   **    (2) The integer is neither the largest nor the smallest
       
 46511   **        possible integer (ticket #3922)
       
 46512   **
       
 46513   ** The second and third terms in the following conditional enforces
       
 46514   ** the second condition under the assumption that addition overflow causes
       
 46515   ** values to wrap around.  On x86 hardware, the third term is always
       
 46516   ** true and could be omitted.  But we leave it in because other
       
 46517   ** architectures might behave differently.
       
 46518   */
       
 46519   if( pMem->r==(double)pMem->u.i && pMem->u.i>SMALLEST_INT64
       
 46520       && ALWAYS(pMem->u.i<LARGEST_INT64) ){
       
 46521     pMem->flags |= MEM_Int;
       
 46522   }
       
 46523 }
       
 46524 
       
 46525 /*
       
 46526 ** Convert pMem to type integer.  Invalidate any prior representations.
       
 46527 */
       
 46528 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
       
 46529   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
 46530   assert( (pMem->flags & MEM_RowSet)==0 );
       
 46531   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
       
 46532 
       
 46533   pMem->u.i = sqlite3VdbeIntValue(pMem);
       
 46534   MemSetTypeFlag(pMem, MEM_Int);
       
 46535   return SQLITE_OK;
       
 46536 }
       
 46537 
       
 46538 /*
       
 46539 ** Convert pMem so that it is of type MEM_Real.
       
 46540 ** Invalidate any prior representations.
       
 46541 */
       
 46542 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
       
 46543   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
 46544   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
       
 46545 
       
 46546   pMem->r = sqlite3VdbeRealValue(pMem);
       
 46547   MemSetTypeFlag(pMem, MEM_Real);
       
 46548   return SQLITE_OK;
       
 46549 }
       
 46550 
       
 46551 /*
       
 46552 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
       
 46553 ** Invalidate any prior representations.
       
 46554 */
       
 46555 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
       
 46556   double r1, r2;
       
 46557   i64 i;
       
 46558   assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 );
       
 46559   assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
       
 46560   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
 46561   r1 = sqlite3VdbeRealValue(pMem);
       
 46562   i = doubleToInt64(r1);
       
 46563   r2 = (double)i;
       
 46564   if( r1==r2 ){
       
 46565     sqlite3VdbeMemIntegerify(pMem);
       
 46566   }else{
       
 46567     pMem->r = r1;
       
 46568     MemSetTypeFlag(pMem, MEM_Real);
       
 46569   }
       
 46570   return SQLITE_OK;
       
 46571 }
       
 46572 
       
 46573 /*
       
 46574 ** Delete any previous value and set the value stored in *pMem to NULL.
       
 46575 */
       
 46576 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
       
 46577   if( pMem->flags & MEM_Frame ){
       
 46578     sqlite3VdbeFrameDelete(pMem->u.pFrame);
       
 46579   }
       
 46580   if( pMem->flags & MEM_RowSet ){
       
 46581     sqlite3RowSetClear(pMem->u.pRowSet);
       
 46582   }
       
 46583   MemSetTypeFlag(pMem, MEM_Null);
       
 46584   pMem->type = SQLITE_NULL;
       
 46585 }
       
 46586 
       
 46587 /*
       
 46588 ** Delete any previous value and set the value to be a BLOB of length
       
 46589 ** n containing all zeros.
       
 46590 */
       
 46591 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
       
 46592   sqlite3VdbeMemRelease(pMem);
       
 46593   pMem->flags = MEM_Blob|MEM_Zero;
       
 46594   pMem->type = SQLITE_BLOB;
       
 46595   pMem->n = 0;
       
 46596   if( n<0 ) n = 0;
       
 46597   pMem->u.nZero = n;
       
 46598   pMem->enc = SQLITE_UTF8;
       
 46599 
       
 46600 #ifdef SQLITE_OMIT_INCRBLOB
       
 46601   sqlite3VdbeMemGrow(pMem, n, 0);
       
 46602   if( pMem->z ){
       
 46603     pMem->n = n;
       
 46604     memset(pMem->z, 0, n);
       
 46605   }
       
 46606 #endif
       
 46607 }
       
 46608 
       
 46609 /*
       
 46610 ** Delete any previous value and set the value stored in *pMem to val,
       
 46611 ** manifest type INTEGER.
       
 46612 */
       
 46613 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
       
 46614   sqlite3VdbeMemRelease(pMem);
       
 46615   pMem->u.i = val;
       
 46616   pMem->flags = MEM_Int;
       
 46617   pMem->type = SQLITE_INTEGER;
       
 46618 }
       
 46619 
       
 46620 /*
       
 46621 ** Delete any previous value and set the value stored in *pMem to val,
       
 46622 ** manifest type REAL.
       
 46623 */
       
 46624 SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
       
 46625   if( sqlite3IsNaN(val) ){
       
 46626     sqlite3VdbeMemSetNull(pMem);
       
 46627   }else{
       
 46628     sqlite3VdbeMemRelease(pMem);
       
 46629     pMem->r = val;
       
 46630     pMem->flags = MEM_Real;
       
 46631     pMem->type = SQLITE_FLOAT;
       
 46632   }
       
 46633 }
       
 46634 
       
 46635 /*
       
 46636 ** Delete any previous value and set the value of pMem to be an
       
 46637 ** empty boolean index.
       
 46638 */
       
 46639 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
       
 46640   sqlite3 *db = pMem->db;
       
 46641   assert( db!=0 );
       
 46642   assert( (pMem->flags & MEM_RowSet)==0 );
       
 46643   sqlite3VdbeMemRelease(pMem);
       
 46644   pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
       
 46645   if( db->mallocFailed ){
       
 46646     pMem->flags = MEM_Null;
       
 46647   }else{
       
 46648     assert( pMem->zMalloc );
       
 46649     pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, 
       
 46650                                        sqlite3DbMallocSize(db, pMem->zMalloc));
       
 46651     assert( pMem->u.pRowSet!=0 );
       
 46652     pMem->flags = MEM_RowSet;
       
 46653   }
       
 46654 }
       
 46655 
       
 46656 /*
       
 46657 ** Return true if the Mem object contains a TEXT or BLOB that is
       
 46658 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
       
 46659 */
       
 46660 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
       
 46661   assert( p->db!=0 );
       
 46662   if( p->flags & (MEM_Str|MEM_Blob) ){
       
 46663     int n = p->n;
       
 46664     if( p->flags & MEM_Zero ){
       
 46665       n += p->u.nZero;
       
 46666     }
       
 46667     return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
       
 46668   }
       
 46669   return 0; 
       
 46670 }
       
 46671 
       
 46672 /*
       
 46673 ** Size of struct Mem not including the Mem.zMalloc member.
       
 46674 */
       
 46675 #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
       
 46676 
       
 46677 /*
       
 46678 ** Make an shallow copy of pFrom into pTo.  Prior contents of
       
 46679 ** pTo are freed.  The pFrom->z field is not duplicated.  If
       
 46680 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
       
 46681 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
       
 46682 */
       
 46683 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
       
 46684   assert( (pFrom->flags & MEM_RowSet)==0 );
       
 46685   sqlite3VdbeMemReleaseExternal(pTo);
       
 46686   memcpy(pTo, pFrom, MEMCELLSIZE);
       
 46687   pTo->xDel = 0;
       
 46688   if( (pFrom->flags&MEM_Dyn)!=0 || pFrom->z==pFrom->zMalloc ){
       
 46689     pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
       
 46690     assert( srcType==MEM_Ephem || srcType==MEM_Static );
       
 46691     pTo->flags |= srcType;
       
 46692   }
       
 46693 }
       
 46694 
       
 46695 /*
       
 46696 ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
       
 46697 ** freed before the copy is made.
       
 46698 */
       
 46699 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
       
 46700   int rc = SQLITE_OK;
       
 46701 
       
 46702   assert( (pFrom->flags & MEM_RowSet)==0 );
       
 46703   sqlite3VdbeMemReleaseExternal(pTo);
       
 46704   memcpy(pTo, pFrom, MEMCELLSIZE);
       
 46705   pTo->flags &= ~MEM_Dyn;
       
 46706 
       
 46707   if( pTo->flags&(MEM_Str|MEM_Blob) ){
       
 46708     if( 0==(pFrom->flags&MEM_Static) ){
       
 46709       pTo->flags |= MEM_Ephem;
       
 46710       rc = sqlite3VdbeMemMakeWriteable(pTo);
       
 46711     }
       
 46712   }
       
 46713 
       
 46714   return rc;
       
 46715 }
       
 46716 
       
 46717 /*
       
 46718 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
       
 46719 ** freed. If pFrom contains ephemeral data, a copy is made.
       
 46720 **
       
 46721 ** pFrom contains an SQL NULL when this routine returns.
       
 46722 */
       
 46723 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
       
 46724   assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
       
 46725   assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
       
 46726   assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
       
 46727 
       
 46728   sqlite3VdbeMemRelease(pTo);
       
 46729   memcpy(pTo, pFrom, sizeof(Mem));
       
 46730   pFrom->flags = MEM_Null;
       
 46731   pFrom->xDel = 0;
       
 46732   pFrom->zMalloc = 0;
       
 46733 }
       
 46734 
       
 46735 /*
       
 46736 ** Change the value of a Mem to be a string or a BLOB.
       
 46737 **
       
 46738 ** The memory management strategy depends on the value of the xDel
       
 46739 ** parameter. If the value passed is SQLITE_TRANSIENT, then the 
       
 46740 ** string is copied into a (possibly existing) buffer managed by the 
       
 46741 ** Mem structure. Otherwise, any existing buffer is freed and the
       
 46742 ** pointer copied.
       
 46743 **
       
 46744 ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
       
 46745 ** size limit) then no memory allocation occurs.  If the string can be
       
 46746 ** stored without allocating memory, then it is.  If a memory allocation
       
 46747 ** is required to store the string, then value of pMem is unchanged.  In
       
 46748 ** either case, SQLITE_TOOBIG is returned.
       
 46749 */
       
 46750 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
       
 46751   Mem *pMem,          /* Memory cell to set to string value */
       
 46752   const char *z,      /* String pointer */
       
 46753   int n,              /* Bytes in string, or negative */
       
 46754   u8 enc,             /* Encoding of z.  0 for BLOBs */
       
 46755   void (*xDel)(void*) /* Destructor function */
       
 46756 ){
       
 46757   int nByte = n;      /* New value for pMem->n */
       
 46758   int iLimit;         /* Maximum allowed string or blob size */
       
 46759   u16 flags = 0;      /* New value for pMem->flags */
       
 46760 
       
 46761   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
 46762   assert( (pMem->flags & MEM_RowSet)==0 );
       
 46763 
       
 46764   /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
       
 46765   if( !z ){
       
 46766     sqlite3VdbeMemSetNull(pMem);
       
 46767     return SQLITE_OK;
       
 46768   }
       
 46769 
       
 46770   if( pMem->db ){
       
 46771     iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
       
 46772   }else{
       
 46773     iLimit = SQLITE_MAX_LENGTH;
       
 46774   }
       
 46775   flags = (enc==0?MEM_Blob:MEM_Str);
       
 46776   if( nByte<0 ){
       
 46777     assert( enc!=0 );
       
 46778     if( enc==SQLITE_UTF8 ){
       
 46779       for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
       
 46780     }else{
       
 46781       for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
       
 46782     }
       
 46783     flags |= MEM_Term;
       
 46784   }
       
 46785 
       
 46786   /* The following block sets the new values of Mem.z and Mem.xDel. It
       
 46787   ** also sets a flag in local variable "flags" to indicate the memory
       
 46788   ** management (one of MEM_Dyn or MEM_Static).
       
 46789   */
       
 46790   if( xDel==SQLITE_TRANSIENT ){
       
 46791     int nAlloc = nByte;
       
 46792     if( flags&MEM_Term ){
       
 46793       nAlloc += (enc==SQLITE_UTF8?1:2);
       
 46794     }
       
 46795     if( nByte>iLimit ){
       
 46796       return SQLITE_TOOBIG;
       
 46797     }
       
 46798     if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
       
 46799       return SQLITE_NOMEM;
       
 46800     }
       
 46801     memcpy(pMem->z, z, nAlloc);
       
 46802   }else if( xDel==SQLITE_DYNAMIC ){
       
 46803     sqlite3VdbeMemRelease(pMem);
       
 46804     pMem->zMalloc = pMem->z = (char *)z;
       
 46805     pMem->xDel = 0;
       
 46806   }else{
       
 46807     sqlite3VdbeMemRelease(pMem);
       
 46808     pMem->z = (char *)z;
       
 46809     pMem->xDel = xDel;
       
 46810     flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
       
 46811   }
       
 46812 
       
 46813   pMem->n = nByte;
       
 46814   pMem->flags = flags;
       
 46815   pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
       
 46816   pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
       
 46817 
       
 46818 #ifndef SQLITE_OMIT_UTF16
       
 46819   if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
       
 46820     return SQLITE_NOMEM;
       
 46821   }
       
 46822 #endif
       
 46823 
       
 46824   if( nByte>iLimit ){
       
 46825     return SQLITE_TOOBIG;
       
 46826   }
       
 46827 
       
 46828   return SQLITE_OK;
       
 46829 }
       
 46830 
       
 46831 /*
       
 46832 ** Compare the values contained by the two memory cells, returning
       
 46833 ** negative, zero or positive if pMem1 is less than, equal to, or greater
       
 46834 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
       
 46835 ** and reals) sorted numerically, followed by text ordered by the collating
       
 46836 ** sequence pColl and finally blob's ordered by memcmp().
       
 46837 **
       
 46838 ** Two NULL values are considered equal by this function.
       
 46839 */
       
 46840 SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
       
 46841   int rc;
       
 46842   int f1, f2;
       
 46843   int combined_flags;
       
 46844 
       
 46845   /* Interchange pMem1 and pMem2 if the collating sequence specifies
       
 46846   ** DESC order.
       
 46847   */
       
 46848   f1 = pMem1->flags;
       
 46849   f2 = pMem2->flags;
       
 46850   combined_flags = f1|f2;
       
 46851   assert( (combined_flags & MEM_RowSet)==0 );
       
 46852  
       
 46853   /* If one value is NULL, it is less than the other. If both values
       
 46854   ** are NULL, return 0.
       
 46855   */
       
 46856   if( combined_flags&MEM_Null ){
       
 46857     return (f2&MEM_Null) - (f1&MEM_Null);
       
 46858   }
       
 46859 
       
 46860   /* If one value is a number and the other is not, the number is less.
       
 46861   ** If both are numbers, compare as reals if one is a real, or as integers
       
 46862   ** if both values are integers.
       
 46863   */
       
 46864   if( combined_flags&(MEM_Int|MEM_Real) ){
       
 46865     if( !(f1&(MEM_Int|MEM_Real)) ){
       
 46866       return 1;
       
 46867     }
       
 46868     if( !(f2&(MEM_Int|MEM_Real)) ){
       
 46869       return -1;
       
 46870     }
       
 46871     if( (f1 & f2 & MEM_Int)==0 ){
       
 46872       double r1, r2;
       
 46873       if( (f1&MEM_Real)==0 ){
       
 46874         r1 = (double)pMem1->u.i;
       
 46875       }else{
       
 46876         r1 = pMem1->r;
       
 46877       }
       
 46878       if( (f2&MEM_Real)==0 ){
       
 46879         r2 = (double)pMem2->u.i;
       
 46880       }else{
       
 46881         r2 = pMem2->r;
       
 46882       }
       
 46883       if( r1<r2 ) return -1;
       
 46884       if( r1>r2 ) return 1;
       
 46885       return 0;
       
 46886     }else{
       
 46887       assert( f1&MEM_Int );
       
 46888       assert( f2&MEM_Int );
       
 46889       if( pMem1->u.i < pMem2->u.i ) return -1;
       
 46890       if( pMem1->u.i > pMem2->u.i ) return 1;
       
 46891       return 0;
       
 46892     }
       
 46893   }
       
 46894 
       
 46895   /* If one value is a string and the other is a blob, the string is less.
       
 46896   ** If both are strings, compare using the collating functions.
       
 46897   */
       
 46898   if( combined_flags&MEM_Str ){
       
 46899     if( (f1 & MEM_Str)==0 ){
       
 46900       return 1;
       
 46901     }
       
 46902     if( (f2 & MEM_Str)==0 ){
       
 46903       return -1;
       
 46904     }
       
 46905 
       
 46906     assert( pMem1->enc==pMem2->enc );
       
 46907     assert( pMem1->enc==SQLITE_UTF8 || 
       
 46908             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
       
 46909 
       
 46910     /* The collation sequence must be defined at this point, even if
       
 46911     ** the user deletes the collation sequence after the vdbe program is
       
 46912     ** compiled (this was not always the case).
       
 46913     */
       
 46914     assert( !pColl || pColl->xCmp );
       
 46915 
       
 46916     if( pColl ){
       
 46917       if( pMem1->enc==pColl->enc ){
       
 46918         /* The strings are already in the correct encoding.  Call the
       
 46919         ** comparison function directly */
       
 46920         return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
       
 46921       }else{
       
 46922         const void *v1, *v2;
       
 46923         int n1, n2;
       
 46924         Mem c1;
       
 46925         Mem c2;
       
 46926         memset(&c1, 0, sizeof(c1));
       
 46927         memset(&c2, 0, sizeof(c2));
       
 46928         sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
       
 46929         sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
       
 46930         v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
       
 46931         n1 = v1==0 ? 0 : c1.n;
       
 46932         v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
       
 46933         n2 = v2==0 ? 0 : c2.n;
       
 46934         rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
       
 46935         sqlite3VdbeMemRelease(&c1);
       
 46936         sqlite3VdbeMemRelease(&c2);
       
 46937         return rc;
       
 46938       }
       
 46939     }
       
 46940     /* If a NULL pointer was passed as the collate function, fall through
       
 46941     ** to the blob case and use memcmp().  */
       
 46942   }
       
 46943  
       
 46944   /* Both values must be blobs.  Compare using memcmp().  */
       
 46945   rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
       
 46946   if( rc==0 ){
       
 46947     rc = pMem1->n - pMem2->n;
       
 46948   }
       
 46949   return rc;
       
 46950 }
       
 46951 
       
 46952 /*
       
 46953 ** Move data out of a btree key or data field and into a Mem structure.
       
 46954 ** The data or key is taken from the entry that pCur is currently pointing
       
 46955 ** to.  offset and amt determine what portion of the data or key to retrieve.
       
 46956 ** key is true to get the key or false to get data.  The result is written
       
 46957 ** into the pMem element.
       
 46958 **
       
 46959 ** The pMem structure is assumed to be uninitialized.  Any prior content
       
 46960 ** is overwritten without being freed.
       
 46961 **
       
 46962 ** If this routine fails for any reason (malloc returns NULL or unable
       
 46963 ** to read from the disk) then the pMem is left in an inconsistent state.
       
 46964 */
       
 46965 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
       
 46966   BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
       
 46967   int offset,       /* Offset from the start of data to return bytes from. */
       
 46968   int amt,          /* Number of bytes to return. */
       
 46969   int key,          /* If true, retrieve from the btree key, not data. */
       
 46970   Mem *pMem         /* OUT: Return data in this Mem structure. */
       
 46971 ){
       
 46972   char *zData;        /* Data from the btree layer */
       
 46973   int available = 0;  /* Number of bytes available on the local btree page */
       
 46974   int rc = SQLITE_OK; /* Return code */
       
 46975 
       
 46976   assert( sqlite3BtreeCursorIsValid(pCur) );
       
 46977 
       
 46978   /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() 
       
 46979   ** that both the BtShared and database handle mutexes are held. */
       
 46980   assert( (pMem->flags & MEM_RowSet)==0 );
       
 46981   if( key ){
       
 46982     zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
       
 46983   }else{
       
 46984     zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
       
 46985   }
       
 46986   assert( zData!=0 );
       
 46987 
       
 46988   if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
       
 46989     sqlite3VdbeMemRelease(pMem);
       
 46990     pMem->z = &zData[offset];
       
 46991     pMem->flags = MEM_Blob|MEM_Ephem;
       
 46992   }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
       
 46993     pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
       
 46994     pMem->enc = 0;
       
 46995     pMem->type = SQLITE_BLOB;
       
 46996     if( key ){
       
 46997       rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
       
 46998     }else{
       
 46999       rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
       
 47000     }
       
 47001     pMem->z[amt] = 0;
       
 47002     pMem->z[amt+1] = 0;
       
 47003     if( rc!=SQLITE_OK ){
       
 47004       sqlite3VdbeMemRelease(pMem);
       
 47005     }
       
 47006   }
       
 47007   pMem->n = amt;
       
 47008 
       
 47009   return rc;
       
 47010 }
       
 47011 
       
 47012 /* This function is only available internally, it is not part of the
       
 47013 ** external API. It works in a similar way to sqlite3_value_text(),
       
 47014 ** except the data returned is in the encoding specified by the second
       
 47015 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
       
 47016 ** SQLITE_UTF8.
       
 47017 **
       
 47018 ** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
       
 47019 ** If that is the case, then the result must be aligned on an even byte
       
 47020 ** boundary.
       
 47021 */
       
 47022 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
       
 47023   if( !pVal ) return 0;
       
 47024 
       
 47025   assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
       
 47026   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
       
 47027   assert( (pVal->flags & MEM_RowSet)==0 );
       
 47028 
       
 47029   if( pVal->flags&MEM_Null ){
       
 47030     return 0;
       
 47031   }
       
 47032   assert( (MEM_Blob>>3) == MEM_Str );
       
 47033   pVal->flags |= (pVal->flags & MEM_Blob)>>3;
       
 47034   expandBlob(pVal);
       
 47035   if( pVal->flags&MEM_Str ){
       
 47036     sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
       
 47037     if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
       
 47038       assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
       
 47039       if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
       
 47040         return 0;
       
 47041       }
       
 47042     }
       
 47043     sqlite3VdbeMemNulTerminate(pVal);
       
 47044   }else{
       
 47045     assert( (pVal->flags&MEM_Blob)==0 );
       
 47046     sqlite3VdbeMemStringify(pVal, enc);
       
 47047     assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
       
 47048   }
       
 47049   assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
       
 47050               || pVal->db->mallocFailed );
       
 47051   if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
       
 47052     return pVal->z;
       
 47053   }else{
       
 47054     return 0;
       
 47055   }
       
 47056 }
       
 47057 
       
 47058 /*
       
 47059 ** Create a new sqlite3_value object.
       
 47060 */
       
 47061 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
       
 47062   Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
       
 47063   if( p ){
       
 47064     p->flags = MEM_Null;
       
 47065     p->type = SQLITE_NULL;
       
 47066     p->db = db;
       
 47067   }
       
 47068   return p;
       
 47069 }
       
 47070 
       
 47071 /*
       
 47072 ** Create a new sqlite3_value object, containing the value of pExpr.
       
 47073 **
       
 47074 ** This only works for very simple expressions that consist of one constant
       
 47075 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
       
 47076 ** be converted directly into a value, then the value is allocated and
       
 47077 ** a pointer written to *ppVal. The caller is responsible for deallocating
       
 47078 ** the value by passing it to sqlite3ValueFree() later on. If the expression
       
 47079 ** cannot be converted to a value, then *ppVal is set to NULL.
       
 47080 */
       
 47081 SQLITE_PRIVATE int sqlite3ValueFromExpr(
       
 47082   sqlite3 *db,              /* The database connection */
       
 47083   Expr *pExpr,              /* The expression to evaluate */
       
 47084   u8 enc,                   /* Encoding to use */
       
 47085   u8 affinity,              /* Affinity to use */
       
 47086   sqlite3_value **ppVal     /* Write the new value here */
       
 47087 ){
       
 47088   int op;
       
 47089   char *zVal = 0;
       
 47090   sqlite3_value *pVal = 0;
       
 47091 
       
 47092   if( !pExpr ){
       
 47093     *ppVal = 0;
       
 47094     return SQLITE_OK;
       
 47095   }
       
 47096   op = pExpr->op;
       
 47097   if( op==TK_REGISTER ){
       
 47098     op = pExpr->op2;
       
 47099   }
       
 47100 
       
 47101   if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
       
 47102     pVal = sqlite3ValueNew(db);
       
 47103     if( pVal==0 ) goto no_mem;
       
 47104     if( ExprHasProperty(pExpr, EP_IntValue) ){
       
 47105       sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue);
       
 47106     }else{
       
 47107       zVal = sqlite3DbStrDup(db, pExpr->u.zToken);
       
 47108       if( zVal==0 ) goto no_mem;
       
 47109       sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
       
 47110       if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
       
 47111     }
       
 47112     if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
       
 47113       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
       
 47114     }else{
       
 47115       sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
       
 47116     }
       
 47117     if( enc!=SQLITE_UTF8 ){
       
 47118       sqlite3VdbeChangeEncoding(pVal, enc);
       
 47119     }
       
 47120   }else if( op==TK_UMINUS ) {
       
 47121     if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
       
 47122       pVal->u.i = -1 * pVal->u.i;
       
 47123       /* (double)-1 In case of SQLITE_OMIT_FLOATING_POINT... */
       
 47124       pVal->r = (double)-1 * pVal->r;
       
 47125     }
       
 47126   }
       
 47127 #ifndef SQLITE_OMIT_BLOB_LITERAL
       
 47128   else if( op==TK_BLOB ){
       
 47129     int nVal;
       
 47130     assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
       
 47131     assert( pExpr->u.zToken[1]=='\'' );
       
 47132     pVal = sqlite3ValueNew(db);
       
 47133     if( !pVal ) goto no_mem;
       
 47134     zVal = &pExpr->u.zToken[2];
       
 47135     nVal = sqlite3Strlen30(zVal)-1;
       
 47136     assert( zVal[nVal]=='\'' );
       
 47137     sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
       
 47138                          0, SQLITE_DYNAMIC);
       
 47139   }
       
 47140 #endif
       
 47141 
       
 47142   *ppVal = pVal;
       
 47143   return SQLITE_OK;
       
 47144 
       
 47145 no_mem:
       
 47146   db->mallocFailed = 1;
       
 47147   sqlite3DbFree(db, zVal);
       
 47148   sqlite3ValueFree(pVal);
       
 47149   *ppVal = 0;
       
 47150   return SQLITE_NOMEM;
       
 47151 }
       
 47152 
       
 47153 /*
       
 47154 ** Change the string value of an sqlite3_value object
       
 47155 */
       
 47156 SQLITE_PRIVATE void sqlite3ValueSetStr(
       
 47157   sqlite3_value *v,     /* Value to be set */
       
 47158   int n,                /* Length of string z */
       
 47159   const void *z,        /* Text of the new string */
       
 47160   u8 enc,               /* Encoding to use */
       
 47161   void (*xDel)(void*)   /* Destructor for the string */
       
 47162 ){
       
 47163   if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
       
 47164 }
       
 47165 
       
 47166 /*
       
 47167 ** Free an sqlite3_value object
       
 47168 */
       
 47169 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
       
 47170   if( !v ) return;
       
 47171   sqlite3VdbeMemRelease((Mem *)v);
       
 47172   sqlite3DbFree(((Mem*)v)->db, v);
       
 47173 }
       
 47174 
       
 47175 /*
       
 47176 ** Return the number of bytes in the sqlite3_value object assuming
       
 47177 ** that it uses the encoding "enc"
       
 47178 */
       
 47179 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
       
 47180   Mem *p = (Mem*)pVal;
       
 47181   if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
       
 47182     if( p->flags & MEM_Zero ){
       
 47183       return p->n + p->u.nZero;
       
 47184     }else{
       
 47185       return p->n;
       
 47186     }
       
 47187   }
       
 47188   return 0;
       
 47189 }
       
 47190 
       
 47191 /************** End of vdbemem.c *********************************************/
       
 47192 /************** Begin file vdbeaux.c *****************************************/
       
 47193 /*
       
 47194 ** 2003 September 6
       
 47195 **
       
 47196 ** The author disclaims copyright to this source code.  In place of
       
 47197 ** a legal notice, here is a blessing:
       
 47198 **
       
 47199 **    May you do good and not evil.
       
 47200 **    May you find forgiveness for yourself and forgive others.
       
 47201 **    May you share freely, never taking more than you give.
       
 47202 **
       
 47203 *************************************************************************
       
 47204 ** This file contains code used for creating, destroying, and populating
       
 47205 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)  Prior
       
 47206 ** to version 2.8.7, all this code was combined into the vdbe.c source file.
       
 47207 ** But that file was getting too big so this subroutines were split out.
       
 47208 **
       
 47209 ** $Id: vdbeaux.c,v 1.480 2009/08/08 18:01:08 drh Exp $
       
 47210 */
       
 47211 
       
 47212 
       
 47213 
       
 47214 /*
       
 47215 ** When debugging the code generator in a symbolic debugger, one can
       
 47216 ** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed
       
 47217 ** as they are added to the instruction stream.
       
 47218 */
       
 47219 #ifdef SQLITE_DEBUG
       
 47220 SQLITE_PRIVATE int sqlite3VdbeAddopTrace = 0;
       
 47221 #endif
       
 47222 
       
 47223 
       
 47224 /*
       
 47225 ** Create a new virtual database engine.
       
 47226 */
       
 47227 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3 *db){
       
 47228   Vdbe *p;
       
 47229   p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
       
 47230   if( p==0 ) return 0;
       
 47231   p->db = db;
       
 47232   if( db->pVdbe ){
       
 47233     db->pVdbe->pPrev = p;
       
 47234   }
       
 47235   p->pNext = db->pVdbe;
       
 47236   p->pPrev = 0;
       
 47237   db->pVdbe = p;
       
 47238   p->magic = VDBE_MAGIC_INIT;
       
 47239   return p;
       
 47240 }
       
 47241 
       
 47242 /*
       
 47243 ** Remember the SQL string for a prepared statement.
       
 47244 */
       
 47245 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
       
 47246   if( p==0 ) return;
       
 47247 #ifdef SQLITE_OMIT_TRACE
       
 47248   if( !isPrepareV2 ) return;
       
 47249 #endif
       
 47250   assert( p->zSql==0 );
       
 47251   p->zSql = sqlite3DbStrNDup(p->db, z, n);
       
 47252   p->isPrepareV2 = isPrepareV2 ? 1 : 0;
       
 47253 }
       
 47254 
       
 47255 /*
       
 47256 ** Return the SQL associated with a prepared statement
       
 47257 */
       
 47258 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
       
 47259   Vdbe *p = (Vdbe *)pStmt;
       
 47260   return (p->isPrepareV2 ? p->zSql : 0);
       
 47261 }
       
 47262 
       
 47263 /*
       
 47264 ** Swap all content between two VDBE structures.
       
 47265 */
       
 47266 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
       
 47267   Vdbe tmp, *pTmp;
       
 47268   char *zTmp;
       
 47269   tmp = *pA;
       
 47270   *pA = *pB;
       
 47271   *pB = tmp;
       
 47272   pTmp = pA->pNext;
       
 47273   pA->pNext = pB->pNext;
       
 47274   pB->pNext = pTmp;
       
 47275   pTmp = pA->pPrev;
       
 47276   pA->pPrev = pB->pPrev;
       
 47277   pB->pPrev = pTmp;
       
 47278   zTmp = pA->zSql;
       
 47279   pA->zSql = pB->zSql;
       
 47280   pB->zSql = zTmp;
       
 47281   pB->isPrepareV2 = pA->isPrepareV2;
       
 47282 }
       
 47283 
       
 47284 #ifdef SQLITE_DEBUG
       
 47285 /*
       
 47286 ** Turn tracing on or off
       
 47287 */
       
 47288 SQLITE_PRIVATE void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
       
 47289   p->trace = trace;
       
 47290 }
       
 47291 #endif
       
 47292 
       
 47293 /*
       
 47294 ** Resize the Vdbe.aOp array so that it is at least one op larger than 
       
 47295 ** it was.
       
 47296 **
       
 47297 ** If an out-of-memory error occurs while resizing the array, return
       
 47298 ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain 
       
 47299 ** unchanged (this is so that any opcodes already allocated can be 
       
 47300 ** correctly deallocated along with the rest of the Vdbe).
       
 47301 */
       
 47302 static int growOpArray(Vdbe *p){
       
 47303   VdbeOp *pNew;
       
 47304   int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
       
 47305   pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
       
 47306   if( pNew ){
       
 47307     p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
       
 47308     p->aOp = pNew;
       
 47309   }
       
 47310   return (pNew ? SQLITE_OK : SQLITE_NOMEM);
       
 47311 }
       
 47312 
       
 47313 /*
       
 47314 ** Add a new instruction to the list of instructions current in the
       
 47315 ** VDBE.  Return the address of the new instruction.
       
 47316 **
       
 47317 ** Parameters:
       
 47318 **
       
 47319 **    p               Pointer to the VDBE
       
 47320 **
       
 47321 **    op              The opcode for this instruction
       
 47322 **
       
 47323 **    p1, p2, p3      Operands
       
 47324 **
       
 47325 ** Use the sqlite3VdbeResolveLabel() function to fix an address and
       
 47326 ** the sqlite3VdbeChangeP4() function to change the value of the P4
       
 47327 ** operand.
       
 47328 */
       
 47329 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
       
 47330   int i;
       
 47331   VdbeOp *pOp;
       
 47332 
       
 47333   i = p->nOp;
       
 47334   assert( p->magic==VDBE_MAGIC_INIT );
       
 47335   assert( op>0 && op<0xff );
       
 47336   if( p->nOpAlloc<=i ){
       
 47337     if( growOpArray(p) ){
       
 47338       return 1;
       
 47339     }
       
 47340   }
       
 47341   p->nOp++;
       
 47342   pOp = &p->aOp[i];
       
 47343   pOp->opcode = (u8)op;
       
 47344   pOp->p5 = 0;
       
 47345   pOp->p1 = p1;
       
 47346   pOp->p2 = p2;
       
 47347   pOp->p3 = p3;
       
 47348   pOp->p4.p = 0;
       
 47349   pOp->p4type = P4_NOTUSED;
       
 47350   p->expired = 0;
       
 47351 #ifdef SQLITE_DEBUG
       
 47352   pOp->zComment = 0;
       
 47353   if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
       
 47354 #endif
       
 47355 #ifdef VDBE_PROFILE
       
 47356   pOp->cycles = 0;
       
 47357   pOp->cnt = 0;
       
 47358 #endif
       
 47359   return i;
       
 47360 }
       
 47361 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
       
 47362   return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
       
 47363 }
       
 47364 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
       
 47365   return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
       
 47366 }
       
 47367 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
       
 47368   return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
       
 47369 }
       
 47370 
       
 47371 
       
 47372 /*
       
 47373 ** Add an opcode that includes the p4 value as a pointer.
       
 47374 */
       
 47375 SQLITE_PRIVATE int sqlite3VdbeAddOp4(
       
 47376   Vdbe *p,            /* Add the opcode to this VM */
       
 47377   int op,             /* The new opcode */
       
 47378   int p1,             /* The P1 operand */
       
 47379   int p2,             /* The P2 operand */
       
 47380   int p3,             /* The P3 operand */
       
 47381   const char *zP4,    /* The P4 operand */
       
 47382   int p4type          /* P4 operand type */
       
 47383 ){
       
 47384   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
       
 47385   sqlite3VdbeChangeP4(p, addr, zP4, p4type);
       
 47386   return addr;
       
 47387 }
       
 47388 
       
 47389 /*
       
 47390 ** Create a new symbolic label for an instruction that has yet to be
       
 47391 ** coded.  The symbolic label is really just a negative number.  The
       
 47392 ** label can be used as the P2 value of an operation.  Later, when
       
 47393 ** the label is resolved to a specific address, the VDBE will scan
       
 47394 ** through its operation list and change all values of P2 which match
       
 47395 ** the label into the resolved address.
       
 47396 **
       
 47397 ** The VDBE knows that a P2 value is a label because labels are
       
 47398 ** always negative and P2 values are suppose to be non-negative.
       
 47399 ** Hence, a negative P2 value is a label that has yet to be resolved.
       
 47400 **
       
 47401 ** Zero is returned if a malloc() fails.
       
 47402 */
       
 47403 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *p){
       
 47404   int i;
       
 47405   i = p->nLabel++;
       
 47406   assert( p->magic==VDBE_MAGIC_INIT );
       
 47407   if( i>=p->nLabelAlloc ){
       
 47408     int n = p->nLabelAlloc*2 + 5;
       
 47409     p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
       
 47410                                        n*sizeof(p->aLabel[0]));
       
 47411     p->nLabelAlloc = sqlite3DbMallocSize(p->db, p->aLabel)/sizeof(p->aLabel[0]);
       
 47412   }
       
 47413   if( p->aLabel ){
       
 47414     p->aLabel[i] = -1;
       
 47415   }
       
 47416   return -1-i;
       
 47417 }
       
 47418 
       
 47419 /*
       
 47420 ** Resolve label "x" to be the address of the next instruction to
       
 47421 ** be inserted.  The parameter "x" must have been obtained from
       
 47422 ** a prior call to sqlite3VdbeMakeLabel().
       
 47423 */
       
 47424 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *p, int x){
       
 47425   int j = -1-x;
       
 47426   assert( p->magic==VDBE_MAGIC_INIT );
       
 47427   assert( j>=0 && j<p->nLabel );
       
 47428   if( p->aLabel ){
       
 47429     p->aLabel[j] = p->nOp;
       
 47430   }
       
 47431 }
       
 47432 
       
 47433 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
       
 47434 
       
 47435 /*
       
 47436 ** The following type and function are used to iterate through all opcodes
       
 47437 ** in a Vdbe main program and each of the sub-programs (triggers) it may 
       
 47438 ** invoke directly or indirectly. It should be used as follows:
       
 47439 **
       
 47440 **   Op *pOp;
       
 47441 **   VdbeOpIter sIter;
       
 47442 **
       
 47443 **   memset(&sIter, 0, sizeof(sIter));
       
 47444 **   sIter.v = v;                            // v is of type Vdbe* 
       
 47445 **   while( (pOp = opIterNext(&sIter)) ){
       
 47446 **     // Do something with pOp
       
 47447 **   }
       
 47448 **   sqlite3DbFree(v->db, sIter.apSub);
       
 47449 ** 
       
 47450 */
       
 47451 typedef struct VdbeOpIter VdbeOpIter;
       
 47452 struct VdbeOpIter {
       
 47453   Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
       
 47454   SubProgram **apSub;        /* Array of subprograms */
       
 47455   int nSub;                  /* Number of entries in apSub */
       
 47456   int iAddr;                 /* Address of next instruction to return */
       
 47457   int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
       
 47458 };
       
 47459 static Op *opIterNext(VdbeOpIter *p){
       
 47460   Vdbe *v = p->v;
       
 47461   Op *pRet = 0;
       
 47462   Op *aOp;
       
 47463   int nOp;
       
 47464 
       
 47465   if( p->iSub<=p->nSub ){
       
 47466 
       
 47467     if( p->iSub==0 ){
       
 47468       aOp = v->aOp;
       
 47469       nOp = v->nOp;
       
 47470     }else{
       
 47471       aOp = p->apSub[p->iSub-1]->aOp;
       
 47472       nOp = p->apSub[p->iSub-1]->nOp;
       
 47473     }
       
 47474     assert( p->iAddr<nOp );
       
 47475 
       
 47476     pRet = &aOp[p->iAddr];
       
 47477     p->iAddr++;
       
 47478     if( p->iAddr==nOp ){
       
 47479       p->iSub++;
       
 47480       p->iAddr = 0;
       
 47481     }
       
 47482   
       
 47483     if( pRet->p4type==P4_SUBPROGRAM ){
       
 47484       int nByte = (p->nSub+1)*sizeof(SubProgram*);
       
 47485       int j;
       
 47486       for(j=0; j<p->nSub; j++){
       
 47487         if( p->apSub[j]==pRet->p4.pProgram ) break;
       
 47488       }
       
 47489       if( j==p->nSub ){
       
 47490         p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
       
 47491         if( !p->apSub ){
       
 47492           pRet = 0;
       
 47493         }else{
       
 47494           p->apSub[p->nSub++] = pRet->p4.pProgram;
       
 47495         }
       
 47496       }
       
 47497     }
       
 47498   }
       
 47499 
       
 47500   return pRet;
       
 47501 }
       
 47502 
       
 47503 /*
       
 47504 ** Check if the program stored in the VM associated with pParse may
       
 47505 ** throw an ABORT exception (causing the statement, but not entire transaction
       
 47506 ** to be rolled back). This condition is true if the main program or any
       
 47507 ** sub-programs contains any of the following:
       
 47508 **
       
 47509 **   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
       
 47510 **   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
       
 47511 **   *  OP_Destroy
       
 47512 **   *  OP_VUpdate
       
 47513 **   *  OP_VRename
       
 47514 **   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
       
 47515 **
       
 47516 ** Then check that the value of Parse.mayAbort is true if an
       
 47517 ** ABORT may be thrown, or false otherwise. Return true if it does
       
 47518 ** match, or false otherwise. This function is intended to be used as
       
 47519 ** part of an assert statement in the compiler. Similar to:
       
 47520 **
       
 47521 **   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
       
 47522 */
       
 47523 SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
       
 47524   int hasAbort = 0;
       
 47525   Op *pOp;
       
 47526   VdbeOpIter sIter;
       
 47527   memset(&sIter, 0, sizeof(sIter));
       
 47528   sIter.v = v;
       
 47529 
       
 47530   while( (pOp = opIterNext(&sIter))!=0 ){
       
 47531     int opcode = pOp->opcode;
       
 47532     if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 
       
 47533 #ifndef SQLITE_OMIT_FOREIGN_KEY
       
 47534      || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1) 
       
 47535 #endif
       
 47536      || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 
       
 47537       && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
       
 47538     ){
       
 47539       hasAbort = 1;
       
 47540       break;
       
 47541     }
       
 47542   }
       
 47543   sqlite3DbFree(v->db, sIter.apSub);
       
 47544 
       
 47545   /* Return true if hasAbort==mayAbort. Or if a malloc failure occured.
       
 47546   ** If malloc failed, then the while() loop above may not have iterated
       
 47547   ** through all opcodes and hasAbort may be set incorrectly. Return
       
 47548   ** true for this case to prevent the assert() in the callers frame
       
 47549   ** from failing.  */
       
 47550   return ( v->db->mallocFailed || hasAbort==mayAbort );
       
 47551 }
       
 47552 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
       
 47553 
       
 47554 /*
       
 47555 ** Loop through the program looking for P2 values that are negative
       
 47556 ** on jump instructions.  Each such value is a label.  Resolve the
       
 47557 ** label by setting the P2 value to its correct non-zero value.
       
 47558 **
       
 47559 ** This routine is called once after all opcodes have been inserted.
       
 47560 **
       
 47561 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument 
       
 47562 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by 
       
 47563 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
       
 47564 */
       
 47565 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
       
 47566   int i;
       
 47567   int nMaxArgs = *pMaxFuncArgs;
       
 47568   Op *pOp;
       
 47569   int *aLabel = p->aLabel;
       
 47570   p->readOnly = 1;
       
 47571   for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
       
 47572     u8 opcode = pOp->opcode;
       
 47573 
       
 47574     if( opcode==OP_Function || opcode==OP_AggStep ){
       
 47575       if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
       
 47576 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 47577     }else if( opcode==OP_VUpdate ){
       
 47578       if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
       
 47579 #endif
       
 47580     }else if( opcode==OP_Transaction && pOp->p2!=0 ){
       
 47581       p->readOnly = 0;
       
 47582 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 47583     }else if( opcode==OP_VFilter ){
       
 47584       int n;
       
 47585       assert( p->nOp - i >= 3 );
       
 47586       assert( pOp[-1].opcode==OP_Integer );
       
 47587       n = pOp[-1].p1;
       
 47588       if( n>nMaxArgs ) nMaxArgs = n;
       
 47589 #endif
       
 47590     }
       
 47591 
       
 47592     if( sqlite3VdbeOpcodeHasProperty(opcode, OPFLG_JUMP) && pOp->p2<0 ){
       
 47593       assert( -1-pOp->p2<p->nLabel );
       
 47594       pOp->p2 = aLabel[-1-pOp->p2];
       
 47595     }
       
 47596   }
       
 47597   sqlite3DbFree(p->db, p->aLabel);
       
 47598   p->aLabel = 0;
       
 47599 
       
 47600   *pMaxFuncArgs = nMaxArgs;
       
 47601 }
       
 47602 
       
 47603 /*
       
 47604 ** Return the address of the next instruction to be inserted.
       
 47605 */
       
 47606 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
       
 47607   assert( p->magic==VDBE_MAGIC_INIT );
       
 47608   return p->nOp;
       
 47609 }
       
 47610 
       
 47611 /*
       
 47612 ** This function returns a pointer to the array of opcodes associated with
       
 47613 ** the Vdbe passed as the first argument. It is the callers responsibility
       
 47614 ** to arrange for the returned array to be eventually freed using the 
       
 47615 ** vdbeFreeOpArray() function.
       
 47616 **
       
 47617 ** Before returning, *pnOp is set to the number of entries in the returned
       
 47618 ** array. Also, *pnMaxArg is set to the larger of its current value and 
       
 47619 ** the number of entries in the Vdbe.apArg[] array required to execute the 
       
 47620 ** returned program.
       
 47621 */
       
 47622 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
       
 47623   VdbeOp *aOp = p->aOp;
       
 47624   assert( aOp && !p->db->mallocFailed );
       
 47625 
       
 47626   /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
       
 47627   assert( p->aMutex.nMutex==0 );
       
 47628 
       
 47629   resolveP2Values(p, pnMaxArg);
       
 47630   *pnOp = p->nOp;
       
 47631   p->aOp = 0;
       
 47632   return aOp;
       
 47633 }
       
 47634 
       
 47635 /*
       
 47636 ** Add a whole list of operations to the operation stack.  Return the
       
 47637 ** address of the first operation added.
       
 47638 */
       
 47639 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
       
 47640   int addr;
       
 47641   assert( p->magic==VDBE_MAGIC_INIT );
       
 47642   if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
       
 47643     return 0;
       
 47644   }
       
 47645   addr = p->nOp;
       
 47646   if( ALWAYS(nOp>0) ){
       
 47647     int i;
       
 47648     VdbeOpList const *pIn = aOp;
       
 47649     for(i=0; i<nOp; i++, pIn++){
       
 47650       int p2 = pIn->p2;
       
 47651       VdbeOp *pOut = &p->aOp[i+addr];
       
 47652       pOut->opcode = pIn->opcode;
       
 47653       pOut->p1 = pIn->p1;
       
 47654       if( p2<0 && sqlite3VdbeOpcodeHasProperty(pOut->opcode, OPFLG_JUMP) ){
       
 47655         pOut->p2 = addr + ADDR(p2);
       
 47656       }else{
       
 47657         pOut->p2 = p2;
       
 47658       }
       
 47659       pOut->p3 = pIn->p3;
       
 47660       pOut->p4type = P4_NOTUSED;
       
 47661       pOut->p4.p = 0;
       
 47662       pOut->p5 = 0;
       
 47663 #ifdef SQLITE_DEBUG
       
 47664       pOut->zComment = 0;
       
 47665       if( sqlite3VdbeAddopTrace ){
       
 47666         sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
       
 47667       }
       
 47668 #endif
       
 47669     }
       
 47670     p->nOp += nOp;
       
 47671   }
       
 47672   return addr;
       
 47673 }
       
 47674 
       
 47675 /*
       
 47676 ** Change the value of the P1 operand for a specific instruction.
       
 47677 ** This routine is useful when a large program is loaded from a
       
 47678 ** static array using sqlite3VdbeAddOpList but we want to make a
       
 47679 ** few minor changes to the program.
       
 47680 */
       
 47681 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
       
 47682   assert( p!=0 );
       
 47683   assert( addr>=0 );
       
 47684   if( p->nOp>addr ){
       
 47685     p->aOp[addr].p1 = val;
       
 47686   }
       
 47687 }
       
 47688 
       
 47689 /*
       
 47690 ** Change the value of the P2 operand for a specific instruction.
       
 47691 ** This routine is useful for setting a jump destination.
       
 47692 */
       
 47693 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
       
 47694   assert( p!=0 );
       
 47695   assert( addr>=0 );
       
 47696   if( p->nOp>addr ){
       
 47697     p->aOp[addr].p2 = val;
       
 47698   }
       
 47699 }
       
 47700 
       
 47701 /*
       
 47702 ** Change the value of the P3 operand for a specific instruction.
       
 47703 */
       
 47704 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
       
 47705   assert( p!=0 );
       
 47706   assert( addr>=0 );
       
 47707   if( p->nOp>addr ){
       
 47708     p->aOp[addr].p3 = val;
       
 47709   }
       
 47710 }
       
 47711 
       
 47712 /*
       
 47713 ** Change the value of the P5 operand for the most recently
       
 47714 ** added operation.
       
 47715 */
       
 47716 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
       
 47717   assert( p!=0 );
       
 47718   if( p->aOp ){
       
 47719     assert( p->nOp>0 );
       
 47720     p->aOp[p->nOp-1].p5 = val;
       
 47721   }
       
 47722 }
       
 47723 
       
 47724 /*
       
 47725 ** Change the P2 operand of instruction addr so that it points to
       
 47726 ** the address of the next instruction to be coded.
       
 47727 */
       
 47728 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
       
 47729   sqlite3VdbeChangeP2(p, addr, p->nOp);
       
 47730 }
       
 47731 
       
 47732 
       
 47733 /*
       
 47734 ** If the input FuncDef structure is ephemeral, then free it.  If
       
 47735 ** the FuncDef is not ephermal, then do nothing.
       
 47736 */
       
 47737 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
       
 47738   if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
       
 47739     sqlite3DbFree(db, pDef);
       
 47740   }
       
 47741 }
       
 47742 
       
 47743 /*
       
 47744 ** Delete a P4 value if necessary.
       
 47745 */
       
 47746 static void freeP4(sqlite3 *db, int p4type, void *p4){
       
 47747   if( p4 ){
       
 47748     switch( p4type ){
       
 47749       case P4_REAL:
       
 47750       case P4_INT64:
       
 47751       case P4_MPRINTF:
       
 47752       case P4_DYNAMIC:
       
 47753       case P4_KEYINFO:
       
 47754       case P4_INTARRAY:
       
 47755       case P4_KEYINFO_HANDOFF: {
       
 47756         sqlite3DbFree(db, p4);
       
 47757         break;
       
 47758       }
       
 47759       case P4_VDBEFUNC: {
       
 47760         VdbeFunc *pVdbeFunc = (VdbeFunc *)p4;
       
 47761         freeEphemeralFunction(db, pVdbeFunc->pFunc);
       
 47762         sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
       
 47763         sqlite3DbFree(db, pVdbeFunc);
       
 47764         break;
       
 47765       }
       
 47766       case P4_FUNCDEF: {
       
 47767         freeEphemeralFunction(db, (FuncDef*)p4);
       
 47768         break;
       
 47769       }
       
 47770       case P4_MEM: {
       
 47771         sqlite3ValueFree((sqlite3_value*)p4);
       
 47772         break;
       
 47773       }
       
 47774       case P4_VTAB : {
       
 47775         sqlite3VtabUnlock((VTable *)p4);
       
 47776         break;
       
 47777       }
       
 47778       case P4_SUBPROGRAM : {
       
 47779         sqlite3VdbeProgramDelete(db, (SubProgram *)p4, 1);
       
 47780         break;
       
 47781       }
       
 47782     }
       
 47783   }
       
 47784 }
       
 47785 
       
 47786 /*
       
 47787 ** Free the space allocated for aOp and any p4 values allocated for the
       
 47788 ** opcodes contained within. If aOp is not NULL it is assumed to contain 
       
 47789 ** nOp entries. 
       
 47790 */
       
 47791 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
       
 47792   if( aOp ){
       
 47793     Op *pOp;
       
 47794     for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
       
 47795       freeP4(db, pOp->p4type, pOp->p4.p);
       
 47796 #ifdef SQLITE_DEBUG
       
 47797       sqlite3DbFree(db, pOp->zComment);
       
 47798 #endif     
       
 47799     }
       
 47800   }
       
 47801   sqlite3DbFree(db, aOp);
       
 47802 }
       
 47803 
       
 47804 /*
       
 47805 ** Decrement the ref-count on the SubProgram structure passed as the
       
 47806 ** second argument. If the ref-count reaches zero, free the structure.
       
 47807 **
       
 47808 ** The array of VDBE opcodes stored as SubProgram.aOp is freed if
       
 47809 ** either the ref-count reaches zero or parameter freeop is non-zero.
       
 47810 **
       
 47811 ** Since the array of opcodes pointed to by SubProgram.aOp may directly
       
 47812 ** or indirectly contain a reference to the SubProgram structure itself.
       
 47813 ** By passing a non-zero freeop parameter, the caller may ensure that all
       
 47814 ** SubProgram structures and their aOp arrays are freed, even when there
       
 47815 ** are such circular references.
       
 47816 */
       
 47817 SQLITE_PRIVATE void sqlite3VdbeProgramDelete(sqlite3 *db, SubProgram *p, int freeop){
       
 47818   if( p ){
       
 47819     assert( p->nRef>0 );
       
 47820     if( freeop || p->nRef==1 ){
       
 47821       Op *aOp = p->aOp;
       
 47822       p->aOp = 0;
       
 47823       vdbeFreeOpArray(db, aOp, p->nOp);
       
 47824       p->nOp = 0;
       
 47825     }
       
 47826     p->nRef--;
       
 47827     if( p->nRef==0 ){
       
 47828       sqlite3DbFree(db, p);
       
 47829     }
       
 47830   }
       
 47831 }
       
 47832 
       
 47833 
       
 47834 /*
       
 47835 ** Change N opcodes starting at addr to No-ops.
       
 47836 */
       
 47837 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
       
 47838   if( p->aOp ){
       
 47839     VdbeOp *pOp = &p->aOp[addr];
       
 47840     sqlite3 *db = p->db;
       
 47841     while( N-- ){
       
 47842       freeP4(db, pOp->p4type, pOp->p4.p);
       
 47843       memset(pOp, 0, sizeof(pOp[0]));
       
 47844       pOp->opcode = OP_Noop;
       
 47845       pOp++;
       
 47846     }
       
 47847   }
       
 47848 }
       
 47849 
       
 47850 /*
       
 47851 ** Change the value of the P4 operand for a specific instruction.
       
 47852 ** This routine is useful when a large program is loaded from a
       
 47853 ** static array using sqlite3VdbeAddOpList but we want to make a
       
 47854 ** few minor changes to the program.
       
 47855 **
       
 47856 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
       
 47857 ** the string is made into memory obtained from sqlite3_malloc().
       
 47858 ** A value of n==0 means copy bytes of zP4 up to and including the
       
 47859 ** first null byte.  If n>0 then copy n+1 bytes of zP4.
       
 47860 **
       
 47861 ** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
       
 47862 ** A copy is made of the KeyInfo structure into memory obtained from
       
 47863 ** sqlite3_malloc, to be freed when the Vdbe is finalized.
       
 47864 ** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
       
 47865 ** stored in memory that the caller has obtained from sqlite3_malloc. The 
       
 47866 ** caller should not free the allocation, it will be freed when the Vdbe is
       
 47867 ** finalized.
       
 47868 ** 
       
 47869 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
       
 47870 ** to a string or structure that is guaranteed to exist for the lifetime of
       
 47871 ** the Vdbe. In these cases we can just copy the pointer.
       
 47872 **
       
 47873 ** If addr<0 then change P4 on the most recently inserted instruction.
       
 47874 */
       
 47875 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
       
 47876   Op *pOp;
       
 47877   sqlite3 *db;
       
 47878   assert( p!=0 );
       
 47879   db = p->db;
       
 47880   assert( p->magic==VDBE_MAGIC_INIT );
       
 47881   if( p->aOp==0 || db->mallocFailed ){
       
 47882     if ( n!=P4_KEYINFO && n!=P4_VTAB ) {
       
 47883       freeP4(db, n, (void*)*(char**)&zP4);
       
 47884     }
       
 47885     return;
       
 47886   }
       
 47887   assert( p->nOp>0 );
       
 47888   assert( addr<p->nOp );
       
 47889   if( addr<0 ){
       
 47890     addr = p->nOp - 1;
       
 47891   }
       
 47892   pOp = &p->aOp[addr];
       
 47893   freeP4(db, pOp->p4type, pOp->p4.p);
       
 47894   pOp->p4.p = 0;
       
 47895   if( n==P4_INT32 ){
       
 47896     /* Note: this cast is safe, because the origin data point was an int
       
 47897     ** that was cast to a (const char *). */
       
 47898     pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
       
 47899     pOp->p4type = P4_INT32;
       
 47900   }else if( zP4==0 ){
       
 47901     pOp->p4.p = 0;
       
 47902     pOp->p4type = P4_NOTUSED;
       
 47903   }else if( n==P4_KEYINFO ){
       
 47904     KeyInfo *pKeyInfo;
       
 47905     int nField, nByte;
       
 47906 
       
 47907     nField = ((KeyInfo*)zP4)->nField;
       
 47908     nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
       
 47909     pKeyInfo = sqlite3Malloc( nByte );
       
 47910     pOp->p4.pKeyInfo = pKeyInfo;
       
 47911     if( pKeyInfo ){
       
 47912       u8 *aSortOrder;
       
 47913       memcpy(pKeyInfo, zP4, nByte);
       
 47914       aSortOrder = pKeyInfo->aSortOrder;
       
 47915       if( aSortOrder ){
       
 47916         pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
       
 47917         memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
       
 47918       }
       
 47919       pOp->p4type = P4_KEYINFO;
       
 47920     }else{
       
 47921       p->db->mallocFailed = 1;
       
 47922       pOp->p4type = P4_NOTUSED;
       
 47923     }
       
 47924   }else if( n==P4_KEYINFO_HANDOFF ){
       
 47925     pOp->p4.p = (void*)zP4;
       
 47926     pOp->p4type = P4_KEYINFO;
       
 47927   }else if( n==P4_VTAB ){
       
 47928     pOp->p4.p = (void*)zP4;
       
 47929     pOp->p4type = P4_VTAB;
       
 47930     sqlite3VtabLock((VTable *)zP4);
       
 47931     assert( ((VTable *)zP4)->db==p->db );
       
 47932   }else if( n<0 ){
       
 47933     pOp->p4.p = (void*)zP4;
       
 47934     pOp->p4type = (signed char)n;
       
 47935   }else{
       
 47936     if( n==0 ) n = sqlite3Strlen30(zP4);
       
 47937     pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
       
 47938     pOp->p4type = P4_DYNAMIC;
       
 47939   }
       
 47940 }
       
 47941 
       
 47942 #ifndef NDEBUG
       
 47943 /*
       
 47944 ** Change the comment on the the most recently coded instruction.  Or
       
 47945 ** insert a No-op and add the comment to that new instruction.  This
       
 47946 ** makes the code easier to read during debugging.  None of this happens
       
 47947 ** in a production build.
       
 47948 */
       
 47949 SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
       
 47950   va_list ap;
       
 47951   if( !p ) return;
       
 47952   assert( p->nOp>0 || p->aOp==0 );
       
 47953   assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
       
 47954   if( p->nOp ){
       
 47955     char **pz = &p->aOp[p->nOp-1].zComment;
       
 47956     va_start(ap, zFormat);
       
 47957     sqlite3DbFree(p->db, *pz);
       
 47958     *pz = sqlite3VMPrintf(p->db, zFormat, ap);
       
 47959     va_end(ap);
       
 47960   }
       
 47961 }
       
 47962 SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
       
 47963   va_list ap;
       
 47964   if( !p ) return;
       
 47965   sqlite3VdbeAddOp0(p, OP_Noop);
       
 47966   assert( p->nOp>0 || p->aOp==0 );
       
 47967   assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
       
 47968   if( p->nOp ){
       
 47969     char **pz = &p->aOp[p->nOp-1].zComment;
       
 47970     va_start(ap, zFormat);
       
 47971     sqlite3DbFree(p->db, *pz);
       
 47972     *pz = sqlite3VMPrintf(p->db, zFormat, ap);
       
 47973     va_end(ap);
       
 47974   }
       
 47975 }
       
 47976 #endif  /* NDEBUG */
       
 47977 
       
 47978 /*
       
 47979 ** Return the opcode for a given address.  If the address is -1, then
       
 47980 ** return the most recently inserted opcode.
       
 47981 **
       
 47982 ** If a memory allocation error has occurred prior to the calling of this
       
 47983 ** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
       
 47984 ** is readable and writable, but it has no effect.  The return of a dummy
       
 47985 ** opcode allows the call to continue functioning after a OOM fault without
       
 47986 ** having to check to see if the return from this routine is a valid pointer.
       
 47987 **
       
 47988 ** About the #ifdef SQLITE_OMIT_TRACE:  Normally, this routine is never called
       
 47989 ** unless p->nOp>0.  This is because in the absense of SQLITE_OMIT_TRACE,
       
 47990 ** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
       
 47991 ** a new VDBE is created.  So we are free to set addr to p->nOp-1 without
       
 47992 ** having to double-check to make sure that the result is non-negative. But
       
 47993 ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
       
 47994 ** check the value of p->nOp-1 before continuing.
       
 47995 */
       
 47996 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
       
 47997   static VdbeOp dummy;
       
 47998   assert( p->magic==VDBE_MAGIC_INIT );
       
 47999   if( addr<0 ){
       
 48000 #ifdef SQLITE_OMIT_TRACE
       
 48001     if( p->nOp==0 ) return &dummy;
       
 48002 #endif
       
 48003     addr = p->nOp - 1;
       
 48004   }
       
 48005   assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
       
 48006   if( p->db->mallocFailed ){
       
 48007     return &dummy;
       
 48008   }else{
       
 48009     return &p->aOp[addr];
       
 48010   }
       
 48011 }
       
 48012 
       
 48013 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
       
 48014      || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
       
 48015 /*
       
 48016 ** Compute a string that describes the P4 parameter for an opcode.
       
 48017 ** Use zTemp for any required temporary buffer space.
       
 48018 */
       
 48019 static char *displayP4(Op *pOp, char *zTemp, int nTemp){
       
 48020   char *zP4 = zTemp;
       
 48021   assert( nTemp>=20 );
       
 48022   switch( pOp->p4type ){
       
 48023     case P4_KEYINFO_STATIC:
       
 48024     case P4_KEYINFO: {
       
 48025       int i, j;
       
 48026       KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
       
 48027       sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
       
 48028       i = sqlite3Strlen30(zTemp);
       
 48029       for(j=0; j<pKeyInfo->nField; j++){
       
 48030         CollSeq *pColl = pKeyInfo->aColl[j];
       
 48031         if( pColl ){
       
 48032           int n = sqlite3Strlen30(pColl->zName);
       
 48033           if( i+n>nTemp-6 ){
       
 48034             memcpy(&zTemp[i],",...",4);
       
 48035             break;
       
 48036           }
       
 48037           zTemp[i++] = ',';
       
 48038           if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
       
 48039             zTemp[i++] = '-';
       
 48040           }
       
 48041           memcpy(&zTemp[i], pColl->zName,n+1);
       
 48042           i += n;
       
 48043         }else if( i+4<nTemp-6 ){
       
 48044           memcpy(&zTemp[i],",nil",4);
       
 48045           i += 4;
       
 48046         }
       
 48047       }
       
 48048       zTemp[i++] = ')';
       
 48049       zTemp[i] = 0;
       
 48050       assert( i<nTemp );
       
 48051       break;
       
 48052     }
       
 48053     case P4_COLLSEQ: {
       
 48054       CollSeq *pColl = pOp->p4.pColl;
       
 48055       sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
       
 48056       break;
       
 48057     }
       
 48058     case P4_FUNCDEF: {
       
 48059       FuncDef *pDef = pOp->p4.pFunc;
       
 48060       sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
       
 48061       break;
       
 48062     }
       
 48063     case P4_INT64: {
       
 48064       sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
       
 48065       break;
       
 48066     }
       
 48067     case P4_INT32: {
       
 48068       sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
       
 48069       break;
       
 48070     }
       
 48071     case P4_REAL: {
       
 48072       sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
       
 48073       break;
       
 48074     }
       
 48075     case P4_MEM: {
       
 48076       Mem *pMem = pOp->p4.pMem;
       
 48077       assert( (pMem->flags & MEM_Null)==0 );
       
 48078       if( pMem->flags & MEM_Str ){
       
 48079         zP4 = pMem->z;
       
 48080       }else if( pMem->flags & MEM_Int ){
       
 48081         sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
       
 48082       }else if( pMem->flags & MEM_Real ){
       
 48083         sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
       
 48084       }else{
       
 48085         assert( pMem->flags & MEM_Blob );
       
 48086         zP4 = "(blob)";
       
 48087       }
       
 48088       break;
       
 48089     }
       
 48090 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 48091     case P4_VTAB: {
       
 48092       sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
       
 48093       sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
       
 48094       break;
       
 48095     }
       
 48096 #endif
       
 48097     case P4_INTARRAY: {
       
 48098       sqlite3_snprintf(nTemp, zTemp, "intarray");
       
 48099       break;
       
 48100     }
       
 48101     case P4_SUBPROGRAM: {
       
 48102       sqlite3_snprintf(nTemp, zTemp, "program");
       
 48103       break;
       
 48104     }
       
 48105     default: {
       
 48106       zP4 = pOp->p4.z;
       
 48107       if( zP4==0 ){
       
 48108         zP4 = zTemp;
       
 48109         zTemp[0] = 0;
       
 48110       }
       
 48111     }
       
 48112   }
       
 48113   assert( zP4!=0 );
       
 48114   return zP4;
       
 48115 }
       
 48116 #endif
       
 48117 
       
 48118 /*
       
 48119 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
       
 48120 */
       
 48121 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
       
 48122   int mask;
       
 48123   assert( i>=0 && i<p->db->nDb && i<sizeof(u32)*8 );
       
 48124   assert( i<(int)sizeof(p->btreeMask)*8 );
       
 48125   mask = ((u32)1)<<i;
       
 48126   if( (p->btreeMask & mask)==0 ){
       
 48127     p->btreeMask |= mask;
       
 48128     sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
       
 48129   }
       
 48130 }
       
 48131 
       
 48132 
       
 48133 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
       
 48134 /*
       
 48135 ** Print a single opcode.  This routine is used for debugging only.
       
 48136 */
       
 48137 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
       
 48138   char *zP4;
       
 48139   char zPtr[50];
       
 48140   static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
       
 48141   if( pOut==0 ) pOut = stdout;
       
 48142   zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
       
 48143   fprintf(pOut, zFormat1, pc, 
       
 48144       sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
       
 48145 #ifdef SQLITE_DEBUG
       
 48146       pOp->zComment ? pOp->zComment : ""
       
 48147 #else
       
 48148       ""
       
 48149 #endif
       
 48150   );
       
 48151   fflush(pOut);
       
 48152 }
       
 48153 #endif
       
 48154 
       
 48155 /*
       
 48156 ** Release an array of N Mem elements
       
 48157 */
       
 48158 static void releaseMemArray(Mem *p, int N){
       
 48159   if( p && N ){
       
 48160     Mem *pEnd;
       
 48161     sqlite3 *db = p->db;
       
 48162     u8 malloc_failed = db->mallocFailed;
       
 48163     for(pEnd=&p[N]; p<pEnd; p++){
       
 48164       assert( (&p[1])==pEnd || p[0].db==p[1].db );
       
 48165 
       
 48166       /* This block is really an inlined version of sqlite3VdbeMemRelease()
       
 48167       ** that takes advantage of the fact that the memory cell value is 
       
 48168       ** being set to NULL after releasing any dynamic resources.
       
 48169       **
       
 48170       ** The justification for duplicating code is that according to 
       
 48171       ** callgrind, this causes a certain test case to hit the CPU 4.7 
       
 48172       ** percent less (x86 linux, gcc version 4.1.2, -O6) than if 
       
 48173       ** sqlite3MemRelease() were called from here. With -O2, this jumps
       
 48174       ** to 6.6 percent. The test case is inserting 1000 rows into a table 
       
 48175       ** with no indexes using a single prepared INSERT statement, bind() 
       
 48176       ** and reset(). Inserts are grouped into a transaction.
       
 48177       */
       
 48178       if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
       
 48179         sqlite3VdbeMemRelease(p);
       
 48180       }else if( p->zMalloc ){
       
 48181         sqlite3DbFree(db, p->zMalloc);
       
 48182         p->zMalloc = 0;
       
 48183       }
       
 48184 
       
 48185       p->flags = MEM_Null;
       
 48186     }
       
 48187     db->mallocFailed = malloc_failed;
       
 48188   }
       
 48189 }
       
 48190 
       
 48191 /*
       
 48192 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
       
 48193 ** allocated by the OP_Program opcode in sqlite3VdbeExec().
       
 48194 */
       
 48195 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
       
 48196   int i;
       
 48197   Mem *aMem = VdbeFrameMem(p);
       
 48198   VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
       
 48199   for(i=0; i<p->nChildCsr; i++){
       
 48200     sqlite3VdbeFreeCursor(p->v, apCsr[i]);
       
 48201   }
       
 48202   releaseMemArray(aMem, p->nChildMem);
       
 48203   sqlite3DbFree(p->v->db, p);
       
 48204 }
       
 48205 
       
 48206 
       
 48207 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
       
 48208 SQLITE_PRIVATE int sqlite3VdbeReleaseBuffers(Vdbe *p){
       
 48209   int ii;
       
 48210   int nFree = 0;
       
 48211   assert( sqlite3_mutex_held(p->db->mutex) );
       
 48212   for(ii=1; ii<=p->nMem; ii++){
       
 48213     Mem *pMem = &p->aMem[ii];
       
 48214     if( pMem->flags & MEM_RowSet ){
       
 48215       sqlite3RowSetClear(pMem->u.pRowSet);
       
 48216     }
       
 48217     if( pMem->z && pMem->flags&MEM_Dyn ){
       
 48218       assert( !pMem->xDel );
       
 48219       nFree += sqlite3DbMallocSize(pMem->db, pMem->z);
       
 48220       sqlite3VdbeMemRelease(pMem);
       
 48221     }
       
 48222   }
       
 48223   return nFree;
       
 48224 }
       
 48225 #endif
       
 48226 
       
 48227 #ifndef SQLITE_OMIT_EXPLAIN
       
 48228 /*
       
 48229 ** Give a listing of the program in the virtual machine.
       
 48230 **
       
 48231 ** The interface is the same as sqlite3VdbeExec().  But instead of
       
 48232 ** running the code, it invokes the callback once for each instruction.
       
 48233 ** This feature is used to implement "EXPLAIN".
       
 48234 **
       
 48235 ** When p->explain==1, each instruction is listed.  When
       
 48236 ** p->explain==2, only OP_Explain instructions are listed and these
       
 48237 ** are shown in a different format.  p->explain==2 is used to implement
       
 48238 ** EXPLAIN QUERY PLAN.
       
 48239 */
       
 48240 SQLITE_PRIVATE int sqlite3VdbeList(
       
 48241   Vdbe *p                   /* The VDBE */
       
 48242 ){
       
 48243   int nRow;                            /* Total number of rows to return */
       
 48244   int nSub = 0;                        /* Number of sub-vdbes seen so far */
       
 48245   SubProgram **apSub = 0;              /* Array of sub-vdbes */
       
 48246   Mem *pSub = 0;
       
 48247   sqlite3 *db = p->db;
       
 48248   int i;
       
 48249   int rc = SQLITE_OK;
       
 48250   Mem *pMem = p->pResultSet = &p->aMem[1];
       
 48251 
       
 48252   assert( p->explain );
       
 48253   assert( p->magic==VDBE_MAGIC_RUN );
       
 48254   assert( db->magic==SQLITE_MAGIC_BUSY );
       
 48255   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
       
 48256 
       
 48257   /* Even though this opcode does not use dynamic strings for
       
 48258   ** the result, result columns may become dynamic if the user calls
       
 48259   ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
       
 48260   */
       
 48261   releaseMemArray(pMem, 8);
       
 48262 
       
 48263   if( p->rc==SQLITE_NOMEM ){
       
 48264     /* This happens if a malloc() inside a call to sqlite3_column_text() or
       
 48265     ** sqlite3_column_text16() failed.  */
       
 48266     db->mallocFailed = 1;
       
 48267     return SQLITE_ERROR;
       
 48268   }
       
 48269 
       
 48270   /* Figure out total number of rows that will be returned by this 
       
 48271   ** EXPLAIN program.  */
       
 48272   nRow = p->nOp;
       
 48273   if( p->explain==1 ){
       
 48274     pSub = &p->aMem[9];
       
 48275     if( pSub->flags&MEM_Blob ){
       
 48276       nSub = pSub->n/sizeof(Vdbe*);
       
 48277       apSub = (SubProgram **)pSub->z;
       
 48278     }
       
 48279     for(i=0; i<nSub; i++){
       
 48280       nRow += apSub[i]->nOp;
       
 48281     }
       
 48282   }
       
 48283 
       
 48284   do{
       
 48285     i = p->pc++;
       
 48286   }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
       
 48287   if( i>=nRow ){
       
 48288     p->rc = SQLITE_OK;
       
 48289     rc = SQLITE_DONE;
       
 48290   }else if( db->u1.isInterrupted ){
       
 48291     p->rc = SQLITE_INTERRUPT;
       
 48292     rc = SQLITE_ERROR;
       
 48293     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
       
 48294   }else{
       
 48295     char *z;
       
 48296     Op *pOp;
       
 48297     if( i<p->nOp ){
       
 48298       pOp = &p->aOp[i];
       
 48299     }else{
       
 48300       int j;
       
 48301       i -= p->nOp;
       
 48302       for(j=0; i>=apSub[j]->nOp; j++){
       
 48303         i -= apSub[j]->nOp;
       
 48304       }
       
 48305       pOp = &apSub[j]->aOp[i];
       
 48306     }
       
 48307     if( p->explain==1 ){
       
 48308       pMem->flags = MEM_Int;
       
 48309       pMem->type = SQLITE_INTEGER;
       
 48310       pMem->u.i = i;                                /* Program counter */
       
 48311       pMem++;
       
 48312   
       
 48313       pMem->flags = MEM_Static|MEM_Str|MEM_Term;
       
 48314       pMem->z = (char*)sqlite3OpcodeName(pOp->opcode);  /* Opcode */
       
 48315       assert( pMem->z!=0 );
       
 48316       pMem->n = sqlite3Strlen30(pMem->z);
       
 48317       pMem->type = SQLITE_TEXT;
       
 48318       pMem->enc = SQLITE_UTF8;
       
 48319       pMem++;
       
 48320 
       
 48321       if( pOp->p4type==P4_SUBPROGRAM ){
       
 48322         int nByte = (nSub+1)*sizeof(SubProgram*);
       
 48323         int j;
       
 48324         for(j=0; j<nSub; j++){
       
 48325           if( apSub[j]==pOp->p4.pProgram ) break;
       
 48326         }
       
 48327         if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, 1) ){
       
 48328           apSub = (SubProgram **)pSub->z;
       
 48329           apSub[nSub++] = pOp->p4.pProgram;
       
 48330           pSub->flags |= MEM_Blob;
       
 48331           pSub->n = nSub*sizeof(SubProgram*);
       
 48332         }
       
 48333       }
       
 48334     }
       
 48335 
       
 48336     pMem->flags = MEM_Int;
       
 48337     pMem->u.i = pOp->p1;                          /* P1 */
       
 48338     pMem->type = SQLITE_INTEGER;
       
 48339     pMem++;
       
 48340 
       
 48341     pMem->flags = MEM_Int;
       
 48342     pMem->u.i = pOp->p2;                          /* P2 */
       
 48343     pMem->type = SQLITE_INTEGER;
       
 48344     pMem++;
       
 48345 
       
 48346     if( p->explain==1 ){
       
 48347       pMem->flags = MEM_Int;
       
 48348       pMem->u.i = pOp->p3;                          /* P3 */
       
 48349       pMem->type = SQLITE_INTEGER;
       
 48350       pMem++;
       
 48351     }
       
 48352 
       
 48353     if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
       
 48354       assert( p->db->mallocFailed );
       
 48355       return SQLITE_ERROR;
       
 48356     }
       
 48357     pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
       
 48358     z = displayP4(pOp, pMem->z, 32);
       
 48359     if( z!=pMem->z ){
       
 48360       sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
       
 48361     }else{
       
 48362       assert( pMem->z!=0 );
       
 48363       pMem->n = sqlite3Strlen30(pMem->z);
       
 48364       pMem->enc = SQLITE_UTF8;
       
 48365     }
       
 48366     pMem->type = SQLITE_TEXT;
       
 48367     pMem++;
       
 48368 
       
 48369     if( p->explain==1 ){
       
 48370       if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
       
 48371         assert( p->db->mallocFailed );
       
 48372         return SQLITE_ERROR;
       
 48373       }
       
 48374       pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
       
 48375       pMem->n = 2;
       
 48376       sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
       
 48377       pMem->type = SQLITE_TEXT;
       
 48378       pMem->enc = SQLITE_UTF8;
       
 48379       pMem++;
       
 48380   
       
 48381 #ifdef SQLITE_DEBUG
       
 48382       if( pOp->zComment ){
       
 48383         pMem->flags = MEM_Str|MEM_Term;
       
 48384         pMem->z = pOp->zComment;
       
 48385         pMem->n = sqlite3Strlen30(pMem->z);
       
 48386         pMem->enc = SQLITE_UTF8;
       
 48387         pMem->type = SQLITE_TEXT;
       
 48388       }else
       
 48389 #endif
       
 48390       {
       
 48391         pMem->flags = MEM_Null;                       /* Comment */
       
 48392         pMem->type = SQLITE_NULL;
       
 48393       }
       
 48394     }
       
 48395 
       
 48396     p->nResColumn = 8 - 5*(p->explain-1);
       
 48397     p->rc = SQLITE_OK;
       
 48398     rc = SQLITE_ROW;
       
 48399   }
       
 48400   return rc;
       
 48401 }
       
 48402 #endif /* SQLITE_OMIT_EXPLAIN */
       
 48403 
       
 48404 #ifdef SQLITE_DEBUG
       
 48405 /*
       
 48406 ** Print the SQL that was used to generate a VDBE program.
       
 48407 */
       
 48408 SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
       
 48409   int nOp = p->nOp;
       
 48410   VdbeOp *pOp;
       
 48411   if( nOp<1 ) return;
       
 48412   pOp = &p->aOp[0];
       
 48413   if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
       
 48414     const char *z = pOp->p4.z;
       
 48415     while( sqlite3Isspace(*z) ) z++;
       
 48416     printf("SQL: [%s]\n", z);
       
 48417   }
       
 48418 }
       
 48419 #endif
       
 48420 
       
 48421 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
       
 48422 /*
       
 48423 ** Print an IOTRACE message showing SQL content.
       
 48424 */
       
 48425 SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
       
 48426   int nOp = p->nOp;
       
 48427   VdbeOp *pOp;
       
 48428   if( sqlite3IoTrace==0 ) return;
       
 48429   if( nOp<1 ) return;
       
 48430   pOp = &p->aOp[0];
       
 48431   if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
       
 48432     int i, j;
       
 48433     char z[1000];
       
 48434     sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
       
 48435     for(i=0; sqlite3Isspace(z[i]); i++){}
       
 48436     for(j=0; z[i]; i++){
       
 48437       if( sqlite3Isspace(z[i]) ){
       
 48438         if( z[i-1]!=' ' ){
       
 48439           z[j++] = ' ';
       
 48440         }
       
 48441       }else{
       
 48442         z[j++] = z[i];
       
 48443       }
       
 48444     }
       
 48445     z[j] = 0;
       
 48446     sqlite3IoTrace("SQL %s\n", z);
       
 48447   }
       
 48448 }
       
 48449 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
       
 48450 
       
 48451 /*
       
 48452 ** Allocate space from a fixed size buffer.  Make *pp point to the
       
 48453 ** allocated space.  (Note:  pp is a char* rather than a void** to
       
 48454 ** work around the pointer aliasing rules of C.)  *pp should initially
       
 48455 ** be zero.  If *pp is not zero, that means that the space has already
       
 48456 ** been allocated and this routine is a noop.
       
 48457 **
       
 48458 ** nByte is the number of bytes of space needed.
       
 48459 **
       
 48460 ** *ppFrom point to available space and pEnd points to the end of the
       
 48461 ** available space.
       
 48462 **
       
 48463 ** *pnByte is a counter of the number of bytes of space that have failed
       
 48464 ** to allocate.  If there is insufficient space in *ppFrom to satisfy the
       
 48465 ** request, then increment *pnByte by the amount of the request.
       
 48466 */
       
 48467 static void allocSpace(
       
 48468   char *pp,            /* IN/OUT: Set *pp to point to allocated buffer */
       
 48469   int nByte,           /* Number of bytes to allocate */
       
 48470   u8 **ppFrom,         /* IN/OUT: Allocate from *ppFrom */
       
 48471   u8 *pEnd,            /* Pointer to 1 byte past the end of *ppFrom buffer */
       
 48472   int *pnByte          /* If allocation cannot be made, increment *pnByte */
       
 48473 ){
       
 48474   assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
       
 48475   if( (*(void**)pp)==0 ){
       
 48476     nByte = ROUND8(nByte);
       
 48477     if( &(*ppFrom)[nByte] <= pEnd ){
       
 48478       *(void**)pp = (void *)*ppFrom;
       
 48479       *ppFrom += nByte;
       
 48480     }else{
       
 48481       *pnByte += nByte;
       
 48482     }
       
 48483   }
       
 48484 }
       
 48485 
       
 48486 /*
       
 48487 ** Prepare a virtual machine for execution.  This involves things such
       
 48488 ** as allocating stack space and initializing the program counter.
       
 48489 ** After the VDBE has be prepped, it can be executed by one or more
       
 48490 ** calls to sqlite3VdbeExec().  
       
 48491 **
       
 48492 ** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
       
 48493 ** VDBE_MAGIC_RUN.
       
 48494 **
       
 48495 ** This function may be called more than once on a single virtual machine.
       
 48496 ** The first call is made while compiling the SQL statement. Subsequent
       
 48497 ** calls are made as part of the process of resetting a statement to be
       
 48498 ** re-executed (from a call to sqlite3_reset()). The nVar, nMem, nCursor 
       
 48499 ** and isExplain parameters are only passed correct values the first time
       
 48500 ** the function is called. On subsequent calls, from sqlite3_reset(), nVar
       
 48501 ** is passed -1 and nMem, nCursor and isExplain are all passed zero.
       
 48502 */
       
 48503 SQLITE_PRIVATE void sqlite3VdbeMakeReady(
       
 48504   Vdbe *p,                       /* The VDBE */
       
 48505   int nVar,                      /* Number of '?' see in the SQL statement */
       
 48506   int nMem,                      /* Number of memory cells to allocate */
       
 48507   int nCursor,                   /* Number of cursors to allocate */
       
 48508   int nArg,                      /* Maximum number of args in SubPrograms */
       
 48509   int isExplain,                 /* True if the EXPLAIN keywords is present */
       
 48510   int usesStmtJournal            /* True to set Vdbe.usesStmtJournal */
       
 48511 ){
       
 48512   int n;
       
 48513   sqlite3 *db = p->db;
       
 48514 
       
 48515   assert( p!=0 );
       
 48516   assert( p->magic==VDBE_MAGIC_INIT );
       
 48517 
       
 48518   /* There should be at least one opcode.
       
 48519   */
       
 48520   assert( p->nOp>0 );
       
 48521 
       
 48522   /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
       
 48523   p->magic = VDBE_MAGIC_RUN;
       
 48524 
       
 48525   /* For each cursor required, also allocate a memory cell. Memory
       
 48526   ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
       
 48527   ** the vdbe program. Instead they are used to allocate space for
       
 48528   ** VdbeCursor/BtCursor structures. The blob of memory associated with 
       
 48529   ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
       
 48530   ** stores the blob of memory associated with cursor 1, etc.
       
 48531   **
       
 48532   ** See also: allocateCursor().
       
 48533   */
       
 48534   nMem += nCursor;
       
 48535 
       
 48536   /* Allocate space for memory registers, SQL variables, VDBE cursors and 
       
 48537   ** an array to marshal SQL function arguments in. This is only done the
       
 48538   ** first time this function is called for a given VDBE, not when it is
       
 48539   ** being called from sqlite3_reset() to reset the virtual machine.
       
 48540   */
       
 48541   if( nVar>=0 && ALWAYS(db->mallocFailed==0) ){
       
 48542     u8 *zCsr = (u8 *)&p->aOp[p->nOp];
       
 48543     u8 *zEnd = (u8 *)&p->aOp[p->nOpAlloc];
       
 48544     int nByte;
       
 48545     resolveP2Values(p, &nArg);
       
 48546     p->usesStmtJournal = (u8)usesStmtJournal;
       
 48547     if( isExplain && nMem<10 ){
       
 48548       nMem = 10;
       
 48549     }
       
 48550     memset(zCsr, 0, zEnd-zCsr);
       
 48551     zCsr += (zCsr - (u8*)0)&7;
       
 48552     assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
       
 48553 
       
 48554     do {
       
 48555       nByte = 0;
       
 48556       allocSpace((char*)&p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
       
 48557       allocSpace((char*)&p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
       
 48558       allocSpace((char*)&p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
       
 48559       allocSpace((char*)&p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
       
 48560       allocSpace((char*)&p->apCsr, 
       
 48561                  nCursor*sizeof(VdbeCursor*), &zCsr, zEnd, &nByte
       
 48562       );
       
 48563       if( nByte ){
       
 48564         p->pFree = sqlite3DbMallocZero(db, nByte);
       
 48565       }
       
 48566       zCsr = p->pFree;
       
 48567       zEnd = &zCsr[nByte];
       
 48568     }while( nByte && !db->mallocFailed );
       
 48569 
       
 48570     p->nCursor = (u16)nCursor;
       
 48571     if( p->aVar ){
       
 48572       p->nVar = (u16)nVar;
       
 48573       for(n=0; n<nVar; n++){
       
 48574         p->aVar[n].flags = MEM_Null;
       
 48575         p->aVar[n].db = db;
       
 48576       }
       
 48577     }
       
 48578     if( p->aMem ){
       
 48579       p->aMem--;                      /* aMem[] goes from 1..nMem */
       
 48580       p->nMem = nMem;                 /*       not from 0..nMem-1 */
       
 48581       for(n=1; n<=nMem; n++){
       
 48582         p->aMem[n].flags = MEM_Null;
       
 48583         p->aMem[n].db = db;
       
 48584       }
       
 48585     }
       
 48586   }
       
 48587 #ifdef SQLITE_DEBUG
       
 48588   for(n=1; n<p->nMem; n++){
       
 48589     assert( p->aMem[n].db==db );
       
 48590   }
       
 48591 #endif
       
 48592 
       
 48593   p->pc = -1;
       
 48594   p->rc = SQLITE_OK;
       
 48595   p->errorAction = OE_Abort;
       
 48596   p->explain |= isExplain;
       
 48597   p->magic = VDBE_MAGIC_RUN;
       
 48598   p->nChange = 0;
       
 48599   p->cacheCtr = 1;
       
 48600   p->minWriteFileFormat = 255;
       
 48601   p->iStatement = 0;
       
 48602 #ifdef VDBE_PROFILE
       
 48603   {
       
 48604     int i;
       
 48605     for(i=0; i<p->nOp; i++){
       
 48606       p->aOp[i].cnt = 0;
       
 48607       p->aOp[i].cycles = 0;
       
 48608     }
       
 48609   }
       
 48610 #endif
       
 48611 }
       
 48612 
       
 48613 /*
       
 48614 ** Close a VDBE cursor and release all the resources that cursor 
       
 48615 ** happens to hold.
       
 48616 */
       
 48617 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
       
 48618   if( pCx==0 ){
       
 48619     return;
       
 48620   }
       
 48621   if( pCx->pBt ){
       
 48622     sqlite3BtreeClose(pCx->pBt);
       
 48623     /* The pCx->pCursor will be close automatically, if it exists, by
       
 48624     ** the call above. */
       
 48625   }else if( pCx->pCursor ){
       
 48626     sqlite3BtreeCloseCursor(pCx->pCursor);
       
 48627   }
       
 48628 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 48629   if( pCx->pVtabCursor ){
       
 48630     sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
       
 48631     const sqlite3_module *pModule = pCx->pModule;
       
 48632     p->inVtabMethod = 1;
       
 48633     (void)sqlite3SafetyOff(p->db);
       
 48634     pModule->xClose(pVtabCursor);
       
 48635     (void)sqlite3SafetyOn(p->db);
       
 48636     p->inVtabMethod = 0;
       
 48637   }
       
 48638 #endif
       
 48639 }
       
 48640 
       
 48641 /*
       
 48642 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
       
 48643 ** is used, for example, when a trigger sub-program is halted to restore
       
 48644 ** control to the main program.
       
 48645 */
       
 48646 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
       
 48647   Vdbe *v = pFrame->v;
       
 48648   v->aOp = pFrame->aOp;
       
 48649   v->nOp = pFrame->nOp;
       
 48650   v->aMem = pFrame->aMem;
       
 48651   v->nMem = pFrame->nMem;
       
 48652   v->apCsr = pFrame->apCsr;
       
 48653   v->nCursor = pFrame->nCursor;
       
 48654   v->db->lastRowid = pFrame->lastRowid;
       
 48655   v->nChange = pFrame->nChange;
       
 48656   return pFrame->pc;
       
 48657 }
       
 48658 
       
 48659 /*
       
 48660 ** Close all cursors.
       
 48661 **
       
 48662 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory 
       
 48663 ** cell array. This is necessary as the memory cell array may contain
       
 48664 ** pointers to VdbeFrame objects, which may in turn contain pointers to
       
 48665 ** open cursors.
       
 48666 */
       
 48667 static void closeAllCursors(Vdbe *p){
       
 48668   if( p->pFrame ){
       
 48669     VdbeFrame *pFrame = p->pFrame;
       
 48670     for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
       
 48671     sqlite3VdbeFrameRestore(pFrame);
       
 48672   }
       
 48673   p->pFrame = 0;
       
 48674   p->nFrame = 0;
       
 48675 
       
 48676   if( p->apCsr ){
       
 48677     int i;
       
 48678     for(i=0; i<p->nCursor; i++){
       
 48679       VdbeCursor *pC = p->apCsr[i];
       
 48680       if( pC ){
       
 48681         sqlite3VdbeFreeCursor(p, pC);
       
 48682         p->apCsr[i] = 0;
       
 48683       }
       
 48684     }
       
 48685   }
       
 48686   if( p->aMem ){
       
 48687     releaseMemArray(&p->aMem[1], p->nMem);
       
 48688   }
       
 48689 }
       
 48690 
       
 48691 /*
       
 48692 ** Clean up the VM after execution.
       
 48693 **
       
 48694 ** This routine will automatically close any cursors, lists, and/or
       
 48695 ** sorters that were left open.  It also deletes the values of
       
 48696 ** variables in the aVar[] array.
       
 48697 */
       
 48698 static void Cleanup(Vdbe *p){
       
 48699   sqlite3 *db = p->db;
       
 48700 
       
 48701 #ifdef SQLITE_DEBUG
       
 48702   /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
       
 48703   ** Vdbe.aMem[] arrays have already been cleaned up.  */
       
 48704   int i;
       
 48705   for(i=0; i<p->nCursor; i++) assert( p->apCsr==0 || p->apCsr[i]==0 );
       
 48706   for(i=1; i<=p->nMem; i++) assert( p->aMem==0 || p->aMem[i].flags==MEM_Null );
       
 48707 #endif
       
 48708 
       
 48709   sqlite3DbFree(db, p->zErrMsg);
       
 48710   p->zErrMsg = 0;
       
 48711   p->pResultSet = 0;
       
 48712 }
       
 48713 
       
 48714 /*
       
 48715 ** Set the number of result columns that will be returned by this SQL
       
 48716 ** statement. This is now set at compile time, rather than during
       
 48717 ** execution of the vdbe program so that sqlite3_column_count() can
       
 48718 ** be called on an SQL statement before sqlite3_step().
       
 48719 */
       
 48720 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
       
 48721   Mem *pColName;
       
 48722   int n;
       
 48723   sqlite3 *db = p->db;
       
 48724 
       
 48725   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
       
 48726   sqlite3DbFree(db, p->aColName);
       
 48727   n = nResColumn*COLNAME_N;
       
 48728   p->nResColumn = (u16)nResColumn;
       
 48729   p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
       
 48730   if( p->aColName==0 ) return;
       
 48731   while( n-- > 0 ){
       
 48732     pColName->flags = MEM_Null;
       
 48733     pColName->db = p->db;
       
 48734     pColName++;
       
 48735   }
       
 48736 }
       
 48737 
       
 48738 /*
       
 48739 ** Set the name of the idx'th column to be returned by the SQL statement.
       
 48740 ** zName must be a pointer to a nul terminated string.
       
 48741 **
       
 48742 ** This call must be made after a call to sqlite3VdbeSetNumCols().
       
 48743 **
       
 48744 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
       
 48745 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
       
 48746 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
       
 48747 */
       
 48748 SQLITE_PRIVATE int sqlite3VdbeSetColName(
       
 48749   Vdbe *p,                         /* Vdbe being configured */
       
 48750   int idx,                         /* Index of column zName applies to */
       
 48751   int var,                         /* One of the COLNAME_* constants */
       
 48752   const char *zName,               /* Pointer to buffer containing name */
       
 48753   void (*xDel)(void*)              /* Memory management strategy for zName */
       
 48754 ){
       
 48755   int rc;
       
 48756   Mem *pColName;
       
 48757   assert( idx<p->nResColumn );
       
 48758   assert( var<COLNAME_N );
       
 48759   if( p->db->mallocFailed ){
       
 48760     assert( !zName || xDel!=SQLITE_DYNAMIC );
       
 48761     return SQLITE_NOMEM;
       
 48762   }
       
 48763   assert( p->aColName!=0 );
       
 48764   pColName = &(p->aColName[idx+var*p->nResColumn]);
       
 48765   rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
       
 48766   assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
       
 48767   return rc;
       
 48768 }
       
 48769 
       
 48770 /*
       
 48771 ** A read or write transaction may or may not be active on database handle
       
 48772 ** db. If a transaction is active, commit it. If there is a
       
 48773 ** write-transaction spanning more than one database file, this routine
       
 48774 ** takes care of the master journal trickery.
       
 48775 */
       
 48776 static int vdbeCommit(sqlite3 *db, Vdbe *p){
       
 48777   int i;
       
 48778   int nTrans = 0;  /* Number of databases with an active write-transaction */
       
 48779   int rc = SQLITE_OK;
       
 48780   int needXcommit = 0;
       
 48781 
       
 48782 #ifdef SQLITE_OMIT_VIRTUALTABLE
       
 48783   /* With this option, sqlite3VtabSync() is defined to be simply 
       
 48784   ** SQLITE_OK so p is not used. 
       
 48785   */
       
 48786   UNUSED_PARAMETER(p);
       
 48787 #endif
       
 48788 
       
 48789   /* Before doing anything else, call the xSync() callback for any
       
 48790   ** virtual module tables written in this transaction. This has to
       
 48791   ** be done before determining whether a master journal file is 
       
 48792   ** required, as an xSync() callback may add an attached database
       
 48793   ** to the transaction.
       
 48794   */
       
 48795   rc = sqlite3VtabSync(db, &p->zErrMsg);
       
 48796   if( rc!=SQLITE_OK ){
       
 48797     return rc;
       
 48798   }
       
 48799 
       
 48800   /* This loop determines (a) if the commit hook should be invoked and
       
 48801   ** (b) how many database files have open write transactions, not 
       
 48802   ** including the temp database. (b) is important because if more than 
       
 48803   ** one database file has an open write transaction, a master journal
       
 48804   ** file is required for an atomic commit.
       
 48805   */ 
       
 48806   for(i=0; i<db->nDb; i++){ 
       
 48807     Btree *pBt = db->aDb[i].pBt;
       
 48808     if( sqlite3BtreeIsInTrans(pBt) ){
       
 48809       needXcommit = 1;
       
 48810       if( i!=1 ) nTrans++;
       
 48811     }
       
 48812   }
       
 48813 
       
 48814   /* If there are any write-transactions at all, invoke the commit hook */
       
 48815   if( needXcommit && db->xCommitCallback ){
       
 48816     (void)sqlite3SafetyOff(db);
       
 48817     rc = db->xCommitCallback(db->pCommitArg);
       
 48818     (void)sqlite3SafetyOn(db);
       
 48819     if( rc ){
       
 48820       return SQLITE_CONSTRAINT;
       
 48821     }
       
 48822   }
       
 48823 
       
 48824   /* The simple case - no more than one database file (not counting the
       
 48825   ** TEMP database) has a transaction active.   There is no need for the
       
 48826   ** master-journal.
       
 48827   **
       
 48828   ** If the return value of sqlite3BtreeGetFilename() is a zero length
       
 48829   ** string, it means the main database is :memory: or a temp file.  In 
       
 48830   ** that case we do not support atomic multi-file commits, so use the 
       
 48831   ** simple case then too.
       
 48832   */
       
 48833   if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
       
 48834    || nTrans<=1
       
 48835   ){
       
 48836     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
       
 48837       Btree *pBt = db->aDb[i].pBt;
       
 48838       if( pBt ){
       
 48839         rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
       
 48840       }
       
 48841     }
       
 48842 
       
 48843     /* Do the commit only if all databases successfully complete phase 1. 
       
 48844     ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
       
 48845     ** IO error while deleting or truncating a journal file. It is unlikely,
       
 48846     ** but could happen. In this case abandon processing and return the error.
       
 48847     */
       
 48848     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
       
 48849       Btree *pBt = db->aDb[i].pBt;
       
 48850       if( pBt ){
       
 48851         rc = sqlite3BtreeCommitPhaseTwo(pBt);
       
 48852       }
       
 48853     }
       
 48854     if( rc==SQLITE_OK ){
       
 48855       sqlite3VtabCommit(db);
       
 48856     }
       
 48857   }
       
 48858 
       
 48859   /* The complex case - There is a multi-file write-transaction active.
       
 48860   ** This requires a master journal file to ensure the transaction is
       
 48861   ** committed atomicly.
       
 48862   */
       
 48863 #ifndef SQLITE_OMIT_DISKIO
       
 48864   else{
       
 48865     sqlite3_vfs *pVfs = db->pVfs;
       
 48866     int needSync = 0;
       
 48867     char *zMaster = 0;   /* File-name for the master journal */
       
 48868     char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
       
 48869     sqlite3_file *pMaster = 0;
       
 48870     i64 offset = 0;
       
 48871     int res;
       
 48872 
       
 48873     /* Select a master journal file name */
       
 48874     do {
       
 48875       u32 iRandom;
       
 48876       sqlite3DbFree(db, zMaster);
       
 48877       sqlite3_randomness(sizeof(iRandom), &iRandom);
       
 48878       zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, iRandom&0x7fffffff);
       
 48879       if( !zMaster ){
       
 48880         return SQLITE_NOMEM;
       
 48881       }
       
 48882       rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
       
 48883     }while( rc==SQLITE_OK && res );
       
 48884     if( rc==SQLITE_OK ){
       
 48885       /* Open the master journal. */
       
 48886       rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, 
       
 48887           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
       
 48888           SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
       
 48889       );
       
 48890     }
       
 48891     if( rc!=SQLITE_OK ){
       
 48892       sqlite3DbFree(db, zMaster);
       
 48893       return rc;
       
 48894     }
       
 48895  
       
 48896     /* Write the name of each database file in the transaction into the new
       
 48897     ** master journal file. If an error occurs at this point close
       
 48898     ** and delete the master journal file. All the individual journal files
       
 48899     ** still have 'null' as the master journal pointer, so they will roll
       
 48900     ** back independently if a failure occurs.
       
 48901     */
       
 48902     for(i=0; i<db->nDb; i++){
       
 48903       Btree *pBt = db->aDb[i].pBt;
       
 48904       if( i==1 ) continue;   /* Ignore the TEMP database */
       
 48905       if( sqlite3BtreeIsInTrans(pBt) ){
       
 48906         char const *zFile = sqlite3BtreeGetJournalname(pBt);
       
 48907         if( zFile[0]==0 ) continue;  /* Ignore :memory: databases */
       
 48908         if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
       
 48909           needSync = 1;
       
 48910         }
       
 48911         rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
       
 48912         offset += sqlite3Strlen30(zFile)+1;
       
 48913         if( rc!=SQLITE_OK ){
       
 48914           sqlite3OsCloseFree(pMaster);
       
 48915           sqlite3OsDelete(pVfs, zMaster, 0);
       
 48916           sqlite3DbFree(db, zMaster);
       
 48917           return rc;
       
 48918         }
       
 48919       }
       
 48920     }
       
 48921 
       
 48922     /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
       
 48923     ** flag is set this is not required.
       
 48924     */
       
 48925     if( needSync 
       
 48926      && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
       
 48927      && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
       
 48928     ){
       
 48929       sqlite3OsCloseFree(pMaster);
       
 48930       sqlite3OsDelete(pVfs, zMaster, 0);
       
 48931       sqlite3DbFree(db, zMaster);
       
 48932       return rc;
       
 48933     }
       
 48934 
       
 48935     /* Sync all the db files involved in the transaction. The same call
       
 48936     ** sets the master journal pointer in each individual journal. If
       
 48937     ** an error occurs here, do not delete the master journal file.
       
 48938     **
       
 48939     ** If the error occurs during the first call to
       
 48940     ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
       
 48941     ** master journal file will be orphaned. But we cannot delete it,
       
 48942     ** in case the master journal file name was written into the journal
       
 48943     ** file before the failure occurred.
       
 48944     */
       
 48945     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
       
 48946       Btree *pBt = db->aDb[i].pBt;
       
 48947       if( pBt ){
       
 48948         rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
       
 48949       }
       
 48950     }
       
 48951     sqlite3OsCloseFree(pMaster);
       
 48952     if( rc!=SQLITE_OK ){
       
 48953       sqlite3DbFree(db, zMaster);
       
 48954       return rc;
       
 48955     }
       
 48956 
       
 48957     /* Delete the master journal file. This commits the transaction. After
       
 48958     ** doing this the directory is synced again before any individual
       
 48959     ** transaction files are deleted.
       
 48960     */
       
 48961     rc = sqlite3OsDelete(pVfs, zMaster, 1);
       
 48962     sqlite3DbFree(db, zMaster);
       
 48963     zMaster = 0;
       
 48964     if( rc ){
       
 48965       return rc;
       
 48966     }
       
 48967 
       
 48968     /* All files and directories have already been synced, so the following
       
 48969     ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
       
 48970     ** deleting or truncating journals. If something goes wrong while
       
 48971     ** this is happening we don't really care. The integrity of the
       
 48972     ** transaction is already guaranteed, but some stray 'cold' journals
       
 48973     ** may be lying around. Returning an error code won't help matters.
       
 48974     */
       
 48975     disable_simulated_io_errors();
       
 48976     sqlite3BeginBenignMalloc();
       
 48977     for(i=0; i<db->nDb; i++){ 
       
 48978       Btree *pBt = db->aDb[i].pBt;
       
 48979       if( pBt ){
       
 48980         sqlite3BtreeCommitPhaseTwo(pBt);
       
 48981       }
       
 48982     }
       
 48983     sqlite3EndBenignMalloc();
       
 48984     enable_simulated_io_errors();
       
 48985 
       
 48986     sqlite3VtabCommit(db);
       
 48987   }
       
 48988 #endif
       
 48989 
       
 48990   return rc;
       
 48991 }
       
 48992 
       
 48993 /* 
       
 48994 ** This routine checks that the sqlite3.activeVdbeCnt count variable
       
 48995 ** matches the number of vdbe's in the list sqlite3.pVdbe that are
       
 48996 ** currently active. An assertion fails if the two counts do not match.
       
 48997 ** This is an internal self-check only - it is not an essential processing
       
 48998 ** step.
       
 48999 **
       
 49000 ** This is a no-op if NDEBUG is defined.
       
 49001 */
       
 49002 #ifndef NDEBUG
       
 49003 static void checkActiveVdbeCnt(sqlite3 *db){
       
 49004   Vdbe *p;
       
 49005   int cnt = 0;
       
 49006   int nWrite = 0;
       
 49007   p = db->pVdbe;
       
 49008   while( p ){
       
 49009     if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
       
 49010       cnt++;
       
 49011       if( p->readOnly==0 ) nWrite++;
       
 49012     }
       
 49013     p = p->pNext;
       
 49014   }
       
 49015   assert( cnt==db->activeVdbeCnt );
       
 49016   assert( nWrite==db->writeVdbeCnt );
       
 49017 }
       
 49018 #else
       
 49019 #define checkActiveVdbeCnt(x)
       
 49020 #endif
       
 49021 
       
 49022 /*
       
 49023 ** For every Btree that in database connection db which 
       
 49024 ** has been modified, "trip" or invalidate each cursor in
       
 49025 ** that Btree might have been modified so that the cursor
       
 49026 ** can never be used again.  This happens when a rollback
       
 49027 *** occurs.  We have to trip all the other cursors, even
       
 49028 ** cursor from other VMs in different database connections,
       
 49029 ** so that none of them try to use the data at which they
       
 49030 ** were pointing and which now may have been changed due
       
 49031 ** to the rollback.
       
 49032 **
       
 49033 ** Remember that a rollback can delete tables complete and
       
 49034 ** reorder rootpages.  So it is not sufficient just to save
       
 49035 ** the state of the cursor.  We have to invalidate the cursor
       
 49036 ** so that it is never used again.
       
 49037 */
       
 49038 static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
       
 49039   int i;
       
 49040   for(i=0; i<db->nDb; i++){
       
 49041     Btree *p = db->aDb[i].pBt;
       
 49042     if( p && sqlite3BtreeIsInTrans(p) ){
       
 49043       sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
       
 49044     }
       
 49045   }
       
 49046 }
       
 49047 
       
 49048 /*
       
 49049 ** If the Vdbe passed as the first argument opened a statement-transaction,
       
 49050 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
       
 49051 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
       
 49052 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the 
       
 49053 ** statement transaction is commtted.
       
 49054 **
       
 49055 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. 
       
 49056 ** Otherwise SQLITE_OK.
       
 49057 */
       
 49058 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
       
 49059   sqlite3 *const db = p->db;
       
 49060   int rc = SQLITE_OK;
       
 49061 
       
 49062   /* If p->iStatement is greater than zero, then this Vdbe opened a 
       
 49063   ** statement transaction that should be closed here. The only exception
       
 49064   ** is that an IO error may have occured, causing an emergency rollback.
       
 49065   ** In this case (db->nStatement==0), and there is nothing to do.
       
 49066   */
       
 49067   if( db->nStatement && p->iStatement ){
       
 49068     int i;
       
 49069     const int iSavepoint = p->iStatement-1;
       
 49070 
       
 49071     assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
       
 49072     assert( db->nStatement>0 );
       
 49073     assert( p->iStatement==(db->nStatement+db->nSavepoint) );
       
 49074 
       
 49075     for(i=0; i<db->nDb; i++){ 
       
 49076       int rc2 = SQLITE_OK;
       
 49077       Btree *pBt = db->aDb[i].pBt;
       
 49078       if( pBt ){
       
 49079         if( eOp==SAVEPOINT_ROLLBACK ){
       
 49080           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
       
 49081         }
       
 49082         if( rc2==SQLITE_OK ){
       
 49083           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
       
 49084         }
       
 49085         if( rc==SQLITE_OK ){
       
 49086           rc = rc2;
       
 49087         }
       
 49088       }
       
 49089     }
       
 49090     db->nStatement--;
       
 49091     p->iStatement = 0;
       
 49092 
       
 49093     /* If the statement transaction is being rolled back, also restore the 
       
 49094     ** database handles deferred constraint counter to the value it had when 
       
 49095     ** the statement transaction was opened.  */
       
 49096     if( eOp==SAVEPOINT_ROLLBACK ){
       
 49097       db->nDeferredCons = p->nStmtDefCons;
       
 49098     }
       
 49099   }
       
 49100   return rc;
       
 49101 }
       
 49102 
       
 49103 /*
       
 49104 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
       
 49105 ** this routine obtains the mutex associated with each BtShared structure
       
 49106 ** that may be accessed by the VM passed as an argument. In doing so it
       
 49107 ** sets the BtShared.db member of each of the BtShared structures, ensuring
       
 49108 ** that the correct busy-handler callback is invoked if required.
       
 49109 **
       
 49110 ** If SQLite is not threadsafe but does support shared-cache mode, then
       
 49111 ** sqlite3BtreeEnterAll() is invoked to set the BtShared.db variables
       
 49112 ** of all of BtShared structures accessible via the database handle 
       
 49113 ** associated with the VM. Of course only a subset of these structures
       
 49114 ** will be accessed by the VM, and we could use Vdbe.btreeMask to figure
       
 49115 ** that subset out, but there is no advantage to doing so.
       
 49116 **
       
 49117 ** If SQLite is not threadsafe and does not support shared-cache mode, this
       
 49118 ** function is a no-op.
       
 49119 */
       
 49120 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 49121 SQLITE_PRIVATE void sqlite3VdbeMutexArrayEnter(Vdbe *p){
       
 49122 #if SQLITE_THREADSAFE
       
 49123   sqlite3BtreeMutexArrayEnter(&p->aMutex);
       
 49124 #else
       
 49125   sqlite3BtreeEnterAll(p->db);
       
 49126 #endif
       
 49127 }
       
 49128 #endif
       
 49129 
       
 49130 /*
       
 49131 ** This function is called when a transaction opened by the database 
       
 49132 ** handle associated with the VM passed as an argument is about to be 
       
 49133 ** committed. If there are outstanding deferred foreign key constraint
       
 49134 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
       
 49135 **
       
 49136 ** If there are outstanding FK violations and this function returns 
       
 49137 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write
       
 49138 ** an error message to it. Then return SQLITE_ERROR.
       
 49139 */
       
 49140 #ifndef SQLITE_OMIT_FOREIGN_KEY
       
 49141 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
       
 49142   sqlite3 *db = p->db;
       
 49143   if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){
       
 49144     p->rc = SQLITE_CONSTRAINT;
       
 49145     p->errorAction = OE_Abort;
       
 49146     sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed");
       
 49147     return SQLITE_ERROR;
       
 49148   }
       
 49149   return SQLITE_OK;
       
 49150 }
       
 49151 #endif
       
 49152 
       
 49153 /*
       
 49154 ** This routine is called the when a VDBE tries to halt.  If the VDBE
       
 49155 ** has made changes and is in autocommit mode, then commit those
       
 49156 ** changes.  If a rollback is needed, then do the rollback.
       
 49157 **
       
 49158 ** This routine is the only way to move the state of a VM from
       
 49159 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
       
 49160 ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
       
 49161 **
       
 49162 ** Return an error code.  If the commit could not complete because of
       
 49163 ** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
       
 49164 ** means the close did not happen and needs to be repeated.
       
 49165 */
       
 49166 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
       
 49167   int rc;                         /* Used to store transient return codes */
       
 49168   sqlite3 *db = p->db;
       
 49169 
       
 49170   /* This function contains the logic that determines if a statement or
       
 49171   ** transaction will be committed or rolled back as a result of the
       
 49172   ** execution of this virtual machine. 
       
 49173   **
       
 49174   ** If any of the following errors occur:
       
 49175   **
       
 49176   **     SQLITE_NOMEM
       
 49177   **     SQLITE_IOERR
       
 49178   **     SQLITE_FULL
       
 49179   **     SQLITE_INTERRUPT
       
 49180   **
       
 49181   ** Then the internal cache might have been left in an inconsistent
       
 49182   ** state.  We need to rollback the statement transaction, if there is
       
 49183   ** one, or the complete transaction if there is no statement transaction.
       
 49184   */
       
 49185 
       
 49186   if( p->db->mallocFailed ){
       
 49187     p->rc = SQLITE_NOMEM;
       
 49188   }
       
 49189   closeAllCursors(p);
       
 49190   if( p->magic!=VDBE_MAGIC_RUN ){
       
 49191     return SQLITE_OK;
       
 49192   }
       
 49193   checkActiveVdbeCnt(db);
       
 49194 
       
 49195   /* No commit or rollback needed if the program never started */
       
 49196   if( p->pc>=0 ){
       
 49197     int mrc;   /* Primary error code from p->rc */
       
 49198     int eStatementOp = 0;
       
 49199     int isSpecialError;            /* Set to true if a 'special' error */
       
 49200 
       
 49201     /* Lock all btrees used by the statement */
       
 49202     sqlite3VdbeMutexArrayEnter(p);
       
 49203 
       
 49204     /* Check for one of the special errors */
       
 49205     mrc = p->rc & 0xff;
       
 49206     assert( p->rc!=SQLITE_IOERR_BLOCKED );  /* This error no longer exists */
       
 49207     isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
       
 49208                      || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
       
 49209     if( isSpecialError ){
       
 49210       /* If the query was read-only, we need do no rollback at all. Otherwise,
       
 49211       ** proceed with the special handling.
       
 49212       */
       
 49213       if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
       
 49214         if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
       
 49215           eStatementOp = SAVEPOINT_ROLLBACK;
       
 49216         }else{
       
 49217           /* We are forced to roll back the active transaction. Before doing
       
 49218           ** so, abort any other statements this handle currently has active.
       
 49219           */
       
 49220           invalidateCursorsOnModifiedBtrees(db);
       
 49221           sqlite3RollbackAll(db);
       
 49222           sqlite3CloseSavepoints(db);
       
 49223           db->autoCommit = 1;
       
 49224         }
       
 49225       }
       
 49226     }
       
 49227 
       
 49228     /* Check for immediate foreign key violations. */
       
 49229     if( p->rc==SQLITE_OK ){
       
 49230       sqlite3VdbeCheckFk(p, 0);
       
 49231     }
       
 49232   
       
 49233     /* If the auto-commit flag is set and this is the only active writer 
       
 49234     ** VM, then we do either a commit or rollback of the current transaction. 
       
 49235     **
       
 49236     ** Note: This block also runs if one of the special errors handled 
       
 49237     ** above has occurred. 
       
 49238     */
       
 49239     if( !sqlite3VtabInSync(db) 
       
 49240      && db->autoCommit 
       
 49241      && db->writeVdbeCnt==(p->readOnly==0) 
       
 49242     ){
       
 49243       if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
       
 49244         if( sqlite3VdbeCheckFk(p, 1) ){
       
 49245           sqlite3BtreeMutexArrayLeave(&p->aMutex);
       
 49246           return SQLITE_ERROR;
       
 49247         }
       
 49248         /* The auto-commit flag is true, the vdbe program was successful 
       
 49249         ** or hit an 'OR FAIL' constraint and there are no deferred foreign
       
 49250         ** key constraints to hold up the transaction. This means a commit 
       
 49251         ** is required.  */
       
 49252         rc = vdbeCommit(db, p);
       
 49253         if( rc==SQLITE_BUSY ){
       
 49254           sqlite3BtreeMutexArrayLeave(&p->aMutex);
       
 49255           return SQLITE_BUSY;
       
 49256         }else if( rc!=SQLITE_OK ){
       
 49257           p->rc = rc;
       
 49258           sqlite3RollbackAll(db);
       
 49259         }else{
       
 49260           db->nDeferredCons = 0;
       
 49261           sqlite3CommitInternalChanges(db);
       
 49262         }
       
 49263       }else{
       
 49264         sqlite3RollbackAll(db);
       
 49265       }
       
 49266       db->nStatement = 0;
       
 49267     }else if( eStatementOp==0 ){
       
 49268       if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
       
 49269         eStatementOp = SAVEPOINT_RELEASE;
       
 49270       }else if( p->errorAction==OE_Abort ){
       
 49271         eStatementOp = SAVEPOINT_ROLLBACK;
       
 49272       }else{
       
 49273         invalidateCursorsOnModifiedBtrees(db);
       
 49274         sqlite3RollbackAll(db);
       
 49275         sqlite3CloseSavepoints(db);
       
 49276         db->autoCommit = 1;
       
 49277       }
       
 49278     }
       
 49279   
       
 49280     /* If eStatementOp is non-zero, then a statement transaction needs to
       
 49281     ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
       
 49282     ** do so. If this operation returns an error, and the current statement
       
 49283     ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then set the error
       
 49284     ** code to the new value.
       
 49285     */
       
 49286     if( eStatementOp ){
       
 49287       rc = sqlite3VdbeCloseStatement(p, eStatementOp);
       
 49288       if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
       
 49289         p->rc = rc;
       
 49290         sqlite3DbFree(db, p->zErrMsg);
       
 49291         p->zErrMsg = 0;
       
 49292       }
       
 49293     }
       
 49294   
       
 49295     /* If this was an INSERT, UPDATE or DELETE and no statement transaction
       
 49296     ** has been rolled back, update the database connection change-counter. 
       
 49297     */
       
 49298     if( p->changeCntOn ){
       
 49299       if( eStatementOp!=SAVEPOINT_ROLLBACK ){
       
 49300         sqlite3VdbeSetChanges(db, p->nChange);
       
 49301       }else{
       
 49302         sqlite3VdbeSetChanges(db, 0);
       
 49303       }
       
 49304       p->nChange = 0;
       
 49305     }
       
 49306   
       
 49307     /* Rollback or commit any schema changes that occurred. */
       
 49308     if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
       
 49309       sqlite3ResetInternalSchema(db, 0);
       
 49310       db->flags = (db->flags | SQLITE_InternChanges);
       
 49311     }
       
 49312 
       
 49313     /* Release the locks */
       
 49314     sqlite3BtreeMutexArrayLeave(&p->aMutex);
       
 49315   }
       
 49316 
       
 49317   /* We have successfully halted and closed the VM.  Record this fact. */
       
 49318   if( p->pc>=0 ){
       
 49319     db->activeVdbeCnt--;
       
 49320     if( !p->readOnly ){
       
 49321       db->writeVdbeCnt--;
       
 49322     }
       
 49323     assert( db->activeVdbeCnt>=db->writeVdbeCnt );
       
 49324   }
       
 49325   p->magic = VDBE_MAGIC_HALT;
       
 49326   checkActiveVdbeCnt(db);
       
 49327   if( p->db->mallocFailed ){
       
 49328     p->rc = SQLITE_NOMEM;
       
 49329   }
       
 49330 
       
 49331   /* If the auto-commit flag is set to true, then any locks that were held
       
 49332   ** by connection db have now been released. Call sqlite3ConnectionUnlocked() 
       
 49333   ** to invoke any required unlock-notify callbacks.
       
 49334   */
       
 49335   if( db->autoCommit ){
       
 49336     sqlite3ConnectionUnlocked(db);
       
 49337   }
       
 49338 
       
 49339   assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
       
 49340   return SQLITE_OK;
       
 49341 }
       
 49342 
       
 49343 
       
 49344 /*
       
 49345 ** Each VDBE holds the result of the most recent sqlite3_step() call
       
 49346 ** in p->rc.  This routine sets that result back to SQLITE_OK.
       
 49347 */
       
 49348 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
       
 49349   p->rc = SQLITE_OK;
       
 49350 }
       
 49351 
       
 49352 /*
       
 49353 ** Clean up a VDBE after execution but do not delete the VDBE just yet.
       
 49354 ** Write any error messages into *pzErrMsg.  Return the result code.
       
 49355 **
       
 49356 ** After this routine is run, the VDBE should be ready to be executed
       
 49357 ** again.
       
 49358 **
       
 49359 ** To look at it another way, this routine resets the state of the
       
 49360 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
       
 49361 ** VDBE_MAGIC_INIT.
       
 49362 */
       
 49363 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
       
 49364   sqlite3 *db;
       
 49365   db = p->db;
       
 49366 
       
 49367   /* If the VM did not run to completion or if it encountered an
       
 49368   ** error, then it might not have been halted properly.  So halt
       
 49369   ** it now.
       
 49370   */
       
 49371   (void)sqlite3SafetyOn(db);
       
 49372   sqlite3VdbeHalt(p);
       
 49373   (void)sqlite3SafetyOff(db);
       
 49374 
       
 49375   /* If the VDBE has be run even partially, then transfer the error code
       
 49376   ** and error message from the VDBE into the main database structure.  But
       
 49377   ** if the VDBE has just been set to run but has not actually executed any
       
 49378   ** instructions yet, leave the main database error information unchanged.
       
 49379   */
       
 49380   if( p->pc>=0 ){
       
 49381     if( p->zErrMsg ){
       
 49382       sqlite3BeginBenignMalloc();
       
 49383       sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT);
       
 49384       sqlite3EndBenignMalloc();
       
 49385       db->errCode = p->rc;
       
 49386       sqlite3DbFree(db, p->zErrMsg);
       
 49387       p->zErrMsg = 0;
       
 49388     }else if( p->rc ){
       
 49389       sqlite3Error(db, p->rc, 0);
       
 49390     }else{
       
 49391       sqlite3Error(db, SQLITE_OK, 0);
       
 49392     }
       
 49393   }else if( p->rc && p->expired ){
       
 49394     /* The expired flag was set on the VDBE before the first call
       
 49395     ** to sqlite3_step(). For consistency (since sqlite3_step() was
       
 49396     ** called), set the database error in this case as well.
       
 49397     */
       
 49398     sqlite3Error(db, p->rc, 0);
       
 49399     sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
       
 49400     sqlite3DbFree(db, p->zErrMsg);
       
 49401     p->zErrMsg = 0;
       
 49402   }
       
 49403 
       
 49404   /* Reclaim all memory used by the VDBE
       
 49405   */
       
 49406   Cleanup(p);
       
 49407 
       
 49408   /* Save profiling information from this VDBE run.
       
 49409   */
       
 49410 #ifdef VDBE_PROFILE
       
 49411   {
       
 49412     FILE *out = fopen("vdbe_profile.out", "a");
       
 49413     if( out ){
       
 49414       int i;
       
 49415       fprintf(out, "---- ");
       
 49416       for(i=0; i<p->nOp; i++){
       
 49417         fprintf(out, "%02x", p->aOp[i].opcode);
       
 49418       }
       
 49419       fprintf(out, "\n");
       
 49420       for(i=0; i<p->nOp; i++){
       
 49421         fprintf(out, "%6d %10lld %8lld ",
       
 49422            p->aOp[i].cnt,
       
 49423            p->aOp[i].cycles,
       
 49424            p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
       
 49425         );
       
 49426         sqlite3VdbePrintOp(out, i, &p->aOp[i]);
       
 49427       }
       
 49428       fclose(out);
       
 49429     }
       
 49430   }
       
 49431 #endif
       
 49432   p->magic = VDBE_MAGIC_INIT;
       
 49433   return p->rc & db->errMask;
       
 49434 }
       
 49435  
       
 49436 /*
       
 49437 ** Clean up and delete a VDBE after execution.  Return an integer which is
       
 49438 ** the result code.  Write any error message text into *pzErrMsg.
       
 49439 */
       
 49440 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
       
 49441   int rc = SQLITE_OK;
       
 49442   if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
       
 49443     rc = sqlite3VdbeReset(p);
       
 49444     assert( (rc & p->db->errMask)==rc );
       
 49445   }
       
 49446   sqlite3VdbeDelete(p);
       
 49447   return rc;
       
 49448 }
       
 49449 
       
 49450 /*
       
 49451 ** Call the destructor for each auxdata entry in pVdbeFunc for which
       
 49452 ** the corresponding bit in mask is clear.  Auxdata entries beyond 31
       
 49453 ** are always destroyed.  To destroy all auxdata entries, call this
       
 49454 ** routine with mask==0.
       
 49455 */
       
 49456 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
       
 49457   int i;
       
 49458   for(i=0; i<pVdbeFunc->nAux; i++){
       
 49459     struct AuxData *pAux = &pVdbeFunc->apAux[i];
       
 49460     if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){
       
 49461       if( pAux->xDelete ){
       
 49462         pAux->xDelete(pAux->pAux);
       
 49463       }
       
 49464       pAux->pAux = 0;
       
 49465     }
       
 49466   }
       
 49467 }
       
 49468 
       
 49469 /*
       
 49470 ** Delete an entire VDBE.
       
 49471 */
       
 49472 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
       
 49473   sqlite3 *db;
       
 49474 
       
 49475   if( NEVER(p==0) ) return;
       
 49476   db = p->db;
       
 49477   if( p->pPrev ){
       
 49478     p->pPrev->pNext = p->pNext;
       
 49479   }else{
       
 49480     assert( db->pVdbe==p );
       
 49481     db->pVdbe = p->pNext;
       
 49482   }
       
 49483   if( p->pNext ){
       
 49484     p->pNext->pPrev = p->pPrev;
       
 49485   }
       
 49486   releaseMemArray(p->aVar, p->nVar);
       
 49487   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
       
 49488   vdbeFreeOpArray(db, p->aOp, p->nOp);
       
 49489   sqlite3DbFree(db, p->aLabel);
       
 49490   sqlite3DbFree(db, p->aColName);
       
 49491   sqlite3DbFree(db, p->zSql);
       
 49492   p->magic = VDBE_MAGIC_DEAD;
       
 49493   sqlite3DbFree(db, p->pFree);
       
 49494   sqlite3DbFree(db, p);
       
 49495 }
       
 49496 
       
 49497 /*
       
 49498 ** Make sure the cursor p is ready to read or write the row to which it
       
 49499 ** was last positioned.  Return an error code if an OOM fault or I/O error
       
 49500 ** prevents us from positioning the cursor to its correct position.
       
 49501 **
       
 49502 ** If a MoveTo operation is pending on the given cursor, then do that
       
 49503 ** MoveTo now.  If no move is pending, check to see if the row has been
       
 49504 ** deleted out from under the cursor and if it has, mark the row as
       
 49505 ** a NULL row.
       
 49506 **
       
 49507 ** If the cursor is already pointing to the correct row and that row has
       
 49508 ** not been deleted out from under the cursor, then this routine is a no-op.
       
 49509 */
       
 49510 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
       
 49511   if( p->deferredMoveto ){
       
 49512     int res, rc;
       
 49513 #ifdef SQLITE_TEST
       
 49514     extern int sqlite3_search_count;
       
 49515 #endif
       
 49516     assert( p->isTable );
       
 49517     rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
       
 49518     if( rc ) return rc;
       
 49519     p->lastRowid = p->movetoTarget;
       
 49520     p->rowidIsValid = ALWAYS(res==0) ?1:0;
       
 49521     if( NEVER(res<0) ){
       
 49522       rc = sqlite3BtreeNext(p->pCursor, &res);
       
 49523       if( rc ) return rc;
       
 49524     }
       
 49525 #ifdef SQLITE_TEST
       
 49526     sqlite3_search_count++;
       
 49527 #endif
       
 49528     p->deferredMoveto = 0;
       
 49529     p->cacheStatus = CACHE_STALE;
       
 49530   }else if( ALWAYS(p->pCursor) ){
       
 49531     int hasMoved;
       
 49532     int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
       
 49533     if( rc ) return rc;
       
 49534     if( hasMoved ){
       
 49535       p->cacheStatus = CACHE_STALE;
       
 49536       p->nullRow = 1;
       
 49537     }
       
 49538   }
       
 49539   return SQLITE_OK;
       
 49540 }
       
 49541 
       
 49542 /*
       
 49543 ** The following functions:
       
 49544 **
       
 49545 ** sqlite3VdbeSerialType()
       
 49546 ** sqlite3VdbeSerialTypeLen()
       
 49547 ** sqlite3VdbeSerialLen()
       
 49548 ** sqlite3VdbeSerialPut()
       
 49549 ** sqlite3VdbeSerialGet()
       
 49550 **
       
 49551 ** encapsulate the code that serializes values for storage in SQLite
       
 49552 ** data and index records. Each serialized value consists of a
       
 49553 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
       
 49554 ** integer, stored as a varint.
       
 49555 **
       
 49556 ** In an SQLite index record, the serial type is stored directly before
       
 49557 ** the blob of data that it corresponds to. In a table record, all serial
       
 49558 ** types are stored at the start of the record, and the blobs of data at
       
 49559 ** the end. Hence these functions allow the caller to handle the
       
 49560 ** serial-type and data blob seperately.
       
 49561 **
       
 49562 ** The following table describes the various storage classes for data:
       
 49563 **
       
 49564 **   serial type        bytes of data      type
       
 49565 **   --------------     ---------------    ---------------
       
 49566 **      0                     0            NULL
       
 49567 **      1                     1            signed integer
       
 49568 **      2                     2            signed integer
       
 49569 **      3                     3            signed integer
       
 49570 **      4                     4            signed integer
       
 49571 **      5                     6            signed integer
       
 49572 **      6                     8            signed integer
       
 49573 **      7                     8            IEEE float
       
 49574 **      8                     0            Integer constant 0
       
 49575 **      9                     0            Integer constant 1
       
 49576 **     10,11                               reserved for expansion
       
 49577 **    N>=12 and even       (N-12)/2        BLOB
       
 49578 **    N>=13 and odd        (N-13)/2        text
       
 49579 **
       
 49580 ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
       
 49581 ** of SQLite will not understand those serial types.
       
 49582 */
       
 49583 
       
 49584 /*
       
 49585 ** Return the serial-type for the value stored in pMem.
       
 49586 */
       
 49587 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
       
 49588   int flags = pMem->flags;
       
 49589   int n;
       
 49590 
       
 49591   if( flags&MEM_Null ){
       
 49592     return 0;
       
 49593   }
       
 49594   if( flags&MEM_Int ){
       
 49595     /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
       
 49596 #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
       
 49597     i64 i = pMem->u.i;
       
 49598     u64 u;
       
 49599     if( file_format>=4 && (i&1)==i ){
       
 49600       return 8+(u32)i;
       
 49601     }
       
 49602     u = i<0 ? -i : i;
       
 49603     if( u<=127 ) return 1;
       
 49604     if( u<=32767 ) return 2;
       
 49605     if( u<=8388607 ) return 3;
       
 49606     if( u<=2147483647 ) return 4;
       
 49607     if( u<=MAX_6BYTE ) return 5;
       
 49608     return 6;
       
 49609   }
       
 49610   if( flags&MEM_Real ){
       
 49611     return 7;
       
 49612   }
       
 49613   assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
       
 49614   n = pMem->n;
       
 49615   if( flags & MEM_Zero ){
       
 49616     n += pMem->u.nZero;
       
 49617   }
       
 49618   assert( n>=0 );
       
 49619   return ((n*2) + 12 + ((flags&MEM_Str)!=0));
       
 49620 }
       
 49621 
       
 49622 /*
       
 49623 ** Return the length of the data corresponding to the supplied serial-type.
       
 49624 */
       
 49625 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
       
 49626   if( serial_type>=12 ){
       
 49627     return (serial_type-12)/2;
       
 49628   }else{
       
 49629     static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
       
 49630     return aSize[serial_type];
       
 49631   }
       
 49632 }
       
 49633 
       
 49634 /*
       
 49635 ** If we are on an architecture with mixed-endian floating 
       
 49636 ** points (ex: ARM7) then swap the lower 4 bytes with the 
       
 49637 ** upper 4 bytes.  Return the result.
       
 49638 **
       
 49639 ** For most architectures, this is a no-op.
       
 49640 **
       
 49641 ** (later):  It is reported to me that the mixed-endian problem
       
 49642 ** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
       
 49643 ** that early versions of GCC stored the two words of a 64-bit
       
 49644 ** float in the wrong order.  And that error has been propagated
       
 49645 ** ever since.  The blame is not necessarily with GCC, though.
       
 49646 ** GCC might have just copying the problem from a prior compiler.
       
 49647 ** I am also told that newer versions of GCC that follow a different
       
 49648 ** ABI get the byte order right.
       
 49649 **
       
 49650 ** Developers using SQLite on an ARM7 should compile and run their
       
 49651 ** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
       
 49652 ** enabled, some asserts below will ensure that the byte order of
       
 49653 ** floating point values is correct.
       
 49654 **
       
 49655 ** (2007-08-30)  Frank van Vugt has studied this problem closely
       
 49656 ** and has send his findings to the SQLite developers.  Frank
       
 49657 ** writes that some Linux kernels offer floating point hardware
       
 49658 ** emulation that uses only 32-bit mantissas instead of a full 
       
 49659 ** 48-bits as required by the IEEE standard.  (This is the
       
 49660 ** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
       
 49661 ** byte swapping becomes very complicated.  To avoid problems,
       
 49662 ** the necessary byte swapping is carried out using a 64-bit integer
       
 49663 ** rather than a 64-bit float.  Frank assures us that the code here
       
 49664 ** works for him.  We, the developers, have no way to independently
       
 49665 ** verify this, but Frank seems to know what he is talking about
       
 49666 ** so we trust him.
       
 49667 */
       
 49668 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
       
 49669 static u64 floatSwap(u64 in){
       
 49670   union {
       
 49671     u64 r;
       
 49672     u32 i[2];
       
 49673   } u;
       
 49674   u32 t;
       
 49675 
       
 49676   u.r = in;
       
 49677   t = u.i[0];
       
 49678   u.i[0] = u.i[1];
       
 49679   u.i[1] = t;
       
 49680   return u.r;
       
 49681 }
       
 49682 # define swapMixedEndianFloat(X)  X = floatSwap(X)
       
 49683 #else
       
 49684 # define swapMixedEndianFloat(X)
       
 49685 #endif
       
 49686 
       
 49687 /*
       
 49688 ** Write the serialized data blob for the value stored in pMem into 
       
 49689 ** buf. It is assumed that the caller has allocated sufficient space.
       
 49690 ** Return the number of bytes written.
       
 49691 **
       
 49692 ** nBuf is the amount of space left in buf[].  nBuf must always be
       
 49693 ** large enough to hold the entire field.  Except, if the field is
       
 49694 ** a blob with a zero-filled tail, then buf[] might be just the right
       
 49695 ** size to hold everything except for the zero-filled tail.  If buf[]
       
 49696 ** is only big enough to hold the non-zero prefix, then only write that
       
 49697 ** prefix into buf[].  But if buf[] is large enough to hold both the
       
 49698 ** prefix and the tail then write the prefix and set the tail to all
       
 49699 ** zeros.
       
 49700 **
       
 49701 ** Return the number of bytes actually written into buf[].  The number
       
 49702 ** of bytes in the zero-filled tail is included in the return value only
       
 49703 ** if those bytes were zeroed in buf[].
       
 49704 */ 
       
 49705 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
       
 49706   u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
       
 49707   u32 len;
       
 49708 
       
 49709   /* Integer and Real */
       
 49710   if( serial_type<=7 && serial_type>0 ){
       
 49711     u64 v;
       
 49712     u32 i;
       
 49713     if( serial_type==7 ){
       
 49714       assert( sizeof(v)==sizeof(pMem->r) );
       
 49715       memcpy(&v, &pMem->r, sizeof(v));
       
 49716       swapMixedEndianFloat(v);
       
 49717     }else{
       
 49718       v = pMem->u.i;
       
 49719     }
       
 49720     len = i = sqlite3VdbeSerialTypeLen(serial_type);
       
 49721     assert( len<=(u32)nBuf );
       
 49722     while( i-- ){
       
 49723       buf[i] = (u8)(v&0xFF);
       
 49724       v >>= 8;
       
 49725     }
       
 49726     return len;
       
 49727   }
       
 49728 
       
 49729   /* String or blob */
       
 49730   if( serial_type>=12 ){
       
 49731     assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
       
 49732              == (int)sqlite3VdbeSerialTypeLen(serial_type) );
       
 49733     assert( pMem->n<=nBuf );
       
 49734     len = pMem->n;
       
 49735     memcpy(buf, pMem->z, len);
       
 49736     if( pMem->flags & MEM_Zero ){
       
 49737       len += pMem->u.nZero;
       
 49738       assert( nBuf>=0 );
       
 49739       if( len > (u32)nBuf ){
       
 49740         len = (u32)nBuf;
       
 49741       }
       
 49742       memset(&buf[pMem->n], 0, len-pMem->n);
       
 49743     }
       
 49744     return len;
       
 49745   }
       
 49746 
       
 49747   /* NULL or constants 0 or 1 */
       
 49748   return 0;
       
 49749 }
       
 49750 
       
 49751 /*
       
 49752 ** Deserialize the data blob pointed to by buf as serial type serial_type
       
 49753 ** and store the result in pMem.  Return the number of bytes read.
       
 49754 */ 
       
 49755 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
       
 49756   const unsigned char *buf,     /* Buffer to deserialize from */
       
 49757   u32 serial_type,              /* Serial type to deserialize */
       
 49758   Mem *pMem                     /* Memory cell to write value into */
       
 49759 ){
       
 49760   switch( serial_type ){
       
 49761     case 10:   /* Reserved for future use */
       
 49762     case 11:   /* Reserved for future use */
       
 49763     case 0: {  /* NULL */
       
 49764       pMem->flags = MEM_Null;
       
 49765       break;
       
 49766     }
       
 49767     case 1: { /* 1-byte signed integer */
       
 49768       pMem->u.i = (signed char)buf[0];
       
 49769       pMem->flags = MEM_Int;
       
 49770       return 1;
       
 49771     }
       
 49772     case 2: { /* 2-byte signed integer */
       
 49773       pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
       
 49774       pMem->flags = MEM_Int;
       
 49775       return 2;
       
 49776     }
       
 49777     case 3: { /* 3-byte signed integer */
       
 49778       pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
       
 49779       pMem->flags = MEM_Int;
       
 49780       return 3;
       
 49781     }
       
 49782     case 4: { /* 4-byte signed integer */
       
 49783       pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
       
 49784       pMem->flags = MEM_Int;
       
 49785       return 4;
       
 49786     }
       
 49787     case 5: { /* 6-byte signed integer */
       
 49788       u64 x = (((signed char)buf[0])<<8) | buf[1];
       
 49789       u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
       
 49790       x = (x<<32) | y;
       
 49791       pMem->u.i = *(i64*)&x;
       
 49792       pMem->flags = MEM_Int;
       
 49793       return 6;
       
 49794     }
       
 49795     case 6:   /* 8-byte signed integer */
       
 49796     case 7: { /* IEEE floating point */
       
 49797       u64 x;
       
 49798       u32 y;
       
 49799 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
       
 49800       /* Verify that integers and floating point values use the same
       
 49801       ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
       
 49802       ** defined that 64-bit floating point values really are mixed
       
 49803       ** endian.
       
 49804       */
       
 49805       static const u64 t1 = ((u64)0x3ff00000)<<32;
       
 49806       static const double r1 = 1.0;
       
 49807       u64 t2 = t1;
       
 49808       swapMixedEndianFloat(t2);
       
 49809       assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
       
 49810 #endif
       
 49811 
       
 49812       x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
       
 49813       y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
       
 49814       x = (x<<32) | y;
       
 49815       if( serial_type==6 ){
       
 49816         pMem->u.i = *(i64*)&x;
       
 49817         pMem->flags = MEM_Int;
       
 49818       }else{
       
 49819         assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
       
 49820         swapMixedEndianFloat(x);
       
 49821         memcpy(&pMem->r, &x, sizeof(x));
       
 49822         pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
       
 49823       }
       
 49824       return 8;
       
 49825     }
       
 49826     case 8:    /* Integer 0 */
       
 49827     case 9: {  /* Integer 1 */
       
 49828       pMem->u.i = serial_type-8;
       
 49829       pMem->flags = MEM_Int;
       
 49830       return 0;
       
 49831     }
       
 49832     default: {
       
 49833       u32 len = (serial_type-12)/2;
       
 49834       pMem->z = (char *)buf;
       
 49835       pMem->n = len;
       
 49836       pMem->xDel = 0;
       
 49837       if( serial_type&0x01 ){
       
 49838         pMem->flags = MEM_Str | MEM_Ephem;
       
 49839       }else{
       
 49840         pMem->flags = MEM_Blob | MEM_Ephem;
       
 49841       }
       
 49842       return len;
       
 49843     }
       
 49844   }
       
 49845   return 0;
       
 49846 }
       
 49847 
       
 49848 
       
 49849 /*
       
 49850 ** Given the nKey-byte encoding of a record in pKey[], parse the
       
 49851 ** record into a UnpackedRecord structure.  Return a pointer to
       
 49852 ** that structure.
       
 49853 **
       
 49854 ** The calling function might provide szSpace bytes of memory
       
 49855 ** space at pSpace.  This space can be used to hold the returned
       
 49856 ** VDbeParsedRecord structure if it is large enough.  If it is
       
 49857 ** not big enough, space is obtained from sqlite3_malloc().
       
 49858 **
       
 49859 ** The returned structure should be closed by a call to
       
 49860 ** sqlite3VdbeDeleteUnpackedRecord().
       
 49861 */ 
       
 49862 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeRecordUnpack(
       
 49863   KeyInfo *pKeyInfo,     /* Information about the record format */
       
 49864   int nKey,              /* Size of the binary record */
       
 49865   const void *pKey,      /* The binary record */
       
 49866   char *pSpace,          /* Unaligned space available to hold the object */
       
 49867   int szSpace            /* Size of pSpace[] in bytes */
       
 49868 ){
       
 49869   const unsigned char *aKey = (const unsigned char *)pKey;
       
 49870   UnpackedRecord *p;  /* The unpacked record that we will return */
       
 49871   int nByte;          /* Memory space needed to hold p, in bytes */
       
 49872   int d;
       
 49873   u32 idx;
       
 49874   u16 u;              /* Unsigned loop counter */
       
 49875   u32 szHdr;
       
 49876   Mem *pMem;
       
 49877   int nOff;           /* Increase pSpace by this much to 8-byte align it */
       
 49878   
       
 49879   /*
       
 49880   ** We want to shift the pointer pSpace up such that it is 8-byte aligned.
       
 49881   ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift 
       
 49882   ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
       
 49883   */
       
 49884   nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
       
 49885   pSpace += nOff;
       
 49886   szSpace -= nOff;
       
 49887   nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
       
 49888   if( nByte>szSpace ){
       
 49889     p = sqlite3DbMallocRaw(pKeyInfo->db, nByte);
       
 49890     if( p==0 ) return 0;
       
 49891     p->flags = UNPACKED_NEED_FREE | UNPACKED_NEED_DESTROY;
       
 49892   }else{
       
 49893     p = (UnpackedRecord*)pSpace;
       
 49894     p->flags = UNPACKED_NEED_DESTROY;
       
 49895   }
       
 49896   p->pKeyInfo = pKeyInfo;
       
 49897   p->nField = pKeyInfo->nField + 1;
       
 49898   p->aMem = pMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
       
 49899   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
       
 49900   idx = getVarint32(aKey, szHdr);
       
 49901   d = szHdr;
       
 49902   u = 0;
       
 49903   while( idx<szHdr && u<p->nField && d<=nKey ){
       
 49904     u32 serial_type;
       
 49905 
       
 49906     idx += getVarint32(&aKey[idx], serial_type);
       
 49907     pMem->enc = pKeyInfo->enc;
       
 49908     pMem->db = pKeyInfo->db;
       
 49909     pMem->flags = 0;
       
 49910     pMem->zMalloc = 0;
       
 49911     d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
       
 49912     pMem++;
       
 49913     u++;
       
 49914   }
       
 49915   assert( u<=pKeyInfo->nField + 1 );
       
 49916   p->nField = u;
       
 49917   return (void*)p;
       
 49918 }
       
 49919 
       
 49920 /*
       
 49921 ** This routine destroys a UnpackedRecord object.
       
 49922 */
       
 49923 SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
       
 49924   int i;
       
 49925   Mem *pMem;
       
 49926 
       
 49927   assert( p!=0 );
       
 49928   assert( p->flags & UNPACKED_NEED_DESTROY );
       
 49929   for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
       
 49930     /* The unpacked record is always constructed by the
       
 49931     ** sqlite3VdbeUnpackRecord() function above, which makes all
       
 49932     ** strings and blobs static.  And none of the elements are
       
 49933     ** ever transformed, so there is never anything to delete.
       
 49934     */
       
 49935     if( NEVER(pMem->zMalloc) ) sqlite3VdbeMemRelease(pMem);
       
 49936   }
       
 49937   if( p->flags & UNPACKED_NEED_FREE ){
       
 49938     sqlite3DbFree(p->pKeyInfo->db, p);
       
 49939   }
       
 49940 }
       
 49941 
       
 49942 /*
       
 49943 ** This function compares the two table rows or index records
       
 49944 ** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
       
 49945 ** or positive integer if key1 is less than, equal to or 
       
 49946 ** greater than key2.  The {nKey1, pKey1} key must be a blob
       
 49947 ** created by th OP_MakeRecord opcode of the VDBE.  The pPKey2
       
 49948 ** key must be a parsed key such as obtained from
       
 49949 ** sqlite3VdbeParseRecord.
       
 49950 **
       
 49951 ** Key1 and Key2 do not have to contain the same number of fields.
       
 49952 ** The key with fewer fields is usually compares less than the 
       
 49953 ** longer key.  However if the UNPACKED_INCRKEY flags in pPKey2 is set
       
 49954 ** and the common prefixes are equal, then key1 is less than key2.
       
 49955 ** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
       
 49956 ** equal, then the keys are considered to be equal and
       
 49957 ** the parts beyond the common prefix are ignored.
       
 49958 **
       
 49959 ** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of
       
 49960 ** the header of pKey1 is ignored.  It is assumed that pKey1 is
       
 49961 ** an index key, and thus ends with a rowid value.  The last byte
       
 49962 ** of the header will therefore be the serial type of the rowid:
       
 49963 ** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types.
       
 49964 ** The serial type of the final rowid will always be a single byte.
       
 49965 ** By ignoring this last byte of the header, we force the comparison
       
 49966 ** to ignore the rowid at the end of key1.
       
 49967 */
       
 49968 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
       
 49969   int nKey1, const void *pKey1, /* Left key */
       
 49970   UnpackedRecord *pPKey2        /* Right key */
       
 49971 ){
       
 49972   int d1;            /* Offset into aKey[] of next data element */
       
 49973   u32 idx1;          /* Offset into aKey[] of next header element */
       
 49974   u32 szHdr1;        /* Number of bytes in header */
       
 49975   int i = 0;
       
 49976   int nField;
       
 49977   int rc = 0;
       
 49978   const unsigned char *aKey1 = (const unsigned char *)pKey1;
       
 49979   KeyInfo *pKeyInfo;
       
 49980   Mem mem1;
       
 49981 
       
 49982   pKeyInfo = pPKey2->pKeyInfo;
       
 49983   mem1.enc = pKeyInfo->enc;
       
 49984   mem1.db = pKeyInfo->db;
       
 49985   mem1.flags = 0;
       
 49986   mem1.u.i = 0;  /* not needed, here to silence compiler warning */
       
 49987   mem1.zMalloc = 0;
       
 49988   
       
 49989   idx1 = getVarint32(aKey1, szHdr1);
       
 49990   d1 = szHdr1;
       
 49991   if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){
       
 49992     szHdr1--;
       
 49993   }
       
 49994   nField = pKeyInfo->nField;
       
 49995   while( idx1<szHdr1 && i<pPKey2->nField ){
       
 49996     u32 serial_type1;
       
 49997 
       
 49998     /* Read the serial types for the next element in each key. */
       
 49999     idx1 += getVarint32( aKey1+idx1, serial_type1 );
       
 50000     if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
       
 50001 
       
 50002     /* Extract the values to be compared.
       
 50003     */
       
 50004     d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
       
 50005 
       
 50006     /* Do the comparison
       
 50007     */
       
 50008     rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
       
 50009                            i<nField ? pKeyInfo->aColl[i] : 0);
       
 50010     if( rc!=0 ){
       
 50011       break;
       
 50012     }
       
 50013     i++;
       
 50014   }
       
 50015 
       
 50016   /* No memory allocation is ever used on mem1. */
       
 50017   if( NEVER(mem1.zMalloc) ) sqlite3VdbeMemRelease(&mem1);
       
 50018 
       
 50019   /* If the PREFIX_SEARCH flag is set and all fields except the final
       
 50020   ** rowid field were equal, then clear the PREFIX_SEARCH flag and set 
       
 50021   ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
       
 50022   ** This is used by the OP_IsUnique opcode.
       
 50023   */
       
 50024   if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
       
 50025     assert( idx1==szHdr1 && rc );
       
 50026     assert( mem1.flags & MEM_Int );
       
 50027     pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
       
 50028     pPKey2->rowid = mem1.u.i;
       
 50029   }
       
 50030 
       
 50031   if( rc==0 ){
       
 50032     /* rc==0 here means that one of the keys ran out of fields and
       
 50033     ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
       
 50034     ** flag is set, then break the tie by treating key2 as larger.
       
 50035     ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
       
 50036     ** are considered to be equal.  Otherwise, the longer key is the 
       
 50037     ** larger.  As it happens, the pPKey2 will always be the longer
       
 50038     ** if there is a difference.
       
 50039     */
       
 50040     if( pPKey2->flags & UNPACKED_INCRKEY ){
       
 50041       rc = -1;
       
 50042     }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
       
 50043       /* Leave rc==0 */
       
 50044     }else if( idx1<szHdr1 ){
       
 50045       rc = 1;
       
 50046     }
       
 50047   }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
       
 50048                && pKeyInfo->aSortOrder[i] ){
       
 50049     rc = -rc;
       
 50050   }
       
 50051 
       
 50052   return rc;
       
 50053 }
       
 50054  
       
 50055 
       
 50056 /*
       
 50057 ** pCur points at an index entry created using the OP_MakeRecord opcode.
       
 50058 ** Read the rowid (the last field in the record) and store it in *rowid.
       
 50059 ** Return SQLITE_OK if everything works, or an error code otherwise.
       
 50060 **
       
 50061 ** pCur might be pointing to text obtained from a corrupt database file.
       
 50062 ** So the content cannot be trusted.  Do appropriate checks on the content.
       
 50063 */
       
 50064 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
       
 50065   i64 nCellKey = 0;
       
 50066   int rc;
       
 50067   u32 szHdr;        /* Size of the header */
       
 50068   u32 typeRowid;    /* Serial type of the rowid */
       
 50069   u32 lenRowid;     /* Size of the rowid */
       
 50070   Mem m, v;
       
 50071 
       
 50072   UNUSED_PARAMETER(db);
       
 50073 
       
 50074   /* Get the size of the index entry.  Only indices entries of less
       
 50075   ** than 2GiB are support - anything large must be database corruption.
       
 50076   ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
       
 50077   ** this code can safely assume that nCellKey is 32-bits  
       
 50078   */
       
 50079   assert( sqlite3BtreeCursorIsValid(pCur) );
       
 50080   rc = sqlite3BtreeKeySize(pCur, &nCellKey);
       
 50081   assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
       
 50082   assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
       
 50083 
       
 50084   /* Read in the complete content of the index entry */
       
 50085   memset(&m, 0, sizeof(m));
       
 50086   rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
       
 50087   if( rc ){
       
 50088     return rc;
       
 50089   }
       
 50090 
       
 50091   /* The index entry must begin with a header size */
       
 50092   (void)getVarint32((u8*)m.z, szHdr);
       
 50093   testcase( szHdr==3 );
       
 50094   testcase( szHdr==m.n );
       
 50095   if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
       
 50096     goto idx_rowid_corruption;
       
 50097   }
       
 50098 
       
 50099   /* The last field of the index should be an integer - the ROWID.
       
 50100   ** Verify that the last entry really is an integer. */
       
 50101   (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
       
 50102   testcase( typeRowid==1 );
       
 50103   testcase( typeRowid==2 );
       
 50104   testcase( typeRowid==3 );
       
 50105   testcase( typeRowid==4 );
       
 50106   testcase( typeRowid==5 );
       
 50107   testcase( typeRowid==6 );
       
 50108   testcase( typeRowid==8 );
       
 50109   testcase( typeRowid==9 );
       
 50110   if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
       
 50111     goto idx_rowid_corruption;
       
 50112   }
       
 50113   lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
       
 50114   testcase( (u32)m.n==szHdr+lenRowid );
       
 50115   if( unlikely((u32)m.n<szHdr+lenRowid) ){
       
 50116     goto idx_rowid_corruption;
       
 50117   }
       
 50118 
       
 50119   /* Fetch the integer off the end of the index record */
       
 50120   sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
       
 50121   *rowid = v.u.i;
       
 50122   sqlite3VdbeMemRelease(&m);
       
 50123   return SQLITE_OK;
       
 50124 
       
 50125   /* Jump here if database corruption is detected after m has been
       
 50126   ** allocated.  Free the m object and return SQLITE_CORRUPT. */
       
 50127 idx_rowid_corruption:
       
 50128   testcase( m.zMalloc!=0 );
       
 50129   sqlite3VdbeMemRelease(&m);
       
 50130   return SQLITE_CORRUPT_BKPT;
       
 50131 }
       
 50132 
       
 50133 /*
       
 50134 ** Compare the key of the index entry that cursor pC is pointing to against
       
 50135 ** the key string in pUnpacked.  Write into *pRes a number
       
 50136 ** that is negative, zero, or positive if pC is less than, equal to,
       
 50137 ** or greater than pUnpacked.  Return SQLITE_OK on success.
       
 50138 **
       
 50139 ** pUnpacked is either created without a rowid or is truncated so that it
       
 50140 ** omits the rowid at the end.  The rowid at the end of the index entry
       
 50141 ** is ignored as well.  Hence, this routine only compares the prefixes 
       
 50142 ** of the keys prior to the final rowid, not the entire key.
       
 50143 */
       
 50144 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
       
 50145   VdbeCursor *pC,             /* The cursor to compare against */
       
 50146   UnpackedRecord *pUnpacked,  /* Unpacked version of key to compare against */
       
 50147   int *res                    /* Write the comparison result here */
       
 50148 ){
       
 50149   i64 nCellKey = 0;
       
 50150   int rc;
       
 50151   BtCursor *pCur = pC->pCursor;
       
 50152   Mem m;
       
 50153 
       
 50154   assert( sqlite3BtreeCursorIsValid(pCur) );
       
 50155   rc = sqlite3BtreeKeySize(pCur, &nCellKey);
       
 50156   assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
       
 50157   /* nCellKey will always be between 0 and 0xffffffff because of the say
       
 50158   ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
       
 50159   if( nCellKey<=0 || nCellKey>0x7fffffff ){
       
 50160     *res = 0;
       
 50161     return SQLITE_CORRUPT;
       
 50162   }
       
 50163   memset(&m, 0, sizeof(m));
       
 50164   rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
       
 50165   if( rc ){
       
 50166     return rc;
       
 50167   }
       
 50168   assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID );
       
 50169   *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
       
 50170   sqlite3VdbeMemRelease(&m);
       
 50171   return SQLITE_OK;
       
 50172 }
       
 50173 
       
 50174 /*
       
 50175 ** This routine sets the value to be returned by subsequent calls to
       
 50176 ** sqlite3_changes() on the database handle 'db'. 
       
 50177 */
       
 50178 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
       
 50179   assert( sqlite3_mutex_held(db->mutex) );
       
 50180   db->nChange = nChange;
       
 50181   db->nTotalChange += nChange;
       
 50182 }
       
 50183 
       
 50184 /*
       
 50185 ** Set a flag in the vdbe to update the change counter when it is finalised
       
 50186 ** or reset.
       
 50187 */
       
 50188 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
       
 50189   v->changeCntOn = 1;
       
 50190 }
       
 50191 
       
 50192 /*
       
 50193 ** Mark every prepared statement associated with a database connection
       
 50194 ** as expired.
       
 50195 **
       
 50196 ** An expired statement means that recompilation of the statement is
       
 50197 ** recommend.  Statements expire when things happen that make their
       
 50198 ** programs obsolete.  Removing user-defined functions or collating
       
 50199 ** sequences, or changing an authorization function are the types of
       
 50200 ** things that make prepared statements obsolete.
       
 50201 */
       
 50202 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
       
 50203   Vdbe *p;
       
 50204   for(p = db->pVdbe; p; p=p->pNext){
       
 50205     p->expired = 1;
       
 50206   }
       
 50207 }
       
 50208 
       
 50209 /*
       
 50210 ** Return the database associated with the Vdbe.
       
 50211 */
       
 50212 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
       
 50213   return v->db;
       
 50214 }
       
 50215 
       
 50216 /************** End of vdbeaux.c *********************************************/
       
 50217 /************** Begin file vdbeapi.c *****************************************/
       
 50218 /*
       
 50219 ** 2004 May 26
       
 50220 **
       
 50221 ** The author disclaims copyright to this source code.  In place of
       
 50222 ** a legal notice, here is a blessing:
       
 50223 **
       
 50224 **    May you do good and not evil.
       
 50225 **    May you find forgiveness for yourself and forgive others.
       
 50226 **    May you share freely, never taking more than you give.
       
 50227 **
       
 50228 *************************************************************************
       
 50229 **
       
 50230 ** This file contains code use to implement APIs that are part of the
       
 50231 ** VDBE.
       
 50232 **
       
 50233 ** $Id: vdbeapi.c,v 1.167 2009/06/25 01:47:12 drh Exp $
       
 50234 */
       
 50235 
       
 50236 #ifndef SQLITE_OMIT_DEPRECATED
       
 50237 /*
       
 50238 ** Return TRUE (non-zero) of the statement supplied as an argument needs
       
 50239 ** to be recompiled.  A statement needs to be recompiled whenever the
       
 50240 ** execution environment changes in a way that would alter the program
       
 50241 ** that sqlite3_prepare() generates.  For example, if new functions or
       
 50242 ** collating sequences are registered or if an authorizer function is
       
 50243 ** added or changed.
       
 50244 */
       
 50245 SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
       
 50246   Vdbe *p = (Vdbe*)pStmt;
       
 50247   return p==0 || p->expired;
       
 50248 }
       
 50249 #endif
       
 50250 
       
 50251 /*
       
 50252 ** The following routine destroys a virtual machine that is created by
       
 50253 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
       
 50254 ** success/failure code that describes the result of executing the virtual
       
 50255 ** machine.
       
 50256 **
       
 50257 ** This routine sets the error code and string returned by
       
 50258 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
       
 50259 */
       
 50260 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
       
 50261   int rc;
       
 50262   if( pStmt==0 ){
       
 50263     rc = SQLITE_OK;
       
 50264   }else{
       
 50265     Vdbe *v = (Vdbe*)pStmt;
       
 50266     sqlite3 *db = v->db;
       
 50267 #if SQLITE_THREADSAFE
       
 50268     sqlite3_mutex *mutex = v->db->mutex;
       
 50269 #endif
       
 50270     sqlite3_mutex_enter(mutex);
       
 50271     rc = sqlite3VdbeFinalize(v);
       
 50272     rc = sqlite3ApiExit(db, rc);
       
 50273     sqlite3_mutex_leave(mutex);
       
 50274   }
       
 50275   return rc;
       
 50276 }
       
 50277 
       
 50278 /*
       
 50279 ** Terminate the current execution of an SQL statement and reset it
       
 50280 ** back to its starting state so that it can be reused. A success code from
       
 50281 ** the prior execution is returned.
       
 50282 **
       
 50283 ** This routine sets the error code and string returned by
       
 50284 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
       
 50285 */
       
 50286 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
       
 50287   int rc;
       
 50288   if( pStmt==0 ){
       
 50289     rc = SQLITE_OK;
       
 50290   }else{
       
 50291     Vdbe *v = (Vdbe*)pStmt;
       
 50292     sqlite3_mutex_enter(v->db->mutex);
       
 50293     rc = sqlite3VdbeReset(v);
       
 50294     sqlite3VdbeMakeReady(v, -1, 0, 0, 0, 0, 0);
       
 50295     assert( (rc & (v->db->errMask))==rc );
       
 50296     rc = sqlite3ApiExit(v->db, rc);
       
 50297     sqlite3_mutex_leave(v->db->mutex);
       
 50298   }
       
 50299   return rc;
       
 50300 }
       
 50301 
       
 50302 /*
       
 50303 ** Set all the parameters in the compiled SQL statement to NULL.
       
 50304 */
       
 50305 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
       
 50306   int i;
       
 50307   int rc = SQLITE_OK;
       
 50308   Vdbe *p = (Vdbe*)pStmt;
       
 50309 #if SQLITE_THREADSAFE
       
 50310   sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
       
 50311 #endif
       
 50312   sqlite3_mutex_enter(mutex);
       
 50313   for(i=0; i<p->nVar; i++){
       
 50314     sqlite3VdbeMemRelease(&p->aVar[i]);
       
 50315     p->aVar[i].flags = MEM_Null;
       
 50316   }
       
 50317   sqlite3_mutex_leave(mutex);
       
 50318   return rc;
       
 50319 }
       
 50320 
       
 50321 
       
 50322 /**************************** sqlite3_value_  *******************************
       
 50323 ** The following routines extract information from a Mem or sqlite3_value
       
 50324 ** structure.
       
 50325 */
       
 50326 SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
       
 50327   Mem *p = (Mem*)pVal;
       
 50328   if( p->flags & (MEM_Blob|MEM_Str) ){
       
 50329     sqlite3VdbeMemExpandBlob(p);
       
 50330     p->flags &= ~MEM_Str;
       
 50331     p->flags |= MEM_Blob;
       
 50332     return p->z;
       
 50333   }else{
       
 50334     return sqlite3_value_text(pVal);
       
 50335   }
       
 50336 }
       
 50337 SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
       
 50338   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
       
 50339 }
       
 50340 SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
       
 50341   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
       
 50342 }
       
 50343 SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
       
 50344   return sqlite3VdbeRealValue((Mem*)pVal);
       
 50345 }
       
 50346 SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
       
 50347   return (int)sqlite3VdbeIntValue((Mem*)pVal);
       
 50348 }
       
 50349 SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
       
 50350   return sqlite3VdbeIntValue((Mem*)pVal);
       
 50351 }
       
 50352 SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
       
 50353   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
       
 50354 }
       
 50355 #ifndef SQLITE_OMIT_UTF16
       
 50356 SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
       
 50357   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
       
 50358 }
       
 50359 SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
       
 50360   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
       
 50361 }
       
 50362 SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
       
 50363   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
       
 50364 }
       
 50365 #endif /* SQLITE_OMIT_UTF16 */
       
 50366 SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
       
 50367   return pVal->type;
       
 50368 }
       
 50369 
       
 50370 /**************************** sqlite3_result_  *******************************
       
 50371 ** The following routines are used by user-defined functions to specify
       
 50372 ** the function result.
       
 50373 **
       
 50374 ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
       
 50375 ** result as a string or blob but if the string or blob is too large, it
       
 50376 ** then sets the error code to SQLITE_TOOBIG
       
 50377 */
       
 50378 static void setResultStrOrError(
       
 50379   sqlite3_context *pCtx,  /* Function context */
       
 50380   const char *z,          /* String pointer */
       
 50381   int n,                  /* Bytes in string, or negative */
       
 50382   u8 enc,                 /* Encoding of z.  0 for BLOBs */
       
 50383   void (*xDel)(void*)     /* Destructor function */
       
 50384 ){
       
 50385   if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
       
 50386     sqlite3_result_error_toobig(pCtx);
       
 50387   }
       
 50388 }
       
 50389 SQLITE_API void sqlite3_result_blob(
       
 50390   sqlite3_context *pCtx, 
       
 50391   const void *z, 
       
 50392   int n, 
       
 50393   void (*xDel)(void *)
       
 50394 ){
       
 50395   assert( n>=0 );
       
 50396   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50397   setResultStrOrError(pCtx, z, n, 0, xDel);
       
 50398 }
       
 50399 SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
       
 50400   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50401   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
       
 50402 }
       
 50403 SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
       
 50404   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50405   pCtx->isError = SQLITE_ERROR;
       
 50406   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
       
 50407 }
       
 50408 #ifndef SQLITE_OMIT_UTF16
       
 50409 SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
       
 50410   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50411   pCtx->isError = SQLITE_ERROR;
       
 50412   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
       
 50413 }
       
 50414 #endif
       
 50415 SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
       
 50416   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50417   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
       
 50418 }
       
 50419 SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
       
 50420   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50421   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
       
 50422 }
       
 50423 SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
       
 50424   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50425   sqlite3VdbeMemSetNull(&pCtx->s);
       
 50426 }
       
 50427 SQLITE_API void sqlite3_result_text(
       
 50428   sqlite3_context *pCtx, 
       
 50429   const char *z, 
       
 50430   int n,
       
 50431   void (*xDel)(void *)
       
 50432 ){
       
 50433   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50434   setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
       
 50435 }
       
 50436 #ifndef SQLITE_OMIT_UTF16
       
 50437 SQLITE_API void sqlite3_result_text16(
       
 50438   sqlite3_context *pCtx, 
       
 50439   const void *z, 
       
 50440   int n, 
       
 50441   void (*xDel)(void *)
       
 50442 ){
       
 50443   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50444   setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
       
 50445 }
       
 50446 SQLITE_API void sqlite3_result_text16be(
       
 50447   sqlite3_context *pCtx, 
       
 50448   const void *z, 
       
 50449   int n, 
       
 50450   void (*xDel)(void *)
       
 50451 ){
       
 50452   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50453   setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
       
 50454 }
       
 50455 SQLITE_API void sqlite3_result_text16le(
       
 50456   sqlite3_context *pCtx, 
       
 50457   const void *z, 
       
 50458   int n, 
       
 50459   void (*xDel)(void *)
       
 50460 ){
       
 50461   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50462   setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
       
 50463 }
       
 50464 #endif /* SQLITE_OMIT_UTF16 */
       
 50465 SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
       
 50466   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50467   sqlite3VdbeMemCopy(&pCtx->s, pValue);
       
 50468 }
       
 50469 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
       
 50470   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50471   sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
       
 50472 }
       
 50473 SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
       
 50474   pCtx->isError = errCode;
       
 50475   if( pCtx->s.flags & MEM_Null ){
       
 50476     sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1, 
       
 50477                          SQLITE_UTF8, SQLITE_STATIC);
       
 50478   }
       
 50479 }
       
 50480 
       
 50481 /* Force an SQLITE_TOOBIG error. */
       
 50482 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
       
 50483   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50484   pCtx->isError = SQLITE_TOOBIG;
       
 50485   sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 
       
 50486                        SQLITE_UTF8, SQLITE_STATIC);
       
 50487 }
       
 50488 
       
 50489 /* An SQLITE_NOMEM error. */
       
 50490 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
       
 50491   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50492   sqlite3VdbeMemSetNull(&pCtx->s);
       
 50493   pCtx->isError = SQLITE_NOMEM;
       
 50494   pCtx->s.db->mallocFailed = 1;
       
 50495 }
       
 50496 
       
 50497 /*
       
 50498 ** Execute the statement pStmt, either until a row of data is ready, the
       
 50499 ** statement is completely executed or an error occurs.
       
 50500 **
       
 50501 ** This routine implements the bulk of the logic behind the sqlite_step()
       
 50502 ** API.  The only thing omitted is the automatic recompile if a 
       
 50503 ** schema change has occurred.  That detail is handled by the
       
 50504 ** outer sqlite3_step() wrapper procedure.
       
 50505 */
       
 50506 static int sqlite3Step(Vdbe *p){
       
 50507   sqlite3 *db;
       
 50508   int rc;
       
 50509 
       
 50510   assert(p);
       
 50511   if( p->magic!=VDBE_MAGIC_RUN ){
       
 50512     return SQLITE_MISUSE;
       
 50513   }
       
 50514 
       
 50515   /* Assert that malloc() has not failed */
       
 50516   db = p->db;
       
 50517   if( db->mallocFailed ){
       
 50518     return SQLITE_NOMEM;
       
 50519   }
       
 50520 
       
 50521   if( p->pc<=0 && p->expired ){
       
 50522     if( ALWAYS(p->rc==SQLITE_OK) ){
       
 50523       p->rc = SQLITE_SCHEMA;
       
 50524     }
       
 50525     rc = SQLITE_ERROR;
       
 50526     goto end_of_step;
       
 50527   }
       
 50528   if( sqlite3SafetyOn(db) ){
       
 50529     p->rc = SQLITE_MISUSE;
       
 50530     return SQLITE_MISUSE;
       
 50531   }
       
 50532   if( p->pc<0 ){
       
 50533     /* If there are no other statements currently running, then
       
 50534     ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
       
 50535     ** from interrupting a statement that has not yet started.
       
 50536     */
       
 50537     if( db->activeVdbeCnt==0 ){
       
 50538       db->u1.isInterrupted = 0;
       
 50539     }
       
 50540 
       
 50541     assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 );
       
 50542 
       
 50543 #ifndef SQLITE_OMIT_TRACE
       
 50544     if( db->xProfile && !db->init.busy ){
       
 50545       double rNow;
       
 50546       sqlite3OsCurrentTime(db->pVfs, &rNow);
       
 50547       p->startTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
       
 50548     }
       
 50549 #endif
       
 50550 
       
 50551     db->activeVdbeCnt++;
       
 50552     if( p->readOnly==0 ) db->writeVdbeCnt++;
       
 50553     p->pc = 0;
       
 50554   }
       
 50555 #ifndef SQLITE_OMIT_EXPLAIN
       
 50556   if( p->explain ){
       
 50557     rc = sqlite3VdbeList(p);
       
 50558   }else
       
 50559 #endif /* SQLITE_OMIT_EXPLAIN */
       
 50560   {
       
 50561     rc = sqlite3VdbeExec(p);
       
 50562   }
       
 50563 
       
 50564   if( sqlite3SafetyOff(db) ){
       
 50565     rc = SQLITE_MISUSE;
       
 50566   }
       
 50567 
       
 50568 #ifndef SQLITE_OMIT_TRACE
       
 50569   /* Invoke the profile callback if there is one
       
 50570   */
       
 50571   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
       
 50572     double rNow;
       
 50573     u64 elapseTime;
       
 50574 
       
 50575     sqlite3OsCurrentTime(db->pVfs, &rNow);
       
 50576     elapseTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
       
 50577     elapseTime -= p->startTime;
       
 50578     db->xProfile(db->pProfileArg, p->zSql, elapseTime);
       
 50579   }
       
 50580 #endif
       
 50581 
       
 50582   db->errCode = rc;
       
 50583   if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
       
 50584     p->rc = SQLITE_NOMEM;
       
 50585   }
       
 50586 end_of_step:
       
 50587   /* At this point local variable rc holds the value that should be 
       
 50588   ** returned if this statement was compiled using the legacy 
       
 50589   ** sqlite3_prepare() interface. According to the docs, this can only
       
 50590   ** be one of the values in the first assert() below. Variable p->rc 
       
 50591   ** contains the value that would be returned if sqlite3_finalize() 
       
 50592   ** were called on statement p.
       
 50593   */
       
 50594   assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR 
       
 50595        || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
       
 50596   );
       
 50597   assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
       
 50598   if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
       
 50599     /* If this statement was prepared using sqlite3_prepare_v2(), and an
       
 50600     ** error has occured, then return the error code in p->rc to the
       
 50601     ** caller. Set the error code in the database handle to the same value.
       
 50602     */ 
       
 50603     rc = db->errCode = p->rc;
       
 50604   }
       
 50605   return (rc&db->errMask);
       
 50606 }
       
 50607 
       
 50608 /*
       
 50609 ** This is the top-level implementation of sqlite3_step().  Call
       
 50610 ** sqlite3Step() to do most of the work.  If a schema error occurs,
       
 50611 ** call sqlite3Reprepare() and try again.
       
 50612 */
       
 50613 SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
       
 50614   int rc = SQLITE_MISUSE;
       
 50615   if( pStmt ){
       
 50616     int cnt = 0;
       
 50617     Vdbe *v = (Vdbe*)pStmt;
       
 50618     sqlite3 *db = v->db;
       
 50619     sqlite3_mutex_enter(db->mutex);
       
 50620     while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
       
 50621            && cnt++ < 5
       
 50622            && (rc = sqlite3Reprepare(v))==SQLITE_OK ){
       
 50623       sqlite3_reset(pStmt);
       
 50624       v->expired = 0;
       
 50625     }
       
 50626     if( rc==SQLITE_SCHEMA && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
       
 50627       /* This case occurs after failing to recompile an sql statement. 
       
 50628       ** The error message from the SQL compiler has already been loaded 
       
 50629       ** into the database handle. This block copies the error message 
       
 50630       ** from the database handle into the statement and sets the statement
       
 50631       ** program counter to 0 to ensure that when the statement is 
       
 50632       ** finalized or reset the parser error message is available via
       
 50633       ** sqlite3_errmsg() and sqlite3_errcode().
       
 50634       */
       
 50635       const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
       
 50636       sqlite3DbFree(db, v->zErrMsg);
       
 50637       if( !db->mallocFailed ){
       
 50638         v->zErrMsg = sqlite3DbStrDup(db, zErr);
       
 50639       } else {
       
 50640         v->zErrMsg = 0;
       
 50641         v->rc = SQLITE_NOMEM;
       
 50642       }
       
 50643     }
       
 50644     rc = sqlite3ApiExit(db, rc);
       
 50645     sqlite3_mutex_leave(db->mutex);
       
 50646   }
       
 50647   return rc;
       
 50648 }
       
 50649 
       
 50650 /*
       
 50651 ** Extract the user data from a sqlite3_context structure and return a
       
 50652 ** pointer to it.
       
 50653 */
       
 50654 SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
       
 50655   assert( p && p->pFunc );
       
 50656   return p->pFunc->pUserData;
       
 50657 }
       
 50658 
       
 50659 /*
       
 50660 ** Extract the user data from a sqlite3_context structure and return a
       
 50661 ** pointer to it.
       
 50662 */
       
 50663 SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
       
 50664   assert( p && p->pFunc );
       
 50665   return p->s.db;
       
 50666 }
       
 50667 
       
 50668 /*
       
 50669 ** The following is the implementation of an SQL function that always
       
 50670 ** fails with an error message stating that the function is used in the
       
 50671 ** wrong context.  The sqlite3_overload_function() API might construct
       
 50672 ** SQL function that use this routine so that the functions will exist
       
 50673 ** for name resolution but are actually overloaded by the xFindFunction
       
 50674 ** method of virtual tables.
       
 50675 */
       
 50676 SQLITE_PRIVATE void sqlite3InvalidFunction(
       
 50677   sqlite3_context *context,  /* The function calling context */
       
 50678   int NotUsed,               /* Number of arguments to the function */
       
 50679   sqlite3_value **NotUsed2   /* Value of each argument */
       
 50680 ){
       
 50681   const char *zName = context->pFunc->zName;
       
 50682   char *zErr;
       
 50683   UNUSED_PARAMETER2(NotUsed, NotUsed2);
       
 50684   zErr = sqlite3_mprintf(
       
 50685       "unable to use function %s in the requested context", zName);
       
 50686   sqlite3_result_error(context, zErr, -1);
       
 50687   sqlite3_free(zErr);
       
 50688 }
       
 50689 
       
 50690 /*
       
 50691 ** Allocate or return the aggregate context for a user function.  A new
       
 50692 ** context is allocated on the first call.  Subsequent calls return the
       
 50693 ** same context that was returned on prior calls.
       
 50694 */
       
 50695 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
       
 50696   Mem *pMem;
       
 50697   assert( p && p->pFunc && p->pFunc->xStep );
       
 50698   assert( sqlite3_mutex_held(p->s.db->mutex) );
       
 50699   pMem = p->pMem;
       
 50700   if( (pMem->flags & MEM_Agg)==0 ){
       
 50701     if( nByte==0 ){
       
 50702       sqlite3VdbeMemReleaseExternal(pMem);
       
 50703       pMem->flags = MEM_Null;
       
 50704       pMem->z = 0;
       
 50705     }else{
       
 50706       sqlite3VdbeMemGrow(pMem, nByte, 0);
       
 50707       pMem->flags = MEM_Agg;
       
 50708       pMem->u.pDef = p->pFunc;
       
 50709       if( pMem->z ){
       
 50710         memset(pMem->z, 0, nByte);
       
 50711       }
       
 50712     }
       
 50713   }
       
 50714   return (void*)pMem->z;
       
 50715 }
       
 50716 
       
 50717 /*
       
 50718 ** Return the auxilary data pointer, if any, for the iArg'th argument to
       
 50719 ** the user-function defined by pCtx.
       
 50720 */
       
 50721 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
       
 50722   VdbeFunc *pVdbeFunc;
       
 50723 
       
 50724   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50725   pVdbeFunc = pCtx->pVdbeFunc;
       
 50726   if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
       
 50727     return 0;
       
 50728   }
       
 50729   return pVdbeFunc->apAux[iArg].pAux;
       
 50730 }
       
 50731 
       
 50732 /*
       
 50733 ** Set the auxilary data pointer and delete function, for the iArg'th
       
 50734 ** argument to the user-function defined by pCtx. Any previous value is
       
 50735 ** deleted by calling the delete function specified when it was set.
       
 50736 */
       
 50737 SQLITE_API void sqlite3_set_auxdata(
       
 50738   sqlite3_context *pCtx, 
       
 50739   int iArg, 
       
 50740   void *pAux, 
       
 50741   void (*xDelete)(void*)
       
 50742 ){
       
 50743   struct AuxData *pAuxData;
       
 50744   VdbeFunc *pVdbeFunc;
       
 50745   if( iArg<0 ) goto failed;
       
 50746 
       
 50747   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
       
 50748   pVdbeFunc = pCtx->pVdbeFunc;
       
 50749   if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
       
 50750     int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
       
 50751     int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
       
 50752     pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
       
 50753     if( !pVdbeFunc ){
       
 50754       goto failed;
       
 50755     }
       
 50756     pCtx->pVdbeFunc = pVdbeFunc;
       
 50757     memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
       
 50758     pVdbeFunc->nAux = iArg+1;
       
 50759     pVdbeFunc->pFunc = pCtx->pFunc;
       
 50760   }
       
 50761 
       
 50762   pAuxData = &pVdbeFunc->apAux[iArg];
       
 50763   if( pAuxData->pAux && pAuxData->xDelete ){
       
 50764     pAuxData->xDelete(pAuxData->pAux);
       
 50765   }
       
 50766   pAuxData->pAux = pAux;
       
 50767   pAuxData->xDelete = xDelete;
       
 50768   return;
       
 50769 
       
 50770 failed:
       
 50771   if( xDelete ){
       
 50772     xDelete(pAux);
       
 50773   }
       
 50774 }
       
 50775 
       
 50776 #ifndef SQLITE_OMIT_DEPRECATED
       
 50777 /*
       
 50778 ** Return the number of times the Step function of a aggregate has been 
       
 50779 ** called.
       
 50780 **
       
 50781 ** This function is deprecated.  Do not use it for new code.  It is
       
 50782 ** provide only to avoid breaking legacy code.  New aggregate function
       
 50783 ** implementations should keep their own counts within their aggregate
       
 50784 ** context.
       
 50785 */
       
 50786 SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
       
 50787   assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
       
 50788   return p->pMem->n;
       
 50789 }
       
 50790 #endif
       
 50791 
       
 50792 /*
       
 50793 ** Return the number of columns in the result set for the statement pStmt.
       
 50794 */
       
 50795 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
       
 50796   Vdbe *pVm = (Vdbe *)pStmt;
       
 50797   return pVm ? pVm->nResColumn : 0;
       
 50798 }
       
 50799 
       
 50800 /*
       
 50801 ** Return the number of values available from the current row of the
       
 50802 ** currently executing statement pStmt.
       
 50803 */
       
 50804 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
       
 50805   Vdbe *pVm = (Vdbe *)pStmt;
       
 50806   if( pVm==0 || pVm->pResultSet==0 ) return 0;
       
 50807   return pVm->nResColumn;
       
 50808 }
       
 50809 
       
 50810 
       
 50811 /*
       
 50812 ** Check to see if column iCol of the given statement is valid.  If
       
 50813 ** it is, return a pointer to the Mem for the value of that column.
       
 50814 ** If iCol is not valid, return a pointer to a Mem which has a value
       
 50815 ** of NULL.
       
 50816 */
       
 50817 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
       
 50818   Vdbe *pVm;
       
 50819   int vals;
       
 50820   Mem *pOut;
       
 50821 
       
 50822   pVm = (Vdbe *)pStmt;
       
 50823   if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
       
 50824     sqlite3_mutex_enter(pVm->db->mutex);
       
 50825     vals = sqlite3_data_count(pStmt);
       
 50826     pOut = &pVm->pResultSet[i];
       
 50827   }else{
       
 50828     /* If the value passed as the second argument is out of range, return
       
 50829     ** a pointer to the following static Mem object which contains the
       
 50830     ** value SQL NULL. Even though the Mem structure contains an element
       
 50831     ** of type i64, on certain architecture (x86) with certain compiler
       
 50832     ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
       
 50833     ** instead of an 8-byte one. This all works fine, except that when
       
 50834     ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
       
 50835     ** that a Mem structure is located on an 8-byte boundary. To prevent
       
 50836     ** this assert() from failing, when building with SQLITE_DEBUG defined
       
 50837     ** using gcc, force nullMem to be 8-byte aligned using the magical
       
 50838     ** __attribute__((aligned(8))) macro.  */
       
 50839     static const Mem nullMem 
       
 50840 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
       
 50841       __attribute__((aligned(8))) 
       
 50842 #endif
       
 50843       = {{0}, (double)0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
       
 50844 
       
 50845     if( pVm && ALWAYS(pVm->db) ){
       
 50846       sqlite3_mutex_enter(pVm->db->mutex);
       
 50847       sqlite3Error(pVm->db, SQLITE_RANGE, 0);
       
 50848     }
       
 50849     pOut = (Mem*)&nullMem;
       
 50850   }
       
 50851   return pOut;
       
 50852 }
       
 50853 
       
 50854 /*
       
 50855 ** This function is called after invoking an sqlite3_value_XXX function on a 
       
 50856 ** column value (i.e. a value returned by evaluating an SQL expression in the
       
 50857 ** select list of a SELECT statement) that may cause a malloc() failure. If 
       
 50858 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
       
 50859 ** code of statement pStmt set to SQLITE_NOMEM.
       
 50860 **
       
 50861 ** Specifically, this is called from within:
       
 50862 **
       
 50863 **     sqlite3_column_int()
       
 50864 **     sqlite3_column_int64()
       
 50865 **     sqlite3_column_text()
       
 50866 **     sqlite3_column_text16()
       
 50867 **     sqlite3_column_real()
       
 50868 **     sqlite3_column_bytes()
       
 50869 **     sqlite3_column_bytes16()
       
 50870 **
       
 50871 ** But not for sqlite3_column_blob(), which never calls malloc().
       
 50872 */
       
 50873 static void columnMallocFailure(sqlite3_stmt *pStmt)
       
 50874 {
       
 50875   /* If malloc() failed during an encoding conversion within an
       
 50876   ** sqlite3_column_XXX API, then set the return code of the statement to
       
 50877   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
       
 50878   ** and _finalize() will return NOMEM.
       
 50879   */
       
 50880   Vdbe *p = (Vdbe *)pStmt;
       
 50881   if( p ){
       
 50882     p->rc = sqlite3ApiExit(p->db, p->rc);
       
 50883     sqlite3_mutex_leave(p->db->mutex);
       
 50884   }
       
 50885 }
       
 50886 
       
 50887 /**************************** sqlite3_column_  *******************************
       
 50888 ** The following routines are used to access elements of the current row
       
 50889 ** in the result set.
       
 50890 */
       
 50891 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
       
 50892   const void *val;
       
 50893   val = sqlite3_value_blob( columnMem(pStmt,i) );
       
 50894   /* Even though there is no encoding conversion, value_blob() might
       
 50895   ** need to call malloc() to expand the result of a zeroblob() 
       
 50896   ** expression. 
       
 50897   */
       
 50898   columnMallocFailure(pStmt);
       
 50899   return val;
       
 50900 }
       
 50901 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
       
 50902   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
       
 50903   columnMallocFailure(pStmt);
       
 50904   return val;
       
 50905 }
       
 50906 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
       
 50907   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
       
 50908   columnMallocFailure(pStmt);
       
 50909   return val;
       
 50910 }
       
 50911 SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
       
 50912   double val = sqlite3_value_double( columnMem(pStmt,i) );
       
 50913   columnMallocFailure(pStmt);
       
 50914   return val;
       
 50915 }
       
 50916 SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
       
 50917   int val = sqlite3_value_int( columnMem(pStmt,i) );
       
 50918   columnMallocFailure(pStmt);
       
 50919   return val;
       
 50920 }
       
 50921 SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
       
 50922   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
       
 50923   columnMallocFailure(pStmt);
       
 50924   return val;
       
 50925 }
       
 50926 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
       
 50927   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
       
 50928   columnMallocFailure(pStmt);
       
 50929   return val;
       
 50930 }
       
 50931 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
       
 50932   Mem *pOut = columnMem(pStmt, i);
       
 50933   if( pOut->flags&MEM_Static ){
       
 50934     pOut->flags &= ~MEM_Static;
       
 50935     pOut->flags |= MEM_Ephem;
       
 50936   }
       
 50937   columnMallocFailure(pStmt);
       
 50938   return (sqlite3_value *)pOut;
       
 50939 }
       
 50940 #ifndef SQLITE_OMIT_UTF16
       
 50941 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
       
 50942   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
       
 50943   columnMallocFailure(pStmt);
       
 50944   return val;
       
 50945 }
       
 50946 #endif /* SQLITE_OMIT_UTF16 */
       
 50947 SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
       
 50948   int iType = sqlite3_value_type( columnMem(pStmt,i) );
       
 50949   columnMallocFailure(pStmt);
       
 50950   return iType;
       
 50951 }
       
 50952 
       
 50953 /* The following function is experimental and subject to change or
       
 50954 ** removal */
       
 50955 /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
       
 50956 **  return sqlite3_value_numeric_type( columnMem(pStmt,i) );
       
 50957 **}
       
 50958 */
       
 50959 
       
 50960 /*
       
 50961 ** Convert the N-th element of pStmt->pColName[] into a string using
       
 50962 ** xFunc() then return that string.  If N is out of range, return 0.
       
 50963 **
       
 50964 ** There are up to 5 names for each column.  useType determines which
       
 50965 ** name is returned.  Here are the names:
       
 50966 **
       
 50967 **    0      The column name as it should be displayed for output
       
 50968 **    1      The datatype name for the column
       
 50969 **    2      The name of the database that the column derives from
       
 50970 **    3      The name of the table that the column derives from
       
 50971 **    4      The name of the table column that the result column derives from
       
 50972 **
       
 50973 ** If the result is not a simple column reference (if it is an expression
       
 50974 ** or a constant) then useTypes 2, 3, and 4 return NULL.
       
 50975 */
       
 50976 static const void *columnName(
       
 50977   sqlite3_stmt *pStmt,
       
 50978   int N,
       
 50979   const void *(*xFunc)(Mem*),
       
 50980   int useType
       
 50981 ){
       
 50982   const void *ret = 0;
       
 50983   Vdbe *p = (Vdbe *)pStmt;
       
 50984   int n;
       
 50985   sqlite3 *db = p->db;
       
 50986   
       
 50987   assert( db!=0 );
       
 50988   n = sqlite3_column_count(pStmt);
       
 50989   if( N<n && N>=0 ){
       
 50990     N += useType*n;
       
 50991     sqlite3_mutex_enter(db->mutex);
       
 50992     assert( db->mallocFailed==0 );
       
 50993     ret = xFunc(&p->aColName[N]);
       
 50994      /* A malloc may have failed inside of the xFunc() call. If this
       
 50995     ** is the case, clear the mallocFailed flag and return NULL.
       
 50996     */
       
 50997     if( db->mallocFailed ){
       
 50998       db->mallocFailed = 0;
       
 50999       ret = 0;
       
 51000     }
       
 51001     sqlite3_mutex_leave(db->mutex);
       
 51002   }
       
 51003   return ret;
       
 51004 }
       
 51005 
       
 51006 /*
       
 51007 ** Return the name of the Nth column of the result set returned by SQL
       
 51008 ** statement pStmt.
       
 51009 */
       
 51010 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
       
 51011   return columnName(
       
 51012       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
       
 51013 }
       
 51014 #ifndef SQLITE_OMIT_UTF16
       
 51015 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
       
 51016   return columnName(
       
 51017       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
       
 51018 }
       
 51019 #endif
       
 51020 
       
 51021 /*
       
 51022 ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
       
 51023 ** not define OMIT_DECLTYPE.
       
 51024 */
       
 51025 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
       
 51026 # error "Must not define both SQLITE_OMIT_DECLTYPE \
       
 51027          and SQLITE_ENABLE_COLUMN_METADATA"
       
 51028 #endif
       
 51029 
       
 51030 #ifndef SQLITE_OMIT_DECLTYPE
       
 51031 /*
       
 51032 ** Return the column declaration type (if applicable) of the 'i'th column
       
 51033 ** of the result set of SQL statement pStmt.
       
 51034 */
       
 51035 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
       
 51036   return columnName(
       
 51037       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
       
 51038 }
       
 51039 #ifndef SQLITE_OMIT_UTF16
       
 51040 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
       
 51041   return columnName(
       
 51042       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
       
 51043 }
       
 51044 #endif /* SQLITE_OMIT_UTF16 */
       
 51045 #endif /* SQLITE_OMIT_DECLTYPE */
       
 51046 
       
 51047 #ifdef SQLITE_ENABLE_COLUMN_METADATA
       
 51048 /*
       
 51049 ** Return the name of the database from which a result column derives.
       
 51050 ** NULL is returned if the result column is an expression or constant or
       
 51051 ** anything else which is not an unabiguous reference to a database column.
       
 51052 */
       
 51053 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
       
 51054   return columnName(
       
 51055       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
       
 51056 }
       
 51057 #ifndef SQLITE_OMIT_UTF16
       
 51058 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
       
 51059   return columnName(
       
 51060       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
       
 51061 }
       
 51062 #endif /* SQLITE_OMIT_UTF16 */
       
 51063 
       
 51064 /*
       
 51065 ** Return the name of the table from which a result column derives.
       
 51066 ** NULL is returned if the result column is an expression or constant or
       
 51067 ** anything else which is not an unabiguous reference to a database column.
       
 51068 */
       
 51069 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
       
 51070   return columnName(
       
 51071       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
       
 51072 }
       
 51073 #ifndef SQLITE_OMIT_UTF16
       
 51074 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
       
 51075   return columnName(
       
 51076       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
       
 51077 }
       
 51078 #endif /* SQLITE_OMIT_UTF16 */
       
 51079 
       
 51080 /*
       
 51081 ** Return the name of the table column from which a result column derives.
       
 51082 ** NULL is returned if the result column is an expression or constant or
       
 51083 ** anything else which is not an unabiguous reference to a database column.
       
 51084 */
       
 51085 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
       
 51086   return columnName(
       
 51087       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
       
 51088 }
       
 51089 #ifndef SQLITE_OMIT_UTF16
       
 51090 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
       
 51091   return columnName(
       
 51092       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
       
 51093 }
       
 51094 #endif /* SQLITE_OMIT_UTF16 */
       
 51095 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
       
 51096 
       
 51097 
       
 51098 /******************************* sqlite3_bind_  ***************************
       
 51099 ** 
       
 51100 ** Routines used to attach values to wildcards in a compiled SQL statement.
       
 51101 */
       
 51102 /*
       
 51103 ** Unbind the value bound to variable i in virtual machine p. This is the 
       
 51104 ** the same as binding a NULL value to the column. If the "i" parameter is
       
 51105 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
       
 51106 **
       
 51107 ** A successful evaluation of this routine acquires the mutex on p.
       
 51108 ** the mutex is released if any kind of error occurs.
       
 51109 **
       
 51110 ** The error code stored in database p->db is overwritten with the return
       
 51111 ** value in any case.
       
 51112 */
       
 51113 static int vdbeUnbind(Vdbe *p, int i){
       
 51114   Mem *pVar;
       
 51115   if( p==0 ) return SQLITE_MISUSE;
       
 51116   sqlite3_mutex_enter(p->db->mutex);
       
 51117   if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
       
 51118     sqlite3Error(p->db, SQLITE_MISUSE, 0);
       
 51119     sqlite3_mutex_leave(p->db->mutex);
       
 51120     return SQLITE_MISUSE;
       
 51121   }
       
 51122   if( i<1 || i>p->nVar ){
       
 51123     sqlite3Error(p->db, SQLITE_RANGE, 0);
       
 51124     sqlite3_mutex_leave(p->db->mutex);
       
 51125     return SQLITE_RANGE;
       
 51126   }
       
 51127   i--;
       
 51128   pVar = &p->aVar[i];
       
 51129   sqlite3VdbeMemRelease(pVar);
       
 51130   pVar->flags = MEM_Null;
       
 51131   sqlite3Error(p->db, SQLITE_OK, 0);
       
 51132   return SQLITE_OK;
       
 51133 }
       
 51134 
       
 51135 /*
       
 51136 ** Bind a text or BLOB value.
       
 51137 */
       
 51138 static int bindText(
       
 51139   sqlite3_stmt *pStmt,   /* The statement to bind against */
       
 51140   int i,                 /* Index of the parameter to bind */
       
 51141   const void *zData,     /* Pointer to the data to be bound */
       
 51142   int nData,             /* Number of bytes of data to be bound */
       
 51143   void (*xDel)(void*),   /* Destructor for the data */
       
 51144   u8 encoding            /* Encoding for the data */
       
 51145 ){
       
 51146   Vdbe *p = (Vdbe *)pStmt;
       
 51147   Mem *pVar;
       
 51148   int rc;
       
 51149 
       
 51150   rc = vdbeUnbind(p, i);
       
 51151   if( rc==SQLITE_OK ){
       
 51152     if( zData!=0 ){
       
 51153       pVar = &p->aVar[i-1];
       
 51154       rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
       
 51155       if( rc==SQLITE_OK && encoding!=0 ){
       
 51156         rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
       
 51157       }
       
 51158       sqlite3Error(p->db, rc, 0);
       
 51159       rc = sqlite3ApiExit(p->db, rc);
       
 51160     }
       
 51161     sqlite3_mutex_leave(p->db->mutex);
       
 51162   }
       
 51163   return rc;
       
 51164 }
       
 51165 
       
 51166 
       
 51167 /*
       
 51168 ** Bind a blob value to an SQL statement variable.
       
 51169 */
       
 51170 SQLITE_API int sqlite3_bind_blob(
       
 51171   sqlite3_stmt *pStmt, 
       
 51172   int i, 
       
 51173   const void *zData, 
       
 51174   int nData, 
       
 51175   void (*xDel)(void*)
       
 51176 ){
       
 51177   return bindText(pStmt, i, zData, nData, xDel, 0);
       
 51178 }
       
 51179 SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
       
 51180   int rc;
       
 51181   Vdbe *p = (Vdbe *)pStmt;
       
 51182   rc = vdbeUnbind(p, i);
       
 51183   if( rc==SQLITE_OK ){
       
 51184     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
       
 51185     sqlite3_mutex_leave(p->db->mutex);
       
 51186   }
       
 51187   return rc;
       
 51188 }
       
 51189 SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
       
 51190   return sqlite3_bind_int64(p, i, (i64)iValue);
       
 51191 }
       
 51192 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
       
 51193   int rc;
       
 51194   Vdbe *p = (Vdbe *)pStmt;
       
 51195   rc = vdbeUnbind(p, i);
       
 51196   if( rc==SQLITE_OK ){
       
 51197     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
       
 51198     sqlite3_mutex_leave(p->db->mutex);
       
 51199   }
       
 51200   return rc;
       
 51201 }
       
 51202 SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
       
 51203   int rc;
       
 51204   Vdbe *p = (Vdbe*)pStmt;
       
 51205   rc = vdbeUnbind(p, i);
       
 51206   if( rc==SQLITE_OK ){
       
 51207     sqlite3_mutex_leave(p->db->mutex);
       
 51208   }
       
 51209   return rc;
       
 51210 }
       
 51211 SQLITE_API int sqlite3_bind_text( 
       
 51212   sqlite3_stmt *pStmt, 
       
 51213   int i, 
       
 51214   const char *zData, 
       
 51215   int nData, 
       
 51216   void (*xDel)(void*)
       
 51217 ){
       
 51218   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
       
 51219 }
       
 51220 #ifndef SQLITE_OMIT_UTF16
       
 51221 SQLITE_API int sqlite3_bind_text16(
       
 51222   sqlite3_stmt *pStmt, 
       
 51223   int i, 
       
 51224   const void *zData, 
       
 51225   int nData, 
       
 51226   void (*xDel)(void*)
       
 51227 ){
       
 51228   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
       
 51229 }
       
 51230 #endif /* SQLITE_OMIT_UTF16 */
       
 51231 SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
       
 51232   int rc;
       
 51233   switch( pValue->type ){
       
 51234     case SQLITE_INTEGER: {
       
 51235       rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
       
 51236       break;
       
 51237     }
       
 51238     case SQLITE_FLOAT: {
       
 51239       rc = sqlite3_bind_double(pStmt, i, pValue->r);
       
 51240       break;
       
 51241     }
       
 51242     case SQLITE_BLOB: {
       
 51243       if( pValue->flags & MEM_Zero ){
       
 51244         rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
       
 51245       }else{
       
 51246         rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
       
 51247       }
       
 51248       break;
       
 51249     }
       
 51250     case SQLITE_TEXT: {
       
 51251       rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
       
 51252                               pValue->enc);
       
 51253       break;
       
 51254     }
       
 51255     default: {
       
 51256       rc = sqlite3_bind_null(pStmt, i);
       
 51257       break;
       
 51258     }
       
 51259   }
       
 51260   return rc;
       
 51261 }
       
 51262 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
       
 51263   int rc;
       
 51264   Vdbe *p = (Vdbe *)pStmt;
       
 51265   rc = vdbeUnbind(p, i);
       
 51266   if( rc==SQLITE_OK ){
       
 51267     sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
       
 51268     sqlite3_mutex_leave(p->db->mutex);
       
 51269   }
       
 51270   return rc;
       
 51271 }
       
 51272 
       
 51273 /*
       
 51274 ** Return the number of wildcards that can be potentially bound to.
       
 51275 ** This routine is added to support DBD::SQLite.  
       
 51276 */
       
 51277 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
       
 51278   Vdbe *p = (Vdbe*)pStmt;
       
 51279   return p ? p->nVar : 0;
       
 51280 }
       
 51281 
       
 51282 /*
       
 51283 ** Create a mapping from variable numbers to variable names
       
 51284 ** in the Vdbe.azVar[] array, if such a mapping does not already
       
 51285 ** exist.
       
 51286 */
       
 51287 static void createVarMap(Vdbe *p){
       
 51288   if( !p->okVar ){
       
 51289     int j;
       
 51290     Op *pOp;
       
 51291     sqlite3_mutex_enter(p->db->mutex);
       
 51292     /* The race condition here is harmless.  If two threads call this
       
 51293     ** routine on the same Vdbe at the same time, they both might end
       
 51294     ** up initializing the Vdbe.azVar[] array.  That is a little extra
       
 51295     ** work but it results in the same answer.
       
 51296     */
       
 51297     for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
       
 51298       if( pOp->opcode==OP_Variable ){
       
 51299         assert( pOp->p1>0 && pOp->p1<=p->nVar );
       
 51300         p->azVar[pOp->p1-1] = pOp->p4.z;
       
 51301       }
       
 51302     }
       
 51303     p->okVar = 1;
       
 51304     sqlite3_mutex_leave(p->db->mutex);
       
 51305   }
       
 51306 }
       
 51307 
       
 51308 /*
       
 51309 ** Return the name of a wildcard parameter.  Return NULL if the index
       
 51310 ** is out of range or if the wildcard is unnamed.
       
 51311 **
       
 51312 ** The result is always UTF-8.
       
 51313 */
       
 51314 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
       
 51315   Vdbe *p = (Vdbe*)pStmt;
       
 51316   if( p==0 || i<1 || i>p->nVar ){
       
 51317     return 0;
       
 51318   }
       
 51319   createVarMap(p);
       
 51320   return p->azVar[i-1];
       
 51321 }
       
 51322 
       
 51323 /*
       
 51324 ** Given a wildcard parameter name, return the index of the variable
       
 51325 ** with that name.  If there is no variable with the given name,
       
 51326 ** return 0.
       
 51327 */
       
 51328 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
       
 51329   Vdbe *p = (Vdbe*)pStmt;
       
 51330   int i;
       
 51331   if( p==0 ){
       
 51332     return 0;
       
 51333   }
       
 51334   createVarMap(p); 
       
 51335   if( zName ){
       
 51336     for(i=0; i<p->nVar; i++){
       
 51337       const char *z = p->azVar[i];
       
 51338       if( z && strcmp(z,zName)==0 ){
       
 51339         return i+1;
       
 51340       }
       
 51341     }
       
 51342   }
       
 51343   return 0;
       
 51344 }
       
 51345 
       
 51346 /*
       
 51347 ** Transfer all bindings from the first statement over to the second.
       
 51348 */
       
 51349 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
       
 51350   Vdbe *pFrom = (Vdbe*)pFromStmt;
       
 51351   Vdbe *pTo = (Vdbe*)pToStmt;
       
 51352   int i;
       
 51353   assert( pTo->db==pFrom->db );
       
 51354   assert( pTo->nVar==pFrom->nVar );
       
 51355   sqlite3_mutex_enter(pTo->db->mutex);
       
 51356   for(i=0; i<pFrom->nVar; i++){
       
 51357     sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
       
 51358   }
       
 51359   sqlite3_mutex_leave(pTo->db->mutex);
       
 51360   return SQLITE_OK;
       
 51361 }
       
 51362 
       
 51363 #ifndef SQLITE_OMIT_DEPRECATED
       
 51364 /*
       
 51365 ** Deprecated external interface.  Internal/core SQLite code
       
 51366 ** should call sqlite3TransferBindings.
       
 51367 **
       
 51368 ** Is is misuse to call this routine with statements from different
       
 51369 ** database connections.  But as this is a deprecated interface, we
       
 51370 ** will not bother to check for that condition.
       
 51371 **
       
 51372 ** If the two statements contain a different number of bindings, then
       
 51373 ** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
       
 51374 ** SQLITE_OK is returned.
       
 51375 */
       
 51376 SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
       
 51377   Vdbe *pFrom = (Vdbe*)pFromStmt;
       
 51378   Vdbe *pTo = (Vdbe*)pToStmt;
       
 51379   if( pFrom->nVar!=pTo->nVar ){
       
 51380     return SQLITE_ERROR;
       
 51381   }
       
 51382   return sqlite3TransferBindings(pFromStmt, pToStmt);
       
 51383 }
       
 51384 #endif
       
 51385 
       
 51386 /*
       
 51387 ** Return the sqlite3* database handle to which the prepared statement given
       
 51388 ** in the argument belongs.  This is the same database handle that was
       
 51389 ** the first argument to the sqlite3_prepare() that was used to create
       
 51390 ** the statement in the first place.
       
 51391 */
       
 51392 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
       
 51393   return pStmt ? ((Vdbe*)pStmt)->db : 0;
       
 51394 }
       
 51395 
       
 51396 /*
       
 51397 ** Return a pointer to the next prepared statement after pStmt associated
       
 51398 ** with database connection pDb.  If pStmt is NULL, return the first
       
 51399 ** prepared statement for the database connection.  Return NULL if there
       
 51400 ** are no more.
       
 51401 */
       
 51402 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
       
 51403   sqlite3_stmt *pNext;
       
 51404   sqlite3_mutex_enter(pDb->mutex);
       
 51405   if( pStmt==0 ){
       
 51406     pNext = (sqlite3_stmt*)pDb->pVdbe;
       
 51407   }else{
       
 51408     pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
       
 51409   }
       
 51410   sqlite3_mutex_leave(pDb->mutex);
       
 51411   return pNext;
       
 51412 }
       
 51413 
       
 51414 /*
       
 51415 ** Return the value of a status counter for a prepared statement
       
 51416 */
       
 51417 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
       
 51418   Vdbe *pVdbe = (Vdbe*)pStmt;
       
 51419   int v = pVdbe->aCounter[op-1];
       
 51420   if( resetFlag ) pVdbe->aCounter[op-1] = 0;
       
 51421   return v;
       
 51422 }
       
 51423 
       
 51424 /************** End of vdbeapi.c *********************************************/
       
 51425 /************** Begin file vdbe.c ********************************************/
       
 51426 /*
       
 51427 ** 2001 September 15
       
 51428 **
       
 51429 ** The author disclaims copyright to this source code.  In place of
       
 51430 ** a legal notice, here is a blessing:
       
 51431 **
       
 51432 **    May you do good and not evil.
       
 51433 **    May you find forgiveness for yourself and forgive others.
       
 51434 **    May you share freely, never taking more than you give.
       
 51435 **
       
 51436 *************************************************************************
       
 51437 ** The code in this file implements execution method of the 
       
 51438 ** Virtual Database Engine (VDBE).  A separate file ("vdbeaux.c")
       
 51439 ** handles housekeeping details such as creating and deleting
       
 51440 ** VDBE instances.  This file is solely interested in executing
       
 51441 ** the VDBE program.
       
 51442 **
       
 51443 ** In the external interface, an "sqlite3_stmt*" is an opaque pointer
       
 51444 ** to a VDBE.
       
 51445 **
       
 51446 ** The SQL parser generates a program which is then executed by
       
 51447 ** the VDBE to do the work of the SQL statement.  VDBE programs are 
       
 51448 ** similar in form to assembly language.  The program consists of
       
 51449 ** a linear sequence of operations.  Each operation has an opcode 
       
 51450 ** and 5 operands.  Operands P1, P2, and P3 are integers.  Operand P4 
       
 51451 ** is a null-terminated string.  Operand P5 is an unsigned character.
       
 51452 ** Few opcodes use all 5 operands.
       
 51453 **
       
 51454 ** Computation results are stored on a set of registers numbered beginning
       
 51455 ** with 1 and going up to Vdbe.nMem.  Each register can store
       
 51456 ** either an integer, a null-terminated string, a floating point
       
 51457 ** number, or the SQL "NULL" value.  An implicit conversion from one
       
 51458 ** type to the other occurs as necessary.
       
 51459 ** 
       
 51460 ** Most of the code in this file is taken up by the sqlite3VdbeExec()
       
 51461 ** function which does the work of interpreting a VDBE program.
       
 51462 ** But other routines are also provided to help in building up
       
 51463 ** a program instruction by instruction.
       
 51464 **
       
 51465 ** Various scripts scan this source file in order to generate HTML
       
 51466 ** documentation, headers files, or other derived files.  The formatting
       
 51467 ** of the code in this file is, therefore, important.  See other comments
       
 51468 ** in this file for details.  If in doubt, do not deviate from existing
       
 51469 ** commenting and indentation practices when changing or adding code.
       
 51470 **
       
 51471 ** $Id: vdbe.c,v 1.874 2009/07/24 17:58:53 danielk1977 Exp $
       
 51472 */
       
 51473 
       
 51474 /*
       
 51475 ** The following global variable is incremented every time a cursor
       
 51476 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
       
 51477 ** procedures use this information to make sure that indices are
       
 51478 ** working correctly.  This variable has no function other than to
       
 51479 ** help verify the correct operation of the library.
       
 51480 */
       
 51481 #ifdef SQLITE_TEST
       
 51482 SQLITE_API int sqlite3_search_count = 0;
       
 51483 #endif
       
 51484 
       
 51485 /*
       
 51486 ** When this global variable is positive, it gets decremented once before
       
 51487 ** each instruction in the VDBE.  When reaches zero, the u1.isInterrupted
       
 51488 ** field of the sqlite3 structure is set in order to simulate and interrupt.
       
 51489 **
       
 51490 ** This facility is used for testing purposes only.  It does not function
       
 51491 ** in an ordinary build.
       
 51492 */
       
 51493 #ifdef SQLITE_TEST
       
 51494 SQLITE_API int sqlite3_interrupt_count = 0;
       
 51495 #endif
       
 51496 
       
 51497 /*
       
 51498 ** The next global variable is incremented each type the OP_Sort opcode
       
 51499 ** is executed.  The test procedures use this information to make sure that
       
 51500 ** sorting is occurring or not occurring at appropriate times.   This variable
       
 51501 ** has no function other than to help verify the correct operation of the
       
 51502 ** library.
       
 51503 */
       
 51504 #ifdef SQLITE_TEST
       
 51505 SQLITE_API int sqlite3_sort_count = 0;
       
 51506 #endif
       
 51507 
       
 51508 /*
       
 51509 ** The next global variable records the size of the largest MEM_Blob
       
 51510 ** or MEM_Str that has been used by a VDBE opcode.  The test procedures
       
 51511 ** use this information to make sure that the zero-blob functionality
       
 51512 ** is working correctly.   This variable has no function other than to
       
 51513 ** help verify the correct operation of the library.
       
 51514 */
       
 51515 #ifdef SQLITE_TEST
       
 51516 SQLITE_API int sqlite3_max_blobsize = 0;
       
 51517 static void updateMaxBlobsize(Mem *p){
       
 51518   if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
       
 51519     sqlite3_max_blobsize = p->n;
       
 51520   }
       
 51521 }
       
 51522 #endif
       
 51523 
       
 51524 /*
       
 51525 ** The next global variable is incremented each type the OP_Found opcode
       
 51526 ** is executed. This is used to test whether or not the foreign key
       
 51527 ** operation implemented using OP_FkIsZero is working. This variable
       
 51528 ** has no function other than to help verify the correct operation of the
       
 51529 ** library.
       
 51530 */
       
 51531 #ifdef SQLITE_TEST
       
 51532 SQLITE_API int sqlite3_found_count = 0;
       
 51533 #endif
       
 51534 
       
 51535 /*
       
 51536 ** Test a register to see if it exceeds the current maximum blob size.
       
 51537 ** If it does, record the new maximum blob size.
       
 51538 */
       
 51539 #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
       
 51540 # define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
       
 51541 #else
       
 51542 # define UPDATE_MAX_BLOBSIZE(P)
       
 51543 #endif
       
 51544 
       
 51545 /*
       
 51546 ** Convert the given register into a string if it isn't one
       
 51547 ** already. Return non-zero if a malloc() fails.
       
 51548 */
       
 51549 #define Stringify(P, enc) \
       
 51550    if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
       
 51551      { goto no_mem; }
       
 51552 
       
 51553 /*
       
 51554 ** An ephemeral string value (signified by the MEM_Ephem flag) contains
       
 51555 ** a pointer to a dynamically allocated string where some other entity
       
 51556 ** is responsible for deallocating that string.  Because the register
       
 51557 ** does not control the string, it might be deleted without the register
       
 51558 ** knowing it.
       
 51559 **
       
 51560 ** This routine converts an ephemeral string into a dynamically allocated
       
 51561 ** string that the register itself controls.  In other words, it
       
 51562 ** converts an MEM_Ephem string into an MEM_Dyn string.
       
 51563 */
       
 51564 #define Deephemeralize(P) \
       
 51565    if( ((P)->flags&MEM_Ephem)!=0 \
       
 51566        && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
       
 51567 
       
 51568 /*
       
 51569 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
       
 51570 ** P if required.
       
 51571 */
       
 51572 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
       
 51573 
       
 51574 /*
       
 51575 ** Argument pMem points at a register that will be passed to a
       
 51576 ** user-defined function or returned to the user as the result of a query.
       
 51577 ** The second argument, 'db_enc' is the text encoding used by the vdbe for
       
 51578 ** register variables.  This routine sets the pMem->enc and pMem->type
       
 51579 ** variables used by the sqlite3_value_*() routines.
       
 51580 */
       
 51581 #define storeTypeInfo(A,B) _storeTypeInfo(A)
       
 51582 static void _storeTypeInfo(Mem *pMem){
       
 51583   int flags = pMem->flags;
       
 51584   if( flags & MEM_Null ){
       
 51585     pMem->type = SQLITE_NULL;
       
 51586   }
       
 51587   else if( flags & MEM_Int ){
       
 51588     pMem->type = SQLITE_INTEGER;
       
 51589   }
       
 51590   else if( flags & MEM_Real ){
       
 51591     pMem->type = SQLITE_FLOAT;
       
 51592   }
       
 51593   else if( flags & MEM_Str ){
       
 51594     pMem->type = SQLITE_TEXT;
       
 51595   }else{
       
 51596     pMem->type = SQLITE_BLOB;
       
 51597   }
       
 51598 }
       
 51599 
       
 51600 /*
       
 51601 ** Properties of opcodes.  The OPFLG_INITIALIZER macro is
       
 51602 ** created by mkopcodeh.awk during compilation.  Data is obtained
       
 51603 ** from the comments following the "case OP_xxxx:" statements in
       
 51604 ** this file.  
       
 51605 */
       
 51606 static const unsigned char opcodeProperty[] = OPFLG_INITIALIZER;
       
 51607 
       
 51608 /*
       
 51609 ** Return true if an opcode has any of the OPFLG_xxx properties
       
 51610 ** specified by mask.
       
 51611 */
       
 51612 SQLITE_PRIVATE int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){
       
 51613   assert( opcode>0 && opcode<(int)sizeof(opcodeProperty) );
       
 51614   return (opcodeProperty[opcode]&mask)!=0;
       
 51615 }
       
 51616 
       
 51617 /*
       
 51618 ** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
       
 51619 ** if we run out of memory.
       
 51620 */
       
 51621 static VdbeCursor *allocateCursor(
       
 51622   Vdbe *p,              /* The virtual machine */
       
 51623   int iCur,             /* Index of the new VdbeCursor */
       
 51624   int nField,           /* Number of fields in the table or index */
       
 51625   int iDb,              /* When database the cursor belongs to, or -1 */
       
 51626   int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
       
 51627 ){
       
 51628   /* Find the memory cell that will be used to store the blob of memory
       
 51629   ** required for this VdbeCursor structure. It is convenient to use a 
       
 51630   ** vdbe memory cell to manage the memory allocation required for a
       
 51631   ** VdbeCursor structure for the following reasons:
       
 51632   **
       
 51633   **   * Sometimes cursor numbers are used for a couple of different
       
 51634   **     purposes in a vdbe program. The different uses might require
       
 51635   **     different sized allocations. Memory cells provide growable
       
 51636   **     allocations.
       
 51637   **
       
 51638   **   * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
       
 51639   **     be freed lazily via the sqlite3_release_memory() API. This
       
 51640   **     minimizes the number of malloc calls made by the system.
       
 51641   **
       
 51642   ** Memory cells for cursors are allocated at the top of the address
       
 51643   ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
       
 51644   ** cursor 1 is managed by memory cell (p->nMem-1), etc.
       
 51645   */
       
 51646   Mem *pMem = &p->aMem[p->nMem-iCur];
       
 51647 
       
 51648   int nByte;
       
 51649   VdbeCursor *pCx = 0;
       
 51650   nByte = 
       
 51651       sizeof(VdbeCursor) + 
       
 51652       (isBtreeCursor?sqlite3BtreeCursorSize():0) + 
       
 51653       2*nField*sizeof(u32);
       
 51654 
       
 51655   assert( iCur<p->nCursor );
       
 51656   if( p->apCsr[iCur] ){
       
 51657     sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
       
 51658     p->apCsr[iCur] = 0;
       
 51659   }
       
 51660   if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
       
 51661     p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
       
 51662     memset(pMem->z, 0, nByte);
       
 51663     pCx->iDb = iDb;
       
 51664     pCx->nField = nField;
       
 51665     if( nField ){
       
 51666       pCx->aType = (u32 *)&pMem->z[sizeof(VdbeCursor)];
       
 51667     }
       
 51668     if( isBtreeCursor ){
       
 51669       pCx->pCursor = (BtCursor*)
       
 51670           &pMem->z[sizeof(VdbeCursor)+2*nField*sizeof(u32)];
       
 51671     }
       
 51672   }
       
 51673   return pCx;
       
 51674 }
       
 51675 
       
 51676 /*
       
 51677 ** Try to convert a value into a numeric representation if we can
       
 51678 ** do so without loss of information.  In other words, if the string
       
 51679 ** looks like a number, convert it into a number.  If it does not
       
 51680 ** look like a number, leave it alone.
       
 51681 */
       
 51682 static void applyNumericAffinity(Mem *pRec){
       
 51683   if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
       
 51684     int realnum;
       
 51685     sqlite3VdbeMemNulTerminate(pRec);
       
 51686     if( (pRec->flags&MEM_Str)
       
 51687          && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){
       
 51688       i64 value;
       
 51689       sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8);
       
 51690       if( !realnum && sqlite3Atoi64(pRec->z, &value) ){
       
 51691         pRec->u.i = value;
       
 51692         MemSetTypeFlag(pRec, MEM_Int);
       
 51693       }else{
       
 51694         sqlite3VdbeMemRealify(pRec);
       
 51695       }
       
 51696     }
       
 51697   }
       
 51698 }
       
 51699 
       
 51700 /*
       
 51701 ** Processing is determine by the affinity parameter:
       
 51702 **
       
 51703 ** SQLITE_AFF_INTEGER:
       
 51704 ** SQLITE_AFF_REAL:
       
 51705 ** SQLITE_AFF_NUMERIC:
       
 51706 **    Try to convert pRec to an integer representation or a 
       
 51707 **    floating-point representation if an integer representation
       
 51708 **    is not possible.  Note that the integer representation is
       
 51709 **    always preferred, even if the affinity is REAL, because
       
 51710 **    an integer representation is more space efficient on disk.
       
 51711 **
       
 51712 ** SQLITE_AFF_TEXT:
       
 51713 **    Convert pRec to a text representation.
       
 51714 **
       
 51715 ** SQLITE_AFF_NONE:
       
 51716 **    No-op.  pRec is unchanged.
       
 51717 */
       
 51718 static void applyAffinity(
       
 51719   Mem *pRec,          /* The value to apply affinity to */
       
 51720   char affinity,      /* The affinity to be applied */
       
 51721   u8 enc              /* Use this text encoding */
       
 51722 ){
       
 51723   if( affinity==SQLITE_AFF_TEXT ){
       
 51724     /* Only attempt the conversion to TEXT if there is an integer or real
       
 51725     ** representation (blob and NULL do not get converted) but no string
       
 51726     ** representation.
       
 51727     */
       
 51728     if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
       
 51729       sqlite3VdbeMemStringify(pRec, enc);
       
 51730     }
       
 51731     pRec->flags &= ~(MEM_Real|MEM_Int);
       
 51732   }else if( affinity!=SQLITE_AFF_NONE ){
       
 51733     assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
       
 51734              || affinity==SQLITE_AFF_NUMERIC );
       
 51735     applyNumericAffinity(pRec);
       
 51736     if( pRec->flags & MEM_Real ){
       
 51737       sqlite3VdbeIntegerAffinity(pRec);
       
 51738     }
       
 51739   }
       
 51740 }
       
 51741 
       
 51742 /*
       
 51743 ** Try to convert the type of a function argument or a result column
       
 51744 ** into a numeric representation.  Use either INTEGER or REAL whichever
       
 51745 ** is appropriate.  But only do the conversion if it is possible without
       
 51746 ** loss of information and return the revised type of the argument.
       
 51747 **
       
 51748 ** This is an EXPERIMENTAL api and is subject to change or removal.
       
 51749 */
       
 51750 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
       
 51751   Mem *pMem = (Mem*)pVal;
       
 51752   applyNumericAffinity(pMem);
       
 51753   storeTypeInfo(pMem, 0);
       
 51754   return pMem->type;
       
 51755 }
       
 51756 
       
 51757 /*
       
 51758 ** Exported version of applyAffinity(). This one works on sqlite3_value*, 
       
 51759 ** not the internal Mem* type.
       
 51760 */
       
 51761 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
       
 51762   sqlite3_value *pVal, 
       
 51763   u8 affinity, 
       
 51764   u8 enc
       
 51765 ){
       
 51766   applyAffinity((Mem *)pVal, affinity, enc);
       
 51767 }
       
 51768 
       
 51769 #ifdef SQLITE_DEBUG
       
 51770 /*
       
 51771 ** Write a nice string representation of the contents of cell pMem
       
 51772 ** into buffer zBuf, length nBuf.
       
 51773 */
       
 51774 SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
       
 51775   char *zCsr = zBuf;
       
 51776   int f = pMem->flags;
       
 51777 
       
 51778   static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
       
 51779 
       
 51780   if( f&MEM_Blob ){
       
 51781     int i;
       
 51782     char c;
       
 51783     if( f & MEM_Dyn ){
       
 51784       c = 'z';
       
 51785       assert( (f & (MEM_Static|MEM_Ephem))==0 );
       
 51786     }else if( f & MEM_Static ){
       
 51787       c = 't';
       
 51788       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
       
 51789     }else if( f & MEM_Ephem ){
       
 51790       c = 'e';
       
 51791       assert( (f & (MEM_Static|MEM_Dyn))==0 );
       
 51792     }else{
       
 51793       c = 's';
       
 51794     }
       
 51795 
       
 51796     sqlite3_snprintf(100, zCsr, "%c", c);
       
 51797     zCsr += sqlite3Strlen30(zCsr);
       
 51798     sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
       
 51799     zCsr += sqlite3Strlen30(zCsr);
       
 51800     for(i=0; i<16 && i<pMem->n; i++){
       
 51801       sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
       
 51802       zCsr += sqlite3Strlen30(zCsr);
       
 51803     }
       
 51804     for(i=0; i<16 && i<pMem->n; i++){
       
 51805       char z = pMem->z[i];
       
 51806       if( z<32 || z>126 ) *zCsr++ = '.';
       
 51807       else *zCsr++ = z;
       
 51808     }
       
 51809 
       
 51810     sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
       
 51811     zCsr += sqlite3Strlen30(zCsr);
       
 51812     if( f & MEM_Zero ){
       
 51813       sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
       
 51814       zCsr += sqlite3Strlen30(zCsr);
       
 51815     }
       
 51816     *zCsr = '\0';
       
 51817   }else if( f & MEM_Str ){
       
 51818     int j, k;
       
 51819     zBuf[0] = ' ';
       
 51820     if( f & MEM_Dyn ){
       
 51821       zBuf[1] = 'z';
       
 51822       assert( (f & (MEM_Static|MEM_Ephem))==0 );
       
 51823     }else if( f & MEM_Static ){
       
 51824       zBuf[1] = 't';
       
 51825       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
       
 51826     }else if( f & MEM_Ephem ){
       
 51827       zBuf[1] = 'e';
       
 51828       assert( (f & (MEM_Static|MEM_Dyn))==0 );
       
 51829     }else{
       
 51830       zBuf[1] = 's';
       
 51831     }
       
 51832     k = 2;
       
 51833     sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
       
 51834     k += sqlite3Strlen30(&zBuf[k]);
       
 51835     zBuf[k++] = '[';
       
 51836     for(j=0; j<15 && j<pMem->n; j++){
       
 51837       u8 c = pMem->z[j];
       
 51838       if( c>=0x20 && c<0x7f ){
       
 51839         zBuf[k++] = c;
       
 51840       }else{
       
 51841         zBuf[k++] = '.';
       
 51842       }
       
 51843     }
       
 51844     zBuf[k++] = ']';
       
 51845     sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
       
 51846     k += sqlite3Strlen30(&zBuf[k]);
       
 51847     zBuf[k++] = 0;
       
 51848   }
       
 51849 }
       
 51850 #endif
       
 51851 
       
 51852 #ifdef SQLITE_DEBUG
       
 51853 /*
       
 51854 ** Print the value of a register for tracing purposes:
       
 51855 */
       
 51856 static void memTracePrint(FILE *out, Mem *p){
       
 51857   if( p->flags & MEM_Null ){
       
 51858     fprintf(out, " NULL");
       
 51859   }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
       
 51860     fprintf(out, " si:%lld", p->u.i);
       
 51861   }else if( p->flags & MEM_Int ){
       
 51862     fprintf(out, " i:%lld", p->u.i);
       
 51863 #ifndef SQLITE_OMIT_FLOATING_POINT
       
 51864   }else if( p->flags & MEM_Real ){
       
 51865     fprintf(out, " r:%g", p->r);
       
 51866 #endif
       
 51867   }else if( p->flags & MEM_RowSet ){
       
 51868     fprintf(out, " (rowset)");
       
 51869   }else{
       
 51870     char zBuf[200];
       
 51871     sqlite3VdbeMemPrettyPrint(p, zBuf);
       
 51872     fprintf(out, " ");
       
 51873     fprintf(out, "%s", zBuf);
       
 51874   }
       
 51875 }
       
 51876 static void registerTrace(FILE *out, int iReg, Mem *p){
       
 51877   fprintf(out, "REG[%d] = ", iReg);
       
 51878   memTracePrint(out, p);
       
 51879   fprintf(out, "\n");
       
 51880 }
       
 51881 #endif
       
 51882 
       
 51883 #ifdef SQLITE_DEBUG
       
 51884 #  define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
       
 51885 #else
       
 51886 #  define REGISTER_TRACE(R,M)
       
 51887 #endif
       
 51888 
       
 51889 
       
 51890 #ifdef VDBE_PROFILE
       
 51891 
       
 51892 /* 
       
 51893 ** hwtime.h contains inline assembler code for implementing 
       
 51894 ** high-performance timing routines.
       
 51895 */
       
 51896 /************** Include hwtime.h in the middle of vdbe.c *********************/
       
 51897 /************** Begin file hwtime.h ******************************************/
       
 51898 /*
       
 51899 ** 2008 May 27
       
 51900 **
       
 51901 ** The author disclaims copyright to this source code.  In place of
       
 51902 ** a legal notice, here is a blessing:
       
 51903 **
       
 51904 **    May you do good and not evil.
       
 51905 **    May you find forgiveness for yourself and forgive others.
       
 51906 **    May you share freely, never taking more than you give.
       
 51907 **
       
 51908 ******************************************************************************
       
 51909 **
       
 51910 ** This file contains inline asm code for retrieving "high-performance"
       
 51911 ** counters for x86 class CPUs.
       
 51912 **
       
 51913 ** $Id: hwtime.h,v 1.3 2008/08/01 14:33:15 shane Exp $
       
 51914 */
       
 51915 #ifndef _HWTIME_H_
       
 51916 #define _HWTIME_H_
       
 51917 
       
 51918 /*
       
 51919 ** The following routine only works on pentium-class (or newer) processors.
       
 51920 ** It uses the RDTSC opcode to read the cycle count value out of the
       
 51921 ** processor and returns that value.  This can be used for high-res
       
 51922 ** profiling.
       
 51923 */
       
 51924 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
       
 51925       (defined(i386) || defined(__i386__) || defined(_M_IX86))
       
 51926 
       
 51927   #if defined(__GNUC__)
       
 51928 
       
 51929   __inline__ sqlite_uint64 sqlite3Hwtime(void){
       
 51930      unsigned int lo, hi;
       
 51931      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
       
 51932      return (sqlite_uint64)hi << 32 | lo;
       
 51933   }
       
 51934 
       
 51935   #elif defined(_MSC_VER)
       
 51936 
       
 51937   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
       
 51938      __asm {
       
 51939         rdtsc
       
 51940         ret       ; return value at EDX:EAX
       
 51941      }
       
 51942   }
       
 51943 
       
 51944   #endif
       
 51945 
       
 51946 #elif (defined(__GNUC__) && defined(__x86_64__))
       
 51947 
       
 51948   __inline__ sqlite_uint64 sqlite3Hwtime(void){
       
 51949       unsigned long val;
       
 51950       __asm__ __volatile__ ("rdtsc" : "=A" (val));
       
 51951       return val;
       
 51952   }
       
 51953  
       
 51954 #elif (defined(__GNUC__) && defined(__ppc__))
       
 51955 
       
 51956   __inline__ sqlite_uint64 sqlite3Hwtime(void){
       
 51957       unsigned long long retval;
       
 51958       unsigned long junk;
       
 51959       __asm__ __volatile__ ("\n\
       
 51960           1:      mftbu   %1\n\
       
 51961                   mftb    %L0\n\
       
 51962                   mftbu   %0\n\
       
 51963                   cmpw    %0,%1\n\
       
 51964                   bne     1b"
       
 51965                   : "=r" (retval), "=r" (junk));
       
 51966       return retval;
       
 51967   }
       
 51968 
       
 51969 #else
       
 51970 
       
 51971   #error Need implementation of sqlite3Hwtime() for your platform.
       
 51972 
       
 51973   /*
       
 51974   ** To compile without implementing sqlite3Hwtime() for your platform,
       
 51975   ** you can remove the above #error and use the following
       
 51976   ** stub function.  You will lose timing support for many
       
 51977   ** of the debugging and testing utilities, but it should at
       
 51978   ** least compile and run.
       
 51979   */
       
 51980 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
       
 51981 
       
 51982 #endif
       
 51983 
       
 51984 #endif /* !defined(_HWTIME_H_) */
       
 51985 
       
 51986 /************** End of hwtime.h **********************************************/
       
 51987 /************** Continuing where we left off in vdbe.c ***********************/
       
 51988 
       
 51989 #endif
       
 51990 
       
 51991 /*
       
 51992 ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
       
 51993 ** sqlite3_interrupt() routine has been called.  If it has been, then
       
 51994 ** processing of the VDBE program is interrupted.
       
 51995 **
       
 51996 ** This macro added to every instruction that does a jump in order to
       
 51997 ** implement a loop.  This test used to be on every single instruction,
       
 51998 ** but that meant we more testing that we needed.  By only testing the
       
 51999 ** flag on jump instructions, we get a (small) speed improvement.
       
 52000 */
       
 52001 #define CHECK_FOR_INTERRUPT \
       
 52002    if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
       
 52003 
       
 52004 #ifdef SQLITE_DEBUG
       
 52005 static int fileExists(sqlite3 *db, const char *zFile){
       
 52006   int res = 0;
       
 52007   int rc = SQLITE_OK;
       
 52008 #ifdef SQLITE_TEST
       
 52009   /* If we are currently testing IO errors, then do not call OsAccess() to
       
 52010   ** test for the presence of zFile. This is because any IO error that
       
 52011   ** occurs here will not be reported, causing the test to fail.
       
 52012   */
       
 52013   extern int sqlite3_io_error_pending;
       
 52014   if( sqlite3_io_error_pending<=0 )
       
 52015 #endif
       
 52016     rc = sqlite3OsAccess(db->pVfs, zFile, SQLITE_ACCESS_EXISTS, &res);
       
 52017   return (res && rc==SQLITE_OK);
       
 52018 }
       
 52019 #endif
       
 52020 
       
 52021 #ifndef NDEBUG
       
 52022 /*
       
 52023 ** This function is only called from within an assert() expression. It
       
 52024 ** checks that the sqlite3.nTransaction variable is correctly set to
       
 52025 ** the number of non-transaction savepoints currently in the 
       
 52026 ** linked list starting at sqlite3.pSavepoint.
       
 52027 ** 
       
 52028 ** Usage:
       
 52029 **
       
 52030 **     assert( checkSavepointCount(db) );
       
 52031 */
       
 52032 static int checkSavepointCount(sqlite3 *db){
       
 52033   int n = 0;
       
 52034   Savepoint *p;
       
 52035   for(p=db->pSavepoint; p; p=p->pNext) n++;
       
 52036   assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
       
 52037   return 1;
       
 52038 }
       
 52039 #endif
       
 52040 
       
 52041 /*
       
 52042 ** Execute as much of a VDBE program as we can then return.
       
 52043 **
       
 52044 ** sqlite3VdbeMakeReady() must be called before this routine in order to
       
 52045 ** close the program with a final OP_Halt and to set up the callbacks
       
 52046 ** and the error message pointer.
       
 52047 **
       
 52048 ** Whenever a row or result data is available, this routine will either
       
 52049 ** invoke the result callback (if there is one) or return with
       
 52050 ** SQLITE_ROW.
       
 52051 **
       
 52052 ** If an attempt is made to open a locked database, then this routine
       
 52053 ** will either invoke the busy callback (if there is one) or it will
       
 52054 ** return SQLITE_BUSY.
       
 52055 **
       
 52056 ** If an error occurs, an error message is written to memory obtained
       
 52057 ** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
       
 52058 ** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
       
 52059 **
       
 52060 ** If the callback ever returns non-zero, then the program exits
       
 52061 ** immediately.  There will be no error message but the p->rc field is
       
 52062 ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
       
 52063 **
       
 52064 ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
       
 52065 ** routine to return SQLITE_ERROR.
       
 52066 **
       
 52067 ** Other fatal errors return SQLITE_ERROR.
       
 52068 **
       
 52069 ** After this routine has finished, sqlite3VdbeFinalize() should be
       
 52070 ** used to clean up the mess that was left behind.
       
 52071 */
       
 52072 SQLITE_PRIVATE int sqlite3VdbeExec(
       
 52073   Vdbe *p                    /* The VDBE */
       
 52074 ){
       
 52075   int pc;                    /* The program counter */
       
 52076   Op *pOp;                   /* Current operation */
       
 52077   int rc = SQLITE_OK;        /* Value to return */
       
 52078   sqlite3 *db = p->db;       /* The database */
       
 52079   u8 encoding = ENC(db);     /* The database encoding */
       
 52080   Mem *pIn1 = 0;             /* 1st input operand */
       
 52081   Mem *pIn2 = 0;             /* 2nd input operand */
       
 52082   Mem *pIn3 = 0;             /* 3rd input operand */
       
 52083   Mem *pOut = 0;             /* Output operand */
       
 52084   u8 opProperty;
       
 52085   int iCompare = 0;          /* Result of last OP_Compare operation */
       
 52086   int *aPermute = 0;         /* Permutation of columns for OP_Compare */
       
 52087 #ifdef VDBE_PROFILE
       
 52088   u64 start;                 /* CPU clock count at start of opcode */
       
 52089   int origPc;                /* Program counter at start of opcode */
       
 52090 #endif
       
 52091 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
       
 52092   int nProgressOps = 0;      /* Opcodes executed since progress callback. */
       
 52093 #endif
       
 52094   /********************************************************************
       
 52095   ** Automatically generated code
       
 52096   **
       
 52097   ** The following union is automatically generated by the
       
 52098   ** vdbe-compress.tcl script.  The purpose of this union is to
       
 52099   ** reduce the amount of stack space required by this function.
       
 52100   ** See comments in the vdbe-compress.tcl script for details.
       
 52101   */
       
 52102   union vdbeExecUnion {
       
 52103     struct OP_Yield_stack_vars {
       
 52104       int pcDest;
       
 52105     } aa;
       
 52106     struct OP_Variable_stack_vars {
       
 52107       int p1;          /* Variable to copy from */
       
 52108       int p2;          /* Register to copy to */
       
 52109       int n;           /* Number of values left to copy */
       
 52110       Mem *pVar;       /* Value being transferred */
       
 52111     } ab;
       
 52112     struct OP_Move_stack_vars {
       
 52113       char *zMalloc;   /* Holding variable for allocated memory */
       
 52114       int n;           /* Number of registers left to copy */
       
 52115       int p1;          /* Register to copy from */
       
 52116       int p2;          /* Register to copy to */
       
 52117     } ac;
       
 52118     struct OP_ResultRow_stack_vars {
       
 52119       Mem *pMem;
       
 52120       int i;
       
 52121     } ad;
       
 52122     struct OP_Concat_stack_vars {
       
 52123       i64 nByte;
       
 52124     } ae;
       
 52125     struct OP_Remainder_stack_vars {
       
 52126       int flags;      /* Combined MEM_* flags from both inputs */
       
 52127       i64 iA;         /* Integer value of left operand */
       
 52128       i64 iB;         /* Integer value of right operand */
       
 52129       double rA;      /* Real value of left operand */
       
 52130       double rB;      /* Real value of right operand */
       
 52131     } af;
       
 52132     struct OP_Function_stack_vars {
       
 52133       int i;
       
 52134       Mem *pArg;
       
 52135       sqlite3_context ctx;
       
 52136       sqlite3_value **apVal;
       
 52137       int n;
       
 52138     } ag;
       
 52139     struct OP_ShiftRight_stack_vars {
       
 52140       i64 a;
       
 52141       i64 b;
       
 52142     } ah;
       
 52143     struct OP_Ge_stack_vars {
       
 52144       int res;            /* Result of the comparison of pIn1 against pIn3 */
       
 52145       char affinity;      /* Affinity to use for comparison */
       
 52146     } ai;
       
 52147     struct OP_Compare_stack_vars {
       
 52148       int n;
       
 52149       int i;
       
 52150       int p1;
       
 52151       int p2;
       
 52152       const KeyInfo *pKeyInfo;
       
 52153       int idx;
       
 52154       CollSeq *pColl;    /* Collating sequence to use on this term */
       
 52155       int bRev;          /* True for DESCENDING sort order */
       
 52156     } aj;
       
 52157     struct OP_Or_stack_vars {
       
 52158       int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
       
 52159       int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
       
 52160     } ak;
       
 52161     struct OP_IfNot_stack_vars {
       
 52162       int c;
       
 52163     } al;
       
 52164     struct OP_Column_stack_vars {
       
 52165       u32 payloadSize;   /* Number of bytes in the record */
       
 52166       i64 payloadSize64; /* Number of bytes in the record */
       
 52167       int p1;            /* P1 value of the opcode */
       
 52168       int p2;            /* column number to retrieve */
       
 52169       VdbeCursor *pC;    /* The VDBE cursor */
       
 52170       char *zRec;        /* Pointer to complete record-data */
       
 52171       BtCursor *pCrsr;   /* The BTree cursor */
       
 52172       u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
       
 52173       u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
       
 52174       int nField;        /* number of fields in the record */
       
 52175       int len;           /* The length of the serialized data for the column */
       
 52176       int i;             /* Loop counter */
       
 52177       char *zData;       /* Part of the record being decoded */
       
 52178       Mem *pDest;        /* Where to write the extracted value */
       
 52179       Mem sMem;          /* For storing the record being decoded */
       
 52180       u8 *zIdx;          /* Index into header */
       
 52181       u8 *zEndHdr;       /* Pointer to first byte after the header */
       
 52182       u32 offset;        /* Offset into the data */
       
 52183       u64 offset64;      /* 64-bit offset.  64 bits needed to catch overflow */
       
 52184       int szHdr;         /* Size of the header size field at start of record */
       
 52185       int avail;         /* Number of bytes of available data */
       
 52186       Mem *pReg;         /* PseudoTable input register */
       
 52187     } am;
       
 52188     struct OP_Affinity_stack_vars {
       
 52189       char *zAffinity;   /* The affinity to be applied */
       
 52190       Mem *pData0;       /* First register to which to apply affinity */
       
 52191       Mem *pLast;        /* Last register to which to apply affinity */
       
 52192       Mem *pRec;         /* Current register */
       
 52193     } an;
       
 52194     struct OP_MakeRecord_stack_vars {
       
 52195       u8 *zNewRecord;        /* A buffer to hold the data for the new record */
       
 52196       Mem *pRec;             /* The new record */
       
 52197       u64 nData;             /* Number of bytes of data space */
       
 52198       int nHdr;              /* Number of bytes of header space */
       
 52199       i64 nByte;             /* Data space required for this record */
       
 52200       int nZero;             /* Number of zero bytes at the end of the record */
       
 52201       int nVarint;           /* Number of bytes in a varint */
       
 52202       u32 serial_type;       /* Type field */
       
 52203       Mem *pData0;           /* First field to be combined into the record */
       
 52204       Mem *pLast;            /* Last field of the record */
       
 52205       int nField;            /* Number of fields in the record */
       
 52206       char *zAffinity;       /* The affinity string for the record */
       
 52207       int file_format;       /* File format to use for encoding */
       
 52208       int i;                 /* Space used in zNewRecord[] */
       
 52209       int len;               /* Length of a field */
       
 52210     } ao;
       
 52211     struct OP_Count_stack_vars {
       
 52212       i64 nEntry;
       
 52213       BtCursor *pCrsr;
       
 52214     } ap;
       
 52215     struct OP_Savepoint_stack_vars {
       
 52216       int p1;                         /* Value of P1 operand */
       
 52217       char *zName;                    /* Name of savepoint */
       
 52218       int nName;
       
 52219       Savepoint *pNew;
       
 52220       Savepoint *pSavepoint;
       
 52221       Savepoint *pTmp;
       
 52222       int iSavepoint;
       
 52223       int ii;
       
 52224     } aq;
       
 52225     struct OP_AutoCommit_stack_vars {
       
 52226       int desiredAutoCommit;
       
 52227       int iRollback;
       
 52228       int turnOnAC;
       
 52229     } ar;
       
 52230     struct OP_Transaction_stack_vars {
       
 52231       Btree *pBt;
       
 52232     } as;
       
 52233     struct OP_ReadCookie_stack_vars {
       
 52234       int iMeta;
       
 52235       int iDb;
       
 52236       int iCookie;
       
 52237     } at;
       
 52238     struct OP_SetCookie_stack_vars {
       
 52239       Db *pDb;
       
 52240     } au;
       
 52241     struct OP_VerifyCookie_stack_vars {
       
 52242       int iMeta;
       
 52243       Btree *pBt;
       
 52244     } av;
       
 52245     struct OP_OpenWrite_stack_vars {
       
 52246       int nField;
       
 52247       KeyInfo *pKeyInfo;
       
 52248       int p2;
       
 52249       int iDb;
       
 52250       int wrFlag;
       
 52251       Btree *pX;
       
 52252       VdbeCursor *pCur;
       
 52253       Db *pDb;
       
 52254     } aw;
       
 52255     struct OP_OpenEphemeral_stack_vars {
       
 52256       VdbeCursor *pCx;
       
 52257     } ax;
       
 52258     struct OP_OpenPseudo_stack_vars {
       
 52259       VdbeCursor *pCx;
       
 52260     } ay;
       
 52261     struct OP_SeekGt_stack_vars {
       
 52262       int res;
       
 52263       int oc;
       
 52264       VdbeCursor *pC;
       
 52265       UnpackedRecord r;
       
 52266       int nField;
       
 52267       i64 iKey;      /* The rowid we are to seek to */
       
 52268     } az;
       
 52269     struct OP_Seek_stack_vars {
       
 52270       VdbeCursor *pC;
       
 52271     } ba;
       
 52272     struct OP_Found_stack_vars {
       
 52273       int alreadyExists;
       
 52274       VdbeCursor *pC;
       
 52275       int res;
       
 52276       UnpackedRecord *pIdxKey;
       
 52277       char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
       
 52278     } bb;
       
 52279     struct OP_IsUnique_stack_vars {
       
 52280       u16 ii;
       
 52281       VdbeCursor *pCx;
       
 52282       BtCursor *pCrsr;
       
 52283       u16 nField;
       
 52284       Mem *aMem;
       
 52285       UnpackedRecord r;                  /* B-Tree index search key */
       
 52286       i64 R;                             /* Rowid stored in register P3 */
       
 52287     } bc;
       
 52288     struct OP_NotExists_stack_vars {
       
 52289       VdbeCursor *pC;
       
 52290       BtCursor *pCrsr;
       
 52291       int res;
       
 52292       u64 iKey;
       
 52293     } bd;
       
 52294     struct OP_NewRowid_stack_vars {
       
 52295       i64 v;                 /* The new rowid */
       
 52296       VdbeCursor *pC;        /* Cursor of table to get the new rowid */
       
 52297       int res;               /* Result of an sqlite3BtreeLast() */
       
 52298       int cnt;               /* Counter to limit the number of searches */
       
 52299       Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
       
 52300       VdbeFrame *pFrame;     /* Root frame of VDBE */
       
 52301     } be;
       
 52302     struct OP_Insert_stack_vars {
       
 52303       Mem *pData;       /* MEM cell holding data for the record to be inserted */
       
 52304       Mem *pKey;        /* MEM cell holding key  for the record */
       
 52305       i64 iKey;         /* The integer ROWID or key for the record to be inserted */
       
 52306       VdbeCursor *pC;   /* Cursor to table into which insert is written */
       
 52307       int nZero;        /* Number of zero-bytes to append */
       
 52308       int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
       
 52309       const char *zDb;  /* database name - used by the update hook */
       
 52310       const char *zTbl; /* Table name - used by the opdate hook */
       
 52311       int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
       
 52312     } bf;
       
 52313     struct OP_Delete_stack_vars {
       
 52314       i64 iKey;
       
 52315       VdbeCursor *pC;
       
 52316     } bg;
       
 52317     struct OP_RowData_stack_vars {
       
 52318       VdbeCursor *pC;
       
 52319       BtCursor *pCrsr;
       
 52320       u32 n;
       
 52321       i64 n64;
       
 52322     } bh;
       
 52323     struct OP_Rowid_stack_vars {
       
 52324       VdbeCursor *pC;
       
 52325       i64 v;
       
 52326       sqlite3_vtab *pVtab;
       
 52327       const sqlite3_module *pModule;
       
 52328     } bi;
       
 52329     struct OP_NullRow_stack_vars {
       
 52330       VdbeCursor *pC;
       
 52331     } bj;
       
 52332     struct OP_Last_stack_vars {
       
 52333       VdbeCursor *pC;
       
 52334       BtCursor *pCrsr;
       
 52335       int res;
       
 52336     } bk;
       
 52337     struct OP_Rewind_stack_vars {
       
 52338       VdbeCursor *pC;
       
 52339       BtCursor *pCrsr;
       
 52340       int res;
       
 52341     } bl;
       
 52342     struct OP_Next_stack_vars {
       
 52343       VdbeCursor *pC;
       
 52344       BtCursor *pCrsr;
       
 52345       int res;
       
 52346     } bm;
       
 52347     struct OP_IdxInsert_stack_vars {
       
 52348       VdbeCursor *pC;
       
 52349       BtCursor *pCrsr;
       
 52350       int nKey;
       
 52351       const char *zKey;
       
 52352     } bn;
       
 52353     struct OP_IdxDelete_stack_vars {
       
 52354       VdbeCursor *pC;
       
 52355       BtCursor *pCrsr;
       
 52356       int res;
       
 52357       UnpackedRecord r;
       
 52358     } bo;
       
 52359     struct OP_IdxRowid_stack_vars {
       
 52360       BtCursor *pCrsr;
       
 52361       VdbeCursor *pC;
       
 52362       i64 rowid;
       
 52363     } bp;
       
 52364     struct OP_IdxGE_stack_vars {
       
 52365       VdbeCursor *pC;
       
 52366       int res;
       
 52367       UnpackedRecord r;
       
 52368     } bq;
       
 52369     struct OP_Destroy_stack_vars {
       
 52370       int iMoved;
       
 52371       int iCnt;
       
 52372       Vdbe *pVdbe;
       
 52373       int iDb;
       
 52374     } br;
       
 52375     struct OP_Clear_stack_vars {
       
 52376       int nChange;
       
 52377     } bs;
       
 52378     struct OP_CreateTable_stack_vars {
       
 52379       int pgno;
       
 52380       int flags;
       
 52381       Db *pDb;
       
 52382     } bt;
       
 52383     struct OP_ParseSchema_stack_vars {
       
 52384       int iDb;
       
 52385       const char *zMaster;
       
 52386       char *zSql;
       
 52387       InitData initData;
       
 52388     } bu;
       
 52389     struct OP_IntegrityCk_stack_vars {
       
 52390       int nRoot;      /* Number of tables to check.  (Number of root pages.) */
       
 52391       int *aRoot;     /* Array of rootpage numbers for tables to be checked */
       
 52392       int j;          /* Loop counter */
       
 52393       int nErr;       /* Number of errors reported */
       
 52394       char *z;        /* Text of the error report */
       
 52395       Mem *pnErr;     /* Register keeping track of errors remaining */
       
 52396     } bv;
       
 52397     struct OP_RowSetAdd_stack_vars {
       
 52398       Mem *pIdx;
       
 52399       Mem *pVal;
       
 52400     } bw;
       
 52401     struct OP_RowSetRead_stack_vars {
       
 52402       Mem *pIdx;
       
 52403       i64 val;
       
 52404     } bx;
       
 52405     struct OP_RowSetTest_stack_vars {
       
 52406       int iSet;
       
 52407       int exists;
       
 52408     } by;
       
 52409     struct OP_Program_stack_vars {
       
 52410       int nMem;               /* Number of memory registers for sub-program */
       
 52411       int nByte;              /* Bytes of runtime space required for sub-program */
       
 52412       Mem *pRt;               /* Register to allocate runtime space */
       
 52413       Mem *pMem;              /* Used to iterate through memory cells */
       
 52414       Mem *pEnd;              /* Last memory cell in new array */
       
 52415       VdbeFrame *pFrame;      /* New vdbe frame to execute in */
       
 52416       SubProgram *pProgram;   /* Sub-program to execute */
       
 52417       void *t;                /* Token identifying trigger */
       
 52418     } bz;
       
 52419     struct OP_Param_stack_vars {
       
 52420       VdbeFrame *pFrame;
       
 52421       Mem *pIn;
       
 52422     } ca;
       
 52423     struct OP_MemMax_stack_vars {
       
 52424       Mem *pIn1;
       
 52425       VdbeFrame *pFrame;
       
 52426     } cb;
       
 52427     struct OP_AggStep_stack_vars {
       
 52428       int n;
       
 52429       int i;
       
 52430       Mem *pMem;
       
 52431       Mem *pRec;
       
 52432       sqlite3_context ctx;
       
 52433       sqlite3_value **apVal;
       
 52434     } cc;
       
 52435     struct OP_AggFinal_stack_vars {
       
 52436       Mem *pMem;
       
 52437     } cd;
       
 52438     struct OP_IncrVacuum_stack_vars {
       
 52439       Btree *pBt;
       
 52440     } ce;
       
 52441     struct OP_VBegin_stack_vars {
       
 52442       VTable *pVTab;
       
 52443     } cf;
       
 52444     struct OP_VOpen_stack_vars {
       
 52445       VdbeCursor *pCur;
       
 52446       sqlite3_vtab_cursor *pVtabCursor;
       
 52447       sqlite3_vtab *pVtab;
       
 52448       sqlite3_module *pModule;
       
 52449     } cg;
       
 52450     struct OP_VFilter_stack_vars {
       
 52451       int nArg;
       
 52452       int iQuery;
       
 52453       const sqlite3_module *pModule;
       
 52454       Mem *pQuery;
       
 52455       Mem *pArgc;
       
 52456       sqlite3_vtab_cursor *pVtabCursor;
       
 52457       sqlite3_vtab *pVtab;
       
 52458       VdbeCursor *pCur;
       
 52459       int res;
       
 52460       int i;
       
 52461       Mem **apArg;
       
 52462     } ch;
       
 52463     struct OP_VColumn_stack_vars {
       
 52464       sqlite3_vtab *pVtab;
       
 52465       const sqlite3_module *pModule;
       
 52466       Mem *pDest;
       
 52467       sqlite3_context sContext;
       
 52468     } ci;
       
 52469     struct OP_VNext_stack_vars {
       
 52470       sqlite3_vtab *pVtab;
       
 52471       const sqlite3_module *pModule;
       
 52472       int res;
       
 52473       VdbeCursor *pCur;
       
 52474     } cj;
       
 52475     struct OP_VRename_stack_vars {
       
 52476       sqlite3_vtab *pVtab;
       
 52477       Mem *pName;
       
 52478     } ck;
       
 52479     struct OP_VUpdate_stack_vars {
       
 52480       sqlite3_vtab *pVtab;
       
 52481       sqlite3_module *pModule;
       
 52482       int nArg;
       
 52483       int i;
       
 52484       sqlite_int64 rowid;
       
 52485       Mem **apArg;
       
 52486       Mem *pX;
       
 52487     } cl;
       
 52488     struct OP_Pagecount_stack_vars {
       
 52489       int p1;
       
 52490       int nPage;
       
 52491       Pager *pPager;
       
 52492     } cm;
       
 52493     struct OP_Trace_stack_vars {
       
 52494       char *zTrace;
       
 52495     } cn;
       
 52496   } u;
       
 52497   /* End automatically generated code
       
 52498   ********************************************************************/
       
 52499 
       
 52500   assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
       
 52501   assert( db->magic==SQLITE_MAGIC_BUSY );
       
 52502   sqlite3VdbeMutexArrayEnter(p);
       
 52503   if( p->rc==SQLITE_NOMEM ){
       
 52504     /* This happens if a malloc() inside a call to sqlite3_column_text() or
       
 52505     ** sqlite3_column_text16() failed.  */
       
 52506     goto no_mem;
       
 52507   }
       
 52508   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
       
 52509   p->rc = SQLITE_OK;
       
 52510   assert( p->explain==0 );
       
 52511   p->pResultSet = 0;
       
 52512   db->busyHandler.nBusy = 0;
       
 52513   CHECK_FOR_INTERRUPT;
       
 52514   sqlite3VdbeIOTraceSql(p);
       
 52515 #ifdef SQLITE_DEBUG
       
 52516   sqlite3BeginBenignMalloc();
       
 52517   if( p->pc==0 
       
 52518    && ((p->db->flags & SQLITE_VdbeListing) || fileExists(db, "vdbe_explain"))
       
 52519   ){
       
 52520     int i;
       
 52521     printf("VDBE Program Listing:\n");
       
 52522     sqlite3VdbePrintSql(p);
       
 52523     for(i=0; i<p->nOp; i++){
       
 52524       sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
       
 52525     }
       
 52526   }
       
 52527   if( fileExists(db, "vdbe_trace") ){
       
 52528     p->trace = stdout;
       
 52529   }
       
 52530   sqlite3EndBenignMalloc();
       
 52531 #endif
       
 52532   for(pc=p->pc; rc==SQLITE_OK; pc++){
       
 52533     assert( pc>=0 && pc<p->nOp );
       
 52534     if( db->mallocFailed ) goto no_mem;
       
 52535 #ifdef VDBE_PROFILE
       
 52536     origPc = pc;
       
 52537     start = sqlite3Hwtime();
       
 52538 #endif
       
 52539     pOp = &p->aOp[pc];
       
 52540 
       
 52541     /* Only allow tracing if SQLITE_DEBUG is defined.
       
 52542     */
       
 52543 #ifdef SQLITE_DEBUG
       
 52544     if( p->trace ){
       
 52545       if( pc==0 ){
       
 52546         printf("VDBE Execution Trace:\n");
       
 52547         sqlite3VdbePrintSql(p);
       
 52548       }
       
 52549       sqlite3VdbePrintOp(p->trace, pc, pOp);
       
 52550     }
       
 52551     if( p->trace==0 && pc==0 ){
       
 52552       sqlite3BeginBenignMalloc();
       
 52553       if( fileExists(db, "vdbe_sqltrace") ){
       
 52554         sqlite3VdbePrintSql(p);
       
 52555       }
       
 52556       sqlite3EndBenignMalloc();
       
 52557     }
       
 52558 #endif
       
 52559       
       
 52560 
       
 52561     /* Check to see if we need to simulate an interrupt.  This only happens
       
 52562     ** if we have a special test build.
       
 52563     */
       
 52564 #ifdef SQLITE_TEST
       
 52565     if( sqlite3_interrupt_count>0 ){
       
 52566       sqlite3_interrupt_count--;
       
 52567       if( sqlite3_interrupt_count==0 ){
       
 52568         sqlite3_interrupt(db);
       
 52569       }
       
 52570     }
       
 52571 #endif
       
 52572 
       
 52573 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
       
 52574     /* Call the progress callback if it is configured and the required number
       
 52575     ** of VDBE ops have been executed (either since this invocation of
       
 52576     ** sqlite3VdbeExec() or since last time the progress callback was called).
       
 52577     ** If the progress callback returns non-zero, exit the virtual machine with
       
 52578     ** a return code SQLITE_ABORT.
       
 52579     */
       
 52580     if( db->xProgress ){
       
 52581       if( db->nProgressOps==nProgressOps ){
       
 52582         int prc;
       
 52583         if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
       
 52584         prc =db->xProgress(db->pProgressArg);
       
 52585         if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
       
 52586         if( prc!=0 ){
       
 52587           rc = SQLITE_INTERRUPT;
       
 52588           goto vdbe_error_halt;
       
 52589         }
       
 52590         nProgressOps = 0;
       
 52591       }
       
 52592       nProgressOps++;
       
 52593     }
       
 52594 #endif
       
 52595 
       
 52596     /* Do common setup processing for any opcode that is marked
       
 52597     ** with the "out2-prerelease" tag.  Such opcodes have a single
       
 52598     ** output which is specified by the P2 parameter.  The P2 register
       
 52599     ** is initialized to a NULL.
       
 52600     */
       
 52601     opProperty = opcodeProperty[pOp->opcode];
       
 52602     if( (opProperty & OPFLG_OUT2_PRERELEASE)!=0 ){
       
 52603       assert( pOp->p2>0 );
       
 52604       assert( pOp->p2<=p->nMem );
       
 52605       pOut = &p->aMem[pOp->p2];
       
 52606       sqlite3VdbeMemReleaseExternal(pOut);
       
 52607       pOut->flags = MEM_Null;
       
 52608       pOut->n = 0;
       
 52609     }else
       
 52610  
       
 52611     /* Do common setup for opcodes marked with one of the following
       
 52612     ** combinations of properties.
       
 52613     **
       
 52614     **           in1
       
 52615     **           in1 in2
       
 52616     **           in1 in2 out3
       
 52617     **           in1 in3
       
 52618     **
       
 52619     ** Variables pIn1, pIn2, and pIn3 are made to point to appropriate
       
 52620     ** registers for inputs.  Variable pOut points to the output register.
       
 52621     */
       
 52622     if( (opProperty & OPFLG_IN1)!=0 ){
       
 52623       assert( pOp->p1>0 );
       
 52624       assert( pOp->p1<=p->nMem );
       
 52625       pIn1 = &p->aMem[pOp->p1];
       
 52626       REGISTER_TRACE(pOp->p1, pIn1);
       
 52627       if( (opProperty & OPFLG_IN2)!=0 ){
       
 52628         assert( pOp->p2>0 );
       
 52629         assert( pOp->p2<=p->nMem );
       
 52630         pIn2 = &p->aMem[pOp->p2];
       
 52631         REGISTER_TRACE(pOp->p2, pIn2);
       
 52632         /* As currently implemented, in2 implies out3.  There is no reason
       
 52633         ** why this has to be, it just worked out that way. */
       
 52634         assert( (opProperty & OPFLG_OUT3)!=0 );
       
 52635         assert( pOp->p3>0 );
       
 52636         assert( pOp->p3<=p->nMem );
       
 52637         pOut = &p->aMem[pOp->p3];
       
 52638       }else if( (opProperty & OPFLG_IN3)!=0 ){
       
 52639         assert( pOp->p3>0 );
       
 52640         assert( pOp->p3<=p->nMem );
       
 52641         pIn3 = &p->aMem[pOp->p3];
       
 52642         REGISTER_TRACE(pOp->p3, pIn3);
       
 52643       }
       
 52644     }else if( (opProperty & OPFLG_IN2)!=0 ){
       
 52645       assert( pOp->p2>0 );
       
 52646       assert( pOp->p2<=p->nMem );
       
 52647       pIn2 = &p->aMem[pOp->p2];
       
 52648       REGISTER_TRACE(pOp->p2, pIn2);
       
 52649     }else if( (opProperty & OPFLG_IN3)!=0 ){
       
 52650       assert( pOp->p3>0 );
       
 52651       assert( pOp->p3<=p->nMem );
       
 52652       pIn3 = &p->aMem[pOp->p3];
       
 52653       REGISTER_TRACE(pOp->p3, pIn3);
       
 52654     }
       
 52655 
       
 52656     switch( pOp->opcode ){
       
 52657 
       
 52658 /*****************************************************************************
       
 52659 ** What follows is a massive switch statement where each case implements a
       
 52660 ** separate instruction in the virtual machine.  If we follow the usual
       
 52661 ** indentation conventions, each case should be indented by 6 spaces.  But
       
 52662 ** that is a lot of wasted space on the left margin.  So the code within
       
 52663 ** the switch statement will break with convention and be flush-left. Another
       
 52664 ** big comment (similar to this one) will mark the point in the code where
       
 52665 ** we transition back to normal indentation.
       
 52666 **
       
 52667 ** The formatting of each case is important.  The makefile for SQLite
       
 52668 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
       
 52669 ** file looking for lines that begin with "case OP_".  The opcodes.h files
       
 52670 ** will be filled with #defines that give unique integer values to each
       
 52671 ** opcode and the opcodes.c file is filled with an array of strings where
       
 52672 ** each string is the symbolic name for the corresponding opcode.  If the
       
 52673 ** case statement is followed by a comment of the form "/# same as ... #/"
       
 52674 ** that comment is used to determine the particular value of the opcode.
       
 52675 **
       
 52676 ** Other keywords in the comment that follows each case are used to
       
 52677 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
       
 52678 ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3.  See
       
 52679 ** the mkopcodeh.awk script for additional information.
       
 52680 **
       
 52681 ** Documentation about VDBE opcodes is generated by scanning this file
       
 52682 ** for lines of that contain "Opcode:".  That line and all subsequent
       
 52683 ** comment lines are used in the generation of the opcode.html documentation
       
 52684 ** file.
       
 52685 **
       
 52686 ** SUMMARY:
       
 52687 **
       
 52688 **     Formatting is important to scripts that scan this file.
       
 52689 **     Do not deviate from the formatting style currently in use.
       
 52690 **
       
 52691 *****************************************************************************/
       
 52692 
       
 52693 /* Opcode:  Goto * P2 * * *
       
 52694 **
       
 52695 ** An unconditional jump to address P2.
       
 52696 ** The next instruction executed will be 
       
 52697 ** the one at index P2 from the beginning of
       
 52698 ** the program.
       
 52699 */
       
 52700 case OP_Goto: {             /* jump */
       
 52701   CHECK_FOR_INTERRUPT;
       
 52702   pc = pOp->p2 - 1;
       
 52703   break;
       
 52704 }
       
 52705 
       
 52706 /* Opcode:  Gosub P1 P2 * * *
       
 52707 **
       
 52708 ** Write the current address onto register P1
       
 52709 ** and then jump to address P2.
       
 52710 */
       
 52711 case OP_Gosub: {            /* jump */
       
 52712   assert( pOp->p1>0 );
       
 52713   assert( pOp->p1<=p->nMem );
       
 52714   pIn1 = &p->aMem[pOp->p1];
       
 52715   assert( (pIn1->flags & MEM_Dyn)==0 );
       
 52716   pIn1->flags = MEM_Int;
       
 52717   pIn1->u.i = pc;
       
 52718   REGISTER_TRACE(pOp->p1, pIn1);
       
 52719   pc = pOp->p2 - 1;
       
 52720   break;
       
 52721 }
       
 52722 
       
 52723 /* Opcode:  Return P1 * * * *
       
 52724 **
       
 52725 ** Jump to the next instruction after the address in register P1.
       
 52726 */
       
 52727 case OP_Return: {           /* in1 */
       
 52728   assert( pIn1->flags & MEM_Int );
       
 52729   pc = (int)pIn1->u.i;
       
 52730   break;
       
 52731 }
       
 52732 
       
 52733 /* Opcode:  Yield P1 * * * *
       
 52734 **
       
 52735 ** Swap the program counter with the value in register P1.
       
 52736 */
       
 52737 case OP_Yield: {            /* in1 */
       
 52738 #if 0  /* local variables moved into u.aa */
       
 52739   int pcDest;
       
 52740 #endif /* local variables moved into u.aa */
       
 52741   assert( (pIn1->flags & MEM_Dyn)==0 );
       
 52742   pIn1->flags = MEM_Int;
       
 52743   u.aa.pcDest = (int)pIn1->u.i;
       
 52744   pIn1->u.i = pc;
       
 52745   REGISTER_TRACE(pOp->p1, pIn1);
       
 52746   pc = u.aa.pcDest;
       
 52747   break;
       
 52748 }
       
 52749 
       
 52750 /* Opcode:  HaltIfNull  P1 P2 P3 P4 *
       
 52751 **
       
 52752 ** Check the value in register P3.  If is is NULL then Halt using
       
 52753 ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
       
 52754 ** value in register P3 is not NULL, then this routine is a no-op.
       
 52755 */
       
 52756 case OP_HaltIfNull: {      /* in3 */
       
 52757   if( (pIn3->flags & MEM_Null)==0 ) break;
       
 52758   /* Fall through into OP_Halt */
       
 52759 }
       
 52760 
       
 52761 /* Opcode:  Halt P1 P2 * P4 *
       
 52762 **
       
 52763 ** Exit immediately.  All open cursors, etc are closed
       
 52764 ** automatically.
       
 52765 **
       
 52766 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
       
 52767 ** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
       
 52768 ** For errors, it can be some other value.  If P1!=0 then P2 will determine
       
 52769 ** whether or not to rollback the current transaction.  Do not rollback
       
 52770 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
       
 52771 ** then back out all changes that have occurred during this execution of the
       
 52772 ** VDBE, but do not rollback the transaction. 
       
 52773 **
       
 52774 ** If P4 is not null then it is an error message string.
       
 52775 **
       
 52776 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
       
 52777 ** every program.  So a jump past the last instruction of the program
       
 52778 ** is the same as executing Halt.
       
 52779 */
       
 52780 case OP_Halt: {
       
 52781   if( pOp->p1==SQLITE_OK && p->pFrame ){
       
 52782     /* Halt the sub-program. Return control to the parent frame. */
       
 52783     VdbeFrame *pFrame = p->pFrame;
       
 52784     p->pFrame = pFrame->pParent;
       
 52785     p->nFrame--;
       
 52786     sqlite3VdbeSetChanges(db, p->nChange);
       
 52787     pc = sqlite3VdbeFrameRestore(pFrame);
       
 52788     if( pOp->p2==OE_Ignore ){
       
 52789       /* Instruction pc is the OP_Program that invoked the sub-program 
       
 52790       ** currently being halted. If the p2 instruction of this OP_Halt
       
 52791       ** instruction is set to OE_Ignore, then the sub-program is throwing
       
 52792       ** an IGNORE exception. In this case jump to the address specified
       
 52793       ** as the p2 of the calling OP_Program.  */
       
 52794       pc = p->aOp[pc].p2-1;
       
 52795     }
       
 52796     break;
       
 52797   }
       
 52798 
       
 52799   p->rc = pOp->p1;
       
 52800   p->errorAction = (u8)pOp->p2;
       
 52801   p->pc = pc;
       
 52802   if( pOp->p4.z ){
       
 52803     sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
       
 52804   }
       
 52805   rc = sqlite3VdbeHalt(p);
       
 52806   assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
       
 52807   if( rc==SQLITE_BUSY ){
       
 52808     p->rc = rc = SQLITE_BUSY;
       
 52809   }else{
       
 52810     assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
       
 52811     assert( rc==SQLITE_OK || db->nDeferredCons>0 );
       
 52812     rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
       
 52813   }
       
 52814   goto vdbe_return;
       
 52815 }
       
 52816 
       
 52817 /* Opcode: Integer P1 P2 * * *
       
 52818 **
       
 52819 ** The 32-bit integer value P1 is written into register P2.
       
 52820 */
       
 52821 case OP_Integer: {         /* out2-prerelease */
       
 52822   pOut->flags = MEM_Int;
       
 52823   pOut->u.i = pOp->p1;
       
 52824   break;
       
 52825 }
       
 52826 
       
 52827 /* Opcode: Int64 * P2 * P4 *
       
 52828 **
       
 52829 ** P4 is a pointer to a 64-bit integer value.
       
 52830 ** Write that value into register P2.
       
 52831 */
       
 52832 case OP_Int64: {           /* out2-prerelease */
       
 52833   assert( pOp->p4.pI64!=0 );
       
 52834   pOut->flags = MEM_Int;
       
 52835   pOut->u.i = *pOp->p4.pI64;
       
 52836   break;
       
 52837 }
       
 52838 
       
 52839 /* Opcode: Real * P2 * P4 *
       
 52840 **
       
 52841 ** P4 is a pointer to a 64-bit floating point value.
       
 52842 ** Write that value into register P2.
       
 52843 */
       
 52844 case OP_Real: {            /* same as TK_FLOAT, out2-prerelease */
       
 52845   pOut->flags = MEM_Real;
       
 52846   assert( !sqlite3IsNaN(*pOp->p4.pReal) );
       
 52847   pOut->r = *pOp->p4.pReal;
       
 52848   break;
       
 52849 }
       
 52850 
       
 52851 /* Opcode: String8 * P2 * P4 *
       
 52852 **
       
 52853 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 
       
 52854 ** into an OP_String before it is executed for the first time.
       
 52855 */
       
 52856 case OP_String8: {         /* same as TK_STRING, out2-prerelease */
       
 52857   assert( pOp->p4.z!=0 );
       
 52858   pOp->opcode = OP_String;
       
 52859   pOp->p1 = sqlite3Strlen30(pOp->p4.z);
       
 52860 
       
 52861 #ifndef SQLITE_OMIT_UTF16
       
 52862   if( encoding!=SQLITE_UTF8 ){
       
 52863     rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
       
 52864     if( rc==SQLITE_TOOBIG ) goto too_big;
       
 52865     if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
       
 52866     assert( pOut->zMalloc==pOut->z );
       
 52867     assert( pOut->flags & MEM_Dyn );
       
 52868     pOut->zMalloc = 0;
       
 52869     pOut->flags |= MEM_Static;
       
 52870     pOut->flags &= ~MEM_Dyn;
       
 52871     if( pOp->p4type==P4_DYNAMIC ){
       
 52872       sqlite3DbFree(db, pOp->p4.z);
       
 52873     }
       
 52874     pOp->p4type = P4_DYNAMIC;
       
 52875     pOp->p4.z = pOut->z;
       
 52876     pOp->p1 = pOut->n;
       
 52877   }
       
 52878 #endif
       
 52879   if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
       
 52880     goto too_big;
       
 52881   }
       
 52882   /* Fall through to the next case, OP_String */
       
 52883 }
       
 52884   
       
 52885 /* Opcode: String P1 P2 * P4 *
       
 52886 **
       
 52887 ** The string value P4 of length P1 (bytes) is stored in register P2.
       
 52888 */
       
 52889 case OP_String: {          /* out2-prerelease */
       
 52890   assert( pOp->p4.z!=0 );
       
 52891   pOut->flags = MEM_Str|MEM_Static|MEM_Term;
       
 52892   pOut->z = pOp->p4.z;
       
 52893   pOut->n = pOp->p1;
       
 52894   pOut->enc = encoding;
       
 52895   UPDATE_MAX_BLOBSIZE(pOut);
       
 52896   break;
       
 52897 }
       
 52898 
       
 52899 /* Opcode: Null * P2 * * *
       
 52900 **
       
 52901 ** Write a NULL into register P2.
       
 52902 */
       
 52903 case OP_Null: {           /* out2-prerelease */
       
 52904   break;
       
 52905 }
       
 52906 
       
 52907 
       
 52908 /* Opcode: Blob P1 P2 * P4
       
 52909 **
       
 52910 ** P4 points to a blob of data P1 bytes long.  Store this
       
 52911 ** blob in register P2. This instruction is not coded directly
       
 52912 ** by the compiler. Instead, the compiler layer specifies
       
 52913 ** an OP_HexBlob opcode, with the hex string representation of
       
 52914 ** the blob as P4. This opcode is transformed to an OP_Blob
       
 52915 ** the first time it is executed.
       
 52916 */
       
 52917 case OP_Blob: {                /* out2-prerelease */
       
 52918   assert( pOp->p1 <= SQLITE_MAX_LENGTH );
       
 52919   sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
       
 52920   pOut->enc = encoding;
       
 52921   UPDATE_MAX_BLOBSIZE(pOut);
       
 52922   break;
       
 52923 }
       
 52924 
       
 52925 /* Opcode: Variable P1 P2 P3 P4 *
       
 52926 **
       
 52927 ** Transfer the values of bound parameters P1..P1+P3-1 into registers
       
 52928 ** P2..P2+P3-1.
       
 52929 **
       
 52930 ** If the parameter is named, then its name appears in P4 and P3==1.
       
 52931 ** The P4 value is used by sqlite3_bind_parameter_name().
       
 52932 */
       
 52933 case OP_Variable: {
       
 52934 #if 0  /* local variables moved into u.ab */
       
 52935   int p1;          /* Variable to copy from */
       
 52936   int p2;          /* Register to copy to */
       
 52937   int n;           /* Number of values left to copy */
       
 52938   Mem *pVar;       /* Value being transferred */
       
 52939 #endif /* local variables moved into u.ab */
       
 52940 
       
 52941   u.ab.p1 = pOp->p1 - 1;
       
 52942   u.ab.p2 = pOp->p2;
       
 52943   u.ab.n = pOp->p3;
       
 52944   assert( u.ab.p1>=0 && u.ab.p1+u.ab.n<=p->nVar );
       
 52945   assert( u.ab.p2>=1 && u.ab.p2+u.ab.n-1<=p->nMem );
       
 52946   assert( pOp->p4.z==0 || pOp->p3==1 );
       
 52947 
       
 52948   while( u.ab.n-- > 0 ){
       
 52949     u.ab.pVar = &p->aVar[u.ab.p1++];
       
 52950     if( sqlite3VdbeMemTooBig(u.ab.pVar) ){
       
 52951       goto too_big;
       
 52952     }
       
 52953     pOut = &p->aMem[u.ab.p2++];
       
 52954     sqlite3VdbeMemReleaseExternal(pOut);
       
 52955     pOut->flags = MEM_Null;
       
 52956     sqlite3VdbeMemShallowCopy(pOut, u.ab.pVar, MEM_Static);
       
 52957     UPDATE_MAX_BLOBSIZE(pOut);
       
 52958   }
       
 52959   break;
       
 52960 }
       
 52961 
       
 52962 /* Opcode: Move P1 P2 P3 * *
       
 52963 **
       
 52964 ** Move the values in register P1..P1+P3-1 over into
       
 52965 ** registers P2..P2+P3-1.  Registers P1..P1+P1-1 are
       
 52966 ** left holding a NULL.  It is an error for register ranges
       
 52967 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
       
 52968 */
       
 52969 case OP_Move: {
       
 52970 #if 0  /* local variables moved into u.ac */
       
 52971   char *zMalloc;   /* Holding variable for allocated memory */
       
 52972   int n;           /* Number of registers left to copy */
       
 52973   int p1;          /* Register to copy from */
       
 52974   int p2;          /* Register to copy to */
       
 52975 #endif /* local variables moved into u.ac */
       
 52976 
       
 52977   u.ac.n = pOp->p3;
       
 52978   u.ac.p1 = pOp->p1;
       
 52979   u.ac.p2 = pOp->p2;
       
 52980   assert( u.ac.n>0 && u.ac.p1>0 && u.ac.p2>0 );
       
 52981   assert( u.ac.p1+u.ac.n<=u.ac.p2 || u.ac.p2+u.ac.n<=u.ac.p1 );
       
 52982 
       
 52983   pIn1 = &p->aMem[u.ac.p1];
       
 52984   pOut = &p->aMem[u.ac.p2];
       
 52985   while( u.ac.n-- ){
       
 52986     assert( pOut<=&p->aMem[p->nMem] );
       
 52987     assert( pIn1<=&p->aMem[p->nMem] );
       
 52988     u.ac.zMalloc = pOut->zMalloc;
       
 52989     pOut->zMalloc = 0;
       
 52990     sqlite3VdbeMemMove(pOut, pIn1);
       
 52991     pIn1->zMalloc = u.ac.zMalloc;
       
 52992     REGISTER_TRACE(u.ac.p2++, pOut);
       
 52993     pIn1++;
       
 52994     pOut++;
       
 52995   }
       
 52996   break;
       
 52997 }
       
 52998 
       
 52999 /* Opcode: Copy P1 P2 * * *
       
 53000 **
       
 53001 ** Make a copy of register P1 into register P2.
       
 53002 **
       
 53003 ** This instruction makes a deep copy of the value.  A duplicate
       
 53004 ** is made of any string or blob constant.  See also OP_SCopy.
       
 53005 */
       
 53006 case OP_Copy: {             /* in1 */
       
 53007   assert( pOp->p2>0 );
       
 53008   assert( pOp->p2<=p->nMem );
       
 53009   pOut = &p->aMem[pOp->p2];
       
 53010   assert( pOut!=pIn1 );
       
 53011   sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
       
 53012   Deephemeralize(pOut);
       
 53013   REGISTER_TRACE(pOp->p2, pOut);
       
 53014   break;
       
 53015 }
       
 53016 
       
 53017 /* Opcode: SCopy P1 P2 * * *
       
 53018 **
       
 53019 ** Make a shallow copy of register P1 into register P2.
       
 53020 **
       
 53021 ** This instruction makes a shallow copy of the value.  If the value
       
 53022 ** is a string or blob, then the copy is only a pointer to the
       
 53023 ** original and hence if the original changes so will the copy.
       
 53024 ** Worse, if the original is deallocated, the copy becomes invalid.
       
 53025 ** Thus the program must guarantee that the original will not change
       
 53026 ** during the lifetime of the copy.  Use OP_Copy to make a complete
       
 53027 ** copy.
       
 53028 */
       
 53029 case OP_SCopy: {            /* in1 */
       
 53030   REGISTER_TRACE(pOp->p1, pIn1);
       
 53031   assert( pOp->p2>0 );
       
 53032   assert( pOp->p2<=p->nMem );
       
 53033   pOut = &p->aMem[pOp->p2];
       
 53034   assert( pOut!=pIn1 );
       
 53035   sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
       
 53036   REGISTER_TRACE(pOp->p2, pOut);
       
 53037   break;
       
 53038 }
       
 53039 
       
 53040 /* Opcode: ResultRow P1 P2 * * *
       
 53041 **
       
 53042 ** The registers P1 through P1+P2-1 contain a single row of
       
 53043 ** results. This opcode causes the sqlite3_step() call to terminate
       
 53044 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
       
 53045 ** structure to provide access to the top P1 values as the result
       
 53046 ** row.
       
 53047 */
       
 53048 case OP_ResultRow: {
       
 53049 #if 0  /* local variables moved into u.ad */
       
 53050   Mem *pMem;
       
 53051   int i;
       
 53052 #endif /* local variables moved into u.ad */
       
 53053   assert( p->nResColumn==pOp->p2 );
       
 53054   assert( pOp->p1>0 );
       
 53055   assert( pOp->p1+pOp->p2<=p->nMem+1 );
       
 53056 
       
 53057   /* If this statement has violated immediate foreign key constraints, do
       
 53058   ** not return the number of rows modified. And do not RELEASE the statement
       
 53059   ** transaction. It needs to be rolled back.  */
       
 53060   if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
       
 53061     assert( db->flags&SQLITE_CountRows );
       
 53062     assert( p->usesStmtJournal );
       
 53063     break;
       
 53064   }
       
 53065 
       
 53066   /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
       
 53067   ** DML statements invoke this opcode to return the number of rows
       
 53068   ** modified to the user. This is the only way that a VM that
       
 53069   ** opens a statement transaction may invoke this opcode.
       
 53070   **
       
 53071   ** In case this is such a statement, close any statement transaction
       
 53072   ** opened by this VM before returning control to the user. This is to
       
 53073   ** ensure that statement-transactions are always nested, not overlapping.
       
 53074   ** If the open statement-transaction is not closed here, then the user
       
 53075   ** may step another VM that opens its own statement transaction. This
       
 53076   ** may lead to overlapping statement transactions.
       
 53077   **
       
 53078   ** The statement transaction is never a top-level transaction.  Hence
       
 53079   ** the RELEASE call below can never fail.
       
 53080   */
       
 53081   assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
       
 53082   rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
       
 53083   if( NEVER(rc!=SQLITE_OK) ){
       
 53084     break;
       
 53085   }
       
 53086 
       
 53087   /* Invalidate all ephemeral cursor row caches */
       
 53088   p->cacheCtr = (p->cacheCtr + 2)|1;
       
 53089 
       
 53090   /* Make sure the results of the current row are \000 terminated
       
 53091   ** and have an assigned type.  The results are de-ephemeralized as
       
 53092   ** as side effect.
       
 53093   */
       
 53094   u.ad.pMem = p->pResultSet = &p->aMem[pOp->p1];
       
 53095   for(u.ad.i=0; u.ad.i<pOp->p2; u.ad.i++){
       
 53096     sqlite3VdbeMemNulTerminate(&u.ad.pMem[u.ad.i]);
       
 53097     storeTypeInfo(&u.ad.pMem[u.ad.i], encoding);
       
 53098     REGISTER_TRACE(pOp->p1+u.ad.i, &u.ad.pMem[u.ad.i]);
       
 53099   }
       
 53100   if( db->mallocFailed ) goto no_mem;
       
 53101 
       
 53102   /* Return SQLITE_ROW
       
 53103   */
       
 53104   p->pc = pc + 1;
       
 53105   rc = SQLITE_ROW;
       
 53106   goto vdbe_return;
       
 53107 }
       
 53108 
       
 53109 /* Opcode: Concat P1 P2 P3 * *
       
 53110 **
       
 53111 ** Add the text in register P1 onto the end of the text in
       
 53112 ** register P2 and store the result in register P3.
       
 53113 ** If either the P1 or P2 text are NULL then store NULL in P3.
       
 53114 **
       
 53115 **   P3 = P2 || P1
       
 53116 **
       
 53117 ** It is illegal for P1 and P3 to be the same register. Sometimes,
       
 53118 ** if P3 is the same register as P2, the implementation is able
       
 53119 ** to avoid a memcpy().
       
 53120 */
       
 53121 case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
       
 53122 #if 0  /* local variables moved into u.ae */
       
 53123   i64 nByte;
       
 53124 #endif /* local variables moved into u.ae */
       
 53125 
       
 53126   assert( pIn1!=pOut );
       
 53127   if( (pIn1->flags | pIn2->flags) & MEM_Null ){
       
 53128     sqlite3VdbeMemSetNull(pOut);
       
 53129     break;
       
 53130   }
       
 53131   if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
       
 53132   Stringify(pIn1, encoding);
       
 53133   Stringify(pIn2, encoding);
       
 53134   u.ae.nByte = pIn1->n + pIn2->n;
       
 53135   if( u.ae.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
       
 53136     goto too_big;
       
 53137   }
       
 53138   MemSetTypeFlag(pOut, MEM_Str);
       
 53139   if( sqlite3VdbeMemGrow(pOut, (int)u.ae.nByte+2, pOut==pIn2) ){
       
 53140     goto no_mem;
       
 53141   }
       
 53142   if( pOut!=pIn2 ){
       
 53143     memcpy(pOut->z, pIn2->z, pIn2->n);
       
 53144   }
       
 53145   memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
       
 53146   pOut->z[u.ae.nByte] = 0;
       
 53147   pOut->z[u.ae.nByte+1] = 0;
       
 53148   pOut->flags |= MEM_Term;
       
 53149   pOut->n = (int)u.ae.nByte;
       
 53150   pOut->enc = encoding;
       
 53151   UPDATE_MAX_BLOBSIZE(pOut);
       
 53152   break;
       
 53153 }
       
 53154 
       
 53155 /* Opcode: Add P1 P2 P3 * *
       
 53156 **
       
 53157 ** Add the value in register P1 to the value in register P2
       
 53158 ** and store the result in register P3.
       
 53159 ** If either input is NULL, the result is NULL.
       
 53160 */
       
 53161 /* Opcode: Multiply P1 P2 P3 * *
       
 53162 **
       
 53163 **
       
 53164 ** Multiply the value in register P1 by the value in register P2
       
 53165 ** and store the result in register P3.
       
 53166 ** If either input is NULL, the result is NULL.
       
 53167 */
       
 53168 /* Opcode: Subtract P1 P2 P3 * *
       
 53169 **
       
 53170 ** Subtract the value in register P1 from the value in register P2
       
 53171 ** and store the result in register P3.
       
 53172 ** If either input is NULL, the result is NULL.
       
 53173 */
       
 53174 /* Opcode: Divide P1 P2 P3 * *
       
 53175 **
       
 53176 ** Divide the value in register P1 by the value in register P2
       
 53177 ** and store the result in register P3 (P3=P2/P1). If the value in 
       
 53178 ** register P1 is zero, then the result is NULL. If either input is 
       
 53179 ** NULL, the result is NULL.
       
 53180 */
       
 53181 /* Opcode: Remainder P1 P2 P3 * *
       
 53182 **
       
 53183 ** Compute the remainder after integer division of the value in
       
 53184 ** register P1 by the value in register P2 and store the result in P3. 
       
 53185 ** If the value in register P2 is zero the result is NULL.
       
 53186 ** If either operand is NULL, the result is NULL.
       
 53187 */
       
 53188 case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
       
 53189 case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
       
 53190 case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
       
 53191 case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
       
 53192 case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
       
 53193 #if 0  /* local variables moved into u.af */
       
 53194   int flags;      /* Combined MEM_* flags from both inputs */
       
 53195   i64 iA;         /* Integer value of left operand */
       
 53196   i64 iB;         /* Integer value of right operand */
       
 53197   double rA;      /* Real value of left operand */
       
 53198   double rB;      /* Real value of right operand */
       
 53199 #endif /* local variables moved into u.af */
       
 53200 
       
 53201   applyNumericAffinity(pIn1);
       
 53202   applyNumericAffinity(pIn2);
       
 53203   u.af.flags = pIn1->flags | pIn2->flags;
       
 53204   if( (u.af.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
       
 53205   if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
       
 53206     u.af.iA = pIn1->u.i;
       
 53207     u.af.iB = pIn2->u.i;
       
 53208     switch( pOp->opcode ){
       
 53209       case OP_Add:         u.af.iB += u.af.iA;       break;
       
 53210       case OP_Subtract:    u.af.iB -= u.af.iA;       break;
       
 53211       case OP_Multiply:    u.af.iB *= u.af.iA;       break;
       
 53212       case OP_Divide: {
       
 53213         if( u.af.iA==0 ) goto arithmetic_result_is_null;
       
 53214         /* Dividing the largest possible negative 64-bit integer (1<<63) by
       
 53215         ** -1 returns an integer too large to store in a 64-bit data-type. On
       
 53216         ** some architectures, the value overflows to (1<<63). On others,
       
 53217         ** a SIGFPE is issued. The following statement normalizes this
       
 53218         ** behavior so that all architectures behave as if integer
       
 53219         ** overflow occurred.
       
 53220         */
       
 53221         if( u.af.iA==-1 && u.af.iB==SMALLEST_INT64 ) u.af.iA = 1;
       
 53222         u.af.iB /= u.af.iA;
       
 53223         break;
       
 53224       }
       
 53225       default: {
       
 53226         if( u.af.iA==0 ) goto arithmetic_result_is_null;
       
 53227         if( u.af.iA==-1 ) u.af.iA = 1;
       
 53228         u.af.iB %= u.af.iA;
       
 53229         break;
       
 53230       }
       
 53231     }
       
 53232     pOut->u.i = u.af.iB;
       
 53233     MemSetTypeFlag(pOut, MEM_Int);
       
 53234   }else{
       
 53235     u.af.rA = sqlite3VdbeRealValue(pIn1);
       
 53236     u.af.rB = sqlite3VdbeRealValue(pIn2);
       
 53237     switch( pOp->opcode ){
       
 53238       case OP_Add:         u.af.rB += u.af.rA;       break;
       
 53239       case OP_Subtract:    u.af.rB -= u.af.rA;       break;
       
 53240       case OP_Multiply:    u.af.rB *= u.af.rA;       break;
       
 53241       case OP_Divide: {
       
 53242         /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
       
 53243         if( u.af.rA==(double)0 ) goto arithmetic_result_is_null;
       
 53244         u.af.rB /= u.af.rA;
       
 53245         break;
       
 53246       }
       
 53247       default: {
       
 53248         u.af.iA = (i64)u.af.rA;
       
 53249         u.af.iB = (i64)u.af.rB;
       
 53250         if( u.af.iA==0 ) goto arithmetic_result_is_null;
       
 53251         if( u.af.iA==-1 ) u.af.iA = 1;
       
 53252         u.af.rB = (double)(u.af.iB % u.af.iA);
       
 53253         break;
       
 53254       }
       
 53255     }
       
 53256     if( sqlite3IsNaN(u.af.rB) ){
       
 53257       goto arithmetic_result_is_null;
       
 53258     }
       
 53259     pOut->r = u.af.rB;
       
 53260     MemSetTypeFlag(pOut, MEM_Real);
       
 53261     if( (u.af.flags & MEM_Real)==0 ){
       
 53262       sqlite3VdbeIntegerAffinity(pOut);
       
 53263     }
       
 53264   }
       
 53265   break;
       
 53266 
       
 53267 arithmetic_result_is_null:
       
 53268   sqlite3VdbeMemSetNull(pOut);
       
 53269   break;
       
 53270 }
       
 53271 
       
 53272 /* Opcode: CollSeq * * P4
       
 53273 **
       
 53274 ** P4 is a pointer to a CollSeq struct. If the next call to a user function
       
 53275 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
       
 53276 ** be returned. This is used by the built-in min(), max() and nullif()
       
 53277 ** functions.
       
 53278 **
       
 53279 ** The interface used by the implementation of the aforementioned functions
       
 53280 ** to retrieve the collation sequence set by this opcode is not available
       
 53281 ** publicly, only to user functions defined in func.c.
       
 53282 */
       
 53283 case OP_CollSeq: {
       
 53284   assert( pOp->p4type==P4_COLLSEQ );
       
 53285   break;
       
 53286 }
       
 53287 
       
 53288 /* Opcode: Function P1 P2 P3 P4 P5
       
 53289 **
       
 53290 ** Invoke a user function (P4 is a pointer to a Function structure that
       
 53291 ** defines the function) with P5 arguments taken from register P2 and
       
 53292 ** successors.  The result of the function is stored in register P3.
       
 53293 ** Register P3 must not be one of the function inputs.
       
 53294 **
       
 53295 ** P1 is a 32-bit bitmask indicating whether or not each argument to the 
       
 53296 ** function was determined to be constant at compile time. If the first
       
 53297 ** argument was constant then bit 0 of P1 is set. This is used to determine
       
 53298 ** whether meta data associated with a user function argument using the
       
 53299 ** sqlite3_set_auxdata() API may be safely retained until the next
       
 53300 ** invocation of this opcode.
       
 53301 **
       
 53302 ** See also: AggStep and AggFinal
       
 53303 */
       
 53304 case OP_Function: {
       
 53305 #if 0  /* local variables moved into u.ag */
       
 53306   int i;
       
 53307   Mem *pArg;
       
 53308   sqlite3_context ctx;
       
 53309   sqlite3_value **apVal;
       
 53310   int n;
       
 53311 #endif /* local variables moved into u.ag */
       
 53312 
       
 53313   u.ag.n = pOp->p5;
       
 53314   u.ag.apVal = p->apArg;
       
 53315   assert( u.ag.apVal || u.ag.n==0 );
       
 53316 
       
 53317   assert( u.ag.n==0 || (pOp->p2>0 && pOp->p2+u.ag.n<=p->nMem+1) );
       
 53318   assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ag.n );
       
 53319   u.ag.pArg = &p->aMem[pOp->p2];
       
 53320   for(u.ag.i=0; u.ag.i<u.ag.n; u.ag.i++, u.ag.pArg++){
       
 53321     u.ag.apVal[u.ag.i] = u.ag.pArg;
       
 53322     storeTypeInfo(u.ag.pArg, encoding);
       
 53323     REGISTER_TRACE(pOp->p2, u.ag.pArg);
       
 53324   }
       
 53325 
       
 53326   assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
       
 53327   if( pOp->p4type==P4_FUNCDEF ){
       
 53328     u.ag.ctx.pFunc = pOp->p4.pFunc;
       
 53329     u.ag.ctx.pVdbeFunc = 0;
       
 53330   }else{
       
 53331     u.ag.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
       
 53332     u.ag.ctx.pFunc = u.ag.ctx.pVdbeFunc->pFunc;
       
 53333   }
       
 53334 
       
 53335   assert( pOp->p3>0 && pOp->p3<=p->nMem );
       
 53336   pOut = &p->aMem[pOp->p3];
       
 53337   u.ag.ctx.s.flags = MEM_Null;
       
 53338   u.ag.ctx.s.db = db;
       
 53339   u.ag.ctx.s.xDel = 0;
       
 53340   u.ag.ctx.s.zMalloc = 0;
       
 53341 
       
 53342   /* The output cell may already have a buffer allocated. Move
       
 53343   ** the pointer to u.ag.ctx.s so in case the user-function can use
       
 53344   ** the already allocated buffer instead of allocating a new one.
       
 53345   */
       
 53346   sqlite3VdbeMemMove(&u.ag.ctx.s, pOut);
       
 53347   MemSetTypeFlag(&u.ag.ctx.s, MEM_Null);
       
 53348 
       
 53349   u.ag.ctx.isError = 0;
       
 53350   if( u.ag.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
       
 53351     assert( pOp>p->aOp );
       
 53352     assert( pOp[-1].p4type==P4_COLLSEQ );
       
 53353     assert( pOp[-1].opcode==OP_CollSeq );
       
 53354     u.ag.ctx.pColl = pOp[-1].p4.pColl;
       
 53355   }
       
 53356   if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
       
 53357   (*u.ag.ctx.pFunc->xFunc)(&u.ag.ctx, u.ag.n, u.ag.apVal);
       
 53358   if( sqlite3SafetyOn(db) ){
       
 53359     sqlite3VdbeMemRelease(&u.ag.ctx.s);
       
 53360     goto abort_due_to_misuse;
       
 53361   }
       
 53362   if( db->mallocFailed ){
       
 53363     /* Even though a malloc() has failed, the implementation of the
       
 53364     ** user function may have called an sqlite3_result_XXX() function
       
 53365     ** to return a value. The following call releases any resources
       
 53366     ** associated with such a value.
       
 53367     **
       
 53368     ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn()
       
 53369     ** fails also (the if(...) statement above). But if people are
       
 53370     ** misusing sqlite, they have bigger problems than a leaked value.
       
 53371     */
       
 53372     sqlite3VdbeMemRelease(&u.ag.ctx.s);
       
 53373     goto no_mem;
       
 53374   }
       
 53375 
       
 53376   /* If any auxiliary data functions have been called by this user function,
       
 53377   ** immediately call the destructor for any non-static values.
       
 53378   */
       
 53379   if( u.ag.ctx.pVdbeFunc ){
       
 53380     sqlite3VdbeDeleteAuxData(u.ag.ctx.pVdbeFunc, pOp->p1);
       
 53381     pOp->p4.pVdbeFunc = u.ag.ctx.pVdbeFunc;
       
 53382     pOp->p4type = P4_VDBEFUNC;
       
 53383   }
       
 53384 
       
 53385   /* If the function returned an error, throw an exception */
       
 53386   if( u.ag.ctx.isError ){
       
 53387     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ag.ctx.s));
       
 53388     rc = u.ag.ctx.isError;
       
 53389   }
       
 53390 
       
 53391   /* Copy the result of the function into register P3 */
       
 53392   sqlite3VdbeChangeEncoding(&u.ag.ctx.s, encoding);
       
 53393   sqlite3VdbeMemMove(pOut, &u.ag.ctx.s);
       
 53394   if( sqlite3VdbeMemTooBig(pOut) ){
       
 53395     goto too_big;
       
 53396   }
       
 53397   REGISTER_TRACE(pOp->p3, pOut);
       
 53398   UPDATE_MAX_BLOBSIZE(pOut);
       
 53399   break;
       
 53400 }
       
 53401 
       
 53402 /* Opcode: BitAnd P1 P2 P3 * *
       
 53403 **
       
 53404 ** Take the bit-wise AND of the values in register P1 and P2 and
       
 53405 ** store the result in register P3.
       
 53406 ** If either input is NULL, the result is NULL.
       
 53407 */
       
 53408 /* Opcode: BitOr P1 P2 P3 * *
       
 53409 **
       
 53410 ** Take the bit-wise OR of the values in register P1 and P2 and
       
 53411 ** store the result in register P3.
       
 53412 ** If either input is NULL, the result is NULL.
       
 53413 */
       
 53414 /* Opcode: ShiftLeft P1 P2 P3 * *
       
 53415 **
       
 53416 ** Shift the integer value in register P2 to the left by the
       
 53417 ** number of bits specified by the integer in regiser P1.
       
 53418 ** Store the result in register P3.
       
 53419 ** If either input is NULL, the result is NULL.
       
 53420 */
       
 53421 /* Opcode: ShiftRight P1 P2 P3 * *
       
 53422 **
       
 53423 ** Shift the integer value in register P2 to the right by the
       
 53424 ** number of bits specified by the integer in register P1.
       
 53425 ** Store the result in register P3.
       
 53426 ** If either input is NULL, the result is NULL.
       
 53427 */
       
 53428 case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
       
 53429 case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
       
 53430 case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
       
 53431 case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
       
 53432 #if 0  /* local variables moved into u.ah */
       
 53433   i64 a;
       
 53434   i64 b;
       
 53435 #endif /* local variables moved into u.ah */
       
 53436 
       
 53437   if( (pIn1->flags | pIn2->flags) & MEM_Null ){
       
 53438     sqlite3VdbeMemSetNull(pOut);
       
 53439     break;
       
 53440   }
       
 53441   u.ah.a = sqlite3VdbeIntValue(pIn2);
       
 53442   u.ah.b = sqlite3VdbeIntValue(pIn1);
       
 53443   switch( pOp->opcode ){
       
 53444     case OP_BitAnd:      u.ah.a &= u.ah.b;     break;
       
 53445     case OP_BitOr:       u.ah.a |= u.ah.b;     break;
       
 53446     case OP_ShiftLeft:   u.ah.a <<= u.ah.b;    break;
       
 53447     default:  assert( pOp->opcode==OP_ShiftRight );
       
 53448                          u.ah.a >>= u.ah.b;    break;
       
 53449   }
       
 53450   pOut->u.i = u.ah.a;
       
 53451   MemSetTypeFlag(pOut, MEM_Int);
       
 53452   break;
       
 53453 }
       
 53454 
       
 53455 /* Opcode: AddImm  P1 P2 * * *
       
 53456 ** 
       
 53457 ** Add the constant P2 to the value in register P1.
       
 53458 ** The result is always an integer.
       
 53459 **
       
 53460 ** To force any register to be an integer, just add 0.
       
 53461 */
       
 53462 case OP_AddImm: {            /* in1 */
       
 53463   sqlite3VdbeMemIntegerify(pIn1);
       
 53464   pIn1->u.i += pOp->p2;
       
 53465   break;
       
 53466 }
       
 53467 
       
 53468 /* Opcode: MustBeInt P1 P2 * * *
       
 53469 ** 
       
 53470 ** Force the value in register P1 to be an integer.  If the value
       
 53471 ** in P1 is not an integer and cannot be converted into an integer
       
 53472 ** without data loss, then jump immediately to P2, or if P2==0
       
 53473 ** raise an SQLITE_MISMATCH exception.
       
 53474 */
       
 53475 case OP_MustBeInt: {            /* jump, in1 */
       
 53476   applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
       
 53477   if( (pIn1->flags & MEM_Int)==0 ){
       
 53478     if( pOp->p2==0 ){
       
 53479       rc = SQLITE_MISMATCH;
       
 53480       goto abort_due_to_error;
       
 53481     }else{
       
 53482       pc = pOp->p2 - 1;
       
 53483     }
       
 53484   }else{
       
 53485     MemSetTypeFlag(pIn1, MEM_Int);
       
 53486   }
       
 53487   break;
       
 53488 }
       
 53489 
       
 53490 /* Opcode: RealAffinity P1 * * * *
       
 53491 **
       
 53492 ** If register P1 holds an integer convert it to a real value.
       
 53493 **
       
 53494 ** This opcode is used when extracting information from a column that
       
 53495 ** has REAL affinity.  Such column values may still be stored as
       
 53496 ** integers, for space efficiency, but after extraction we want them
       
 53497 ** to have only a real value.
       
 53498 */
       
 53499 case OP_RealAffinity: {                  /* in1 */
       
 53500   if( pIn1->flags & MEM_Int ){
       
 53501     sqlite3VdbeMemRealify(pIn1);
       
 53502   }
       
 53503   break;
       
 53504 }
       
 53505 
       
 53506 #ifndef SQLITE_OMIT_CAST
       
 53507 /* Opcode: ToText P1 * * * *
       
 53508 **
       
 53509 ** Force the value in register P1 to be text.
       
 53510 ** If the value is numeric, convert it to a string using the
       
 53511 ** equivalent of printf().  Blob values are unchanged and
       
 53512 ** are afterwards simply interpreted as text.
       
 53513 **
       
 53514 ** A NULL value is not changed by this routine.  It remains NULL.
       
 53515 */
       
 53516 case OP_ToText: {                  /* same as TK_TO_TEXT, in1 */
       
 53517   if( pIn1->flags & MEM_Null ) break;
       
 53518   assert( MEM_Str==(MEM_Blob>>3) );
       
 53519   pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
       
 53520   applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
       
 53521   rc = ExpandBlob(pIn1);
       
 53522   assert( pIn1->flags & MEM_Str || db->mallocFailed );
       
 53523   pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
       
 53524   UPDATE_MAX_BLOBSIZE(pIn1);
       
 53525   break;
       
 53526 }
       
 53527 
       
 53528 /* Opcode: ToBlob P1 * * * *
       
 53529 **
       
 53530 ** Force the value in register P1 to be a BLOB.
       
 53531 ** If the value is numeric, convert it to a string first.
       
 53532 ** Strings are simply reinterpreted as blobs with no change
       
 53533 ** to the underlying data.
       
 53534 **
       
 53535 ** A NULL value is not changed by this routine.  It remains NULL.
       
 53536 */
       
 53537 case OP_ToBlob: {                  /* same as TK_TO_BLOB, in1 */
       
 53538   if( pIn1->flags & MEM_Null ) break;
       
 53539   if( (pIn1->flags & MEM_Blob)==0 ){
       
 53540     applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
       
 53541     assert( pIn1->flags & MEM_Str || db->mallocFailed );
       
 53542     MemSetTypeFlag(pIn1, MEM_Blob);
       
 53543   }else{
       
 53544     pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
       
 53545   }
       
 53546   UPDATE_MAX_BLOBSIZE(pIn1);
       
 53547   break;
       
 53548 }
       
 53549 
       
 53550 /* Opcode: ToNumeric P1 * * * *
       
 53551 **
       
 53552 ** Force the value in register P1 to be numeric (either an
       
 53553 ** integer or a floating-point number.)
       
 53554 ** If the value is text or blob, try to convert it to an using the
       
 53555 ** equivalent of atoi() or atof() and store 0 if no such conversion 
       
 53556 ** is possible.
       
 53557 **
       
 53558 ** A NULL value is not changed by this routine.  It remains NULL.
       
 53559 */
       
 53560 case OP_ToNumeric: {                  /* same as TK_TO_NUMERIC, in1 */
       
 53561   if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){
       
 53562     sqlite3VdbeMemNumerify(pIn1);
       
 53563   }
       
 53564   break;
       
 53565 }
       
 53566 #endif /* SQLITE_OMIT_CAST */
       
 53567 
       
 53568 /* Opcode: ToInt P1 * * * *
       
 53569 **
       
 53570 ** Force the value in register P1 be an integer.  If
       
 53571 ** The value is currently a real number, drop its fractional part.
       
 53572 ** If the value is text or blob, try to convert it to an integer using the
       
 53573 ** equivalent of atoi() and store 0 if no such conversion is possible.
       
 53574 **
       
 53575 ** A NULL value is not changed by this routine.  It remains NULL.
       
 53576 */
       
 53577 case OP_ToInt: {                  /* same as TK_TO_INT, in1 */
       
 53578   if( (pIn1->flags & MEM_Null)==0 ){
       
 53579     sqlite3VdbeMemIntegerify(pIn1);
       
 53580   }
       
 53581   break;
       
 53582 }
       
 53583 
       
 53584 #ifndef SQLITE_OMIT_CAST
       
 53585 /* Opcode: ToReal P1 * * * *
       
 53586 **
       
 53587 ** Force the value in register P1 to be a floating point number.
       
 53588 ** If The value is currently an integer, convert it.
       
 53589 ** If the value is text or blob, try to convert it to an integer using the
       
 53590 ** equivalent of atoi() and store 0.0 if no such conversion is possible.
       
 53591 **
       
 53592 ** A NULL value is not changed by this routine.  It remains NULL.
       
 53593 */
       
 53594 case OP_ToReal: {                  /* same as TK_TO_REAL, in1 */
       
 53595   if( (pIn1->flags & MEM_Null)==0 ){
       
 53596     sqlite3VdbeMemRealify(pIn1);
       
 53597   }
       
 53598   break;
       
 53599 }
       
 53600 #endif /* SQLITE_OMIT_CAST */
       
 53601 
       
 53602 /* Opcode: Lt P1 P2 P3 P4 P5
       
 53603 **
       
 53604 ** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
       
 53605 ** jump to address P2.  
       
 53606 **
       
 53607 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
       
 53608 ** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL 
       
 53609 ** bit is clear then fall thru if either operand is NULL.
       
 53610 **
       
 53611 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
       
 53612 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 
       
 53613 ** to coerce both inputs according to this affinity before the
       
 53614 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
       
 53615 ** affinity is used. Note that the affinity conversions are stored
       
 53616 ** back into the input registers P1 and P3.  So this opcode can cause
       
 53617 ** persistent changes to registers P1 and P3.
       
 53618 **
       
 53619 ** Once any conversions have taken place, and neither value is NULL, 
       
 53620 ** the values are compared. If both values are blobs then memcmp() is
       
 53621 ** used to determine the results of the comparison.  If both values
       
 53622 ** are text, then the appropriate collating function specified in
       
 53623 ** P4 is  used to do the comparison.  If P4 is not specified then
       
 53624 ** memcmp() is used to compare text string.  If both values are
       
 53625 ** numeric, then a numeric comparison is used. If the two values
       
 53626 ** are of different types, then numbers are considered less than
       
 53627 ** strings and strings are considered less than blobs.
       
 53628 **
       
 53629 ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
       
 53630 ** store a boolean result (either 0, or 1, or NULL) in register P2.
       
 53631 */
       
 53632 /* Opcode: Ne P1 P2 P3 P4 P5
       
 53633 **
       
 53634 ** This works just like the Lt opcode except that the jump is taken if
       
 53635 ** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
       
 53636 ** additional information.
       
 53637 **
       
 53638 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
       
 53639 ** true or false and is never NULL.  If both operands are NULL then the result
       
 53640 ** of comparison is false.  If either operand is NULL then the result is true.
       
 53641 ** If neither operand is NULL the the result is the same as it would be if
       
 53642 ** the SQLITE_NULLEQ flag were omitted from P5.
       
 53643 */
       
 53644 /* Opcode: Eq P1 P2 P3 P4 P5
       
 53645 **
       
 53646 ** This works just like the Lt opcode except that the jump is taken if
       
 53647 ** the operands in registers P1 and P3 are equal.
       
 53648 ** See the Lt opcode for additional information.
       
 53649 **
       
 53650 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
       
 53651 ** true or false and is never NULL.  If both operands are NULL then the result
       
 53652 ** of comparison is true.  If either operand is NULL then the result is false.
       
 53653 ** If neither operand is NULL the the result is the same as it would be if
       
 53654 ** the SQLITE_NULLEQ flag were omitted from P5.
       
 53655 */
       
 53656 /* Opcode: Le P1 P2 P3 P4 P5
       
 53657 **
       
 53658 ** This works just like the Lt opcode except that the jump is taken if
       
 53659 ** the content of register P3 is less than or equal to the content of
       
 53660 ** register P1.  See the Lt opcode for additional information.
       
 53661 */
       
 53662 /* Opcode: Gt P1 P2 P3 P4 P5
       
 53663 **
       
 53664 ** This works just like the Lt opcode except that the jump is taken if
       
 53665 ** the content of register P3 is greater than the content of
       
 53666 ** register P1.  See the Lt opcode for additional information.
       
 53667 */
       
 53668 /* Opcode: Ge P1 P2 P3 P4 P5
       
 53669 **
       
 53670 ** This works just like the Lt opcode except that the jump is taken if
       
 53671 ** the content of register P3 is greater than or equal to the content of
       
 53672 ** register P1.  See the Lt opcode for additional information.
       
 53673 */
       
 53674 case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
       
 53675 case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
       
 53676 case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
       
 53677 case OP_Le:               /* same as TK_LE, jump, in1, in3 */
       
 53678 case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
       
 53679 case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
       
 53680 #if 0  /* local variables moved into u.ai */
       
 53681   int res;            /* Result of the comparison of pIn1 against pIn3 */
       
 53682   char affinity;      /* Affinity to use for comparison */
       
 53683 #endif /* local variables moved into u.ai */
       
 53684 
       
 53685   if( (pIn1->flags | pIn3->flags)&MEM_Null ){
       
 53686     /* One or both operands are NULL */
       
 53687     if( pOp->p5 & SQLITE_NULLEQ ){
       
 53688       /* If SQLITE_NULLEQ is set (which will only happen if the operator is
       
 53689       ** OP_Eq or OP_Ne) then take the jump or not depending on whether
       
 53690       ** or not both operands are null.
       
 53691       */
       
 53692       assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
       
 53693       u.ai.res = (pIn1->flags & pIn3->flags & MEM_Null)==0;
       
 53694     }else{
       
 53695       /* SQLITE_NULLEQ is clear and at least one operand is NULL,
       
 53696       ** then the result is always NULL.
       
 53697       ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
       
 53698       */
       
 53699       if( pOp->p5 & SQLITE_STOREP2 ){
       
 53700         pOut = &p->aMem[pOp->p2];
       
 53701         MemSetTypeFlag(pOut, MEM_Null);
       
 53702         REGISTER_TRACE(pOp->p2, pOut);
       
 53703       }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
       
 53704         pc = pOp->p2-1;
       
 53705       }
       
 53706       break;
       
 53707     }
       
 53708   }else{
       
 53709     /* Neither operand is NULL.  Do a comparison. */
       
 53710     u.ai.affinity = pOp->p5 & SQLITE_AFF_MASK;
       
 53711     if( u.ai.affinity ){
       
 53712       applyAffinity(pIn1, u.ai.affinity, encoding);
       
 53713       applyAffinity(pIn3, u.ai.affinity, encoding);
       
 53714       if( db->mallocFailed ) goto no_mem;
       
 53715     }
       
 53716 
       
 53717     assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
       
 53718     ExpandBlob(pIn1);
       
 53719     ExpandBlob(pIn3);
       
 53720     u.ai.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
       
 53721   }
       
 53722   switch( pOp->opcode ){
       
 53723     case OP_Eq:    u.ai.res = u.ai.res==0;     break;
       
 53724     case OP_Ne:    u.ai.res = u.ai.res!=0;     break;
       
 53725     case OP_Lt:    u.ai.res = u.ai.res<0;      break;
       
 53726     case OP_Le:    u.ai.res = u.ai.res<=0;     break;
       
 53727     case OP_Gt:    u.ai.res = u.ai.res>0;      break;
       
 53728     default:       u.ai.res = u.ai.res>=0;     break;
       
 53729   }
       
 53730 
       
 53731   if( pOp->p5 & SQLITE_STOREP2 ){
       
 53732     pOut = &p->aMem[pOp->p2];
       
 53733     MemSetTypeFlag(pOut, MEM_Int);
       
 53734     pOut->u.i = u.ai.res;
       
 53735     REGISTER_TRACE(pOp->p2, pOut);
       
 53736   }else if( u.ai.res ){
       
 53737     pc = pOp->p2-1;
       
 53738   }
       
 53739   break;
       
 53740 }
       
 53741 
       
 53742 /* Opcode: Permutation * * * P4 *
       
 53743 **
       
 53744 ** Set the permutation used by the OP_Compare operator to be the array
       
 53745 ** of integers in P4.
       
 53746 **
       
 53747 ** The permutation is only valid until the next OP_Permutation, OP_Compare,
       
 53748 ** OP_Halt, or OP_ResultRow.  Typically the OP_Permutation should occur
       
 53749 ** immediately prior to the OP_Compare.
       
 53750 */
       
 53751 case OP_Permutation: {
       
 53752   assert( pOp->p4type==P4_INTARRAY );
       
 53753   assert( pOp->p4.ai );
       
 53754   aPermute = pOp->p4.ai;
       
 53755   break;
       
 53756 }
       
 53757 
       
 53758 /* Opcode: Compare P1 P2 P3 P4 *
       
 53759 **
       
 53760 ** Compare to vectors of registers in reg(P1)..reg(P1+P3-1) (all this
       
 53761 ** one "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
       
 53762 ** the comparison for use by the next OP_Jump instruct.
       
 53763 **
       
 53764 ** P4 is a KeyInfo structure that defines collating sequences and sort
       
 53765 ** orders for the comparison.  The permutation applies to registers
       
 53766 ** only.  The KeyInfo elements are used sequentially.
       
 53767 **
       
 53768 ** The comparison is a sort comparison, so NULLs compare equal,
       
 53769 ** NULLs are less than numbers, numbers are less than strings,
       
 53770 ** and strings are less than blobs.
       
 53771 */
       
 53772 case OP_Compare: {
       
 53773 #if 0  /* local variables moved into u.aj */
       
 53774   int n;
       
 53775   int i;
       
 53776   int p1;
       
 53777   int p2;
       
 53778   const KeyInfo *pKeyInfo;
       
 53779   int idx;
       
 53780   CollSeq *pColl;    /* Collating sequence to use on this term */
       
 53781   int bRev;          /* True for DESCENDING sort order */
       
 53782 #endif /* local variables moved into u.aj */
       
 53783 
       
 53784   u.aj.n = pOp->p3;
       
 53785   u.aj.pKeyInfo = pOp->p4.pKeyInfo;
       
 53786   assert( u.aj.n>0 );
       
 53787   assert( u.aj.pKeyInfo!=0 );
       
 53788   u.aj.p1 = pOp->p1;
       
 53789   u.aj.p2 = pOp->p2;
       
 53790 #if SQLITE_DEBUG
       
 53791   if( aPermute ){
       
 53792     int k, mx = 0;
       
 53793     for(k=0; k<u.aj.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
       
 53794     assert( u.aj.p1>0 && u.aj.p1+mx<=p->nMem+1 );
       
 53795     assert( u.aj.p2>0 && u.aj.p2+mx<=p->nMem+1 );
       
 53796   }else{
       
 53797     assert( u.aj.p1>0 && u.aj.p1+u.aj.n<=p->nMem+1 );
       
 53798     assert( u.aj.p2>0 && u.aj.p2+u.aj.n<=p->nMem+1 );
       
 53799   }
       
 53800 #endif /* SQLITE_DEBUG */
       
 53801   for(u.aj.i=0; u.aj.i<u.aj.n; u.aj.i++){
       
 53802     u.aj.idx = aPermute ? aPermute[u.aj.i] : u.aj.i;
       
 53803     REGISTER_TRACE(u.aj.p1+u.aj.idx, &p->aMem[u.aj.p1+u.aj.idx]);
       
 53804     REGISTER_TRACE(u.aj.p2+u.aj.idx, &p->aMem[u.aj.p2+u.aj.idx]);
       
 53805     assert( u.aj.i<u.aj.pKeyInfo->nField );
       
 53806     u.aj.pColl = u.aj.pKeyInfo->aColl[u.aj.i];
       
 53807     u.aj.bRev = u.aj.pKeyInfo->aSortOrder[u.aj.i];
       
 53808     iCompare = sqlite3MemCompare(&p->aMem[u.aj.p1+u.aj.idx], &p->aMem[u.aj.p2+u.aj.idx], u.aj.pColl);
       
 53809     if( iCompare ){
       
 53810       if( u.aj.bRev ) iCompare = -iCompare;
       
 53811       break;
       
 53812     }
       
 53813   }
       
 53814   aPermute = 0;
       
 53815   break;
       
 53816 }
       
 53817 
       
 53818 /* Opcode: Jump P1 P2 P3 * *
       
 53819 **
       
 53820 ** Jump to the instruction at address P1, P2, or P3 depending on whether
       
 53821 ** in the most recent OP_Compare instruction the P1 vector was less than
       
 53822 ** equal to, or greater than the P2 vector, respectively.
       
 53823 */
       
 53824 case OP_Jump: {             /* jump */
       
 53825   if( iCompare<0 ){
       
 53826     pc = pOp->p1 - 1;
       
 53827   }else if( iCompare==0 ){
       
 53828     pc = pOp->p2 - 1;
       
 53829   }else{
       
 53830     pc = pOp->p3 - 1;
       
 53831   }
       
 53832   break;
       
 53833 }
       
 53834 
       
 53835 /* Opcode: And P1 P2 P3 * *
       
 53836 **
       
 53837 ** Take the logical AND of the values in registers P1 and P2 and
       
 53838 ** write the result into register P3.
       
 53839 **
       
 53840 ** If either P1 or P2 is 0 (false) then the result is 0 even if
       
 53841 ** the other input is NULL.  A NULL and true or two NULLs give
       
 53842 ** a NULL output.
       
 53843 */
       
 53844 /* Opcode: Or P1 P2 P3 * *
       
 53845 **
       
 53846 ** Take the logical OR of the values in register P1 and P2 and
       
 53847 ** store the answer in register P3.
       
 53848 **
       
 53849 ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
       
 53850 ** even if the other input is NULL.  A NULL and false or two NULLs
       
 53851 ** give a NULL output.
       
 53852 */
       
 53853 case OP_And:              /* same as TK_AND, in1, in2, out3 */
       
 53854 case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
       
 53855 #if 0  /* local variables moved into u.ak */
       
 53856   int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
       
 53857   int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
       
 53858 #endif /* local variables moved into u.ak */
       
 53859 
       
 53860   if( pIn1->flags & MEM_Null ){
       
 53861     u.ak.v1 = 2;
       
 53862   }else{
       
 53863     u.ak.v1 = sqlite3VdbeIntValue(pIn1)!=0;
       
 53864   }
       
 53865   if( pIn2->flags & MEM_Null ){
       
 53866     u.ak.v2 = 2;
       
 53867   }else{
       
 53868     u.ak.v2 = sqlite3VdbeIntValue(pIn2)!=0;
       
 53869   }
       
 53870   if( pOp->opcode==OP_And ){
       
 53871     static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
       
 53872     u.ak.v1 = and_logic[u.ak.v1*3+u.ak.v2];
       
 53873   }else{
       
 53874     static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
       
 53875     u.ak.v1 = or_logic[u.ak.v1*3+u.ak.v2];
       
 53876   }
       
 53877   if( u.ak.v1==2 ){
       
 53878     MemSetTypeFlag(pOut, MEM_Null);
       
 53879   }else{
       
 53880     pOut->u.i = u.ak.v1;
       
 53881     MemSetTypeFlag(pOut, MEM_Int);
       
 53882   }
       
 53883   break;
       
 53884 }
       
 53885 
       
 53886 /* Opcode: Not P1 P2 * * *
       
 53887 **
       
 53888 ** Interpret the value in register P1 as a boolean value.  Store the
       
 53889 ** boolean complement in register P2.  If the value in register P1 is 
       
 53890 ** NULL, then a NULL is stored in P2.
       
 53891 */
       
 53892 case OP_Not: {                /* same as TK_NOT, in1 */
       
 53893   pOut = &p->aMem[pOp->p2];
       
 53894   if( pIn1->flags & MEM_Null ){
       
 53895     sqlite3VdbeMemSetNull(pOut);
       
 53896   }else{
       
 53897     sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
       
 53898   }
       
 53899   break;
       
 53900 }
       
 53901 
       
 53902 /* Opcode: BitNot P1 P2 * * *
       
 53903 **
       
 53904 ** Interpret the content of register P1 as an integer.  Store the
       
 53905 ** ones-complement of the P1 value into register P2.  If P1 holds
       
 53906 ** a NULL then store a NULL in P2.
       
 53907 */
       
 53908 case OP_BitNot: {             /* same as TK_BITNOT, in1 */
       
 53909   pOut = &p->aMem[pOp->p2];
       
 53910   if( pIn1->flags & MEM_Null ){
       
 53911     sqlite3VdbeMemSetNull(pOut);
       
 53912   }else{
       
 53913     sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
       
 53914   }
       
 53915   break;
       
 53916 }
       
 53917 
       
 53918 /* Opcode: If P1 P2 P3 * *
       
 53919 **
       
 53920 ** Jump to P2 if the value in register P1 is true.  The value is
       
 53921 ** is considered true if it is numeric and non-zero.  If the value
       
 53922 ** in P1 is NULL then take the jump if P3 is true.
       
 53923 */
       
 53924 /* Opcode: IfNot P1 P2 P3 * *
       
 53925 **
       
 53926 ** Jump to P2 if the value in register P1 is False.  The value is
       
 53927 ** is considered true if it has a numeric value of zero.  If the value
       
 53928 ** in P1 is NULL then take the jump if P3 is true.
       
 53929 */
       
 53930 case OP_If:                 /* jump, in1 */
       
 53931 case OP_IfNot: {            /* jump, in1 */
       
 53932 #if 0  /* local variables moved into u.al */
       
 53933   int c;
       
 53934 #endif /* local variables moved into u.al */
       
 53935   if( pIn1->flags & MEM_Null ){
       
 53936     u.al.c = pOp->p3;
       
 53937   }else{
       
 53938 #ifdef SQLITE_OMIT_FLOATING_POINT
       
 53939     u.al.c = sqlite3VdbeIntValue(pIn1)!=0;
       
 53940 #else
       
 53941     u.al.c = sqlite3VdbeRealValue(pIn1)!=0.0;
       
 53942 #endif
       
 53943     if( pOp->opcode==OP_IfNot ) u.al.c = !u.al.c;
       
 53944   }
       
 53945   if( u.al.c ){
       
 53946     pc = pOp->p2-1;
       
 53947   }
       
 53948   break;
       
 53949 }
       
 53950 
       
 53951 /* Opcode: IsNull P1 P2 * * *
       
 53952 **
       
 53953 ** Jump to P2 if the value in register P1 is NULL.
       
 53954 */
       
 53955 case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
       
 53956   if( (pIn1->flags & MEM_Null)!=0 ){
       
 53957     pc = pOp->p2 - 1;
       
 53958   }
       
 53959   break;
       
 53960 }
       
 53961 
       
 53962 /* Opcode: NotNull P1 P2 * * *
       
 53963 **
       
 53964 ** Jump to P2 if the value in register P1 is not NULL.  
       
 53965 */
       
 53966 case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
       
 53967   if( (pIn1->flags & MEM_Null)==0 ){
       
 53968     pc = pOp->p2 - 1;
       
 53969   }
       
 53970   break;
       
 53971 }
       
 53972 
       
 53973 /* Opcode: Column P1 P2 P3 P4 P5
       
 53974 **
       
 53975 ** Interpret the data that cursor P1 points to as a structure built using
       
 53976 ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
       
 53977 ** information about the format of the data.)  Extract the P2-th column
       
 53978 ** from this record.  If there are less that (P2+1) 
       
 53979 ** values in the record, extract a NULL.
       
 53980 **
       
 53981 ** The value extracted is stored in register P3.
       
 53982 **
       
 53983 ** If the column contains fewer than P2 fields, then extract a NULL.  Or,
       
 53984 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
       
 53985 ** the result.
       
 53986 **
       
 53987 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
       
 53988 ** then the cache of the cursor is reset prior to extracting the column.
       
 53989 ** The first OP_Column against a pseudo-table after the value of the content
       
 53990 ** register has changed should have this bit set.
       
 53991 */
       
 53992 case OP_Column: {
       
 53993 #if 0  /* local variables moved into u.am */
       
 53994   u32 payloadSize;   /* Number of bytes in the record */
       
 53995   i64 payloadSize64; /* Number of bytes in the record */
       
 53996   int p1;            /* P1 value of the opcode */
       
 53997   int p2;            /* column number to retrieve */
       
 53998   VdbeCursor *pC;    /* The VDBE cursor */
       
 53999   char *zRec;        /* Pointer to complete record-data */
       
 54000   BtCursor *pCrsr;   /* The BTree cursor */
       
 54001   u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
       
 54002   u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
       
 54003   int nField;        /* number of fields in the record */
       
 54004   int len;           /* The length of the serialized data for the column */
       
 54005   int i;             /* Loop counter */
       
 54006   char *zData;       /* Part of the record being decoded */
       
 54007   Mem *pDest;        /* Where to write the extracted value */
       
 54008   Mem sMem;          /* For storing the record being decoded */
       
 54009   u8 *zIdx;          /* Index into header */
       
 54010   u8 *zEndHdr;       /* Pointer to first byte after the header */
       
 54011   u32 offset;        /* Offset into the data */
       
 54012   u64 offset64;      /* 64-bit offset.  64 bits needed to catch overflow */
       
 54013   int szHdr;         /* Size of the header size field at start of record */
       
 54014   int avail;         /* Number of bytes of available data */
       
 54015   Mem *pReg;         /* PseudoTable input register */
       
 54016 #endif /* local variables moved into u.am */
       
 54017 
       
 54018 
       
 54019   u.am.p1 = pOp->p1;
       
 54020   u.am.p2 = pOp->p2;
       
 54021   u.am.pC = 0;
       
 54022   memset(&u.am.sMem, 0, sizeof(u.am.sMem));
       
 54023   assert( u.am.p1<p->nCursor );
       
 54024   assert( pOp->p3>0 && pOp->p3<=p->nMem );
       
 54025   u.am.pDest = &p->aMem[pOp->p3];
       
 54026   MemSetTypeFlag(u.am.pDest, MEM_Null);
       
 54027   u.am.zRec = 0;
       
 54028 
       
 54029   /* This block sets the variable u.am.payloadSize to be the total number of
       
 54030   ** bytes in the record.
       
 54031   **
       
 54032   ** u.am.zRec is set to be the complete text of the record if it is available.
       
 54033   ** The complete record text is always available for pseudo-tables
       
 54034   ** If the record is stored in a cursor, the complete record text
       
 54035   ** might be available in the  u.am.pC->aRow cache.  Or it might not be.
       
 54036   ** If the data is unavailable,  u.am.zRec is set to NULL.
       
 54037   **
       
 54038   ** We also compute the number of columns in the record.  For cursors,
       
 54039   ** the number of columns is stored in the VdbeCursor.nField element.
       
 54040   */
       
 54041   u.am.pC = p->apCsr[u.am.p1];
       
 54042   assert( u.am.pC!=0 );
       
 54043 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 54044   assert( u.am.pC->pVtabCursor==0 );
       
 54045 #endif
       
 54046   u.am.pCrsr = u.am.pC->pCursor;
       
 54047   if( u.am.pCrsr!=0 ){
       
 54048     /* The record is stored in a B-Tree */
       
 54049     rc = sqlite3VdbeCursorMoveto(u.am.pC);
       
 54050     if( rc ) goto abort_due_to_error;
       
 54051     if( u.am.pC->nullRow ){
       
 54052       u.am.payloadSize = 0;
       
 54053     }else if( u.am.pC->cacheStatus==p->cacheCtr ){
       
 54054       u.am.payloadSize = u.am.pC->payloadSize;
       
 54055       u.am.zRec = (char*)u.am.pC->aRow;
       
 54056     }else if( u.am.pC->isIndex ){
       
 54057       assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
       
 54058       rc = sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64);
       
 54059       assert( rc==SQLITE_OK );   /* True because of CursorMoveto() call above */
       
 54060       /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
       
 54061       ** payload size, so it is impossible for u.am.payloadSize64 to be
       
 54062       ** larger than 32 bits. */
       
 54063       assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 );
       
 54064       u.am.payloadSize = (u32)u.am.payloadSize64;
       
 54065     }else{
       
 54066       assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
       
 54067       rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
       
 54068       assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
       
 54069     }
       
 54070   }else if( u.am.pC->pseudoTableReg>0 ){
       
 54071     u.am.pReg = &p->aMem[u.am.pC->pseudoTableReg];
       
 54072     assert( u.am.pReg->flags & MEM_Blob );
       
 54073     u.am.payloadSize = u.am.pReg->n;
       
 54074     u.am.zRec = u.am.pReg->z;
       
 54075     u.am.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
       
 54076     assert( u.am.payloadSize==0 || u.am.zRec!=0 );
       
 54077   }else{
       
 54078     /* Consider the row to be NULL */
       
 54079     u.am.payloadSize = 0;
       
 54080   }
       
 54081 
       
 54082   /* If u.am.payloadSize is 0, then just store a NULL */
       
 54083   if( u.am.payloadSize==0 ){
       
 54084     assert( u.am.pDest->flags&MEM_Null );
       
 54085     goto op_column_out;
       
 54086   }
       
 54087   assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
       
 54088   if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
       
 54089     goto too_big;
       
 54090   }
       
 54091 
       
 54092   u.am.nField = u.am.pC->nField;
       
 54093   assert( u.am.p2<u.am.nField );
       
 54094 
       
 54095   /* Read and parse the table header.  Store the results of the parse
       
 54096   ** into the record header cache fields of the cursor.
       
 54097   */
       
 54098   u.am.aType = u.am.pC->aType;
       
 54099   if( u.am.pC->cacheStatus==p->cacheCtr ){
       
 54100     u.am.aOffset = u.am.pC->aOffset;
       
 54101   }else{
       
 54102     assert(u.am.aType);
       
 54103     u.am.avail = 0;
       
 54104     u.am.pC->aOffset = u.am.aOffset = &u.am.aType[u.am.nField];
       
 54105     u.am.pC->payloadSize = u.am.payloadSize;
       
 54106     u.am.pC->cacheStatus = p->cacheCtr;
       
 54107 
       
 54108     /* Figure out how many bytes are in the header */
       
 54109     if( u.am.zRec ){
       
 54110       u.am.zData = u.am.zRec;
       
 54111     }else{
       
 54112       if( u.am.pC->isIndex ){
       
 54113         u.am.zData = (char*)sqlite3BtreeKeyFetch(u.am.pCrsr, &u.am.avail);
       
 54114       }else{
       
 54115         u.am.zData = (char*)sqlite3BtreeDataFetch(u.am.pCrsr, &u.am.avail);
       
 54116       }
       
 54117       /* If KeyFetch()/DataFetch() managed to get the entire payload,
       
 54118       ** save the payload in the u.am.pC->aRow cache.  That will save us from
       
 54119       ** having to make additional calls to fetch the content portion of
       
 54120       ** the record.
       
 54121       */
       
 54122       assert( u.am.avail>=0 );
       
 54123       if( u.am.payloadSize <= (u32)u.am.avail ){
       
 54124         u.am.zRec = u.am.zData;
       
 54125         u.am.pC->aRow = (u8*)u.am.zData;
       
 54126       }else{
       
 54127         u.am.pC->aRow = 0;
       
 54128       }
       
 54129     }
       
 54130     /* The following assert is true in all cases accept when
       
 54131     ** the database file has been corrupted externally.
       
 54132     **    assert( u.am.zRec!=0 || u.am.avail>=u.am.payloadSize || u.am.avail>=9 ); */
       
 54133     u.am.szHdr = getVarint32((u8*)u.am.zData, u.am.offset);
       
 54134 
       
 54135     /* Make sure a corrupt database has not given us an oversize header.
       
 54136     ** Do this now to avoid an oversize memory allocation.
       
 54137     **
       
 54138     ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
       
 54139     ** types use so much data space that there can only be 4096 and 32 of
       
 54140     ** them, respectively.  So the maximum header length results from a
       
 54141     ** 3-byte type for each of the maximum of 32768 columns plus three
       
 54142     ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
       
 54143     */
       
 54144     if( u.am.offset > 98307 ){
       
 54145       rc = SQLITE_CORRUPT_BKPT;
       
 54146       goto op_column_out;
       
 54147     }
       
 54148 
       
 54149     /* Compute in u.am.len the number of bytes of data we need to read in order
       
 54150     ** to get u.am.nField type values.  u.am.offset is an upper bound on this.  But
       
 54151     ** u.am.nField might be significantly less than the true number of columns
       
 54152     ** in the table, and in that case, 5*u.am.nField+3 might be smaller than u.am.offset.
       
 54153     ** We want to minimize u.am.len in order to limit the size of the memory
       
 54154     ** allocation, especially if a corrupt database file has caused u.am.offset
       
 54155     ** to be oversized. Offset is limited to 98307 above.  But 98307 might
       
 54156     ** still exceed Robson memory allocation limits on some configurations.
       
 54157     ** On systems that cannot tolerate large memory allocations, u.am.nField*5+3
       
 54158     ** will likely be much smaller since u.am.nField will likely be less than
       
 54159     ** 20 or so.  This insures that Robson memory allocation limits are
       
 54160     ** not exceeded even for corrupt database files.
       
 54161     */
       
 54162     u.am.len = u.am.nField*5 + 3;
       
 54163     if( u.am.len > (int)u.am.offset ) u.am.len = (int)u.am.offset;
       
 54164 
       
 54165     /* The KeyFetch() or DataFetch() above are fast and will get the entire
       
 54166     ** record header in most cases.  But they will fail to get the complete
       
 54167     ** record header if the record header does not fit on a single page
       
 54168     ** in the B-Tree.  When that happens, use sqlite3VdbeMemFromBtree() to
       
 54169     ** acquire the complete header text.
       
 54170     */
       
 54171     if( !u.am.zRec && u.am.avail<u.am.len ){
       
 54172       u.am.sMem.flags = 0;
       
 54173       u.am.sMem.db = 0;
       
 54174       rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, 0, u.am.len, u.am.pC->isIndex, &u.am.sMem);
       
 54175       if( rc!=SQLITE_OK ){
       
 54176         goto op_column_out;
       
 54177       }
       
 54178       u.am.zData = u.am.sMem.z;
       
 54179     }
       
 54180     u.am.zEndHdr = (u8 *)&u.am.zData[u.am.len];
       
 54181     u.am.zIdx = (u8 *)&u.am.zData[u.am.szHdr];
       
 54182 
       
 54183     /* Scan the header and use it to fill in the u.am.aType[] and u.am.aOffset[]
       
 54184     ** arrays.  u.am.aType[u.am.i] will contain the type integer for the u.am.i-th
       
 54185     ** column and u.am.aOffset[u.am.i] will contain the u.am.offset from the beginning
       
 54186     ** of the record to the start of the data for the u.am.i-th column
       
 54187     */
       
 54188     u.am.offset64 = u.am.offset;
       
 54189     for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){
       
 54190       if( u.am.zIdx<u.am.zEndHdr ){
       
 54191         u.am.aOffset[u.am.i] = (u32)u.am.offset64;
       
 54192         u.am.zIdx += getVarint32(u.am.zIdx, u.am.aType[u.am.i]);
       
 54193         u.am.offset64 += sqlite3VdbeSerialTypeLen(u.am.aType[u.am.i]);
       
 54194       }else{
       
 54195         /* If u.am.i is less that u.am.nField, then there are less fields in this
       
 54196         ** record than SetNumColumns indicated there are columns in the
       
 54197         ** table. Set the u.am.offset for any extra columns not present in
       
 54198         ** the record to 0. This tells code below to store a NULL
       
 54199         ** instead of deserializing a value from the record.
       
 54200         */
       
 54201         u.am.aOffset[u.am.i] = 0;
       
 54202       }
       
 54203     }
       
 54204     sqlite3VdbeMemRelease(&u.am.sMem);
       
 54205     u.am.sMem.flags = MEM_Null;
       
 54206 
       
 54207     /* If we have read more header data than was contained in the header,
       
 54208     ** or if the end of the last field appears to be past the end of the
       
 54209     ** record, or if the end of the last field appears to be before the end
       
 54210     ** of the record (when all fields present), then we must be dealing
       
 54211     ** with a corrupt database.
       
 54212     */
       
 54213     if( (u.am.zIdx > u.am.zEndHdr)|| (u.am.offset64 > u.am.payloadSize)
       
 54214      || (u.am.zIdx==u.am.zEndHdr && u.am.offset64!=(u64)u.am.payloadSize) ){
       
 54215       rc = SQLITE_CORRUPT_BKPT;
       
 54216       goto op_column_out;
       
 54217     }
       
 54218   }
       
 54219 
       
 54220   /* Get the column information. If u.am.aOffset[u.am.p2] is non-zero, then
       
 54221   ** deserialize the value from the record. If u.am.aOffset[u.am.p2] is zero,
       
 54222   ** then there are not enough fields in the record to satisfy the
       
 54223   ** request.  In this case, set the value NULL or to P4 if P4 is
       
 54224   ** a pointer to a Mem object.
       
 54225   */
       
 54226   if( u.am.aOffset[u.am.p2] ){
       
 54227     assert( rc==SQLITE_OK );
       
 54228     if( u.am.zRec ){
       
 54229       sqlite3VdbeMemReleaseExternal(u.am.pDest);
       
 54230       sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest);
       
 54231     }else{
       
 54232       u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]);
       
 54233       sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest);
       
 54234       rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem);
       
 54235       if( rc!=SQLITE_OK ){
       
 54236         goto op_column_out;
       
 54237       }
       
 54238       u.am.zData = u.am.sMem.z;
       
 54239       sqlite3VdbeSerialGet((u8*)u.am.zData, u.am.aType[u.am.p2], u.am.pDest);
       
 54240     }
       
 54241     u.am.pDest->enc = encoding;
       
 54242   }else{
       
 54243     if( pOp->p4type==P4_MEM ){
       
 54244       sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static);
       
 54245     }else{
       
 54246       assert( u.am.pDest->flags&MEM_Null );
       
 54247     }
       
 54248   }
       
 54249 
       
 54250   /* If we dynamically allocated space to hold the data (in the
       
 54251   ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
       
 54252   ** dynamically allocated space over to the u.am.pDest structure.
       
 54253   ** This prevents a memory copy.
       
 54254   */
       
 54255   if( u.am.sMem.zMalloc ){
       
 54256     assert( u.am.sMem.z==u.am.sMem.zMalloc );
       
 54257     assert( !(u.am.pDest->flags & MEM_Dyn) );
       
 54258     assert( !(u.am.pDest->flags & (MEM_Blob|MEM_Str)) || u.am.pDest->z==u.am.sMem.z );
       
 54259     u.am.pDest->flags &= ~(MEM_Ephem|MEM_Static);
       
 54260     u.am.pDest->flags |= MEM_Term;
       
 54261     u.am.pDest->z = u.am.sMem.z;
       
 54262     u.am.pDest->zMalloc = u.am.sMem.zMalloc;
       
 54263   }
       
 54264 
       
 54265   rc = sqlite3VdbeMemMakeWriteable(u.am.pDest);
       
 54266 
       
 54267 op_column_out:
       
 54268   UPDATE_MAX_BLOBSIZE(u.am.pDest);
       
 54269   REGISTER_TRACE(pOp->p3, u.am.pDest);
       
 54270   break;
       
 54271 }
       
 54272 
       
 54273 /* Opcode: Affinity P1 P2 * P4 *
       
 54274 **
       
 54275 ** Apply affinities to a range of P2 registers starting with P1.
       
 54276 **
       
 54277 ** P4 is a string that is P2 characters long. The nth character of the
       
 54278 ** string indicates the column affinity that should be used for the nth
       
 54279 ** memory cell in the range.
       
 54280 */
       
 54281 case OP_Affinity: {
       
 54282 #if 0  /* local variables moved into u.an */
       
 54283   char *zAffinity;   /* The affinity to be applied */
       
 54284   Mem *pData0;       /* First register to which to apply affinity */
       
 54285   Mem *pLast;        /* Last register to which to apply affinity */
       
 54286   Mem *pRec;         /* Current register */
       
 54287 #endif /* local variables moved into u.an */
       
 54288 
       
 54289   u.an.zAffinity = pOp->p4.z;
       
 54290   u.an.pData0 = &p->aMem[pOp->p1];
       
 54291   u.an.pLast = &u.an.pData0[pOp->p2-1];
       
 54292   for(u.an.pRec=u.an.pData0; u.an.pRec<=u.an.pLast; u.an.pRec++){
       
 54293     ExpandBlob(u.an.pRec);
       
 54294     applyAffinity(u.an.pRec, u.an.zAffinity[u.an.pRec-u.an.pData0], encoding);
       
 54295   }
       
 54296   break;
       
 54297 }
       
 54298 
       
 54299 /* Opcode: MakeRecord P1 P2 P3 P4 *
       
 54300 **
       
 54301 ** Convert P2 registers beginning with P1 into a single entry
       
 54302 ** suitable for use as a data record in a database table or as a key
       
 54303 ** in an index.  The details of the format are irrelevant as long as
       
 54304 ** the OP_Column opcode can decode the record later.
       
 54305 ** Refer to source code comments for the details of the record
       
 54306 ** format.
       
 54307 **
       
 54308 ** P4 may be a string that is P2 characters long.  The nth character of the
       
 54309 ** string indicates the column affinity that should be used for the nth
       
 54310 ** field of the index key.
       
 54311 **
       
 54312 ** The mapping from character to affinity is given by the SQLITE_AFF_
       
 54313 ** macros defined in sqliteInt.h.
       
 54314 **
       
 54315 ** If P4 is NULL then all index fields have the affinity NONE.
       
 54316 */
       
 54317 case OP_MakeRecord: {
       
 54318 #if 0  /* local variables moved into u.ao */
       
 54319   u8 *zNewRecord;        /* A buffer to hold the data for the new record */
       
 54320   Mem *pRec;             /* The new record */
       
 54321   u64 nData;             /* Number of bytes of data space */
       
 54322   int nHdr;              /* Number of bytes of header space */
       
 54323   i64 nByte;             /* Data space required for this record */
       
 54324   int nZero;             /* Number of zero bytes at the end of the record */
       
 54325   int nVarint;           /* Number of bytes in a varint */
       
 54326   u32 serial_type;       /* Type field */
       
 54327   Mem *pData0;           /* First field to be combined into the record */
       
 54328   Mem *pLast;            /* Last field of the record */
       
 54329   int nField;            /* Number of fields in the record */
       
 54330   char *zAffinity;       /* The affinity string for the record */
       
 54331   int file_format;       /* File format to use for encoding */
       
 54332   int i;                 /* Space used in zNewRecord[] */
       
 54333   int len;               /* Length of a field */
       
 54334 #endif /* local variables moved into u.ao */
       
 54335 
       
 54336   /* Assuming the record contains N fields, the record format looks
       
 54337   ** like this:
       
 54338   **
       
 54339   ** ------------------------------------------------------------------------
       
 54340   ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
       
 54341   ** ------------------------------------------------------------------------
       
 54342   **
       
 54343   ** Data(0) is taken from register P1.  Data(1) comes from register P1+1
       
 54344   ** and so froth.
       
 54345   **
       
 54346   ** Each type field is a varint representing the serial type of the
       
 54347   ** corresponding data element (see sqlite3VdbeSerialType()). The
       
 54348   ** hdr-size field is also a varint which is the offset from the beginning
       
 54349   ** of the record to data0.
       
 54350   */
       
 54351   u.ao.nData = 0;         /* Number of bytes of data space */
       
 54352   u.ao.nHdr = 0;          /* Number of bytes of header space */
       
 54353   u.ao.nByte = 0;         /* Data space required for this record */
       
 54354   u.ao.nZero = 0;         /* Number of zero bytes at the end of the record */
       
 54355   u.ao.nField = pOp->p1;
       
 54356   u.ao.zAffinity = pOp->p4.z;
       
 54357   assert( u.ao.nField>0 && pOp->p2>0 && pOp->p2+u.ao.nField<=p->nMem+1 );
       
 54358   u.ao.pData0 = &p->aMem[u.ao.nField];
       
 54359   u.ao.nField = pOp->p2;
       
 54360   u.ao.pLast = &u.ao.pData0[u.ao.nField-1];
       
 54361   u.ao.file_format = p->minWriteFileFormat;
       
 54362 
       
 54363   /* Loop through the elements that will make up the record to figure
       
 54364   ** out how much space is required for the new record.
       
 54365   */
       
 54366   for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
       
 54367     if( u.ao.zAffinity ){
       
 54368       applyAffinity(u.ao.pRec, u.ao.zAffinity[u.ao.pRec-u.ao.pData0], encoding);
       
 54369     }
       
 54370     if( u.ao.pRec->flags&MEM_Zero && u.ao.pRec->n>0 ){
       
 54371       sqlite3VdbeMemExpandBlob(u.ao.pRec);
       
 54372     }
       
 54373     u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
       
 54374     u.ao.len = sqlite3VdbeSerialTypeLen(u.ao.serial_type);
       
 54375     u.ao.nData += u.ao.len;
       
 54376     u.ao.nHdr += sqlite3VarintLen(u.ao.serial_type);
       
 54377     if( u.ao.pRec->flags & MEM_Zero ){
       
 54378       /* Only pure zero-filled BLOBs can be input to this Opcode.
       
 54379       ** We do not allow blobs with a prefix and a zero-filled tail. */
       
 54380       u.ao.nZero += u.ao.pRec->u.nZero;
       
 54381     }else if( u.ao.len ){
       
 54382       u.ao.nZero = 0;
       
 54383     }
       
 54384   }
       
 54385 
       
 54386   /* Add the initial header varint and total the size */
       
 54387   u.ao.nHdr += u.ao.nVarint = sqlite3VarintLen(u.ao.nHdr);
       
 54388   if( u.ao.nVarint<sqlite3VarintLen(u.ao.nHdr) ){
       
 54389     u.ao.nHdr++;
       
 54390   }
       
 54391   u.ao.nByte = u.ao.nHdr+u.ao.nData-u.ao.nZero;
       
 54392   if( u.ao.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
       
 54393     goto too_big;
       
 54394   }
       
 54395 
       
 54396   /* Make sure the output register has a buffer large enough to store
       
 54397   ** the new record. The output register (pOp->p3) is not allowed to
       
 54398   ** be one of the input registers (because the following call to
       
 54399   ** sqlite3VdbeMemGrow() could clobber the value before it is used).
       
 54400   */
       
 54401   assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
       
 54402   pOut = &p->aMem[pOp->p3];
       
 54403   if( sqlite3VdbeMemGrow(pOut, (int)u.ao.nByte, 0) ){
       
 54404     goto no_mem;
       
 54405   }
       
 54406   u.ao.zNewRecord = (u8 *)pOut->z;
       
 54407 
       
 54408   /* Write the record */
       
 54409   u.ao.i = putVarint32(u.ao.zNewRecord, u.ao.nHdr);
       
 54410   for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
       
 54411     u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
       
 54412     u.ao.i += putVarint32(&u.ao.zNewRecord[u.ao.i], u.ao.serial_type);      /* serial type */
       
 54413   }
       
 54414   for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){  /* serial data */
       
 54415     u.ao.i += sqlite3VdbeSerialPut(&u.ao.zNewRecord[u.ao.i], (int)(u.ao.nByte-u.ao.i), u.ao.pRec,u.ao.file_format);
       
 54416   }
       
 54417   assert( u.ao.i==u.ao.nByte );
       
 54418 
       
 54419   assert( pOp->p3>0 && pOp->p3<=p->nMem );
       
 54420   pOut->n = (int)u.ao.nByte;
       
 54421   pOut->flags = MEM_Blob | MEM_Dyn;
       
 54422   pOut->xDel = 0;
       
 54423   if( u.ao.nZero ){
       
 54424     pOut->u.nZero = u.ao.nZero;
       
 54425     pOut->flags |= MEM_Zero;
       
 54426   }
       
 54427   pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
       
 54428   REGISTER_TRACE(pOp->p3, pOut);
       
 54429   UPDATE_MAX_BLOBSIZE(pOut);
       
 54430   break;
       
 54431 }
       
 54432 
       
 54433 /* Opcode: Count P1 P2 * * *
       
 54434 **
       
 54435 ** Store the number of entries (an integer value) in the table or index 
       
 54436 ** opened by cursor P1 in register P2
       
 54437 */
       
 54438 #ifndef SQLITE_OMIT_BTREECOUNT
       
 54439 case OP_Count: {         /* out2-prerelease */
       
 54440 #if 0  /* local variables moved into u.ap */
       
 54441   i64 nEntry;
       
 54442   BtCursor *pCrsr;
       
 54443 #endif /* local variables moved into u.ap */
       
 54444 
       
 54445   u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor;
       
 54446   if( u.ap.pCrsr ){
       
 54447     rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry);
       
 54448   }else{
       
 54449     u.ap.nEntry = 0;
       
 54450   }
       
 54451   pOut->flags = MEM_Int;
       
 54452   pOut->u.i = u.ap.nEntry;
       
 54453   break;
       
 54454 }
       
 54455 #endif
       
 54456 
       
 54457 /* Opcode: Savepoint P1 * * P4 *
       
 54458 **
       
 54459 ** Open, release or rollback the savepoint named by parameter P4, depending
       
 54460 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
       
 54461 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
       
 54462 */
       
 54463 case OP_Savepoint: {
       
 54464 #if 0  /* local variables moved into u.aq */
       
 54465   int p1;                         /* Value of P1 operand */
       
 54466   char *zName;                    /* Name of savepoint */
       
 54467   int nName;
       
 54468   Savepoint *pNew;
       
 54469   Savepoint *pSavepoint;
       
 54470   Savepoint *pTmp;
       
 54471   int iSavepoint;
       
 54472   int ii;
       
 54473 #endif /* local variables moved into u.aq */
       
 54474 
       
 54475   u.aq.p1 = pOp->p1;
       
 54476   u.aq.zName = pOp->p4.z;
       
 54477 
       
 54478   /* Assert that the u.aq.p1 parameter is valid. Also that if there is no open
       
 54479   ** transaction, then there cannot be any savepoints.
       
 54480   */
       
 54481   assert( db->pSavepoint==0 || db->autoCommit==0 );
       
 54482   assert( u.aq.p1==SAVEPOINT_BEGIN||u.aq.p1==SAVEPOINT_RELEASE||u.aq.p1==SAVEPOINT_ROLLBACK );
       
 54483   assert( db->pSavepoint || db->isTransactionSavepoint==0 );
       
 54484   assert( checkSavepointCount(db) );
       
 54485 
       
 54486   if( u.aq.p1==SAVEPOINT_BEGIN ){
       
 54487     if( db->writeVdbeCnt>0 ){
       
 54488       /* A new savepoint cannot be created if there are active write
       
 54489       ** statements (i.e. open read/write incremental blob handles).
       
 54490       */
       
 54491       sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
       
 54492         "SQL statements in progress");
       
 54493       rc = SQLITE_BUSY;
       
 54494     }else{
       
 54495       u.aq.nName = sqlite3Strlen30(u.aq.zName);
       
 54496 
       
 54497       /* Create a new savepoint structure. */
       
 54498       u.aq.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.aq.nName+1);
       
 54499       if( u.aq.pNew ){
       
 54500         u.aq.pNew->zName = (char *)&u.aq.pNew[1];
       
 54501         memcpy(u.aq.pNew->zName, u.aq.zName, u.aq.nName+1);
       
 54502 
       
 54503         /* If there is no open transaction, then mark this as a special
       
 54504         ** "transaction savepoint". */
       
 54505         if( db->autoCommit ){
       
 54506           db->autoCommit = 0;
       
 54507           db->isTransactionSavepoint = 1;
       
 54508         }else{
       
 54509           db->nSavepoint++;
       
 54510         }
       
 54511 
       
 54512         /* Link the new savepoint into the database handle's list. */
       
 54513         u.aq.pNew->pNext = db->pSavepoint;
       
 54514         db->pSavepoint = u.aq.pNew;
       
 54515         u.aq.pNew->nDeferredCons = db->nDeferredCons;
       
 54516       }
       
 54517     }
       
 54518   }else{
       
 54519     u.aq.iSavepoint = 0;
       
 54520 
       
 54521     /* Find the named savepoint. If there is no such savepoint, then an
       
 54522     ** an error is returned to the user.  */
       
 54523     for(
       
 54524       u.aq.pSavepoint = db->pSavepoint;
       
 54525       u.aq.pSavepoint && sqlite3StrICmp(u.aq.pSavepoint->zName, u.aq.zName);
       
 54526       u.aq.pSavepoint = u.aq.pSavepoint->pNext
       
 54527     ){
       
 54528       u.aq.iSavepoint++;
       
 54529     }
       
 54530     if( !u.aq.pSavepoint ){
       
 54531       sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.aq.zName);
       
 54532       rc = SQLITE_ERROR;
       
 54533     }else if(
       
 54534         db->writeVdbeCnt>0 || (u.aq.p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
       
 54535     ){
       
 54536       /* It is not possible to release (commit) a savepoint if there are
       
 54537       ** active write statements. It is not possible to rollback a savepoint
       
 54538       ** if there are any active statements at all.
       
 54539       */
       
 54540       sqlite3SetString(&p->zErrMsg, db,
       
 54541         "cannot %s savepoint - SQL statements in progress",
       
 54542         (u.aq.p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
       
 54543       );
       
 54544       rc = SQLITE_BUSY;
       
 54545     }else{
       
 54546 
       
 54547       /* Determine whether or not this is a transaction savepoint. If so,
       
 54548       ** and this is a RELEASE command, then the current transaction
       
 54549       ** is committed.
       
 54550       */
       
 54551       int isTransaction = u.aq.pSavepoint->pNext==0 && db->isTransactionSavepoint;
       
 54552       if( isTransaction && u.aq.p1==SAVEPOINT_RELEASE ){
       
 54553         if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
       
 54554           goto vdbe_return;
       
 54555         }
       
 54556         db->autoCommit = 1;
       
 54557         if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
       
 54558           p->pc = pc;
       
 54559           db->autoCommit = 0;
       
 54560           p->rc = rc = SQLITE_BUSY;
       
 54561           goto vdbe_return;
       
 54562         }
       
 54563         db->isTransactionSavepoint = 0;
       
 54564         rc = p->rc;
       
 54565       }else{
       
 54566         u.aq.iSavepoint = db->nSavepoint - u.aq.iSavepoint - 1;
       
 54567         for(u.aq.ii=0; u.aq.ii<db->nDb; u.aq.ii++){
       
 54568           rc = sqlite3BtreeSavepoint(db->aDb[u.aq.ii].pBt, u.aq.p1, u.aq.iSavepoint);
       
 54569           if( rc!=SQLITE_OK ){
       
 54570             goto abort_due_to_error;
       
 54571           }
       
 54572         }
       
 54573         if( u.aq.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
       
 54574           sqlite3ExpirePreparedStatements(db);
       
 54575           sqlite3ResetInternalSchema(db, 0);
       
 54576         }
       
 54577       }
       
 54578 
       
 54579       /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
       
 54580       ** savepoints nested inside of the savepoint being operated on. */
       
 54581       while( db->pSavepoint!=u.aq.pSavepoint ){
       
 54582         u.aq.pTmp = db->pSavepoint;
       
 54583         db->pSavepoint = u.aq.pTmp->pNext;
       
 54584         sqlite3DbFree(db, u.aq.pTmp);
       
 54585         db->nSavepoint--;
       
 54586       }
       
 54587 
       
 54588       /* If it is a RELEASE, then destroy the savepoint being operated on
       
 54589       ** too. If it is a ROLLBACK TO, then set the number of deferred
       
 54590       ** constraint violations present in the database to the value stored
       
 54591       ** when the savepoint was created.  */
       
 54592       if( u.aq.p1==SAVEPOINT_RELEASE ){
       
 54593         assert( u.aq.pSavepoint==db->pSavepoint );
       
 54594         db->pSavepoint = u.aq.pSavepoint->pNext;
       
 54595         sqlite3DbFree(db, u.aq.pSavepoint);
       
 54596         if( !isTransaction ){
       
 54597           db->nSavepoint--;
       
 54598         }
       
 54599       }else{
       
 54600         db->nDeferredCons = u.aq.pSavepoint->nDeferredCons;
       
 54601       }
       
 54602     }
       
 54603   }
       
 54604 
       
 54605   break;
       
 54606 }
       
 54607 
       
 54608 /* Opcode: AutoCommit P1 P2 * * *
       
 54609 **
       
 54610 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
       
 54611 ** back any currently active btree transactions. If there are any active
       
 54612 ** VMs (apart from this one), then a ROLLBACK fails.  A COMMIT fails if
       
 54613 ** there are active writing VMs or active VMs that use shared cache.
       
 54614 **
       
 54615 ** This instruction causes the VM to halt.
       
 54616 */
       
 54617 case OP_AutoCommit: {
       
 54618 #if 0  /* local variables moved into u.ar */
       
 54619   int desiredAutoCommit;
       
 54620   int iRollback;
       
 54621   int turnOnAC;
       
 54622 #endif /* local variables moved into u.ar */
       
 54623 
       
 54624   u.ar.desiredAutoCommit = pOp->p1;
       
 54625   u.ar.iRollback = pOp->p2;
       
 54626   u.ar.turnOnAC = u.ar.desiredAutoCommit && !db->autoCommit;
       
 54627   assert( u.ar.desiredAutoCommit==1 || u.ar.desiredAutoCommit==0 );
       
 54628   assert( u.ar.desiredAutoCommit==1 || u.ar.iRollback==0 );
       
 54629   assert( db->activeVdbeCnt>0 );  /* At least this one VM is active */
       
 54630 
       
 54631   if( u.ar.turnOnAC && u.ar.iRollback && db->activeVdbeCnt>1 ){
       
 54632     /* If this instruction implements a ROLLBACK and other VMs are
       
 54633     ** still running, and a transaction is active, return an error indicating
       
 54634     ** that the other VMs must complete first.
       
 54635     */
       
 54636     sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
       
 54637         "SQL statements in progress");
       
 54638     rc = SQLITE_BUSY;
       
 54639   }else if( u.ar.turnOnAC && !u.ar.iRollback && db->writeVdbeCnt>0 ){
       
 54640     /* If this instruction implements a COMMIT and other VMs are writing
       
 54641     ** return an error indicating that the other VMs must complete first.
       
 54642     */
       
 54643     sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
       
 54644         "SQL statements in progress");
       
 54645     rc = SQLITE_BUSY;
       
 54646   }else if( u.ar.desiredAutoCommit!=db->autoCommit ){
       
 54647     if( u.ar.iRollback ){
       
 54648       assert( u.ar.desiredAutoCommit==1 );
       
 54649       sqlite3RollbackAll(db);
       
 54650       db->autoCommit = 1;
       
 54651     }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
       
 54652       goto vdbe_return;
       
 54653     }else{
       
 54654       db->autoCommit = (u8)u.ar.desiredAutoCommit;
       
 54655       if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
       
 54656         p->pc = pc;
       
 54657         db->autoCommit = (u8)(1-u.ar.desiredAutoCommit);
       
 54658         p->rc = rc = SQLITE_BUSY;
       
 54659         goto vdbe_return;
       
 54660       }
       
 54661     }
       
 54662     assert( db->nStatement==0 );
       
 54663     sqlite3CloseSavepoints(db);
       
 54664     if( p->rc==SQLITE_OK ){
       
 54665       rc = SQLITE_DONE;
       
 54666     }else{
       
 54667       rc = SQLITE_ERROR;
       
 54668     }
       
 54669     goto vdbe_return;
       
 54670   }else{
       
 54671     sqlite3SetString(&p->zErrMsg, db,
       
 54672         (!u.ar.desiredAutoCommit)?"cannot start a transaction within a transaction":(
       
 54673         (u.ar.iRollback)?"cannot rollback - no transaction is active":
       
 54674                    "cannot commit - no transaction is active"));
       
 54675 
       
 54676     rc = SQLITE_ERROR;
       
 54677   }
       
 54678   break;
       
 54679 }
       
 54680 
       
 54681 /* Opcode: Transaction P1 P2 * * *
       
 54682 **
       
 54683 ** Begin a transaction.  The transaction ends when a Commit or Rollback
       
 54684 ** opcode is encountered.  Depending on the ON CONFLICT setting, the
       
 54685 ** transaction might also be rolled back if an error is encountered.
       
 54686 **
       
 54687 ** P1 is the index of the database file on which the transaction is
       
 54688 ** started.  Index 0 is the main database file and index 1 is the
       
 54689 ** file used for temporary tables.  Indices of 2 or more are used for
       
 54690 ** attached databases.
       
 54691 **
       
 54692 ** If P2 is non-zero, then a write-transaction is started.  A RESERVED lock is
       
 54693 ** obtained on the database file when a write-transaction is started.  No
       
 54694 ** other process can start another write transaction while this transaction is
       
 54695 ** underway.  Starting a write transaction also creates a rollback journal. A
       
 54696 ** write transaction must be started before any changes can be made to the
       
 54697 ** database.  If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
       
 54698 ** on the file.
       
 54699 **
       
 54700 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
       
 54701 ** true (this flag is set if the Vdbe may modify more than one row and may
       
 54702 ** throw an ABORT exception), a statement transaction may also be opened.
       
 54703 ** More specifically, a statement transaction is opened iff the database
       
 54704 ** connection is currently not in autocommit mode, or if there are other
       
 54705 ** active statements. A statement transaction allows the affects of this
       
 54706 ** VDBE to be rolled back after an error without having to roll back the
       
 54707 ** entire transaction. If no error is encountered, the statement transaction
       
 54708 ** will automatically commit when the VDBE halts.
       
 54709 **
       
 54710 ** If P2 is zero, then a read-lock is obtained on the database file.
       
 54711 */
       
 54712 case OP_Transaction: {
       
 54713 #if 0  /* local variables moved into u.as */
       
 54714   Btree *pBt;
       
 54715 #endif /* local variables moved into u.as */
       
 54716 
       
 54717   assert( pOp->p1>=0 && pOp->p1<db->nDb );
       
 54718   assert( (p->btreeMask & (1<<pOp->p1))!=0 );
       
 54719   u.as.pBt = db->aDb[pOp->p1].pBt;
       
 54720 
       
 54721   if( u.as.pBt ){
       
 54722     rc = sqlite3BtreeBeginTrans(u.as.pBt, pOp->p2);
       
 54723     if( rc==SQLITE_BUSY ){
       
 54724       p->pc = pc;
       
 54725       p->rc = rc = SQLITE_BUSY;
       
 54726       goto vdbe_return;
       
 54727     }
       
 54728     if( rc!=SQLITE_OK ){
       
 54729       goto abort_due_to_error;
       
 54730     }
       
 54731 
       
 54732     if( pOp->p2 && p->usesStmtJournal
       
 54733      && (db->autoCommit==0 || db->activeVdbeCnt>1)
       
 54734     ){
       
 54735       assert( sqlite3BtreeIsInTrans(u.as.pBt) );
       
 54736       if( p->iStatement==0 ){
       
 54737         assert( db->nStatement>=0 && db->nSavepoint>=0 );
       
 54738         db->nStatement++;
       
 54739         p->iStatement = db->nSavepoint + db->nStatement;
       
 54740       }
       
 54741       rc = sqlite3BtreeBeginStmt(u.as.pBt, p->iStatement);
       
 54742 
       
 54743       /* Store the current value of the database handles deferred constraint
       
 54744       ** counter. If the statement transaction needs to be rolled back,
       
 54745       ** the value of this counter needs to be restored too.  */
       
 54746       p->nStmtDefCons = db->nDeferredCons;
       
 54747     }
       
 54748   }
       
 54749   break;
       
 54750 }
       
 54751 
       
 54752 /* Opcode: ReadCookie P1 P2 P3 * *
       
 54753 **
       
 54754 ** Read cookie number P3 from database P1 and write it into register P2.
       
 54755 ** P3==1 is the schema version.  P3==2 is the database format.
       
 54756 ** P3==3 is the recommended pager cache size, and so forth.  P1==0 is
       
 54757 ** the main database file and P1==1 is the database file used to store
       
 54758 ** temporary tables.
       
 54759 **
       
 54760 ** There must be a read-lock on the database (either a transaction
       
 54761 ** must be started or there must be an open cursor) before
       
 54762 ** executing this instruction.
       
 54763 */
       
 54764 case OP_ReadCookie: {               /* out2-prerelease */
       
 54765 #if 0  /* local variables moved into u.at */
       
 54766   int iMeta;
       
 54767   int iDb;
       
 54768   int iCookie;
       
 54769 #endif /* local variables moved into u.at */
       
 54770 
       
 54771   u.at.iDb = pOp->p1;
       
 54772   u.at.iCookie = pOp->p3;
       
 54773   assert( pOp->p3<SQLITE_N_BTREE_META );
       
 54774   assert( u.at.iDb>=0 && u.at.iDb<db->nDb );
       
 54775   assert( db->aDb[u.at.iDb].pBt!=0 );
       
 54776   assert( (p->btreeMask & (1<<u.at.iDb))!=0 );
       
 54777 
       
 54778   sqlite3BtreeGetMeta(db->aDb[u.at.iDb].pBt, u.at.iCookie, (u32 *)&u.at.iMeta);
       
 54779   pOut->u.i = u.at.iMeta;
       
 54780   MemSetTypeFlag(pOut, MEM_Int);
       
 54781   break;
       
 54782 }
       
 54783 
       
 54784 /* Opcode: SetCookie P1 P2 P3 * *
       
 54785 **
       
 54786 ** Write the content of register P3 (interpreted as an integer)
       
 54787 ** into cookie number P2 of database P1.  P2==1 is the schema version.  
       
 54788 ** P2==2 is the database format. P2==3 is the recommended pager cache 
       
 54789 ** size, and so forth.  P1==0 is the main database file and P1==1 is the 
       
 54790 ** database file used to store temporary tables.
       
 54791 **
       
 54792 ** A transaction must be started before executing this opcode.
       
 54793 */
       
 54794 case OP_SetCookie: {       /* in3 */
       
 54795 #if 0  /* local variables moved into u.au */
       
 54796   Db *pDb;
       
 54797 #endif /* local variables moved into u.au */
       
 54798   assert( pOp->p2<SQLITE_N_BTREE_META );
       
 54799   assert( pOp->p1>=0 && pOp->p1<db->nDb );
       
 54800   assert( (p->btreeMask & (1<<pOp->p1))!=0 );
       
 54801   u.au.pDb = &db->aDb[pOp->p1];
       
 54802   assert( u.au.pDb->pBt!=0 );
       
 54803   sqlite3VdbeMemIntegerify(pIn3);
       
 54804   /* See note about index shifting on OP_ReadCookie */
       
 54805   rc = sqlite3BtreeUpdateMeta(u.au.pDb->pBt, pOp->p2, (int)pIn3->u.i);
       
 54806   if( pOp->p2==BTREE_SCHEMA_VERSION ){
       
 54807     /* When the schema cookie changes, record the new cookie internally */
       
 54808     u.au.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
       
 54809     db->flags |= SQLITE_InternChanges;
       
 54810   }else if( pOp->p2==BTREE_FILE_FORMAT ){
       
 54811     /* Record changes in the file format */
       
 54812     u.au.pDb->pSchema->file_format = (u8)pIn3->u.i;
       
 54813   }
       
 54814   if( pOp->p1==1 ){
       
 54815     /* Invalidate all prepared statements whenever the TEMP database
       
 54816     ** schema is changed.  Ticket #1644 */
       
 54817     sqlite3ExpirePreparedStatements(db);
       
 54818   }
       
 54819   break;
       
 54820 }
       
 54821 
       
 54822 /* Opcode: VerifyCookie P1 P2 *
       
 54823 **
       
 54824 ** Check the value of global database parameter number 0 (the
       
 54825 ** schema version) and make sure it is equal to P2.  
       
 54826 ** P1 is the database number which is 0 for the main database file
       
 54827 ** and 1 for the file holding temporary tables and some higher number
       
 54828 ** for auxiliary databases.
       
 54829 **
       
 54830 ** The cookie changes its value whenever the database schema changes.
       
 54831 ** This operation is used to detect when that the cookie has changed
       
 54832 ** and that the current process needs to reread the schema.
       
 54833 **
       
 54834 ** Either a transaction needs to have been started or an OP_Open needs
       
 54835 ** to be executed (to establish a read lock) before this opcode is
       
 54836 ** invoked.
       
 54837 */
       
 54838 case OP_VerifyCookie: {
       
 54839 #if 0  /* local variables moved into u.av */
       
 54840   int iMeta;
       
 54841   Btree *pBt;
       
 54842 #endif /* local variables moved into u.av */
       
 54843   assert( pOp->p1>=0 && pOp->p1<db->nDb );
       
 54844   assert( (p->btreeMask & (1<<pOp->p1))!=0 );
       
 54845   u.av.pBt = db->aDb[pOp->p1].pBt;
       
 54846   if( u.av.pBt ){
       
 54847     sqlite3BtreeGetMeta(u.av.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.av.iMeta);
       
 54848   }else{
       
 54849     u.av.iMeta = 0;
       
 54850   }
       
 54851   if( u.av.iMeta!=pOp->p2 ){
       
 54852     sqlite3DbFree(db, p->zErrMsg);
       
 54853     p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
       
 54854     /* If the schema-cookie from the database file matches the cookie
       
 54855     ** stored with the in-memory representation of the schema, do
       
 54856     ** not reload the schema from the database file.
       
 54857     **
       
 54858     ** If virtual-tables are in use, this is not just an optimization.
       
 54859     ** Often, v-tables store their data in other SQLite tables, which
       
 54860     ** are queried from within xNext() and other v-table methods using
       
 54861     ** prepared queries. If such a query is out-of-date, we do not want to
       
 54862     ** discard the database schema, as the user code implementing the
       
 54863     ** v-table would have to be ready for the sqlite3_vtab structure itself
       
 54864     ** to be invalidated whenever sqlite3_step() is called from within
       
 54865     ** a v-table method.
       
 54866     */
       
 54867     if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.av.iMeta ){
       
 54868       sqlite3ResetInternalSchema(db, pOp->p1);
       
 54869     }
       
 54870 
       
 54871     sqlite3ExpirePreparedStatements(db);
       
 54872     rc = SQLITE_SCHEMA;
       
 54873   }
       
 54874   break;
       
 54875 }
       
 54876 
       
 54877 /* Opcode: OpenRead P1 P2 P3 P4 P5
       
 54878 **
       
 54879 ** Open a read-only cursor for the database table whose root page is
       
 54880 ** P2 in a database file.  The database file is determined by P3. 
       
 54881 ** P3==0 means the main database, P3==1 means the database used for 
       
 54882 ** temporary tables, and P3>1 means used the corresponding attached
       
 54883 ** database.  Give the new cursor an identifier of P1.  The P1
       
 54884 ** values need not be contiguous but all P1 values should be small integers.
       
 54885 ** It is an error for P1 to be negative.
       
 54886 **
       
 54887 ** If P5!=0 then use the content of register P2 as the root page, not
       
 54888 ** the value of P2 itself.
       
 54889 **
       
 54890 ** There will be a read lock on the database whenever there is an
       
 54891 ** open cursor.  If the database was unlocked prior to this instruction
       
 54892 ** then a read lock is acquired as part of this instruction.  A read
       
 54893 ** lock allows other processes to read the database but prohibits
       
 54894 ** any other process from modifying the database.  The read lock is
       
 54895 ** released when all cursors are closed.  If this instruction attempts
       
 54896 ** to get a read lock but fails, the script terminates with an
       
 54897 ** SQLITE_BUSY error code.
       
 54898 **
       
 54899 ** The P4 value may be either an integer (P4_INT32) or a pointer to
       
 54900 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
       
 54901 ** structure, then said structure defines the content and collating 
       
 54902 ** sequence of the index being opened. Otherwise, if P4 is an integer 
       
 54903 ** value, it is set to the number of columns in the table.
       
 54904 **
       
 54905 ** See also OpenWrite.
       
 54906 */
       
 54907 /* Opcode: OpenWrite P1 P2 P3 P4 P5
       
 54908 **
       
 54909 ** Open a read/write cursor named P1 on the table or index whose root
       
 54910 ** page is P2.  Or if P5!=0 use the content of register P2 to find the
       
 54911 ** root page.
       
 54912 **
       
 54913 ** The P4 value may be either an integer (P4_INT32) or a pointer to
       
 54914 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
       
 54915 ** structure, then said structure defines the content and collating 
       
 54916 ** sequence of the index being opened. Otherwise, if P4 is an integer 
       
 54917 ** value, it is set to the number of columns in the table, or to the
       
 54918 ** largest index of any column of the table that is actually used.
       
 54919 **
       
 54920 ** This instruction works just like OpenRead except that it opens the cursor
       
 54921 ** in read/write mode.  For a given table, there can be one or more read-only
       
 54922 ** cursors or a single read/write cursor but not both.
       
 54923 **
       
 54924 ** See also OpenRead.
       
 54925 */
       
 54926 case OP_OpenRead:
       
 54927 case OP_OpenWrite: {
       
 54928 #if 0  /* local variables moved into u.aw */
       
 54929   int nField;
       
 54930   KeyInfo *pKeyInfo;
       
 54931   int p2;
       
 54932   int iDb;
       
 54933   int wrFlag;
       
 54934   Btree *pX;
       
 54935   VdbeCursor *pCur;
       
 54936   Db *pDb;
       
 54937 #endif /* local variables moved into u.aw */
       
 54938 
       
 54939   u.aw.nField = 0;
       
 54940   u.aw.pKeyInfo = 0;
       
 54941   u.aw.p2 = pOp->p2;
       
 54942   u.aw.iDb = pOp->p3;
       
 54943   assert( u.aw.iDb>=0 && u.aw.iDb<db->nDb );
       
 54944   assert( (p->btreeMask & (1<<u.aw.iDb))!=0 );
       
 54945   u.aw.pDb = &db->aDb[u.aw.iDb];
       
 54946   u.aw.pX = u.aw.pDb->pBt;
       
 54947   assert( u.aw.pX!=0 );
       
 54948   if( pOp->opcode==OP_OpenWrite ){
       
 54949     u.aw.wrFlag = 1;
       
 54950     if( u.aw.pDb->pSchema->file_format < p->minWriteFileFormat ){
       
 54951       p->minWriteFileFormat = u.aw.pDb->pSchema->file_format;
       
 54952     }
       
 54953   }else{
       
 54954     u.aw.wrFlag = 0;
       
 54955   }
       
 54956   if( pOp->p5 ){
       
 54957     assert( u.aw.p2>0 );
       
 54958     assert( u.aw.p2<=p->nMem );
       
 54959     pIn2 = &p->aMem[u.aw.p2];
       
 54960     sqlite3VdbeMemIntegerify(pIn2);
       
 54961     u.aw.p2 = (int)pIn2->u.i;
       
 54962     /* The u.aw.p2 value always comes from a prior OP_CreateTable opcode and
       
 54963     ** that opcode will always set the u.aw.p2 value to 2 or more or else fail.
       
 54964     ** If there were a failure, the prepared statement would have halted
       
 54965     ** before reaching this instruction. */
       
 54966     if( NEVER(u.aw.p2<2) ) {
       
 54967       rc = SQLITE_CORRUPT_BKPT;
       
 54968       goto abort_due_to_error;
       
 54969     }
       
 54970   }
       
 54971   if( pOp->p4type==P4_KEYINFO ){
       
 54972     u.aw.pKeyInfo = pOp->p4.pKeyInfo;
       
 54973     u.aw.pKeyInfo->enc = ENC(p->db);
       
 54974     u.aw.nField = u.aw.pKeyInfo->nField+1;
       
 54975   }else if( pOp->p4type==P4_INT32 ){
       
 54976     u.aw.nField = pOp->p4.i;
       
 54977   }
       
 54978   assert( pOp->p1>=0 );
       
 54979   u.aw.pCur = allocateCursor(p, pOp->p1, u.aw.nField, u.aw.iDb, 1);
       
 54980   if( u.aw.pCur==0 ) goto no_mem;
       
 54981   u.aw.pCur->nullRow = 1;
       
 54982   rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor);
       
 54983   u.aw.pCur->pKeyInfo = u.aw.pKeyInfo;
       
 54984 
       
 54985   /* Since it performs no memory allocation or IO, the only values that
       
 54986   ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK.
       
 54987   ** SQLITE_EMPTY is only returned when attempting to open the table
       
 54988   ** rooted at page 1 of a zero-byte database.  */
       
 54989   assert( rc==SQLITE_EMPTY || rc==SQLITE_OK );
       
 54990   if( rc==SQLITE_EMPTY ){
       
 54991     u.aw.pCur->pCursor = 0;
       
 54992     rc = SQLITE_OK;
       
 54993   }
       
 54994 
       
 54995   /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
       
 54996   ** SQLite used to check if the root-page flags were sane at this point
       
 54997   ** and report database corruption if they were not, but this check has
       
 54998   ** since moved into the btree layer.  */
       
 54999   u.aw.pCur->isTable = pOp->p4type!=P4_KEYINFO;
       
 55000   u.aw.pCur->isIndex = !u.aw.pCur->isTable;
       
 55001   break;
       
 55002 }
       
 55003 
       
 55004 /* Opcode: OpenEphemeral P1 P2 * P4 *
       
 55005 **
       
 55006 ** Open a new cursor P1 to a transient table.
       
 55007 ** The cursor is always opened read/write even if 
       
 55008 ** the main database is read-only.  The transient or virtual
       
 55009 ** table is deleted automatically when the cursor is closed.
       
 55010 **
       
 55011 ** P2 is the number of columns in the virtual table.
       
 55012 ** The cursor points to a BTree table if P4==0 and to a BTree index
       
 55013 ** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
       
 55014 ** that defines the format of keys in the index.
       
 55015 **
       
 55016 ** This opcode was once called OpenTemp.  But that created
       
 55017 ** confusion because the term "temp table", might refer either
       
 55018 ** to a TEMP table at the SQL level, or to a table opened by
       
 55019 ** this opcode.  Then this opcode was call OpenVirtual.  But
       
 55020 ** that created confusion with the whole virtual-table idea.
       
 55021 */
       
 55022 case OP_OpenEphemeral: {
       
 55023 #if 0  /* local variables moved into u.ax */
       
 55024   VdbeCursor *pCx;
       
 55025 #endif /* local variables moved into u.ax */
       
 55026   static const int openFlags =
       
 55027       SQLITE_OPEN_READWRITE |
       
 55028       SQLITE_OPEN_CREATE |
       
 55029       SQLITE_OPEN_EXCLUSIVE |
       
 55030       SQLITE_OPEN_DELETEONCLOSE |
       
 55031       SQLITE_OPEN_TRANSIENT_DB;
       
 55032 
       
 55033   assert( pOp->p1>=0 );
       
 55034   u.ax.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
       
 55035   if( u.ax.pCx==0 ) goto no_mem;
       
 55036   u.ax.pCx->nullRow = 1;
       
 55037   rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags,
       
 55038                            &u.ax.pCx->pBt);
       
 55039   if( rc==SQLITE_OK ){
       
 55040     rc = sqlite3BtreeBeginTrans(u.ax.pCx->pBt, 1);
       
 55041   }
       
 55042   if( rc==SQLITE_OK ){
       
 55043     /* If a transient index is required, create it by calling
       
 55044     ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
       
 55045     ** opening it. If a transient table is required, just use the
       
 55046     ** automatically created table with root-page 1 (an INTKEY table).
       
 55047     */
       
 55048     if( pOp->p4.pKeyInfo ){
       
 55049       int pgno;
       
 55050       assert( pOp->p4type==P4_KEYINFO );
       
 55051       rc = sqlite3BtreeCreateTable(u.ax.pCx->pBt, &pgno, BTREE_ZERODATA);
       
 55052       if( rc==SQLITE_OK ){
       
 55053         assert( pgno==MASTER_ROOT+1 );
       
 55054         rc = sqlite3BtreeCursor(u.ax.pCx->pBt, pgno, 1,
       
 55055                                 (KeyInfo*)pOp->p4.z, u.ax.pCx->pCursor);
       
 55056         u.ax.pCx->pKeyInfo = pOp->p4.pKeyInfo;
       
 55057         u.ax.pCx->pKeyInfo->enc = ENC(p->db);
       
 55058       }
       
 55059       u.ax.pCx->isTable = 0;
       
 55060     }else{
       
 55061       rc = sqlite3BtreeCursor(u.ax.pCx->pBt, MASTER_ROOT, 1, 0, u.ax.pCx->pCursor);
       
 55062       u.ax.pCx->isTable = 1;
       
 55063     }
       
 55064   }
       
 55065   u.ax.pCx->isIndex = !u.ax.pCx->isTable;
       
 55066   break;
       
 55067 }
       
 55068 
       
 55069 /* Opcode: OpenPseudo P1 P2 P3 * *
       
 55070 **
       
 55071 ** Open a new cursor that points to a fake table that contains a single
       
 55072 ** row of data.  The content of that one row in the content of memory
       
 55073 ** register P2.  In other words, cursor P1 becomes an alias for the 
       
 55074 ** MEM_Blob content contained in register P2.
       
 55075 **
       
 55076 ** A pseudo-table created by this opcode is used to hold the a single
       
 55077 ** row output from the sorter so that the row can be decomposed into
       
 55078 ** individual columns using the OP_Column opcode.  The OP_Column opcode
       
 55079 ** is the only cursor opcode that works with a pseudo-table.
       
 55080 **
       
 55081 ** P3 is the number of fields in the records that will be stored by
       
 55082 ** the pseudo-table.
       
 55083 */
       
 55084 case OP_OpenPseudo: {
       
 55085 #if 0  /* local variables moved into u.ay */
       
 55086   VdbeCursor *pCx;
       
 55087 #endif /* local variables moved into u.ay */
       
 55088 
       
 55089   assert( pOp->p1>=0 );
       
 55090   u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
       
 55091   if( u.ay.pCx==0 ) goto no_mem;
       
 55092   u.ay.pCx->nullRow = 1;
       
 55093   u.ay.pCx->pseudoTableReg = pOp->p2;
       
 55094   u.ay.pCx->isTable = 1;
       
 55095   u.ay.pCx->isIndex = 0;
       
 55096   break;
       
 55097 }
       
 55098 
       
 55099 /* Opcode: Close P1 * * * *
       
 55100 **
       
 55101 ** Close a cursor previously opened as P1.  If P1 is not
       
 55102 ** currently open, this instruction is a no-op.
       
 55103 */
       
 55104 case OP_Close: {
       
 55105   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 55106   sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
       
 55107   p->apCsr[pOp->p1] = 0;
       
 55108   break;
       
 55109 }
       
 55110 
       
 55111 /* Opcode: SeekGe P1 P2 P3 P4 *
       
 55112 **
       
 55113 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
       
 55114 ** use the value in register P3 as the key.  If cursor P1 refers 
       
 55115 ** to an SQL index, then P3 is the first in an array of P4 registers 
       
 55116 ** that are used as an unpacked index key. 
       
 55117 **
       
 55118 ** Reposition cursor P1 so that  it points to the smallest entry that 
       
 55119 ** is greater than or equal to the key value. If there are no records 
       
 55120 ** greater than or equal to the key and P2 is not zero, then jump to P2.
       
 55121 **
       
 55122 ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
       
 55123 */
       
 55124 /* Opcode: SeekGt P1 P2 P3 P4 *
       
 55125 **
       
 55126 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
       
 55127 ** use the value in register P3 as a key. If cursor P1 refers 
       
 55128 ** to an SQL index, then P3 is the first in an array of P4 registers 
       
 55129 ** that are used as an unpacked index key. 
       
 55130 **
       
 55131 ** Reposition cursor P1 so that  it points to the smallest entry that 
       
 55132 ** is greater than the key value. If there are no records greater than 
       
 55133 ** the key and P2 is not zero, then jump to P2.
       
 55134 **
       
 55135 ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
       
 55136 */
       
 55137 /* Opcode: SeekLt P1 P2 P3 P4 * 
       
 55138 **
       
 55139 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
       
 55140 ** use the value in register P3 as a key. If cursor P1 refers 
       
 55141 ** to an SQL index, then P3 is the first in an array of P4 registers 
       
 55142 ** that are used as an unpacked index key. 
       
 55143 **
       
 55144 ** Reposition cursor P1 so that  it points to the largest entry that 
       
 55145 ** is less than the key value. If there are no records less than 
       
 55146 ** the key and P2 is not zero, then jump to P2.
       
 55147 **
       
 55148 ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
       
 55149 */
       
 55150 /* Opcode: SeekLe P1 P2 P3 P4 *
       
 55151 **
       
 55152 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
       
 55153 ** use the value in register P3 as a key. If cursor P1 refers 
       
 55154 ** to an SQL index, then P3 is the first in an array of P4 registers 
       
 55155 ** that are used as an unpacked index key. 
       
 55156 **
       
 55157 ** Reposition cursor P1 so that it points to the largest entry that 
       
 55158 ** is less than or equal to the key value. If there are no records 
       
 55159 ** less than or equal to the key and P2 is not zero, then jump to P2.
       
 55160 **
       
 55161 ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
       
 55162 */
       
 55163 case OP_SeekLt:         /* jump, in3 */
       
 55164 case OP_SeekLe:         /* jump, in3 */
       
 55165 case OP_SeekGe:         /* jump, in3 */
       
 55166 case OP_SeekGt: {       /* jump, in3 */
       
 55167 #if 0  /* local variables moved into u.az */
       
 55168   int res;
       
 55169   int oc;
       
 55170   VdbeCursor *pC;
       
 55171   UnpackedRecord r;
       
 55172   int nField;
       
 55173   i64 iKey;      /* The rowid we are to seek to */
       
 55174 #endif /* local variables moved into u.az */
       
 55175 
       
 55176   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 55177   assert( pOp->p2!=0 );
       
 55178   u.az.pC = p->apCsr[pOp->p1];
       
 55179   assert( u.az.pC!=0 );
       
 55180   assert( u.az.pC->pseudoTableReg==0 );
       
 55181   if( u.az.pC->pCursor!=0 ){
       
 55182     u.az.oc = pOp->opcode;
       
 55183     u.az.pC->nullRow = 0;
       
 55184     if( u.az.pC->isTable ){
       
 55185       /* The input value in P3 might be of any type: integer, real, string,
       
 55186       ** blob, or NULL.  But it needs to be an integer before we can do
       
 55187       ** the seek, so covert it. */
       
 55188       applyNumericAffinity(pIn3);
       
 55189       u.az.iKey = sqlite3VdbeIntValue(pIn3);
       
 55190       u.az.pC->rowidIsValid = 0;
       
 55191 
       
 55192       /* If the P3 value could not be converted into an integer without
       
 55193       ** loss of information, then special processing is required... */
       
 55194       if( (pIn3->flags & MEM_Int)==0 ){
       
 55195         if( (pIn3->flags & MEM_Real)==0 ){
       
 55196           /* If the P3 value cannot be converted into any kind of a number,
       
 55197           ** then the seek is not possible, so jump to P2 */
       
 55198           pc = pOp->p2 - 1;
       
 55199           break;
       
 55200         }
       
 55201         /* If we reach this point, then the P3 value must be a floating
       
 55202         ** point number. */
       
 55203         assert( (pIn3->flags & MEM_Real)!=0 );
       
 55204 
       
 55205         if( u.az.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.az.iKey || pIn3->r>0) ){
       
 55206           /* The P3 value is too large in magnitude to be expressed as an
       
 55207           ** integer. */
       
 55208           u.az.res = 1;
       
 55209           if( pIn3->r<0 ){
       
 55210             if( u.az.oc==OP_SeekGt || u.az.oc==OP_SeekGe ){
       
 55211               rc = sqlite3BtreeFirst(u.az.pC->pCursor, &u.az.res);
       
 55212               if( rc!=SQLITE_OK ) goto abort_due_to_error;
       
 55213             }
       
 55214           }else{
       
 55215             if( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekLe ){
       
 55216               rc = sqlite3BtreeLast(u.az.pC->pCursor, &u.az.res);
       
 55217               if( rc!=SQLITE_OK ) goto abort_due_to_error;
       
 55218             }
       
 55219           }
       
 55220           if( u.az.res ){
       
 55221             pc = pOp->p2 - 1;
       
 55222           }
       
 55223           break;
       
 55224         }else if( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekGe ){
       
 55225           /* Use the ceiling() function to convert real->int */
       
 55226           if( pIn3->r > (double)u.az.iKey ) u.az.iKey++;
       
 55227         }else{
       
 55228           /* Use the floor() function to convert real->int */
       
 55229           assert( u.az.oc==OP_SeekLe || u.az.oc==OP_SeekGt );
       
 55230           if( pIn3->r < (double)u.az.iKey ) u.az.iKey--;
       
 55231         }
       
 55232       }
       
 55233       rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, 0, (u64)u.az.iKey, 0, &u.az.res);
       
 55234       if( rc!=SQLITE_OK ){
       
 55235         goto abort_due_to_error;
       
 55236       }
       
 55237       if( u.az.res==0 ){
       
 55238         u.az.pC->rowidIsValid = 1;
       
 55239         u.az.pC->lastRowid = u.az.iKey;
       
 55240       }
       
 55241     }else{
       
 55242       u.az.nField = pOp->p4.i;
       
 55243       assert( pOp->p4type==P4_INT32 );
       
 55244       assert( u.az.nField>0 );
       
 55245       u.az.r.pKeyInfo = u.az.pC->pKeyInfo;
       
 55246       u.az.r.nField = (u16)u.az.nField;
       
 55247       if( u.az.oc==OP_SeekGt || u.az.oc==OP_SeekLe ){
       
 55248         u.az.r.flags = UNPACKED_INCRKEY;
       
 55249       }else{
       
 55250         u.az.r.flags = 0;
       
 55251       }
       
 55252       u.az.r.aMem = &p->aMem[pOp->p3];
       
 55253       rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, &u.az.r, 0, 0, &u.az.res);
       
 55254       if( rc!=SQLITE_OK ){
       
 55255         goto abort_due_to_error;
       
 55256       }
       
 55257       u.az.pC->rowidIsValid = 0;
       
 55258     }
       
 55259     u.az.pC->deferredMoveto = 0;
       
 55260     u.az.pC->cacheStatus = CACHE_STALE;
       
 55261 #ifdef SQLITE_TEST
       
 55262     sqlite3_search_count++;
       
 55263 #endif
       
 55264     if( u.az.oc==OP_SeekGe || u.az.oc==OP_SeekGt ){
       
 55265       if( u.az.res<0 || (u.az.res==0 && u.az.oc==OP_SeekGt) ){
       
 55266         rc = sqlite3BtreeNext(u.az.pC->pCursor, &u.az.res);
       
 55267         if( rc!=SQLITE_OK ) goto abort_due_to_error;
       
 55268         u.az.pC->rowidIsValid = 0;
       
 55269       }else{
       
 55270         u.az.res = 0;
       
 55271       }
       
 55272     }else{
       
 55273       assert( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekLe );
       
 55274       if( u.az.res>0 || (u.az.res==0 && u.az.oc==OP_SeekLt) ){
       
 55275         rc = sqlite3BtreePrevious(u.az.pC->pCursor, &u.az.res);
       
 55276         if( rc!=SQLITE_OK ) goto abort_due_to_error;
       
 55277         u.az.pC->rowidIsValid = 0;
       
 55278       }else{
       
 55279         /* u.az.res might be negative because the table is empty.  Check to
       
 55280         ** see if this is the case.
       
 55281         */
       
 55282         u.az.res = sqlite3BtreeEof(u.az.pC->pCursor);
       
 55283       }
       
 55284     }
       
 55285     assert( pOp->p2>0 );
       
 55286     if( u.az.res ){
       
 55287       pc = pOp->p2 - 1;
       
 55288     }
       
 55289   }else{
       
 55290     /* This happens when attempting to open the sqlite3_master table
       
 55291     ** for read access returns SQLITE_EMPTY. In this case always
       
 55292     ** take the jump (since there are no records in the table).
       
 55293     */
       
 55294     pc = pOp->p2 - 1;
       
 55295   }
       
 55296   break;
       
 55297 }
       
 55298 
       
 55299 /* Opcode: Seek P1 P2 * * *
       
 55300 **
       
 55301 ** P1 is an open table cursor and P2 is a rowid integer.  Arrange
       
 55302 ** for P1 to move so that it points to the rowid given by P2.
       
 55303 **
       
 55304 ** This is actually a deferred seek.  Nothing actually happens until
       
 55305 ** the cursor is used to read a record.  That way, if no reads
       
 55306 ** occur, no unnecessary I/O happens.
       
 55307 */
       
 55308 case OP_Seek: {    /* in2 */
       
 55309 #if 0  /* local variables moved into u.ba */
       
 55310   VdbeCursor *pC;
       
 55311 #endif /* local variables moved into u.ba */
       
 55312 
       
 55313   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 55314   u.ba.pC = p->apCsr[pOp->p1];
       
 55315   assert( u.ba.pC!=0 );
       
 55316   if( ALWAYS(u.ba.pC->pCursor!=0) ){
       
 55317     assert( u.ba.pC->isTable );
       
 55318     u.ba.pC->nullRow = 0;
       
 55319     u.ba.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
       
 55320     u.ba.pC->rowidIsValid = 0;
       
 55321     u.ba.pC->deferredMoveto = 1;
       
 55322   }
       
 55323   break;
       
 55324 }
       
 55325   
       
 55326 
       
 55327 /* Opcode: Found P1 P2 P3 * *
       
 55328 **
       
 55329 ** Register P3 holds a blob constructed by MakeRecord.  P1 is an index.
       
 55330 ** If an entry that matches the value in register p3 exists in P1 then
       
 55331 ** jump to P2.  If the P3 value does not match any entry in P1
       
 55332 ** then fall thru.  The P1 cursor is left pointing at the matching entry
       
 55333 ** if it exists.
       
 55334 **
       
 55335 ** This instruction is used to implement the IN operator where the
       
 55336 ** left-hand side is a SELECT statement.  P1 may be a true index, or it
       
 55337 ** may be a temporary index that holds the results of the SELECT
       
 55338 ** statement.   This instruction is also used to implement the
       
 55339 ** DISTINCT keyword in SELECT statements.
       
 55340 **
       
 55341 ** This instruction checks if index P1 contains a record for which 
       
 55342 ** the first N serialized values exactly match the N serialized values
       
 55343 ** in the record in register P3, where N is the total number of values in
       
 55344 ** the P3 record (the P3 record is a prefix of the P1 record). 
       
 55345 **
       
 55346 ** See also: NotFound, IsUnique, NotExists
       
 55347 */
       
 55348 /* Opcode: NotFound P1 P2 P3 * *
       
 55349 **
       
 55350 ** Register P3 holds a blob constructed by MakeRecord.  P1 is
       
 55351 ** an index.  If no entry exists in P1 that matches the blob then jump
       
 55352 ** to P2.  If an entry does existing, fall through.  The cursor is left
       
 55353 ** pointing to the entry that matches.
       
 55354 **
       
 55355 ** See also: Found, NotExists, IsUnique
       
 55356 */
       
 55357 case OP_NotFound:       /* jump, in3 */
       
 55358 case OP_Found: {        /* jump, in3 */
       
 55359 #if 0  /* local variables moved into u.bb */
       
 55360   int alreadyExists;
       
 55361   VdbeCursor *pC;
       
 55362   int res;
       
 55363   UnpackedRecord *pIdxKey;
       
 55364   char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
       
 55365 #endif /* local variables moved into u.bb */
       
 55366 
       
 55367 #ifdef SQLITE_TEST
       
 55368   sqlite3_found_count++;
       
 55369 #endif
       
 55370 
       
 55371   u.bb.alreadyExists = 0;
       
 55372   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 55373   u.bb.pC = p->apCsr[pOp->p1];
       
 55374   assert( u.bb.pC!=0 );
       
 55375   if( ALWAYS(u.bb.pC->pCursor!=0) ){
       
 55376 
       
 55377     assert( u.bb.pC->isTable==0 );
       
 55378     assert( pIn3->flags & MEM_Blob );
       
 55379     ExpandBlob(pIn3);
       
 55380     u.bb.pIdxKey = sqlite3VdbeRecordUnpack(u.bb.pC->pKeyInfo, pIn3->n, pIn3->z,
       
 55381                                       u.bb.aTempRec, sizeof(u.bb.aTempRec));
       
 55382     if( u.bb.pIdxKey==0 ){
       
 55383       goto no_mem;
       
 55384     }
       
 55385     if( pOp->opcode==OP_Found ){
       
 55386       u.bb.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
       
 55387     }
       
 55388     rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, u.bb.pIdxKey, 0, 0, &u.bb.res);
       
 55389     sqlite3VdbeDeleteUnpackedRecord(u.bb.pIdxKey);
       
 55390     if( rc!=SQLITE_OK ){
       
 55391       break;
       
 55392     }
       
 55393     u.bb.alreadyExists = (u.bb.res==0);
       
 55394     u.bb.pC->deferredMoveto = 0;
       
 55395     u.bb.pC->cacheStatus = CACHE_STALE;
       
 55396   }
       
 55397   if( pOp->opcode==OP_Found ){
       
 55398     if( u.bb.alreadyExists ) pc = pOp->p2 - 1;
       
 55399   }else{
       
 55400     if( !u.bb.alreadyExists ) pc = pOp->p2 - 1;
       
 55401   }
       
 55402   break;
       
 55403 }
       
 55404 
       
 55405 /* Opcode: IsUnique P1 P2 P3 P4 *
       
 55406 **
       
 55407 ** Cursor P1 is open on an index.  So it has no data and its key consists 
       
 55408 ** of a record generated by OP_MakeRecord where the last field is the 
       
 55409 ** rowid of the entry that the index refers to.
       
 55410 **
       
 55411 ** The P3 register contains an integer record number. Call this record 
       
 55412 ** number R. Register P4 is the first in a set of N contiguous registers
       
 55413 ** that make up an unpacked index key that can be used with cursor P1.
       
 55414 ** The value of N can be inferred from the cursor. N includes the rowid
       
 55415 ** value appended to the end of the index record. This rowid value may
       
 55416 ** or may not be the same as R.
       
 55417 **
       
 55418 ** If any of the N registers beginning with register P4 contains a NULL
       
 55419 ** value, jump immediately to P2.
       
 55420 **
       
 55421 ** Otherwise, this instruction checks if cursor P1 contains an entry
       
 55422 ** where the first (N-1) fields match but the rowid value at the end
       
 55423 ** of the index entry is not R. If there is no such entry, control jumps
       
 55424 ** to instruction P2. Otherwise, the rowid of the conflicting index
       
 55425 ** entry is copied to register P3 and control falls through to the next
       
 55426 ** instruction.
       
 55427 **
       
 55428 ** See also: NotFound, NotExists, Found
       
 55429 */
       
 55430 case OP_IsUnique: {        /* jump, in3 */
       
 55431 #if 0  /* local variables moved into u.bc */
       
 55432   u16 ii;
       
 55433   VdbeCursor *pCx;
       
 55434   BtCursor *pCrsr;
       
 55435   u16 nField;
       
 55436   Mem *aMem;
       
 55437   UnpackedRecord r;                  /* B-Tree index search key */
       
 55438   i64 R;                             /* Rowid stored in register P3 */
       
 55439 #endif /* local variables moved into u.bc */
       
 55440 
       
 55441   u.bc.aMem = &p->aMem[pOp->p4.i];
       
 55442   /* Assert that the values of parameters P1 and P4 are in range. */
       
 55443   assert( pOp->p4type==P4_INT32 );
       
 55444   assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
       
 55445   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 55446 
       
 55447   /* Find the index cursor. */
       
 55448   u.bc.pCx = p->apCsr[pOp->p1];
       
 55449   assert( u.bc.pCx->deferredMoveto==0 );
       
 55450   u.bc.pCx->seekResult = 0;
       
 55451   u.bc.pCx->cacheStatus = CACHE_STALE;
       
 55452   u.bc.pCrsr = u.bc.pCx->pCursor;
       
 55453 
       
 55454   /* If any of the values are NULL, take the jump. */
       
 55455   u.bc.nField = u.bc.pCx->pKeyInfo->nField;
       
 55456   for(u.bc.ii=0; u.bc.ii<u.bc.nField; u.bc.ii++){
       
 55457     if( u.bc.aMem[u.bc.ii].flags & MEM_Null ){
       
 55458       pc = pOp->p2 - 1;
       
 55459       u.bc.pCrsr = 0;
       
 55460       break;
       
 55461     }
       
 55462   }
       
 55463   assert( (u.bc.aMem[u.bc.nField].flags & MEM_Null)==0 );
       
 55464 
       
 55465   if( u.bc.pCrsr!=0 ){
       
 55466     /* Populate the index search key. */
       
 55467     u.bc.r.pKeyInfo = u.bc.pCx->pKeyInfo;
       
 55468     u.bc.r.nField = u.bc.nField + 1;
       
 55469     u.bc.r.flags = UNPACKED_PREFIX_SEARCH;
       
 55470     u.bc.r.aMem = u.bc.aMem;
       
 55471 
       
 55472     /* Extract the value of u.bc.R from register P3. */
       
 55473     sqlite3VdbeMemIntegerify(pIn3);
       
 55474     u.bc.R = pIn3->u.i;
       
 55475 
       
 55476     /* Search the B-Tree index. If no conflicting record is found, jump
       
 55477     ** to P2. Otherwise, copy the rowid of the conflicting record to
       
 55478     ** register P3 and fall through to the next instruction.  */
       
 55479     rc = sqlite3BtreeMovetoUnpacked(u.bc.pCrsr, &u.bc.r, 0, 0, &u.bc.pCx->seekResult);
       
 55480     if( (u.bc.r.flags & UNPACKED_PREFIX_SEARCH) || u.bc.r.rowid==u.bc.R ){
       
 55481       pc = pOp->p2 - 1;
       
 55482     }else{
       
 55483       pIn3->u.i = u.bc.r.rowid;
       
 55484     }
       
 55485   }
       
 55486   break;
       
 55487 }
       
 55488 
       
 55489 /* Opcode: NotExists P1 P2 P3 * *
       
 55490 **
       
 55491 ** Use the content of register P3 as a integer key.  If a record 
       
 55492 ** with that key does not exist in table of P1, then jump to P2. 
       
 55493 ** If the record does exist, then fall thru.  The cursor is left 
       
 55494 ** pointing to the record if it exists.
       
 55495 **
       
 55496 ** The difference between this operation and NotFound is that this
       
 55497 ** operation assumes the key is an integer and that P1 is a table whereas
       
 55498 ** NotFound assumes key is a blob constructed from MakeRecord and
       
 55499 ** P1 is an index.
       
 55500 **
       
 55501 ** See also: Found, NotFound, IsUnique
       
 55502 */
       
 55503 case OP_NotExists: {        /* jump, in3 */
       
 55504 #if 0  /* local variables moved into u.bd */
       
 55505   VdbeCursor *pC;
       
 55506   BtCursor *pCrsr;
       
 55507   int res;
       
 55508   u64 iKey;
       
 55509 #endif /* local variables moved into u.bd */
       
 55510 
       
 55511   assert( pIn3->flags & MEM_Int );
       
 55512   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 55513   u.bd.pC = p->apCsr[pOp->p1];
       
 55514   assert( u.bd.pC!=0 );
       
 55515   assert( u.bd.pC->isTable );
       
 55516   assert( u.bd.pC->pseudoTableReg==0 );
       
 55517   u.bd.pCrsr = u.bd.pC->pCursor;
       
 55518   if( u.bd.pCrsr!=0 ){
       
 55519     u.bd.res = 0;
       
 55520     u.bd.iKey = pIn3->u.i;
       
 55521     rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, 0, u.bd.iKey, 0, &u.bd.res);
       
 55522     u.bd.pC->lastRowid = pIn3->u.i;
       
 55523     u.bd.pC->rowidIsValid = u.bd.res==0 ?1:0;
       
 55524     u.bd.pC->nullRow = 0;
       
 55525     u.bd.pC->cacheStatus = CACHE_STALE;
       
 55526     u.bd.pC->deferredMoveto = 0;
       
 55527     if( u.bd.res!=0 ){
       
 55528       pc = pOp->p2 - 1;
       
 55529       assert( u.bd.pC->rowidIsValid==0 );
       
 55530     }
       
 55531     u.bd.pC->seekResult = u.bd.res;
       
 55532   }else{
       
 55533     /* This happens when an attempt to open a read cursor on the
       
 55534     ** sqlite_master table returns SQLITE_EMPTY.
       
 55535     */
       
 55536     pc = pOp->p2 - 1;
       
 55537     assert( u.bd.pC->rowidIsValid==0 );
       
 55538     u.bd.pC->seekResult = 0;
       
 55539   }
       
 55540   break;
       
 55541 }
       
 55542 
       
 55543 /* Opcode: Sequence P1 P2 * * *
       
 55544 **
       
 55545 ** Find the next available sequence number for cursor P1.
       
 55546 ** Write the sequence number into register P2.
       
 55547 ** The sequence number on the cursor is incremented after this
       
 55548 ** instruction.  
       
 55549 */
       
 55550 case OP_Sequence: {           /* out2-prerelease */
       
 55551   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 55552   assert( p->apCsr[pOp->p1]!=0 );
       
 55553   pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
       
 55554   MemSetTypeFlag(pOut, MEM_Int);
       
 55555   break;
       
 55556 }
       
 55557 
       
 55558 
       
 55559 /* Opcode: NewRowid P1 P2 P3 * *
       
 55560 **
       
 55561 ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
       
 55562 ** The record number is not previously used as a key in the database
       
 55563 ** table that cursor P1 points to.  The new record number is written
       
 55564 ** written to register P2.
       
 55565 **
       
 55566 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds 
       
 55567 ** the largest previously generated record number. No new record numbers are
       
 55568 ** allowed to be less than this value. When this value reaches its maximum, 
       
 55569 ** a SQLITE_FULL error is generated. The P3 register is updated with the '
       
 55570 ** generated record number. This P3 mechanism is used to help implement the
       
 55571 ** AUTOINCREMENT feature.
       
 55572 */
       
 55573 case OP_NewRowid: {           /* out2-prerelease */
       
 55574 #if 0  /* local variables moved into u.be */
       
 55575   i64 v;                 /* The new rowid */
       
 55576   VdbeCursor *pC;        /* Cursor of table to get the new rowid */
       
 55577   int res;               /* Result of an sqlite3BtreeLast() */
       
 55578   int cnt;               /* Counter to limit the number of searches */
       
 55579   Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
       
 55580   VdbeFrame *pFrame;     /* Root frame of VDBE */
       
 55581 #endif /* local variables moved into u.be */
       
 55582 
       
 55583   u.be.v = 0;
       
 55584   u.be.res = 0;
       
 55585   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 55586   u.be.pC = p->apCsr[pOp->p1];
       
 55587   assert( u.be.pC!=0 );
       
 55588   if( NEVER(u.be.pC->pCursor==0) ){
       
 55589     /* The zero initialization above is all that is needed */
       
 55590   }else{
       
 55591     /* The next rowid or record number (different terms for the same
       
 55592     ** thing) is obtained in a two-step algorithm.
       
 55593     **
       
 55594     ** First we attempt to find the largest existing rowid and add one
       
 55595     ** to that.  But if the largest existing rowid is already the maximum
       
 55596     ** positive integer, we have to fall through to the second
       
 55597     ** probabilistic algorithm
       
 55598     **
       
 55599     ** The second algorithm is to select a rowid at random and see if
       
 55600     ** it already exists in the table.  If it does not exist, we have
       
 55601     ** succeeded.  If the random rowid does exist, we select a new one
       
 55602     ** and try again, up to 100 times.
       
 55603     */
       
 55604     assert( u.be.pC->isTable );
       
 55605     u.be.cnt = 0;
       
 55606 
       
 55607 #ifdef SQLITE_32BIT_ROWID
       
 55608 #   define MAX_ROWID 0x7fffffff
       
 55609 #else
       
 55610     /* Some compilers complain about constants of the form 0x7fffffffffffffff.
       
 55611     ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
       
 55612     ** to provide the constant while making all compilers happy.
       
 55613     */
       
 55614 #   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
       
 55615 #endif
       
 55616 
       
 55617     if( !u.be.pC->useRandomRowid ){
       
 55618       u.be.v = sqlite3BtreeGetCachedRowid(u.be.pC->pCursor);
       
 55619       if( u.be.v==0 ){
       
 55620         rc = sqlite3BtreeLast(u.be.pC->pCursor, &u.be.res);
       
 55621         if( rc!=SQLITE_OK ){
       
 55622           goto abort_due_to_error;
       
 55623         }
       
 55624         if( u.be.res ){
       
 55625           u.be.v = 1;
       
 55626         }else{
       
 55627           assert( sqlite3BtreeCursorIsValid(u.be.pC->pCursor) );
       
 55628           rc = sqlite3BtreeKeySize(u.be.pC->pCursor, &u.be.v);
       
 55629           assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
       
 55630           if( u.be.v==MAX_ROWID ){
       
 55631             u.be.pC->useRandomRowid = 1;
       
 55632           }else{
       
 55633             u.be.v++;
       
 55634           }
       
 55635         }
       
 55636       }
       
 55637 
       
 55638 #ifndef SQLITE_OMIT_AUTOINCREMENT
       
 55639       if( pOp->p3 ){
       
 55640         /* Assert that P3 is a valid memory cell. */
       
 55641         assert( pOp->p3>0 );
       
 55642         if( p->pFrame ){
       
 55643           for(u.be.pFrame=p->pFrame; u.be.pFrame->pParent; u.be.pFrame=u.be.pFrame->pParent);
       
 55644           /* Assert that P3 is a valid memory cell. */
       
 55645           assert( pOp->p3<=u.be.pFrame->nMem );
       
 55646           u.be.pMem = &u.be.pFrame->aMem[pOp->p3];
       
 55647         }else{
       
 55648           /* Assert that P3 is a valid memory cell. */
       
 55649           assert( pOp->p3<=p->nMem );
       
 55650           u.be.pMem = &p->aMem[pOp->p3];
       
 55651         }
       
 55652 
       
 55653         REGISTER_TRACE(pOp->p3, u.be.pMem);
       
 55654         sqlite3VdbeMemIntegerify(u.be.pMem);
       
 55655         assert( (u.be.pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
       
 55656         if( u.be.pMem->u.i==MAX_ROWID || u.be.pC->useRandomRowid ){
       
 55657           rc = SQLITE_FULL;
       
 55658           goto abort_due_to_error;
       
 55659         }
       
 55660         if( u.be.v<u.be.pMem->u.i+1 ){
       
 55661           u.be.v = u.be.pMem->u.i + 1;
       
 55662         }
       
 55663         u.be.pMem->u.i = u.be.v;
       
 55664       }
       
 55665 #endif
       
 55666 
       
 55667       sqlite3BtreeSetCachedRowid(u.be.pC->pCursor, u.be.v<MAX_ROWID ? u.be.v+1 : 0);
       
 55668     }
       
 55669     if( u.be.pC->useRandomRowid ){
       
 55670       assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
       
 55671                              ** an AUTOINCREMENT table. */
       
 55672       u.be.v = db->lastRowid;
       
 55673       u.be.cnt = 0;
       
 55674       do{
       
 55675         if( u.be.cnt==0 && (u.be.v&0xffffff)==u.be.v ){
       
 55676           u.be.v++;
       
 55677         }else{
       
 55678           sqlite3_randomness(sizeof(u.be.v), &u.be.v);
       
 55679           if( u.be.cnt<5 ) u.be.v &= 0xffffff;
       
 55680         }
       
 55681         rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, 0, (u64)u.be.v, 0, &u.be.res);
       
 55682         u.be.cnt++;
       
 55683       }while( u.be.cnt<100 && rc==SQLITE_OK && u.be.res==0 );
       
 55684       if( rc==SQLITE_OK && u.be.res==0 ){
       
 55685         rc = SQLITE_FULL;
       
 55686         goto abort_due_to_error;
       
 55687       }
       
 55688     }
       
 55689     u.be.pC->rowidIsValid = 0;
       
 55690     u.be.pC->deferredMoveto = 0;
       
 55691     u.be.pC->cacheStatus = CACHE_STALE;
       
 55692   }
       
 55693   MemSetTypeFlag(pOut, MEM_Int);
       
 55694   pOut->u.i = u.be.v;
       
 55695   break;
       
 55696 }
       
 55697 
       
 55698 /* Opcode: Insert P1 P2 P3 P4 P5
       
 55699 **
       
 55700 ** Write an entry into the table of cursor P1.  A new entry is
       
 55701 ** created if it doesn't already exist or the data for an existing
       
 55702 ** entry is overwritten.  The data is the value MEM_Blob stored in register
       
 55703 ** number P2. The key is stored in register P3. The key must
       
 55704 ** be a MEM_Int.
       
 55705 **
       
 55706 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
       
 55707 ** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
       
 55708 ** then rowid is stored for subsequent return by the
       
 55709 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
       
 55710 **
       
 55711 ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
       
 55712 ** the last seek operation (OP_NotExists) was a success, then this
       
 55713 ** operation will not attempt to find the appropriate row before doing
       
 55714 ** the insert but will instead overwrite the row that the cursor is
       
 55715 ** currently pointing to.  Presumably, the prior OP_NotExists opcode
       
 55716 ** has already positioned the cursor correctly.  This is an optimization
       
 55717 ** that boosts performance by avoiding redundant seeks.
       
 55718 **
       
 55719 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
       
 55720 ** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
       
 55721 ** is part of an INSERT operation.  The difference is only important to
       
 55722 ** the update hook.
       
 55723 **
       
 55724 ** Parameter P4 may point to a string containing the table-name, or
       
 55725 ** may be NULL. If it is not NULL, then the update-hook 
       
 55726 ** (sqlite3.xUpdateCallback) is invoked following a successful insert.
       
 55727 **
       
 55728 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
       
 55729 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
       
 55730 ** and register P2 becomes ephemeral.  If the cursor is changed, the
       
 55731 ** value of register P2 will then change.  Make sure this does not
       
 55732 ** cause any problems.)
       
 55733 **
       
 55734 ** This instruction only works on tables.  The equivalent instruction
       
 55735 ** for indices is OP_IdxInsert.
       
 55736 */
       
 55737 case OP_Insert: {
       
 55738 #if 0  /* local variables moved into u.bf */
       
 55739   Mem *pData;       /* MEM cell holding data for the record to be inserted */
       
 55740   Mem *pKey;        /* MEM cell holding key  for the record */
       
 55741   i64 iKey;         /* The integer ROWID or key for the record to be inserted */
       
 55742   VdbeCursor *pC;   /* Cursor to table into which insert is written */
       
 55743   int nZero;        /* Number of zero-bytes to append */
       
 55744   int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
       
 55745   const char *zDb;  /* database name - used by the update hook */
       
 55746   const char *zTbl; /* Table name - used by the opdate hook */
       
 55747   int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
       
 55748 #endif /* local variables moved into u.bf */
       
 55749 
       
 55750   u.bf.pData = &p->aMem[pOp->p2];
       
 55751   u.bf.pKey = &p->aMem[pOp->p3];
       
 55752   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 55753   u.bf.pC = p->apCsr[pOp->p1];
       
 55754   assert( u.bf.pC!=0 );
       
 55755   assert( u.bf.pC->pCursor!=0 );
       
 55756   assert( u.bf.pC->pseudoTableReg==0 );
       
 55757   assert( u.bf.pKey->flags & MEM_Int );
       
 55758   assert( u.bf.pC->isTable );
       
 55759   REGISTER_TRACE(pOp->p2, u.bf.pData);
       
 55760   REGISTER_TRACE(pOp->p3, u.bf.pKey);
       
 55761 
       
 55762   u.bf.iKey = u.bf.pKey->u.i;
       
 55763   if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
       
 55764   if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = u.bf.pKey->u.i;
       
 55765   if( u.bf.pData->flags & MEM_Null ){
       
 55766     u.bf.pData->z = 0;
       
 55767     u.bf.pData->n = 0;
       
 55768   }else{
       
 55769     assert( u.bf.pData->flags & (MEM_Blob|MEM_Str) );
       
 55770   }
       
 55771   u.bf.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bf.pC->seekResult : 0);
       
 55772   if( u.bf.pData->flags & MEM_Zero ){
       
 55773     u.bf.nZero = u.bf.pData->u.nZero;
       
 55774   }else{
       
 55775     u.bf.nZero = 0;
       
 55776   }
       
 55777   sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, 0);
       
 55778   rc = sqlite3BtreeInsert(u.bf.pC->pCursor, 0, u.bf.iKey,
       
 55779                           u.bf.pData->z, u.bf.pData->n, u.bf.nZero,
       
 55780                           pOp->p5 & OPFLAG_APPEND, u.bf.seekResult
       
 55781   );
       
 55782   u.bf.pC->rowidIsValid = 0;
       
 55783   u.bf.pC->deferredMoveto = 0;
       
 55784   u.bf.pC->cacheStatus = CACHE_STALE;
       
 55785 
       
 55786   /* Invoke the update-hook if required. */
       
 55787   if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
       
 55788     u.bf.zDb = db->aDb[u.bf.pC->iDb].zName;
       
 55789     u.bf.zTbl = pOp->p4.z;
       
 55790     u.bf.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
       
 55791     assert( u.bf.pC->isTable );
       
 55792     db->xUpdateCallback(db->pUpdateArg, u.bf.op, u.bf.zDb, u.bf.zTbl, u.bf.iKey);
       
 55793     assert( u.bf.pC->iDb>=0 );
       
 55794   }
       
 55795   break;
       
 55796 }
       
 55797 
       
 55798 /* Opcode: Delete P1 P2 * P4 *
       
 55799 **
       
 55800 ** Delete the record at which the P1 cursor is currently pointing.
       
 55801 **
       
 55802 ** The cursor will be left pointing at either the next or the previous
       
 55803 ** record in the table. If it is left pointing at the next record, then
       
 55804 ** the next Next instruction will be a no-op.  Hence it is OK to delete
       
 55805 ** a record from within an Next loop.
       
 55806 **
       
 55807 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
       
 55808 ** incremented (otherwise not).
       
 55809 **
       
 55810 ** P1 must not be pseudo-table.  It has to be a real table with
       
 55811 ** multiple rows.
       
 55812 **
       
 55813 ** If P4 is not NULL, then it is the name of the table that P1 is
       
 55814 ** pointing to.  The update hook will be invoked, if it exists.
       
 55815 ** If P4 is not NULL then the P1 cursor must have been positioned
       
 55816 ** using OP_NotFound prior to invoking this opcode.
       
 55817 */
       
 55818 case OP_Delete: {
       
 55819 #if 0  /* local variables moved into u.bg */
       
 55820   i64 iKey;
       
 55821   VdbeCursor *pC;
       
 55822 #endif /* local variables moved into u.bg */
       
 55823 
       
 55824   u.bg.iKey = 0;
       
 55825   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 55826   u.bg.pC = p->apCsr[pOp->p1];
       
 55827   assert( u.bg.pC!=0 );
       
 55828   assert( u.bg.pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
       
 55829 
       
 55830   /* If the update-hook will be invoked, set u.bg.iKey to the rowid of the
       
 55831   ** row being deleted.
       
 55832   */
       
 55833   if( db->xUpdateCallback && pOp->p4.z ){
       
 55834     assert( u.bg.pC->isTable );
       
 55835     assert( u.bg.pC->rowidIsValid );  /* lastRowid set by previous OP_NotFound */
       
 55836     u.bg.iKey = u.bg.pC->lastRowid;
       
 55837   }
       
 55838 
       
 55839   /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
       
 55840   ** OP_Column on the same table without any intervening operations that
       
 55841   ** might move or invalidate the cursor.  Hence cursor u.bg.pC is always pointing
       
 55842   ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
       
 55843   ** below is always a no-op and cannot fail.  We will run it anyhow, though,
       
 55844   ** to guard against future changes to the code generator.
       
 55845   **/
       
 55846   assert( u.bg.pC->deferredMoveto==0 );
       
 55847   rc = sqlite3VdbeCursorMoveto(u.bg.pC);
       
 55848   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
       
 55849 
       
 55850   sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0);
       
 55851   rc = sqlite3BtreeDelete(u.bg.pC->pCursor);
       
 55852   u.bg.pC->cacheStatus = CACHE_STALE;
       
 55853 
       
 55854   /* Invoke the update-hook if required. */
       
 55855   if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
       
 55856     const char *zDb = db->aDb[u.bg.pC->iDb].zName;
       
 55857     const char *zTbl = pOp->p4.z;
       
 55858     db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bg.iKey);
       
 55859     assert( u.bg.pC->iDb>=0 );
       
 55860   }
       
 55861   if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
       
 55862   break;
       
 55863 }
       
 55864 /* Opcode: ResetCount * * * * *
       
 55865 **
       
 55866 ** The value of the change counter is copied to the database handle
       
 55867 ** change counter (returned by subsequent calls to sqlite3_changes()).
       
 55868 ** Then the VMs internal change counter resets to 0.
       
 55869 ** This is used by trigger programs.
       
 55870 */
       
 55871 case OP_ResetCount: {
       
 55872   sqlite3VdbeSetChanges(db, p->nChange);
       
 55873   p->nChange = 0;
       
 55874   break;
       
 55875 }
       
 55876 
       
 55877 /* Opcode: RowData P1 P2 * * *
       
 55878 **
       
 55879 ** Write into register P2 the complete row data for cursor P1.
       
 55880 ** There is no interpretation of the data.  
       
 55881 ** It is just copied onto the P2 register exactly as 
       
 55882 ** it is found in the database file.
       
 55883 **
       
 55884 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
       
 55885 ** of a real table, not a pseudo-table.
       
 55886 */
       
 55887 /* Opcode: RowKey P1 P2 * * *
       
 55888 **
       
 55889 ** Write into register P2 the complete row key for cursor P1.
       
 55890 ** There is no interpretation of the data.  
       
 55891 ** The key is copied onto the P3 register exactly as 
       
 55892 ** it is found in the database file.
       
 55893 **
       
 55894 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
       
 55895 ** of a real table, not a pseudo-table.
       
 55896 */
       
 55897 case OP_RowKey:
       
 55898 case OP_RowData: {
       
 55899 #if 0  /* local variables moved into u.bh */
       
 55900   VdbeCursor *pC;
       
 55901   BtCursor *pCrsr;
       
 55902   u32 n;
       
 55903   i64 n64;
       
 55904 #endif /* local variables moved into u.bh */
       
 55905 
       
 55906   pOut = &p->aMem[pOp->p2];
       
 55907 
       
 55908   /* Note that RowKey and RowData are really exactly the same instruction */
       
 55909   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 55910   u.bh.pC = p->apCsr[pOp->p1];
       
 55911   assert( u.bh.pC->isTable || pOp->opcode==OP_RowKey );
       
 55912   assert( u.bh.pC->isIndex || pOp->opcode==OP_RowData );
       
 55913   assert( u.bh.pC!=0 );
       
 55914   assert( u.bh.pC->nullRow==0 );
       
 55915   assert( u.bh.pC->pseudoTableReg==0 );
       
 55916   assert( u.bh.pC->pCursor!=0 );
       
 55917   u.bh.pCrsr = u.bh.pC->pCursor;
       
 55918   assert( sqlite3BtreeCursorIsValid(u.bh.pCrsr) );
       
 55919 
       
 55920   /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
       
 55921   ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
       
 55922   ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
       
 55923   ** a no-op and can never fail.  But we leave it in place as a safety.
       
 55924   */
       
 55925   assert( u.bh.pC->deferredMoveto==0 );
       
 55926   rc = sqlite3VdbeCursorMoveto(u.bh.pC);
       
 55927   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
       
 55928 
       
 55929   if( u.bh.pC->isIndex ){
       
 55930     assert( !u.bh.pC->isTable );
       
 55931     rc = sqlite3BtreeKeySize(u.bh.pCrsr, &u.bh.n64);
       
 55932     assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
       
 55933     if( u.bh.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
       
 55934       goto too_big;
       
 55935     }
       
 55936     u.bh.n = (u32)u.bh.n64;
       
 55937   }else{
       
 55938     rc = sqlite3BtreeDataSize(u.bh.pCrsr, &u.bh.n);
       
 55939     assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
       
 55940     if( u.bh.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
       
 55941       goto too_big;
       
 55942     }
       
 55943   }
       
 55944   if( sqlite3VdbeMemGrow(pOut, u.bh.n, 0) ){
       
 55945     goto no_mem;
       
 55946   }
       
 55947   pOut->n = u.bh.n;
       
 55948   MemSetTypeFlag(pOut, MEM_Blob);
       
 55949   if( u.bh.pC->isIndex ){
       
 55950     rc = sqlite3BtreeKey(u.bh.pCrsr, 0, u.bh.n, pOut->z);
       
 55951   }else{
       
 55952     rc = sqlite3BtreeData(u.bh.pCrsr, 0, u.bh.n, pOut->z);
       
 55953   }
       
 55954   pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
       
 55955   UPDATE_MAX_BLOBSIZE(pOut);
       
 55956   break;
       
 55957 }
       
 55958 
       
 55959 /* Opcode: Rowid P1 P2 * * *
       
 55960 **
       
 55961 ** Store in register P2 an integer which is the key of the table entry that
       
 55962 ** P1 is currently point to.
       
 55963 **
       
 55964 ** P1 can be either an ordinary table or a virtual table.  There used to
       
 55965 ** be a separate OP_VRowid opcode for use with virtual tables, but this
       
 55966 ** one opcode now works for both table types.
       
 55967 */
       
 55968 case OP_Rowid: {                 /* out2-prerelease */
       
 55969 #if 0  /* local variables moved into u.bi */
       
 55970   VdbeCursor *pC;
       
 55971   i64 v;
       
 55972   sqlite3_vtab *pVtab;
       
 55973   const sqlite3_module *pModule;
       
 55974 #endif /* local variables moved into u.bi */
       
 55975 
       
 55976   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 55977   u.bi.pC = p->apCsr[pOp->p1];
       
 55978   assert( u.bi.pC!=0 );
       
 55979   assert( u.bi.pC->pseudoTableReg==0 );
       
 55980   if( u.bi.pC->nullRow ){
       
 55981     /* Do nothing so that reg[P2] remains NULL */
       
 55982     break;
       
 55983   }else if( u.bi.pC->deferredMoveto ){
       
 55984     u.bi.v = u.bi.pC->movetoTarget;
       
 55985 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 55986   }else if( u.bi.pC->pVtabCursor ){
       
 55987     u.bi.pVtab = u.bi.pC->pVtabCursor->pVtab;
       
 55988     u.bi.pModule = u.bi.pVtab->pModule;
       
 55989     assert( u.bi.pModule->xRowid );
       
 55990     if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
       
 55991     rc = u.bi.pModule->xRowid(u.bi.pC->pVtabCursor, &u.bi.v);
       
 55992     sqlite3DbFree(db, p->zErrMsg);
       
 55993     p->zErrMsg = u.bi.pVtab->zErrMsg;
       
 55994     u.bi.pVtab->zErrMsg = 0;
       
 55995     if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
       
 55996 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 55997   }else{
       
 55998     assert( u.bi.pC->pCursor!=0 );
       
 55999     rc = sqlite3VdbeCursorMoveto(u.bi.pC);
       
 56000     if( rc ) goto abort_due_to_error;
       
 56001     if( u.bi.pC->rowidIsValid ){
       
 56002       u.bi.v = u.bi.pC->lastRowid;
       
 56003     }else{
       
 56004       rc = sqlite3BtreeKeySize(u.bi.pC->pCursor, &u.bi.v);
       
 56005       assert( rc==SQLITE_OK );  /* Always so because of CursorMoveto() above */
       
 56006     }
       
 56007   }
       
 56008   pOut->u.i = u.bi.v;
       
 56009   MemSetTypeFlag(pOut, MEM_Int);
       
 56010   break;
       
 56011 }
       
 56012 
       
 56013 /* Opcode: NullRow P1 * * * *
       
 56014 **
       
 56015 ** Move the cursor P1 to a null row.  Any OP_Column operations
       
 56016 ** that occur while the cursor is on the null row will always
       
 56017 ** write a NULL.
       
 56018 */
       
 56019 case OP_NullRow: {
       
 56020 #if 0  /* local variables moved into u.bj */
       
 56021   VdbeCursor *pC;
       
 56022 #endif /* local variables moved into u.bj */
       
 56023 
       
 56024   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 56025   u.bj.pC = p->apCsr[pOp->p1];
       
 56026   assert( u.bj.pC!=0 );
       
 56027   u.bj.pC->nullRow = 1;
       
 56028   u.bj.pC->rowidIsValid = 0;
       
 56029   if( u.bj.pC->pCursor ){
       
 56030     sqlite3BtreeClearCursor(u.bj.pC->pCursor);
       
 56031   }
       
 56032   break;
       
 56033 }
       
 56034 
       
 56035 /* Opcode: Last P1 P2 * * *
       
 56036 **
       
 56037 ** The next use of the Rowid or Column or Next instruction for P1 
       
 56038 ** will refer to the last entry in the database table or index.
       
 56039 ** If the table or index is empty and P2>0, then jump immediately to P2.
       
 56040 ** If P2 is 0 or if the table or index is not empty, fall through
       
 56041 ** to the following instruction.
       
 56042 */
       
 56043 case OP_Last: {        /* jump */
       
 56044 #if 0  /* local variables moved into u.bk */
       
 56045   VdbeCursor *pC;
       
 56046   BtCursor *pCrsr;
       
 56047   int res;
       
 56048 #endif /* local variables moved into u.bk */
       
 56049 
       
 56050   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 56051   u.bk.pC = p->apCsr[pOp->p1];
       
 56052   assert( u.bk.pC!=0 );
       
 56053   u.bk.pCrsr = u.bk.pC->pCursor;
       
 56054   if( u.bk.pCrsr==0 ){
       
 56055     u.bk.res = 1;
       
 56056   }else{
       
 56057     rc = sqlite3BtreeLast(u.bk.pCrsr, &u.bk.res);
       
 56058   }
       
 56059   u.bk.pC->nullRow = (u8)u.bk.res;
       
 56060   u.bk.pC->deferredMoveto = 0;
       
 56061   u.bk.pC->rowidIsValid = 0;
       
 56062   u.bk.pC->cacheStatus = CACHE_STALE;
       
 56063   if( pOp->p2>0 && u.bk.res ){
       
 56064     pc = pOp->p2 - 1;
       
 56065   }
       
 56066   break;
       
 56067 }
       
 56068 
       
 56069 
       
 56070 /* Opcode: Sort P1 P2 * * *
       
 56071 **
       
 56072 ** This opcode does exactly the same thing as OP_Rewind except that
       
 56073 ** it increments an undocumented global variable used for testing.
       
 56074 **
       
 56075 ** Sorting is accomplished by writing records into a sorting index,
       
 56076 ** then rewinding that index and playing it back from beginning to
       
 56077 ** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
       
 56078 ** rewinding so that the global variable will be incremented and
       
 56079 ** regression tests can determine whether or not the optimizer is
       
 56080 ** correctly optimizing out sorts.
       
 56081 */
       
 56082 case OP_Sort: {        /* jump */
       
 56083 #ifdef SQLITE_TEST
       
 56084   sqlite3_sort_count++;
       
 56085   sqlite3_search_count--;
       
 56086 #endif
       
 56087   p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
       
 56088   /* Fall through into OP_Rewind */
       
 56089 }
       
 56090 /* Opcode: Rewind P1 P2 * * *
       
 56091 **
       
 56092 ** The next use of the Rowid or Column or Next instruction for P1 
       
 56093 ** will refer to the first entry in the database table or index.
       
 56094 ** If the table or index is empty and P2>0, then jump immediately to P2.
       
 56095 ** If P2 is 0 or if the table or index is not empty, fall through
       
 56096 ** to the following instruction.
       
 56097 */
       
 56098 case OP_Rewind: {        /* jump */
       
 56099 #if 0  /* local variables moved into u.bl */
       
 56100   VdbeCursor *pC;
       
 56101   BtCursor *pCrsr;
       
 56102   int res;
       
 56103 #endif /* local variables moved into u.bl */
       
 56104 
       
 56105   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 56106   u.bl.pC = p->apCsr[pOp->p1];
       
 56107   assert( u.bl.pC!=0 );
       
 56108   if( (u.bl.pCrsr = u.bl.pC->pCursor)!=0 ){
       
 56109     rc = sqlite3BtreeFirst(u.bl.pCrsr, &u.bl.res);
       
 56110     u.bl.pC->atFirst = u.bl.res==0 ?1:0;
       
 56111     u.bl.pC->deferredMoveto = 0;
       
 56112     u.bl.pC->cacheStatus = CACHE_STALE;
       
 56113     u.bl.pC->rowidIsValid = 0;
       
 56114   }else{
       
 56115     u.bl.res = 1;
       
 56116   }
       
 56117   u.bl.pC->nullRow = (u8)u.bl.res;
       
 56118   assert( pOp->p2>0 && pOp->p2<p->nOp );
       
 56119   if( u.bl.res ){
       
 56120     pc = pOp->p2 - 1;
       
 56121   }
       
 56122   break;
       
 56123 }
       
 56124 
       
 56125 /* Opcode: Next P1 P2 * * *
       
 56126 **
       
 56127 ** Advance cursor P1 so that it points to the next key/data pair in its
       
 56128 ** table or index.  If there are no more key/value pairs then fall through
       
 56129 ** to the following instruction.  But if the cursor advance was successful,
       
 56130 ** jump immediately to P2.
       
 56131 **
       
 56132 ** The P1 cursor must be for a real table, not a pseudo-table.
       
 56133 **
       
 56134 ** See also: Prev
       
 56135 */
       
 56136 /* Opcode: Prev P1 P2 * * *
       
 56137 **
       
 56138 ** Back up cursor P1 so that it points to the previous key/data pair in its
       
 56139 ** table or index.  If there is no previous key/value pairs then fall through
       
 56140 ** to the following instruction.  But if the cursor backup was successful,
       
 56141 ** jump immediately to P2.
       
 56142 **
       
 56143 ** The P1 cursor must be for a real table, not a pseudo-table.
       
 56144 */
       
 56145 case OP_Prev:          /* jump */
       
 56146 case OP_Next: {        /* jump */
       
 56147 #if 0  /* local variables moved into u.bm */
       
 56148   VdbeCursor *pC;
       
 56149   BtCursor *pCrsr;
       
 56150   int res;
       
 56151 #endif /* local variables moved into u.bm */
       
 56152 
       
 56153   CHECK_FOR_INTERRUPT;
       
 56154   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 56155   u.bm.pC = p->apCsr[pOp->p1];
       
 56156   if( u.bm.pC==0 ){
       
 56157     break;  /* See ticket #2273 */
       
 56158   }
       
 56159   u.bm.pCrsr = u.bm.pC->pCursor;
       
 56160   if( u.bm.pCrsr==0 ){
       
 56161     u.bm.pC->nullRow = 1;
       
 56162     break;
       
 56163   }
       
 56164   u.bm.res = 1;
       
 56165   assert( u.bm.pC->deferredMoveto==0 );
       
 56166   rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(u.bm.pCrsr, &u.bm.res) :
       
 56167                               sqlite3BtreePrevious(u.bm.pCrsr, &u.bm.res);
       
 56168   u.bm.pC->nullRow = (u8)u.bm.res;
       
 56169   u.bm.pC->cacheStatus = CACHE_STALE;
       
 56170   if( u.bm.res==0 ){
       
 56171     pc = pOp->p2 - 1;
       
 56172     if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
       
 56173 #ifdef SQLITE_TEST
       
 56174     sqlite3_search_count++;
       
 56175 #endif
       
 56176   }
       
 56177   u.bm.pC->rowidIsValid = 0;
       
 56178   break;
       
 56179 }
       
 56180 
       
 56181 /* Opcode: IdxInsert P1 P2 P3 * P5
       
 56182 **
       
 56183 ** Register P2 holds a SQL index key made using the
       
 56184 ** MakeRecord instructions.  This opcode writes that key
       
 56185 ** into the index P1.  Data for the entry is nil.
       
 56186 **
       
 56187 ** P3 is a flag that provides a hint to the b-tree layer that this
       
 56188 ** insert is likely to be an append.
       
 56189 **
       
 56190 ** This instruction only works for indices.  The equivalent instruction
       
 56191 ** for tables is OP_Insert.
       
 56192 */
       
 56193 case OP_IdxInsert: {        /* in2 */
       
 56194 #if 0  /* local variables moved into u.bn */
       
 56195   VdbeCursor *pC;
       
 56196   BtCursor *pCrsr;
       
 56197   int nKey;
       
 56198   const char *zKey;
       
 56199 #endif /* local variables moved into u.bn */
       
 56200 
       
 56201   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 56202   u.bn.pC = p->apCsr[pOp->p1];
       
 56203   assert( u.bn.pC!=0 );
       
 56204   assert( pIn2->flags & MEM_Blob );
       
 56205   u.bn.pCrsr = u.bn.pC->pCursor;
       
 56206   if( ALWAYS(u.bn.pCrsr!=0) ){
       
 56207     assert( u.bn.pC->isTable==0 );
       
 56208     rc = ExpandBlob(pIn2);
       
 56209     if( rc==SQLITE_OK ){
       
 56210       u.bn.nKey = pIn2->n;
       
 56211       u.bn.zKey = pIn2->z;
       
 56212       rc = sqlite3BtreeInsert(u.bn.pCrsr, u.bn.zKey, u.bn.nKey, "", 0, 0, pOp->p3,
       
 56213           ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bn.pC->seekResult : 0)
       
 56214       );
       
 56215       assert( u.bn.pC->deferredMoveto==0 );
       
 56216       u.bn.pC->cacheStatus = CACHE_STALE;
       
 56217     }
       
 56218   }
       
 56219   break;
       
 56220 }
       
 56221 
       
 56222 /* Opcode: IdxDelete P1 P2 P3 * *
       
 56223 **
       
 56224 ** The content of P3 registers starting at register P2 form
       
 56225 ** an unpacked index key. This opcode removes that entry from the 
       
 56226 ** index opened by cursor P1.
       
 56227 */
       
 56228 case OP_IdxDelete: {
       
 56229 #if 0  /* local variables moved into u.bo */
       
 56230   VdbeCursor *pC;
       
 56231   BtCursor *pCrsr;
       
 56232   int res;
       
 56233   UnpackedRecord r;
       
 56234 #endif /* local variables moved into u.bo */
       
 56235 
       
 56236   assert( pOp->p3>0 );
       
 56237   assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
       
 56238   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 56239   u.bo.pC = p->apCsr[pOp->p1];
       
 56240   assert( u.bo.pC!=0 );
       
 56241   u.bo.pCrsr = u.bo.pC->pCursor;
       
 56242   if( ALWAYS(u.bo.pCrsr!=0) ){
       
 56243     u.bo.r.pKeyInfo = u.bo.pC->pKeyInfo;
       
 56244     u.bo.r.nField = (u16)pOp->p3;
       
 56245     u.bo.r.flags = 0;
       
 56246     u.bo.r.aMem = &p->aMem[pOp->p2];
       
 56247     rc = sqlite3BtreeMovetoUnpacked(u.bo.pCrsr, &u.bo.r, 0, 0, &u.bo.res);
       
 56248     if( rc==SQLITE_OK && u.bo.res==0 ){
       
 56249       rc = sqlite3BtreeDelete(u.bo.pCrsr);
       
 56250     }
       
 56251     assert( u.bo.pC->deferredMoveto==0 );
       
 56252     u.bo.pC->cacheStatus = CACHE_STALE;
       
 56253   }
       
 56254   break;
       
 56255 }
       
 56256 
       
 56257 /* Opcode: IdxRowid P1 P2 * * *
       
 56258 **
       
 56259 ** Write into register P2 an integer which is the last entry in the record at
       
 56260 ** the end of the index key pointed to by cursor P1.  This integer should be
       
 56261 ** the rowid of the table entry to which this index entry points.
       
 56262 **
       
 56263 ** See also: Rowid, MakeRecord.
       
 56264 */
       
 56265 case OP_IdxRowid: {              /* out2-prerelease */
       
 56266 #if 0  /* local variables moved into u.bp */
       
 56267   BtCursor *pCrsr;
       
 56268   VdbeCursor *pC;
       
 56269   i64 rowid;
       
 56270 #endif /* local variables moved into u.bp */
       
 56271 
       
 56272   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 56273   u.bp.pC = p->apCsr[pOp->p1];
       
 56274   assert( u.bp.pC!=0 );
       
 56275   u.bp.pCrsr = u.bp.pC->pCursor;
       
 56276   if( ALWAYS(u.bp.pCrsr!=0) ){
       
 56277     rc = sqlite3VdbeCursorMoveto(u.bp.pC);
       
 56278     if( NEVER(rc) ) goto abort_due_to_error;
       
 56279     assert( u.bp.pC->deferredMoveto==0 );
       
 56280     assert( u.bp.pC->isTable==0 );
       
 56281     if( !u.bp.pC->nullRow ){
       
 56282       rc = sqlite3VdbeIdxRowid(db, u.bp.pCrsr, &u.bp.rowid);
       
 56283       if( rc!=SQLITE_OK ){
       
 56284         goto abort_due_to_error;
       
 56285       }
       
 56286       MemSetTypeFlag(pOut, MEM_Int);
       
 56287       pOut->u.i = u.bp.rowid;
       
 56288     }
       
 56289   }
       
 56290   break;
       
 56291 }
       
 56292 
       
 56293 /* Opcode: IdxGE P1 P2 P3 P4 P5
       
 56294 **
       
 56295 ** The P4 register values beginning with P3 form an unpacked index 
       
 56296 ** key that omits the ROWID.  Compare this key value against the index 
       
 56297 ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
       
 56298 **
       
 56299 ** If the P1 index entry is greater than or equal to the key value
       
 56300 ** then jump to P2.  Otherwise fall through to the next instruction.
       
 56301 **
       
 56302 ** If P5 is non-zero then the key value is increased by an epsilon 
       
 56303 ** prior to the comparison.  This make the opcode work like IdxGT except
       
 56304 ** that if the key from register P3 is a prefix of the key in the cursor,
       
 56305 ** the result is false whereas it would be true with IdxGT.
       
 56306 */
       
 56307 /* Opcode: IdxLT P1 P2 P3 * P5
       
 56308 **
       
 56309 ** The P4 register values beginning with P3 form an unpacked index 
       
 56310 ** key that omits the ROWID.  Compare this key value against the index 
       
 56311 ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
       
 56312 **
       
 56313 ** If the P1 index entry is less than the key value then jump to P2.
       
 56314 ** Otherwise fall through to the next instruction.
       
 56315 **
       
 56316 ** If P5 is non-zero then the key value is increased by an epsilon prior 
       
 56317 ** to the comparison.  This makes the opcode work like IdxLE.
       
 56318 */
       
 56319 case OP_IdxLT:          /* jump, in3 */
       
 56320 case OP_IdxGE: {        /* jump, in3 */
       
 56321 #if 0  /* local variables moved into u.bq */
       
 56322   VdbeCursor *pC;
       
 56323   int res;
       
 56324   UnpackedRecord r;
       
 56325 #endif /* local variables moved into u.bq */
       
 56326 
       
 56327   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
       
 56328   u.bq.pC = p->apCsr[pOp->p1];
       
 56329   assert( u.bq.pC!=0 );
       
 56330   if( ALWAYS(u.bq.pC->pCursor!=0) ){
       
 56331     assert( u.bq.pC->deferredMoveto==0 );
       
 56332     assert( pOp->p5==0 || pOp->p5==1 );
       
 56333     assert( pOp->p4type==P4_INT32 );
       
 56334     u.bq.r.pKeyInfo = u.bq.pC->pKeyInfo;
       
 56335     u.bq.r.nField = (u16)pOp->p4.i;
       
 56336     if( pOp->p5 ){
       
 56337       u.bq.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
       
 56338     }else{
       
 56339       u.bq.r.flags = UNPACKED_IGNORE_ROWID;
       
 56340     }
       
 56341     u.bq.r.aMem = &p->aMem[pOp->p3];
       
 56342     rc = sqlite3VdbeIdxKeyCompare(u.bq.pC, &u.bq.r, &u.bq.res);
       
 56343     if( pOp->opcode==OP_IdxLT ){
       
 56344       u.bq.res = -u.bq.res;
       
 56345     }else{
       
 56346       assert( pOp->opcode==OP_IdxGE );
       
 56347       u.bq.res++;
       
 56348     }
       
 56349     if( u.bq.res>0 ){
       
 56350       pc = pOp->p2 - 1 ;
       
 56351     }
       
 56352   }
       
 56353   break;
       
 56354 }
       
 56355 
       
 56356 /* Opcode: Destroy P1 P2 P3 * *
       
 56357 **
       
 56358 ** Delete an entire database table or index whose root page in the database
       
 56359 ** file is given by P1.
       
 56360 **
       
 56361 ** The table being destroyed is in the main database file if P3==0.  If
       
 56362 ** P3==1 then the table to be clear is in the auxiliary database file
       
 56363 ** that is used to store tables create using CREATE TEMPORARY TABLE.
       
 56364 **
       
 56365 ** If AUTOVACUUM is enabled then it is possible that another root page
       
 56366 ** might be moved into the newly deleted root page in order to keep all
       
 56367 ** root pages contiguous at the beginning of the database.  The former
       
 56368 ** value of the root page that moved - its value before the move occurred -
       
 56369 ** is stored in register P2.  If no page 
       
 56370 ** movement was required (because the table being dropped was already 
       
 56371 ** the last one in the database) then a zero is stored in register P2.
       
 56372 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
       
 56373 **
       
 56374 ** See also: Clear
       
 56375 */
       
 56376 case OP_Destroy: {     /* out2-prerelease */
       
 56377 #if 0  /* local variables moved into u.br */
       
 56378   int iMoved;
       
 56379   int iCnt;
       
 56380   Vdbe *pVdbe;
       
 56381   int iDb;
       
 56382 #endif /* local variables moved into u.br */
       
 56383 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 56384   u.br.iCnt = 0;
       
 56385   for(u.br.pVdbe=db->pVdbe; u.br.pVdbe; u.br.pVdbe = u.br.pVdbe->pNext){
       
 56386     if( u.br.pVdbe->magic==VDBE_MAGIC_RUN && u.br.pVdbe->inVtabMethod<2 && u.br.pVdbe->pc>=0 ){
       
 56387       u.br.iCnt++;
       
 56388     }
       
 56389   }
       
 56390 #else
       
 56391   u.br.iCnt = db->activeVdbeCnt;
       
 56392 #endif
       
 56393   if( u.br.iCnt>1 ){
       
 56394     rc = SQLITE_LOCKED;
       
 56395     p->errorAction = OE_Abort;
       
 56396   }else{
       
 56397     u.br.iDb = pOp->p3;
       
 56398     assert( u.br.iCnt==1 );
       
 56399     assert( (p->btreeMask & (1<<u.br.iDb))!=0 );
       
 56400     rc = sqlite3BtreeDropTable(db->aDb[u.br.iDb].pBt, pOp->p1, &u.br.iMoved);
       
 56401     MemSetTypeFlag(pOut, MEM_Int);
       
 56402     pOut->u.i = u.br.iMoved;
       
 56403 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 56404     if( rc==SQLITE_OK && u.br.iMoved!=0 ){
       
 56405       sqlite3RootPageMoved(&db->aDb[u.br.iDb], u.br.iMoved, pOp->p1);
       
 56406     }
       
 56407 #endif
       
 56408   }
       
 56409   break;
       
 56410 }
       
 56411 
       
 56412 /* Opcode: Clear P1 P2 P3
       
 56413 **
       
 56414 ** Delete all contents of the database table or index whose root page
       
 56415 ** in the database file is given by P1.  But, unlike Destroy, do not
       
 56416 ** remove the table or index from the database file.
       
 56417 **
       
 56418 ** The table being clear is in the main database file if P2==0.  If
       
 56419 ** P2==1 then the table to be clear is in the auxiliary database file
       
 56420 ** that is used to store tables create using CREATE TEMPORARY TABLE.
       
 56421 **
       
 56422 ** If the P3 value is non-zero, then the table referred to must be an
       
 56423 ** intkey table (an SQL table, not an index). In this case the row change 
       
 56424 ** count is incremented by the number of rows in the table being cleared. 
       
 56425 ** If P3 is greater than zero, then the value stored in register P3 is
       
 56426 ** also incremented by the number of rows in the table being cleared.
       
 56427 **
       
 56428 ** See also: Destroy
       
 56429 */
       
 56430 case OP_Clear: {
       
 56431 #if 0  /* local variables moved into u.bs */
       
 56432   int nChange;
       
 56433 #endif /* local variables moved into u.bs */
       
 56434 
       
 56435   u.bs.nChange = 0;
       
 56436   assert( (p->btreeMask & (1<<pOp->p2))!=0 );
       
 56437   rc = sqlite3BtreeClearTable(
       
 56438       db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bs.nChange : 0)
       
 56439   );
       
 56440   if( pOp->p3 ){
       
 56441     p->nChange += u.bs.nChange;
       
 56442     if( pOp->p3>0 ){
       
 56443       p->aMem[pOp->p3].u.i += u.bs.nChange;
       
 56444     }
       
 56445   }
       
 56446   break;
       
 56447 }
       
 56448 
       
 56449 /* Opcode: CreateTable P1 P2 * * *
       
 56450 **
       
 56451 ** Allocate a new table in the main database file if P1==0 or in the
       
 56452 ** auxiliary database file if P1==1 or in an attached database if
       
 56453 ** P1>1.  Write the root page number of the new table into
       
 56454 ** register P2
       
 56455 **
       
 56456 ** The difference between a table and an index is this:  A table must
       
 56457 ** have a 4-byte integer key and can have arbitrary data.  An index
       
 56458 ** has an arbitrary key but no data.
       
 56459 **
       
 56460 ** See also: CreateIndex
       
 56461 */
       
 56462 /* Opcode: CreateIndex P1 P2 * * *
       
 56463 **
       
 56464 ** Allocate a new index in the main database file if P1==0 or in the
       
 56465 ** auxiliary database file if P1==1 or in an attached database if
       
 56466 ** P1>1.  Write the root page number of the new table into
       
 56467 ** register P2.
       
 56468 **
       
 56469 ** See documentation on OP_CreateTable for additional information.
       
 56470 */
       
 56471 case OP_CreateIndex:            /* out2-prerelease */
       
 56472 case OP_CreateTable: {          /* out2-prerelease */
       
 56473 #if 0  /* local variables moved into u.bt */
       
 56474   int pgno;
       
 56475   int flags;
       
 56476   Db *pDb;
       
 56477 #endif /* local variables moved into u.bt */
       
 56478 
       
 56479   u.bt.pgno = 0;
       
 56480   assert( pOp->p1>=0 && pOp->p1<db->nDb );
       
 56481   assert( (p->btreeMask & (1<<pOp->p1))!=0 );
       
 56482   u.bt.pDb = &db->aDb[pOp->p1];
       
 56483   assert( u.bt.pDb->pBt!=0 );
       
 56484   if( pOp->opcode==OP_CreateTable ){
       
 56485     /* u.bt.flags = BTREE_INTKEY; */
       
 56486     u.bt.flags = BTREE_LEAFDATA|BTREE_INTKEY;
       
 56487   }else{
       
 56488     u.bt.flags = BTREE_ZERODATA;
       
 56489   }
       
 56490   rc = sqlite3BtreeCreateTable(u.bt.pDb->pBt, &u.bt.pgno, u.bt.flags);
       
 56491   pOut->u.i = u.bt.pgno;
       
 56492   MemSetTypeFlag(pOut, MEM_Int);
       
 56493   break;
       
 56494 }
       
 56495 
       
 56496 /* Opcode: ParseSchema P1 P2 * P4 *
       
 56497 **
       
 56498 ** Read and parse all entries from the SQLITE_MASTER table of database P1
       
 56499 ** that match the WHERE clause P4.  P2 is the "force" flag.   Always do
       
 56500 ** the parsing if P2 is true.  If P2 is false, then this routine is a
       
 56501 ** no-op if the schema is not currently loaded.  In other words, if P2
       
 56502 ** is false, the SQLITE_MASTER table is only parsed if the rest of the
       
 56503 ** schema is already loaded into the symbol table.
       
 56504 **
       
 56505 ** This opcode invokes the parser to create a new virtual machine,
       
 56506 ** then runs the new virtual machine.  It is thus a re-entrant opcode.
       
 56507 */
       
 56508 case OP_ParseSchema: {
       
 56509 #if 0  /* local variables moved into u.bu */
       
 56510   int iDb;
       
 56511   const char *zMaster;
       
 56512   char *zSql;
       
 56513   InitData initData;
       
 56514 #endif /* local variables moved into u.bu */
       
 56515 
       
 56516   u.bu.iDb = pOp->p1;
       
 56517   assert( u.bu.iDb>=0 && u.bu.iDb<db->nDb );
       
 56518 
       
 56519   /* If pOp->p2 is 0, then this opcode is being executed to read a
       
 56520   ** single row, for example the row corresponding to a new index
       
 56521   ** created by this VDBE, from the sqlite_master table. It only
       
 56522   ** does this if the corresponding in-memory schema is currently
       
 56523   ** loaded. Otherwise, the new index definition can be loaded along
       
 56524   ** with the rest of the schema when it is required.
       
 56525   **
       
 56526   ** Although the mutex on the BtShared object that corresponds to
       
 56527   ** database u.bu.iDb (the database containing the sqlite_master table
       
 56528   ** read by this instruction) is currently held, it is necessary to
       
 56529   ** obtain the mutexes on all attached databases before checking if
       
 56530   ** the schema of u.bu.iDb is loaded. This is because, at the start of
       
 56531   ** the sqlite3_exec() call below, SQLite will invoke
       
 56532   ** sqlite3BtreeEnterAll(). If all mutexes are not already held, the
       
 56533   ** u.bu.iDb mutex may be temporarily released to avoid deadlock. If
       
 56534   ** this happens, then some other thread may delete the in-memory
       
 56535   ** schema of database u.bu.iDb before the SQL statement runs. The schema
       
 56536   ** will not be reloaded becuase the db->init.busy flag is set. This
       
 56537   ** can result in a "no such table: sqlite_master" or "malformed
       
 56538   ** database schema" error being returned to the user.
       
 56539   */
       
 56540   assert( sqlite3BtreeHoldsMutex(db->aDb[u.bu.iDb].pBt) );
       
 56541   sqlite3BtreeEnterAll(db);
       
 56542   if( pOp->p2 || DbHasProperty(db, u.bu.iDb, DB_SchemaLoaded) ){
       
 56543     u.bu.zMaster = SCHEMA_TABLE(u.bu.iDb);
       
 56544     u.bu.initData.db = db;
       
 56545     u.bu.initData.iDb = pOp->p1;
       
 56546     u.bu.initData.pzErrMsg = &p->zErrMsg;
       
 56547     u.bu.zSql = sqlite3MPrintf(db,
       
 56548        "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
       
 56549        db->aDb[u.bu.iDb].zName, u.bu.zMaster, pOp->p4.z);
       
 56550     if( u.bu.zSql==0 ){
       
 56551       rc = SQLITE_NOMEM;
       
 56552     }else{
       
 56553       (void)sqlite3SafetyOff(db);
       
 56554       assert( db->init.busy==0 );
       
 56555       db->init.busy = 1;
       
 56556       u.bu.initData.rc = SQLITE_OK;
       
 56557       assert( !db->mallocFailed );
       
 56558       rc = sqlite3_exec(db, u.bu.zSql, sqlite3InitCallback, &u.bu.initData, 0);
       
 56559       if( rc==SQLITE_OK ) rc = u.bu.initData.rc;
       
 56560       sqlite3DbFree(db, u.bu.zSql);
       
 56561       db->init.busy = 0;
       
 56562       (void)sqlite3SafetyOn(db);
       
 56563     }
       
 56564   }
       
 56565   sqlite3BtreeLeaveAll(db);
       
 56566   if( rc==SQLITE_NOMEM ){
       
 56567     goto no_mem;
       
 56568   }
       
 56569   break;
       
 56570 }
       
 56571 
       
 56572 #if !defined(SQLITE_OMIT_ANALYZE)
       
 56573 /* Opcode: LoadAnalysis P1 * * * *
       
 56574 **
       
 56575 ** Read the sqlite_stat1 table for database P1 and load the content
       
 56576 ** of that table into the internal index hash table.  This will cause
       
 56577 ** the analysis to be used when preparing all subsequent queries.
       
 56578 */
       
 56579 case OP_LoadAnalysis: {
       
 56580   assert( pOp->p1>=0 && pOp->p1<db->nDb );
       
 56581   rc = sqlite3AnalysisLoad(db, pOp->p1);
       
 56582   break;  
       
 56583 }
       
 56584 #endif /* !defined(SQLITE_OMIT_ANALYZE) */
       
 56585 
       
 56586 /* Opcode: DropTable P1 * * P4 *
       
 56587 **
       
 56588 ** Remove the internal (in-memory) data structures that describe
       
 56589 ** the table named P4 in database P1.  This is called after a table
       
 56590 ** is dropped in order to keep the internal representation of the
       
 56591 ** schema consistent with what is on disk.
       
 56592 */
       
 56593 case OP_DropTable: {
       
 56594   sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
       
 56595   break;
       
 56596 }
       
 56597 
       
 56598 /* Opcode: DropIndex P1 * * P4 *
       
 56599 **
       
 56600 ** Remove the internal (in-memory) data structures that describe
       
 56601 ** the index named P4 in database P1.  This is called after an index
       
 56602 ** is dropped in order to keep the internal representation of the
       
 56603 ** schema consistent with what is on disk.
       
 56604 */
       
 56605 case OP_DropIndex: {
       
 56606   sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
       
 56607   break;
       
 56608 }
       
 56609 
       
 56610 /* Opcode: DropTrigger P1 * * P4 *
       
 56611 **
       
 56612 ** Remove the internal (in-memory) data structures that describe
       
 56613 ** the trigger named P4 in database P1.  This is called after a trigger
       
 56614 ** is dropped in order to keep the internal representation of the
       
 56615 ** schema consistent with what is on disk.
       
 56616 */
       
 56617 case OP_DropTrigger: {
       
 56618   sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
       
 56619   break;
       
 56620 }
       
 56621 
       
 56622 
       
 56623 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
       
 56624 /* Opcode: IntegrityCk P1 P2 P3 * P5
       
 56625 **
       
 56626 ** Do an analysis of the currently open database.  Store in
       
 56627 ** register P1 the text of an error message describing any problems.
       
 56628 ** If no problems are found, store a NULL in register P1.
       
 56629 **
       
 56630 ** The register P3 contains the maximum number of allowed errors.
       
 56631 ** At most reg(P3) errors will be reported.
       
 56632 ** In other words, the analysis stops as soon as reg(P1) errors are 
       
 56633 ** seen.  Reg(P1) is updated with the number of errors remaining.
       
 56634 **
       
 56635 ** The root page numbers of all tables in the database are integer
       
 56636 ** stored in reg(P1), reg(P1+1), reg(P1+2), ....  There are P2 tables
       
 56637 ** total.
       
 56638 **
       
 56639 ** If P5 is not zero, the check is done on the auxiliary database
       
 56640 ** file, not the main database file.
       
 56641 **
       
 56642 ** This opcode is used to implement the integrity_check pragma.
       
 56643 */
       
 56644 case OP_IntegrityCk: {
       
 56645 #if 0  /* local variables moved into u.bv */
       
 56646   int nRoot;      /* Number of tables to check.  (Number of root pages.) */
       
 56647   int *aRoot;     /* Array of rootpage numbers for tables to be checked */
       
 56648   int j;          /* Loop counter */
       
 56649   int nErr;       /* Number of errors reported */
       
 56650   char *z;        /* Text of the error report */
       
 56651   Mem *pnErr;     /* Register keeping track of errors remaining */
       
 56652 #endif /* local variables moved into u.bv */
       
 56653 
       
 56654   u.bv.nRoot = pOp->p2;
       
 56655   assert( u.bv.nRoot>0 );
       
 56656   u.bv.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bv.nRoot+1) );
       
 56657   if( u.bv.aRoot==0 ) goto no_mem;
       
 56658   assert( pOp->p3>0 && pOp->p3<=p->nMem );
       
 56659   u.bv.pnErr = &p->aMem[pOp->p3];
       
 56660   assert( (u.bv.pnErr->flags & MEM_Int)!=0 );
       
 56661   assert( (u.bv.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
       
 56662   pIn1 = &p->aMem[pOp->p1];
       
 56663   for(u.bv.j=0; u.bv.j<u.bv.nRoot; u.bv.j++){
       
 56664     u.bv.aRoot[u.bv.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bv.j]);
       
 56665   }
       
 56666   u.bv.aRoot[u.bv.j] = 0;
       
 56667   assert( pOp->p5<db->nDb );
       
 56668   assert( (p->btreeMask & (1<<pOp->p5))!=0 );
       
 56669   u.bv.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bv.aRoot, u.bv.nRoot,
       
 56670                                  (int)u.bv.pnErr->u.i, &u.bv.nErr);
       
 56671   sqlite3DbFree(db, u.bv.aRoot);
       
 56672   u.bv.pnErr->u.i -= u.bv.nErr;
       
 56673   sqlite3VdbeMemSetNull(pIn1);
       
 56674   if( u.bv.nErr==0 ){
       
 56675     assert( u.bv.z==0 );
       
 56676   }else if( u.bv.z==0 ){
       
 56677     goto no_mem;
       
 56678   }else{
       
 56679     sqlite3VdbeMemSetStr(pIn1, u.bv.z, -1, SQLITE_UTF8, sqlite3_free);
       
 56680   }
       
 56681   UPDATE_MAX_BLOBSIZE(pIn1);
       
 56682   sqlite3VdbeChangeEncoding(pIn1, encoding);
       
 56683   break;
       
 56684 }
       
 56685 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
       
 56686 
       
 56687 /* Opcode: RowSetAdd P1 P2 * * *
       
 56688 **
       
 56689 ** Insert the integer value held by register P2 into a boolean index
       
 56690 ** held in register P1.
       
 56691 **
       
 56692 ** An assertion fails if P2 is not an integer.
       
 56693 */
       
 56694 case OP_RowSetAdd: {       /* in2 */
       
 56695 #if 0  /* local variables moved into u.bw */
       
 56696   Mem *pIdx;
       
 56697   Mem *pVal;
       
 56698 #endif /* local variables moved into u.bw */
       
 56699   assert( pOp->p1>0 && pOp->p1<=p->nMem );
       
 56700   u.bw.pIdx = &p->aMem[pOp->p1];
       
 56701   assert( pOp->p2>0 && pOp->p2<=p->nMem );
       
 56702   u.bw.pVal = &p->aMem[pOp->p2];
       
 56703   assert( (u.bw.pVal->flags & MEM_Int)!=0 );
       
 56704   if( (u.bw.pIdx->flags & MEM_RowSet)==0 ){
       
 56705     sqlite3VdbeMemSetRowSet(u.bw.pIdx);
       
 56706     if( (u.bw.pIdx->flags & MEM_RowSet)==0 ) goto no_mem;
       
 56707   }
       
 56708   sqlite3RowSetInsert(u.bw.pIdx->u.pRowSet, u.bw.pVal->u.i);
       
 56709   break;
       
 56710 }
       
 56711 
       
 56712 /* Opcode: RowSetRead P1 P2 P3 * *
       
 56713 **
       
 56714 ** Extract the smallest value from boolean index P1 and put that value into
       
 56715 ** register P3.  Or, if boolean index P1 is initially empty, leave P3
       
 56716 ** unchanged and jump to instruction P2.
       
 56717 */
       
 56718 case OP_RowSetRead: {       /* jump, out3 */
       
 56719 #if 0  /* local variables moved into u.bx */
       
 56720   Mem *pIdx;
       
 56721   i64 val;
       
 56722 #endif /* local variables moved into u.bx */
       
 56723   assert( pOp->p1>0 && pOp->p1<=p->nMem );
       
 56724   CHECK_FOR_INTERRUPT;
       
 56725   u.bx.pIdx = &p->aMem[pOp->p1];
       
 56726   pOut = &p->aMem[pOp->p3];
       
 56727   if( (u.bx.pIdx->flags & MEM_RowSet)==0
       
 56728    || sqlite3RowSetNext(u.bx.pIdx->u.pRowSet, &u.bx.val)==0
       
 56729   ){
       
 56730     /* The boolean index is empty */
       
 56731     sqlite3VdbeMemSetNull(u.bx.pIdx);
       
 56732     pc = pOp->p2 - 1;
       
 56733   }else{
       
 56734     /* A value was pulled from the index */
       
 56735     assert( pOp->p3>0 && pOp->p3<=p->nMem );
       
 56736     sqlite3VdbeMemSetInt64(pOut, u.bx.val);
       
 56737   }
       
 56738   break;
       
 56739 }
       
 56740 
       
 56741 /* Opcode: RowSetTest P1 P2 P3 P4
       
 56742 **
       
 56743 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
       
 56744 ** contains a RowSet object and that RowSet object contains
       
 56745 ** the value held in P3, jump to register P2. Otherwise, insert the
       
 56746 ** integer in P3 into the RowSet and continue on to the
       
 56747 ** next opcode.
       
 56748 **
       
 56749 ** The RowSet object is optimized for the case where successive sets
       
 56750 ** of integers, where each set contains no duplicates. Each set
       
 56751 ** of values is identified by a unique P4 value. The first set
       
 56752 ** must have P4==0, the final set P4=-1.  P4 must be either -1 or
       
 56753 ** non-negative.  For non-negative values of P4 only the lower 4
       
 56754 ** bits are significant.
       
 56755 **
       
 56756 ** This allows optimizations: (a) when P4==0 there is no need to test
       
 56757 ** the rowset object for P3, as it is guaranteed not to contain it,
       
 56758 ** (b) when P4==-1 there is no need to insert the value, as it will
       
 56759 ** never be tested for, and (c) when a value that is part of set X is
       
 56760 ** inserted, there is no need to search to see if the same value was
       
 56761 ** previously inserted as part of set X (only if it was previously
       
 56762 ** inserted as part of some other set).
       
 56763 */
       
 56764 case OP_RowSetTest: {                     /* jump, in1, in3 */
       
 56765 #if 0  /* local variables moved into u.by */
       
 56766   int iSet;
       
 56767   int exists;
       
 56768 #endif /* local variables moved into u.by */
       
 56769 
       
 56770   u.by.iSet = pOp->p4.i;
       
 56771   assert( pIn3->flags&MEM_Int );
       
 56772 
       
 56773   /* If there is anything other than a rowset object in memory cell P1,
       
 56774   ** delete it now and initialize P1 with an empty rowset
       
 56775   */
       
 56776   if( (pIn1->flags & MEM_RowSet)==0 ){
       
 56777     sqlite3VdbeMemSetRowSet(pIn1);
       
 56778     if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
       
 56779   }
       
 56780 
       
 56781   assert( pOp->p4type==P4_INT32 );
       
 56782   assert( u.by.iSet==-1 || u.by.iSet>=0 );
       
 56783   if( u.by.iSet ){
       
 56784     u.by.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
       
 56785                                (u8)(u.by.iSet>=0 ? u.by.iSet & 0xf : 0xff),
       
 56786                                pIn3->u.i);
       
 56787     if( u.by.exists ){
       
 56788       pc = pOp->p2 - 1;
       
 56789       break;
       
 56790     }
       
 56791   }
       
 56792   if( u.by.iSet>=0 ){
       
 56793     sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
       
 56794   }
       
 56795   break;
       
 56796 }
       
 56797 
       
 56798 
       
 56799 #ifndef SQLITE_OMIT_TRIGGER
       
 56800 
       
 56801 /* Opcode: Program P1 P2 P3 P4 *
       
 56802 **
       
 56803 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). 
       
 56804 **
       
 56805 ** P1 contains the address of the memory cell that contains the first memory 
       
 56806 ** cell in an array of values used as arguments to the sub-program. P2 
       
 56807 ** contains the address to jump to if the sub-program throws an IGNORE 
       
 56808 ** exception using the RAISE() function. Register P3 contains the address 
       
 56809 ** of a memory cell in this (the parent) VM that is used to allocate the 
       
 56810 ** memory required by the sub-vdbe at runtime.
       
 56811 **
       
 56812 ** P4 is a pointer to the VM containing the trigger program.
       
 56813 */
       
 56814 case OP_Program: {        /* jump */
       
 56815 #if 0  /* local variables moved into u.bz */
       
 56816   int nMem;               /* Number of memory registers for sub-program */
       
 56817   int nByte;              /* Bytes of runtime space required for sub-program */
       
 56818   Mem *pRt;               /* Register to allocate runtime space */
       
 56819   Mem *pMem;              /* Used to iterate through memory cells */
       
 56820   Mem *pEnd;              /* Last memory cell in new array */
       
 56821   VdbeFrame *pFrame;      /* New vdbe frame to execute in */
       
 56822   SubProgram *pProgram;   /* Sub-program to execute */
       
 56823   void *t;                /* Token identifying trigger */
       
 56824 #endif /* local variables moved into u.bz */
       
 56825 
       
 56826   u.bz.pProgram = pOp->p4.pProgram;
       
 56827   u.bz.pRt = &p->aMem[pOp->p3];
       
 56828   assert( u.bz.pProgram->nOp>0 );
       
 56829 
       
 56830   /* If the p5 flag is clear, then recursive invocation of triggers is
       
 56831   ** disabled for backwards compatibility (p5 is set if this sub-program
       
 56832   ** is really a trigger, not a foreign key action, and the flag set
       
 56833   ** and cleared by the "PRAGMA recursive_triggers" command is clear).
       
 56834   **
       
 56835   ** It is recursive invocation of triggers, at the SQL level, that is
       
 56836   ** disabled. In some cases a single trigger may generate more than one
       
 56837   ** SubProgram (if the trigger may be executed with more than one different
       
 56838   ** ON CONFLICT algorithm). SubProgram structures associated with a
       
 56839   ** single trigger all have the same value for the SubProgram.token
       
 56840   ** variable.  */
       
 56841   if( pOp->p5 ){
       
 56842     u.bz.t = u.bz.pProgram->token;
       
 56843     for(u.bz.pFrame=p->pFrame; u.bz.pFrame && u.bz.pFrame->token!=u.bz.t; u.bz.pFrame=u.bz.pFrame->pParent);
       
 56844     if( u.bz.pFrame ) break;
       
 56845   }
       
 56846 
       
 56847   if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
       
 56848     rc = SQLITE_ERROR;
       
 56849     sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
       
 56850     break;
       
 56851   }
       
 56852 
       
 56853   /* Register u.bz.pRt is used to store the memory required to save the state
       
 56854   ** of the current program, and the memory required at runtime to execute
       
 56855   ** the trigger program. If this trigger has been fired before, then u.bz.pRt
       
 56856   ** is already allocated. Otherwise, it must be initialized.  */
       
 56857   if( (u.bz.pRt->flags&MEM_Frame)==0 ){
       
 56858     /* SubProgram.nMem is set to the number of memory cells used by the
       
 56859     ** program stored in SubProgram.aOp. As well as these, one memory
       
 56860     ** cell is required for each cursor used by the program. Set local
       
 56861     ** variable u.bz.nMem (and later, VdbeFrame.nChildMem) to this value.
       
 56862     */
       
 56863     u.bz.nMem = u.bz.pProgram->nMem + u.bz.pProgram->nCsr;
       
 56864     u.bz.nByte = ROUND8(sizeof(VdbeFrame))
       
 56865               + u.bz.nMem * sizeof(Mem)
       
 56866               + u.bz.pProgram->nCsr * sizeof(VdbeCursor *);
       
 56867     u.bz.pFrame = sqlite3DbMallocZero(db, u.bz.nByte);
       
 56868     if( !u.bz.pFrame ){
       
 56869       goto no_mem;
       
 56870     }
       
 56871     sqlite3VdbeMemRelease(u.bz.pRt);
       
 56872     u.bz.pRt->flags = MEM_Frame;
       
 56873     u.bz.pRt->u.pFrame = u.bz.pFrame;
       
 56874 
       
 56875     u.bz.pFrame->v = p;
       
 56876     u.bz.pFrame->nChildMem = u.bz.nMem;
       
 56877     u.bz.pFrame->nChildCsr = u.bz.pProgram->nCsr;
       
 56878     u.bz.pFrame->pc = pc;
       
 56879     u.bz.pFrame->aMem = p->aMem;
       
 56880     u.bz.pFrame->nMem = p->nMem;
       
 56881     u.bz.pFrame->apCsr = p->apCsr;
       
 56882     u.bz.pFrame->nCursor = p->nCursor;
       
 56883     u.bz.pFrame->aOp = p->aOp;
       
 56884     u.bz.pFrame->nOp = p->nOp;
       
 56885     u.bz.pFrame->token = u.bz.pProgram->token;
       
 56886 
       
 56887     u.bz.pEnd = &VdbeFrameMem(u.bz.pFrame)[u.bz.pFrame->nChildMem];
       
 56888     for(u.bz.pMem=VdbeFrameMem(u.bz.pFrame); u.bz.pMem!=u.bz.pEnd; u.bz.pMem++){
       
 56889       u.bz.pMem->flags = MEM_Null;
       
 56890       u.bz.pMem->db = db;
       
 56891     }
       
 56892   }else{
       
 56893     u.bz.pFrame = u.bz.pRt->u.pFrame;
       
 56894     assert( u.bz.pProgram->nMem+u.bz.pProgram->nCsr==u.bz.pFrame->nChildMem );
       
 56895     assert( u.bz.pProgram->nCsr==u.bz.pFrame->nChildCsr );
       
 56896     assert( pc==u.bz.pFrame->pc );
       
 56897   }
       
 56898 
       
 56899   p->nFrame++;
       
 56900   u.bz.pFrame->pParent = p->pFrame;
       
 56901   u.bz.pFrame->lastRowid = db->lastRowid;
       
 56902   u.bz.pFrame->nChange = p->nChange;
       
 56903   p->nChange = 0;
       
 56904   p->pFrame = u.bz.pFrame;
       
 56905   p->aMem = &VdbeFrameMem(u.bz.pFrame)[-1];
       
 56906   p->nMem = u.bz.pFrame->nChildMem;
       
 56907   p->nCursor = (u16)u.bz.pFrame->nChildCsr;
       
 56908   p->apCsr = (VdbeCursor **)&p->aMem[p->nMem+1];
       
 56909   p->aOp = u.bz.pProgram->aOp;
       
 56910   p->nOp = u.bz.pProgram->nOp;
       
 56911   pc = -1;
       
 56912 
       
 56913   break;
       
 56914 }
       
 56915 
       
 56916 /* Opcode: Param P1 P2 * * *
       
 56917 **
       
 56918 ** This opcode is only ever present in sub-programs called via the 
       
 56919 ** OP_Program instruction. Copy a value currently stored in a memory 
       
 56920 ** cell of the calling (parent) frame to cell P2 in the current frames 
       
 56921 ** address space. This is used by trigger programs to access the new.* 
       
 56922 ** and old.* values.
       
 56923 **
       
 56924 ** The address of the cell in the parent frame is determined by adding
       
 56925 ** the value of the P1 argument to the value of the P1 argument to the
       
 56926 ** calling OP_Program instruction.
       
 56927 */
       
 56928 case OP_Param: {           /* out2-prerelease */
       
 56929 #if 0  /* local variables moved into u.ca */
       
 56930   VdbeFrame *pFrame;
       
 56931   Mem *pIn;
       
 56932 #endif /* local variables moved into u.ca */
       
 56933   u.ca.pFrame = p->pFrame;
       
 56934   u.ca.pIn = &u.ca.pFrame->aMem[pOp->p1 + u.ca.pFrame->aOp[u.ca.pFrame->pc].p1];
       
 56935   sqlite3VdbeMemShallowCopy(pOut, u.ca.pIn, MEM_Ephem);
       
 56936   break;
       
 56937 }
       
 56938 
       
 56939 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
       
 56940 
       
 56941 #ifndef SQLITE_OMIT_FOREIGN_KEY
       
 56942 /* Opcode: FkCounter P1 P2 * * *
       
 56943 **
       
 56944 ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
       
 56945 ** If P1 is non-zero, the database constraint counter is incremented 
       
 56946 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the 
       
 56947 ** statement counter is incremented (immediate foreign key constraints).
       
 56948 */
       
 56949 case OP_FkCounter: {
       
 56950   if( pOp->p1 ){
       
 56951     db->nDeferredCons += pOp->p2;
       
 56952   }else{
       
 56953     p->nFkConstraint += pOp->p2;
       
 56954   }
       
 56955   break;
       
 56956 }
       
 56957 
       
 56958 /* Opcode: FkIfZero P1 P2 * * *
       
 56959 **
       
 56960 ** This opcode tests if a foreign key constraint-counter is currently zero.
       
 56961 ** If so, jump to instruction P2. Otherwise, fall through to the next 
       
 56962 ** instruction.
       
 56963 **
       
 56964 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
       
 56965 ** is zero (the one that counts deferred constraint violations). If P1 is
       
 56966 ** zero, the jump is taken if the statement constraint-counter is zero
       
 56967 ** (immediate foreign key constraint violations).
       
 56968 */
       
 56969 case OP_FkIfZero: {         /* jump */
       
 56970   if( pOp->p1 ){
       
 56971     if( db->nDeferredCons==0 ) pc = pOp->p2-1;
       
 56972   }else{
       
 56973     if( p->nFkConstraint==0 ) pc = pOp->p2-1;
       
 56974   }
       
 56975   break;
       
 56976 }
       
 56977 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
       
 56978 
       
 56979 #ifndef SQLITE_OMIT_AUTOINCREMENT
       
 56980 /* Opcode: MemMax P1 P2 * * *
       
 56981 **
       
 56982 ** P1 is a register in the root frame of this VM (the root frame is
       
 56983 ** different from the current frame if this instruction is being executed
       
 56984 ** within a sub-program). Set the value of register P1 to the maximum of 
       
 56985 ** its current value and the value in register P2.
       
 56986 **
       
 56987 ** This instruction throws an error if the memory cell is not initially
       
 56988 ** an integer.
       
 56989 */
       
 56990 case OP_MemMax: {        /* in2 */
       
 56991 #if 0  /* local variables moved into u.cb */
       
 56992   Mem *pIn1;
       
 56993   VdbeFrame *pFrame;
       
 56994 #endif /* local variables moved into u.cb */
       
 56995   if( p->pFrame ){
       
 56996     for(u.cb.pFrame=p->pFrame; u.cb.pFrame->pParent; u.cb.pFrame=u.cb.pFrame->pParent);
       
 56997     u.cb.pIn1 = &u.cb.pFrame->aMem[pOp->p1];
       
 56998   }else{
       
 56999     u.cb.pIn1 = &p->aMem[pOp->p1];
       
 57000   }
       
 57001   sqlite3VdbeMemIntegerify(u.cb.pIn1);
       
 57002   sqlite3VdbeMemIntegerify(pIn2);
       
 57003   if( u.cb.pIn1->u.i<pIn2->u.i){
       
 57004     u.cb.pIn1->u.i = pIn2->u.i;
       
 57005   }
       
 57006   break;
       
 57007 }
       
 57008 #endif /* SQLITE_OMIT_AUTOINCREMENT */
       
 57009 
       
 57010 /* Opcode: IfPos P1 P2 * * *
       
 57011 **
       
 57012 ** If the value of register P1 is 1 or greater, jump to P2.
       
 57013 **
       
 57014 ** It is illegal to use this instruction on a register that does
       
 57015 ** not contain an integer.  An assertion fault will result if you try.
       
 57016 */
       
 57017 case OP_IfPos: {        /* jump, in1 */
       
 57018   assert( pIn1->flags&MEM_Int );
       
 57019   if( pIn1->u.i>0 ){
       
 57020      pc = pOp->p2 - 1;
       
 57021   }
       
 57022   break;
       
 57023 }
       
 57024 
       
 57025 /* Opcode: IfNeg P1 P2 * * *
       
 57026 **
       
 57027 ** If the value of register P1 is less than zero, jump to P2. 
       
 57028 **
       
 57029 ** It is illegal to use this instruction on a register that does
       
 57030 ** not contain an integer.  An assertion fault will result if you try.
       
 57031 */
       
 57032 case OP_IfNeg: {        /* jump, in1 */
       
 57033   assert( pIn1->flags&MEM_Int );
       
 57034   if( pIn1->u.i<0 ){
       
 57035      pc = pOp->p2 - 1;
       
 57036   }
       
 57037   break;
       
 57038 }
       
 57039 
       
 57040 /* Opcode: IfZero P1 P2 * * *
       
 57041 **
       
 57042 ** If the value of register P1 is exactly 0, jump to P2. 
       
 57043 **
       
 57044 ** It is illegal to use this instruction on a register that does
       
 57045 ** not contain an integer.  An assertion fault will result if you try.
       
 57046 */
       
 57047 case OP_IfZero: {        /* jump, in1 */
       
 57048   assert( pIn1->flags&MEM_Int );
       
 57049   if( pIn1->u.i==0 ){
       
 57050      pc = pOp->p2 - 1;
       
 57051   }
       
 57052   break;
       
 57053 }
       
 57054 
       
 57055 /* Opcode: AggStep * P2 P3 P4 P5
       
 57056 **
       
 57057 ** Execute the step function for an aggregate.  The
       
 57058 ** function has P5 arguments.   P4 is a pointer to the FuncDef
       
 57059 ** structure that specifies the function.  Use register
       
 57060 ** P3 as the accumulator.
       
 57061 **
       
 57062 ** The P5 arguments are taken from register P2 and its
       
 57063 ** successors.
       
 57064 */
       
 57065 case OP_AggStep: {
       
 57066 #if 0  /* local variables moved into u.cc */
       
 57067   int n;
       
 57068   int i;
       
 57069   Mem *pMem;
       
 57070   Mem *pRec;
       
 57071   sqlite3_context ctx;
       
 57072   sqlite3_value **apVal;
       
 57073 #endif /* local variables moved into u.cc */
       
 57074 
       
 57075   u.cc.n = pOp->p5;
       
 57076   assert( u.cc.n>=0 );
       
 57077   u.cc.pRec = &p->aMem[pOp->p2];
       
 57078   u.cc.apVal = p->apArg;
       
 57079   assert( u.cc.apVal || u.cc.n==0 );
       
 57080   for(u.cc.i=0; u.cc.i<u.cc.n; u.cc.i++, u.cc.pRec++){
       
 57081     u.cc.apVal[u.cc.i] = u.cc.pRec;
       
 57082     storeTypeInfo(u.cc.pRec, encoding);
       
 57083   }
       
 57084   u.cc.ctx.pFunc = pOp->p4.pFunc;
       
 57085   assert( pOp->p3>0 && pOp->p3<=p->nMem );
       
 57086   u.cc.ctx.pMem = u.cc.pMem = &p->aMem[pOp->p3];
       
 57087   u.cc.pMem->n++;
       
 57088   u.cc.ctx.s.flags = MEM_Null;
       
 57089   u.cc.ctx.s.z = 0;
       
 57090   u.cc.ctx.s.zMalloc = 0;
       
 57091   u.cc.ctx.s.xDel = 0;
       
 57092   u.cc.ctx.s.db = db;
       
 57093   u.cc.ctx.isError = 0;
       
 57094   u.cc.ctx.pColl = 0;
       
 57095   if( u.cc.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
       
 57096     assert( pOp>p->aOp );
       
 57097     assert( pOp[-1].p4type==P4_COLLSEQ );
       
 57098     assert( pOp[-1].opcode==OP_CollSeq );
       
 57099     u.cc.ctx.pColl = pOp[-1].p4.pColl;
       
 57100   }
       
 57101   (u.cc.ctx.pFunc->xStep)(&u.cc.ctx, u.cc.n, u.cc.apVal);
       
 57102   if( u.cc.ctx.isError ){
       
 57103     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cc.ctx.s));
       
 57104     rc = u.cc.ctx.isError;
       
 57105   }
       
 57106   sqlite3VdbeMemRelease(&u.cc.ctx.s);
       
 57107   break;
       
 57108 }
       
 57109 
       
 57110 /* Opcode: AggFinal P1 P2 * P4 *
       
 57111 **
       
 57112 ** Execute the finalizer function for an aggregate.  P1 is
       
 57113 ** the memory location that is the accumulator for the aggregate.
       
 57114 **
       
 57115 ** P2 is the number of arguments that the step function takes and
       
 57116 ** P4 is a pointer to the FuncDef for this function.  The P2
       
 57117 ** argument is not used by this opcode.  It is only there to disambiguate
       
 57118 ** functions that can take varying numbers of arguments.  The
       
 57119 ** P4 argument is only needed for the degenerate case where
       
 57120 ** the step function was not previously called.
       
 57121 */
       
 57122 case OP_AggFinal: {
       
 57123 #if 0  /* local variables moved into u.cd */
       
 57124   Mem *pMem;
       
 57125 #endif /* local variables moved into u.cd */
       
 57126   assert( pOp->p1>0 && pOp->p1<=p->nMem );
       
 57127   u.cd.pMem = &p->aMem[pOp->p1];
       
 57128   assert( (u.cd.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
       
 57129   rc = sqlite3VdbeMemFinalize(u.cd.pMem, pOp->p4.pFunc);
       
 57130   if( rc ){
       
 57131     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cd.pMem));
       
 57132   }
       
 57133   sqlite3VdbeChangeEncoding(u.cd.pMem, encoding);
       
 57134   UPDATE_MAX_BLOBSIZE(u.cd.pMem);
       
 57135   if( sqlite3VdbeMemTooBig(u.cd.pMem) ){
       
 57136     goto too_big;
       
 57137   }
       
 57138   break;
       
 57139 }
       
 57140 
       
 57141 
       
 57142 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
       
 57143 /* Opcode: Vacuum * * * * *
       
 57144 **
       
 57145 ** Vacuum the entire database.  This opcode will cause other virtual
       
 57146 ** machines to be created and run.  It may not be called from within
       
 57147 ** a transaction.
       
 57148 */
       
 57149 case OP_Vacuum: {
       
 57150   if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; 
       
 57151   rc = sqlite3RunVacuum(&p->zErrMsg, db);
       
 57152   if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
       
 57153   break;
       
 57154 }
       
 57155 #endif
       
 57156 
       
 57157 #if !defined(SQLITE_OMIT_AUTOVACUUM)
       
 57158 /* Opcode: IncrVacuum P1 P2 * * *
       
 57159 **
       
 57160 ** Perform a single step of the incremental vacuum procedure on
       
 57161 ** the P1 database. If the vacuum has finished, jump to instruction
       
 57162 ** P2. Otherwise, fall through to the next instruction.
       
 57163 */
       
 57164 case OP_IncrVacuum: {        /* jump */
       
 57165 #if 0  /* local variables moved into u.ce */
       
 57166   Btree *pBt;
       
 57167 #endif /* local variables moved into u.ce */
       
 57168 
       
 57169   assert( pOp->p1>=0 && pOp->p1<db->nDb );
       
 57170   assert( (p->btreeMask & (1<<pOp->p1))!=0 );
       
 57171   u.ce.pBt = db->aDb[pOp->p1].pBt;
       
 57172   rc = sqlite3BtreeIncrVacuum(u.ce.pBt);
       
 57173   if( rc==SQLITE_DONE ){
       
 57174     pc = pOp->p2 - 1;
       
 57175     rc = SQLITE_OK;
       
 57176   }
       
 57177   break;
       
 57178 }
       
 57179 #endif
       
 57180 
       
 57181 /* Opcode: Expire P1 * * * *
       
 57182 **
       
 57183 ** Cause precompiled statements to become expired. An expired statement
       
 57184 ** fails with an error code of SQLITE_SCHEMA if it is ever executed 
       
 57185 ** (via sqlite3_step()).
       
 57186 ** 
       
 57187 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
       
 57188 ** then only the currently executing statement is affected. 
       
 57189 */
       
 57190 case OP_Expire: {
       
 57191   if( !pOp->p1 ){
       
 57192     sqlite3ExpirePreparedStatements(db);
       
 57193   }else{
       
 57194     p->expired = 1;
       
 57195   }
       
 57196   break;
       
 57197 }
       
 57198 
       
 57199 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 57200 /* Opcode: TableLock P1 P2 P3 P4 *
       
 57201 **
       
 57202 ** Obtain a lock on a particular table. This instruction is only used when
       
 57203 ** the shared-cache feature is enabled. 
       
 57204 **
       
 57205 ** P1 is the index of the database in sqlite3.aDb[] of the database
       
 57206 ** on which the lock is acquired.  A readlock is obtained if P3==0 or
       
 57207 ** a write lock if P3==1.
       
 57208 **
       
 57209 ** P2 contains the root-page of the table to lock.
       
 57210 **
       
 57211 ** P4 contains a pointer to the name of the table being locked. This is only
       
 57212 ** used to generate an error message if the lock cannot be obtained.
       
 57213 */
       
 57214 case OP_TableLock: {
       
 57215   u8 isWriteLock = (u8)pOp->p3;
       
 57216   if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
       
 57217     int p1 = pOp->p1; 
       
 57218     assert( p1>=0 && p1<db->nDb );
       
 57219     assert( (p->btreeMask & (1<<p1))!=0 );
       
 57220     assert( isWriteLock==0 || isWriteLock==1 );
       
 57221     rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
       
 57222     if( (rc&0xFF)==SQLITE_LOCKED ){
       
 57223       const char *z = pOp->p4.z;
       
 57224       sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
       
 57225     }
       
 57226   }
       
 57227   break;
       
 57228 }
       
 57229 #endif /* SQLITE_OMIT_SHARED_CACHE */
       
 57230 
       
 57231 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 57232 /* Opcode: VBegin * * * P4 *
       
 57233 **
       
 57234 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the 
       
 57235 ** xBegin method for that table.
       
 57236 **
       
 57237 ** Also, whether or not P4 is set, check that this is not being called from
       
 57238 ** within a callback to a virtual table xSync() method. If it is, the error
       
 57239 ** code will be set to SQLITE_LOCKED.
       
 57240 */
       
 57241 case OP_VBegin: {
       
 57242 #if 0  /* local variables moved into u.cf */
       
 57243   VTable *pVTab;
       
 57244 #endif /* local variables moved into u.cf */
       
 57245   u.cf.pVTab = pOp->p4.pVtab;
       
 57246   rc = sqlite3VtabBegin(db, u.cf.pVTab);
       
 57247   if( u.cf.pVTab ){
       
 57248     sqlite3DbFree(db, p->zErrMsg);
       
 57249     p->zErrMsg = u.cf.pVTab->pVtab->zErrMsg;
       
 57250     u.cf.pVTab->pVtab->zErrMsg = 0;
       
 57251   }
       
 57252   break;
       
 57253 }
       
 57254 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 57255 
       
 57256 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 57257 /* Opcode: VCreate P1 * * P4 *
       
 57258 **
       
 57259 ** P4 is the name of a virtual table in database P1. Call the xCreate method
       
 57260 ** for that table.
       
 57261 */
       
 57262 case OP_VCreate: {
       
 57263   rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
       
 57264   break;
       
 57265 }
       
 57266 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 57267 
       
 57268 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 57269 /* Opcode: VDestroy P1 * * P4 *
       
 57270 **
       
 57271 ** P4 is the name of a virtual table in database P1.  Call the xDestroy method
       
 57272 ** of that table.
       
 57273 */
       
 57274 case OP_VDestroy: {
       
 57275   p->inVtabMethod = 2;
       
 57276   rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
       
 57277   p->inVtabMethod = 0;
       
 57278   break;
       
 57279 }
       
 57280 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 57281 
       
 57282 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 57283 /* Opcode: VOpen P1 * * P4 *
       
 57284 **
       
 57285 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
       
 57286 ** P1 is a cursor number.  This opcode opens a cursor to the virtual
       
 57287 ** table and stores that cursor in P1.
       
 57288 */
       
 57289 case OP_VOpen: {
       
 57290 #if 0  /* local variables moved into u.cg */
       
 57291   VdbeCursor *pCur;
       
 57292   sqlite3_vtab_cursor *pVtabCursor;
       
 57293   sqlite3_vtab *pVtab;
       
 57294   sqlite3_module *pModule;
       
 57295 #endif /* local variables moved into u.cg */
       
 57296 
       
 57297   u.cg.pCur = 0;
       
 57298   u.cg.pVtabCursor = 0;
       
 57299   u.cg.pVtab = pOp->p4.pVtab->pVtab;
       
 57300   u.cg.pModule = (sqlite3_module *)u.cg.pVtab->pModule;
       
 57301   assert(u.cg.pVtab && u.cg.pModule);
       
 57302   if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
       
 57303   rc = u.cg.pModule->xOpen(u.cg.pVtab, &u.cg.pVtabCursor);
       
 57304   sqlite3DbFree(db, p->zErrMsg);
       
 57305   p->zErrMsg = u.cg.pVtab->zErrMsg;
       
 57306   u.cg.pVtab->zErrMsg = 0;
       
 57307   if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
       
 57308   if( SQLITE_OK==rc ){
       
 57309     /* Initialize sqlite3_vtab_cursor base class */
       
 57310     u.cg.pVtabCursor->pVtab = u.cg.pVtab;
       
 57311 
       
 57312     /* Initialise vdbe cursor object */
       
 57313     u.cg.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
       
 57314     if( u.cg.pCur ){
       
 57315       u.cg.pCur->pVtabCursor = u.cg.pVtabCursor;
       
 57316       u.cg.pCur->pModule = u.cg.pVtabCursor->pVtab->pModule;
       
 57317     }else{
       
 57318       db->mallocFailed = 1;
       
 57319       u.cg.pModule->xClose(u.cg.pVtabCursor);
       
 57320     }
       
 57321   }
       
 57322   break;
       
 57323 }
       
 57324 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 57325 
       
 57326 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 57327 /* Opcode: VFilter P1 P2 P3 P4 *
       
 57328 **
       
 57329 ** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
       
 57330 ** the filtered result set is empty.
       
 57331 **
       
 57332 ** P4 is either NULL or a string that was generated by the xBestIndex
       
 57333 ** method of the module.  The interpretation of the P4 string is left
       
 57334 ** to the module implementation.
       
 57335 **
       
 57336 ** This opcode invokes the xFilter method on the virtual table specified
       
 57337 ** by P1.  The integer query plan parameter to xFilter is stored in register
       
 57338 ** P3. Register P3+1 stores the argc parameter to be passed to the
       
 57339 ** xFilter method. Registers P3+2..P3+1+argc are the argc
       
 57340 ** additional parameters which are passed to
       
 57341 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
       
 57342 **
       
 57343 ** A jump is made to P2 if the result set after filtering would be empty.
       
 57344 */
       
 57345 case OP_VFilter: {   /* jump */
       
 57346 #if 0  /* local variables moved into u.ch */
       
 57347   int nArg;
       
 57348   int iQuery;
       
 57349   const sqlite3_module *pModule;
       
 57350   Mem *pQuery;
       
 57351   Mem *pArgc;
       
 57352   sqlite3_vtab_cursor *pVtabCursor;
       
 57353   sqlite3_vtab *pVtab;
       
 57354   VdbeCursor *pCur;
       
 57355   int res;
       
 57356   int i;
       
 57357   Mem **apArg;
       
 57358 #endif /* local variables moved into u.ch */
       
 57359 
       
 57360   u.ch.pQuery = &p->aMem[pOp->p3];
       
 57361   u.ch.pArgc = &u.ch.pQuery[1];
       
 57362   u.ch.pCur = p->apCsr[pOp->p1];
       
 57363   REGISTER_TRACE(pOp->p3, u.ch.pQuery);
       
 57364   assert( u.ch.pCur->pVtabCursor );
       
 57365   u.ch.pVtabCursor = u.ch.pCur->pVtabCursor;
       
 57366   u.ch.pVtab = u.ch.pVtabCursor->pVtab;
       
 57367   u.ch.pModule = u.ch.pVtab->pModule;
       
 57368 
       
 57369   /* Grab the index number and argc parameters */
       
 57370   assert( (u.ch.pQuery->flags&MEM_Int)!=0 && u.ch.pArgc->flags==MEM_Int );
       
 57371   u.ch.nArg = (int)u.ch.pArgc->u.i;
       
 57372   u.ch.iQuery = (int)u.ch.pQuery->u.i;
       
 57373 
       
 57374   /* Invoke the xFilter method */
       
 57375   {
       
 57376     u.ch.res = 0;
       
 57377     u.ch.apArg = p->apArg;
       
 57378     for(u.ch.i = 0; u.ch.i<u.ch.nArg; u.ch.i++){
       
 57379       u.ch.apArg[u.ch.i] = &u.ch.pArgc[u.ch.i+1];
       
 57380       storeTypeInfo(u.ch.apArg[u.ch.i], 0);
       
 57381     }
       
 57382 
       
 57383     if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
       
 57384     p->inVtabMethod = 1;
       
 57385     rc = u.ch.pModule->xFilter(u.ch.pVtabCursor, u.ch.iQuery, pOp->p4.z, u.ch.nArg, u.ch.apArg);
       
 57386     p->inVtabMethod = 0;
       
 57387     sqlite3DbFree(db, p->zErrMsg);
       
 57388     p->zErrMsg = u.ch.pVtab->zErrMsg;
       
 57389     u.ch.pVtab->zErrMsg = 0;
       
 57390     if( rc==SQLITE_OK ){
       
 57391       u.ch.res = u.ch.pModule->xEof(u.ch.pVtabCursor);
       
 57392     }
       
 57393     if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
       
 57394 
       
 57395     if( u.ch.res ){
       
 57396       pc = pOp->p2 - 1;
       
 57397     }
       
 57398   }
       
 57399   u.ch.pCur->nullRow = 0;
       
 57400 
       
 57401   break;
       
 57402 }
       
 57403 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 57404 
       
 57405 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 57406 /* Opcode: VColumn P1 P2 P3 * *
       
 57407 **
       
 57408 ** Store the value of the P2-th column of
       
 57409 ** the row of the virtual-table that the 
       
 57410 ** P1 cursor is pointing to into register P3.
       
 57411 */
       
 57412 case OP_VColumn: {
       
 57413 #if 0  /* local variables moved into u.ci */
       
 57414   sqlite3_vtab *pVtab;
       
 57415   const sqlite3_module *pModule;
       
 57416   Mem *pDest;
       
 57417   sqlite3_context sContext;
       
 57418 #endif /* local variables moved into u.ci */
       
 57419 
       
 57420   VdbeCursor *pCur = p->apCsr[pOp->p1];
       
 57421   assert( pCur->pVtabCursor );
       
 57422   assert( pOp->p3>0 && pOp->p3<=p->nMem );
       
 57423   u.ci.pDest = &p->aMem[pOp->p3];
       
 57424   if( pCur->nullRow ){
       
 57425     sqlite3VdbeMemSetNull(u.ci.pDest);
       
 57426     break;
       
 57427   }
       
 57428   u.ci.pVtab = pCur->pVtabCursor->pVtab;
       
 57429   u.ci.pModule = u.ci.pVtab->pModule;
       
 57430   assert( u.ci.pModule->xColumn );
       
 57431   memset(&u.ci.sContext, 0, sizeof(u.ci.sContext));
       
 57432 
       
 57433   /* The output cell may already have a buffer allocated. Move
       
 57434   ** the current contents to u.ci.sContext.s so in case the user-function
       
 57435   ** can use the already allocated buffer instead of allocating a
       
 57436   ** new one.
       
 57437   */
       
 57438   sqlite3VdbeMemMove(&u.ci.sContext.s, u.ci.pDest);
       
 57439   MemSetTypeFlag(&u.ci.sContext.s, MEM_Null);
       
 57440 
       
 57441   if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
       
 57442   rc = u.ci.pModule->xColumn(pCur->pVtabCursor, &u.ci.sContext, pOp->p2);
       
 57443   sqlite3DbFree(db, p->zErrMsg);
       
 57444   p->zErrMsg = u.ci.pVtab->zErrMsg;
       
 57445   u.ci.pVtab->zErrMsg = 0;
       
 57446   if( u.ci.sContext.isError ){
       
 57447     rc = u.ci.sContext.isError;
       
 57448   }
       
 57449 
       
 57450   /* Copy the result of the function to the P3 register. We
       
 57451   ** do this regardless of whether or not an error occurred to ensure any
       
 57452   ** dynamic allocation in u.ci.sContext.s (a Mem struct) is  released.
       
 57453   */
       
 57454   sqlite3VdbeChangeEncoding(&u.ci.sContext.s, encoding);
       
 57455   REGISTER_TRACE(pOp->p3, u.ci.pDest);
       
 57456   sqlite3VdbeMemMove(u.ci.pDest, &u.ci.sContext.s);
       
 57457   UPDATE_MAX_BLOBSIZE(u.ci.pDest);
       
 57458 
       
 57459   if( sqlite3SafetyOn(db) ){
       
 57460     goto abort_due_to_misuse;
       
 57461   }
       
 57462   if( sqlite3VdbeMemTooBig(u.ci.pDest) ){
       
 57463     goto too_big;
       
 57464   }
       
 57465   break;
       
 57466 }
       
 57467 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 57468 
       
 57469 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 57470 /* Opcode: VNext P1 P2 * * *
       
 57471 **
       
 57472 ** Advance virtual table P1 to the next row in its result set and
       
 57473 ** jump to instruction P2.  Or, if the virtual table has reached
       
 57474 ** the end of its result set, then fall through to the next instruction.
       
 57475 */
       
 57476 case OP_VNext: {   /* jump */
       
 57477 #if 0  /* local variables moved into u.cj */
       
 57478   sqlite3_vtab *pVtab;
       
 57479   const sqlite3_module *pModule;
       
 57480   int res;
       
 57481   VdbeCursor *pCur;
       
 57482 #endif /* local variables moved into u.cj */
       
 57483 
       
 57484   u.cj.res = 0;
       
 57485   u.cj.pCur = p->apCsr[pOp->p1];
       
 57486   assert( u.cj.pCur->pVtabCursor );
       
 57487   if( u.cj.pCur->nullRow ){
       
 57488     break;
       
 57489   }
       
 57490   u.cj.pVtab = u.cj.pCur->pVtabCursor->pVtab;
       
 57491   u.cj.pModule = u.cj.pVtab->pModule;
       
 57492   assert( u.cj.pModule->xNext );
       
 57493 
       
 57494   /* Invoke the xNext() method of the module. There is no way for the
       
 57495   ** underlying implementation to return an error if one occurs during
       
 57496   ** xNext(). Instead, if an error occurs, true is returned (indicating that
       
 57497   ** data is available) and the error code returned when xColumn or
       
 57498   ** some other method is next invoked on the save virtual table cursor.
       
 57499   */
       
 57500   if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
       
 57501   p->inVtabMethod = 1;
       
 57502   rc = u.cj.pModule->xNext(u.cj.pCur->pVtabCursor);
       
 57503   p->inVtabMethod = 0;
       
 57504   sqlite3DbFree(db, p->zErrMsg);
       
 57505   p->zErrMsg = u.cj.pVtab->zErrMsg;
       
 57506   u.cj.pVtab->zErrMsg = 0;
       
 57507   if( rc==SQLITE_OK ){
       
 57508     u.cj.res = u.cj.pModule->xEof(u.cj.pCur->pVtabCursor);
       
 57509   }
       
 57510   if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
       
 57511 
       
 57512   if( !u.cj.res ){
       
 57513     /* If there is data, jump to P2 */
       
 57514     pc = pOp->p2 - 1;
       
 57515   }
       
 57516   break;
       
 57517 }
       
 57518 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 57519 
       
 57520 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 57521 /* Opcode: VRename P1 * * P4 *
       
 57522 **
       
 57523 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
       
 57524 ** This opcode invokes the corresponding xRename method. The value
       
 57525 ** in register P1 is passed as the zName argument to the xRename method.
       
 57526 */
       
 57527 case OP_VRename: {
       
 57528 #if 0  /* local variables moved into u.ck */
       
 57529   sqlite3_vtab *pVtab;
       
 57530   Mem *pName;
       
 57531 #endif /* local variables moved into u.ck */
       
 57532 
       
 57533   u.ck.pVtab = pOp->p4.pVtab->pVtab;
       
 57534   u.ck.pName = &p->aMem[pOp->p1];
       
 57535   assert( u.ck.pVtab->pModule->xRename );
       
 57536   REGISTER_TRACE(pOp->p1, u.ck.pName);
       
 57537   assert( u.ck.pName->flags & MEM_Str );
       
 57538   if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
       
 57539   rc = u.ck.pVtab->pModule->xRename(u.ck.pVtab, u.ck.pName->z);
       
 57540   sqlite3DbFree(db, p->zErrMsg);
       
 57541   p->zErrMsg = u.ck.pVtab->zErrMsg;
       
 57542   u.ck.pVtab->zErrMsg = 0;
       
 57543   if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
       
 57544 
       
 57545   break;
       
 57546 }
       
 57547 #endif
       
 57548 
       
 57549 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 57550 /* Opcode: VUpdate P1 P2 P3 P4 *
       
 57551 **
       
 57552 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
       
 57553 ** This opcode invokes the corresponding xUpdate method. P2 values
       
 57554 ** are contiguous memory cells starting at P3 to pass to the xUpdate 
       
 57555 ** invocation. The value in register (P3+P2-1) corresponds to the 
       
 57556 ** p2th element of the argv array passed to xUpdate.
       
 57557 **
       
 57558 ** The xUpdate method will do a DELETE or an INSERT or both.
       
 57559 ** The argv[0] element (which corresponds to memory cell P3)
       
 57560 ** is the rowid of a row to delete.  If argv[0] is NULL then no 
       
 57561 ** deletion occurs.  The argv[1] element is the rowid of the new 
       
 57562 ** row.  This can be NULL to have the virtual table select the new 
       
 57563 ** rowid for itself.  The subsequent elements in the array are 
       
 57564 ** the values of columns in the new row.
       
 57565 **
       
 57566 ** If P2==1 then no insert is performed.  argv[0] is the rowid of
       
 57567 ** a row to delete.
       
 57568 **
       
 57569 ** P1 is a boolean flag. If it is set to true and the xUpdate call
       
 57570 ** is successful, then the value returned by sqlite3_last_insert_rowid() 
       
 57571 ** is set to the value of the rowid for the row just inserted.
       
 57572 */
       
 57573 case OP_VUpdate: {
       
 57574 #if 0  /* local variables moved into u.cl */
       
 57575   sqlite3_vtab *pVtab;
       
 57576   sqlite3_module *pModule;
       
 57577   int nArg;
       
 57578   int i;
       
 57579   sqlite_int64 rowid;
       
 57580   Mem **apArg;
       
 57581   Mem *pX;
       
 57582 #endif /* local variables moved into u.cl */
       
 57583 
       
 57584   u.cl.pVtab = pOp->p4.pVtab->pVtab;
       
 57585   u.cl.pModule = (sqlite3_module *)u.cl.pVtab->pModule;
       
 57586   u.cl.nArg = pOp->p2;
       
 57587   assert( pOp->p4type==P4_VTAB );
       
 57588   if( ALWAYS(u.cl.pModule->xUpdate) ){
       
 57589     u.cl.apArg = p->apArg;
       
 57590     u.cl.pX = &p->aMem[pOp->p3];
       
 57591     for(u.cl.i=0; u.cl.i<u.cl.nArg; u.cl.i++){
       
 57592       storeTypeInfo(u.cl.pX, 0);
       
 57593       u.cl.apArg[u.cl.i] = u.cl.pX;
       
 57594       u.cl.pX++;
       
 57595     }
       
 57596     if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
       
 57597     rc = u.cl.pModule->xUpdate(u.cl.pVtab, u.cl.nArg, u.cl.apArg, &u.cl.rowid);
       
 57598     sqlite3DbFree(db, p->zErrMsg);
       
 57599     p->zErrMsg = u.cl.pVtab->zErrMsg;
       
 57600     u.cl.pVtab->zErrMsg = 0;
       
 57601     if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
       
 57602     if( rc==SQLITE_OK && pOp->p1 ){
       
 57603       assert( u.cl.nArg>1 && u.cl.apArg[0] && (u.cl.apArg[0]->flags&MEM_Null) );
       
 57604       db->lastRowid = u.cl.rowid;
       
 57605     }
       
 57606     p->nChange++;
       
 57607   }
       
 57608   break;
       
 57609 }
       
 57610 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 57611 
       
 57612 #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
       
 57613 /* Opcode: Pagecount P1 P2 * * *
       
 57614 **
       
 57615 ** Write the current number of pages in database P1 to memory cell P2.
       
 57616 */
       
 57617 case OP_Pagecount: {            /* out2-prerelease */
       
 57618 #if 0  /* local variables moved into u.cm */
       
 57619   int p1;
       
 57620   int nPage;
       
 57621   Pager *pPager;
       
 57622 #endif /* local variables moved into u.cm */
       
 57623 
       
 57624   u.cm.p1 = pOp->p1;
       
 57625   u.cm.pPager = sqlite3BtreePager(db->aDb[u.cm.p1].pBt);
       
 57626   rc = sqlite3PagerPagecount(u.cm.pPager, &u.cm.nPage);
       
 57627   /* OP_Pagecount is always called from within a read transaction.  The
       
 57628   ** page count has already been successfully read and cached.  So the
       
 57629   ** sqlite3PagerPagecount() call above cannot fail. */
       
 57630   if( ALWAYS(rc==SQLITE_OK) ){
       
 57631     pOut->flags = MEM_Int;
       
 57632     pOut->u.i = u.cm.nPage;
       
 57633   }
       
 57634   break;
       
 57635 }
       
 57636 #endif
       
 57637 
       
 57638 #ifndef SQLITE_OMIT_TRACE
       
 57639 /* Opcode: Trace * * * P4 *
       
 57640 **
       
 57641 ** If tracing is enabled (by the sqlite3_trace()) interface, then
       
 57642 ** the UTF-8 string contained in P4 is emitted on the trace callback.
       
 57643 */
       
 57644 case OP_Trace: {
       
 57645 #if 0  /* local variables moved into u.cn */
       
 57646   char *zTrace;
       
 57647 #endif /* local variables moved into u.cn */
       
 57648 
       
 57649   u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
       
 57650   if( u.cn.zTrace ){
       
 57651     if( db->xTrace ){
       
 57652       db->xTrace(db->pTraceArg, u.cn.zTrace);
       
 57653     }
       
 57654 #ifdef SQLITE_DEBUG
       
 57655     if( (db->flags & SQLITE_SqlTrace)!=0 ){
       
 57656       sqlite3DebugPrintf("SQL-trace: %s\n", u.cn.zTrace);
       
 57657     }
       
 57658 #endif /* SQLITE_DEBUG */
       
 57659   }
       
 57660   break;
       
 57661 }
       
 57662 #endif
       
 57663 
       
 57664 
       
 57665 /* Opcode: Noop * * * * *
       
 57666 **
       
 57667 ** Do nothing.  This instruction is often useful as a jump
       
 57668 ** destination.
       
 57669 */
       
 57670 /*
       
 57671 ** The magic Explain opcode are only inserted when explain==2 (which
       
 57672 ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
       
 57673 ** This opcode records information from the optimizer.  It is the
       
 57674 ** the same as a no-op.  This opcodesnever appears in a real VM program.
       
 57675 */
       
 57676 default: {          /* This is really OP_Noop and OP_Explain */
       
 57677   break;
       
 57678 }
       
 57679 
       
 57680 /*****************************************************************************
       
 57681 ** The cases of the switch statement above this line should all be indented
       
 57682 ** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
       
 57683 ** readability.  From this point on down, the normal indentation rules are
       
 57684 ** restored.
       
 57685 *****************************************************************************/
       
 57686     }
       
 57687 
       
 57688 #ifdef VDBE_PROFILE
       
 57689     {
       
 57690       u64 elapsed = sqlite3Hwtime() - start;
       
 57691       pOp->cycles += elapsed;
       
 57692       pOp->cnt++;
       
 57693 #if 0
       
 57694         fprintf(stdout, "%10llu ", elapsed);
       
 57695         sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]);
       
 57696 #endif
       
 57697     }
       
 57698 #endif
       
 57699 
       
 57700     /* The following code adds nothing to the actual functionality
       
 57701     ** of the program.  It is only here for testing and debugging.
       
 57702     ** On the other hand, it does burn CPU cycles every time through
       
 57703     ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
       
 57704     */
       
 57705 #ifndef NDEBUG
       
 57706     assert( pc>=-1 && pc<p->nOp );
       
 57707 
       
 57708 #ifdef SQLITE_DEBUG
       
 57709     if( p->trace ){
       
 57710       if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
       
 57711       if( opProperty & OPFLG_OUT2_PRERELEASE ){
       
 57712         registerTrace(p->trace, pOp->p2, pOut);
       
 57713       }
       
 57714       if( opProperty & OPFLG_OUT3 ){
       
 57715         registerTrace(p->trace, pOp->p3, pOut);
       
 57716       }
       
 57717     }
       
 57718 #endif  /* SQLITE_DEBUG */
       
 57719 #endif  /* NDEBUG */
       
 57720   }  /* The end of the for(;;) loop the loops through opcodes */
       
 57721 
       
 57722   /* If we reach this point, it means that execution is finished with
       
 57723   ** an error of some kind.
       
 57724   */
       
 57725 vdbe_error_halt:
       
 57726   assert( rc );
       
 57727   p->rc = rc;
       
 57728   sqlite3VdbeHalt(p);
       
 57729   if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
       
 57730   rc = SQLITE_ERROR;
       
 57731 
       
 57732   /* This is the only way out of this procedure.  We have to
       
 57733   ** release the mutexes on btrees that were acquired at the
       
 57734   ** top. */
       
 57735 vdbe_return:
       
 57736   sqlite3BtreeMutexArrayLeave(&p->aMutex);
       
 57737   return rc;
       
 57738 
       
 57739   /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
       
 57740   ** is encountered.
       
 57741   */
       
 57742 too_big:
       
 57743   sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
       
 57744   rc = SQLITE_TOOBIG;
       
 57745   goto vdbe_error_halt;
       
 57746 
       
 57747   /* Jump to here if a malloc() fails.
       
 57748   */
       
 57749 no_mem:
       
 57750   db->mallocFailed = 1;
       
 57751   sqlite3SetString(&p->zErrMsg, db, "out of memory");
       
 57752   rc = SQLITE_NOMEM;
       
 57753   goto vdbe_error_halt;
       
 57754 
       
 57755   /* Jump to here for an SQLITE_MISUSE error.
       
 57756   */
       
 57757 abort_due_to_misuse:
       
 57758   rc = SQLITE_MISUSE;
       
 57759   /* Fall thru into abort_due_to_error */
       
 57760 
       
 57761   /* Jump to here for any other kind of fatal error.  The "rc" variable
       
 57762   ** should hold the error number.
       
 57763   */
       
 57764 abort_due_to_error:
       
 57765   assert( p->zErrMsg==0 );
       
 57766   if( db->mallocFailed ) rc = SQLITE_NOMEM;
       
 57767   if( rc!=SQLITE_IOERR_NOMEM ){
       
 57768     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
       
 57769   }
       
 57770   goto vdbe_error_halt;
       
 57771 
       
 57772   /* Jump to here if the sqlite3_interrupt() API sets the interrupt
       
 57773   ** flag.
       
 57774   */
       
 57775 abort_due_to_interrupt:
       
 57776   assert( db->u1.isInterrupted );
       
 57777   rc = SQLITE_INTERRUPT;
       
 57778   p->rc = rc;
       
 57779   sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
       
 57780   goto vdbe_error_halt;
       
 57781 }
       
 57782 
       
 57783 /************** End of vdbe.c ************************************************/
       
 57784 /************** Begin file vdbeblob.c ****************************************/
       
 57785 /*
       
 57786 ** 2007 May 1
       
 57787 **
       
 57788 ** The author disclaims copyright to this source code.  In place of
       
 57789 ** a legal notice, here is a blessing:
       
 57790 **
       
 57791 **    May you do good and not evil.
       
 57792 **    May you find forgiveness for yourself and forgive others.
       
 57793 **    May you share freely, never taking more than you give.
       
 57794 **
       
 57795 *************************************************************************
       
 57796 **
       
 57797 ** This file contains code used to implement incremental BLOB I/O.
       
 57798 **
       
 57799 ** $Id: vdbeblob.c,v 1.35 2009/07/02 07:47:33 danielk1977 Exp $
       
 57800 */
       
 57801 
       
 57802 
       
 57803 #ifndef SQLITE_OMIT_INCRBLOB
       
 57804 
       
 57805 /*
       
 57806 ** Valid sqlite3_blob* handles point to Incrblob structures.
       
 57807 */
       
 57808 typedef struct Incrblob Incrblob;
       
 57809 struct Incrblob {
       
 57810   int flags;              /* Copy of "flags" passed to sqlite3_blob_open() */
       
 57811   int nByte;              /* Size of open blob, in bytes */
       
 57812   int iOffset;            /* Byte offset of blob in cursor data */
       
 57813   BtCursor *pCsr;         /* Cursor pointing at blob row */
       
 57814   sqlite3_stmt *pStmt;    /* Statement holding cursor open */
       
 57815   sqlite3 *db;            /* The associated database */
       
 57816 };
       
 57817 
       
 57818 /*
       
 57819 ** Open a blob handle.
       
 57820 */
       
 57821 SQLITE_API int sqlite3_blob_open(
       
 57822   sqlite3* db,            /* The database connection */
       
 57823   const char *zDb,        /* The attached database containing the blob */
       
 57824   const char *zTable,     /* The table containing the blob */
       
 57825   const char *zColumn,    /* The column containing the blob */
       
 57826   sqlite_int64 iRow,      /* The row containing the glob */
       
 57827   int flags,              /* True -> read/write access, false -> read-only */
       
 57828   sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
       
 57829 ){
       
 57830   int nAttempt = 0;
       
 57831   int iCol;               /* Index of zColumn in row-record */
       
 57832 
       
 57833   /* This VDBE program seeks a btree cursor to the identified 
       
 57834   ** db/table/row entry. The reason for using a vdbe program instead
       
 57835   ** of writing code to use the b-tree layer directly is that the
       
 57836   ** vdbe program will take advantage of the various transaction,
       
 57837   ** locking and error handling infrastructure built into the vdbe.
       
 57838   **
       
 57839   ** After seeking the cursor, the vdbe executes an OP_ResultRow.
       
 57840   ** Code external to the Vdbe then "borrows" the b-tree cursor and
       
 57841   ** uses it to implement the blob_read(), blob_write() and 
       
 57842   ** blob_bytes() functions.
       
 57843   **
       
 57844   ** The sqlite3_blob_close() function finalizes the vdbe program,
       
 57845   ** which closes the b-tree cursor and (possibly) commits the 
       
 57846   ** transaction.
       
 57847   */
       
 57848   static const VdbeOpList openBlob[] = {
       
 57849     {OP_Transaction, 0, 0, 0},     /* 0: Start a transaction */
       
 57850     {OP_VerifyCookie, 0, 0, 0},    /* 1: Check the schema cookie */
       
 57851     {OP_TableLock, 0, 0, 0},       /* 2: Acquire a read or write lock */
       
 57852 
       
 57853     /* One of the following two instructions is replaced by an OP_Noop. */
       
 57854     {OP_OpenRead, 0, 0, 0},        /* 3: Open cursor 0 for reading */
       
 57855     {OP_OpenWrite, 0, 0, 0},       /* 4: Open cursor 0 for read/write */
       
 57856 
       
 57857     {OP_Variable, 1, 1, 1},        /* 5: Push the rowid to the stack */
       
 57858     {OP_NotExists, 0, 9, 1},       /* 6: Seek the cursor */
       
 57859     {OP_Column, 0, 0, 1},          /* 7  */
       
 57860     {OP_ResultRow, 1, 0, 0},       /* 8  */
       
 57861     {OP_Close, 0, 0, 0},           /* 9  */
       
 57862     {OP_Halt, 0, 0, 0},            /* 10 */
       
 57863   };
       
 57864 
       
 57865   Vdbe *v = 0;
       
 57866   int rc = SQLITE_OK;
       
 57867   char *zErr = 0;
       
 57868   Table *pTab;
       
 57869   Parse *pParse;
       
 57870 
       
 57871   *ppBlob = 0;
       
 57872   sqlite3_mutex_enter(db->mutex);
       
 57873   pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
       
 57874   if( pParse==0 ){
       
 57875     rc = SQLITE_NOMEM;
       
 57876     goto blob_open_out;
       
 57877   }
       
 57878   do {
       
 57879     memset(pParse, 0, sizeof(Parse));
       
 57880     pParse->db = db;
       
 57881 
       
 57882     if( sqlite3SafetyOn(db) ){
       
 57883       sqlite3DbFree(db, zErr);
       
 57884       sqlite3StackFree(db, pParse);
       
 57885       sqlite3_mutex_leave(db->mutex);
       
 57886       return SQLITE_MISUSE;
       
 57887     }
       
 57888 
       
 57889     sqlite3BtreeEnterAll(db);
       
 57890     pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
       
 57891     if( pTab && IsVirtual(pTab) ){
       
 57892       pTab = 0;
       
 57893       sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
       
 57894     }
       
 57895 #ifndef SQLITE_OMIT_VIEW
       
 57896     if( pTab && pTab->pSelect ){
       
 57897       pTab = 0;
       
 57898       sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
       
 57899     }
       
 57900 #endif
       
 57901     if( !pTab ){
       
 57902       if( pParse->zErrMsg ){
       
 57903         sqlite3DbFree(db, zErr);
       
 57904         zErr = pParse->zErrMsg;
       
 57905         pParse->zErrMsg = 0;
       
 57906       }
       
 57907       rc = SQLITE_ERROR;
       
 57908       (void)sqlite3SafetyOff(db);
       
 57909       sqlite3BtreeLeaveAll(db);
       
 57910       goto blob_open_out;
       
 57911     }
       
 57912 
       
 57913     /* Now search pTab for the exact column. */
       
 57914     for(iCol=0; iCol < pTab->nCol; iCol++) {
       
 57915       if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
       
 57916         break;
       
 57917       }
       
 57918     }
       
 57919     if( iCol==pTab->nCol ){
       
 57920       sqlite3DbFree(db, zErr);
       
 57921       zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
       
 57922       rc = SQLITE_ERROR;
       
 57923       (void)sqlite3SafetyOff(db);
       
 57924       sqlite3BtreeLeaveAll(db);
       
 57925       goto blob_open_out;
       
 57926     }
       
 57927 
       
 57928     /* If the value is being opened for writing, check that the
       
 57929     ** column is not indexed, and that it is not part of a foreign key. 
       
 57930     ** It is against the rules to open a column to which either of these
       
 57931     ** descriptions applies for writing.  */
       
 57932     if( flags ){
       
 57933       const char *zFault = 0;
       
 57934       Index *pIdx;
       
 57935 #ifndef SQLITE_OMIT_FOREIGN_KEY
       
 57936       if( db->flags&SQLITE_ForeignKeys ){
       
 57937         /* Check that the column is not part of an FK child key definition. It
       
 57938         ** is not necessary to check if it is part of a parent key, as parent
       
 57939         ** key columns must be indexed. The check below will pick up this 
       
 57940         ** case.  */
       
 57941         FKey *pFKey;
       
 57942         for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
       
 57943           int j;
       
 57944           for(j=0; j<pFKey->nCol; j++){
       
 57945             if( pFKey->aCol[j].iFrom==iCol ){
       
 57946               zFault = "foreign key";
       
 57947             }
       
 57948           }
       
 57949         }
       
 57950       }
       
 57951 #endif
       
 57952       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       
 57953         int j;
       
 57954         for(j=0; j<pIdx->nColumn; j++){
       
 57955           if( pIdx->aiColumn[j]==iCol ){
       
 57956             zFault = "indexed";
       
 57957           }
       
 57958         }
       
 57959       }
       
 57960       if( zFault ){
       
 57961         sqlite3DbFree(db, zErr);
       
 57962         zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
       
 57963         rc = SQLITE_ERROR;
       
 57964         (void)sqlite3SafetyOff(db);
       
 57965         sqlite3BtreeLeaveAll(db);
       
 57966         goto blob_open_out;
       
 57967       }
       
 57968     }
       
 57969 
       
 57970     v = sqlite3VdbeCreate(db);
       
 57971     if( v ){
       
 57972       int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       
 57973       sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
       
 57974       flags = !!flags;                 /* flags = (flags ? 1 : 0); */
       
 57975 
       
 57976       /* Configure the OP_Transaction */
       
 57977       sqlite3VdbeChangeP1(v, 0, iDb);
       
 57978       sqlite3VdbeChangeP2(v, 0, flags);
       
 57979 
       
 57980       /* Configure the OP_VerifyCookie */
       
 57981       sqlite3VdbeChangeP1(v, 1, iDb);
       
 57982       sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
       
 57983 
       
 57984       /* Make sure a mutex is held on the table to be accessed */
       
 57985       sqlite3VdbeUsesBtree(v, iDb); 
       
 57986 
       
 57987       /* Configure the OP_TableLock instruction */
       
 57988       sqlite3VdbeChangeP1(v, 2, iDb);
       
 57989       sqlite3VdbeChangeP2(v, 2, pTab->tnum);
       
 57990       sqlite3VdbeChangeP3(v, 2, flags);
       
 57991       sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
       
 57992 
       
 57993       /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
       
 57994       ** parameter of the other to pTab->tnum.  */
       
 57995       sqlite3VdbeChangeToNoop(v, 4 - flags, 1);
       
 57996       sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
       
 57997       sqlite3VdbeChangeP3(v, 3 + flags, iDb);
       
 57998 
       
 57999       /* Configure the number of columns. Configure the cursor to
       
 58000       ** think that the table has one more column than it really
       
 58001       ** does. An OP_Column to retrieve this imaginary column will
       
 58002       ** always return an SQL NULL. This is useful because it means
       
 58003       ** we can invoke OP_Column to fill in the vdbe cursors type 
       
 58004       ** and offset cache without causing any IO.
       
 58005       */
       
 58006       sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
       
 58007       sqlite3VdbeChangeP2(v, 7, pTab->nCol);
       
 58008       if( !db->mallocFailed ){
       
 58009         sqlite3VdbeMakeReady(v, 1, 1, 1, 0, 0, 0);
       
 58010       }
       
 58011     }
       
 58012    
       
 58013     sqlite3BtreeLeaveAll(db);
       
 58014     rc = sqlite3SafetyOff(db);
       
 58015     if( NEVER(rc!=SQLITE_OK) || db->mallocFailed ){
       
 58016       goto blob_open_out;
       
 58017     }
       
 58018 
       
 58019     sqlite3_bind_int64((sqlite3_stmt *)v, 1, iRow);
       
 58020     rc = sqlite3_step((sqlite3_stmt *)v);
       
 58021     if( rc!=SQLITE_ROW ){
       
 58022       nAttempt++;
       
 58023       rc = sqlite3_finalize((sqlite3_stmt *)v);
       
 58024       sqlite3DbFree(db, zErr);
       
 58025       zErr = sqlite3MPrintf(db, sqlite3_errmsg(db));
       
 58026       v = 0;
       
 58027     }
       
 58028   } while( nAttempt<5 && rc==SQLITE_SCHEMA );
       
 58029 
       
 58030   if( rc==SQLITE_ROW ){
       
 58031     /* The row-record has been opened successfully. Check that the
       
 58032     ** column in question contains text or a blob. If it contains
       
 58033     ** text, it is up to the caller to get the encoding right.
       
 58034     */
       
 58035     Incrblob *pBlob;
       
 58036     u32 type = v->apCsr[0]->aType[iCol];
       
 58037 
       
 58038     if( type<12 ){
       
 58039       sqlite3DbFree(db, zErr);
       
 58040       zErr = sqlite3MPrintf(db, "cannot open value of type %s",
       
 58041           type==0?"null": type==7?"real": "integer"
       
 58042       );
       
 58043       rc = SQLITE_ERROR;
       
 58044       goto blob_open_out;
       
 58045     }
       
 58046     pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
       
 58047     if( db->mallocFailed ){
       
 58048       sqlite3DbFree(db, pBlob);
       
 58049       goto blob_open_out;
       
 58050     }
       
 58051     pBlob->flags = flags;
       
 58052     pBlob->pCsr =  v->apCsr[0]->pCursor;
       
 58053     sqlite3BtreeEnterCursor(pBlob->pCsr);
       
 58054     sqlite3BtreeCacheOverflow(pBlob->pCsr);
       
 58055     sqlite3BtreeLeaveCursor(pBlob->pCsr);
       
 58056     pBlob->pStmt = (sqlite3_stmt *)v;
       
 58057     pBlob->iOffset = v->apCsr[0]->aOffset[iCol];
       
 58058     pBlob->nByte = sqlite3VdbeSerialTypeLen(type);
       
 58059     pBlob->db = db;
       
 58060     *ppBlob = (sqlite3_blob *)pBlob;
       
 58061     rc = SQLITE_OK;
       
 58062   }else if( rc==SQLITE_OK ){
       
 58063     sqlite3DbFree(db, zErr);
       
 58064     zErr = sqlite3MPrintf(db, "no such rowid: %lld", iRow);
       
 58065     rc = SQLITE_ERROR;
       
 58066   }
       
 58067 
       
 58068 blob_open_out:
       
 58069   if( v && (rc!=SQLITE_OK || db->mallocFailed) ){
       
 58070     sqlite3VdbeFinalize(v);
       
 58071   }
       
 58072   sqlite3Error(db, rc, zErr);
       
 58073   sqlite3DbFree(db, zErr);
       
 58074   sqlite3StackFree(db, pParse);
       
 58075   rc = sqlite3ApiExit(db, rc);
       
 58076   sqlite3_mutex_leave(db->mutex);
       
 58077   return rc;
       
 58078 }
       
 58079 
       
 58080 /*
       
 58081 ** Close a blob handle that was previously created using
       
 58082 ** sqlite3_blob_open().
       
 58083 */
       
 58084 SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
       
 58085   Incrblob *p = (Incrblob *)pBlob;
       
 58086   int rc;
       
 58087   sqlite3 *db;
       
 58088 
       
 58089   if( p ){
       
 58090     db = p->db;
       
 58091     sqlite3_mutex_enter(db->mutex);
       
 58092     rc = sqlite3_finalize(p->pStmt);
       
 58093     sqlite3DbFree(db, p);
       
 58094     sqlite3_mutex_leave(db->mutex);
       
 58095   }else{
       
 58096     rc = SQLITE_OK;
       
 58097   }
       
 58098   return rc;
       
 58099 }
       
 58100 
       
 58101 /*
       
 58102 ** Perform a read or write operation on a blob
       
 58103 */
       
 58104 static int blobReadWrite(
       
 58105   sqlite3_blob *pBlob, 
       
 58106   void *z, 
       
 58107   int n, 
       
 58108   int iOffset, 
       
 58109   int (*xCall)(BtCursor*, u32, u32, void*)
       
 58110 ){
       
 58111   int rc;
       
 58112   Incrblob *p = (Incrblob *)pBlob;
       
 58113   Vdbe *v;
       
 58114   sqlite3 *db;
       
 58115 
       
 58116   if( p==0 ) return SQLITE_MISUSE;
       
 58117   db = p->db;
       
 58118   sqlite3_mutex_enter(db->mutex);
       
 58119   v = (Vdbe*)p->pStmt;
       
 58120 
       
 58121   if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
       
 58122     /* Request is out of range. Return a transient error. */
       
 58123     rc = SQLITE_ERROR;
       
 58124     sqlite3Error(db, SQLITE_ERROR, 0);
       
 58125   } else if( v==0 ){
       
 58126     /* If there is no statement handle, then the blob-handle has
       
 58127     ** already been invalidated. Return SQLITE_ABORT in this case.
       
 58128     */
       
 58129     rc = SQLITE_ABORT;
       
 58130   }else{
       
 58131     /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
       
 58132     ** returned, clean-up the statement handle.
       
 58133     */
       
 58134     assert( db == v->db );
       
 58135     sqlite3BtreeEnterCursor(p->pCsr);
       
 58136     rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
       
 58137     sqlite3BtreeLeaveCursor(p->pCsr);
       
 58138     if( rc==SQLITE_ABORT ){
       
 58139       sqlite3VdbeFinalize(v);
       
 58140       p->pStmt = 0;
       
 58141     }else{
       
 58142       db->errCode = rc;
       
 58143       v->rc = rc;
       
 58144     }
       
 58145   }
       
 58146   rc = sqlite3ApiExit(db, rc);
       
 58147   sqlite3_mutex_leave(db->mutex);
       
 58148   return rc;
       
 58149 }
       
 58150 
       
 58151 /*
       
 58152 ** Read data from a blob handle.
       
 58153 */
       
 58154 SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
       
 58155   return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
       
 58156 }
       
 58157 
       
 58158 /*
       
 58159 ** Write data to a blob handle.
       
 58160 */
       
 58161 SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
       
 58162   return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
       
 58163 }
       
 58164 
       
 58165 /*
       
 58166 ** Query a blob handle for the size of the data.
       
 58167 **
       
 58168 ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
       
 58169 ** so no mutex is required for access.
       
 58170 */
       
 58171 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
       
 58172   Incrblob *p = (Incrblob *)pBlob;
       
 58173   return p ? p->nByte : 0;
       
 58174 }
       
 58175 
       
 58176 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
       
 58177 
       
 58178 /************** End of vdbeblob.c ********************************************/
       
 58179 /************** Begin file journal.c *****************************************/
       
 58180 /*
       
 58181 ** 2007 August 22
       
 58182 **
       
 58183 ** The author disclaims copyright to this source code.  In place of
       
 58184 ** a legal notice, here is a blessing:
       
 58185 **
       
 58186 **    May you do good and not evil.
       
 58187 **    May you find forgiveness for yourself and forgive others.
       
 58188 **    May you share freely, never taking more than you give.
       
 58189 **
       
 58190 *************************************************************************
       
 58191 **
       
 58192 ** @(#) $Id: journal.c,v 1.9 2009/01/20 17:06:27 danielk1977 Exp $
       
 58193 */
       
 58194 
       
 58195 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
       
 58196 
       
 58197 /*
       
 58198 ** This file implements a special kind of sqlite3_file object used
       
 58199 ** by SQLite to create journal files if the atomic-write optimization
       
 58200 ** is enabled.
       
 58201 **
       
 58202 ** The distinctive characteristic of this sqlite3_file is that the
       
 58203 ** actual on disk file is created lazily. When the file is created,
       
 58204 ** the caller specifies a buffer size for an in-memory buffer to
       
 58205 ** be used to service read() and write() requests. The actual file
       
 58206 ** on disk is not created or populated until either:
       
 58207 **
       
 58208 **   1) The in-memory representation grows too large for the allocated 
       
 58209 **      buffer, or
       
 58210 **   2) The sqlite3JournalCreate() function is called.
       
 58211 */
       
 58212 
       
 58213 
       
 58214 
       
 58215 /*
       
 58216 ** A JournalFile object is a subclass of sqlite3_file used by
       
 58217 ** as an open file handle for journal files.
       
 58218 */
       
 58219 struct JournalFile {
       
 58220   sqlite3_io_methods *pMethod;    /* I/O methods on journal files */
       
 58221   int nBuf;                       /* Size of zBuf[] in bytes */
       
 58222   char *zBuf;                     /* Space to buffer journal writes */
       
 58223   int iSize;                      /* Amount of zBuf[] currently used */
       
 58224   int flags;                      /* xOpen flags */
       
 58225   sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
       
 58226   sqlite3_file *pReal;            /* The "real" underlying file descriptor */
       
 58227   const char *zJournal;           /* Name of the journal file */
       
 58228 };
       
 58229 typedef struct JournalFile JournalFile;
       
 58230 
       
 58231 /*
       
 58232 ** If it does not already exists, create and populate the on-disk file 
       
 58233 ** for JournalFile p.
       
 58234 */
       
 58235 static int createFile(JournalFile *p){
       
 58236   int rc = SQLITE_OK;
       
 58237   if( !p->pReal ){
       
 58238     sqlite3_file *pReal = (sqlite3_file *)&p[1];
       
 58239     rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
       
 58240     if( rc==SQLITE_OK ){
       
 58241       p->pReal = pReal;
       
 58242       if( p->iSize>0 ){
       
 58243         assert(p->iSize<=p->nBuf);
       
 58244         rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
       
 58245       }
       
 58246     }
       
 58247   }
       
 58248   return rc;
       
 58249 }
       
 58250 
       
 58251 /*
       
 58252 ** Close the file.
       
 58253 */
       
 58254 static int jrnlClose(sqlite3_file *pJfd){
       
 58255   JournalFile *p = (JournalFile *)pJfd;
       
 58256   if( p->pReal ){
       
 58257     sqlite3OsClose(p->pReal);
       
 58258   }
       
 58259   sqlite3_free(p->zBuf);
       
 58260   return SQLITE_OK;
       
 58261 }
       
 58262 
       
 58263 /*
       
 58264 ** Read data from the file.
       
 58265 */
       
 58266 static int jrnlRead(
       
 58267   sqlite3_file *pJfd,    /* The journal file from which to read */
       
 58268   void *zBuf,            /* Put the results here */
       
 58269   int iAmt,              /* Number of bytes to read */
       
 58270   sqlite_int64 iOfst     /* Begin reading at this offset */
       
 58271 ){
       
 58272   int rc = SQLITE_OK;
       
 58273   JournalFile *p = (JournalFile *)pJfd;
       
 58274   if( p->pReal ){
       
 58275     rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
       
 58276   }else if( (iAmt+iOfst)>p->iSize ){
       
 58277     rc = SQLITE_IOERR_SHORT_READ;
       
 58278   }else{
       
 58279     memcpy(zBuf, &p->zBuf[iOfst], iAmt);
       
 58280   }
       
 58281   return rc;
       
 58282 }
       
 58283 
       
 58284 /*
       
 58285 ** Write data to the file.
       
 58286 */
       
 58287 static int jrnlWrite(
       
 58288   sqlite3_file *pJfd,    /* The journal file into which to write */
       
 58289   const void *zBuf,      /* Take data to be written from here */
       
 58290   int iAmt,              /* Number of bytes to write */
       
 58291   sqlite_int64 iOfst     /* Begin writing at this offset into the file */
       
 58292 ){
       
 58293   int rc = SQLITE_OK;
       
 58294   JournalFile *p = (JournalFile *)pJfd;
       
 58295   if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
       
 58296     rc = createFile(p);
       
 58297   }
       
 58298   if( rc==SQLITE_OK ){
       
 58299     if( p->pReal ){
       
 58300       rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
       
 58301     }else{
       
 58302       memcpy(&p->zBuf[iOfst], zBuf, iAmt);
       
 58303       if( p->iSize<(iOfst+iAmt) ){
       
 58304         p->iSize = (iOfst+iAmt);
       
 58305       }
       
 58306     }
       
 58307   }
       
 58308   return rc;
       
 58309 }
       
 58310 
       
 58311 /*
       
 58312 ** Truncate the file.
       
 58313 */
       
 58314 static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
       
 58315   int rc = SQLITE_OK;
       
 58316   JournalFile *p = (JournalFile *)pJfd;
       
 58317   if( p->pReal ){
       
 58318     rc = sqlite3OsTruncate(p->pReal, size);
       
 58319   }else if( size<p->iSize ){
       
 58320     p->iSize = size;
       
 58321   }
       
 58322   return rc;
       
 58323 }
       
 58324 
       
 58325 /*
       
 58326 ** Sync the file.
       
 58327 */
       
 58328 static int jrnlSync(sqlite3_file *pJfd, int flags){
       
 58329   int rc;
       
 58330   JournalFile *p = (JournalFile *)pJfd;
       
 58331   if( p->pReal ){
       
 58332     rc = sqlite3OsSync(p->pReal, flags);
       
 58333   }else{
       
 58334     rc = SQLITE_OK;
       
 58335   }
       
 58336   return rc;
       
 58337 }
       
 58338 
       
 58339 /*
       
 58340 ** Query the size of the file in bytes.
       
 58341 */
       
 58342 static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
       
 58343   int rc = SQLITE_OK;
       
 58344   JournalFile *p = (JournalFile *)pJfd;
       
 58345   if( p->pReal ){
       
 58346     rc = sqlite3OsFileSize(p->pReal, pSize);
       
 58347   }else{
       
 58348     *pSize = (sqlite_int64) p->iSize;
       
 58349   }
       
 58350   return rc;
       
 58351 }
       
 58352 
       
 58353 /*
       
 58354 ** Table of methods for JournalFile sqlite3_file object.
       
 58355 */
       
 58356 static struct sqlite3_io_methods JournalFileMethods = {
       
 58357   1,             /* iVersion */
       
 58358   jrnlClose,     /* xClose */
       
 58359   jrnlRead,      /* xRead */
       
 58360   jrnlWrite,     /* xWrite */
       
 58361   jrnlTruncate,  /* xTruncate */
       
 58362   jrnlSync,      /* xSync */
       
 58363   jrnlFileSize,  /* xFileSize */
       
 58364   0,             /* xLock */
       
 58365   0,             /* xUnlock */
       
 58366   0,             /* xCheckReservedLock */
       
 58367   0,             /* xFileControl */
       
 58368   0,             /* xSectorSize */
       
 58369   0              /* xDeviceCharacteristics */
       
 58370 };
       
 58371 
       
 58372 /* 
       
 58373 ** Open a journal file.
       
 58374 */
       
 58375 SQLITE_PRIVATE int sqlite3JournalOpen(
       
 58376   sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
       
 58377   const char *zName,         /* Name of the journal file */
       
 58378   sqlite3_file *pJfd,        /* Preallocated, blank file handle */
       
 58379   int flags,                 /* Opening flags */
       
 58380   int nBuf                   /* Bytes buffered before opening the file */
       
 58381 ){
       
 58382   JournalFile *p = (JournalFile *)pJfd;
       
 58383   memset(p, 0, sqlite3JournalSize(pVfs));
       
 58384   if( nBuf>0 ){
       
 58385     p->zBuf = sqlite3MallocZero(nBuf);
       
 58386     if( !p->zBuf ){
       
 58387       return SQLITE_NOMEM;
       
 58388     }
       
 58389   }else{
       
 58390     return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
       
 58391   }
       
 58392   p->pMethod = &JournalFileMethods;
       
 58393   p->nBuf = nBuf;
       
 58394   p->flags = flags;
       
 58395   p->zJournal = zName;
       
 58396   p->pVfs = pVfs;
       
 58397   return SQLITE_OK;
       
 58398 }
       
 58399 
       
 58400 /*
       
 58401 ** If the argument p points to a JournalFile structure, and the underlying
       
 58402 ** file has not yet been created, create it now.
       
 58403 */
       
 58404 SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
       
 58405   if( p->pMethods!=&JournalFileMethods ){
       
 58406     return SQLITE_OK;
       
 58407   }
       
 58408   return createFile((JournalFile *)p);
       
 58409 }
       
 58410 
       
 58411 /* 
       
 58412 ** Return the number of bytes required to store a JournalFile that uses vfs
       
 58413 ** pVfs to create the underlying on-disk files.
       
 58414 */
       
 58415 SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
       
 58416   return (pVfs->szOsFile+sizeof(JournalFile));
       
 58417 }
       
 58418 #endif
       
 58419 
       
 58420 /************** End of journal.c *********************************************/
       
 58421 /************** Begin file memjournal.c **************************************/
       
 58422 /*
       
 58423 ** 2008 October 7
       
 58424 **
       
 58425 ** The author disclaims copyright to this source code.  In place of
       
 58426 ** a legal notice, here is a blessing:
       
 58427 **
       
 58428 **    May you do good and not evil.
       
 58429 **    May you find forgiveness for yourself and forgive others.
       
 58430 **    May you share freely, never taking more than you give.
       
 58431 **
       
 58432 *************************************************************************
       
 58433 **
       
 58434 ** This file contains code use to implement an in-memory rollback journal.
       
 58435 ** The in-memory rollback journal is used to journal transactions for
       
 58436 ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
       
 58437 **
       
 58438 ** @(#) $Id: memjournal.c,v 1.12 2009/05/04 11:42:30 danielk1977 Exp $
       
 58439 */
       
 58440 
       
 58441 /* Forward references to internal structures */
       
 58442 typedef struct MemJournal MemJournal;
       
 58443 typedef struct FilePoint FilePoint;
       
 58444 typedef struct FileChunk FileChunk;
       
 58445 
       
 58446 /* Space to hold the rollback journal is allocated in increments of
       
 58447 ** this many bytes.
       
 58448 **
       
 58449 ** The size chosen is a little less than a power of two.  That way,
       
 58450 ** the FileChunk object will have a size that almost exactly fills
       
 58451 ** a power-of-two allocation.  This mimimizes wasted space in power-of-two
       
 58452 ** memory allocators.
       
 58453 */
       
 58454 #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
       
 58455 
       
 58456 /* Macro to find the minimum of two numeric values.
       
 58457 */
       
 58458 #ifndef MIN
       
 58459 # define MIN(x,y) ((x)<(y)?(x):(y))
       
 58460 #endif
       
 58461 
       
 58462 /*
       
 58463 ** The rollback journal is composed of a linked list of these structures.
       
 58464 */
       
 58465 struct FileChunk {
       
 58466   FileChunk *pNext;               /* Next chunk in the journal */
       
 58467   u8 zChunk[JOURNAL_CHUNKSIZE];   /* Content of this chunk */
       
 58468 };
       
 58469 
       
 58470 /*
       
 58471 ** An instance of this object serves as a cursor into the rollback journal.
       
 58472 ** The cursor can be either for reading or writing.
       
 58473 */
       
 58474 struct FilePoint {
       
 58475   sqlite3_int64 iOffset;          /* Offset from the beginning of the file */
       
 58476   FileChunk *pChunk;              /* Specific chunk into which cursor points */
       
 58477 };
       
 58478 
       
 58479 /*
       
 58480 ** This subclass is a subclass of sqlite3_file.  Each open memory-journal
       
 58481 ** is an instance of this class.
       
 58482 */
       
 58483 struct MemJournal {
       
 58484   sqlite3_io_methods *pMethod;    /* Parent class. MUST BE FIRST */
       
 58485   FileChunk *pFirst;              /* Head of in-memory chunk-list */
       
 58486   FilePoint endpoint;             /* Pointer to the end of the file */
       
 58487   FilePoint readpoint;            /* Pointer to the end of the last xRead() */
       
 58488 };
       
 58489 
       
 58490 /*
       
 58491 ** Read data from the in-memory journal file.  This is the implementation
       
 58492 ** of the sqlite3_vfs.xRead method.
       
 58493 */
       
 58494 static int memjrnlRead(
       
 58495   sqlite3_file *pJfd,    /* The journal file from which to read */
       
 58496   void *zBuf,            /* Put the results here */
       
 58497   int iAmt,              /* Number of bytes to read */
       
 58498   sqlite_int64 iOfst     /* Begin reading at this offset */
       
 58499 ){
       
 58500   MemJournal *p = (MemJournal *)pJfd;
       
 58501   u8 *zOut = zBuf;
       
 58502   int nRead = iAmt;
       
 58503   int iChunkOffset;
       
 58504   FileChunk *pChunk;
       
 58505 
       
 58506   /* SQLite never tries to read past the end of a rollback journal file */
       
 58507   assert( iOfst+iAmt<=p->endpoint.iOffset );
       
 58508 
       
 58509   if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
       
 58510     sqlite3_int64 iOff = 0;
       
 58511     for(pChunk=p->pFirst; 
       
 58512         ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
       
 58513         pChunk=pChunk->pNext
       
 58514     ){
       
 58515       iOff += JOURNAL_CHUNKSIZE;
       
 58516     }
       
 58517   }else{
       
 58518     pChunk = p->readpoint.pChunk;
       
 58519   }
       
 58520 
       
 58521   iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
       
 58522   do {
       
 58523     int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
       
 58524     int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
       
 58525     memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
       
 58526     zOut += nCopy;
       
 58527     nRead -= iSpace;
       
 58528     iChunkOffset = 0;
       
 58529   } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
       
 58530   p->readpoint.iOffset = iOfst+iAmt;
       
 58531   p->readpoint.pChunk = pChunk;
       
 58532 
       
 58533   return SQLITE_OK;
       
 58534 }
       
 58535 
       
 58536 /*
       
 58537 ** Write data to the file.
       
 58538 */
       
 58539 static int memjrnlWrite(
       
 58540   sqlite3_file *pJfd,    /* The journal file into which to write */
       
 58541   const void *zBuf,      /* Take data to be written from here */
       
 58542   int iAmt,              /* Number of bytes to write */
       
 58543   sqlite_int64 iOfst     /* Begin writing at this offset into the file */
       
 58544 ){
       
 58545   MemJournal *p = (MemJournal *)pJfd;
       
 58546   int nWrite = iAmt;
       
 58547   u8 *zWrite = (u8 *)zBuf;
       
 58548 
       
 58549   /* An in-memory journal file should only ever be appended to. Random
       
 58550   ** access writes are not required by sqlite.
       
 58551   */
       
 58552   assert( iOfst==p->endpoint.iOffset );
       
 58553   UNUSED_PARAMETER(iOfst);
       
 58554 
       
 58555   while( nWrite>0 ){
       
 58556     FileChunk *pChunk = p->endpoint.pChunk;
       
 58557     int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
       
 58558     int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
       
 58559 
       
 58560     if( iChunkOffset==0 ){
       
 58561       /* New chunk is required to extend the file. */
       
 58562       FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
       
 58563       if( !pNew ){
       
 58564         return SQLITE_IOERR_NOMEM;
       
 58565       }
       
 58566       pNew->pNext = 0;
       
 58567       if( pChunk ){
       
 58568         assert( p->pFirst );
       
 58569         pChunk->pNext = pNew;
       
 58570       }else{
       
 58571         assert( !p->pFirst );
       
 58572         p->pFirst = pNew;
       
 58573       }
       
 58574       p->endpoint.pChunk = pNew;
       
 58575     }
       
 58576 
       
 58577     memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
       
 58578     zWrite += iSpace;
       
 58579     nWrite -= iSpace;
       
 58580     p->endpoint.iOffset += iSpace;
       
 58581   }
       
 58582 
       
 58583   return SQLITE_OK;
       
 58584 }
       
 58585 
       
 58586 /*
       
 58587 ** Truncate the file.
       
 58588 */
       
 58589 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
       
 58590   MemJournal *p = (MemJournal *)pJfd;
       
 58591   FileChunk *pChunk;
       
 58592   assert(size==0);
       
 58593   UNUSED_PARAMETER(size);
       
 58594   pChunk = p->pFirst;
       
 58595   while( pChunk ){
       
 58596     FileChunk *pTmp = pChunk;
       
 58597     pChunk = pChunk->pNext;
       
 58598     sqlite3_free(pTmp);
       
 58599   }
       
 58600   sqlite3MemJournalOpen(pJfd);
       
 58601   return SQLITE_OK;
       
 58602 }
       
 58603 
       
 58604 /*
       
 58605 ** Close the file.
       
 58606 */
       
 58607 static int memjrnlClose(sqlite3_file *pJfd){
       
 58608   memjrnlTruncate(pJfd, 0);
       
 58609   return SQLITE_OK;
       
 58610 }
       
 58611 
       
 58612 
       
 58613 /*
       
 58614 ** Sync the file.
       
 58615 **
       
 58616 ** Syncing an in-memory journal is a no-op.  And, in fact, this routine
       
 58617 ** is never called in a working implementation.  This implementation
       
 58618 ** exists purely as a contingency, in case some malfunction in some other
       
 58619 ** part of SQLite causes Sync to be called by mistake.
       
 58620 */
       
 58621 static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){   /*NO_TEST*/
       
 58622   UNUSED_PARAMETER2(NotUsed, NotUsed2);                        /*NO_TEST*/
       
 58623   assert( 0 );                                                 /*NO_TEST*/
       
 58624   return SQLITE_OK;                                            /*NO_TEST*/
       
 58625 }                                                              /*NO_TEST*/
       
 58626 
       
 58627 /*
       
 58628 ** Query the size of the file in bytes.
       
 58629 */
       
 58630 static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
       
 58631   MemJournal *p = (MemJournal *)pJfd;
       
 58632   *pSize = (sqlite_int64) p->endpoint.iOffset;
       
 58633   return SQLITE_OK;
       
 58634 }
       
 58635 
       
 58636 /*
       
 58637 ** Table of methods for MemJournal sqlite3_file object.
       
 58638 */
       
 58639 static struct sqlite3_io_methods MemJournalMethods = {
       
 58640   1,                /* iVersion */
       
 58641   memjrnlClose,     /* xClose */
       
 58642   memjrnlRead,      /* xRead */
       
 58643   memjrnlWrite,     /* xWrite */
       
 58644   memjrnlTruncate,  /* xTruncate */
       
 58645   memjrnlSync,      /* xSync */
       
 58646   memjrnlFileSize,  /* xFileSize */
       
 58647   0,                /* xLock */
       
 58648   0,                /* xUnlock */
       
 58649   0,                /* xCheckReservedLock */
       
 58650   0,                /* xFileControl */
       
 58651   0,                /* xSectorSize */
       
 58652   0                 /* xDeviceCharacteristics */
       
 58653 };
       
 58654 
       
 58655 /* 
       
 58656 ** Open a journal file.
       
 58657 */
       
 58658 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
       
 58659   MemJournal *p = (MemJournal *)pJfd;
       
 58660   assert( EIGHT_BYTE_ALIGNMENT(p) );
       
 58661   memset(p, 0, sqlite3MemJournalSize());
       
 58662   p->pMethod = &MemJournalMethods;
       
 58663 }
       
 58664 
       
 58665 /*
       
 58666 ** Return true if the file-handle passed as an argument is 
       
 58667 ** an in-memory journal 
       
 58668 */
       
 58669 SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){
       
 58670   return pJfd->pMethods==&MemJournalMethods;
       
 58671 }
       
 58672 
       
 58673 /* 
       
 58674 ** Return the number of bytes required to store a MemJournal that uses vfs
       
 58675 ** pVfs to create the underlying on-disk files.
       
 58676 */
       
 58677 SQLITE_PRIVATE int sqlite3MemJournalSize(void){
       
 58678   return sizeof(MemJournal);
       
 58679 }
       
 58680 
       
 58681 /************** End of memjournal.c ******************************************/
       
 58682 /************** Begin file walker.c ******************************************/
       
 58683 /*
       
 58684 ** 2008 August 16
       
 58685 **
       
 58686 ** The author disclaims copyright to this source code.  In place of
       
 58687 ** a legal notice, here is a blessing:
       
 58688 **
       
 58689 **    May you do good and not evil.
       
 58690 **    May you find forgiveness for yourself and forgive others.
       
 58691 **    May you share freely, never taking more than you give.
       
 58692 **
       
 58693 *************************************************************************
       
 58694 ** This file contains routines used for walking the parser tree for
       
 58695 ** an SQL statement.
       
 58696 **
       
 58697 ** $Id: walker.c,v 1.7 2009/06/15 23:15:59 drh Exp $
       
 58698 */
       
 58699 
       
 58700 
       
 58701 /*
       
 58702 ** Walk an expression tree.  Invoke the callback once for each node
       
 58703 ** of the expression, while decending.  (In other words, the callback
       
 58704 ** is invoked before visiting children.)
       
 58705 **
       
 58706 ** The return value from the callback should be one of the WRC_*
       
 58707 ** constants to specify how to proceed with the walk.
       
 58708 **
       
 58709 **    WRC_Continue      Continue descending down the tree.
       
 58710 **
       
 58711 **    WRC_Prune         Do not descend into child nodes.  But allow
       
 58712 **                      the walk to continue with sibling nodes.
       
 58713 **
       
 58714 **    WRC_Abort         Do no more callbacks.  Unwind the stack and
       
 58715 **                      return the top-level walk call.
       
 58716 **
       
 58717 ** The return value from this routine is WRC_Abort to abandon the tree walk
       
 58718 ** and WRC_Continue to continue.
       
 58719 */
       
 58720 SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
       
 58721   int rc;
       
 58722   if( pExpr==0 ) return WRC_Continue;
       
 58723   testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
       
 58724   testcase( ExprHasProperty(pExpr, EP_Reduced) );
       
 58725   rc = pWalker->xExprCallback(pWalker, pExpr);
       
 58726   if( rc==WRC_Continue
       
 58727               && !ExprHasAnyProperty(pExpr,EP_TokenOnly) ){
       
 58728     if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
       
 58729     if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
       
 58730     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
       
 58731       if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
       
 58732     }else{
       
 58733       if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
       
 58734     }
       
 58735   }
       
 58736   return rc & WRC_Abort;
       
 58737 }
       
 58738 
       
 58739 /*
       
 58740 ** Call sqlite3WalkExpr() for every expression in list p or until
       
 58741 ** an abort request is seen.
       
 58742 */
       
 58743 SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
       
 58744   int i;
       
 58745   struct ExprList_item *pItem;
       
 58746   if( p ){
       
 58747     for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
       
 58748       if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
       
 58749     }
       
 58750   }
       
 58751   return WRC_Continue;
       
 58752 }
       
 58753 
       
 58754 /*
       
 58755 ** Walk all expressions associated with SELECT statement p.  Do
       
 58756 ** not invoke the SELECT callback on p, but do (of course) invoke
       
 58757 ** any expr callbacks and SELECT callbacks that come from subqueries.
       
 58758 ** Return WRC_Abort or WRC_Continue.
       
 58759 */
       
 58760 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
       
 58761   if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
       
 58762   if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
       
 58763   if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
       
 58764   if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
       
 58765   if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
       
 58766   if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
       
 58767   if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
       
 58768   return WRC_Continue;
       
 58769 }
       
 58770 
       
 58771 /*
       
 58772 ** Walk the parse trees associated with all subqueries in the
       
 58773 ** FROM clause of SELECT statement p.  Do not invoke the select
       
 58774 ** callback on p, but do invoke it on each FROM clause subquery
       
 58775 ** and on any subqueries further down in the tree.  Return 
       
 58776 ** WRC_Abort or WRC_Continue;
       
 58777 */
       
 58778 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
       
 58779   SrcList *pSrc;
       
 58780   int i;
       
 58781   struct SrcList_item *pItem;
       
 58782 
       
 58783   pSrc = p->pSrc;
       
 58784   if( ALWAYS(pSrc) ){
       
 58785     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
       
 58786       if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
       
 58787         return WRC_Abort;
       
 58788       }
       
 58789     }
       
 58790   }
       
 58791   return WRC_Continue;
       
 58792 } 
       
 58793 
       
 58794 /*
       
 58795 ** Call sqlite3WalkExpr() for every expression in Select statement p.
       
 58796 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
       
 58797 ** on the compound select chain, p->pPrior.
       
 58798 **
       
 58799 ** Return WRC_Continue under normal conditions.  Return WRC_Abort if
       
 58800 ** there is an abort request.
       
 58801 **
       
 58802 ** If the Walker does not have an xSelectCallback() then this routine
       
 58803 ** is a no-op returning WRC_Continue.
       
 58804 */
       
 58805 SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
       
 58806   int rc;
       
 58807   if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
       
 58808   rc = WRC_Continue;
       
 58809   while( p  ){
       
 58810     rc = pWalker->xSelectCallback(pWalker, p);
       
 58811     if( rc ) break;
       
 58812     if( sqlite3WalkSelectExpr(pWalker, p) ) return WRC_Abort;
       
 58813     if( sqlite3WalkSelectFrom(pWalker, p) ) return WRC_Abort;
       
 58814     p = p->pPrior;
       
 58815   }
       
 58816   return rc & WRC_Abort;
       
 58817 }
       
 58818 
       
 58819 /************** End of walker.c **********************************************/
       
 58820 /************** Begin file resolve.c *****************************************/
       
 58821 /*
       
 58822 ** 2008 August 18
       
 58823 **
       
 58824 ** The author disclaims copyright to this source code.  In place of
       
 58825 ** a legal notice, here is a blessing:
       
 58826 **
       
 58827 **    May you do good and not evil.
       
 58828 **    May you find forgiveness for yourself and forgive others.
       
 58829 **    May you share freely, never taking more than you give.
       
 58830 **
       
 58831 *************************************************************************
       
 58832 **
       
 58833 ** This file contains routines used for walking the parser tree and
       
 58834 ** resolve all identifiers by associating them with a particular
       
 58835 ** table and column.
       
 58836 **
       
 58837 ** $Id: resolve.c,v 1.30 2009/06/15 23:15:59 drh Exp $
       
 58838 */
       
 58839 
       
 58840 /*
       
 58841 ** Turn the pExpr expression into an alias for the iCol-th column of the
       
 58842 ** result set in pEList.
       
 58843 **
       
 58844 ** If the result set column is a simple column reference, then this routine
       
 58845 ** makes an exact copy.  But for any other kind of expression, this
       
 58846 ** routine make a copy of the result set column as the argument to the
       
 58847 ** TK_AS operator.  The TK_AS operator causes the expression to be
       
 58848 ** evaluated just once and then reused for each alias.
       
 58849 **
       
 58850 ** The reason for suppressing the TK_AS term when the expression is a simple
       
 58851 ** column reference is so that the column reference will be recognized as
       
 58852 ** usable by indices within the WHERE clause processing logic. 
       
 58853 **
       
 58854 ** Hack:  The TK_AS operator is inhibited if zType[0]=='G'.  This means
       
 58855 ** that in a GROUP BY clause, the expression is evaluated twice.  Hence:
       
 58856 **
       
 58857 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
       
 58858 **
       
 58859 ** Is equivalent to:
       
 58860 **
       
 58861 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
       
 58862 **
       
 58863 ** The result of random()%5 in the GROUP BY clause is probably different
       
 58864 ** from the result in the result-set.  We might fix this someday.  Or
       
 58865 ** then again, we might not...
       
 58866 */
       
 58867 static void resolveAlias(
       
 58868   Parse *pParse,         /* Parsing context */
       
 58869   ExprList *pEList,      /* A result set */
       
 58870   int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
       
 58871   Expr *pExpr,           /* Transform this into an alias to the result set */
       
 58872   const char *zType      /* "GROUP" or "ORDER" or "" */
       
 58873 ){
       
 58874   Expr *pOrig;           /* The iCol-th column of the result set */
       
 58875   Expr *pDup;            /* Copy of pOrig */
       
 58876   sqlite3 *db;           /* The database connection */
       
 58877 
       
 58878   assert( iCol>=0 && iCol<pEList->nExpr );
       
 58879   pOrig = pEList->a[iCol].pExpr;
       
 58880   assert( pOrig!=0 );
       
 58881   assert( pOrig->flags & EP_Resolved );
       
 58882   db = pParse->db;
       
 58883   if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
       
 58884     pDup = sqlite3ExprDup(db, pOrig, 0);
       
 58885     pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
       
 58886     if( pDup==0 ) return;
       
 58887     if( pEList->a[iCol].iAlias==0 ){
       
 58888       pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
       
 58889     }
       
 58890     pDup->iTable = pEList->a[iCol].iAlias;
       
 58891   }else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){
       
 58892     pDup = sqlite3ExprDup(db, pOrig, 0);
       
 58893     if( pDup==0 ) return;
       
 58894   }else{
       
 58895     char *zToken = pOrig->u.zToken;
       
 58896     assert( zToken!=0 );
       
 58897     pOrig->u.zToken = 0;
       
 58898     pDup = sqlite3ExprDup(db, pOrig, 0);
       
 58899     pOrig->u.zToken = zToken;
       
 58900     if( pDup==0 ) return;
       
 58901     assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
       
 58902     pDup->flags2 |= EP2_MallocedToken;
       
 58903     pDup->u.zToken = sqlite3DbStrDup(db, zToken);
       
 58904   }
       
 58905   if( pExpr->flags & EP_ExpCollate ){
       
 58906     pDup->pColl = pExpr->pColl;
       
 58907     pDup->flags |= EP_ExpCollate;
       
 58908   }
       
 58909   sqlite3ExprClear(db, pExpr);
       
 58910   memcpy(pExpr, pDup, sizeof(*pExpr));
       
 58911   sqlite3DbFree(db, pDup);
       
 58912 }
       
 58913 
       
 58914 /*
       
 58915 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
       
 58916 ** that name in the set of source tables in pSrcList and make the pExpr 
       
 58917 ** expression node refer back to that source column.  The following changes
       
 58918 ** are made to pExpr:
       
 58919 **
       
 58920 **    pExpr->iDb           Set the index in db->aDb[] of the database X
       
 58921 **                         (even if X is implied).
       
 58922 **    pExpr->iTable        Set to the cursor number for the table obtained
       
 58923 **                         from pSrcList.
       
 58924 **    pExpr->pTab          Points to the Table structure of X.Y (even if
       
 58925 **                         X and/or Y are implied.)
       
 58926 **    pExpr->iColumn       Set to the column number within the table.
       
 58927 **    pExpr->op            Set to TK_COLUMN.
       
 58928 **    pExpr->pLeft         Any expression this points to is deleted
       
 58929 **    pExpr->pRight        Any expression this points to is deleted.
       
 58930 **
       
 58931 ** The zDb variable is the name of the database (the "X").  This value may be
       
 58932 ** NULL meaning that name is of the form Y.Z or Z.  Any available database
       
 58933 ** can be used.  The zTable variable is the name of the table (the "Y").  This
       
 58934 ** value can be NULL if zDb is also NULL.  If zTable is NULL it
       
 58935 ** means that the form of the name is Z and that columns from any table
       
 58936 ** can be used.
       
 58937 **
       
 58938 ** If the name cannot be resolved unambiguously, leave an error message
       
 58939 ** in pParse and return WRC_Abort.  Return WRC_Prune on success.
       
 58940 */
       
 58941 static int lookupName(
       
 58942   Parse *pParse,       /* The parsing context */
       
 58943   const char *zDb,     /* Name of the database containing table, or NULL */
       
 58944   const char *zTab,    /* Name of table containing column, or NULL */
       
 58945   const char *zCol,    /* Name of the column. */
       
 58946   NameContext *pNC,    /* The name context used to resolve the name */
       
 58947   Expr *pExpr          /* Make this EXPR node point to the selected column */
       
 58948 ){
       
 58949   int i, j;            /* Loop counters */
       
 58950   int cnt = 0;                      /* Number of matching column names */
       
 58951   int cntTab = 0;                   /* Number of matching table names */
       
 58952   sqlite3 *db = pParse->db;         /* The database connection */
       
 58953   struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
       
 58954   struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
       
 58955   NameContext *pTopNC = pNC;        /* First namecontext in the list */
       
 58956   Schema *pSchema = 0;              /* Schema of the expression */
       
 58957   int isTrigger = 0;
       
 58958 
       
 58959   assert( pNC );     /* the name context cannot be NULL. */
       
 58960   assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
       
 58961   assert( ~ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
       
 58962 
       
 58963   /* Initialize the node to no-match */
       
 58964   pExpr->iTable = -1;
       
 58965   pExpr->pTab = 0;
       
 58966   ExprSetIrreducible(pExpr);
       
 58967 
       
 58968   /* Start at the inner-most context and move outward until a match is found */
       
 58969   while( pNC && cnt==0 ){
       
 58970     ExprList *pEList;
       
 58971     SrcList *pSrcList = pNC->pSrcList;
       
 58972 
       
 58973     if( pSrcList ){
       
 58974       for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
       
 58975         Table *pTab;
       
 58976         int iDb;
       
 58977         Column *pCol;
       
 58978   
       
 58979         pTab = pItem->pTab;
       
 58980         assert( pTab!=0 && pTab->zName!=0 );
       
 58981         iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       
 58982         assert( pTab->nCol>0 );
       
 58983         if( zTab ){
       
 58984           if( pItem->zAlias ){
       
 58985             char *zTabName = pItem->zAlias;
       
 58986             if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
       
 58987           }else{
       
 58988             char *zTabName = pTab->zName;
       
 58989             if( NEVER(zTabName==0) || sqlite3StrICmp(zTabName, zTab)!=0 ){
       
 58990               continue;
       
 58991             }
       
 58992             if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
       
 58993               continue;
       
 58994             }
       
 58995           }
       
 58996         }
       
 58997         if( 0==(cntTab++) ){
       
 58998           pExpr->iTable = pItem->iCursor;
       
 58999           pExpr->pTab = pTab;
       
 59000           pSchema = pTab->pSchema;
       
 59001           pMatch = pItem;
       
 59002         }
       
 59003         for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
       
 59004           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
       
 59005             IdList *pUsing;
       
 59006             cnt++;
       
 59007             pExpr->iTable = pItem->iCursor;
       
 59008             pExpr->pTab = pTab;
       
 59009             pMatch = pItem;
       
 59010             pSchema = pTab->pSchema;
       
 59011             /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
       
 59012             pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
       
 59013             if( i<pSrcList->nSrc-1 ){
       
 59014               if( pItem[1].jointype & JT_NATURAL ){
       
 59015                 /* If this match occurred in the left table of a natural join,
       
 59016                 ** then skip the right table to avoid a duplicate match */
       
 59017                 pItem++;
       
 59018                 i++;
       
 59019               }else if( (pUsing = pItem[1].pUsing)!=0 ){
       
 59020                 /* If this match occurs on a column that is in the USING clause
       
 59021                 ** of a join, skip the search of the right table of the join
       
 59022                 ** to avoid a duplicate match there. */
       
 59023                 int k;
       
 59024                 for(k=0; k<pUsing->nId; k++){
       
 59025                   if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
       
 59026                     pItem++;
       
 59027                     i++;
       
 59028                     break;
       
 59029                   }
       
 59030                 }
       
 59031               }
       
 59032             }
       
 59033             break;
       
 59034           }
       
 59035         }
       
 59036       }
       
 59037     }
       
 59038 
       
 59039 #ifndef SQLITE_OMIT_TRIGGER
       
 59040     /* If we have not already resolved the name, then maybe 
       
 59041     ** it is a new.* or old.* trigger argument reference
       
 59042     */
       
 59043     if( zDb==0 && zTab!=0 && cnt==0 && pParse->pTriggerTab!=0 ){
       
 59044       int op = pParse->eTriggerOp;
       
 59045       Table *pTab = 0;
       
 59046       assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
       
 59047       if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
       
 59048         pExpr->iTable = 1;
       
 59049         pTab = pParse->pTriggerTab;
       
 59050       }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
       
 59051         pExpr->iTable = 0;
       
 59052         pTab = pParse->pTriggerTab;
       
 59053       }
       
 59054 
       
 59055       if( pTab ){ 
       
 59056         int iCol;
       
 59057         pSchema = pTab->pSchema;
       
 59058         cntTab++;
       
 59059         if( sqlite3IsRowid(zCol) ){
       
 59060           iCol = -1;
       
 59061         }else{
       
 59062           for(iCol=0; iCol<pTab->nCol; iCol++){
       
 59063             Column *pCol = &pTab->aCol[iCol];
       
 59064             if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
       
 59065               if( iCol==pTab->iPKey ){
       
 59066                 iCol = -1;
       
 59067               }
       
 59068               break;
       
 59069             }
       
 59070           }
       
 59071         }
       
 59072         if( iCol<pTab->nCol ){
       
 59073           cnt++;
       
 59074           if( iCol<0 ){
       
 59075             pExpr->affinity = SQLITE_AFF_INTEGER;
       
 59076           }else if( pExpr->iTable==0 ){
       
 59077             testcase( iCol==31 );
       
 59078             testcase( iCol==32 );
       
 59079             pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
       
 59080           }
       
 59081           pExpr->iColumn = (i16)iCol;
       
 59082           pExpr->pTab = pTab;
       
 59083           isTrigger = 1;
       
 59084         }
       
 59085       }
       
 59086     }
       
 59087 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
       
 59088 
       
 59089     /*
       
 59090     ** Perhaps the name is a reference to the ROWID
       
 59091     */
       
 59092     if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
       
 59093       cnt = 1;
       
 59094       pExpr->iColumn = -1;
       
 59095       pExpr->affinity = SQLITE_AFF_INTEGER;
       
 59096     }
       
 59097 
       
 59098     /*
       
 59099     ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
       
 59100     ** might refer to an result-set alias.  This happens, for example, when
       
 59101     ** we are resolving names in the WHERE clause of the following command:
       
 59102     **
       
 59103     **     SELECT a+b AS x FROM table WHERE x<10;
       
 59104     **
       
 59105     ** In cases like this, replace pExpr with a copy of the expression that
       
 59106     ** forms the result set entry ("a+b" in the example) and return immediately.
       
 59107     ** Note that the expression in the result set should have already been
       
 59108     ** resolved by the time the WHERE clause is resolved.
       
 59109     */
       
 59110     if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
       
 59111       for(j=0; j<pEList->nExpr; j++){
       
 59112         char *zAs = pEList->a[j].zName;
       
 59113         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
       
 59114           Expr *pOrig;
       
 59115           assert( pExpr->pLeft==0 && pExpr->pRight==0 );
       
 59116           assert( pExpr->x.pList==0 );
       
 59117           assert( pExpr->x.pSelect==0 );
       
 59118           pOrig = pEList->a[j].pExpr;
       
 59119           if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
       
 59120             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
       
 59121             return WRC_Abort;
       
 59122           }
       
 59123           resolveAlias(pParse, pEList, j, pExpr, "");
       
 59124           cnt = 1;
       
 59125           pMatch = 0;
       
 59126           assert( zTab==0 && zDb==0 );
       
 59127           goto lookupname_end;
       
 59128         }
       
 59129       } 
       
 59130     }
       
 59131 
       
 59132     /* Advance to the next name context.  The loop will exit when either
       
 59133     ** we have a match (cnt>0) or when we run out of name contexts.
       
 59134     */
       
 59135     if( cnt==0 ){
       
 59136       pNC = pNC->pNext;
       
 59137     }
       
 59138   }
       
 59139 
       
 59140   /*
       
 59141   ** If X and Y are NULL (in other words if only the column name Z is
       
 59142   ** supplied) and the value of Z is enclosed in double-quotes, then
       
 59143   ** Z is a string literal if it doesn't match any column names.  In that
       
 59144   ** case, we need to return right away and not make any changes to
       
 59145   ** pExpr.
       
 59146   **
       
 59147   ** Because no reference was made to outer contexts, the pNC->nRef
       
 59148   ** fields are not changed in any context.
       
 59149   */
       
 59150   if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
       
 59151     pExpr->op = TK_STRING;
       
 59152     pExpr->pTab = 0;
       
 59153     return WRC_Prune;
       
 59154   }
       
 59155 
       
 59156   /*
       
 59157   ** cnt==0 means there was not match.  cnt>1 means there were two or
       
 59158   ** more matches.  Either way, we have an error.
       
 59159   */
       
 59160   if( cnt!=1 ){
       
 59161     const char *zErr;
       
 59162     zErr = cnt==0 ? "no such column" : "ambiguous column name";
       
 59163     if( zDb ){
       
 59164       sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
       
 59165     }else if( zTab ){
       
 59166       sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
       
 59167     }else{
       
 59168       sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
       
 59169     }
       
 59170     pTopNC->nErr++;
       
 59171   }
       
 59172 
       
 59173   /* If a column from a table in pSrcList is referenced, then record
       
 59174   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
       
 59175   ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
       
 59176   ** column number is greater than the number of bits in the bitmask
       
 59177   ** then set the high-order bit of the bitmask.
       
 59178   */
       
 59179   if( pExpr->iColumn>=0 && pMatch!=0 ){
       
 59180     int n = pExpr->iColumn;
       
 59181     testcase( n==BMS-1 );
       
 59182     if( n>=BMS ){
       
 59183       n = BMS-1;
       
 59184     }
       
 59185     assert( pMatch->iCursor==pExpr->iTable );
       
 59186     pMatch->colUsed |= ((Bitmask)1)<<n;
       
 59187   }
       
 59188 
       
 59189   /* Clean up and return
       
 59190   */
       
 59191   sqlite3ExprDelete(db, pExpr->pLeft);
       
 59192   pExpr->pLeft = 0;
       
 59193   sqlite3ExprDelete(db, pExpr->pRight);
       
 59194   pExpr->pRight = 0;
       
 59195   pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
       
 59196 lookupname_end:
       
 59197   if( cnt==1 ){
       
 59198     assert( pNC!=0 );
       
 59199     sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
       
 59200     /* Increment the nRef value on all name contexts from TopNC up to
       
 59201     ** the point where the name matched. */
       
 59202     for(;;){
       
 59203       assert( pTopNC!=0 );
       
 59204       pTopNC->nRef++;
       
 59205       if( pTopNC==pNC ) break;
       
 59206       pTopNC = pTopNC->pNext;
       
 59207     }
       
 59208     return WRC_Prune;
       
 59209   } else {
       
 59210     return WRC_Abort;
       
 59211   }
       
 59212 }
       
 59213 
       
 59214 /*
       
 59215 ** This routine is callback for sqlite3WalkExpr().
       
 59216 **
       
 59217 ** Resolve symbolic names into TK_COLUMN operators for the current
       
 59218 ** node in the expression tree.  Return 0 to continue the search down
       
 59219 ** the tree or 2 to abort the tree walk.
       
 59220 **
       
 59221 ** This routine also does error checking and name resolution for
       
 59222 ** function names.  The operator for aggregate functions is changed
       
 59223 ** to TK_AGG_FUNCTION.
       
 59224 */
       
 59225 static int resolveExprStep(Walker *pWalker, Expr *pExpr){
       
 59226   NameContext *pNC;
       
 59227   Parse *pParse;
       
 59228 
       
 59229   pNC = pWalker->u.pNC;
       
 59230   assert( pNC!=0 );
       
 59231   pParse = pNC->pParse;
       
 59232   assert( pParse==pWalker->pParse );
       
 59233 
       
 59234   if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune;
       
 59235   ExprSetProperty(pExpr, EP_Resolved);
       
 59236 #ifndef NDEBUG
       
 59237   if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
       
 59238     SrcList *pSrcList = pNC->pSrcList;
       
 59239     int i;
       
 59240     for(i=0; i<pNC->pSrcList->nSrc; i++){
       
 59241       assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
       
 59242     }
       
 59243   }
       
 59244 #endif
       
 59245   switch( pExpr->op ){
       
 59246 
       
 59247 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
       
 59248     /* The special operator TK_ROW means use the rowid for the first
       
 59249     ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
       
 59250     ** clause processing on UPDATE and DELETE statements.
       
 59251     */
       
 59252     case TK_ROW: {
       
 59253       SrcList *pSrcList = pNC->pSrcList;
       
 59254       struct SrcList_item *pItem;
       
 59255       assert( pSrcList && pSrcList->nSrc==1 );
       
 59256       pItem = pSrcList->a; 
       
 59257       pExpr->op = TK_COLUMN;
       
 59258       pExpr->pTab = pItem->pTab;
       
 59259       pExpr->iTable = pItem->iCursor;
       
 59260       pExpr->iColumn = -1;
       
 59261       pExpr->affinity = SQLITE_AFF_INTEGER;
       
 59262       break;
       
 59263     }
       
 59264 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
       
 59265 
       
 59266     /* A lone identifier is the name of a column.
       
 59267     */
       
 59268     case TK_ID: {
       
 59269       return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
       
 59270     }
       
 59271   
       
 59272     /* A table name and column name:     ID.ID
       
 59273     ** Or a database, table and column:  ID.ID.ID
       
 59274     */
       
 59275     case TK_DOT: {
       
 59276       const char *zColumn;
       
 59277       const char *zTable;
       
 59278       const char *zDb;
       
 59279       Expr *pRight;
       
 59280 
       
 59281       /* if( pSrcList==0 ) break; */
       
 59282       pRight = pExpr->pRight;
       
 59283       if( pRight->op==TK_ID ){
       
 59284         zDb = 0;
       
 59285         zTable = pExpr->pLeft->u.zToken;
       
 59286         zColumn = pRight->u.zToken;
       
 59287       }else{
       
 59288         assert( pRight->op==TK_DOT );
       
 59289         zDb = pExpr->pLeft->u.zToken;
       
 59290         zTable = pRight->pLeft->u.zToken;
       
 59291         zColumn = pRight->pRight->u.zToken;
       
 59292       }
       
 59293       return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
       
 59294     }
       
 59295 
       
 59296     /* Resolve function names
       
 59297     */
       
 59298     case TK_CONST_FUNC:
       
 59299     case TK_FUNCTION: {
       
 59300       ExprList *pList = pExpr->x.pList;    /* The argument list */
       
 59301       int n = pList ? pList->nExpr : 0;    /* Number of arguments */
       
 59302       int no_such_func = 0;       /* True if no such function exists */
       
 59303       int wrong_num_args = 0;     /* True if wrong number of arguments */
       
 59304       int is_agg = 0;             /* True if is an aggregate function */
       
 59305       int auth;                   /* Authorization to use the function */
       
 59306       int nId;                    /* Number of characters in function name */
       
 59307       const char *zId;            /* The function name. */
       
 59308       FuncDef *pDef;              /* Information about the function */
       
 59309       u8 enc = ENC(pParse->db);   /* The database encoding */
       
 59310 
       
 59311       testcase( pExpr->op==TK_CONST_FUNC );
       
 59312       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
       
 59313       zId = pExpr->u.zToken;
       
 59314       nId = sqlite3Strlen30(zId);
       
 59315       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
       
 59316       if( pDef==0 ){
       
 59317         pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
       
 59318         if( pDef==0 ){
       
 59319           no_such_func = 1;
       
 59320         }else{
       
 59321           wrong_num_args = 1;
       
 59322         }
       
 59323       }else{
       
 59324         is_agg = pDef->xFunc==0;
       
 59325       }
       
 59326 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 59327       if( pDef ){
       
 59328         auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
       
 59329         if( auth!=SQLITE_OK ){
       
 59330           if( auth==SQLITE_DENY ){
       
 59331             sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
       
 59332                                     pDef->zName);
       
 59333             pNC->nErr++;
       
 59334           }
       
 59335           pExpr->op = TK_NULL;
       
 59336           return WRC_Prune;
       
 59337         }
       
 59338       }
       
 59339 #endif
       
 59340       if( is_agg && !pNC->allowAgg ){
       
 59341         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
       
 59342         pNC->nErr++;
       
 59343         is_agg = 0;
       
 59344       }else if( no_such_func ){
       
 59345         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
       
 59346         pNC->nErr++;
       
 59347       }else if( wrong_num_args ){
       
 59348         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
       
 59349              nId, zId);
       
 59350         pNC->nErr++;
       
 59351       }
       
 59352       if( is_agg ){
       
 59353         pExpr->op = TK_AGG_FUNCTION;
       
 59354         pNC->hasAgg = 1;
       
 59355       }
       
 59356       if( is_agg ) pNC->allowAgg = 0;
       
 59357       sqlite3WalkExprList(pWalker, pList);
       
 59358       if( is_agg ) pNC->allowAgg = 1;
       
 59359       /* FIX ME:  Compute pExpr->affinity based on the expected return
       
 59360       ** type of the function 
       
 59361       */
       
 59362       return WRC_Prune;
       
 59363     }
       
 59364 #ifndef SQLITE_OMIT_SUBQUERY
       
 59365     case TK_SELECT:
       
 59366     case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
       
 59367 #endif
       
 59368     case TK_IN: {
       
 59369       testcase( pExpr->op==TK_IN );
       
 59370       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
       
 59371         int nRef = pNC->nRef;
       
 59372 #ifndef SQLITE_OMIT_CHECK
       
 59373         if( pNC->isCheck ){
       
 59374           sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
       
 59375         }
       
 59376 #endif
       
 59377         sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
       
 59378         assert( pNC->nRef>=nRef );
       
 59379         if( nRef!=pNC->nRef ){
       
 59380           ExprSetProperty(pExpr, EP_VarSelect);
       
 59381         }
       
 59382       }
       
 59383       break;
       
 59384     }
       
 59385 #ifndef SQLITE_OMIT_CHECK
       
 59386     case TK_VARIABLE: {
       
 59387       if( pNC->isCheck ){
       
 59388         sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
       
 59389       }
       
 59390       break;
       
 59391     }
       
 59392 #endif
       
 59393   }
       
 59394   return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
       
 59395 }
       
 59396 
       
 59397 /*
       
 59398 ** pEList is a list of expressions which are really the result set of the
       
 59399 ** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
       
 59400 ** This routine checks to see if pE is a simple identifier which corresponds
       
 59401 ** to the AS-name of one of the terms of the expression list.  If it is,
       
 59402 ** this routine return an integer between 1 and N where N is the number of
       
 59403 ** elements in pEList, corresponding to the matching entry.  If there is
       
 59404 ** no match, or if pE is not a simple identifier, then this routine
       
 59405 ** return 0.
       
 59406 **
       
 59407 ** pEList has been resolved.  pE has not.
       
 59408 */
       
 59409 static int resolveAsName(
       
 59410   Parse *pParse,     /* Parsing context for error messages */
       
 59411   ExprList *pEList,  /* List of expressions to scan */
       
 59412   Expr *pE           /* Expression we are trying to match */
       
 59413 ){
       
 59414   int i;             /* Loop counter */
       
 59415 
       
 59416   UNUSED_PARAMETER(pParse);
       
 59417 
       
 59418   if( pE->op==TK_ID ){
       
 59419     char *zCol = pE->u.zToken;
       
 59420     for(i=0; i<pEList->nExpr; i++){
       
 59421       char *zAs = pEList->a[i].zName;
       
 59422       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
       
 59423         return i+1;
       
 59424       }
       
 59425     }
       
 59426   }
       
 59427   return 0;
       
 59428 }
       
 59429 
       
 59430 /*
       
 59431 ** pE is a pointer to an expression which is a single term in the
       
 59432 ** ORDER BY of a compound SELECT.  The expression has not been
       
 59433 ** name resolved.
       
 59434 **
       
 59435 ** At the point this routine is called, we already know that the
       
 59436 ** ORDER BY term is not an integer index into the result set.  That
       
 59437 ** case is handled by the calling routine.
       
 59438 **
       
 59439 ** Attempt to match pE against result set columns in the left-most
       
 59440 ** SELECT statement.  Return the index i of the matching column,
       
 59441 ** as an indication to the caller that it should sort by the i-th column.
       
 59442 ** The left-most column is 1.  In other words, the value returned is the
       
 59443 ** same integer value that would be used in the SQL statement to indicate
       
 59444 ** the column.
       
 59445 **
       
 59446 ** If there is no match, return 0.  Return -1 if an error occurs.
       
 59447 */
       
 59448 static int resolveOrderByTermToExprList(
       
 59449   Parse *pParse,     /* Parsing context for error messages */
       
 59450   Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
       
 59451   Expr *pE           /* The specific ORDER BY term */
       
 59452 ){
       
 59453   int i;             /* Loop counter */
       
 59454   ExprList *pEList;  /* The columns of the result set */
       
 59455   NameContext nc;    /* Name context for resolving pE */
       
 59456 
       
 59457   assert( sqlite3ExprIsInteger(pE, &i)==0 );
       
 59458   pEList = pSelect->pEList;
       
 59459 
       
 59460   /* Resolve all names in the ORDER BY term expression
       
 59461   */
       
 59462   memset(&nc, 0, sizeof(nc));
       
 59463   nc.pParse = pParse;
       
 59464   nc.pSrcList = pSelect->pSrc;
       
 59465   nc.pEList = pEList;
       
 59466   nc.allowAgg = 1;
       
 59467   nc.nErr = 0;
       
 59468   if( sqlite3ResolveExprNames(&nc, pE) ){
       
 59469     sqlite3ErrorClear(pParse);
       
 59470     return 0;
       
 59471   }
       
 59472 
       
 59473   /* Try to match the ORDER BY expression against an expression
       
 59474   ** in the result set.  Return an 1-based index of the matching
       
 59475   ** result-set entry.
       
 59476   */
       
 59477   for(i=0; i<pEList->nExpr; i++){
       
 59478     if( sqlite3ExprCompare(pEList->a[i].pExpr, pE) ){
       
 59479       return i+1;
       
 59480     }
       
 59481   }
       
 59482 
       
 59483   /* If no match, return 0. */
       
 59484   return 0;
       
 59485 }
       
 59486 
       
 59487 /*
       
 59488 ** Generate an ORDER BY or GROUP BY term out-of-range error.
       
 59489 */
       
 59490 static void resolveOutOfRangeError(
       
 59491   Parse *pParse,         /* The error context into which to write the error */
       
 59492   const char *zType,     /* "ORDER" or "GROUP" */
       
 59493   int i,                 /* The index (1-based) of the term out of range */
       
 59494   int mx                 /* Largest permissible value of i */
       
 59495 ){
       
 59496   sqlite3ErrorMsg(pParse, 
       
 59497     "%r %s BY term out of range - should be "
       
 59498     "between 1 and %d", i, zType, mx);
       
 59499 }
       
 59500 
       
 59501 /*
       
 59502 ** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
       
 59503 ** each term of the ORDER BY clause is a constant integer between 1
       
 59504 ** and N where N is the number of columns in the compound SELECT.
       
 59505 **
       
 59506 ** ORDER BY terms that are already an integer between 1 and N are
       
 59507 ** unmodified.  ORDER BY terms that are integers outside the range of
       
 59508 ** 1 through N generate an error.  ORDER BY terms that are expressions
       
 59509 ** are matched against result set expressions of compound SELECT
       
 59510 ** beginning with the left-most SELECT and working toward the right.
       
 59511 ** At the first match, the ORDER BY expression is transformed into
       
 59512 ** the integer column number.
       
 59513 **
       
 59514 ** Return the number of errors seen.
       
 59515 */
       
 59516 static int resolveCompoundOrderBy(
       
 59517   Parse *pParse,        /* Parsing context.  Leave error messages here */
       
 59518   Select *pSelect       /* The SELECT statement containing the ORDER BY */
       
 59519 ){
       
 59520   int i;
       
 59521   ExprList *pOrderBy;
       
 59522   ExprList *pEList;
       
 59523   sqlite3 *db;
       
 59524   int moreToDo = 1;
       
 59525 
       
 59526   pOrderBy = pSelect->pOrderBy;
       
 59527   if( pOrderBy==0 ) return 0;
       
 59528   db = pParse->db;
       
 59529 #if SQLITE_MAX_COLUMN
       
 59530   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
       
 59531     sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
       
 59532     return 1;
       
 59533   }
       
 59534 #endif
       
 59535   for(i=0; i<pOrderBy->nExpr; i++){
       
 59536     pOrderBy->a[i].done = 0;
       
 59537   }
       
 59538   pSelect->pNext = 0;
       
 59539   while( pSelect->pPrior ){
       
 59540     pSelect->pPrior->pNext = pSelect;
       
 59541     pSelect = pSelect->pPrior;
       
 59542   }
       
 59543   while( pSelect && moreToDo ){
       
 59544     struct ExprList_item *pItem;
       
 59545     moreToDo = 0;
       
 59546     pEList = pSelect->pEList;
       
 59547     assert( pEList!=0 );
       
 59548     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
       
 59549       int iCol = -1;
       
 59550       Expr *pE, *pDup;
       
 59551       if( pItem->done ) continue;
       
 59552       pE = pItem->pExpr;
       
 59553       if( sqlite3ExprIsInteger(pE, &iCol) ){
       
 59554         if( iCol<=0 || iCol>pEList->nExpr ){
       
 59555           resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
       
 59556           return 1;
       
 59557         }
       
 59558       }else{
       
 59559         iCol = resolveAsName(pParse, pEList, pE);
       
 59560         if( iCol==0 ){
       
 59561           pDup = sqlite3ExprDup(db, pE, 0);
       
 59562           if( !db->mallocFailed ){
       
 59563             assert(pDup);
       
 59564             iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
       
 59565           }
       
 59566           sqlite3ExprDelete(db, pDup);
       
 59567         }
       
 59568       }
       
 59569       if( iCol>0 ){
       
 59570         CollSeq *pColl = pE->pColl;
       
 59571         int flags = pE->flags & EP_ExpCollate;
       
 59572         sqlite3ExprDelete(db, pE);
       
 59573         pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
       
 59574         if( pE==0 ) return 1;
       
 59575         pE->pColl = pColl;
       
 59576         pE->flags |= EP_IntValue | flags;
       
 59577         pE->u.iValue = iCol;
       
 59578         pItem->iCol = (u16)iCol;
       
 59579         pItem->done = 1;
       
 59580       }else{
       
 59581         moreToDo = 1;
       
 59582       }
       
 59583     }
       
 59584     pSelect = pSelect->pNext;
       
 59585   }
       
 59586   for(i=0; i<pOrderBy->nExpr; i++){
       
 59587     if( pOrderBy->a[i].done==0 ){
       
 59588       sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
       
 59589             "column in the result set", i+1);
       
 59590       return 1;
       
 59591     }
       
 59592   }
       
 59593   return 0;
       
 59594 }
       
 59595 
       
 59596 /*
       
 59597 ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
       
 59598 ** the SELECT statement pSelect.  If any term is reference to a
       
 59599 ** result set expression (as determined by the ExprList.a.iCol field)
       
 59600 ** then convert that term into a copy of the corresponding result set
       
 59601 ** column.
       
 59602 **
       
 59603 ** If any errors are detected, add an error message to pParse and
       
 59604 ** return non-zero.  Return zero if no errors are seen.
       
 59605 */
       
 59606 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
       
 59607   Parse *pParse,        /* Parsing context.  Leave error messages here */
       
 59608   Select *pSelect,      /* The SELECT statement containing the clause */
       
 59609   ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
       
 59610   const char *zType     /* "ORDER" or "GROUP" */
       
 59611 ){
       
 59612   int i;
       
 59613   sqlite3 *db = pParse->db;
       
 59614   ExprList *pEList;
       
 59615   struct ExprList_item *pItem;
       
 59616 
       
 59617   if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
       
 59618 #if SQLITE_MAX_COLUMN
       
 59619   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
       
 59620     sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
       
 59621     return 1;
       
 59622   }
       
 59623 #endif
       
 59624   pEList = pSelect->pEList;
       
 59625   assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
       
 59626   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
       
 59627     if( pItem->iCol ){
       
 59628       if( pItem->iCol>pEList->nExpr ){
       
 59629         resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
       
 59630         return 1;
       
 59631       }
       
 59632       resolveAlias(pParse, pEList, pItem->iCol-1, pItem->pExpr, zType);
       
 59633     }
       
 59634   }
       
 59635   return 0;
       
 59636 }
       
 59637 
       
 59638 /*
       
 59639 ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
       
 59640 ** The Name context of the SELECT statement is pNC.  zType is either
       
 59641 ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
       
 59642 **
       
 59643 ** This routine resolves each term of the clause into an expression.
       
 59644 ** If the order-by term is an integer I between 1 and N (where N is the
       
 59645 ** number of columns in the result set of the SELECT) then the expression
       
 59646 ** in the resolution is a copy of the I-th result-set expression.  If
       
 59647 ** the order-by term is an identify that corresponds to the AS-name of
       
 59648 ** a result-set expression, then the term resolves to a copy of the
       
 59649 ** result-set expression.  Otherwise, the expression is resolved in
       
 59650 ** the usual way - using sqlite3ResolveExprNames().
       
 59651 **
       
 59652 ** This routine returns the number of errors.  If errors occur, then
       
 59653 ** an appropriate error message might be left in pParse.  (OOM errors
       
 59654 ** excepted.)
       
 59655 */
       
 59656 static int resolveOrderGroupBy(
       
 59657   NameContext *pNC,     /* The name context of the SELECT statement */
       
 59658   Select *pSelect,      /* The SELECT statement holding pOrderBy */
       
 59659   ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
       
 59660   const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
       
 59661 ){
       
 59662   int i;                         /* Loop counter */
       
 59663   int iCol;                      /* Column number */
       
 59664   struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
       
 59665   Parse *pParse;                 /* Parsing context */
       
 59666   int nResult;                   /* Number of terms in the result set */
       
 59667 
       
 59668   if( pOrderBy==0 ) return 0;
       
 59669   nResult = pSelect->pEList->nExpr;
       
 59670   pParse = pNC->pParse;
       
 59671   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
       
 59672     Expr *pE = pItem->pExpr;
       
 59673     iCol = resolveAsName(pParse, pSelect->pEList, pE);
       
 59674     if( iCol>0 ){
       
 59675       /* If an AS-name match is found, mark this ORDER BY column as being
       
 59676       ** a copy of the iCol-th result-set column.  The subsequent call to
       
 59677       ** sqlite3ResolveOrderGroupBy() will convert the expression to a
       
 59678       ** copy of the iCol-th result-set expression. */
       
 59679       pItem->iCol = (u16)iCol;
       
 59680       continue;
       
 59681     }
       
 59682     if( sqlite3ExprIsInteger(pE, &iCol) ){
       
 59683       /* The ORDER BY term is an integer constant.  Again, set the column
       
 59684       ** number so that sqlite3ResolveOrderGroupBy() will convert the
       
 59685       ** order-by term to a copy of the result-set expression */
       
 59686       if( iCol<1 ){
       
 59687         resolveOutOfRangeError(pParse, zType, i+1, nResult);
       
 59688         return 1;
       
 59689       }
       
 59690       pItem->iCol = (u16)iCol;
       
 59691       continue;
       
 59692     }
       
 59693 
       
 59694     /* Otherwise, treat the ORDER BY term as an ordinary expression */
       
 59695     pItem->iCol = 0;
       
 59696     if( sqlite3ResolveExprNames(pNC, pE) ){
       
 59697       return 1;
       
 59698     }
       
 59699   }
       
 59700   return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
       
 59701 }
       
 59702 
       
 59703 /*
       
 59704 ** Resolve names in the SELECT statement p and all of its descendents.
       
 59705 */
       
 59706 static int resolveSelectStep(Walker *pWalker, Select *p){
       
 59707   NameContext *pOuterNC;  /* Context that contains this SELECT */
       
 59708   NameContext sNC;        /* Name context of this SELECT */
       
 59709   int isCompound;         /* True if p is a compound select */
       
 59710   int nCompound;          /* Number of compound terms processed so far */
       
 59711   Parse *pParse;          /* Parsing context */
       
 59712   ExprList *pEList;       /* Result set expression list */
       
 59713   int i;                  /* Loop counter */
       
 59714   ExprList *pGroupBy;     /* The GROUP BY clause */
       
 59715   Select *pLeftmost;      /* Left-most of SELECT of a compound */
       
 59716   sqlite3 *db;            /* Database connection */
       
 59717   
       
 59718 
       
 59719   assert( p!=0 );
       
 59720   if( p->selFlags & SF_Resolved ){
       
 59721     return WRC_Prune;
       
 59722   }
       
 59723   pOuterNC = pWalker->u.pNC;
       
 59724   pParse = pWalker->pParse;
       
 59725   db = pParse->db;
       
 59726 
       
 59727   /* Normally sqlite3SelectExpand() will be called first and will have
       
 59728   ** already expanded this SELECT.  However, if this is a subquery within
       
 59729   ** an expression, sqlite3ResolveExprNames() will be called without a
       
 59730   ** prior call to sqlite3SelectExpand().  When that happens, let
       
 59731   ** sqlite3SelectPrep() do all of the processing for this SELECT.
       
 59732   ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
       
 59733   ** this routine in the correct order.
       
 59734   */
       
 59735   if( (p->selFlags & SF_Expanded)==0 ){
       
 59736     sqlite3SelectPrep(pParse, p, pOuterNC);
       
 59737     return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
       
 59738   }
       
 59739 
       
 59740   isCompound = p->pPrior!=0;
       
 59741   nCompound = 0;
       
 59742   pLeftmost = p;
       
 59743   while( p ){
       
 59744     assert( (p->selFlags & SF_Expanded)!=0 );
       
 59745     assert( (p->selFlags & SF_Resolved)==0 );
       
 59746     p->selFlags |= SF_Resolved;
       
 59747 
       
 59748     /* Resolve the expressions in the LIMIT and OFFSET clauses. These
       
 59749     ** are not allowed to refer to any names, so pass an empty NameContext.
       
 59750     */
       
 59751     memset(&sNC, 0, sizeof(sNC));
       
 59752     sNC.pParse = pParse;
       
 59753     if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
       
 59754         sqlite3ResolveExprNames(&sNC, p->pOffset) ){
       
 59755       return WRC_Abort;
       
 59756     }
       
 59757   
       
 59758     /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
       
 59759     ** resolve the result-set expression list.
       
 59760     */
       
 59761     sNC.allowAgg = 1;
       
 59762     sNC.pSrcList = p->pSrc;
       
 59763     sNC.pNext = pOuterNC;
       
 59764   
       
 59765     /* Resolve names in the result set. */
       
 59766     pEList = p->pEList;
       
 59767     assert( pEList!=0 );
       
 59768     for(i=0; i<pEList->nExpr; i++){
       
 59769       Expr *pX = pEList->a[i].pExpr;
       
 59770       if( sqlite3ResolveExprNames(&sNC, pX) ){
       
 59771         return WRC_Abort;
       
 59772       }
       
 59773     }
       
 59774   
       
 59775     /* Recursively resolve names in all subqueries
       
 59776     */
       
 59777     for(i=0; i<p->pSrc->nSrc; i++){
       
 59778       struct SrcList_item *pItem = &p->pSrc->a[i];
       
 59779       if( pItem->pSelect ){
       
 59780         const char *zSavedContext = pParse->zAuthContext;
       
 59781         if( pItem->zName ) pParse->zAuthContext = pItem->zName;
       
 59782         sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
       
 59783         pParse->zAuthContext = zSavedContext;
       
 59784         if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
       
 59785       }
       
 59786     }
       
 59787   
       
 59788     /* If there are no aggregate functions in the result-set, and no GROUP BY 
       
 59789     ** expression, do not allow aggregates in any of the other expressions.
       
 59790     */
       
 59791     assert( (p->selFlags & SF_Aggregate)==0 );
       
 59792     pGroupBy = p->pGroupBy;
       
 59793     if( pGroupBy || sNC.hasAgg ){
       
 59794       p->selFlags |= SF_Aggregate;
       
 59795     }else{
       
 59796       sNC.allowAgg = 0;
       
 59797     }
       
 59798   
       
 59799     /* If a HAVING clause is present, then there must be a GROUP BY clause.
       
 59800     */
       
 59801     if( p->pHaving && !pGroupBy ){
       
 59802       sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
       
 59803       return WRC_Abort;
       
 59804     }
       
 59805   
       
 59806     /* Add the expression list to the name-context before parsing the
       
 59807     ** other expressions in the SELECT statement. This is so that
       
 59808     ** expressions in the WHERE clause (etc.) can refer to expressions by
       
 59809     ** aliases in the result set.
       
 59810     **
       
 59811     ** Minor point: If this is the case, then the expression will be
       
 59812     ** re-evaluated for each reference to it.
       
 59813     */
       
 59814     sNC.pEList = p->pEList;
       
 59815     if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
       
 59816        sqlite3ResolveExprNames(&sNC, p->pHaving)
       
 59817     ){
       
 59818       return WRC_Abort;
       
 59819     }
       
 59820 
       
 59821     /* The ORDER BY and GROUP BY clauses may not refer to terms in
       
 59822     ** outer queries 
       
 59823     */
       
 59824     sNC.pNext = 0;
       
 59825     sNC.allowAgg = 1;
       
 59826 
       
 59827     /* Process the ORDER BY clause for singleton SELECT statements.
       
 59828     ** The ORDER BY clause for compounds SELECT statements is handled
       
 59829     ** below, after all of the result-sets for all of the elements of
       
 59830     ** the compound have been resolved.
       
 59831     */
       
 59832     if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
       
 59833       return WRC_Abort;
       
 59834     }
       
 59835     if( db->mallocFailed ){
       
 59836       return WRC_Abort;
       
 59837     }
       
 59838   
       
 59839     /* Resolve the GROUP BY clause.  At the same time, make sure 
       
 59840     ** the GROUP BY clause does not contain aggregate functions.
       
 59841     */
       
 59842     if( pGroupBy ){
       
 59843       struct ExprList_item *pItem;
       
 59844     
       
 59845       if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
       
 59846         return WRC_Abort;
       
 59847       }
       
 59848       for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
       
 59849         if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
       
 59850           sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
       
 59851               "the GROUP BY clause");
       
 59852           return WRC_Abort;
       
 59853         }
       
 59854       }
       
 59855     }
       
 59856 
       
 59857     /* Advance to the next term of the compound
       
 59858     */
       
 59859     p = p->pPrior;
       
 59860     nCompound++;
       
 59861   }
       
 59862 
       
 59863   /* Resolve the ORDER BY on a compound SELECT after all terms of
       
 59864   ** the compound have been resolved.
       
 59865   */
       
 59866   if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
       
 59867     return WRC_Abort;
       
 59868   }
       
 59869 
       
 59870   return WRC_Prune;
       
 59871 }
       
 59872 
       
 59873 /*
       
 59874 ** This routine walks an expression tree and resolves references to
       
 59875 ** table columns and result-set columns.  At the same time, do error
       
 59876 ** checking on function usage and set a flag if any aggregate functions
       
 59877 ** are seen.
       
 59878 **
       
 59879 ** To resolve table columns references we look for nodes (or subtrees) of the 
       
 59880 ** form X.Y.Z or Y.Z or just Z where
       
 59881 **
       
 59882 **      X:   The name of a database.  Ex:  "main" or "temp" or
       
 59883 **           the symbolic name assigned to an ATTACH-ed database.
       
 59884 **
       
 59885 **      Y:   The name of a table in a FROM clause.  Or in a trigger
       
 59886 **           one of the special names "old" or "new".
       
 59887 **
       
 59888 **      Z:   The name of a column in table Y.
       
 59889 **
       
 59890 ** The node at the root of the subtree is modified as follows:
       
 59891 **
       
 59892 **    Expr.op        Changed to TK_COLUMN
       
 59893 **    Expr.pTab      Points to the Table object for X.Y
       
 59894 **    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
       
 59895 **    Expr.iTable    The VDBE cursor number for X.Y
       
 59896 **
       
 59897 **
       
 59898 ** To resolve result-set references, look for expression nodes of the
       
 59899 ** form Z (with no X and Y prefix) where the Z matches the right-hand
       
 59900 ** size of an AS clause in the result-set of a SELECT.  The Z expression
       
 59901 ** is replaced by a copy of the left-hand side of the result-set expression.
       
 59902 ** Table-name and function resolution occurs on the substituted expression
       
 59903 ** tree.  For example, in:
       
 59904 **
       
 59905 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
       
 59906 **
       
 59907 ** The "x" term of the order by is replaced by "a+b" to render:
       
 59908 **
       
 59909 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
       
 59910 **
       
 59911 ** Function calls are checked to make sure that the function is 
       
 59912 ** defined and that the correct number of arguments are specified.
       
 59913 ** If the function is an aggregate function, then the pNC->hasAgg is
       
 59914 ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
       
 59915 ** If an expression contains aggregate functions then the EP_Agg
       
 59916 ** property on the expression is set.
       
 59917 **
       
 59918 ** An error message is left in pParse if anything is amiss.  The number
       
 59919 ** if errors is returned.
       
 59920 */
       
 59921 SQLITE_PRIVATE int sqlite3ResolveExprNames( 
       
 59922   NameContext *pNC,       /* Namespace to resolve expressions in. */
       
 59923   Expr *pExpr             /* The expression to be analyzed. */
       
 59924 ){
       
 59925   int savedHasAgg;
       
 59926   Walker w;
       
 59927 
       
 59928   if( pExpr==0 ) return 0;
       
 59929 #if SQLITE_MAX_EXPR_DEPTH>0
       
 59930   {
       
 59931     Parse *pParse = pNC->pParse;
       
 59932     if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
       
 59933       return 1;
       
 59934     }
       
 59935     pParse->nHeight += pExpr->nHeight;
       
 59936   }
       
 59937 #endif
       
 59938   savedHasAgg = pNC->hasAgg;
       
 59939   pNC->hasAgg = 0;
       
 59940   w.xExprCallback = resolveExprStep;
       
 59941   w.xSelectCallback = resolveSelectStep;
       
 59942   w.pParse = pNC->pParse;
       
 59943   w.u.pNC = pNC;
       
 59944   sqlite3WalkExpr(&w, pExpr);
       
 59945 #if SQLITE_MAX_EXPR_DEPTH>0
       
 59946   pNC->pParse->nHeight -= pExpr->nHeight;
       
 59947 #endif
       
 59948   if( pNC->nErr>0 || w.pParse->nErr>0 ){
       
 59949     ExprSetProperty(pExpr, EP_Error);
       
 59950   }
       
 59951   if( pNC->hasAgg ){
       
 59952     ExprSetProperty(pExpr, EP_Agg);
       
 59953   }else if( savedHasAgg ){
       
 59954     pNC->hasAgg = 1;
       
 59955   }
       
 59956   return ExprHasProperty(pExpr, EP_Error);
       
 59957 }
       
 59958 
       
 59959 
       
 59960 /*
       
 59961 ** Resolve all names in all expressions of a SELECT and in all
       
 59962 ** decendents of the SELECT, including compounds off of p->pPrior,
       
 59963 ** subqueries in expressions, and subqueries used as FROM clause
       
 59964 ** terms.
       
 59965 **
       
 59966 ** See sqlite3ResolveExprNames() for a description of the kinds of
       
 59967 ** transformations that occur.
       
 59968 **
       
 59969 ** All SELECT statements should have been expanded using
       
 59970 ** sqlite3SelectExpand() prior to invoking this routine.
       
 59971 */
       
 59972 SQLITE_PRIVATE void sqlite3ResolveSelectNames(
       
 59973   Parse *pParse,         /* The parser context */
       
 59974   Select *p,             /* The SELECT statement being coded. */
       
 59975   NameContext *pOuterNC  /* Name context for parent SELECT statement */
       
 59976 ){
       
 59977   Walker w;
       
 59978 
       
 59979   assert( p!=0 );
       
 59980   w.xExprCallback = resolveExprStep;
       
 59981   w.xSelectCallback = resolveSelectStep;
       
 59982   w.pParse = pParse;
       
 59983   w.u.pNC = pOuterNC;
       
 59984   sqlite3WalkSelect(&w, p);
       
 59985 }
       
 59986 
       
 59987 /************** End of resolve.c *********************************************/
       
 59988 /************** Begin file expr.c ********************************************/
       
 59989 /*
       
 59990 ** 2001 September 15
       
 59991 **
       
 59992 ** The author disclaims copyright to this source code.  In place of
       
 59993 ** a legal notice, here is a blessing:
       
 59994 **
       
 59995 **    May you do good and not evil.
       
 59996 **    May you find forgiveness for yourself and forgive others.
       
 59997 **    May you share freely, never taking more than you give.
       
 59998 **
       
 59999 *************************************************************************
       
 60000 ** This file contains routines used for analyzing expressions and
       
 60001 ** for generating VDBE code that evaluates expressions in SQLite.
       
 60002 */
       
 60003 
       
 60004 /*
       
 60005 ** Return the 'affinity' of the expression pExpr if any.
       
 60006 **
       
 60007 ** If pExpr is a column, a reference to a column via an 'AS' alias,
       
 60008 ** or a sub-select with a column as the return value, then the 
       
 60009 ** affinity of that column is returned. Otherwise, 0x00 is returned,
       
 60010 ** indicating no affinity for the expression.
       
 60011 **
       
 60012 ** i.e. the WHERE clause expresssions in the following statements all
       
 60013 ** have an affinity:
       
 60014 **
       
 60015 ** CREATE TABLE t1(a);
       
 60016 ** SELECT * FROM t1 WHERE a;
       
 60017 ** SELECT a AS b FROM t1 WHERE b;
       
 60018 ** SELECT * FROM t1 WHERE (select a from t1);
       
 60019 */
       
 60020 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
       
 60021   int op = pExpr->op;
       
 60022   if( op==TK_SELECT ){
       
 60023     assert( pExpr->flags&EP_xIsSelect );
       
 60024     return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
       
 60025   }
       
 60026 #ifndef SQLITE_OMIT_CAST
       
 60027   if( op==TK_CAST ){
       
 60028     assert( !ExprHasProperty(pExpr, EP_IntValue) );
       
 60029     return sqlite3AffinityType(pExpr->u.zToken);
       
 60030   }
       
 60031 #endif
       
 60032   if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) 
       
 60033    && pExpr->pTab!=0
       
 60034   ){
       
 60035     /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
       
 60036     ** a TK_COLUMN but was previously evaluated and cached in a register */
       
 60037     int j = pExpr->iColumn;
       
 60038     if( j<0 ) return SQLITE_AFF_INTEGER;
       
 60039     assert( pExpr->pTab && j<pExpr->pTab->nCol );
       
 60040     return pExpr->pTab->aCol[j].affinity;
       
 60041   }
       
 60042   return pExpr->affinity;
       
 60043 }
       
 60044 
       
 60045 /*
       
 60046 ** Set the collating sequence for expression pExpr to be the collating
       
 60047 ** sequence named by pToken.   Return a pointer to the revised expression.
       
 60048 ** The collating sequence is marked as "explicit" using the EP_ExpCollate
       
 60049 ** flag.  An explicit collating sequence will override implicit
       
 60050 ** collating sequences.
       
 60051 */
       
 60052 SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pCollName){
       
 60053   char *zColl = 0;            /* Dequoted name of collation sequence */
       
 60054   CollSeq *pColl;
       
 60055   sqlite3 *db = pParse->db;
       
 60056   zColl = sqlite3NameFromToken(db, pCollName);
       
 60057   if( pExpr && zColl ){
       
 60058     pColl = sqlite3LocateCollSeq(pParse, zColl);
       
 60059     if( pColl ){
       
 60060       pExpr->pColl = pColl;
       
 60061       pExpr->flags |= EP_ExpCollate;
       
 60062     }
       
 60063   }
       
 60064   sqlite3DbFree(db, zColl);
       
 60065   return pExpr;
       
 60066 }
       
 60067 
       
 60068 /*
       
 60069 ** Return the default collation sequence for the expression pExpr. If
       
 60070 ** there is no default collation type, return 0.
       
 60071 */
       
 60072 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
       
 60073   CollSeq *pColl = 0;
       
 60074   Expr *p = pExpr;
       
 60075   while( ALWAYS(p) ){
       
 60076     int op;
       
 60077     pColl = p->pColl;
       
 60078     if( pColl ) break;
       
 60079     op = p->op;
       
 60080     if( p->pTab!=0 && (
       
 60081         op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER || op==TK_TRIGGER
       
 60082     )){
       
 60083       /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
       
 60084       ** a TK_COLUMN but was previously evaluated and cached in a register */
       
 60085       const char *zColl;
       
 60086       int j = p->iColumn;
       
 60087       if( j>=0 ){
       
 60088         sqlite3 *db = pParse->db;
       
 60089         zColl = p->pTab->aCol[j].zColl;
       
 60090         pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
       
 60091         pExpr->pColl = pColl;
       
 60092       }
       
 60093       break;
       
 60094     }
       
 60095     if( op!=TK_CAST && op!=TK_UPLUS ){
       
 60096       break;
       
 60097     }
       
 60098     p = p->pLeft;
       
 60099   }
       
 60100   if( sqlite3CheckCollSeq(pParse, pColl) ){ 
       
 60101     pColl = 0;
       
 60102   }
       
 60103   return pColl;
       
 60104 }
       
 60105 
       
 60106 /*
       
 60107 ** pExpr is an operand of a comparison operator.  aff2 is the
       
 60108 ** type affinity of the other operand.  This routine returns the
       
 60109 ** type affinity that should be used for the comparison operator.
       
 60110 */
       
 60111 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
       
 60112   char aff1 = sqlite3ExprAffinity(pExpr);
       
 60113   if( aff1 && aff2 ){
       
 60114     /* Both sides of the comparison are columns. If one has numeric
       
 60115     ** affinity, use that. Otherwise use no affinity.
       
 60116     */
       
 60117     if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
       
 60118       return SQLITE_AFF_NUMERIC;
       
 60119     }else{
       
 60120       return SQLITE_AFF_NONE;
       
 60121     }
       
 60122   }else if( !aff1 && !aff2 ){
       
 60123     /* Neither side of the comparison is a column.  Compare the
       
 60124     ** results directly.
       
 60125     */
       
 60126     return SQLITE_AFF_NONE;
       
 60127   }else{
       
 60128     /* One side is a column, the other is not. Use the columns affinity. */
       
 60129     assert( aff1==0 || aff2==0 );
       
 60130     return (aff1 + aff2);
       
 60131   }
       
 60132 }
       
 60133 
       
 60134 /*
       
 60135 ** pExpr is a comparison operator.  Return the type affinity that should
       
 60136 ** be applied to both operands prior to doing the comparison.
       
 60137 */
       
 60138 static char comparisonAffinity(Expr *pExpr){
       
 60139   char aff;
       
 60140   assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
       
 60141           pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
       
 60142           pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
       
 60143   assert( pExpr->pLeft );
       
 60144   aff = sqlite3ExprAffinity(pExpr->pLeft);
       
 60145   if( pExpr->pRight ){
       
 60146     aff = sqlite3CompareAffinity(pExpr->pRight, aff);
       
 60147   }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
       
 60148     aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
       
 60149   }else if( !aff ){
       
 60150     aff = SQLITE_AFF_NONE;
       
 60151   }
       
 60152   return aff;
       
 60153 }
       
 60154 
       
 60155 /*
       
 60156 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
       
 60157 ** idx_affinity is the affinity of an indexed column. Return true
       
 60158 ** if the index with affinity idx_affinity may be used to implement
       
 60159 ** the comparison in pExpr.
       
 60160 */
       
 60161 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
       
 60162   char aff = comparisonAffinity(pExpr);
       
 60163   switch( aff ){
       
 60164     case SQLITE_AFF_NONE:
       
 60165       return 1;
       
 60166     case SQLITE_AFF_TEXT:
       
 60167       return idx_affinity==SQLITE_AFF_TEXT;
       
 60168     default:
       
 60169       return sqlite3IsNumericAffinity(idx_affinity);
       
 60170   }
       
 60171 }
       
 60172 
       
 60173 /*
       
 60174 ** Return the P5 value that should be used for a binary comparison
       
 60175 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
       
 60176 */
       
 60177 static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
       
 60178   u8 aff = (char)sqlite3ExprAffinity(pExpr2);
       
 60179   aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
       
 60180   return aff;
       
 60181 }
       
 60182 
       
 60183 /*
       
 60184 ** Return a pointer to the collation sequence that should be used by
       
 60185 ** a binary comparison operator comparing pLeft and pRight.
       
 60186 **
       
 60187 ** If the left hand expression has a collating sequence type, then it is
       
 60188 ** used. Otherwise the collation sequence for the right hand expression
       
 60189 ** is used, or the default (BINARY) if neither expression has a collating
       
 60190 ** type.
       
 60191 **
       
 60192 ** Argument pRight (but not pLeft) may be a null pointer. In this case,
       
 60193 ** it is not considered.
       
 60194 */
       
 60195 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
       
 60196   Parse *pParse, 
       
 60197   Expr *pLeft, 
       
 60198   Expr *pRight
       
 60199 ){
       
 60200   CollSeq *pColl;
       
 60201   assert( pLeft );
       
 60202   if( pLeft->flags & EP_ExpCollate ){
       
 60203     assert( pLeft->pColl );
       
 60204     pColl = pLeft->pColl;
       
 60205   }else if( pRight && pRight->flags & EP_ExpCollate ){
       
 60206     assert( pRight->pColl );
       
 60207     pColl = pRight->pColl;
       
 60208   }else{
       
 60209     pColl = sqlite3ExprCollSeq(pParse, pLeft);
       
 60210     if( !pColl ){
       
 60211       pColl = sqlite3ExprCollSeq(pParse, pRight);
       
 60212     }
       
 60213   }
       
 60214   return pColl;
       
 60215 }
       
 60216 
       
 60217 /*
       
 60218 ** Generate the operands for a comparison operation.  Before
       
 60219 ** generating the code for each operand, set the EP_AnyAff
       
 60220 ** flag on the expression so that it will be able to used a
       
 60221 ** cached column value that has previously undergone an
       
 60222 ** affinity change.
       
 60223 */
       
 60224 static void codeCompareOperands(
       
 60225   Parse *pParse,    /* Parsing and code generating context */
       
 60226   Expr *pLeft,      /* The left operand */
       
 60227   int *pRegLeft,    /* Register where left operand is stored */
       
 60228   int *pFreeLeft,   /* Free this register when done */
       
 60229   Expr *pRight,     /* The right operand */
       
 60230   int *pRegRight,   /* Register where right operand is stored */
       
 60231   int *pFreeRight   /* Write temp register for right operand there */
       
 60232 ){
       
 60233   while( pLeft->op==TK_UPLUS ) pLeft = pLeft->pLeft;
       
 60234   pLeft->flags |= EP_AnyAff;
       
 60235   *pRegLeft = sqlite3ExprCodeTemp(pParse, pLeft, pFreeLeft);
       
 60236   while( pRight->op==TK_UPLUS ) pRight = pRight->pLeft;
       
 60237   pRight->flags |= EP_AnyAff;
       
 60238   *pRegRight = sqlite3ExprCodeTemp(pParse, pRight, pFreeRight);
       
 60239 }
       
 60240 
       
 60241 /*
       
 60242 ** Generate code for a comparison operator.
       
 60243 */
       
 60244 static int codeCompare(
       
 60245   Parse *pParse,    /* The parsing (and code generating) context */
       
 60246   Expr *pLeft,      /* The left operand */
       
 60247   Expr *pRight,     /* The right operand */
       
 60248   int opcode,       /* The comparison opcode */
       
 60249   int in1, int in2, /* Register holding operands */
       
 60250   int dest,         /* Jump here if true.  */
       
 60251   int jumpIfNull    /* If true, jump if either operand is NULL */
       
 60252 ){
       
 60253   int p5;
       
 60254   int addr;
       
 60255   CollSeq *p4;
       
 60256 
       
 60257   p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
       
 60258   p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
       
 60259   addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
       
 60260                            (void*)p4, P4_COLLSEQ);
       
 60261   sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
       
 60262   if( (p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_NONE ){
       
 60263     sqlite3ExprCacheAffinityChange(pParse, in1, 1);
       
 60264     sqlite3ExprCacheAffinityChange(pParse, in2, 1);
       
 60265   }
       
 60266   return addr;
       
 60267 }
       
 60268 
       
 60269 #if SQLITE_MAX_EXPR_DEPTH>0
       
 60270 /*
       
 60271 ** Check that argument nHeight is less than or equal to the maximum
       
 60272 ** expression depth allowed. If it is not, leave an error message in
       
 60273 ** pParse.
       
 60274 */
       
 60275 SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
       
 60276   int rc = SQLITE_OK;
       
 60277   int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
       
 60278   if( nHeight>mxHeight ){
       
 60279     sqlite3ErrorMsg(pParse, 
       
 60280        "Expression tree is too large (maximum depth %d)", mxHeight
       
 60281     );
       
 60282     rc = SQLITE_ERROR;
       
 60283   }
       
 60284   return rc;
       
 60285 }
       
 60286 
       
 60287 /* The following three functions, heightOfExpr(), heightOfExprList()
       
 60288 ** and heightOfSelect(), are used to determine the maximum height
       
 60289 ** of any expression tree referenced by the structure passed as the
       
 60290 ** first argument.
       
 60291 **
       
 60292 ** If this maximum height is greater than the current value pointed
       
 60293 ** to by pnHeight, the second parameter, then set *pnHeight to that
       
 60294 ** value.
       
 60295 */
       
 60296 static void heightOfExpr(Expr *p, int *pnHeight){
       
 60297   if( p ){
       
 60298     if( p->nHeight>*pnHeight ){
       
 60299       *pnHeight = p->nHeight;
       
 60300     }
       
 60301   }
       
 60302 }
       
 60303 static void heightOfExprList(ExprList *p, int *pnHeight){
       
 60304   if( p ){
       
 60305     int i;
       
 60306     for(i=0; i<p->nExpr; i++){
       
 60307       heightOfExpr(p->a[i].pExpr, pnHeight);
       
 60308     }
       
 60309   }
       
 60310 }
       
 60311 static void heightOfSelect(Select *p, int *pnHeight){
       
 60312   if( p ){
       
 60313     heightOfExpr(p->pWhere, pnHeight);
       
 60314     heightOfExpr(p->pHaving, pnHeight);
       
 60315     heightOfExpr(p->pLimit, pnHeight);
       
 60316     heightOfExpr(p->pOffset, pnHeight);
       
 60317     heightOfExprList(p->pEList, pnHeight);
       
 60318     heightOfExprList(p->pGroupBy, pnHeight);
       
 60319     heightOfExprList(p->pOrderBy, pnHeight);
       
 60320     heightOfSelect(p->pPrior, pnHeight);
       
 60321   }
       
 60322 }
       
 60323 
       
 60324 /*
       
 60325 ** Set the Expr.nHeight variable in the structure passed as an 
       
 60326 ** argument. An expression with no children, Expr.pList or 
       
 60327 ** Expr.pSelect member has a height of 1. Any other expression
       
 60328 ** has a height equal to the maximum height of any other 
       
 60329 ** referenced Expr plus one.
       
 60330 */
       
 60331 static void exprSetHeight(Expr *p){
       
 60332   int nHeight = 0;
       
 60333   heightOfExpr(p->pLeft, &nHeight);
       
 60334   heightOfExpr(p->pRight, &nHeight);
       
 60335   if( ExprHasProperty(p, EP_xIsSelect) ){
       
 60336     heightOfSelect(p->x.pSelect, &nHeight);
       
 60337   }else{
       
 60338     heightOfExprList(p->x.pList, &nHeight);
       
 60339   }
       
 60340   p->nHeight = nHeight + 1;
       
 60341 }
       
 60342 
       
 60343 /*
       
 60344 ** Set the Expr.nHeight variable using the exprSetHeight() function. If
       
 60345 ** the height is greater than the maximum allowed expression depth,
       
 60346 ** leave an error in pParse.
       
 60347 */
       
 60348 SQLITE_PRIVATE void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
       
 60349   exprSetHeight(p);
       
 60350   sqlite3ExprCheckHeight(pParse, p->nHeight);
       
 60351 }
       
 60352 
       
 60353 /*
       
 60354 ** Return the maximum height of any expression tree referenced
       
 60355 ** by the select statement passed as an argument.
       
 60356 */
       
 60357 SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
       
 60358   int nHeight = 0;
       
 60359   heightOfSelect(p, &nHeight);
       
 60360   return nHeight;
       
 60361 }
       
 60362 #else
       
 60363   #define exprSetHeight(y)
       
 60364 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */
       
 60365 
       
 60366 /*
       
 60367 ** This routine is the core allocator for Expr nodes.
       
 60368 **
       
 60369 ** Construct a new expression node and return a pointer to it.  Memory
       
 60370 ** for this node and for the pToken argument is a single allocation
       
 60371 ** obtained from sqlite3DbMalloc().  The calling function
       
 60372 ** is responsible for making sure the node eventually gets freed.
       
 60373 **
       
 60374 ** If dequote is true, then the token (if it exists) is dequoted.
       
 60375 ** If dequote is false, no dequoting is performance.  The deQuote
       
 60376 ** parameter is ignored if pToken is NULL or if the token does not
       
 60377 ** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
       
 60378 ** then the EP_DblQuoted flag is set on the expression node.
       
 60379 **
       
 60380 ** Special case:  If op==TK_INTEGER and pToken points to a string that
       
 60381 ** can be translated into a 32-bit integer, then the token is not
       
 60382 ** stored in u.zToken.  Instead, the integer values is written
       
 60383 ** into u.iValue and the EP_IntValue flag is set.  No extra storage
       
 60384 ** is allocated to hold the integer text and the dequote flag is ignored.
       
 60385 */
       
 60386 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
       
 60387   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
       
 60388   int op,                 /* Expression opcode */
       
 60389   const Token *pToken,    /* Token argument.  Might be NULL */
       
 60390   int dequote             /* True to dequote */
       
 60391 ){
       
 60392   Expr *pNew;
       
 60393   int nExtra = 0;
       
 60394   int iValue = 0;
       
 60395 
       
 60396   if( pToken ){
       
 60397     if( op!=TK_INTEGER || pToken->z==0
       
 60398           || sqlite3GetInt32(pToken->z, &iValue)==0 ){
       
 60399       nExtra = pToken->n+1;
       
 60400     }
       
 60401   }
       
 60402   pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
       
 60403   if( pNew ){
       
 60404     pNew->op = (u8)op;
       
 60405     pNew->iAgg = -1;
       
 60406     if( pToken ){
       
 60407       if( nExtra==0 ){
       
 60408         pNew->flags |= EP_IntValue;
       
 60409         pNew->u.iValue = iValue;
       
 60410       }else{
       
 60411         int c;
       
 60412         pNew->u.zToken = (char*)&pNew[1];
       
 60413         memcpy(pNew->u.zToken, pToken->z, pToken->n);
       
 60414         pNew->u.zToken[pToken->n] = 0;
       
 60415         if( dequote && nExtra>=3 
       
 60416              && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
       
 60417           sqlite3Dequote(pNew->u.zToken);
       
 60418           if( c=='"' ) pNew->flags |= EP_DblQuoted;
       
 60419         }
       
 60420       }
       
 60421     }
       
 60422 #if SQLITE_MAX_EXPR_DEPTH>0
       
 60423     pNew->nHeight = 1;
       
 60424 #endif  
       
 60425   }
       
 60426   return pNew;
       
 60427 }
       
 60428 
       
 60429 /*
       
 60430 ** Allocate a new expression node from a zero-terminated token that has
       
 60431 ** already been dequoted.
       
 60432 */
       
 60433 SQLITE_PRIVATE Expr *sqlite3Expr(
       
 60434   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
       
 60435   int op,                 /* Expression opcode */
       
 60436   const char *zToken      /* Token argument.  Might be NULL */
       
 60437 ){
       
 60438   Token x;
       
 60439   x.z = zToken;
       
 60440   x.n = zToken ? sqlite3Strlen30(zToken) : 0;
       
 60441   return sqlite3ExprAlloc(db, op, &x, 0);
       
 60442 }
       
 60443 
       
 60444 /*
       
 60445 ** Attach subtrees pLeft and pRight to the Expr node pRoot.
       
 60446 **
       
 60447 ** If pRoot==NULL that means that a memory allocation error has occurred.
       
 60448 ** In that case, delete the subtrees pLeft and pRight.
       
 60449 */
       
 60450 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
       
 60451   sqlite3 *db,
       
 60452   Expr *pRoot,
       
 60453   Expr *pLeft,
       
 60454   Expr *pRight
       
 60455 ){
       
 60456   if( pRoot==0 ){
       
 60457     assert( db->mallocFailed );
       
 60458     sqlite3ExprDelete(db, pLeft);
       
 60459     sqlite3ExprDelete(db, pRight);
       
 60460   }else{
       
 60461     if( pRight ){
       
 60462       pRoot->pRight = pRight;
       
 60463       if( pRight->flags & EP_ExpCollate ){
       
 60464         pRoot->flags |= EP_ExpCollate;
       
 60465         pRoot->pColl = pRight->pColl;
       
 60466       }
       
 60467     }
       
 60468     if( pLeft ){
       
 60469       pRoot->pLeft = pLeft;
       
 60470       if( pLeft->flags & EP_ExpCollate ){
       
 60471         pRoot->flags |= EP_ExpCollate;
       
 60472         pRoot->pColl = pLeft->pColl;
       
 60473       }
       
 60474     }
       
 60475     exprSetHeight(pRoot);
       
 60476   }
       
 60477 }
       
 60478 
       
 60479 /*
       
 60480 ** Allocate a Expr node which joins as many as two subtrees.
       
 60481 **
       
 60482 ** One or both of the subtrees can be NULL.  Return a pointer to the new
       
 60483 ** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
       
 60484 ** free the subtrees and return NULL.
       
 60485 */
       
 60486 SQLITE_PRIVATE Expr *sqlite3PExpr(
       
 60487   Parse *pParse,          /* Parsing context */
       
 60488   int op,                 /* Expression opcode */
       
 60489   Expr *pLeft,            /* Left operand */
       
 60490   Expr *pRight,           /* Right operand */
       
 60491   const Token *pToken     /* Argument token */
       
 60492 ){
       
 60493   Expr *p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
       
 60494   sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
       
 60495   return p;
       
 60496 }
       
 60497 
       
 60498 /*
       
 60499 ** Join two expressions using an AND operator.  If either expression is
       
 60500 ** NULL, then just return the other expression.
       
 60501 */
       
 60502 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
       
 60503   if( pLeft==0 ){
       
 60504     return pRight;
       
 60505   }else if( pRight==0 ){
       
 60506     return pLeft;
       
 60507   }else{
       
 60508     Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
       
 60509     sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
       
 60510     return pNew;
       
 60511   }
       
 60512 }
       
 60513 
       
 60514 /*
       
 60515 ** Construct a new expression node for a function with multiple
       
 60516 ** arguments.
       
 60517 */
       
 60518 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
       
 60519   Expr *pNew;
       
 60520   sqlite3 *db = pParse->db;
       
 60521   assert( pToken );
       
 60522   pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
       
 60523   if( pNew==0 ){
       
 60524     sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
       
 60525     return 0;
       
 60526   }
       
 60527   pNew->x.pList = pList;
       
 60528   assert( !ExprHasProperty(pNew, EP_xIsSelect) );
       
 60529   sqlite3ExprSetHeight(pParse, pNew);
       
 60530   return pNew;
       
 60531 }
       
 60532 
       
 60533 /*
       
 60534 ** Assign a variable number to an expression that encodes a wildcard
       
 60535 ** in the original SQL statement.  
       
 60536 **
       
 60537 ** Wildcards consisting of a single "?" are assigned the next sequential
       
 60538 ** variable number.
       
 60539 **
       
 60540 ** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
       
 60541 ** sure "nnn" is not too be to avoid a denial of service attack when
       
 60542 ** the SQL statement comes from an external source.
       
 60543 **
       
 60544 ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
       
 60545 ** as the previous instance of the same wildcard.  Or if this is the first
       
 60546 ** instance of the wildcard, the next sequenial variable number is
       
 60547 ** assigned.
       
 60548 */
       
 60549 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
       
 60550   sqlite3 *db = pParse->db;
       
 60551   const char *z;
       
 60552 
       
 60553   if( pExpr==0 ) return;
       
 60554   assert( !ExprHasAnyProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
       
 60555   z = pExpr->u.zToken;
       
 60556   assert( z!=0 );
       
 60557   assert( z[0]!=0 );
       
 60558   if( z[1]==0 ){
       
 60559     /* Wildcard of the form "?".  Assign the next variable number */
       
 60560     assert( z[0]=='?' );
       
 60561     pExpr->iTable = ++pParse->nVar;
       
 60562   }else if( z[0]=='?' ){
       
 60563     /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
       
 60564     ** use it as the variable number */
       
 60565     int i;
       
 60566     pExpr->iTable = i = atoi((char*)&z[1]);
       
 60567     testcase( i==0 );
       
 60568     testcase( i==1 );
       
 60569     testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
       
 60570     testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
       
 60571     if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
       
 60572       sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
       
 60573           db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
       
 60574     }
       
 60575     if( i>pParse->nVar ){
       
 60576       pParse->nVar = i;
       
 60577     }
       
 60578   }else{
       
 60579     /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
       
 60580     ** number as the prior appearance of the same name, or if the name
       
 60581     ** has never appeared before, reuse the same variable number
       
 60582     */
       
 60583     int i;
       
 60584     u32 n;
       
 60585     n = sqlite3Strlen30(z);
       
 60586     for(i=0; i<pParse->nVarExpr; i++){
       
 60587       Expr *pE = pParse->apVarExpr[i];
       
 60588       assert( pE!=0 );
       
 60589       if( memcmp(pE->u.zToken, z, n)==0 && pE->u.zToken[n]==0 ){
       
 60590         pExpr->iTable = pE->iTable;
       
 60591         break;
       
 60592       }
       
 60593     }
       
 60594     if( i>=pParse->nVarExpr ){
       
 60595       pExpr->iTable = ++pParse->nVar;
       
 60596       if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
       
 60597         pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
       
 60598         pParse->apVarExpr =
       
 60599             sqlite3DbReallocOrFree(
       
 60600               db,
       
 60601               pParse->apVarExpr,
       
 60602               pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
       
 60603             );
       
 60604       }
       
 60605       if( !db->mallocFailed ){
       
 60606         assert( pParse->apVarExpr!=0 );
       
 60607         pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
       
 60608       }
       
 60609     }
       
 60610   } 
       
 60611   if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
       
 60612     sqlite3ErrorMsg(pParse, "too many SQL variables");
       
 60613   }
       
 60614 }
       
 60615 
       
 60616 /*
       
 60617 ** Clear an expression structure without deleting the structure itself.
       
 60618 ** Substructure is deleted.
       
 60619 */
       
 60620 SQLITE_PRIVATE void sqlite3ExprClear(sqlite3 *db, Expr *p){
       
 60621   assert( p!=0 );
       
 60622   if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
       
 60623     sqlite3ExprDelete(db, p->pLeft);
       
 60624     sqlite3ExprDelete(db, p->pRight);
       
 60625     if( !ExprHasProperty(p, EP_Reduced) && (p->flags2 & EP2_MallocedToken)!=0 ){
       
 60626       sqlite3DbFree(db, p->u.zToken);
       
 60627     }
       
 60628     if( ExprHasProperty(p, EP_xIsSelect) ){
       
 60629       sqlite3SelectDelete(db, p->x.pSelect);
       
 60630     }else{
       
 60631       sqlite3ExprListDelete(db, p->x.pList);
       
 60632     }
       
 60633   }
       
 60634 }
       
 60635 
       
 60636 /*
       
 60637 ** Recursively delete an expression tree.
       
 60638 */
       
 60639 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
       
 60640   if( p==0 ) return;
       
 60641   sqlite3ExprClear(db, p);
       
 60642   if( !ExprHasProperty(p, EP_Static) ){
       
 60643     sqlite3DbFree(db, p);
       
 60644   }
       
 60645 }
       
 60646 
       
 60647 /*
       
 60648 ** Return the number of bytes allocated for the expression structure 
       
 60649 ** passed as the first argument. This is always one of EXPR_FULLSIZE,
       
 60650 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
       
 60651 */
       
 60652 static int exprStructSize(Expr *p){
       
 60653   if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
       
 60654   if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
       
 60655   return EXPR_FULLSIZE;
       
 60656 }
       
 60657 
       
 60658 /*
       
 60659 ** The dupedExpr*Size() routines each return the number of bytes required
       
 60660 ** to store a copy of an expression or expression tree.  They differ in
       
 60661 ** how much of the tree is measured.
       
 60662 **
       
 60663 **     dupedExprStructSize()     Size of only the Expr structure 
       
 60664 **     dupedExprNodeSize()       Size of Expr + space for token
       
 60665 **     dupedExprSize()           Expr + token + subtree components
       
 60666 **
       
 60667 ***************************************************************************
       
 60668 **
       
 60669 ** The dupedExprStructSize() function returns two values OR-ed together:  
       
 60670 ** (1) the space required for a copy of the Expr structure only and 
       
 60671 ** (2) the EP_xxx flags that indicate what the structure size should be.
       
 60672 ** The return values is always one of:
       
 60673 **
       
 60674 **      EXPR_FULLSIZE
       
 60675 **      EXPR_REDUCEDSIZE   | EP_Reduced
       
 60676 **      EXPR_TOKENONLYSIZE | EP_TokenOnly
       
 60677 **
       
 60678 ** The size of the structure can be found by masking the return value
       
 60679 ** of this routine with 0xfff.  The flags can be found by masking the
       
 60680 ** return value with EP_Reduced|EP_TokenOnly.
       
 60681 **
       
 60682 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
       
 60683 ** (unreduced) Expr objects as they or originally constructed by the parser.
       
 60684 ** During expression analysis, extra information is computed and moved into
       
 60685 ** later parts of teh Expr object and that extra information might get chopped
       
 60686 ** off if the expression is reduced.  Note also that it does not work to
       
 60687 ** make a EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
       
 60688 ** to reduce a pristine expression tree from the parser.  The implementation
       
 60689 ** of dupedExprStructSize() contain multiple assert() statements that attempt
       
 60690 ** to enforce this constraint.
       
 60691 */
       
 60692 static int dupedExprStructSize(Expr *p, int flags){
       
 60693   int nSize;
       
 60694   assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
       
 60695   if( 0==(flags&EXPRDUP_REDUCE) ){
       
 60696     nSize = EXPR_FULLSIZE;
       
 60697   }else{
       
 60698     assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
       
 60699     assert( !ExprHasProperty(p, EP_FromJoin) ); 
       
 60700     assert( (p->flags2 & EP2_MallocedToken)==0 );
       
 60701     assert( (p->flags2 & EP2_Irreducible)==0 );
       
 60702     if( p->pLeft || p->pRight || p->pColl || p->x.pList ){
       
 60703       nSize = EXPR_REDUCEDSIZE | EP_Reduced;
       
 60704     }else{
       
 60705       nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
       
 60706     }
       
 60707   }
       
 60708   return nSize;
       
 60709 }
       
 60710 
       
 60711 /*
       
 60712 ** This function returns the space in bytes required to store the copy 
       
 60713 ** of the Expr structure and a copy of the Expr.u.zToken string (if that
       
 60714 ** string is defined.)
       
 60715 */
       
 60716 static int dupedExprNodeSize(Expr *p, int flags){
       
 60717   int nByte = dupedExprStructSize(p, flags) & 0xfff;
       
 60718   if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
       
 60719     nByte += sqlite3Strlen30(p->u.zToken)+1;
       
 60720   }
       
 60721   return ROUND8(nByte);
       
 60722 }
       
 60723 
       
 60724 /*
       
 60725 ** Return the number of bytes required to create a duplicate of the 
       
 60726 ** expression passed as the first argument. The second argument is a
       
 60727 ** mask containing EXPRDUP_XXX flags.
       
 60728 **
       
 60729 ** The value returned includes space to create a copy of the Expr struct
       
 60730 ** itself and the buffer referred to by Expr.u.zToken, if any.
       
 60731 **
       
 60732 ** If the EXPRDUP_REDUCE flag is set, then the return value includes 
       
 60733 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft 
       
 60734 ** and Expr.pRight variables (but not for any structures pointed to or 
       
 60735 ** descended from the Expr.x.pList or Expr.x.pSelect variables).
       
 60736 */
       
 60737 static int dupedExprSize(Expr *p, int flags){
       
 60738   int nByte = 0;
       
 60739   if( p ){
       
 60740     nByte = dupedExprNodeSize(p, flags);
       
 60741     if( flags&EXPRDUP_REDUCE ){
       
 60742       nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
       
 60743     }
       
 60744   }
       
 60745   return nByte;
       
 60746 }
       
 60747 
       
 60748 /*
       
 60749 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 
       
 60750 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 
       
 60751 ** to store the copy of expression p, the copies of p->u.zToken
       
 60752 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
       
 60753 ** if any. Before returning, *pzBuffer is set to the first byte passed the
       
 60754 ** portion of the buffer copied into by this function.
       
 60755 */
       
 60756 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
       
 60757   Expr *pNew = 0;                      /* Value to return */
       
 60758   if( p ){
       
 60759     const int isReduced = (flags&EXPRDUP_REDUCE);
       
 60760     u8 *zAlloc;
       
 60761     u32 staticFlag = 0;
       
 60762 
       
 60763     assert( pzBuffer==0 || isReduced );
       
 60764 
       
 60765     /* Figure out where to write the new Expr structure. */
       
 60766     if( pzBuffer ){
       
 60767       zAlloc = *pzBuffer;
       
 60768       staticFlag = EP_Static;
       
 60769     }else{
       
 60770       zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
       
 60771     }
       
 60772     pNew = (Expr *)zAlloc;
       
 60773 
       
 60774     if( pNew ){
       
 60775       /* Set nNewSize to the size allocated for the structure pointed to
       
 60776       ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
       
 60777       ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
       
 60778       ** by the copy of the p->u.zToken string (if any).
       
 60779       */
       
 60780       const unsigned nStructSize = dupedExprStructSize(p, flags);
       
 60781       const int nNewSize = nStructSize & 0xfff;
       
 60782       int nToken;
       
 60783       if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
       
 60784         nToken = sqlite3Strlen30(p->u.zToken) + 1;
       
 60785       }else{
       
 60786         nToken = 0;
       
 60787       }
       
 60788       if( isReduced ){
       
 60789         assert( ExprHasProperty(p, EP_Reduced)==0 );
       
 60790         memcpy(zAlloc, p, nNewSize);
       
 60791       }else{
       
 60792         int nSize = exprStructSize(p);
       
 60793         memcpy(zAlloc, p, nSize);
       
 60794         memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
       
 60795       }
       
 60796 
       
 60797       /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
       
 60798       pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
       
 60799       pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
       
 60800       pNew->flags |= staticFlag;
       
 60801 
       
 60802       /* Copy the p->u.zToken string, if any. */
       
 60803       if( nToken ){
       
 60804         char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
       
 60805         memcpy(zToken, p->u.zToken, nToken);
       
 60806       }
       
 60807 
       
 60808       if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
       
 60809         /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
       
 60810         if( ExprHasProperty(p, EP_xIsSelect) ){
       
 60811           pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
       
 60812         }else{
       
 60813           pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
       
 60814         }
       
 60815       }
       
 60816 
       
 60817       /* Fill in pNew->pLeft and pNew->pRight. */
       
 60818       if( ExprHasAnyProperty(pNew, EP_Reduced|EP_TokenOnly) ){
       
 60819         zAlloc += dupedExprNodeSize(p, flags);
       
 60820         if( ExprHasProperty(pNew, EP_Reduced) ){
       
 60821           pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
       
 60822           pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
       
 60823         }
       
 60824         if( pzBuffer ){
       
 60825           *pzBuffer = zAlloc;
       
 60826         }
       
 60827       }else{
       
 60828         pNew->flags2 = 0;
       
 60829         if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
       
 60830           pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
       
 60831           pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
       
 60832         }
       
 60833       }
       
 60834 
       
 60835     }
       
 60836   }
       
 60837   return pNew;
       
 60838 }
       
 60839 
       
 60840 /*
       
 60841 ** The following group of routines make deep copies of expressions,
       
 60842 ** expression lists, ID lists, and select statements.  The copies can
       
 60843 ** be deleted (by being passed to their respective ...Delete() routines)
       
 60844 ** without effecting the originals.
       
 60845 **
       
 60846 ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
       
 60847 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 
       
 60848 ** by subsequent calls to sqlite*ListAppend() routines.
       
 60849 **
       
 60850 ** Any tables that the SrcList might point to are not duplicated.
       
 60851 **
       
 60852 ** The flags parameter contains a combination of the EXPRDUP_XXX flags.
       
 60853 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
       
 60854 ** truncated version of the usual Expr structure that will be stored as
       
 60855 ** part of the in-memory representation of the database schema.
       
 60856 */
       
 60857 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
       
 60858   return exprDup(db, p, flags, 0);
       
 60859 }
       
 60860 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
       
 60861   ExprList *pNew;
       
 60862   struct ExprList_item *pItem, *pOldItem;
       
 60863   int i;
       
 60864   if( p==0 ) return 0;
       
 60865   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
       
 60866   if( pNew==0 ) return 0;
       
 60867   pNew->iECursor = 0;
       
 60868   pNew->nExpr = pNew->nAlloc = p->nExpr;
       
 60869   pNew->a = pItem = sqlite3DbMallocRaw(db,  p->nExpr*sizeof(p->a[0]) );
       
 60870   if( pItem==0 ){
       
 60871     sqlite3DbFree(db, pNew);
       
 60872     return 0;
       
 60873   } 
       
 60874   pOldItem = p->a;
       
 60875   for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
       
 60876     Expr *pOldExpr = pOldItem->pExpr;
       
 60877     pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
       
 60878     pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
       
 60879     pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
       
 60880     pItem->sortOrder = pOldItem->sortOrder;
       
 60881     pItem->done = 0;
       
 60882     pItem->iCol = pOldItem->iCol;
       
 60883     pItem->iAlias = pOldItem->iAlias;
       
 60884   }
       
 60885   return pNew;
       
 60886 }
       
 60887 
       
 60888 /*
       
 60889 ** If cursors, triggers, views and subqueries are all omitted from
       
 60890 ** the build, then none of the following routines, except for 
       
 60891 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
       
 60892 ** called with a NULL argument.
       
 60893 */
       
 60894 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
       
 60895  || !defined(SQLITE_OMIT_SUBQUERY)
       
 60896 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
       
 60897   SrcList *pNew;
       
 60898   int i;
       
 60899   int nByte;
       
 60900   if( p==0 ) return 0;
       
 60901   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
       
 60902   pNew = sqlite3DbMallocRaw(db, nByte );
       
 60903   if( pNew==0 ) return 0;
       
 60904   pNew->nSrc = pNew->nAlloc = p->nSrc;
       
 60905   for(i=0; i<p->nSrc; i++){
       
 60906     struct SrcList_item *pNewItem = &pNew->a[i];
       
 60907     struct SrcList_item *pOldItem = &p->a[i];
       
 60908     Table *pTab;
       
 60909     pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
       
 60910     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
       
 60911     pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
       
 60912     pNewItem->jointype = pOldItem->jointype;
       
 60913     pNewItem->iCursor = pOldItem->iCursor;
       
 60914     pNewItem->isPopulated = pOldItem->isPopulated;
       
 60915     pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
       
 60916     pNewItem->notIndexed = pOldItem->notIndexed;
       
 60917     pNewItem->pIndex = pOldItem->pIndex;
       
 60918     pTab = pNewItem->pTab = pOldItem->pTab;
       
 60919     if( pTab ){
       
 60920       pTab->nRef++;
       
 60921     }
       
 60922     pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
       
 60923     pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
       
 60924     pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
       
 60925     pNewItem->colUsed = pOldItem->colUsed;
       
 60926   }
       
 60927   return pNew;
       
 60928 }
       
 60929 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
       
 60930   IdList *pNew;
       
 60931   int i;
       
 60932   if( p==0 ) return 0;
       
 60933   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
       
 60934   if( pNew==0 ) return 0;
       
 60935   pNew->nId = pNew->nAlloc = p->nId;
       
 60936   pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
       
 60937   if( pNew->a==0 ){
       
 60938     sqlite3DbFree(db, pNew);
       
 60939     return 0;
       
 60940   }
       
 60941   for(i=0; i<p->nId; i++){
       
 60942     struct IdList_item *pNewItem = &pNew->a[i];
       
 60943     struct IdList_item *pOldItem = &p->a[i];
       
 60944     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
       
 60945     pNewItem->idx = pOldItem->idx;
       
 60946   }
       
 60947   return pNew;
       
 60948 }
       
 60949 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
       
 60950   Select *pNew;
       
 60951   if( p==0 ) return 0;
       
 60952   pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
       
 60953   if( pNew==0 ) return 0;
       
 60954   pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
       
 60955   pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
       
 60956   pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
       
 60957   pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
       
 60958   pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
       
 60959   pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
       
 60960   pNew->op = p->op;
       
 60961   pNew->pPrior = sqlite3SelectDup(db, p->pPrior, flags);
       
 60962   pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
       
 60963   pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
       
 60964   pNew->iLimit = 0;
       
 60965   pNew->iOffset = 0;
       
 60966   pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
       
 60967   pNew->pRightmost = 0;
       
 60968   pNew->addrOpenEphm[0] = -1;
       
 60969   pNew->addrOpenEphm[1] = -1;
       
 60970   pNew->addrOpenEphm[2] = -1;
       
 60971   return pNew;
       
 60972 }
       
 60973 #else
       
 60974 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
       
 60975   assert( p==0 );
       
 60976   return 0;
       
 60977 }
       
 60978 #endif
       
 60979 
       
 60980 
       
 60981 /*
       
 60982 ** Add a new element to the end of an expression list.  If pList is
       
 60983 ** initially NULL, then create a new expression list.
       
 60984 **
       
 60985 ** If a memory allocation error occurs, the entire list is freed and
       
 60986 ** NULL is returned.  If non-NULL is returned, then it is guaranteed
       
 60987 ** that the new entry was successfully appended.
       
 60988 */
       
 60989 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
       
 60990   Parse *pParse,          /* Parsing context */
       
 60991   ExprList *pList,        /* List to which to append. Might be NULL */
       
 60992   Expr *pExpr             /* Expression to be appended. Might be NULL */
       
 60993 ){
       
 60994   sqlite3 *db = pParse->db;
       
 60995   if( pList==0 ){
       
 60996     pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
       
 60997     if( pList==0 ){
       
 60998       goto no_mem;
       
 60999     }
       
 61000     assert( pList->nAlloc==0 );
       
 61001   }
       
 61002   if( pList->nAlloc<=pList->nExpr ){
       
 61003     struct ExprList_item *a;
       
 61004     int n = pList->nAlloc*2 + 4;
       
 61005     a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
       
 61006     if( a==0 ){
       
 61007       goto no_mem;
       
 61008     }
       
 61009     pList->a = a;
       
 61010     pList->nAlloc = sqlite3DbMallocSize(db, a)/sizeof(a[0]);
       
 61011   }
       
 61012   assert( pList->a!=0 );
       
 61013   if( 1 ){
       
 61014     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
       
 61015     memset(pItem, 0, sizeof(*pItem));
       
 61016     pItem->pExpr = pExpr;
       
 61017   }
       
 61018   return pList;
       
 61019 
       
 61020 no_mem:     
       
 61021   /* Avoid leaking memory if malloc has failed. */
       
 61022   sqlite3ExprDelete(db, pExpr);
       
 61023   sqlite3ExprListDelete(db, pList);
       
 61024   return 0;
       
 61025 }
       
 61026 
       
 61027 /*
       
 61028 ** Set the ExprList.a[].zName element of the most recently added item
       
 61029 ** on the expression list.
       
 61030 **
       
 61031 ** pList might be NULL following an OOM error.  But pName should never be
       
 61032 ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
       
 61033 ** is set.
       
 61034 */
       
 61035 SQLITE_PRIVATE void sqlite3ExprListSetName(
       
 61036   Parse *pParse,          /* Parsing context */
       
 61037   ExprList *pList,        /* List to which to add the span. */
       
 61038   Token *pName,           /* Name to be added */
       
 61039   int dequote             /* True to cause the name to be dequoted */
       
 61040 ){
       
 61041   assert( pList!=0 || pParse->db->mallocFailed!=0 );
       
 61042   if( pList ){
       
 61043     struct ExprList_item *pItem;
       
 61044     assert( pList->nExpr>0 );
       
 61045     pItem = &pList->a[pList->nExpr-1];
       
 61046     assert( pItem->zName==0 );
       
 61047     pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
       
 61048     if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
       
 61049   }
       
 61050 }
       
 61051 
       
 61052 /*
       
 61053 ** Set the ExprList.a[].zSpan element of the most recently added item
       
 61054 ** on the expression list.
       
 61055 **
       
 61056 ** pList might be NULL following an OOM error.  But pSpan should never be
       
 61057 ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
       
 61058 ** is set.
       
 61059 */
       
 61060 SQLITE_PRIVATE void sqlite3ExprListSetSpan(
       
 61061   Parse *pParse,          /* Parsing context */
       
 61062   ExprList *pList,        /* List to which to add the span. */
       
 61063   ExprSpan *pSpan         /* The span to be added */
       
 61064 ){
       
 61065   sqlite3 *db = pParse->db;
       
 61066   assert( pList!=0 || db->mallocFailed!=0 );
       
 61067   if( pList ){
       
 61068     struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
       
 61069     assert( pList->nExpr>0 );
       
 61070     assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
       
 61071     sqlite3DbFree(db, pItem->zSpan);
       
 61072     pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
       
 61073                                     (int)(pSpan->zEnd - pSpan->zStart));
       
 61074   }
       
 61075 }
       
 61076 
       
 61077 /*
       
 61078 ** If the expression list pEList contains more than iLimit elements,
       
 61079 ** leave an error message in pParse.
       
 61080 */
       
 61081 SQLITE_PRIVATE void sqlite3ExprListCheckLength(
       
 61082   Parse *pParse,
       
 61083   ExprList *pEList,
       
 61084   const char *zObject
       
 61085 ){
       
 61086   int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
       
 61087   testcase( pEList && pEList->nExpr==mx );
       
 61088   testcase( pEList && pEList->nExpr==mx+1 );
       
 61089   if( pEList && pEList->nExpr>mx ){
       
 61090     sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
       
 61091   }
       
 61092 }
       
 61093 
       
 61094 /*
       
 61095 ** Delete an entire expression list.
       
 61096 */
       
 61097 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
       
 61098   int i;
       
 61099   struct ExprList_item *pItem;
       
 61100   if( pList==0 ) return;
       
 61101   assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
       
 61102   assert( pList->nExpr<=pList->nAlloc );
       
 61103   for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
       
 61104     sqlite3ExprDelete(db, pItem->pExpr);
       
 61105     sqlite3DbFree(db, pItem->zName);
       
 61106     sqlite3DbFree(db, pItem->zSpan);
       
 61107   }
       
 61108   sqlite3DbFree(db, pList->a);
       
 61109   sqlite3DbFree(db, pList);
       
 61110 }
       
 61111 
       
 61112 /*
       
 61113 ** These routines are Walker callbacks.  Walker.u.pi is a pointer
       
 61114 ** to an integer.  These routines are checking an expression to see
       
 61115 ** if it is a constant.  Set *Walker.u.pi to 0 if the expression is
       
 61116 ** not constant.
       
 61117 **
       
 61118 ** These callback routines are used to implement the following:
       
 61119 **
       
 61120 **     sqlite3ExprIsConstant()
       
 61121 **     sqlite3ExprIsConstantNotJoin()
       
 61122 **     sqlite3ExprIsConstantOrFunction()
       
 61123 **
       
 61124 */
       
 61125 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
       
 61126 
       
 61127   /* If pWalker->u.i is 3 then any term of the expression that comes from
       
 61128   ** the ON or USING clauses of a join disqualifies the expression
       
 61129   ** from being considered constant. */
       
 61130   if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
       
 61131     pWalker->u.i = 0;
       
 61132     return WRC_Abort;
       
 61133   }
       
 61134 
       
 61135   switch( pExpr->op ){
       
 61136     /* Consider functions to be constant if all their arguments are constant
       
 61137     ** and pWalker->u.i==2 */
       
 61138     case TK_FUNCTION:
       
 61139       if( pWalker->u.i==2 ) return 0;
       
 61140       /* Fall through */
       
 61141     case TK_ID:
       
 61142     case TK_COLUMN:
       
 61143     case TK_AGG_FUNCTION:
       
 61144     case TK_AGG_COLUMN:
       
 61145       testcase( pExpr->op==TK_ID );
       
 61146       testcase( pExpr->op==TK_COLUMN );
       
 61147       testcase( pExpr->op==TK_AGG_FUNCTION );
       
 61148       testcase( pExpr->op==TK_AGG_COLUMN );
       
 61149       pWalker->u.i = 0;
       
 61150       return WRC_Abort;
       
 61151     default:
       
 61152       testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
       
 61153       testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
       
 61154       return WRC_Continue;
       
 61155   }
       
 61156 }
       
 61157 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
       
 61158   UNUSED_PARAMETER(NotUsed);
       
 61159   pWalker->u.i = 0;
       
 61160   return WRC_Abort;
       
 61161 }
       
 61162 static int exprIsConst(Expr *p, int initFlag){
       
 61163   Walker w;
       
 61164   w.u.i = initFlag;
       
 61165   w.xExprCallback = exprNodeIsConstant;
       
 61166   w.xSelectCallback = selectNodeIsConstant;
       
 61167   sqlite3WalkExpr(&w, p);
       
 61168   return w.u.i;
       
 61169 }
       
 61170 
       
 61171 /*
       
 61172 ** Walk an expression tree.  Return 1 if the expression is constant
       
 61173 ** and 0 if it involves variables or function calls.
       
 61174 **
       
 61175 ** For the purposes of this function, a double-quoted string (ex: "abc")
       
 61176 ** is considered a variable but a single-quoted string (ex: 'abc') is
       
 61177 ** a constant.
       
 61178 */
       
 61179 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
       
 61180   return exprIsConst(p, 1);
       
 61181 }
       
 61182 
       
 61183 /*
       
 61184 ** Walk an expression tree.  Return 1 if the expression is constant
       
 61185 ** that does no originate from the ON or USING clauses of a join.
       
 61186 ** Return 0 if it involves variables or function calls or terms from
       
 61187 ** an ON or USING clause.
       
 61188 */
       
 61189 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
       
 61190   return exprIsConst(p, 3);
       
 61191 }
       
 61192 
       
 61193 /*
       
 61194 ** Walk an expression tree.  Return 1 if the expression is constant
       
 61195 ** or a function call with constant arguments.  Return and 0 if there
       
 61196 ** are any variables.
       
 61197 **
       
 61198 ** For the purposes of this function, a double-quoted string (ex: "abc")
       
 61199 ** is considered a variable but a single-quoted string (ex: 'abc') is
       
 61200 ** a constant.
       
 61201 */
       
 61202 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p){
       
 61203   return exprIsConst(p, 2);
       
 61204 }
       
 61205 
       
 61206 /*
       
 61207 ** If the expression p codes a constant integer that is small enough
       
 61208 ** to fit in a 32-bit integer, return 1 and put the value of the integer
       
 61209 ** in *pValue.  If the expression is not an integer or if it is too big
       
 61210 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
       
 61211 */
       
 61212 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
       
 61213   int rc = 0;
       
 61214   if( p->flags & EP_IntValue ){
       
 61215     *pValue = p->u.iValue;
       
 61216     return 1;
       
 61217   }
       
 61218   switch( p->op ){
       
 61219     case TK_INTEGER: {
       
 61220       rc = sqlite3GetInt32(p->u.zToken, pValue);
       
 61221       assert( rc==0 );
       
 61222       break;
       
 61223     }
       
 61224     case TK_UPLUS: {
       
 61225       rc = sqlite3ExprIsInteger(p->pLeft, pValue);
       
 61226       break;
       
 61227     }
       
 61228     case TK_UMINUS: {
       
 61229       int v;
       
 61230       if( sqlite3ExprIsInteger(p->pLeft, &v) ){
       
 61231         *pValue = -v;
       
 61232         rc = 1;
       
 61233       }
       
 61234       break;
       
 61235     }
       
 61236     default: break;
       
 61237   }
       
 61238   if( rc ){
       
 61239     assert( ExprHasAnyProperty(p, EP_Reduced|EP_TokenOnly)
       
 61240                || (p->flags2 & EP2_MallocedToken)==0 );
       
 61241     p->op = TK_INTEGER;
       
 61242     p->flags |= EP_IntValue;
       
 61243     p->u.iValue = *pValue;
       
 61244   }
       
 61245   return rc;
       
 61246 }
       
 61247 
       
 61248 /*
       
 61249 ** Return TRUE if the given string is a row-id column name.
       
 61250 */
       
 61251 SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
       
 61252   if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
       
 61253   if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
       
 61254   if( sqlite3StrICmp(z, "OID")==0 ) return 1;
       
 61255   return 0;
       
 61256 }
       
 61257 
       
 61258 /*
       
 61259 ** Return true if we are able to the IN operator optimization on a
       
 61260 ** query of the form
       
 61261 **
       
 61262 **       x IN (SELECT ...)
       
 61263 **
       
 61264 ** Where the SELECT... clause is as specified by the parameter to this
       
 61265 ** routine.
       
 61266 **
       
 61267 ** The Select object passed in has already been preprocessed and no
       
 61268 ** errors have been found.
       
 61269 */
       
 61270 #ifndef SQLITE_OMIT_SUBQUERY
       
 61271 static int isCandidateForInOpt(Select *p){
       
 61272   SrcList *pSrc;
       
 61273   ExprList *pEList;
       
 61274   Table *pTab;
       
 61275   if( p==0 ) return 0;                   /* right-hand side of IN is SELECT */
       
 61276   if( p->pPrior ) return 0;              /* Not a compound SELECT */
       
 61277   if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
       
 61278     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
       
 61279     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
       
 61280     return 0; /* No DISTINCT keyword and no aggregate functions */
       
 61281   }
       
 61282   assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
       
 61283   if( p->pLimit ) return 0;              /* Has no LIMIT clause */
       
 61284   assert( p->pOffset==0 );               /* No LIMIT means no OFFSET */
       
 61285   if( p->pWhere ) return 0;              /* Has no WHERE clause */
       
 61286   pSrc = p->pSrc;
       
 61287   assert( pSrc!=0 );
       
 61288   if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
       
 61289   if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
       
 61290   pTab = pSrc->a[0].pTab;
       
 61291   if( NEVER(pTab==0) ) return 0;
       
 61292   assert( pTab->pSelect==0 );            /* FROM clause is not a view */
       
 61293   if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
       
 61294   pEList = p->pEList;
       
 61295   if( pEList->nExpr!=1 ) return 0;       /* One column in the result set */
       
 61296   if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
       
 61297   return 1;
       
 61298 }
       
 61299 #endif /* SQLITE_OMIT_SUBQUERY */
       
 61300 
       
 61301 /*
       
 61302 ** This function is used by the implementation of the IN (...) operator.
       
 61303 ** It's job is to find or create a b-tree structure that may be used
       
 61304 ** either to test for membership of the (...) set or to iterate through
       
 61305 ** its members, skipping duplicates.
       
 61306 **
       
 61307 ** The index of the cursor opened on the b-tree (database table, database index 
       
 61308 ** or ephermal table) is stored in pX->iTable before this function returns.
       
 61309 ** The returned value of this function indicates the b-tree type, as follows:
       
 61310 **
       
 61311 **   IN_INDEX_ROWID - The cursor was opened on a database table.
       
 61312 **   IN_INDEX_INDEX - The cursor was opened on a database index.
       
 61313 **   IN_INDEX_EPH -   The cursor was opened on a specially created and
       
 61314 **                    populated epheremal table.
       
 61315 **
       
 61316 ** An existing b-tree may only be used if the SELECT is of the simple
       
 61317 ** form:
       
 61318 **
       
 61319 **     SELECT <column> FROM <table>
       
 61320 **
       
 61321 ** If the prNotFound parameter is 0, then the b-tree will be used to iterate
       
 61322 ** through the set members, skipping any duplicates. In this case an
       
 61323 ** epheremal table must be used unless the selected <column> is guaranteed
       
 61324 ** to be unique - either because it is an INTEGER PRIMARY KEY or it
       
 61325 ** has a UNIQUE constraint or UNIQUE index.
       
 61326 **
       
 61327 ** If the prNotFound parameter is not 0, then the b-tree will be used 
       
 61328 ** for fast set membership tests. In this case an epheremal table must 
       
 61329 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can 
       
 61330 ** be found with <column> as its left-most column.
       
 61331 **
       
 61332 ** When the b-tree is being used for membership tests, the calling function
       
 61333 ** needs to know whether or not the structure contains an SQL NULL 
       
 61334 ** value in order to correctly evaluate expressions like "X IN (Y, Z)".
       
 61335 ** If there is a chance that the b-tree might contain a NULL value at
       
 61336 ** runtime, then a register is allocated and the register number written
       
 61337 ** to *prNotFound. If there is no chance that the b-tree contains a
       
 61338 ** NULL value, then *prNotFound is left unchanged.
       
 61339 **
       
 61340 ** If a register is allocated and its location stored in *prNotFound, then
       
 61341 ** its initial value is NULL. If the b-tree does not remain constant
       
 61342 ** for the duration of the query (i.e. the SELECT that generates the b-tree
       
 61343 ** is a correlated subquery) then the value of the allocated register is
       
 61344 ** reset to NULL each time the b-tree is repopulated. This allows the
       
 61345 ** caller to use vdbe code equivalent to the following:
       
 61346 **
       
 61347 **   if( register==NULL ){
       
 61348 **     has_null = <test if data structure contains null>
       
 61349 **     register = 1
       
 61350 **   }
       
 61351 **
       
 61352 ** in order to avoid running the <test if data structure contains null>
       
 61353 ** test more often than is necessary.
       
 61354 */
       
 61355 #ifndef SQLITE_OMIT_SUBQUERY
       
 61356 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
       
 61357   Select *p;                            /* SELECT to the right of IN operator */
       
 61358   int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
       
 61359   int iTab = pParse->nTab++;            /* Cursor of the RHS table */
       
 61360   int mustBeUnique = (prNotFound==0);   /* True if RHS must be unique */
       
 61361 
       
 61362   /* Check to see if an existing table or index can be used to
       
 61363   ** satisfy the query.  This is preferable to generating a new 
       
 61364   ** ephemeral table.
       
 61365   */
       
 61366   p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
       
 61367   if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
       
 61368     sqlite3 *db = pParse->db;              /* Database connection */
       
 61369     Expr *pExpr = p->pEList->a[0].pExpr;   /* Expression <column> */
       
 61370     int iCol = pExpr->iColumn;             /* Index of column <column> */
       
 61371     Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
       
 61372     Table *pTab = p->pSrc->a[0].pTab;      /* Table <table>. */
       
 61373     int iDb;                               /* Database idx for pTab */
       
 61374    
       
 61375     /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
       
 61376     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       
 61377     sqlite3CodeVerifySchema(pParse, iDb);
       
 61378     sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
       
 61379 
       
 61380     /* This function is only called from two places. In both cases the vdbe
       
 61381     ** has already been allocated. So assume sqlite3GetVdbe() is always
       
 61382     ** successful here.
       
 61383     */
       
 61384     assert(v);
       
 61385     if( iCol<0 ){
       
 61386       int iMem = ++pParse->nMem;
       
 61387       int iAddr;
       
 61388 
       
 61389       iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
       
 61390       sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
       
 61391 
       
 61392       sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
       
 61393       eType = IN_INDEX_ROWID;
       
 61394 
       
 61395       sqlite3VdbeJumpHere(v, iAddr);
       
 61396     }else{
       
 61397       Index *pIdx;                         /* Iterator variable */
       
 61398 
       
 61399       /* The collation sequence used by the comparison. If an index is to
       
 61400       ** be used in place of a temp-table, it must be ordered according
       
 61401       ** to this collation sequence.  */
       
 61402       CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
       
 61403 
       
 61404       /* Check that the affinity that will be used to perform the 
       
 61405       ** comparison is the same as the affinity of the column. If
       
 61406       ** it is not, it is not possible to use any index.
       
 61407       */
       
 61408       char aff = comparisonAffinity(pX);
       
 61409       int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
       
 61410 
       
 61411       for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
       
 61412         if( (pIdx->aiColumn[0]==iCol)
       
 61413          && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
       
 61414          && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
       
 61415         ){
       
 61416           int iMem = ++pParse->nMem;
       
 61417           int iAddr;
       
 61418           char *pKey;
       
 61419   
       
 61420           pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
       
 61421           iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
       
 61422           sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
       
 61423   
       
 61424           sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
       
 61425                                pKey,P4_KEYINFO_HANDOFF);
       
 61426           VdbeComment((v, "%s", pIdx->zName));
       
 61427           eType = IN_INDEX_INDEX;
       
 61428 
       
 61429           sqlite3VdbeJumpHere(v, iAddr);
       
 61430           if( prNotFound && !pTab->aCol[iCol].notNull ){
       
 61431             *prNotFound = ++pParse->nMem;
       
 61432           }
       
 61433         }
       
 61434       }
       
 61435     }
       
 61436   }
       
 61437 
       
 61438   if( eType==0 ){
       
 61439     /* Could not found an existing able or index to use as the RHS b-tree.
       
 61440     ** We will have to generate an ephemeral table to do the job.
       
 61441     */
       
 61442     int rMayHaveNull = 0;
       
 61443     eType = IN_INDEX_EPH;
       
 61444     if( prNotFound ){
       
 61445       *prNotFound = rMayHaveNull = ++pParse->nMem;
       
 61446     }else if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
       
 61447       eType = IN_INDEX_ROWID;
       
 61448     }
       
 61449     sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
       
 61450   }else{
       
 61451     pX->iTable = iTab;
       
 61452   }
       
 61453   return eType;
       
 61454 }
       
 61455 #endif
       
 61456 
       
 61457 /*
       
 61458 ** Generate code for scalar subqueries used as an expression
       
 61459 ** and IN operators.  Examples:
       
 61460 **
       
 61461 **     (SELECT a FROM b)          -- subquery
       
 61462 **     EXISTS (SELECT a FROM b)   -- EXISTS subquery
       
 61463 **     x IN (4,5,11)              -- IN operator with list on right-hand side
       
 61464 **     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
       
 61465 **
       
 61466 ** The pExpr parameter describes the expression that contains the IN
       
 61467 ** operator or subquery.
       
 61468 **
       
 61469 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed
       
 61470 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
       
 61471 ** to some integer key column of a table B-Tree. In this case, use an
       
 61472 ** intkey B-Tree to store the set of IN(...) values instead of the usual
       
 61473 ** (slower) variable length keys B-Tree.
       
 61474 **
       
 61475 ** If rMayHaveNull is non-zero, that means that the operation is an IN
       
 61476 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
       
 61477 ** Furthermore, the IN is in a WHERE clause and that we really want
       
 61478 ** to iterate over the RHS of the IN operator in order to quickly locate
       
 61479 ** all corresponding LHS elements.  All this routine does is initialize
       
 61480 ** the register given by rMayHaveNull to NULL.  Calling routines will take
       
 61481 ** care of changing this register value to non-NULL if the RHS is NULL-free.
       
 61482 **
       
 61483 ** If rMayHaveNull is zero, that means that the subquery is being used
       
 61484 ** for membership testing only.  There is no need to initialize any
       
 61485 ** registers to indicate the presense or absence of NULLs on the RHS.
       
 61486 */
       
 61487 #ifndef SQLITE_OMIT_SUBQUERY
       
 61488 SQLITE_PRIVATE void sqlite3CodeSubselect(
       
 61489   Parse *pParse,          /* Parsing context */
       
 61490   Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
       
 61491   int rMayHaveNull,       /* Register that records whether NULLs exist in RHS */
       
 61492   int isRowid             /* If true, LHS of IN operator is a rowid */
       
 61493 ){
       
 61494   int testAddr = 0;                       /* One-time test address */
       
 61495   Vdbe *v = sqlite3GetVdbe(pParse);
       
 61496   if( NEVER(v==0) ) return;
       
 61497   sqlite3ExprCachePush(pParse);
       
 61498 
       
 61499   /* This code must be run in its entirety every time it is encountered
       
 61500   ** if any of the following is true:
       
 61501   **
       
 61502   **    *  The right-hand side is a correlated subquery
       
 61503   **    *  The right-hand side is an expression list containing variables
       
 61504   **    *  We are inside a trigger
       
 61505   **
       
 61506   ** If all of the above are false, then we can run this code just once
       
 61507   ** save the results, and reuse the same result on subsequent invocations.
       
 61508   */
       
 61509   if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->pTriggerTab ){
       
 61510     int mem = ++pParse->nMem;
       
 61511     sqlite3VdbeAddOp1(v, OP_If, mem);
       
 61512     testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
       
 61513     assert( testAddr>0 || pParse->db->mallocFailed );
       
 61514   }
       
 61515 
       
 61516   switch( pExpr->op ){
       
 61517     case TK_IN: {
       
 61518       char affinity;
       
 61519       KeyInfo keyInfo;
       
 61520       int addr;        /* Address of OP_OpenEphemeral instruction */
       
 61521       Expr *pLeft = pExpr->pLeft;
       
 61522 
       
 61523       if( rMayHaveNull ){
       
 61524         sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
       
 61525       }
       
 61526 
       
 61527       affinity = sqlite3ExprAffinity(pLeft);
       
 61528 
       
 61529       /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
       
 61530       ** expression it is handled the same way. A virtual table is 
       
 61531       ** filled with single-field index keys representing the results
       
 61532       ** from the SELECT or the <exprlist>.
       
 61533       **
       
 61534       ** If the 'x' expression is a column value, or the SELECT...
       
 61535       ** statement returns a column value, then the affinity of that
       
 61536       ** column is used to build the index keys. If both 'x' and the
       
 61537       ** SELECT... statement are columns, then numeric affinity is used
       
 61538       ** if either column has NUMERIC or INTEGER affinity. If neither
       
 61539       ** 'x' nor the SELECT... statement are columns, then numeric affinity
       
 61540       ** is used.
       
 61541       */
       
 61542       pExpr->iTable = pParse->nTab++;
       
 61543       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
       
 61544       memset(&keyInfo, 0, sizeof(keyInfo));
       
 61545       keyInfo.nField = 1;
       
 61546 
       
 61547       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
       
 61548         /* Case 1:     expr IN (SELECT ...)
       
 61549         **
       
 61550         ** Generate code to write the results of the select into the temporary
       
 61551         ** table allocated and opened above.
       
 61552         */
       
 61553         SelectDest dest;
       
 61554         ExprList *pEList;
       
 61555 
       
 61556         assert( !isRowid );
       
 61557         sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
       
 61558         dest.affinity = (u8)affinity;
       
 61559         assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
       
 61560         if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
       
 61561           return;
       
 61562         }
       
 61563         pEList = pExpr->x.pSelect->pEList;
       
 61564         if( ALWAYS(pEList!=0 && pEList->nExpr>0) ){ 
       
 61565           keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
       
 61566               pEList->a[0].pExpr);
       
 61567         }
       
 61568       }else if( pExpr->x.pList!=0 ){
       
 61569         /* Case 2:     expr IN (exprlist)
       
 61570         **
       
 61571         ** For each expression, build an index key from the evaluation and
       
 61572         ** store it in the temporary table. If <expr> is a column, then use
       
 61573         ** that columns affinity when building index keys. If <expr> is not
       
 61574         ** a column, use numeric affinity.
       
 61575         */
       
 61576         int i;
       
 61577         ExprList *pList = pExpr->x.pList;
       
 61578         struct ExprList_item *pItem;
       
 61579         int r1, r2, r3;
       
 61580 
       
 61581         if( !affinity ){
       
 61582           affinity = SQLITE_AFF_NONE;
       
 61583         }
       
 61584         keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
       
 61585 
       
 61586         /* Loop through each expression in <exprlist>. */
       
 61587         r1 = sqlite3GetTempReg(pParse);
       
 61588         r2 = sqlite3GetTempReg(pParse);
       
 61589         sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
       
 61590         for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
       
 61591           Expr *pE2 = pItem->pExpr;
       
 61592 
       
 61593           /* If the expression is not constant then we will need to
       
 61594           ** disable the test that was generated above that makes sure
       
 61595           ** this code only executes once.  Because for a non-constant
       
 61596           ** expression we need to rerun this code each time.
       
 61597           */
       
 61598           if( testAddr && !sqlite3ExprIsConstant(pE2) ){
       
 61599             sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
       
 61600             testAddr = 0;
       
 61601           }
       
 61602 
       
 61603           /* Evaluate the expression and insert it into the temp table */
       
 61604           r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
       
 61605           if( isRowid ){
       
 61606             sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, sqlite3VdbeCurrentAddr(v)+2);
       
 61607             sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
       
 61608           }else{
       
 61609             sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
       
 61610             sqlite3ExprCacheAffinityChange(pParse, r3, 1);
       
 61611             sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
       
 61612           }
       
 61613         }
       
 61614         sqlite3ReleaseTempReg(pParse, r1);
       
 61615         sqlite3ReleaseTempReg(pParse, r2);
       
 61616       }
       
 61617       if( !isRowid ){
       
 61618         sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
       
 61619       }
       
 61620       break;
       
 61621     }
       
 61622 
       
 61623     case TK_EXISTS:
       
 61624     case TK_SELECT:
       
 61625     default: {
       
 61626       /* If this has to be a scalar SELECT.  Generate code to put the
       
 61627       ** value of this select in a memory cell and record the number
       
 61628       ** of the memory cell in iColumn.  If this is an EXISTS, write
       
 61629       ** an integer 0 (not exists) or 1 (exists) into a memory cell
       
 61630       ** and record that memory cell in iColumn.
       
 61631       */
       
 61632       static const Token one = { "1", 1 };  /* Token for literal value 1 */
       
 61633       Select *pSel;                         /* SELECT statement to encode */
       
 61634       SelectDest dest;                      /* How to deal with SELECt result */
       
 61635 
       
 61636       testcase( pExpr->op==TK_EXISTS );
       
 61637       testcase( pExpr->op==TK_SELECT );
       
 61638       assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
       
 61639 
       
 61640       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
       
 61641       pSel = pExpr->x.pSelect;
       
 61642       sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
       
 61643       if( pExpr->op==TK_SELECT ){
       
 61644         dest.eDest = SRT_Mem;
       
 61645         sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
       
 61646         VdbeComment((v, "Init subquery result"));
       
 61647       }else{
       
 61648         dest.eDest = SRT_Exists;
       
 61649         sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
       
 61650         VdbeComment((v, "Init EXISTS result"));
       
 61651       }
       
 61652       sqlite3ExprDelete(pParse->db, pSel->pLimit);
       
 61653       pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
       
 61654       if( sqlite3Select(pParse, pSel, &dest) ){
       
 61655         return;
       
 61656       }
       
 61657       pExpr->iColumn = (i16)dest.iParm;
       
 61658       ExprSetIrreducible(pExpr);
       
 61659       break;
       
 61660     }
       
 61661   }
       
 61662 
       
 61663   if( testAddr ){
       
 61664     sqlite3VdbeJumpHere(v, testAddr-1);
       
 61665   }
       
 61666   sqlite3ExprCachePop(pParse, 1);
       
 61667 
       
 61668   return;
       
 61669 }
       
 61670 #endif /* SQLITE_OMIT_SUBQUERY */
       
 61671 
       
 61672 /*
       
 61673 ** Duplicate an 8-byte value
       
 61674 */
       
 61675 static char *dup8bytes(Vdbe *v, const char *in){
       
 61676   char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
       
 61677   if( out ){
       
 61678     memcpy(out, in, 8);
       
 61679   }
       
 61680   return out;
       
 61681 }
       
 61682 
       
 61683 /*
       
 61684 ** Generate an instruction that will put the floating point
       
 61685 ** value described by z[0..n-1] into register iMem.
       
 61686 **
       
 61687 ** The z[] string will probably not be zero-terminated.  But the 
       
 61688 ** z[n] character is guaranteed to be something that does not look
       
 61689 ** like the continuation of the number.
       
 61690 */
       
 61691 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
       
 61692   if( ALWAYS(z!=0) ){
       
 61693     double value;
       
 61694     char *zV;
       
 61695     sqlite3AtoF(z, &value);
       
 61696     assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
       
 61697     if( negateFlag ) value = -value;
       
 61698     zV = dup8bytes(v, (char*)&value);
       
 61699     sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
       
 61700   }
       
 61701 }
       
 61702 
       
 61703 
       
 61704 /*
       
 61705 ** Generate an instruction that will put the integer describe by
       
 61706 ** text z[0..n-1] into register iMem.
       
 61707 **
       
 61708 ** The z[] string will probably not be zero-terminated.  But the 
       
 61709 ** z[n] character is guaranteed to be something that does not look
       
 61710 ** like the continuation of the number.
       
 61711 */
       
 61712 static void codeInteger(Vdbe *v, Expr *pExpr, int negFlag, int iMem){
       
 61713   if( pExpr->flags & EP_IntValue ){
       
 61714     int i = pExpr->u.iValue;
       
 61715     if( negFlag ) i = -i;
       
 61716     sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
       
 61717   }else{
       
 61718     const char *z = pExpr->u.zToken;
       
 61719     assert( z!=0 );
       
 61720     if( sqlite3FitsIn64Bits(z, negFlag) ){
       
 61721       i64 value;
       
 61722       char *zV;
       
 61723       sqlite3Atoi64(z, &value);
       
 61724       if( negFlag ) value = -value;
       
 61725       zV = dup8bytes(v, (char*)&value);
       
 61726       sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
       
 61727     }else{
       
 61728       codeReal(v, z, negFlag, iMem);
       
 61729     }
       
 61730   }
       
 61731 }
       
 61732 
       
 61733 /*
       
 61734 ** Clear a cache entry.
       
 61735 */
       
 61736 static void cacheEntryClear(Parse *pParse, struct yColCache *p){
       
 61737   if( p->tempReg ){
       
 61738     if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
       
 61739       pParse->aTempReg[pParse->nTempReg++] = p->iReg;
       
 61740     }
       
 61741     p->tempReg = 0;
       
 61742   }
       
 61743 }
       
 61744 
       
 61745 
       
 61746 /*
       
 61747 ** Record in the column cache that a particular column from a
       
 61748 ** particular table is stored in a particular register.
       
 61749 */
       
 61750 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
       
 61751   int i;
       
 61752   int minLru;
       
 61753   int idxLru;
       
 61754   struct yColCache *p;
       
 61755 
       
 61756   assert( iReg>0 );  /* Register numbers are always positive */
       
 61757   assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
       
 61758 
       
 61759   /* First replace any existing entry */
       
 61760   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
       
 61761     if( p->iReg && p->iTable==iTab && p->iColumn==iCol ){
       
 61762       cacheEntryClear(pParse, p);
       
 61763       p->iLevel = pParse->iCacheLevel;
       
 61764       p->iReg = iReg;
       
 61765       p->affChange = 0;
       
 61766       p->lru = pParse->iCacheCnt++;
       
 61767       return;
       
 61768     }
       
 61769   }
       
 61770 
       
 61771   /* Find an empty slot and replace it */
       
 61772   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
       
 61773     if( p->iReg==0 ){
       
 61774       p->iLevel = pParse->iCacheLevel;
       
 61775       p->iTable = iTab;
       
 61776       p->iColumn = iCol;
       
 61777       p->iReg = iReg;
       
 61778       p->affChange = 0;
       
 61779       p->tempReg = 0;
       
 61780       p->lru = pParse->iCacheCnt++;
       
 61781       return;
       
 61782     }
       
 61783   }
       
 61784 
       
 61785   /* Replace the last recently used */
       
 61786   minLru = 0x7fffffff;
       
 61787   idxLru = -1;
       
 61788   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
       
 61789     if( p->lru<minLru ){
       
 61790       idxLru = i;
       
 61791       minLru = p->lru;
       
 61792     }
       
 61793   }
       
 61794   if( ALWAYS(idxLru>=0) ){
       
 61795     p = &pParse->aColCache[idxLru];
       
 61796     p->iLevel = pParse->iCacheLevel;
       
 61797     p->iTable = iTab;
       
 61798     p->iColumn = iCol;
       
 61799     p->iReg = iReg;
       
 61800     p->affChange = 0;
       
 61801     p->tempReg = 0;
       
 61802     p->lru = pParse->iCacheCnt++;
       
 61803     return;
       
 61804   }
       
 61805 }
       
 61806 
       
 61807 /*
       
 61808 ** Indicate that a register is being overwritten.  Purge the register
       
 61809 ** from the column cache.
       
 61810 */
       
 61811 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg){
       
 61812   int i;
       
 61813   struct yColCache *p;
       
 61814   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
       
 61815     if( p->iReg==iReg ){
       
 61816       cacheEntryClear(pParse, p);
       
 61817       p->iReg = 0;
       
 61818     }
       
 61819   }
       
 61820 }
       
 61821 
       
 61822 /*
       
 61823 ** Remember the current column cache context.  Any new entries added
       
 61824 ** added to the column cache after this call are removed when the
       
 61825 ** corresponding pop occurs.
       
 61826 */
       
 61827 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
       
 61828   pParse->iCacheLevel++;
       
 61829 }
       
 61830 
       
 61831 /*
       
 61832 ** Remove from the column cache any entries that were added since the
       
 61833 ** the previous N Push operations.  In other words, restore the cache
       
 61834 ** to the state it was in N Pushes ago.
       
 61835 */
       
 61836 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse, int N){
       
 61837   int i;
       
 61838   struct yColCache *p;
       
 61839   assert( N>0 );
       
 61840   assert( pParse->iCacheLevel>=N );
       
 61841   pParse->iCacheLevel -= N;
       
 61842   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
       
 61843     if( p->iReg && p->iLevel>pParse->iCacheLevel ){
       
 61844       cacheEntryClear(pParse, p);
       
 61845       p->iReg = 0;
       
 61846     }
       
 61847   }
       
 61848 }
       
 61849 
       
 61850 /*
       
 61851 ** When a cached column is reused, make sure that its register is
       
 61852 ** no longer available as a temp register.  ticket #3879:  that same
       
 61853 ** register might be in the cache in multiple places, so be sure to
       
 61854 ** get them all.
       
 61855 */
       
 61856 static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
       
 61857   int i;
       
 61858   struct yColCache *p;
       
 61859   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
       
 61860     if( p->iReg==iReg ){
       
 61861       p->tempReg = 0;
       
 61862     }
       
 61863   }
       
 61864 }
       
 61865 
       
 61866 /*
       
 61867 ** Generate code that will extract the iColumn-th column from
       
 61868 ** table pTab and store the column value in a register.  An effort
       
 61869 ** is made to store the column value in register iReg, but this is
       
 61870 ** not guaranteed.  The location of the column value is returned.
       
 61871 **
       
 61872 ** There must be an open cursor to pTab in iTable when this routine
       
 61873 ** is called.  If iColumn<0 then code is generated that extracts the rowid.
       
 61874 **
       
 61875 ** This routine might attempt to reuse the value of the column that
       
 61876 ** has already been loaded into a register.  The value will always
       
 61877 ** be used if it has not undergone any affinity changes.  But if
       
 61878 ** an affinity change has occurred, then the cached value will only be
       
 61879 ** used if allowAffChng is true.
       
 61880 */
       
 61881 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
       
 61882   Parse *pParse,   /* Parsing and code generating context */
       
 61883   Table *pTab,     /* Description of the table we are reading from */
       
 61884   int iColumn,     /* Index of the table column */
       
 61885   int iTable,      /* The cursor pointing to the table */
       
 61886   int iReg,        /* Store results here */
       
 61887   int allowAffChng /* True if prior affinity changes are OK */
       
 61888 ){
       
 61889   Vdbe *v = pParse->pVdbe;
       
 61890   int i;
       
 61891   struct yColCache *p;
       
 61892 
       
 61893   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
       
 61894     if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn
       
 61895            && (!p->affChange || allowAffChng) ){
       
 61896       p->lru = pParse->iCacheCnt++;
       
 61897       sqlite3ExprCachePinRegister(pParse, p->iReg);
       
 61898       return p->iReg;
       
 61899     }
       
 61900   }  
       
 61901   assert( v!=0 );
       
 61902   if( iColumn<0 ){
       
 61903     sqlite3VdbeAddOp2(v, OP_Rowid, iTable, iReg);
       
 61904   }else if( ALWAYS(pTab!=0) ){
       
 61905     int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
       
 61906     sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
       
 61907     sqlite3ColumnDefault(v, pTab, iColumn, iReg);
       
 61908   }
       
 61909   sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
       
 61910   return iReg;
       
 61911 }
       
 61912 
       
 61913 /*
       
 61914 ** Clear all column cache entries.
       
 61915 */
       
 61916 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
       
 61917   int i;
       
 61918   struct yColCache *p;
       
 61919 
       
 61920   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
       
 61921     if( p->iReg ){
       
 61922       cacheEntryClear(pParse, p);
       
 61923       p->iReg = 0;
       
 61924     }
       
 61925   }
       
 61926 }
       
 61927 
       
 61928 /*
       
 61929 ** Record the fact that an affinity change has occurred on iCount
       
 61930 ** registers starting with iStart.
       
 61931 */
       
 61932 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
       
 61933   int iEnd = iStart + iCount - 1;
       
 61934   int i;
       
 61935   struct yColCache *p;
       
 61936   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
       
 61937     int r = p->iReg;
       
 61938     if( r>=iStart && r<=iEnd ){
       
 61939       p->affChange = 1;
       
 61940     }
       
 61941   }
       
 61942 }
       
 61943 
       
 61944 /*
       
 61945 ** Generate code to move content from registers iFrom...iFrom+nReg-1
       
 61946 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
       
 61947 */
       
 61948 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
       
 61949   int i;
       
 61950   struct yColCache *p;
       
 61951   if( NEVER(iFrom==iTo) ) return;
       
 61952   sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
       
 61953   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
       
 61954     int x = p->iReg;
       
 61955     if( x>=iFrom && x<iFrom+nReg ){
       
 61956       p->iReg += iTo-iFrom;
       
 61957     }
       
 61958   }
       
 61959 }
       
 61960 
       
 61961 /*
       
 61962 ** Generate code to copy content from registers iFrom...iFrom+nReg-1
       
 61963 ** over to iTo..iTo+nReg-1.
       
 61964 */
       
 61965 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
       
 61966   int i;
       
 61967   if( NEVER(iFrom==iTo) ) return;
       
 61968   for(i=0; i<nReg; i++){
       
 61969     sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
       
 61970   }
       
 61971 }
       
 61972 
       
 61973 /*
       
 61974 ** Return true if any register in the range iFrom..iTo (inclusive)
       
 61975 ** is used as part of the column cache.
       
 61976 */
       
 61977 static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
       
 61978   int i;
       
 61979   struct yColCache *p;
       
 61980   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
       
 61981     int r = p->iReg;
       
 61982     if( r>=iFrom && r<=iTo ) return 1;
       
 61983   }
       
 61984   return 0;
       
 61985 }
       
 61986 
       
 61987 /*
       
 61988 ** If the last instruction coded is an ephemeral copy of any of
       
 61989 ** the registers in the nReg registers beginning with iReg, then
       
 61990 ** convert the last instruction from OP_SCopy to OP_Copy.
       
 61991 */
       
 61992 SQLITE_PRIVATE void sqlite3ExprHardCopy(Parse *pParse, int iReg, int nReg){
       
 61993   VdbeOp *pOp;
       
 61994   Vdbe *v;
       
 61995 
       
 61996   assert( pParse->db->mallocFailed==0 );
       
 61997   v = pParse->pVdbe;
       
 61998   assert( v!=0 );
       
 61999   pOp = sqlite3VdbeGetOp(v, -1);
       
 62000   assert( pOp!=0 );
       
 62001   if( pOp->opcode==OP_SCopy && pOp->p1>=iReg && pOp->p1<iReg+nReg ){
       
 62002     pOp->opcode = OP_Copy;
       
 62003   }
       
 62004 }
       
 62005 
       
 62006 /*
       
 62007 ** Generate code to store the value of the iAlias-th alias in register
       
 62008 ** target.  The first time this is called, pExpr is evaluated to compute
       
 62009 ** the value of the alias.  The value is stored in an auxiliary register
       
 62010 ** and the number of that register is returned.  On subsequent calls,
       
 62011 ** the register number is returned without generating any code.
       
 62012 **
       
 62013 ** Note that in order for this to work, code must be generated in the
       
 62014 ** same order that it is executed.
       
 62015 **
       
 62016 ** Aliases are numbered starting with 1.  So iAlias is in the range
       
 62017 ** of 1 to pParse->nAlias inclusive.  
       
 62018 **
       
 62019 ** pParse->aAlias[iAlias-1] records the register number where the value
       
 62020 ** of the iAlias-th alias is stored.  If zero, that means that the
       
 62021 ** alias has not yet been computed.
       
 62022 */
       
 62023 static int codeAlias(Parse *pParse, int iAlias, Expr *pExpr, int target){
       
 62024 #if 0
       
 62025   sqlite3 *db = pParse->db;
       
 62026   int iReg;
       
 62027   if( pParse->nAliasAlloc<pParse->nAlias ){
       
 62028     pParse->aAlias = sqlite3DbReallocOrFree(db, pParse->aAlias,
       
 62029                                  sizeof(pParse->aAlias[0])*pParse->nAlias );
       
 62030     testcase( db->mallocFailed && pParse->nAliasAlloc>0 );
       
 62031     if( db->mallocFailed ) return 0;
       
 62032     memset(&pParse->aAlias[pParse->nAliasAlloc], 0,
       
 62033            (pParse->nAlias-pParse->nAliasAlloc)*sizeof(pParse->aAlias[0]));
       
 62034     pParse->nAliasAlloc = pParse->nAlias;
       
 62035   }
       
 62036   assert( iAlias>0 && iAlias<=pParse->nAlias );
       
 62037   iReg = pParse->aAlias[iAlias-1];
       
 62038   if( iReg==0 ){
       
 62039     if( pParse->iCacheLevel>0 ){
       
 62040       iReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
       
 62041     }else{
       
 62042       iReg = ++pParse->nMem;
       
 62043       sqlite3ExprCode(pParse, pExpr, iReg);
       
 62044       pParse->aAlias[iAlias-1] = iReg;
       
 62045     }
       
 62046   }
       
 62047   return iReg;
       
 62048 #else
       
 62049   UNUSED_PARAMETER(iAlias);
       
 62050   return sqlite3ExprCodeTarget(pParse, pExpr, target);
       
 62051 #endif
       
 62052 }
       
 62053 
       
 62054 /*
       
 62055 ** Generate code into the current Vdbe to evaluate the given
       
 62056 ** expression.  Attempt to store the results in register "target".
       
 62057 ** Return the register where results are stored.
       
 62058 **
       
 62059 ** With this routine, there is no guarantee that results will
       
 62060 ** be stored in target.  The result might be stored in some other
       
 62061 ** register if it is convenient to do so.  The calling function
       
 62062 ** must check the return code and move the results to the desired
       
 62063 ** register.
       
 62064 */
       
 62065 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
       
 62066   Vdbe *v = pParse->pVdbe;  /* The VM under construction */
       
 62067   int op;                   /* The opcode being coded */
       
 62068   int inReg = target;       /* Results stored in register inReg */
       
 62069   int regFree1 = 0;         /* If non-zero free this temporary register */
       
 62070   int regFree2 = 0;         /* If non-zero free this temporary register */
       
 62071   int r1, r2, r3, r4;       /* Various register numbers */
       
 62072   sqlite3 *db = pParse->db; /* The database connection */
       
 62073 
       
 62074   assert( target>0 && target<=pParse->nMem );
       
 62075   if( v==0 ){
       
 62076     assert( pParse->db->mallocFailed );
       
 62077     return 0;
       
 62078   }
       
 62079 
       
 62080   if( pExpr==0 ){
       
 62081     op = TK_NULL;
       
 62082   }else{
       
 62083     op = pExpr->op;
       
 62084   }
       
 62085   switch( op ){
       
 62086     case TK_AGG_COLUMN: {
       
 62087       AggInfo *pAggInfo = pExpr->pAggInfo;
       
 62088       struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
       
 62089       if( !pAggInfo->directMode ){
       
 62090         assert( pCol->iMem>0 );
       
 62091         inReg = pCol->iMem;
       
 62092         break;
       
 62093       }else if( pAggInfo->useSortingIdx ){
       
 62094         sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
       
 62095                               pCol->iSorterColumn, target);
       
 62096         break;
       
 62097       }
       
 62098       /* Otherwise, fall thru into the TK_COLUMN case */
       
 62099     }
       
 62100     case TK_COLUMN: {
       
 62101       if( pExpr->iTable<0 ){
       
 62102         /* This only happens when coding check constraints */
       
 62103         assert( pParse->ckBase>0 );
       
 62104         inReg = pExpr->iColumn + pParse->ckBase;
       
 62105       }else{
       
 62106         testcase( (pExpr->flags & EP_AnyAff)!=0 );
       
 62107         inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
       
 62108                                  pExpr->iColumn, pExpr->iTable, target,
       
 62109                                  pExpr->flags & EP_AnyAff);
       
 62110       }
       
 62111       break;
       
 62112     }
       
 62113     case TK_INTEGER: {
       
 62114       codeInteger(v, pExpr, 0, target);
       
 62115       break;
       
 62116     }
       
 62117     case TK_FLOAT: {
       
 62118       assert( !ExprHasProperty(pExpr, EP_IntValue) );
       
 62119       codeReal(v, pExpr->u.zToken, 0, target);
       
 62120       break;
       
 62121     }
       
 62122     case TK_STRING: {
       
 62123       assert( !ExprHasProperty(pExpr, EP_IntValue) );
       
 62124       sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
       
 62125       break;
       
 62126     }
       
 62127     case TK_NULL: {
       
 62128       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
       
 62129       break;
       
 62130     }
       
 62131 #ifndef SQLITE_OMIT_BLOB_LITERAL
       
 62132     case TK_BLOB: {
       
 62133       int n;
       
 62134       const char *z;
       
 62135       char *zBlob;
       
 62136       assert( !ExprHasProperty(pExpr, EP_IntValue) );
       
 62137       assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
       
 62138       assert( pExpr->u.zToken[1]=='\'' );
       
 62139       z = &pExpr->u.zToken[2];
       
 62140       n = sqlite3Strlen30(z) - 1;
       
 62141       assert( z[n]=='\'' );
       
 62142       zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
       
 62143       sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
       
 62144       break;
       
 62145     }
       
 62146 #endif
       
 62147     case TK_VARIABLE: {
       
 62148       VdbeOp *pOp;
       
 62149       assert( !ExprHasProperty(pExpr, EP_IntValue) );
       
 62150       assert( pExpr->u.zToken!=0 );
       
 62151       assert( pExpr->u.zToken[0]!=0 );
       
 62152       if( pExpr->u.zToken[1]==0
       
 62153          && (pOp = sqlite3VdbeGetOp(v, -1))->opcode==OP_Variable
       
 62154          && pOp->p1+pOp->p3==pExpr->iTable
       
 62155          && pOp->p2+pOp->p3==target
       
 62156          && pOp->p4.z==0
       
 62157       ){
       
 62158         /* If the previous instruction was a copy of the previous unnamed
       
 62159         ** parameter into the previous register, then simply increment the
       
 62160         ** repeat count on the prior instruction rather than making a new
       
 62161         ** instruction.
       
 62162         */
       
 62163         pOp->p3++;
       
 62164       }else{
       
 62165         sqlite3VdbeAddOp3(v, OP_Variable, pExpr->iTable, target, 1);
       
 62166         if( pExpr->u.zToken[1]!=0 ){
       
 62167           sqlite3VdbeChangeP4(v, -1, pExpr->u.zToken, 0);
       
 62168         }
       
 62169       }
       
 62170       break;
       
 62171     }
       
 62172     case TK_REGISTER: {
       
 62173       inReg = pExpr->iTable;
       
 62174       break;
       
 62175     }
       
 62176     case TK_AS: {
       
 62177       inReg = codeAlias(pParse, pExpr->iTable, pExpr->pLeft, target);
       
 62178       break;
       
 62179     }
       
 62180 #ifndef SQLITE_OMIT_CAST
       
 62181     case TK_CAST: {
       
 62182       /* Expressions of the form:   CAST(pLeft AS token) */
       
 62183       int aff, to_op;
       
 62184       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
       
 62185       assert( !ExprHasProperty(pExpr, EP_IntValue) );
       
 62186       aff = sqlite3AffinityType(pExpr->u.zToken);
       
 62187       to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
       
 62188       assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
       
 62189       assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
       
 62190       assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
       
 62191       assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
       
 62192       assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
       
 62193       testcase( to_op==OP_ToText );
       
 62194       testcase( to_op==OP_ToBlob );
       
 62195       testcase( to_op==OP_ToNumeric );
       
 62196       testcase( to_op==OP_ToInt );
       
 62197       testcase( to_op==OP_ToReal );
       
 62198       if( inReg!=target ){
       
 62199         sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
       
 62200         inReg = target;
       
 62201       }
       
 62202       sqlite3VdbeAddOp1(v, to_op, inReg);
       
 62203       testcase( usedAsColumnCache(pParse, inReg, inReg) );
       
 62204       sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
       
 62205       break;
       
 62206     }
       
 62207 #endif /* SQLITE_OMIT_CAST */
       
 62208     case TK_LT:
       
 62209     case TK_LE:
       
 62210     case TK_GT:
       
 62211     case TK_GE:
       
 62212     case TK_NE:
       
 62213     case TK_EQ: {
       
 62214       assert( TK_LT==OP_Lt );
       
 62215       assert( TK_LE==OP_Le );
       
 62216       assert( TK_GT==OP_Gt );
       
 62217       assert( TK_GE==OP_Ge );
       
 62218       assert( TK_EQ==OP_Eq );
       
 62219       assert( TK_NE==OP_Ne );
       
 62220       testcase( op==TK_LT );
       
 62221       testcase( op==TK_LE );
       
 62222       testcase( op==TK_GT );
       
 62223       testcase( op==TK_GE );
       
 62224       testcase( op==TK_EQ );
       
 62225       testcase( op==TK_NE );
       
 62226       codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
       
 62227                                   pExpr->pRight, &r2, &regFree2);
       
 62228       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
       
 62229                   r1, r2, inReg, SQLITE_STOREP2);
       
 62230       testcase( regFree1==0 );
       
 62231       testcase( regFree2==0 );
       
 62232       break;
       
 62233     }
       
 62234     case TK_IS:
       
 62235     case TK_ISNOT: {
       
 62236       testcase( op==TK_IS );
       
 62237       testcase( op==TK_ISNOT );
       
 62238       codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
       
 62239                                   pExpr->pRight, &r2, &regFree2);
       
 62240       op = (op==TK_IS) ? TK_EQ : TK_NE;
       
 62241       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
       
 62242                   r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
       
 62243       testcase( regFree1==0 );
       
 62244       testcase( regFree2==0 );
       
 62245       break;
       
 62246     }
       
 62247     case TK_AND:
       
 62248     case TK_OR:
       
 62249     case TK_PLUS:
       
 62250     case TK_STAR:
       
 62251     case TK_MINUS:
       
 62252     case TK_REM:
       
 62253     case TK_BITAND:
       
 62254     case TK_BITOR:
       
 62255     case TK_SLASH:
       
 62256     case TK_LSHIFT:
       
 62257     case TK_RSHIFT: 
       
 62258     case TK_CONCAT: {
       
 62259       assert( TK_AND==OP_And );
       
 62260       assert( TK_OR==OP_Or );
       
 62261       assert( TK_PLUS==OP_Add );
       
 62262       assert( TK_MINUS==OP_Subtract );
       
 62263       assert( TK_REM==OP_Remainder );
       
 62264       assert( TK_BITAND==OP_BitAnd );
       
 62265       assert( TK_BITOR==OP_BitOr );
       
 62266       assert( TK_SLASH==OP_Divide );
       
 62267       assert( TK_LSHIFT==OP_ShiftLeft );
       
 62268       assert( TK_RSHIFT==OP_ShiftRight );
       
 62269       assert( TK_CONCAT==OP_Concat );
       
 62270       testcase( op==TK_AND );
       
 62271       testcase( op==TK_OR );
       
 62272       testcase( op==TK_PLUS );
       
 62273       testcase( op==TK_MINUS );
       
 62274       testcase( op==TK_REM );
       
 62275       testcase( op==TK_BITAND );
       
 62276       testcase( op==TK_BITOR );
       
 62277       testcase( op==TK_SLASH );
       
 62278       testcase( op==TK_LSHIFT );
       
 62279       testcase( op==TK_RSHIFT );
       
 62280       testcase( op==TK_CONCAT );
       
 62281       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
       
 62282       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
       
 62283       sqlite3VdbeAddOp3(v, op, r2, r1, target);
       
 62284       testcase( regFree1==0 );
       
 62285       testcase( regFree2==0 );
       
 62286       break;
       
 62287     }
       
 62288     case TK_UMINUS: {
       
 62289       Expr *pLeft = pExpr->pLeft;
       
 62290       assert( pLeft );
       
 62291       if( pLeft->op==TK_FLOAT ){
       
 62292         assert( !ExprHasProperty(pExpr, EP_IntValue) );
       
 62293         codeReal(v, pLeft->u.zToken, 1, target);
       
 62294       }else if( pLeft->op==TK_INTEGER ){
       
 62295         codeInteger(v, pLeft, 1, target);
       
 62296       }else{
       
 62297         regFree1 = r1 = sqlite3GetTempReg(pParse);
       
 62298         sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
       
 62299         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
       
 62300         sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
       
 62301         testcase( regFree2==0 );
       
 62302       }
       
 62303       inReg = target;
       
 62304       break;
       
 62305     }
       
 62306     case TK_BITNOT:
       
 62307     case TK_NOT: {
       
 62308       assert( TK_BITNOT==OP_BitNot );
       
 62309       assert( TK_NOT==OP_Not );
       
 62310       testcase( op==TK_BITNOT );
       
 62311       testcase( op==TK_NOT );
       
 62312       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
       
 62313       testcase( regFree1==0 );
       
 62314       inReg = target;
       
 62315       sqlite3VdbeAddOp2(v, op, r1, inReg);
       
 62316       break;
       
 62317     }
       
 62318     case TK_ISNULL:
       
 62319     case TK_NOTNULL: {
       
 62320       int addr;
       
 62321       assert( TK_ISNULL==OP_IsNull );
       
 62322       assert( TK_NOTNULL==OP_NotNull );
       
 62323       testcase( op==TK_ISNULL );
       
 62324       testcase( op==TK_NOTNULL );
       
 62325       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
       
 62326       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
       
 62327       testcase( regFree1==0 );
       
 62328       addr = sqlite3VdbeAddOp1(v, op, r1);
       
 62329       sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
       
 62330       sqlite3VdbeJumpHere(v, addr);
       
 62331       break;
       
 62332     }
       
 62333     case TK_AGG_FUNCTION: {
       
 62334       AggInfo *pInfo = pExpr->pAggInfo;
       
 62335       if( pInfo==0 ){
       
 62336         assert( !ExprHasProperty(pExpr, EP_IntValue) );
       
 62337         sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
       
 62338       }else{
       
 62339         inReg = pInfo->aFunc[pExpr->iAgg].iMem;
       
 62340       }
       
 62341       break;
       
 62342     }
       
 62343     case TK_CONST_FUNC:
       
 62344     case TK_FUNCTION: {
       
 62345       ExprList *pFarg;       /* List of function arguments */
       
 62346       int nFarg;             /* Number of function arguments */
       
 62347       FuncDef *pDef;         /* The function definition object */
       
 62348       int nId;               /* Length of the function name in bytes */
       
 62349       const char *zId;       /* The function name */
       
 62350       int constMask = 0;     /* Mask of function arguments that are constant */
       
 62351       int i;                 /* Loop counter */
       
 62352       u8 enc = ENC(db);      /* The text encoding used by this database */
       
 62353       CollSeq *pColl = 0;    /* A collating sequence */
       
 62354 
       
 62355       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
       
 62356       testcase( op==TK_CONST_FUNC );
       
 62357       testcase( op==TK_FUNCTION );
       
 62358       if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
       
 62359         pFarg = 0;
       
 62360       }else{
       
 62361         pFarg = pExpr->x.pList;
       
 62362       }
       
 62363       nFarg = pFarg ? pFarg->nExpr : 0;
       
 62364       assert( !ExprHasProperty(pExpr, EP_IntValue) );
       
 62365       zId = pExpr->u.zToken;
       
 62366       nId = sqlite3Strlen30(zId);
       
 62367       pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
       
 62368       if( pDef==0 ){
       
 62369         sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
       
 62370         break;
       
 62371       }
       
 62372       if( pFarg ){
       
 62373         r1 = sqlite3GetTempRange(pParse, nFarg);
       
 62374         sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
       
 62375         sqlite3ExprCodeExprList(pParse, pFarg, r1, 1);
       
 62376         sqlite3ExprCachePop(pParse, 1);   /* Ticket 2ea2425d34be */
       
 62377       }else{
       
 62378         r1 = 0;
       
 62379       }
       
 62380 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 62381       /* Possibly overload the function if the first argument is
       
 62382       ** a virtual table column.
       
 62383       **
       
 62384       ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
       
 62385       ** second argument, not the first, as the argument to test to
       
 62386       ** see if it is a column in a virtual table.  This is done because
       
 62387       ** the left operand of infix functions (the operand we want to
       
 62388       ** control overloading) ends up as the second argument to the
       
 62389       ** function.  The expression "A glob B" is equivalent to 
       
 62390       ** "glob(B,A).  We want to use the A in "A glob B" to test
       
 62391       ** for function overloading.  But we use the B term in "glob(B,A)".
       
 62392       */
       
 62393       if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
       
 62394         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
       
 62395       }else if( nFarg>0 ){
       
 62396         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
       
 62397       }
       
 62398 #endif
       
 62399       for(i=0; i<nFarg; i++){
       
 62400         if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
       
 62401           constMask |= (1<<i);
       
 62402         }
       
 62403         if( (pDef->flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
       
 62404           pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
       
 62405         }
       
 62406       }
       
 62407       if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){
       
 62408         if( !pColl ) pColl = db->pDfltColl; 
       
 62409         sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
       
 62410       }
       
 62411       sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
       
 62412                         (char*)pDef, P4_FUNCDEF);
       
 62413       sqlite3VdbeChangeP5(v, (u8)nFarg);
       
 62414       if( nFarg ){
       
 62415         sqlite3ReleaseTempRange(pParse, r1, nFarg);
       
 62416       }
       
 62417       sqlite3ExprCacheAffinityChange(pParse, r1, nFarg);
       
 62418       break;
       
 62419     }
       
 62420 #ifndef SQLITE_OMIT_SUBQUERY
       
 62421     case TK_EXISTS:
       
 62422     case TK_SELECT: {
       
 62423       testcase( op==TK_EXISTS );
       
 62424       testcase( op==TK_SELECT );
       
 62425       sqlite3CodeSubselect(pParse, pExpr, 0, 0);
       
 62426       inReg = pExpr->iColumn;
       
 62427       break;
       
 62428     }
       
 62429     case TK_IN: {
       
 62430       int rNotFound = 0;
       
 62431       int rMayHaveNull = 0;
       
 62432       int j2, j3, j4, j5;
       
 62433       char affinity;
       
 62434       int eType;
       
 62435 
       
 62436       VdbeNoopComment((v, "begin IN expr r%d", target));
       
 62437       eType = sqlite3FindInIndex(pParse, pExpr, &rMayHaveNull);
       
 62438       if( rMayHaveNull ){
       
 62439         rNotFound = ++pParse->nMem;
       
 62440       }
       
 62441 
       
 62442       /* Figure out the affinity to use to create a key from the results
       
 62443       ** of the expression. affinityStr stores a static string suitable for
       
 62444       ** P4 of OP_MakeRecord.
       
 62445       */
       
 62446       affinity = comparisonAffinity(pExpr);
       
 62447 
       
 62448 
       
 62449       /* Code the <expr> from "<expr> IN (...)". The temporary table
       
 62450       ** pExpr->iTable contains the values that make up the (...) set.
       
 62451       */
       
 62452       sqlite3ExprCachePush(pParse);
       
 62453       sqlite3ExprCode(pParse, pExpr->pLeft, target);
       
 62454       j2 = sqlite3VdbeAddOp1(v, OP_IsNull, target);
       
 62455       if( eType==IN_INDEX_ROWID ){
       
 62456         j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, target);
       
 62457         j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, target);
       
 62458         sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
       
 62459         j5 = sqlite3VdbeAddOp0(v, OP_Goto);
       
 62460         sqlite3VdbeJumpHere(v, j3);
       
 62461         sqlite3VdbeJumpHere(v, j4);
       
 62462         sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
       
 62463       }else{
       
 62464         r2 = regFree2 = sqlite3GetTempReg(pParse);
       
 62465 
       
 62466         /* Create a record and test for set membership. If the set contains
       
 62467         ** the value, then jump to the end of the test code. The target
       
 62468         ** register still contains the true (1) value written to it earlier.
       
 62469         */
       
 62470         sqlite3VdbeAddOp4(v, OP_MakeRecord, target, 1, r2, &affinity, 1);
       
 62471         sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
       
 62472         j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2);
       
 62473 
       
 62474         /* If the set membership test fails, then the result of the 
       
 62475         ** "x IN (...)" expression must be either 0 or NULL. If the set
       
 62476         ** contains no NULL values, then the result is 0. If the set 
       
 62477         ** contains one or more NULL values, then the result of the
       
 62478         ** expression is also NULL.
       
 62479         */
       
 62480         if( rNotFound==0 ){
       
 62481           /* This branch runs if it is known at compile time (now) that 
       
 62482           ** the set contains no NULL values. This happens as the result
       
 62483           ** of a "NOT NULL" constraint in the database schema. No need
       
 62484           ** to test the data structure at runtime in this case.
       
 62485           */
       
 62486           sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
       
 62487         }else{
       
 62488           /* This block populates the rNotFound register with either NULL
       
 62489           ** or 0 (an integer value). If the data structure contains one
       
 62490           ** or more NULLs, then set rNotFound to NULL. Otherwise, set it
       
 62491           ** to 0. If register rMayHaveNull is already set to some value
       
 62492           ** other than NULL, then the test has already been run and 
       
 62493           ** rNotFound is already populated.
       
 62494           */
       
 62495           static const char nullRecord[] = { 0x02, 0x00 };
       
 62496           j3 = sqlite3VdbeAddOp1(v, OP_NotNull, rMayHaveNull);
       
 62497           sqlite3VdbeAddOp2(v, OP_Null, 0, rNotFound);
       
 62498           sqlite3VdbeAddOp4(v, OP_Blob, 2, rMayHaveNull, 0, 
       
 62499                              nullRecord, P4_STATIC);
       
 62500           j4 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, rMayHaveNull);
       
 62501           sqlite3VdbeAddOp2(v, OP_Integer, 0, rNotFound);
       
 62502           sqlite3VdbeJumpHere(v, j4);
       
 62503           sqlite3VdbeJumpHere(v, j3);
       
 62504 
       
 62505           /* Copy the value of register rNotFound (which is either NULL or 0)
       
 62506           ** into the target register. This will be the result of the
       
 62507           ** expression.
       
 62508           */
       
 62509           sqlite3VdbeAddOp2(v, OP_Copy, rNotFound, target);
       
 62510         }
       
 62511       }
       
 62512       sqlite3VdbeJumpHere(v, j2);
       
 62513       sqlite3VdbeJumpHere(v, j5);
       
 62514       sqlite3ExprCachePop(pParse, 1);
       
 62515       VdbeComment((v, "end IN expr r%d", target));
       
 62516       break;
       
 62517     }
       
 62518 #endif
       
 62519     /*
       
 62520     **    x BETWEEN y AND z
       
 62521     **
       
 62522     ** This is equivalent to
       
 62523     **
       
 62524     **    x>=y AND x<=z
       
 62525     **
       
 62526     ** X is stored in pExpr->pLeft.
       
 62527     ** Y is stored in pExpr->pList->a[0].pExpr.
       
 62528     ** Z is stored in pExpr->pList->a[1].pExpr.
       
 62529     */
       
 62530     case TK_BETWEEN: {
       
 62531       Expr *pLeft = pExpr->pLeft;
       
 62532       struct ExprList_item *pLItem = pExpr->x.pList->a;
       
 62533       Expr *pRight = pLItem->pExpr;
       
 62534 
       
 62535       codeCompareOperands(pParse, pLeft, &r1, &regFree1,
       
 62536                                   pRight, &r2, &regFree2);
       
 62537       testcase( regFree1==0 );
       
 62538       testcase( regFree2==0 );
       
 62539       r3 = sqlite3GetTempReg(pParse);
       
 62540       r4 = sqlite3GetTempReg(pParse);
       
 62541       codeCompare(pParse, pLeft, pRight, OP_Ge,
       
 62542                   r1, r2, r3, SQLITE_STOREP2);
       
 62543       pLItem++;
       
 62544       pRight = pLItem->pExpr;
       
 62545       sqlite3ReleaseTempReg(pParse, regFree2);
       
 62546       r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
       
 62547       testcase( regFree2==0 );
       
 62548       codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
       
 62549       sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
       
 62550       sqlite3ReleaseTempReg(pParse, r3);
       
 62551       sqlite3ReleaseTempReg(pParse, r4);
       
 62552       break;
       
 62553     }
       
 62554     case TK_UPLUS: {
       
 62555       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
       
 62556       break;
       
 62557     }
       
 62558 
       
 62559     case TK_TRIGGER: {
       
 62560       /* If the opcode is TK_TRIGGER, then the expression is a reference
       
 62561       ** to a column in the new.* or old.* pseudo-tables available to
       
 62562       ** trigger programs. In this case Expr.iTable is set to 1 for the
       
 62563       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
       
 62564       ** is set to the column of the pseudo-table to read, or to -1 to
       
 62565       ** read the rowid field.
       
 62566       **
       
 62567       ** The expression is implemented using an OP_Param opcode. The p1
       
 62568       ** parameter is set to 0 for an old.rowid reference, or to (i+1)
       
 62569       ** to reference another column of the old.* pseudo-table, where 
       
 62570       ** i is the index of the column. For a new.rowid reference, p1 is
       
 62571       ** set to (n+1), where n is the number of columns in each pseudo-table.
       
 62572       ** For a reference to any other column in the new.* pseudo-table, p1
       
 62573       ** is set to (n+2+i), where n and i are as defined previously. For
       
 62574       ** example, if the table on which triggers are being fired is
       
 62575       ** declared as:
       
 62576       **
       
 62577       **   CREATE TABLE t1(a, b);
       
 62578       **
       
 62579       ** Then p1 is interpreted as follows:
       
 62580       **
       
 62581       **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
       
 62582       **   p1==1   ->    old.a         p1==4   ->    new.a
       
 62583       **   p1==2   ->    old.b         p1==5   ->    new.b       
       
 62584       */
       
 62585       Table *pTab = pExpr->pTab;
       
 62586       int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
       
 62587 
       
 62588       assert( pExpr->iTable==0 || pExpr->iTable==1 );
       
 62589       assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
       
 62590       assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
       
 62591       assert( p1>=0 && p1<(pTab->nCol*2+2) );
       
 62592 
       
 62593       sqlite3VdbeAddOp2(v, OP_Param, p1, target);
       
 62594       VdbeComment((v, "%s.%s -> $%d",
       
 62595         (pExpr->iTable ? "new" : "old"),
       
 62596         (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
       
 62597         target
       
 62598       ));
       
 62599 
       
 62600       /* If the column has REAL affinity, it may currently be stored as an
       
 62601       ** integer. Use OP_RealAffinity to make sure it is really real.  */
       
 62602       if( pExpr->iColumn>=0 
       
 62603        && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
       
 62604       ){
       
 62605         sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
       
 62606       }
       
 62607       break;
       
 62608     }
       
 62609 
       
 62610 
       
 62611     /*
       
 62612     ** Form A:
       
 62613     **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
       
 62614     **
       
 62615     ** Form B:
       
 62616     **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
       
 62617     **
       
 62618     ** Form A is can be transformed into the equivalent form B as follows:
       
 62619     **   CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
       
 62620     **        WHEN x=eN THEN rN ELSE y END
       
 62621     **
       
 62622     ** X (if it exists) is in pExpr->pLeft.
       
 62623     ** Y is in pExpr->pRight.  The Y is also optional.  If there is no
       
 62624     ** ELSE clause and no other term matches, then the result of the
       
 62625     ** exprssion is NULL.
       
 62626     ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
       
 62627     **
       
 62628     ** The result of the expression is the Ri for the first matching Ei,
       
 62629     ** or if there is no matching Ei, the ELSE term Y, or if there is
       
 62630     ** no ELSE term, NULL.
       
 62631     */
       
 62632     default: assert( op==TK_CASE ); {
       
 62633       int endLabel;                     /* GOTO label for end of CASE stmt */
       
 62634       int nextCase;                     /* GOTO label for next WHEN clause */
       
 62635       int nExpr;                        /* 2x number of WHEN terms */
       
 62636       int i;                            /* Loop counter */
       
 62637       ExprList *pEList;                 /* List of WHEN terms */
       
 62638       struct ExprList_item *aListelem;  /* Array of WHEN terms */
       
 62639       Expr opCompare;                   /* The X==Ei expression */
       
 62640       Expr cacheX;                      /* Cached expression X */
       
 62641       Expr *pX;                         /* The X expression */
       
 62642       Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
       
 62643       VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
       
 62644 
       
 62645       assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
       
 62646       assert((pExpr->x.pList->nExpr % 2) == 0);
       
 62647       assert(pExpr->x.pList->nExpr > 0);
       
 62648       pEList = pExpr->x.pList;
       
 62649       aListelem = pEList->a;
       
 62650       nExpr = pEList->nExpr;
       
 62651       endLabel = sqlite3VdbeMakeLabel(v);
       
 62652       if( (pX = pExpr->pLeft)!=0 ){
       
 62653         cacheX = *pX;
       
 62654         testcase( pX->op==TK_COLUMN );
       
 62655         testcase( pX->op==TK_REGISTER );
       
 62656         cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
       
 62657         testcase( regFree1==0 );
       
 62658         cacheX.op = TK_REGISTER;
       
 62659         opCompare.op = TK_EQ;
       
 62660         opCompare.pLeft = &cacheX;
       
 62661         pTest = &opCompare;
       
 62662       }
       
 62663       for(i=0; i<nExpr; i=i+2){
       
 62664         sqlite3ExprCachePush(pParse);
       
 62665         if( pX ){
       
 62666           assert( pTest!=0 );
       
 62667           opCompare.pRight = aListelem[i].pExpr;
       
 62668         }else{
       
 62669           pTest = aListelem[i].pExpr;
       
 62670         }
       
 62671         nextCase = sqlite3VdbeMakeLabel(v);
       
 62672         testcase( pTest->op==TK_COLUMN );
       
 62673         sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
       
 62674         testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
       
 62675         testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
       
 62676         sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
       
 62677         sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
       
 62678         sqlite3ExprCachePop(pParse, 1);
       
 62679         sqlite3VdbeResolveLabel(v, nextCase);
       
 62680       }
       
 62681       if( pExpr->pRight ){
       
 62682         sqlite3ExprCachePush(pParse);
       
 62683         sqlite3ExprCode(pParse, pExpr->pRight, target);
       
 62684         sqlite3ExprCachePop(pParse, 1);
       
 62685       }else{
       
 62686         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
       
 62687       }
       
 62688       assert( db->mallocFailed || pParse->nErr>0 
       
 62689            || pParse->iCacheLevel==iCacheLevel );
       
 62690       sqlite3VdbeResolveLabel(v, endLabel);
       
 62691       break;
       
 62692     }
       
 62693 #ifndef SQLITE_OMIT_TRIGGER
       
 62694     case TK_RAISE: {
       
 62695       assert( pExpr->affinity==OE_Rollback 
       
 62696            || pExpr->affinity==OE_Abort
       
 62697            || pExpr->affinity==OE_Fail
       
 62698            || pExpr->affinity==OE_Ignore
       
 62699       );
       
 62700       if( !pParse->pTriggerTab ){
       
 62701         sqlite3ErrorMsg(pParse,
       
 62702                        "RAISE() may only be used within a trigger-program");
       
 62703         return 0;
       
 62704       }
       
 62705       if( pExpr->affinity==OE_Abort ){
       
 62706         sqlite3MayAbort(pParse);
       
 62707       }
       
 62708       assert( !ExprHasProperty(pExpr, EP_IntValue) );
       
 62709       if( pExpr->affinity==OE_Ignore ){
       
 62710         sqlite3VdbeAddOp4(
       
 62711             v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
       
 62712       }else{
       
 62713         sqlite3HaltConstraint(pParse, pExpr->affinity, pExpr->u.zToken, 0);
       
 62714       }
       
 62715 
       
 62716       break;
       
 62717     }
       
 62718 #endif
       
 62719   }
       
 62720   sqlite3ReleaseTempReg(pParse, regFree1);
       
 62721   sqlite3ReleaseTempReg(pParse, regFree2);
       
 62722   return inReg;
       
 62723 }
       
 62724 
       
 62725 /*
       
 62726 ** Generate code to evaluate an expression and store the results
       
 62727 ** into a register.  Return the register number where the results
       
 62728 ** are stored.
       
 62729 **
       
 62730 ** If the register is a temporary register that can be deallocated,
       
 62731 ** then write its number into *pReg.  If the result register is not
       
 62732 ** a temporary, then set *pReg to zero.
       
 62733 */
       
 62734 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
       
 62735   int r1 = sqlite3GetTempReg(pParse);
       
 62736   int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
       
 62737   if( r2==r1 ){
       
 62738     *pReg = r1;
       
 62739   }else{
       
 62740     sqlite3ReleaseTempReg(pParse, r1);
       
 62741     *pReg = 0;
       
 62742   }
       
 62743   return r2;
       
 62744 }
       
 62745 
       
 62746 /*
       
 62747 ** Generate code that will evaluate expression pExpr and store the
       
 62748 ** results in register target.  The results are guaranteed to appear
       
 62749 ** in register target.
       
 62750 */
       
 62751 SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
       
 62752   int inReg;
       
 62753 
       
 62754   assert( target>0 && target<=pParse->nMem );
       
 62755   inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
       
 62756   assert( pParse->pVdbe || pParse->db->mallocFailed );
       
 62757   if( inReg!=target && pParse->pVdbe ){
       
 62758     sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
       
 62759   }
       
 62760   return target;
       
 62761 }
       
 62762 
       
 62763 /*
       
 62764 ** Generate code that evalutes the given expression and puts the result
       
 62765 ** in register target.
       
 62766 **
       
 62767 ** Also make a copy of the expression results into another "cache" register
       
 62768 ** and modify the expression so that the next time it is evaluated,
       
 62769 ** the result is a copy of the cache register.
       
 62770 **
       
 62771 ** This routine is used for expressions that are used multiple 
       
 62772 ** times.  They are evaluated once and the results of the expression
       
 62773 ** are reused.
       
 62774 */
       
 62775 SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
       
 62776   Vdbe *v = pParse->pVdbe;
       
 62777   int inReg;
       
 62778   inReg = sqlite3ExprCode(pParse, pExpr, target);
       
 62779   assert( target>0 );
       
 62780   /* This routine is called for terms to INSERT or UPDATE.  And the only
       
 62781   ** other place where expressions can be converted into TK_REGISTER is
       
 62782   ** in WHERE clause processing.  So as currently implemented, there is
       
 62783   ** no way for a TK_REGISTER to exist here.  But it seems prudent to
       
 62784   ** keep the ALWAYS() in case the conditions above change with future
       
 62785   ** modifications or enhancements. */
       
 62786   if( ALWAYS(pExpr->op!=TK_REGISTER) ){  
       
 62787     int iMem;
       
 62788     iMem = ++pParse->nMem;
       
 62789     sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
       
 62790     pExpr->iTable = iMem;
       
 62791     pExpr->op = TK_REGISTER;
       
 62792   }
       
 62793   return inReg;
       
 62794 }
       
 62795 
       
 62796 /*
       
 62797 ** Return TRUE if pExpr is an constant expression that is appropriate
       
 62798 ** for factoring out of a loop.  Appropriate expressions are:
       
 62799 **
       
 62800 **    *  Any expression that evaluates to two or more opcodes.
       
 62801 **
       
 62802 **    *  Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, 
       
 62803 **       or OP_Variable that does not need to be placed in a 
       
 62804 **       specific register.
       
 62805 **
       
 62806 ** There is no point in factoring out single-instruction constant
       
 62807 ** expressions that need to be placed in a particular register.  
       
 62808 ** We could factor them out, but then we would end up adding an
       
 62809 ** OP_SCopy instruction to move the value into the correct register
       
 62810 ** later.  We might as well just use the original instruction and
       
 62811 ** avoid the OP_SCopy.
       
 62812 */
       
 62813 static int isAppropriateForFactoring(Expr *p){
       
 62814   if( !sqlite3ExprIsConstantNotJoin(p) ){
       
 62815     return 0;  /* Only constant expressions are appropriate for factoring */
       
 62816   }
       
 62817   if( (p->flags & EP_FixedDest)==0 ){
       
 62818     return 1;  /* Any constant without a fixed destination is appropriate */
       
 62819   }
       
 62820   while( p->op==TK_UPLUS ) p = p->pLeft;
       
 62821   switch( p->op ){
       
 62822 #ifndef SQLITE_OMIT_BLOB_LITERAL
       
 62823     case TK_BLOB:
       
 62824 #endif
       
 62825     case TK_VARIABLE:
       
 62826     case TK_INTEGER:
       
 62827     case TK_FLOAT:
       
 62828     case TK_NULL:
       
 62829     case TK_STRING: {
       
 62830       testcase( p->op==TK_BLOB );
       
 62831       testcase( p->op==TK_VARIABLE );
       
 62832       testcase( p->op==TK_INTEGER );
       
 62833       testcase( p->op==TK_FLOAT );
       
 62834       testcase( p->op==TK_NULL );
       
 62835       testcase( p->op==TK_STRING );
       
 62836       /* Single-instruction constants with a fixed destination are
       
 62837       ** better done in-line.  If we factor them, they will just end
       
 62838       ** up generating an OP_SCopy to move the value to the destination
       
 62839       ** register. */
       
 62840       return 0;
       
 62841     }
       
 62842     case TK_UMINUS: {
       
 62843       if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
       
 62844         return 0;
       
 62845       }
       
 62846       break;
       
 62847     }
       
 62848     default: {
       
 62849       break;
       
 62850     }
       
 62851   }
       
 62852   return 1;
       
 62853 }
       
 62854 
       
 62855 /*
       
 62856 ** If pExpr is a constant expression that is appropriate for
       
 62857 ** factoring out of a loop, then evaluate the expression
       
 62858 ** into a register and convert the expression into a TK_REGISTER
       
 62859 ** expression.
       
 62860 */
       
 62861 static int evalConstExpr(Walker *pWalker, Expr *pExpr){
       
 62862   Parse *pParse = pWalker->pParse;
       
 62863   switch( pExpr->op ){
       
 62864     case TK_REGISTER: {
       
 62865       return WRC_Prune;
       
 62866     }
       
 62867     case TK_FUNCTION:
       
 62868     case TK_AGG_FUNCTION:
       
 62869     case TK_CONST_FUNC: {
       
 62870       /* The arguments to a function have a fixed destination.
       
 62871       ** Mark them this way to avoid generated unneeded OP_SCopy
       
 62872       ** instructions. 
       
 62873       */
       
 62874       ExprList *pList = pExpr->x.pList;
       
 62875       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
       
 62876       if( pList ){
       
 62877         int i = pList->nExpr;
       
 62878         struct ExprList_item *pItem = pList->a;
       
 62879         for(; i>0; i--, pItem++){
       
 62880           if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest;
       
 62881         }
       
 62882       }
       
 62883       break;
       
 62884     }
       
 62885   }
       
 62886   if( isAppropriateForFactoring(pExpr) ){
       
 62887     int r1 = ++pParse->nMem;
       
 62888     int r2;
       
 62889     r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
       
 62890     if( NEVER(r1!=r2) ) sqlite3ReleaseTempReg(pParse, r1);
       
 62891     pExpr->op2 = pExpr->op;
       
 62892     pExpr->op = TK_REGISTER;
       
 62893     pExpr->iTable = r2;
       
 62894     return WRC_Prune;
       
 62895   }
       
 62896   return WRC_Continue;
       
 62897 }
       
 62898 
       
 62899 /*
       
 62900 ** Preevaluate constant subexpressions within pExpr and store the
       
 62901 ** results in registers.  Modify pExpr so that the constant subexpresions
       
 62902 ** are TK_REGISTER opcodes that refer to the precomputed values.
       
 62903 */
       
 62904 SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
       
 62905   Walker w;
       
 62906   w.xExprCallback = evalConstExpr;
       
 62907   w.xSelectCallback = 0;
       
 62908   w.pParse = pParse;
       
 62909   sqlite3WalkExpr(&w, pExpr);
       
 62910 }
       
 62911 
       
 62912 
       
 62913 /*
       
 62914 ** Generate code that pushes the value of every element of the given
       
 62915 ** expression list into a sequence of registers beginning at target.
       
 62916 **
       
 62917 ** Return the number of elements evaluated.
       
 62918 */
       
 62919 SQLITE_PRIVATE int sqlite3ExprCodeExprList(
       
 62920   Parse *pParse,     /* Parsing context */
       
 62921   ExprList *pList,   /* The expression list to be coded */
       
 62922   int target,        /* Where to write results */
       
 62923   int doHardCopy     /* Make a hard copy of every element */
       
 62924 ){
       
 62925   struct ExprList_item *pItem;
       
 62926   int i, n;
       
 62927   assert( pList!=0 );
       
 62928   assert( target>0 );
       
 62929   n = pList->nExpr;
       
 62930   for(pItem=pList->a, i=0; i<n; i++, pItem++){
       
 62931     if( pItem->iAlias ){
       
 62932       int iReg = codeAlias(pParse, pItem->iAlias, pItem->pExpr, target+i);
       
 62933       Vdbe *v = sqlite3GetVdbe(pParse);
       
 62934       if( iReg!=target+i ){
       
 62935         sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target+i);
       
 62936       }
       
 62937     }else{
       
 62938       sqlite3ExprCode(pParse, pItem->pExpr, target+i);
       
 62939     }
       
 62940     if( doHardCopy && !pParse->db->mallocFailed ){
       
 62941       sqlite3ExprHardCopy(pParse, target, n);
       
 62942     }
       
 62943   }
       
 62944   return n;
       
 62945 }
       
 62946 
       
 62947 /*
       
 62948 ** Generate code for a boolean expression such that a jump is made
       
 62949 ** to the label "dest" if the expression is true but execution
       
 62950 ** continues straight thru if the expression is false.
       
 62951 **
       
 62952 ** If the expression evaluates to NULL (neither true nor false), then
       
 62953 ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
       
 62954 **
       
 62955 ** This code depends on the fact that certain token values (ex: TK_EQ)
       
 62956 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
       
 62957 ** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
       
 62958 ** the make process cause these values to align.  Assert()s in the code
       
 62959 ** below verify that the numbers are aligned correctly.
       
 62960 */
       
 62961 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
       
 62962   Vdbe *v = pParse->pVdbe;
       
 62963   int op = 0;
       
 62964   int regFree1 = 0;
       
 62965   int regFree2 = 0;
       
 62966   int r1, r2;
       
 62967 
       
 62968   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
       
 62969   if( NEVER(v==0) )     return;  /* Existance of VDBE checked by caller */
       
 62970   if( NEVER(pExpr==0) ) return;  /* No way this can happen */
       
 62971   op = pExpr->op;
       
 62972   switch( op ){
       
 62973     case TK_AND: {
       
 62974       int d2 = sqlite3VdbeMakeLabel(v);
       
 62975       testcase( jumpIfNull==0 );
       
 62976       sqlite3ExprCachePush(pParse);
       
 62977       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
       
 62978       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
       
 62979       sqlite3VdbeResolveLabel(v, d2);
       
 62980       sqlite3ExprCachePop(pParse, 1);
       
 62981       break;
       
 62982     }
       
 62983     case TK_OR: {
       
 62984       testcase( jumpIfNull==0 );
       
 62985       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
       
 62986       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
       
 62987       break;
       
 62988     }
       
 62989     case TK_NOT: {
       
 62990       testcase( jumpIfNull==0 );
       
 62991       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
       
 62992       break;
       
 62993     }
       
 62994     case TK_LT:
       
 62995     case TK_LE:
       
 62996     case TK_GT:
       
 62997     case TK_GE:
       
 62998     case TK_NE:
       
 62999     case TK_EQ: {
       
 63000       assert( TK_LT==OP_Lt );
       
 63001       assert( TK_LE==OP_Le );
       
 63002       assert( TK_GT==OP_Gt );
       
 63003       assert( TK_GE==OP_Ge );
       
 63004       assert( TK_EQ==OP_Eq );
       
 63005       assert( TK_NE==OP_Ne );
       
 63006       testcase( op==TK_LT );
       
 63007       testcase( op==TK_LE );
       
 63008       testcase( op==TK_GT );
       
 63009       testcase( op==TK_GE );
       
 63010       testcase( op==TK_EQ );
       
 63011       testcase( op==TK_NE );
       
 63012       testcase( jumpIfNull==0 );
       
 63013       codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
       
 63014                                   pExpr->pRight, &r2, &regFree2);
       
 63015       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
       
 63016                   r1, r2, dest, jumpIfNull);
       
 63017       testcase( regFree1==0 );
       
 63018       testcase( regFree2==0 );
       
 63019       break;
       
 63020     }
       
 63021     case TK_IS:
       
 63022     case TK_ISNOT: {
       
 63023       testcase( op==TK_IS );
       
 63024       testcase( op==TK_ISNOT );
       
 63025       codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
       
 63026                                   pExpr->pRight, &r2, &regFree2);
       
 63027       op = (op==TK_IS) ? TK_EQ : TK_NE;
       
 63028       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
       
 63029                   r1, r2, dest, SQLITE_NULLEQ);
       
 63030       testcase( regFree1==0 );
       
 63031       testcase( regFree2==0 );
       
 63032       break;
       
 63033     }
       
 63034     case TK_ISNULL:
       
 63035     case TK_NOTNULL: {
       
 63036       assert( TK_ISNULL==OP_IsNull );
       
 63037       assert( TK_NOTNULL==OP_NotNull );
       
 63038       testcase( op==TK_ISNULL );
       
 63039       testcase( op==TK_NOTNULL );
       
 63040       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
       
 63041       sqlite3VdbeAddOp2(v, op, r1, dest);
       
 63042       testcase( regFree1==0 );
       
 63043       break;
       
 63044     }
       
 63045     case TK_BETWEEN: {
       
 63046       /*    x BETWEEN y AND z
       
 63047       **
       
 63048       ** Is equivalent to 
       
 63049       **
       
 63050       **    x>=y AND x<=z
       
 63051       **
       
 63052       ** Code it as such, taking care to do the common subexpression
       
 63053       ** elementation of x.
       
 63054       */
       
 63055       Expr exprAnd;
       
 63056       Expr compLeft;
       
 63057       Expr compRight;
       
 63058       Expr exprX;
       
 63059 
       
 63060       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
       
 63061       exprX = *pExpr->pLeft;
       
 63062       exprAnd.op = TK_AND;
       
 63063       exprAnd.pLeft = &compLeft;
       
 63064       exprAnd.pRight = &compRight;
       
 63065       compLeft.op = TK_GE;
       
 63066       compLeft.pLeft = &exprX;
       
 63067       compLeft.pRight = pExpr->x.pList->a[0].pExpr;
       
 63068       compRight.op = TK_LE;
       
 63069       compRight.pLeft = &exprX;
       
 63070       compRight.pRight = pExpr->x.pList->a[1].pExpr;
       
 63071       exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
       
 63072       testcase( regFree1==0 );
       
 63073       exprX.op = TK_REGISTER;
       
 63074       testcase( jumpIfNull==0 );
       
 63075       sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
       
 63076       break;
       
 63077     }
       
 63078     default: {
       
 63079       r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
       
 63080       sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
       
 63081       testcase( regFree1==0 );
       
 63082       testcase( jumpIfNull==0 );
       
 63083       break;
       
 63084     }
       
 63085   }
       
 63086   sqlite3ReleaseTempReg(pParse, regFree1);
       
 63087   sqlite3ReleaseTempReg(pParse, regFree2);  
       
 63088 }
       
 63089 
       
 63090 /*
       
 63091 ** Generate code for a boolean expression such that a jump is made
       
 63092 ** to the label "dest" if the expression is false but execution
       
 63093 ** continues straight thru if the expression is true.
       
 63094 **
       
 63095 ** If the expression evaluates to NULL (neither true nor false) then
       
 63096 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
       
 63097 ** is 0.
       
 63098 */
       
 63099 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
       
 63100   Vdbe *v = pParse->pVdbe;
       
 63101   int op = 0;
       
 63102   int regFree1 = 0;
       
 63103   int regFree2 = 0;
       
 63104   int r1, r2;
       
 63105 
       
 63106   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
       
 63107   if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */
       
 63108   if( pExpr==0 )    return;
       
 63109 
       
 63110   /* The value of pExpr->op and op are related as follows:
       
 63111   **
       
 63112   **       pExpr->op            op
       
 63113   **       ---------          ----------
       
 63114   **       TK_ISNULL          OP_NotNull
       
 63115   **       TK_NOTNULL         OP_IsNull
       
 63116   **       TK_NE              OP_Eq
       
 63117   **       TK_EQ              OP_Ne
       
 63118   **       TK_GT              OP_Le
       
 63119   **       TK_LE              OP_Gt
       
 63120   **       TK_GE              OP_Lt
       
 63121   **       TK_LT              OP_Ge
       
 63122   **
       
 63123   ** For other values of pExpr->op, op is undefined and unused.
       
 63124   ** The value of TK_ and OP_ constants are arranged such that we
       
 63125   ** can compute the mapping above using the following expression.
       
 63126   ** Assert()s verify that the computation is correct.
       
 63127   */
       
 63128   op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
       
 63129 
       
 63130   /* Verify correct alignment of TK_ and OP_ constants
       
 63131   */
       
 63132   assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
       
 63133   assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
       
 63134   assert( pExpr->op!=TK_NE || op==OP_Eq );
       
 63135   assert( pExpr->op!=TK_EQ || op==OP_Ne );
       
 63136   assert( pExpr->op!=TK_LT || op==OP_Ge );
       
 63137   assert( pExpr->op!=TK_LE || op==OP_Gt );
       
 63138   assert( pExpr->op!=TK_GT || op==OP_Le );
       
 63139   assert( pExpr->op!=TK_GE || op==OP_Lt );
       
 63140 
       
 63141   switch( pExpr->op ){
       
 63142     case TK_AND: {
       
 63143       testcase( jumpIfNull==0 );
       
 63144       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
       
 63145       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
       
 63146       break;
       
 63147     }
       
 63148     case TK_OR: {
       
 63149       int d2 = sqlite3VdbeMakeLabel(v);
       
 63150       testcase( jumpIfNull==0 );
       
 63151       sqlite3ExprCachePush(pParse);
       
 63152       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
       
 63153       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
       
 63154       sqlite3VdbeResolveLabel(v, d2);
       
 63155       sqlite3ExprCachePop(pParse, 1);
       
 63156       break;
       
 63157     }
       
 63158     case TK_NOT: {
       
 63159       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
       
 63160       break;
       
 63161     }
       
 63162     case TK_LT:
       
 63163     case TK_LE:
       
 63164     case TK_GT:
       
 63165     case TK_GE:
       
 63166     case TK_NE:
       
 63167     case TK_EQ: {
       
 63168       testcase( op==TK_LT );
       
 63169       testcase( op==TK_LE );
       
 63170       testcase( op==TK_GT );
       
 63171       testcase( op==TK_GE );
       
 63172       testcase( op==TK_EQ );
       
 63173       testcase( op==TK_NE );
       
 63174       testcase( jumpIfNull==0 );
       
 63175       codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
       
 63176                                   pExpr->pRight, &r2, &regFree2);
       
 63177       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
       
 63178                   r1, r2, dest, jumpIfNull);
       
 63179       testcase( regFree1==0 );
       
 63180       testcase( regFree2==0 );
       
 63181       break;
       
 63182     }
       
 63183     case TK_IS:
       
 63184     case TK_ISNOT: {
       
 63185       testcase( pExpr->op==TK_IS );
       
 63186       testcase( pExpr->op==TK_ISNOT );
       
 63187       codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
       
 63188                                   pExpr->pRight, &r2, &regFree2);
       
 63189       op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
       
 63190       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
       
 63191                   r1, r2, dest, SQLITE_NULLEQ);
       
 63192       testcase( regFree1==0 );
       
 63193       testcase( regFree2==0 );
       
 63194       break;
       
 63195     }
       
 63196     case TK_ISNULL:
       
 63197     case TK_NOTNULL: {
       
 63198       testcase( op==TK_ISNULL );
       
 63199       testcase( op==TK_NOTNULL );
       
 63200       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
       
 63201       sqlite3VdbeAddOp2(v, op, r1, dest);
       
 63202       testcase( regFree1==0 );
       
 63203       break;
       
 63204     }
       
 63205     case TK_BETWEEN: {
       
 63206       /*    x BETWEEN y AND z
       
 63207       **
       
 63208       ** Is equivalent to 
       
 63209       **
       
 63210       **    x>=y AND x<=z
       
 63211       **
       
 63212       ** Code it as such, taking care to do the common subexpression
       
 63213       ** elementation of x.
       
 63214       */
       
 63215       Expr exprAnd;
       
 63216       Expr compLeft;
       
 63217       Expr compRight;
       
 63218       Expr exprX;
       
 63219 
       
 63220       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
       
 63221       exprX = *pExpr->pLeft;
       
 63222       exprAnd.op = TK_AND;
       
 63223       exprAnd.pLeft = &compLeft;
       
 63224       exprAnd.pRight = &compRight;
       
 63225       compLeft.op = TK_GE;
       
 63226       compLeft.pLeft = &exprX;
       
 63227       compLeft.pRight = pExpr->x.pList->a[0].pExpr;
       
 63228       compRight.op = TK_LE;
       
 63229       compRight.pLeft = &exprX;
       
 63230       compRight.pRight = pExpr->x.pList->a[1].pExpr;
       
 63231       exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
       
 63232       testcase( regFree1==0 );
       
 63233       exprX.op = TK_REGISTER;
       
 63234       testcase( jumpIfNull==0 );
       
 63235       sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
       
 63236       break;
       
 63237     }
       
 63238     default: {
       
 63239       r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
       
 63240       sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
       
 63241       testcase( regFree1==0 );
       
 63242       testcase( jumpIfNull==0 );
       
 63243       break;
       
 63244     }
       
 63245   }
       
 63246   sqlite3ReleaseTempReg(pParse, regFree1);
       
 63247   sqlite3ReleaseTempReg(pParse, regFree2);
       
 63248 }
       
 63249 
       
 63250 /*
       
 63251 ** Do a deep comparison of two expression trees.  Return TRUE (non-zero)
       
 63252 ** if they are identical and return FALSE if they differ in any way.
       
 63253 **
       
 63254 ** Sometimes this routine will return FALSE even if the two expressions
       
 63255 ** really are equivalent.  If we cannot prove that the expressions are
       
 63256 ** identical, we return FALSE just to be safe.  So if this routine
       
 63257 ** returns false, then you do not really know for certain if the two
       
 63258 ** expressions are the same.  But if you get a TRUE return, then you
       
 63259 ** can be sure the expressions are the same.  In the places where
       
 63260 ** this routine is used, it does not hurt to get an extra FALSE - that
       
 63261 ** just might result in some slightly slower code.  But returning
       
 63262 ** an incorrect TRUE could lead to a malfunction.
       
 63263 */
       
 63264 SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB){
       
 63265   int i;
       
 63266   if( pA==0||pB==0 ){
       
 63267     return pB==pA;
       
 63268   }
       
 63269   assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
       
 63270   assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
       
 63271   if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
       
 63272     return 0;
       
 63273   }
       
 63274   if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
       
 63275   if( pA->op!=pB->op ) return 0;
       
 63276   if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
       
 63277   if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
       
 63278 
       
 63279   if( pA->x.pList && pB->x.pList ){
       
 63280     if( pA->x.pList->nExpr!=pB->x.pList->nExpr ) return 0;
       
 63281     for(i=0; i<pA->x.pList->nExpr; i++){
       
 63282       Expr *pExprA = pA->x.pList->a[i].pExpr;
       
 63283       Expr *pExprB = pB->x.pList->a[i].pExpr;
       
 63284       if( !sqlite3ExprCompare(pExprA, pExprB) ) return 0;
       
 63285     }
       
 63286   }else if( pA->x.pList || pB->x.pList ){
       
 63287     return 0;
       
 63288   }
       
 63289 
       
 63290   if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
       
 63291   if( ExprHasProperty(pA, EP_IntValue) ){
       
 63292     if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
       
 63293       return 0;
       
 63294     }
       
 63295   }else if( pA->op!=TK_COLUMN && pA->u.zToken ){
       
 63296     if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 0;
       
 63297     if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ){
       
 63298       return 0;
       
 63299     }
       
 63300   }
       
 63301   return 1;
       
 63302 }
       
 63303 
       
 63304 
       
 63305 /*
       
 63306 ** Add a new element to the pAggInfo->aCol[] array.  Return the index of
       
 63307 ** the new element.  Return a negative number if malloc fails.
       
 63308 */
       
 63309 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
       
 63310   int i;
       
 63311   pInfo->aCol = sqlite3ArrayAllocate(
       
 63312        db,
       
 63313        pInfo->aCol,
       
 63314        sizeof(pInfo->aCol[0]),
       
 63315        3,
       
 63316        &pInfo->nColumn,
       
 63317        &pInfo->nColumnAlloc,
       
 63318        &i
       
 63319   );
       
 63320   return i;
       
 63321 }    
       
 63322 
       
 63323 /*
       
 63324 ** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
       
 63325 ** the new element.  Return a negative number if malloc fails.
       
 63326 */
       
 63327 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
       
 63328   int i;
       
 63329   pInfo->aFunc = sqlite3ArrayAllocate(
       
 63330        db, 
       
 63331        pInfo->aFunc,
       
 63332        sizeof(pInfo->aFunc[0]),
       
 63333        3,
       
 63334        &pInfo->nFunc,
       
 63335        &pInfo->nFuncAlloc,
       
 63336        &i
       
 63337   );
       
 63338   return i;
       
 63339 }    
       
 63340 
       
 63341 /*
       
 63342 ** This is the xExprCallback for a tree walker.  It is used to
       
 63343 ** implement sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
       
 63344 ** for additional information.
       
 63345 */
       
 63346 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
       
 63347   int i;
       
 63348   NameContext *pNC = pWalker->u.pNC;
       
 63349   Parse *pParse = pNC->pParse;
       
 63350   SrcList *pSrcList = pNC->pSrcList;
       
 63351   AggInfo *pAggInfo = pNC->pAggInfo;
       
 63352 
       
 63353   switch( pExpr->op ){
       
 63354     case TK_AGG_COLUMN:
       
 63355     case TK_COLUMN: {
       
 63356       testcase( pExpr->op==TK_AGG_COLUMN );
       
 63357       testcase( pExpr->op==TK_COLUMN );
       
 63358       /* Check to see if the column is in one of the tables in the FROM
       
 63359       ** clause of the aggregate query */
       
 63360       if( ALWAYS(pSrcList!=0) ){
       
 63361         struct SrcList_item *pItem = pSrcList->a;
       
 63362         for(i=0; i<pSrcList->nSrc; i++, pItem++){
       
 63363           struct AggInfo_col *pCol;
       
 63364           assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
       
 63365           if( pExpr->iTable==pItem->iCursor ){
       
 63366             /* If we reach this point, it means that pExpr refers to a table
       
 63367             ** that is in the FROM clause of the aggregate query.  
       
 63368             **
       
 63369             ** Make an entry for the column in pAggInfo->aCol[] if there
       
 63370             ** is not an entry there already.
       
 63371             */
       
 63372             int k;
       
 63373             pCol = pAggInfo->aCol;
       
 63374             for(k=0; k<pAggInfo->nColumn; k++, pCol++){
       
 63375               if( pCol->iTable==pExpr->iTable &&
       
 63376                   pCol->iColumn==pExpr->iColumn ){
       
 63377                 break;
       
 63378               }
       
 63379             }
       
 63380             if( (k>=pAggInfo->nColumn)
       
 63381              && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 
       
 63382             ){
       
 63383               pCol = &pAggInfo->aCol[k];
       
 63384               pCol->pTab = pExpr->pTab;
       
 63385               pCol->iTable = pExpr->iTable;
       
 63386               pCol->iColumn = pExpr->iColumn;
       
 63387               pCol->iMem = ++pParse->nMem;
       
 63388               pCol->iSorterColumn = -1;
       
 63389               pCol->pExpr = pExpr;
       
 63390               if( pAggInfo->pGroupBy ){
       
 63391                 int j, n;
       
 63392                 ExprList *pGB = pAggInfo->pGroupBy;
       
 63393                 struct ExprList_item *pTerm = pGB->a;
       
 63394                 n = pGB->nExpr;
       
 63395                 for(j=0; j<n; j++, pTerm++){
       
 63396                   Expr *pE = pTerm->pExpr;
       
 63397                   if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
       
 63398                       pE->iColumn==pExpr->iColumn ){
       
 63399                     pCol->iSorterColumn = j;
       
 63400                     break;
       
 63401                   }
       
 63402                 }
       
 63403               }
       
 63404               if( pCol->iSorterColumn<0 ){
       
 63405                 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
       
 63406               }
       
 63407             }
       
 63408             /* There is now an entry for pExpr in pAggInfo->aCol[] (either
       
 63409             ** because it was there before or because we just created it).
       
 63410             ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
       
 63411             ** pAggInfo->aCol[] entry.
       
 63412             */
       
 63413             ExprSetIrreducible(pExpr);
       
 63414             pExpr->pAggInfo = pAggInfo;
       
 63415             pExpr->op = TK_AGG_COLUMN;
       
 63416             pExpr->iAgg = (i16)k;
       
 63417             break;
       
 63418           } /* endif pExpr->iTable==pItem->iCursor */
       
 63419         } /* end loop over pSrcList */
       
 63420       }
       
 63421       return WRC_Prune;
       
 63422     }
       
 63423     case TK_AGG_FUNCTION: {
       
 63424       /* The pNC->nDepth==0 test causes aggregate functions in subqueries
       
 63425       ** to be ignored */
       
 63426       if( pNC->nDepth==0 ){
       
 63427         /* Check to see if pExpr is a duplicate of another aggregate 
       
 63428         ** function that is already in the pAggInfo structure
       
 63429         */
       
 63430         struct AggInfo_func *pItem = pAggInfo->aFunc;
       
 63431         for(i=0; i<pAggInfo->nFunc; i++, pItem++){
       
 63432           if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
       
 63433             break;
       
 63434           }
       
 63435         }
       
 63436         if( i>=pAggInfo->nFunc ){
       
 63437           /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
       
 63438           */
       
 63439           u8 enc = ENC(pParse->db);
       
 63440           i = addAggInfoFunc(pParse->db, pAggInfo);
       
 63441           if( i>=0 ){
       
 63442             assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
       
 63443             pItem = &pAggInfo->aFunc[i];
       
 63444             pItem->pExpr = pExpr;
       
 63445             pItem->iMem = ++pParse->nMem;
       
 63446             assert( !ExprHasProperty(pExpr, EP_IntValue) );
       
 63447             pItem->pFunc = sqlite3FindFunction(pParse->db,
       
 63448                    pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
       
 63449                    pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
       
 63450             if( pExpr->flags & EP_Distinct ){
       
 63451               pItem->iDistinct = pParse->nTab++;
       
 63452             }else{
       
 63453               pItem->iDistinct = -1;
       
 63454             }
       
 63455           }
       
 63456         }
       
 63457         /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
       
 63458         */
       
 63459         assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
       
 63460         ExprSetIrreducible(pExpr);
       
 63461         pExpr->iAgg = (i16)i;
       
 63462         pExpr->pAggInfo = pAggInfo;
       
 63463         return WRC_Prune;
       
 63464       }
       
 63465     }
       
 63466   }
       
 63467   return WRC_Continue;
       
 63468 }
       
 63469 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
       
 63470   NameContext *pNC = pWalker->u.pNC;
       
 63471   if( pNC->nDepth==0 ){
       
 63472     pNC->nDepth++;
       
 63473     sqlite3WalkSelect(pWalker, pSelect);
       
 63474     pNC->nDepth--;
       
 63475     return WRC_Prune;
       
 63476   }else{
       
 63477     return WRC_Continue;
       
 63478   }
       
 63479 }
       
 63480 
       
 63481 /*
       
 63482 ** Analyze the given expression looking for aggregate functions and
       
 63483 ** for variables that need to be added to the pParse->aAgg[] array.
       
 63484 ** Make additional entries to the pParse->aAgg[] array as necessary.
       
 63485 **
       
 63486 ** This routine should only be called after the expression has been
       
 63487 ** analyzed by sqlite3ResolveExprNames().
       
 63488 */
       
 63489 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
       
 63490   Walker w;
       
 63491   w.xExprCallback = analyzeAggregate;
       
 63492   w.xSelectCallback = analyzeAggregatesInSelect;
       
 63493   w.u.pNC = pNC;
       
 63494   assert( pNC->pSrcList!=0 );
       
 63495   sqlite3WalkExpr(&w, pExpr);
       
 63496 }
       
 63497 
       
 63498 /*
       
 63499 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an
       
 63500 ** expression list.  Return the number of errors.
       
 63501 **
       
 63502 ** If an error is found, the analysis is cut short.
       
 63503 */
       
 63504 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
       
 63505   struct ExprList_item *pItem;
       
 63506   int i;
       
 63507   if( pList ){
       
 63508     for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
       
 63509       sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
       
 63510     }
       
 63511   }
       
 63512 }
       
 63513 
       
 63514 /*
       
 63515 ** Allocate a single new register for use to hold some intermediate result.
       
 63516 */
       
 63517 SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
       
 63518   if( pParse->nTempReg==0 ){
       
 63519     return ++pParse->nMem;
       
 63520   }
       
 63521   return pParse->aTempReg[--pParse->nTempReg];
       
 63522 }
       
 63523 
       
 63524 /*
       
 63525 ** Deallocate a register, making available for reuse for some other
       
 63526 ** purpose.
       
 63527 **
       
 63528 ** If a register is currently being used by the column cache, then
       
 63529 ** the dallocation is deferred until the column cache line that uses
       
 63530 ** the register becomes stale.
       
 63531 */
       
 63532 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
       
 63533   if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
       
 63534     int i;
       
 63535     struct yColCache *p;
       
 63536     for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
       
 63537       if( p->iReg==iReg ){
       
 63538         p->tempReg = 1;
       
 63539         return;
       
 63540       }
       
 63541     }
       
 63542     pParse->aTempReg[pParse->nTempReg++] = iReg;
       
 63543   }
       
 63544 }
       
 63545 
       
 63546 /*
       
 63547 ** Allocate or deallocate a block of nReg consecutive registers
       
 63548 */
       
 63549 SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
       
 63550   int i, n;
       
 63551   i = pParse->iRangeReg;
       
 63552   n = pParse->nRangeReg;
       
 63553   if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){
       
 63554     pParse->iRangeReg += nReg;
       
 63555     pParse->nRangeReg -= nReg;
       
 63556   }else{
       
 63557     i = pParse->nMem+1;
       
 63558     pParse->nMem += nReg;
       
 63559   }
       
 63560   return i;
       
 63561 }
       
 63562 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
       
 63563   if( nReg>pParse->nRangeReg ){
       
 63564     pParse->nRangeReg = nReg;
       
 63565     pParse->iRangeReg = iReg;
       
 63566   }
       
 63567 }
       
 63568 
       
 63569 /************** End of expr.c ************************************************/
       
 63570 /************** Begin file alter.c *******************************************/
       
 63571 /*
       
 63572 ** 2005 February 15
       
 63573 **
       
 63574 ** The author disclaims copyright to this source code.  In place of
       
 63575 ** a legal notice, here is a blessing:
       
 63576 **
       
 63577 **    May you do good and not evil.
       
 63578 **    May you find forgiveness for yourself and forgive others.
       
 63579 **    May you share freely, never taking more than you give.
       
 63580 **
       
 63581 *************************************************************************
       
 63582 ** This file contains C code routines that used to generate VDBE code
       
 63583 ** that implements the ALTER TABLE command.
       
 63584 **
       
 63585 ** $Id: alter.c,v 1.62 2009/07/24 17:58:53 danielk1977 Exp $
       
 63586 */
       
 63587 
       
 63588 /*
       
 63589 ** The code in this file only exists if we are not omitting the
       
 63590 ** ALTER TABLE logic from the build.
       
 63591 */
       
 63592 #ifndef SQLITE_OMIT_ALTERTABLE
       
 63593 
       
 63594 
       
 63595 /*
       
 63596 ** This function is used by SQL generated to implement the 
       
 63597 ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
       
 63598 ** CREATE INDEX command. The second is a table name. The table name in 
       
 63599 ** the CREATE TABLE or CREATE INDEX statement is replaced with the third
       
 63600 ** argument and the result returned. Examples:
       
 63601 **
       
 63602 ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
       
 63603 **     -> 'CREATE TABLE def(a, b, c)'
       
 63604 **
       
 63605 ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
       
 63606 **     -> 'CREATE INDEX i ON def(a, b, c)'
       
 63607 */
       
 63608 static void renameTableFunc(
       
 63609   sqlite3_context *context,
       
 63610   int NotUsed,
       
 63611   sqlite3_value **argv
       
 63612 ){
       
 63613   unsigned char const *zSql = sqlite3_value_text(argv[0]);
       
 63614   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
       
 63615 
       
 63616   int token;
       
 63617   Token tname;
       
 63618   unsigned char const *zCsr = zSql;
       
 63619   int len = 0;
       
 63620   char *zRet;
       
 63621 
       
 63622   sqlite3 *db = sqlite3_context_db_handle(context);
       
 63623 
       
 63624   UNUSED_PARAMETER(NotUsed);
       
 63625 
       
 63626   /* The principle used to locate the table name in the CREATE TABLE 
       
 63627   ** statement is that the table name is the first non-space token that
       
 63628   ** is immediately followed by a TK_LP or TK_USING token.
       
 63629   */
       
 63630   if( zSql ){
       
 63631     do {
       
 63632       if( !*zCsr ){
       
 63633         /* Ran out of input before finding an opening bracket. Return NULL. */
       
 63634         return;
       
 63635       }
       
 63636 
       
 63637       /* Store the token that zCsr points to in tname. */
       
 63638       tname.z = (char*)zCsr;
       
 63639       tname.n = len;
       
 63640 
       
 63641       /* Advance zCsr to the next token. Store that token type in 'token',
       
 63642       ** and its length in 'len' (to be used next iteration of this loop).
       
 63643       */
       
 63644       do {
       
 63645         zCsr += len;
       
 63646         len = sqlite3GetToken(zCsr, &token);
       
 63647       } while( token==TK_SPACE );
       
 63648       assert( len>0 );
       
 63649     } while( token!=TK_LP && token!=TK_USING );
       
 63650 
       
 63651     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
       
 63652        zTableName, tname.z+tname.n);
       
 63653     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
       
 63654   }
       
 63655 }
       
 63656 
       
 63657 /*
       
 63658 ** This C function implements an SQL user function that is used by SQL code
       
 63659 ** generated by the ALTER TABLE ... RENAME command to modify the definition
       
 63660 ** of any foreign key constraints that use the table being renamed as the 
       
 63661 ** parent table. It is passed three arguments:
       
 63662 **
       
 63663 **   1) The complete text of the CREATE TABLE statement being modified,
       
 63664 **   2) The old name of the table being renamed, and
       
 63665 **   3) The new name of the table being renamed.
       
 63666 **
       
 63667 ** It returns the new CREATE TABLE statement. For example:
       
 63668 **
       
 63669 **   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
       
 63670 **       -> 'CREATE TABLE t1(a REFERENCES t3)'
       
 63671 */
       
 63672 #ifndef SQLITE_OMIT_FOREIGN_KEY
       
 63673 static void renameParentFunc(
       
 63674   sqlite3_context *context,
       
 63675   int NotUsed,
       
 63676   sqlite3_value **argv
       
 63677 ){
       
 63678   sqlite3 *db = sqlite3_context_db_handle(context);
       
 63679   char *zOutput = 0;
       
 63680   char *zResult;
       
 63681   unsigned char const *zInput = sqlite3_value_text(argv[0]);
       
 63682   unsigned char const *zOld = sqlite3_value_text(argv[1]);
       
 63683   unsigned char const *zNew = sqlite3_value_text(argv[2]);
       
 63684 
       
 63685   unsigned const char *z;         /* Pointer to token */
       
 63686   int n;                          /* Length of token z */
       
 63687   int token;                      /* Type of token */
       
 63688 
       
 63689   UNUSED_PARAMETER(NotUsed);
       
 63690   for(z=zInput; *z; z=z+n){
       
 63691     n = sqlite3GetToken(z, &token);
       
 63692     if( token==TK_REFERENCES ){
       
 63693       char *zParent;
       
 63694       do {
       
 63695         z += n;
       
 63696         n = sqlite3GetToken(z, &token);
       
 63697       }while( token==TK_SPACE );
       
 63698 
       
 63699       zParent = sqlite3DbStrNDup(db, (const char *)z, n);
       
 63700       if( zParent==0 ) break;
       
 63701       sqlite3Dequote(zParent);
       
 63702       if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
       
 63703         char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", 
       
 63704             (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew
       
 63705         );
       
 63706         sqlite3DbFree(db, zOutput);
       
 63707         zOutput = zOut;
       
 63708         zInput = &z[n];
       
 63709       }
       
 63710       sqlite3DbFree(db, zParent);
       
 63711     }
       
 63712   }
       
 63713 
       
 63714   zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), 
       
 63715   sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
       
 63716   sqlite3DbFree(db, zOutput);
       
 63717 }
       
 63718 #endif
       
 63719 
       
 63720 #ifndef SQLITE_OMIT_TRIGGER
       
 63721 /* This function is used by SQL generated to implement the
       
 63722 ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER 
       
 63723 ** statement. The second is a table name. The table name in the CREATE 
       
 63724 ** TRIGGER statement is replaced with the third argument and the result 
       
 63725 ** returned. This is analagous to renameTableFunc() above, except for CREATE
       
 63726 ** TRIGGER, not CREATE INDEX and CREATE TABLE.
       
 63727 */
       
 63728 static void renameTriggerFunc(
       
 63729   sqlite3_context *context,
       
 63730   int NotUsed,
       
 63731   sqlite3_value **argv
       
 63732 ){
       
 63733   unsigned char const *zSql = sqlite3_value_text(argv[0]);
       
 63734   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
       
 63735 
       
 63736   int token;
       
 63737   Token tname;
       
 63738   int dist = 3;
       
 63739   unsigned char const *zCsr = zSql;
       
 63740   int len = 0;
       
 63741   char *zRet;
       
 63742   sqlite3 *db = sqlite3_context_db_handle(context);
       
 63743 
       
 63744   UNUSED_PARAMETER(NotUsed);
       
 63745 
       
 63746   /* The principle used to locate the table name in the CREATE TRIGGER 
       
 63747   ** statement is that the table name is the first token that is immediatedly
       
 63748   ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
       
 63749   ** of TK_WHEN, TK_BEGIN or TK_FOR.
       
 63750   */
       
 63751   if( zSql ){
       
 63752     do {
       
 63753 
       
 63754       if( !*zCsr ){
       
 63755         /* Ran out of input before finding the table name. Return NULL. */
       
 63756         return;
       
 63757       }
       
 63758 
       
 63759       /* Store the token that zCsr points to in tname. */
       
 63760       tname.z = (char*)zCsr;
       
 63761       tname.n = len;
       
 63762 
       
 63763       /* Advance zCsr to the next token. Store that token type in 'token',
       
 63764       ** and its length in 'len' (to be used next iteration of this loop).
       
 63765       */
       
 63766       do {
       
 63767         zCsr += len;
       
 63768         len = sqlite3GetToken(zCsr, &token);
       
 63769       }while( token==TK_SPACE );
       
 63770       assert( len>0 );
       
 63771 
       
 63772       /* Variable 'dist' stores the number of tokens read since the most
       
 63773       ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN 
       
 63774       ** token is read and 'dist' equals 2, the condition stated above
       
 63775       ** to be met.
       
 63776       **
       
 63777       ** Note that ON cannot be a database, table or column name, so
       
 63778       ** there is no need to worry about syntax like 
       
 63779       ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
       
 63780       */
       
 63781       dist++;
       
 63782       if( token==TK_DOT || token==TK_ON ){
       
 63783         dist = 0;
       
 63784       }
       
 63785     } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
       
 63786 
       
 63787     /* Variable tname now contains the token that is the old table-name
       
 63788     ** in the CREATE TRIGGER statement.
       
 63789     */
       
 63790     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
       
 63791        zTableName, tname.z+tname.n);
       
 63792     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
       
 63793   }
       
 63794 }
       
 63795 #endif   /* !SQLITE_OMIT_TRIGGER */
       
 63796 
       
 63797 /*
       
 63798 ** Register built-in functions used to help implement ALTER TABLE
       
 63799 */
       
 63800 SQLITE_PRIVATE void sqlite3AlterFunctions(sqlite3 *db){
       
 63801   sqlite3CreateFunc(db, "sqlite_rename_table", 2, SQLITE_UTF8, 0,
       
 63802                          renameTableFunc, 0, 0);
       
 63803 #ifndef SQLITE_OMIT_TRIGGER
       
 63804   sqlite3CreateFunc(db, "sqlite_rename_trigger", 2, SQLITE_UTF8, 0,
       
 63805                          renameTriggerFunc, 0, 0);
       
 63806 #endif
       
 63807 #ifndef SQLITE_OMIT_FOREIGN_KEY
       
 63808   sqlite3CreateFunc(db, "sqlite_rename_parent", 3, SQLITE_UTF8, 0,
       
 63809                          renameParentFunc, 0, 0);
       
 63810 #endif
       
 63811 }
       
 63812 
       
 63813 /*
       
 63814 ** This function is used to create the text of expressions of the form:
       
 63815 **
       
 63816 **   name=<constant1> OR name=<constant2> OR ...
       
 63817 **
       
 63818 ** If argument zWhere is NULL, then a pointer string containing the text 
       
 63819 ** "name=<constant>" is returned, where <constant> is the quoted version
       
 63820 ** of the string passed as argument zConstant. The returned buffer is
       
 63821 ** allocated using sqlite3DbMalloc(). It is the responsibility of the
       
 63822 ** caller to ensure that it is eventually freed.
       
 63823 **
       
 63824 ** If argument zWhere is not NULL, then the string returned is 
       
 63825 ** "<where> OR name=<constant>", where <where> is the contents of zWhere.
       
 63826 ** In this case zWhere is passed to sqlite3DbFree() before returning.
       
 63827 ** 
       
 63828 */
       
 63829 static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
       
 63830   char *zNew;
       
 63831   if( !zWhere ){
       
 63832     zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
       
 63833   }else{
       
 63834     zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
       
 63835     sqlite3DbFree(db, zWhere);
       
 63836   }
       
 63837   return zNew;
       
 63838 }
       
 63839 
       
 63840 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
       
 63841 /*
       
 63842 ** Generate the text of a WHERE expression which can be used to select all
       
 63843 ** tables that have foreign key constraints that refer to table pTab (i.e.
       
 63844 ** constraints for which pTab is the parent table) from the sqlite_master
       
 63845 ** table.
       
 63846 */
       
 63847 static char *whereForeignKeys(Parse *pParse, Table *pTab){
       
 63848   FKey *p;
       
 63849   char *zWhere = 0;
       
 63850   for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
       
 63851     zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
       
 63852   }
       
 63853   return zWhere;
       
 63854 }
       
 63855 #endif
       
 63856 
       
 63857 /*
       
 63858 ** Generate the text of a WHERE expression which can be used to select all
       
 63859 ** temporary triggers on table pTab from the sqlite_temp_master table. If
       
 63860 ** table pTab has no temporary triggers, or is itself stored in the 
       
 63861 ** temporary database, NULL is returned.
       
 63862 */
       
 63863 static char *whereTempTriggers(Parse *pParse, Table *pTab){
       
 63864   Trigger *pTrig;
       
 63865   char *zWhere = 0;
       
 63866   const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
       
 63867 
       
 63868   /* If the table is not located in the temp-db (in which case NULL is 
       
 63869   ** returned, loop through the tables list of triggers. For each trigger
       
 63870   ** that is not part of the temp-db schema, add a clause to the WHERE 
       
 63871   ** expression being built up in zWhere.
       
 63872   */
       
 63873   if( pTab->pSchema!=pTempSchema ){
       
 63874     sqlite3 *db = pParse->db;
       
 63875     for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
       
 63876       if( pTrig->pSchema==pTempSchema ){
       
 63877         zWhere = whereOrName(db, zWhere, pTrig->zName);
       
 63878       }
       
 63879     }
       
 63880   }
       
 63881   return zWhere;
       
 63882 }
       
 63883 
       
 63884 /*
       
 63885 ** Generate code to drop and reload the internal representation of table
       
 63886 ** pTab from the database, including triggers and temporary triggers.
       
 63887 ** Argument zName is the name of the table in the database schema at
       
 63888 ** the time the generated code is executed. This can be different from
       
 63889 ** pTab->zName if this function is being called to code part of an 
       
 63890 ** "ALTER TABLE RENAME TO" statement.
       
 63891 */
       
 63892 static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
       
 63893   Vdbe *v;
       
 63894   char *zWhere;
       
 63895   int iDb;                   /* Index of database containing pTab */
       
 63896 #ifndef SQLITE_OMIT_TRIGGER
       
 63897   Trigger *pTrig;
       
 63898 #endif
       
 63899 
       
 63900   v = sqlite3GetVdbe(pParse);
       
 63901   if( NEVER(v==0) ) return;
       
 63902   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
       
 63903   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
       
 63904   assert( iDb>=0 );
       
 63905 
       
 63906 #ifndef SQLITE_OMIT_TRIGGER
       
 63907   /* Drop any table triggers from the internal schema. */
       
 63908   for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
       
 63909     int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
       
 63910     assert( iTrigDb==iDb || iTrigDb==1 );
       
 63911     sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
       
 63912   }
       
 63913 #endif
       
 63914 
       
 63915   /* Drop the table and index from the internal schema.  */
       
 63916   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
       
 63917 
       
 63918   /* Reload the table, index and permanent trigger schemas. */
       
 63919   zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
       
 63920   if( !zWhere ) return;
       
 63921   sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
       
 63922 
       
 63923 #ifndef SQLITE_OMIT_TRIGGER
       
 63924   /* Now, if the table is not stored in the temp database, reload any temp 
       
 63925   ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. 
       
 63926   */
       
 63927   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
       
 63928     sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC);
       
 63929   }
       
 63930 #endif
       
 63931 }
       
 63932 
       
 63933 /*
       
 63934 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" 
       
 63935 ** command. 
       
 63936 */
       
 63937 SQLITE_PRIVATE void sqlite3AlterRenameTable(
       
 63938   Parse *pParse,            /* Parser context. */
       
 63939   SrcList *pSrc,            /* The table to rename. */
       
 63940   Token *pName              /* The new table name. */
       
 63941 ){
       
 63942   int iDb;                  /* Database that contains the table */
       
 63943   char *zDb;                /* Name of database iDb */
       
 63944   Table *pTab;              /* Table being renamed */
       
 63945   char *zName = 0;          /* NULL-terminated version of pName */ 
       
 63946   sqlite3 *db = pParse->db; /* Database connection */
       
 63947   int nTabName;             /* Number of UTF-8 characters in zTabName */
       
 63948   const char *zTabName;     /* Original name of the table */
       
 63949   Vdbe *v;
       
 63950 #ifndef SQLITE_OMIT_TRIGGER
       
 63951   char *zWhere = 0;         /* Where clause to locate temp triggers */
       
 63952 #endif
       
 63953   VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
       
 63954   
       
 63955   if( NEVER(db->mallocFailed) ) goto exit_rename_table;
       
 63956   assert( pSrc->nSrc==1 );
       
 63957   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
       
 63958 
       
 63959   pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
       
 63960   if( !pTab ) goto exit_rename_table;
       
 63961   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
       
 63962   zDb = db->aDb[iDb].zName;
       
 63963 
       
 63964   /* Get a NULL terminated version of the new table name. */
       
 63965   zName = sqlite3NameFromToken(db, pName);
       
 63966   if( !zName ) goto exit_rename_table;
       
 63967 
       
 63968   /* Check that a table or index named 'zName' does not already exist
       
 63969   ** in database iDb. If so, this is an error.
       
 63970   */
       
 63971   if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
       
 63972     sqlite3ErrorMsg(pParse, 
       
 63973         "there is already another table or index with this name: %s", zName);
       
 63974     goto exit_rename_table;
       
 63975   }
       
 63976 
       
 63977   /* Make sure it is not a system table being altered, or a reserved name
       
 63978   ** that the table is being renamed to.
       
 63979   */
       
 63980   if( sqlite3Strlen30(pTab->zName)>6 
       
 63981    && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
       
 63982   ){
       
 63983     sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
       
 63984     goto exit_rename_table;
       
 63985   }
       
 63986   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
       
 63987     goto exit_rename_table;
       
 63988   }
       
 63989 
       
 63990 #ifndef SQLITE_OMIT_VIEW
       
 63991   if( pTab->pSelect ){
       
 63992     sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
       
 63993     goto exit_rename_table;
       
 63994   }
       
 63995 #endif
       
 63996 
       
 63997 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 63998   /* Invoke the authorization callback. */
       
 63999   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
       
 64000     goto exit_rename_table;
       
 64001   }
       
 64002 #endif
       
 64003 
       
 64004 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 64005   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
       
 64006     goto exit_rename_table;
       
 64007   }
       
 64008   if( IsVirtual(pTab) ){
       
 64009     pVTab = sqlite3GetVTable(db, pTab);
       
 64010     if( pVTab->pVtab->pModule->xRename==0 ){
       
 64011       pVTab = 0;
       
 64012     }
       
 64013   }
       
 64014 #endif
       
 64015 
       
 64016   /* Begin a transaction and code the VerifyCookie for database iDb. 
       
 64017   ** Then modify the schema cookie (since the ALTER TABLE modifies the
       
 64018   ** schema). Open a statement transaction if the table is a virtual
       
 64019   ** table.
       
 64020   */
       
 64021   v = sqlite3GetVdbe(pParse);
       
 64022   if( v==0 ){
       
 64023     goto exit_rename_table;
       
 64024   }
       
 64025   sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
       
 64026   sqlite3ChangeCookie(pParse, iDb);
       
 64027 
       
 64028   /* If this is a virtual table, invoke the xRename() function if
       
 64029   ** one is defined. The xRename() callback will modify the names
       
 64030   ** of any resources used by the v-table implementation (including other
       
 64031   ** SQLite tables) that are identified by the name of the virtual table.
       
 64032   */
       
 64033 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 64034   if( pVTab ){
       
 64035     int i = ++pParse->nMem;
       
 64036     sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
       
 64037     sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
       
 64038     sqlite3MayAbort(pParse);
       
 64039   }
       
 64040 #endif
       
 64041 
       
 64042   /* figure out how many UTF-8 characters are in zName */
       
 64043   zTabName = pTab->zName;
       
 64044   nTabName = sqlite3Utf8CharLen(zTabName, -1);
       
 64045 
       
 64046 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
       
 64047   if( db->flags&SQLITE_ForeignKeys ){
       
 64048     /* If foreign-key support is enabled, rewrite the CREATE TABLE 
       
 64049     ** statements corresponding to all child tables of foreign key constraints
       
 64050     ** for which the renamed table is the parent table.  */
       
 64051     if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
       
 64052       sqlite3NestedParse(pParse, 
       
 64053           "UPDATE sqlite_master SET "
       
 64054               "sql = sqlite_rename_parent(sql, %Q, %Q) "
       
 64055               "WHERE %s;", zTabName, zName, zWhere);
       
 64056       sqlite3DbFree(db, zWhere);
       
 64057     }
       
 64058   }
       
 64059 #endif
       
 64060 
       
 64061   /* Modify the sqlite_master table to use the new table name. */
       
 64062   sqlite3NestedParse(pParse,
       
 64063       "UPDATE %Q.%s SET "
       
 64064 #ifdef SQLITE_OMIT_TRIGGER
       
 64065           "sql = sqlite_rename_table(sql, %Q), "
       
 64066 #else
       
 64067           "sql = CASE "
       
 64068             "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
       
 64069             "ELSE sqlite_rename_table(sql, %Q) END, "
       
 64070 #endif
       
 64071           "tbl_name = %Q, "
       
 64072           "name = CASE "
       
 64073             "WHEN type='table' THEN %Q "
       
 64074             "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
       
 64075              "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
       
 64076             "ELSE name END "
       
 64077       "WHERE tbl_name=%Q AND "
       
 64078           "(type='table' OR type='index' OR type='trigger');", 
       
 64079       zDb, SCHEMA_TABLE(iDb), zName, zName, zName, 
       
 64080 #ifndef SQLITE_OMIT_TRIGGER
       
 64081       zName,
       
 64082 #endif
       
 64083       zName, nTabName, zTabName
       
 64084   );
       
 64085 
       
 64086 #ifndef SQLITE_OMIT_AUTOINCREMENT
       
 64087   /* If the sqlite_sequence table exists in this database, then update 
       
 64088   ** it with the new table name.
       
 64089   */
       
 64090   if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
       
 64091     sqlite3NestedParse(pParse,
       
 64092         "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
       
 64093         zDb, zName, pTab->zName);
       
 64094   }
       
 64095 #endif
       
 64096 
       
 64097 #ifndef SQLITE_OMIT_TRIGGER
       
 64098   /* If there are TEMP triggers on this table, modify the sqlite_temp_master
       
 64099   ** table. Don't do this if the table being ALTERed is itself located in
       
 64100   ** the temp database.
       
 64101   */
       
 64102   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
       
 64103     sqlite3NestedParse(pParse, 
       
 64104         "UPDATE sqlite_temp_master SET "
       
 64105             "sql = sqlite_rename_trigger(sql, %Q), "
       
 64106             "tbl_name = %Q "
       
 64107             "WHERE %s;", zName, zName, zWhere);
       
 64108     sqlite3DbFree(db, zWhere);
       
 64109   }
       
 64110 #endif
       
 64111 
       
 64112 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
       
 64113   if( db->flags&SQLITE_ForeignKeys ){
       
 64114     FKey *p;
       
 64115     for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
       
 64116       Table *pFrom = p->pFrom;
       
 64117       if( pFrom!=pTab ){
       
 64118         reloadTableSchema(pParse, p->pFrom, pFrom->zName);
       
 64119       }
       
 64120     }
       
 64121   }
       
 64122 #endif
       
 64123 
       
 64124   /* Drop and reload the internal table schema. */
       
 64125   reloadTableSchema(pParse, pTab, zName);
       
 64126 
       
 64127 exit_rename_table:
       
 64128   sqlite3SrcListDelete(db, pSrc);
       
 64129   sqlite3DbFree(db, zName);
       
 64130 }
       
 64131 
       
 64132 
       
 64133 /*
       
 64134 ** Generate code to make sure the file format number is at least minFormat.
       
 64135 ** The generated code will increase the file format number if necessary.
       
 64136 */
       
 64137 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
       
 64138   Vdbe *v;
       
 64139   v = sqlite3GetVdbe(pParse);
       
 64140   /* The VDBE should have been allocated before this routine is called.
       
 64141   ** If that allocation failed, we would have quit before reaching this
       
 64142   ** point */
       
 64143   if( ALWAYS(v) ){
       
 64144     int r1 = sqlite3GetTempReg(pParse);
       
 64145     int r2 = sqlite3GetTempReg(pParse);
       
 64146     int j1;
       
 64147     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
       
 64148     sqlite3VdbeUsesBtree(v, iDb);
       
 64149     sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
       
 64150     j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
       
 64151     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
       
 64152     sqlite3VdbeJumpHere(v, j1);
       
 64153     sqlite3ReleaseTempReg(pParse, r1);
       
 64154     sqlite3ReleaseTempReg(pParse, r2);
       
 64155   }
       
 64156 }
       
 64157 
       
 64158 /*
       
 64159 ** This function is called after an "ALTER TABLE ... ADD" statement
       
 64160 ** has been parsed. Argument pColDef contains the text of the new
       
 64161 ** column definition.
       
 64162 **
       
 64163 ** The Table structure pParse->pNewTable was extended to include
       
 64164 ** the new column during parsing.
       
 64165 */
       
 64166 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
       
 64167   Table *pNew;              /* Copy of pParse->pNewTable */
       
 64168   Table *pTab;              /* Table being altered */
       
 64169   int iDb;                  /* Database number */
       
 64170   const char *zDb;          /* Database name */
       
 64171   const char *zTab;         /* Table name */
       
 64172   char *zCol;               /* Null-terminated column definition */
       
 64173   Column *pCol;             /* The new column */
       
 64174   Expr *pDflt;              /* Default value for the new column */
       
 64175   sqlite3 *db;              /* The database connection; */
       
 64176 
       
 64177   db = pParse->db;
       
 64178   if( pParse->nErr || db->mallocFailed ) return;
       
 64179   pNew = pParse->pNewTable;
       
 64180   assert( pNew );
       
 64181 
       
 64182   assert( sqlite3BtreeHoldsAllMutexes(db) );
       
 64183   iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
       
 64184   zDb = db->aDb[iDb].zName;
       
 64185   zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
       
 64186   pCol = &pNew->aCol[pNew->nCol-1];
       
 64187   pDflt = pCol->pDflt;
       
 64188   pTab = sqlite3FindTable(db, zTab, zDb);
       
 64189   assert( pTab );
       
 64190 
       
 64191 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 64192   /* Invoke the authorization callback. */
       
 64193   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
       
 64194     return;
       
 64195   }
       
 64196 #endif
       
 64197 
       
 64198   /* If the default value for the new column was specified with a 
       
 64199   ** literal NULL, then set pDflt to 0. This simplifies checking
       
 64200   ** for an SQL NULL default below.
       
 64201   */
       
 64202   if( pDflt && pDflt->op==TK_NULL ){
       
 64203     pDflt = 0;
       
 64204   }
       
 64205 
       
 64206   /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
       
 64207   ** If there is a NOT NULL constraint, then the default value for the
       
 64208   ** column must not be NULL.
       
 64209   */
       
 64210   if( pCol->isPrimKey ){
       
 64211     sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
       
 64212     return;
       
 64213   }
       
 64214   if( pNew->pIndex ){
       
 64215     sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
       
 64216     return;
       
 64217   }
       
 64218   if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
       
 64219     sqlite3ErrorMsg(pParse, 
       
 64220         "Cannot add a REFERENCES column with non-NULL default value");
       
 64221     return;
       
 64222   }
       
 64223   if( pCol->notNull && !pDflt ){
       
 64224     sqlite3ErrorMsg(pParse, 
       
 64225         "Cannot add a NOT NULL column with default value NULL");
       
 64226     return;
       
 64227   }
       
 64228 
       
 64229   /* Ensure the default expression is something that sqlite3ValueFromExpr()
       
 64230   ** can handle (i.e. not CURRENT_TIME etc.)
       
 64231   */
       
 64232   if( pDflt ){
       
 64233     sqlite3_value *pVal;
       
 64234     if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
       
 64235       db->mallocFailed = 1;
       
 64236       return;
       
 64237     }
       
 64238     if( !pVal ){
       
 64239       sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
       
 64240       return;
       
 64241     }
       
 64242     sqlite3ValueFree(pVal);
       
 64243   }
       
 64244 
       
 64245   /* Modify the CREATE TABLE statement. */
       
 64246   zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
       
 64247   if( zCol ){
       
 64248     char *zEnd = &zCol[pColDef->n-1];
       
 64249     while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
       
 64250       *zEnd-- = '\0';
       
 64251     }
       
 64252     sqlite3NestedParse(pParse, 
       
 64253         "UPDATE \"%w\".%s SET "
       
 64254           "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
       
 64255         "WHERE type = 'table' AND name = %Q", 
       
 64256       zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
       
 64257       zTab
       
 64258     );
       
 64259     sqlite3DbFree(db, zCol);
       
 64260   }
       
 64261 
       
 64262   /* If the default value of the new column is NULL, then set the file
       
 64263   ** format to 2. If the default value of the new column is not NULL,
       
 64264   ** the file format becomes 3.
       
 64265   */
       
 64266   sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
       
 64267 
       
 64268   /* Reload the schema of the modified table. */
       
 64269   reloadTableSchema(pParse, pTab, pTab->zName);
       
 64270 }
       
 64271 
       
 64272 /*
       
 64273 ** This function is called by the parser after the table-name in
       
 64274 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument 
       
 64275 ** pSrc is the full-name of the table being altered.
       
 64276 **
       
 64277 ** This routine makes a (partial) copy of the Table structure
       
 64278 ** for the table being altered and sets Parse.pNewTable to point
       
 64279 ** to it. Routines called by the parser as the column definition
       
 64280 ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to 
       
 64281 ** the copy. The copy of the Table structure is deleted by tokenize.c 
       
 64282 ** after parsing is finished.
       
 64283 **
       
 64284 ** Routine sqlite3AlterFinishAddColumn() will be called to complete
       
 64285 ** coding the "ALTER TABLE ... ADD" statement.
       
 64286 */
       
 64287 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
       
 64288   Table *pNew;
       
 64289   Table *pTab;
       
 64290   Vdbe *v;
       
 64291   int iDb;
       
 64292   int i;
       
 64293   int nAlloc;
       
 64294   sqlite3 *db = pParse->db;
       
 64295 
       
 64296   /* Look up the table being altered. */
       
 64297   assert( pParse->pNewTable==0 );
       
 64298   assert( sqlite3BtreeHoldsAllMutexes(db) );
       
 64299   if( db->mallocFailed ) goto exit_begin_add_column;
       
 64300   pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
       
 64301   if( !pTab ) goto exit_begin_add_column;
       
 64302 
       
 64303 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 64304   if( IsVirtual(pTab) ){
       
 64305     sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
       
 64306     goto exit_begin_add_column;
       
 64307   }
       
 64308 #endif
       
 64309 
       
 64310   /* Make sure this is not an attempt to ALTER a view. */
       
 64311   if( pTab->pSelect ){
       
 64312     sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
       
 64313     goto exit_begin_add_column;
       
 64314   }
       
 64315 
       
 64316   assert( pTab->addColOffset>0 );
       
 64317   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       
 64318 
       
 64319   /* Put a copy of the Table struct in Parse.pNewTable for the
       
 64320   ** sqlite3AddColumn() function and friends to modify.  But modify
       
 64321   ** the name by adding an "sqlite_altertab_" prefix.  By adding this
       
 64322   ** prefix, we insure that the name will not collide with an existing
       
 64323   ** table because user table are not allowed to have the "sqlite_"
       
 64324   ** prefix on their name.
       
 64325   */
       
 64326   pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
       
 64327   if( !pNew ) goto exit_begin_add_column;
       
 64328   pParse->pNewTable = pNew;
       
 64329   pNew->nRef = 1;
       
 64330   pNew->dbMem = pTab->dbMem;
       
 64331   pNew->nCol = pTab->nCol;
       
 64332   assert( pNew->nCol>0 );
       
 64333   nAlloc = (((pNew->nCol-1)/8)*8)+8;
       
 64334   assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
       
 64335   pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
       
 64336   pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
       
 64337   if( !pNew->aCol || !pNew->zName ){
       
 64338     db->mallocFailed = 1;
       
 64339     goto exit_begin_add_column;
       
 64340   }
       
 64341   memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
       
 64342   for(i=0; i<pNew->nCol; i++){
       
 64343     Column *pCol = &pNew->aCol[i];
       
 64344     pCol->zName = sqlite3DbStrDup(db, pCol->zName);
       
 64345     pCol->zColl = 0;
       
 64346     pCol->zType = 0;
       
 64347     pCol->pDflt = 0;
       
 64348     pCol->zDflt = 0;
       
 64349   }
       
 64350   pNew->pSchema = db->aDb[iDb].pSchema;
       
 64351   pNew->addColOffset = pTab->addColOffset;
       
 64352   pNew->nRef = 1;
       
 64353 
       
 64354   /* Begin a transaction and increment the schema cookie.  */
       
 64355   sqlite3BeginWriteOperation(pParse, 0, iDb);
       
 64356   v = sqlite3GetVdbe(pParse);
       
 64357   if( !v ) goto exit_begin_add_column;
       
 64358   sqlite3ChangeCookie(pParse, iDb);
       
 64359 
       
 64360 exit_begin_add_column:
       
 64361   sqlite3SrcListDelete(db, pSrc);
       
 64362   return;
       
 64363 }
       
 64364 #endif  /* SQLITE_ALTER_TABLE */
       
 64365 
       
 64366 /************** End of alter.c ***********************************************/
       
 64367 /************** Begin file analyze.c *****************************************/
       
 64368 /*
       
 64369 ** 2005 July 8
       
 64370 **
       
 64371 ** The author disclaims copyright to this source code.  In place of
       
 64372 ** a legal notice, here is a blessing:
       
 64373 **
       
 64374 **    May you do good and not evil.
       
 64375 **    May you find forgiveness for yourself and forgive others.
       
 64376 **    May you share freely, never taking more than you give.
       
 64377 **
       
 64378 *************************************************************************
       
 64379 ** This file contains code associated with the ANALYZE command.
       
 64380 **
       
 64381 ** @(#) $Id: analyze.c,v 1.52 2009/04/16 17:45:48 drh Exp $
       
 64382 */
       
 64383 #ifndef SQLITE_OMIT_ANALYZE
       
 64384 
       
 64385 /*
       
 64386 ** This routine generates code that opens the sqlite_stat1 table for
       
 64387 ** writing with cursor iStatCur. If the library was built with the
       
 64388 ** SQLITE_ENABLE_STAT2 macro defined, then the sqlite_stat2 table is
       
 64389 ** opened for writing using cursor (iStatCur+1)
       
 64390 **
       
 64391 ** If the sqlite_stat1 tables does not previously exist, it is created.
       
 64392 ** Similarly, if the sqlite_stat2 table does not exist and the library
       
 64393 ** is compiled with SQLITE_ENABLE_STAT2 defined, it is created. 
       
 64394 **
       
 64395 ** Argument zWhere may be a pointer to a buffer containing a table name,
       
 64396 ** or it may be a NULL pointer. If it is not NULL, then all entries in
       
 64397 ** the sqlite_stat1 and (if applicable) sqlite_stat2 tables associated
       
 64398 ** with the named table are deleted. If zWhere==0, then code is generated
       
 64399 ** to delete all stat table entries.
       
 64400 */
       
 64401 static void openStatTable(
       
 64402   Parse *pParse,          /* Parsing context */
       
 64403   int iDb,                /* The database we are looking in */
       
 64404   int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
       
 64405   const char *zWhere      /* Delete entries associated with this table */
       
 64406 ){
       
 64407   static struct {
       
 64408     const char *zName;
       
 64409     const char *zCols;
       
 64410   } aTable[] = {
       
 64411     { "sqlite_stat1", "tbl,idx,stat" },
       
 64412 #ifdef SQLITE_ENABLE_STAT2
       
 64413     { "sqlite_stat2", "tbl,idx,sampleno,sample" },
       
 64414 #endif
       
 64415   };
       
 64416 
       
 64417   int aRoot[] = {0, 0};
       
 64418   u8 aCreateTbl[] = {0, 0};
       
 64419 
       
 64420   int i;
       
 64421   sqlite3 *db = pParse->db;
       
 64422   Db *pDb;
       
 64423   Vdbe *v = sqlite3GetVdbe(pParse);
       
 64424   if( v==0 ) return;
       
 64425   assert( sqlite3BtreeHoldsAllMutexes(db) );
       
 64426   assert( sqlite3VdbeDb(v)==db );
       
 64427   pDb = &db->aDb[iDb];
       
 64428 
       
 64429   for(i=0; i<ArraySize(aTable); i++){
       
 64430     const char *zTab = aTable[i].zName;
       
 64431     Table *pStat;
       
 64432     if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
       
 64433       /* The sqlite_stat[12] table does not exist. Create it. Note that a 
       
 64434       ** side-effect of the CREATE TABLE statement is to leave the rootpage 
       
 64435       ** of the new table in register pParse->regRoot. This is important 
       
 64436       ** because the OpenWrite opcode below will be needing it. */
       
 64437       sqlite3NestedParse(pParse,
       
 64438           "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
       
 64439       );
       
 64440       aRoot[i] = pParse->regRoot;
       
 64441       aCreateTbl[i] = 1;
       
 64442     }else{
       
 64443       /* The table already exists. If zWhere is not NULL, delete all entries 
       
 64444       ** associated with the table zWhere. If zWhere is NULL, delete the
       
 64445       ** entire contents of the table. */
       
 64446       aRoot[i] = pStat->tnum;
       
 64447       sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
       
 64448       if( zWhere ){
       
 64449         sqlite3NestedParse(pParse,
       
 64450            "DELETE FROM %Q.%s WHERE tbl=%Q", pDb->zName, zTab, zWhere
       
 64451         );
       
 64452       }else{
       
 64453         /* The sqlite_stat[12] table already exists.  Delete all rows. */
       
 64454         sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
       
 64455       }
       
 64456     }
       
 64457   }
       
 64458 
       
 64459   /* Open the sqlite_stat[12] tables for writing. */
       
 64460   for(i=0; i<ArraySize(aTable); i++){
       
 64461     sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
       
 64462     sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
       
 64463     sqlite3VdbeChangeP5(v, aCreateTbl[i]);
       
 64464   }
       
 64465 }
       
 64466 
       
 64467 /*
       
 64468 ** Generate code to do an analysis of all indices associated with
       
 64469 ** a single table.
       
 64470 */
       
 64471 static void analyzeOneTable(
       
 64472   Parse *pParse,   /* Parser context */
       
 64473   Table *pTab,     /* Table whose indices are to be analyzed */
       
 64474   int iStatCur,    /* Index of VdbeCursor that writes the sqlite_stat1 table */
       
 64475   int iMem         /* Available memory locations begin here */
       
 64476 ){
       
 64477   sqlite3 *db = pParse->db;    /* Database handle */
       
 64478   Index *pIdx;                 /* An index to being analyzed */
       
 64479   int iIdxCur;                 /* Cursor open on index being analyzed */
       
 64480   Vdbe *v;                     /* The virtual machine being built up */
       
 64481   int i;                       /* Loop counter */
       
 64482   int topOfLoop;               /* The top of the loop */
       
 64483   int endOfLoop;               /* The end of the loop */
       
 64484   int addr;                    /* The address of an instruction */
       
 64485   int iDb;                     /* Index of database containing pTab */
       
 64486   int regTabname = iMem++;     /* Register containing table name */
       
 64487   int regIdxname = iMem++;     /* Register containing index name */
       
 64488   int regSampleno = iMem++;    /* Register containing next sample number */
       
 64489   int regCol = iMem++;         /* Content of a column analyzed table */
       
 64490   int regRec = iMem++;         /* Register holding completed record */
       
 64491   int regTemp = iMem++;        /* Temporary use register */
       
 64492   int regRowid = iMem++;       /* Rowid for the inserted record */
       
 64493 
       
 64494 #ifdef SQLITE_ENABLE_STAT2
       
 64495   int regTemp2 = iMem++;       /* Temporary use register */
       
 64496   int regSamplerecno = iMem++; /* Index of next sample to record */
       
 64497   int regRecno = iMem++;       /* Current sample index */
       
 64498   int regLast = iMem++;        /* Index of last sample to record */
       
 64499   int regFirst = iMem++;       /* Index of first sample to record */
       
 64500 #endif
       
 64501 
       
 64502   v = sqlite3GetVdbe(pParse);
       
 64503   if( v==0 || NEVER(pTab==0) || pTab->pIndex==0 ){
       
 64504     /* Do no analysis for tables that have no indices */
       
 64505     return;
       
 64506   }
       
 64507   assert( sqlite3BtreeHoldsAllMutexes(db) );
       
 64508   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       
 64509   assert( iDb>=0 );
       
 64510 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 64511   if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
       
 64512       db->aDb[iDb].zName ) ){
       
 64513     return;
       
 64514   }
       
 64515 #endif
       
 64516 
       
 64517   /* Establish a read-lock on the table at the shared-cache level. */
       
 64518   sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
       
 64519 
       
 64520   iIdxCur = pParse->nTab++;
       
 64521   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       
 64522     int nCol = pIdx->nColumn;
       
 64523     KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
       
 64524 
       
 64525     if( iMem+1+(nCol*2)>pParse->nMem ){
       
 64526       pParse->nMem = iMem+1+(nCol*2);
       
 64527     }
       
 64528 
       
 64529     /* Open a cursor to the index to be analyzed. */
       
 64530     assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
       
 64531     sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
       
 64532         (char *)pKey, P4_KEYINFO_HANDOFF);
       
 64533     VdbeComment((v, "%s", pIdx->zName));
       
 64534 
       
 64535     /* Populate the registers containing the table and index names. */
       
 64536     if( pTab->pIndex==pIdx ){
       
 64537       sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
       
 64538     }
       
 64539     sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
       
 64540 
       
 64541 #ifdef SQLITE_ENABLE_STAT2
       
 64542 
       
 64543     /* If this iteration of the loop is generating code to analyze the
       
 64544     ** first index in the pTab->pIndex list, then register regLast has
       
 64545     ** not been populated. In this case populate it now.  */
       
 64546     if( pTab->pIndex==pIdx ){
       
 64547       sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
       
 64548       sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
       
 64549       sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
       
 64550 
       
 64551       sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
       
 64552       sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
       
 64553       addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
       
 64554       sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
       
 64555       sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
       
 64556       sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
       
 64557       sqlite3VdbeAddOp3(v, OP_Divide,  regTemp2, regLast, regLast);
       
 64558       sqlite3VdbeJumpHere(v, addr);
       
 64559     }
       
 64560 
       
 64561     /* Zero the regSampleno and regRecno registers. */
       
 64562     sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
       
 64563     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
       
 64564     sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
       
 64565 #endif
       
 64566 
       
 64567     /* The block of memory cells initialized here is used as follows.
       
 64568     **
       
 64569     **    iMem:                
       
 64570     **        The total number of rows in the table.
       
 64571     **
       
 64572     **    iMem+1 .. iMem+nCol: 
       
 64573     **        Number of distinct entries in index considering the 
       
 64574     **        left-most N columns only, where N is between 1 and nCol, 
       
 64575     **        inclusive.
       
 64576     **
       
 64577     **    iMem+nCol+1 .. Mem+2*nCol:  
       
 64578     **        Previous value of indexed columns, from left to right.
       
 64579     **
       
 64580     ** Cells iMem through iMem+nCol are initialized to 0. The others are 
       
 64581     ** initialized to contain an SQL NULL.
       
 64582     */
       
 64583     for(i=0; i<=nCol; i++){
       
 64584       sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
       
 64585     }
       
 64586     for(i=0; i<nCol; i++){
       
 64587       sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
       
 64588     }
       
 64589 
       
 64590     /* Start the analysis loop. This loop runs through all the entries in
       
 64591     ** the index b-tree.  */
       
 64592     endOfLoop = sqlite3VdbeMakeLabel(v);
       
 64593     sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
       
 64594     topOfLoop = sqlite3VdbeCurrentAddr(v);
       
 64595     sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
       
 64596 
       
 64597     for(i=0; i<nCol; i++){
       
 64598       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
       
 64599 #ifdef SQLITE_ENABLE_STAT2
       
 64600       if( i==0 ){
       
 64601         /* Check if the record that cursor iIdxCur points to contains a
       
 64602         ** value that should be stored in the sqlite_stat2 table. If so,
       
 64603         ** store it.  */
       
 64604         int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
       
 64605         assert( regTabname+1==regIdxname 
       
 64606              && regTabname+2==regSampleno
       
 64607              && regTabname+3==regCol
       
 64608         );
       
 64609         sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
       
 64610         sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
       
 64611         sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
       
 64612         sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
       
 64613 
       
 64614         /* Calculate new values for regSamplerecno and regSampleno.
       
 64615         **
       
 64616         **   sampleno = sampleno + 1
       
 64617         **   samplerecno = samplerecno+(remaining records)/(remaining samples)
       
 64618         */
       
 64619         sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
       
 64620         sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
       
 64621         sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
       
 64622         sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
       
 64623         sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
       
 64624         sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
       
 64625         sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
       
 64626 
       
 64627         sqlite3VdbeJumpHere(v, ne);
       
 64628         sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
       
 64629       }
       
 64630 #endif
       
 64631 
       
 64632       sqlite3VdbeAddOp3(v, OP_Ne, regCol, 0, iMem+nCol+i+1);
       
 64633       /**** TODO:  add collating sequence *****/
       
 64634       sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
       
 64635     }
       
 64636     if( db->mallocFailed ){
       
 64637       /* If a malloc failure has occurred, then the result of the expression 
       
 64638       ** passed as the second argument to the call to sqlite3VdbeJumpHere() 
       
 64639       ** below may be negative. Which causes an assert() to fail (or an
       
 64640       ** out-of-bounds write if SQLITE_DEBUG is not defined).  */
       
 64641       return;
       
 64642     }
       
 64643     sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
       
 64644     for(i=0; i<nCol; i++){
       
 64645       sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-(nCol*2));
       
 64646       sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
       
 64647       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
       
 64648     }
       
 64649 
       
 64650     /* End of the analysis loop. */
       
 64651     sqlite3VdbeResolveLabel(v, endOfLoop);
       
 64652     sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
       
 64653     sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
       
 64654 
       
 64655     /* Store the results in sqlite_stat1.
       
 64656     **
       
 64657     ** The result is a single row of the sqlite_stat1 table.  The first
       
 64658     ** two columns are the names of the table and index.  The third column
       
 64659     ** is a string composed of a list of integer statistics about the
       
 64660     ** index.  The first integer in the list is the total number of entries
       
 64661     ** in the index.  There is one additional integer in the list for each
       
 64662     ** column of the table.  This additional integer is a guess of how many
       
 64663     ** rows of the table the index will select.  If D is the count of distinct
       
 64664     ** values and K is the total number of rows, then the integer is computed
       
 64665     ** as:
       
 64666     **
       
 64667     **        I = (K+D-1)/D
       
 64668     **
       
 64669     ** If K==0 then no entry is made into the sqlite_stat1 table.  
       
 64670     ** If K>0 then it is always the case the D>0 so division by zero
       
 64671     ** is never possible.
       
 64672     */
       
 64673     addr = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
       
 64674     sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
       
 64675     for(i=0; i<nCol; i++){
       
 64676       sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
       
 64677       sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
       
 64678       sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
       
 64679       sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
       
 64680       sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
       
 64681       sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
       
 64682       sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
       
 64683     }
       
 64684     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
       
 64685     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
       
 64686     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
       
 64687     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
       
 64688     sqlite3VdbeJumpHere(v, addr);
       
 64689   }
       
 64690 }
       
 64691 
       
 64692 /*
       
 64693 ** Generate code that will cause the most recent index analysis to
       
 64694 ** be laoded into internal hash tables where is can be used.
       
 64695 */
       
 64696 static void loadAnalysis(Parse *pParse, int iDb){
       
 64697   Vdbe *v = sqlite3GetVdbe(pParse);
       
 64698   if( v ){
       
 64699     sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
       
 64700   }
       
 64701 }
       
 64702 
       
 64703 /*
       
 64704 ** Generate code that will do an analysis of an entire database
       
 64705 */
       
 64706 static void analyzeDatabase(Parse *pParse, int iDb){
       
 64707   sqlite3 *db = pParse->db;
       
 64708   Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
       
 64709   HashElem *k;
       
 64710   int iStatCur;
       
 64711   int iMem;
       
 64712 
       
 64713   sqlite3BeginWriteOperation(pParse, 0, iDb);
       
 64714   iStatCur = pParse->nTab;
       
 64715   pParse->nTab += 2;
       
 64716   openStatTable(pParse, iDb, iStatCur, 0);
       
 64717   iMem = pParse->nMem+1;
       
 64718   for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
       
 64719     Table *pTab = (Table*)sqliteHashData(k);
       
 64720     analyzeOneTable(pParse, pTab, iStatCur, iMem);
       
 64721   }
       
 64722   loadAnalysis(pParse, iDb);
       
 64723 }
       
 64724 
       
 64725 /*
       
 64726 ** Generate code that will do an analysis of a single table in
       
 64727 ** a database.
       
 64728 */
       
 64729 static void analyzeTable(Parse *pParse, Table *pTab){
       
 64730   int iDb;
       
 64731   int iStatCur;
       
 64732 
       
 64733   assert( pTab!=0 );
       
 64734   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
       
 64735   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
       
 64736   sqlite3BeginWriteOperation(pParse, 0, iDb);
       
 64737   iStatCur = pParse->nTab;
       
 64738   pParse->nTab += 2;
       
 64739   openStatTable(pParse, iDb, iStatCur, pTab->zName);
       
 64740   analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1);
       
 64741   loadAnalysis(pParse, iDb);
       
 64742 }
       
 64743 
       
 64744 /*
       
 64745 ** Generate code for the ANALYZE command.  The parser calls this routine
       
 64746 ** when it recognizes an ANALYZE command.
       
 64747 **
       
 64748 **        ANALYZE                            -- 1
       
 64749 **        ANALYZE  <database>                -- 2
       
 64750 **        ANALYZE  ?<database>.?<tablename>  -- 3
       
 64751 **
       
 64752 ** Form 1 causes all indices in all attached databases to be analyzed.
       
 64753 ** Form 2 analyzes all indices the single database named.
       
 64754 ** Form 3 analyzes all indices associated with the named table.
       
 64755 */
       
 64756 SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
       
 64757   sqlite3 *db = pParse->db;
       
 64758   int iDb;
       
 64759   int i;
       
 64760   char *z, *zDb;
       
 64761   Table *pTab;
       
 64762   Token *pTableName;
       
 64763 
       
 64764   /* Read the database schema. If an error occurs, leave an error message
       
 64765   ** and code in pParse and return NULL. */
       
 64766   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
       
 64767   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
       
 64768     return;
       
 64769   }
       
 64770 
       
 64771   assert( pName2!=0 || pName1==0 );
       
 64772   if( pName1==0 ){
       
 64773     /* Form 1:  Analyze everything */
       
 64774     for(i=0; i<db->nDb; i++){
       
 64775       if( i==1 ) continue;  /* Do not analyze the TEMP database */
       
 64776       analyzeDatabase(pParse, i);
       
 64777     }
       
 64778   }else if( pName2->n==0 ){
       
 64779     /* Form 2:  Analyze the database or table named */
       
 64780     iDb = sqlite3FindDb(db, pName1);
       
 64781     if( iDb>=0 ){
       
 64782       analyzeDatabase(pParse, iDb);
       
 64783     }else{
       
 64784       z = sqlite3NameFromToken(db, pName1);
       
 64785       if( z ){
       
 64786         pTab = sqlite3LocateTable(pParse, 0, z, 0);
       
 64787         sqlite3DbFree(db, z);
       
 64788         if( pTab ){
       
 64789           analyzeTable(pParse, pTab);
       
 64790         }
       
 64791       }
       
 64792     }
       
 64793   }else{
       
 64794     /* Form 3: Analyze the fully qualified table name */
       
 64795     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
       
 64796     if( iDb>=0 ){
       
 64797       zDb = db->aDb[iDb].zName;
       
 64798       z = sqlite3NameFromToken(db, pTableName);
       
 64799       if( z ){
       
 64800         pTab = sqlite3LocateTable(pParse, 0, z, zDb);
       
 64801         sqlite3DbFree(db, z);
       
 64802         if( pTab ){
       
 64803           analyzeTable(pParse, pTab);
       
 64804         }
       
 64805       }
       
 64806     }   
       
 64807   }
       
 64808 }
       
 64809 
       
 64810 /*
       
 64811 ** Used to pass information from the analyzer reader through to the
       
 64812 ** callback routine.
       
 64813 */
       
 64814 typedef struct analysisInfo analysisInfo;
       
 64815 struct analysisInfo {
       
 64816   sqlite3 *db;
       
 64817   const char *zDatabase;
       
 64818 };
       
 64819 
       
 64820 /*
       
 64821 ** This callback is invoked once for each index when reading the
       
 64822 ** sqlite_stat1 table.  
       
 64823 **
       
 64824 **     argv[0] = name of the index
       
 64825 **     argv[1] = results of analysis - on integer for each column
       
 64826 */
       
 64827 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
       
 64828   analysisInfo *pInfo = (analysisInfo*)pData;
       
 64829   Index *pIndex;
       
 64830   int i, c;
       
 64831   unsigned int v;
       
 64832   const char *z;
       
 64833 
       
 64834   assert( argc==2 );
       
 64835   UNUSED_PARAMETER2(NotUsed, argc);
       
 64836 
       
 64837   if( argv==0 || argv[0]==0 || argv[1]==0 ){
       
 64838     return 0;
       
 64839   }
       
 64840   pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase);
       
 64841   if( pIndex==0 ){
       
 64842     return 0;
       
 64843   }
       
 64844   z = argv[1];
       
 64845   for(i=0; *z && i<=pIndex->nColumn; i++){
       
 64846     v = 0;
       
 64847     while( (c=z[0])>='0' && c<='9' ){
       
 64848       v = v*10 + c - '0';
       
 64849       z++;
       
 64850     }
       
 64851     pIndex->aiRowEst[i] = v;
       
 64852     if( *z==' ' ) z++;
       
 64853   }
       
 64854   return 0;
       
 64855 }
       
 64856 
       
 64857 /*
       
 64858 ** If the Index.aSample variable is not NULL, delete the aSample[] array
       
 64859 ** and its contents.
       
 64860 */
       
 64861 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(Index *pIdx){
       
 64862 #ifdef SQLITE_ENABLE_STAT2
       
 64863   if( pIdx->aSample ){
       
 64864     int j;
       
 64865     sqlite3 *dbMem = pIdx->pTable->dbMem;
       
 64866     for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
       
 64867       IndexSample *p = &pIdx->aSample[j];
       
 64868       if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
       
 64869         sqlite3DbFree(pIdx->pTable->dbMem, p->u.z);
       
 64870       }
       
 64871     }
       
 64872     sqlite3DbFree(dbMem, pIdx->aSample);
       
 64873     pIdx->aSample = 0;
       
 64874   }
       
 64875 #else
       
 64876   UNUSED_PARAMETER(pIdx);
       
 64877 #endif
       
 64878 }
       
 64879 
       
 64880 /*
       
 64881 ** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
       
 64882 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
       
 64883 ** arrays. The contents of sqlite_stat2 are used to populate the
       
 64884 ** Index.aSample[] arrays.
       
 64885 **
       
 64886 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
       
 64887 ** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined 
       
 64888 ** during compilation and the sqlite_stat2 table is present, no data is 
       
 64889 ** read from it.
       
 64890 **
       
 64891 ** If SQLITE_ENABLE_STAT2 was defined during compilation and the 
       
 64892 ** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
       
 64893 ** returned. However, in this case, data is read from the sqlite_stat1
       
 64894 ** table (if it is present) before returning.
       
 64895 **
       
 64896 ** If an OOM error occurs, this function always sets db->mallocFailed.
       
 64897 ** This means if the caller does not care about other errors, the return
       
 64898 ** code may be ignored.
       
 64899 */
       
 64900 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
       
 64901   analysisInfo sInfo;
       
 64902   HashElem *i;
       
 64903   char *zSql;
       
 64904   int rc;
       
 64905 
       
 64906   assert( iDb>=0 && iDb<db->nDb );
       
 64907   assert( db->aDb[iDb].pBt!=0 );
       
 64908   assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
       
 64909 
       
 64910   /* Clear any prior statistics */
       
 64911   for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
       
 64912     Index *pIdx = sqliteHashData(i);
       
 64913     sqlite3DefaultRowEst(pIdx);
       
 64914     sqlite3DeleteIndexSamples(pIdx);
       
 64915   }
       
 64916 
       
 64917   /* Check to make sure the sqlite_stat1 table exists */
       
 64918   sInfo.db = db;
       
 64919   sInfo.zDatabase = db->aDb[iDb].zName;
       
 64920   if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
       
 64921     return SQLITE_ERROR;
       
 64922   }
       
 64923 
       
 64924   /* Load new statistics out of the sqlite_stat1 table */
       
 64925   zSql = sqlite3MPrintf(db, 
       
 64926       "SELECT idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
       
 64927   if( zSql==0 ){
       
 64928     rc = SQLITE_NOMEM;
       
 64929   }else{
       
 64930     (void)sqlite3SafetyOff(db);
       
 64931     rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
       
 64932     (void)sqlite3SafetyOn(db);
       
 64933     sqlite3DbFree(db, zSql);
       
 64934   }
       
 64935 
       
 64936 
       
 64937   /* Load the statistics from the sqlite_stat2 table. */
       
 64938 #ifdef SQLITE_ENABLE_STAT2
       
 64939   if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
       
 64940     rc = SQLITE_ERROR;
       
 64941   }
       
 64942   if( rc==SQLITE_OK ){
       
 64943     sqlite3_stmt *pStmt = 0;
       
 64944 
       
 64945     zSql = sqlite3MPrintf(db, 
       
 64946         "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
       
 64947     if( !zSql ){
       
 64948       rc = SQLITE_NOMEM;
       
 64949     }else{
       
 64950       (void)sqlite3SafetyOff(db);
       
 64951       rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
       
 64952       (void)sqlite3SafetyOn(db);
       
 64953       sqlite3DbFree(db, zSql);
       
 64954     }
       
 64955 
       
 64956     if( rc==SQLITE_OK ){
       
 64957       (void)sqlite3SafetyOff(db);
       
 64958       while( sqlite3_step(pStmt)==SQLITE_ROW ){
       
 64959         char *zIndex = (char *)sqlite3_column_text(pStmt, 0);
       
 64960         Index *pIdx = sqlite3FindIndex(db, zIndex, sInfo.zDatabase);
       
 64961         if( pIdx ){
       
 64962           int iSample = sqlite3_column_int(pStmt, 1);
       
 64963           sqlite3 *dbMem = pIdx->pTable->dbMem;
       
 64964           assert( dbMem==db || dbMem==0 );
       
 64965           if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
       
 64966             int eType = sqlite3_column_type(pStmt, 2);
       
 64967 
       
 64968             if( pIdx->aSample==0 ){
       
 64969               static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
       
 64970               pIdx->aSample = (IndexSample *)sqlite3DbMallocZero(dbMem, sz);
       
 64971               if( pIdx->aSample==0 ){
       
 64972                 db->mallocFailed = 1;
       
 64973                 break;
       
 64974               }
       
 64975             }
       
 64976 
       
 64977             assert( pIdx->aSample );
       
 64978             {
       
 64979               IndexSample *pSample = &pIdx->aSample[iSample];
       
 64980               pSample->eType = (u8)eType;
       
 64981               if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
       
 64982                 pSample->u.r = sqlite3_column_double(pStmt, 2);
       
 64983               }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
       
 64984                 const char *z = (const char *)(
       
 64985                     (eType==SQLITE_BLOB) ?
       
 64986                     sqlite3_column_blob(pStmt, 2):
       
 64987                     sqlite3_column_text(pStmt, 2)
       
 64988                 );
       
 64989                 int n = sqlite3_column_bytes(pStmt, 2);
       
 64990                 if( n>24 ){
       
 64991                   n = 24;
       
 64992                 }
       
 64993                 pSample->nByte = (u8)n;
       
 64994                 pSample->u.z = sqlite3DbMallocRaw(dbMem, n);
       
 64995                 if( pSample->u.z ){
       
 64996                   memcpy(pSample->u.z, z, n);
       
 64997                 }else{
       
 64998                   db->mallocFailed = 1;
       
 64999                   break;
       
 65000                 }
       
 65001               }
       
 65002             }
       
 65003           }
       
 65004         }
       
 65005       }
       
 65006       rc = sqlite3_finalize(pStmt);
       
 65007       (void)sqlite3SafetyOn(db);
       
 65008     }
       
 65009   }
       
 65010 #endif
       
 65011 
       
 65012   if( rc==SQLITE_NOMEM ){
       
 65013     db->mallocFailed = 1;
       
 65014   }
       
 65015   return rc;
       
 65016 }
       
 65017 
       
 65018 
       
 65019 #endif /* SQLITE_OMIT_ANALYZE */
       
 65020 
       
 65021 /************** End of analyze.c *********************************************/
       
 65022 /************** Begin file attach.c ******************************************/
       
 65023 /*
       
 65024 ** 2003 April 6
       
 65025 **
       
 65026 ** The author disclaims copyright to this source code.  In place of
       
 65027 ** a legal notice, here is a blessing:
       
 65028 **
       
 65029 **    May you do good and not evil.
       
 65030 **    May you find forgiveness for yourself and forgive others.
       
 65031 **    May you share freely, never taking more than you give.
       
 65032 **
       
 65033 *************************************************************************
       
 65034 ** This file contains code used to implement the ATTACH and DETACH commands.
       
 65035 **
       
 65036 ** $Id: attach.c,v 1.93 2009/05/31 21:21:41 drh Exp $
       
 65037 */
       
 65038 
       
 65039 #ifndef SQLITE_OMIT_ATTACH
       
 65040 /*
       
 65041 ** Resolve an expression that was part of an ATTACH or DETACH statement. This
       
 65042 ** is slightly different from resolving a normal SQL expression, because simple
       
 65043 ** identifiers are treated as strings, not possible column names or aliases.
       
 65044 **
       
 65045 ** i.e. if the parser sees:
       
 65046 **
       
 65047 **     ATTACH DATABASE abc AS def
       
 65048 **
       
 65049 ** it treats the two expressions as literal strings 'abc' and 'def' instead of
       
 65050 ** looking for columns of the same name.
       
 65051 **
       
 65052 ** This only applies to the root node of pExpr, so the statement:
       
 65053 **
       
 65054 **     ATTACH DATABASE abc||def AS 'db2'
       
 65055 **
       
 65056 ** will fail because neither abc or def can be resolved.
       
 65057 */
       
 65058 static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
       
 65059 {
       
 65060   int rc = SQLITE_OK;
       
 65061   if( pExpr ){
       
 65062     if( pExpr->op!=TK_ID ){
       
 65063       rc = sqlite3ResolveExprNames(pName, pExpr);
       
 65064       if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){
       
 65065         sqlite3ErrorMsg(pName->pParse, "invalid name: \"%s\"", pExpr->u.zToken);
       
 65066         return SQLITE_ERROR;
       
 65067       }
       
 65068     }else{
       
 65069       pExpr->op = TK_STRING;
       
 65070     }
       
 65071   }
       
 65072   return rc;
       
 65073 }
       
 65074 
       
 65075 /*
       
 65076 ** An SQL user-function registered to do the work of an ATTACH statement. The
       
 65077 ** three arguments to the function come directly from an attach statement:
       
 65078 **
       
 65079 **     ATTACH DATABASE x AS y KEY z
       
 65080 **
       
 65081 **     SELECT sqlite_attach(x, y, z)
       
 65082 **
       
 65083 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
       
 65084 ** third argument.
       
 65085 */
       
 65086 static void attachFunc(
       
 65087   sqlite3_context *context,
       
 65088   int NotUsed,
       
 65089   sqlite3_value **argv
       
 65090 ){
       
 65091   int i;
       
 65092   int rc = 0;
       
 65093   sqlite3 *db = sqlite3_context_db_handle(context);
       
 65094   const char *zName;
       
 65095   const char *zFile;
       
 65096   Db *aNew;
       
 65097   char *zErrDyn = 0;
       
 65098 
       
 65099   UNUSED_PARAMETER(NotUsed);
       
 65100 
       
 65101   zFile = (const char *)sqlite3_value_text(argv[0]);
       
 65102   zName = (const char *)sqlite3_value_text(argv[1]);
       
 65103   if( zFile==0 ) zFile = "";
       
 65104   if( zName==0 ) zName = "";
       
 65105 
       
 65106   /* Check for the following errors:
       
 65107   **
       
 65108   **     * Too many attached databases,
       
 65109   **     * Transaction currently open
       
 65110   **     * Specified database name already being used.
       
 65111   */
       
 65112   if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
       
 65113     zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
       
 65114       db->aLimit[SQLITE_LIMIT_ATTACHED]
       
 65115     );
       
 65116     goto attach_error;
       
 65117   }
       
 65118   if( !db->autoCommit ){
       
 65119     zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
       
 65120     goto attach_error;
       
 65121   }
       
 65122   for(i=0; i<db->nDb; i++){
       
 65123     char *z = db->aDb[i].zName;
       
 65124     assert( z && zName );
       
 65125     if( sqlite3StrICmp(z, zName)==0 ){
       
 65126       zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
       
 65127       goto attach_error;
       
 65128     }
       
 65129   }
       
 65130 
       
 65131   /* Allocate the new entry in the db->aDb[] array and initialise the schema
       
 65132   ** hash tables.
       
 65133   */
       
 65134   if( db->aDb==db->aDbStatic ){
       
 65135     aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
       
 65136     if( aNew==0 ) return;
       
 65137     memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
       
 65138   }else{
       
 65139     aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
       
 65140     if( aNew==0 ) return;
       
 65141   }
       
 65142   db->aDb = aNew;
       
 65143   aNew = &db->aDb[db->nDb];
       
 65144   memset(aNew, 0, sizeof(*aNew));
       
 65145 
       
 65146   /* Open the database file. If the btree is successfully opened, use
       
 65147   ** it to obtain the database schema. At this point the schema may
       
 65148   ** or may not be initialised.
       
 65149   */
       
 65150   rc = sqlite3BtreeFactory(db, zFile, 0, SQLITE_DEFAULT_CACHE_SIZE,
       
 65151                            db->openFlags | SQLITE_OPEN_MAIN_DB,
       
 65152                            &aNew->pBt);
       
 65153   db->nDb++;
       
 65154   if( rc==SQLITE_CONSTRAINT ){
       
 65155     rc = SQLITE_ERROR;
       
 65156     zErrDyn = sqlite3MPrintf(db, "database is already attached");
       
 65157   }else if( rc==SQLITE_OK ){
       
 65158     Pager *pPager;
       
 65159     aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
       
 65160     if( !aNew->pSchema ){
       
 65161       rc = SQLITE_NOMEM;
       
 65162     }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
       
 65163       zErrDyn = sqlite3MPrintf(db, 
       
 65164         "attached databases must use the same text encoding as main database");
       
 65165       rc = SQLITE_ERROR;
       
 65166     }
       
 65167     pPager = sqlite3BtreePager(aNew->pBt);
       
 65168     sqlite3PagerLockingMode(pPager, db->dfltLockMode);
       
 65169     sqlite3PagerJournalMode(pPager, db->dfltJournalMode);
       
 65170   }
       
 65171   aNew->zName = sqlite3DbStrDup(db, zName);
       
 65172   aNew->safety_level = 3;
       
 65173 
       
 65174 #if SQLITE_HAS_CODEC
       
 65175   {
       
 65176     extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
       
 65177     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
       
 65178     int nKey;
       
 65179     char *zKey;
       
 65180     int t = sqlite3_value_type(argv[2]);
       
 65181     switch( t ){
       
 65182       case SQLITE_INTEGER:
       
 65183       case SQLITE_FLOAT:
       
 65184         zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
       
 65185         rc = SQLITE_ERROR;
       
 65186         break;
       
 65187         
       
 65188       case SQLITE_TEXT:
       
 65189       case SQLITE_BLOB:
       
 65190         nKey = sqlite3_value_bytes(argv[2]);
       
 65191         zKey = (char *)sqlite3_value_blob(argv[2]);
       
 65192         sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
       
 65193         break;
       
 65194 
       
 65195       case SQLITE_NULL:
       
 65196         /* No key specified.  Use the key from the main database */
       
 65197         sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
       
 65198         sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
       
 65199         break;
       
 65200     }
       
 65201   }
       
 65202 #endif
       
 65203 
       
 65204   /* If the file was opened successfully, read the schema for the new database.
       
 65205   ** If this fails, or if opening the file failed, then close the file and 
       
 65206   ** remove the entry from the db->aDb[] array. i.e. put everything back the way
       
 65207   ** we found it.
       
 65208   */
       
 65209   if( rc==SQLITE_OK ){
       
 65210     (void)sqlite3SafetyOn(db);
       
 65211     sqlite3BtreeEnterAll(db);
       
 65212     rc = sqlite3Init(db, &zErrDyn);
       
 65213     sqlite3BtreeLeaveAll(db);
       
 65214     (void)sqlite3SafetyOff(db);
       
 65215   }
       
 65216   if( rc ){
       
 65217     int iDb = db->nDb - 1;
       
 65218     assert( iDb>=2 );
       
 65219     if( db->aDb[iDb].pBt ){
       
 65220       sqlite3BtreeClose(db->aDb[iDb].pBt);
       
 65221       db->aDb[iDb].pBt = 0;
       
 65222       db->aDb[iDb].pSchema = 0;
       
 65223     }
       
 65224     sqlite3ResetInternalSchema(db, 0);
       
 65225     db->nDb = iDb;
       
 65226     if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
       
 65227       db->mallocFailed = 1;
       
 65228       sqlite3DbFree(db, zErrDyn);
       
 65229       zErrDyn = sqlite3MPrintf(db, "out of memory");
       
 65230     }else if( zErrDyn==0 ){
       
 65231       zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
       
 65232     }
       
 65233     goto attach_error;
       
 65234   }
       
 65235   
       
 65236   return;
       
 65237 
       
 65238 attach_error:
       
 65239   /* Return an error if we get here */
       
 65240   if( zErrDyn ){
       
 65241     sqlite3_result_error(context, zErrDyn, -1);
       
 65242     sqlite3DbFree(db, zErrDyn);
       
 65243   }
       
 65244   if( rc ) sqlite3_result_error_code(context, rc);
       
 65245 }
       
 65246 
       
 65247 /*
       
 65248 ** An SQL user-function registered to do the work of an DETACH statement. The
       
 65249 ** three arguments to the function come directly from a detach statement:
       
 65250 **
       
 65251 **     DETACH DATABASE x
       
 65252 **
       
 65253 **     SELECT sqlite_detach(x)
       
 65254 */
       
 65255 static void detachFunc(
       
 65256   sqlite3_context *context,
       
 65257   int NotUsed,
       
 65258   sqlite3_value **argv
       
 65259 ){
       
 65260   const char *zName = (const char *)sqlite3_value_text(argv[0]);
       
 65261   sqlite3 *db = sqlite3_context_db_handle(context);
       
 65262   int i;
       
 65263   Db *pDb = 0;
       
 65264   char zErr[128];
       
 65265 
       
 65266   UNUSED_PARAMETER(NotUsed);
       
 65267 
       
 65268   if( zName==0 ) zName = "";
       
 65269   for(i=0; i<db->nDb; i++){
       
 65270     pDb = &db->aDb[i];
       
 65271     if( pDb->pBt==0 ) continue;
       
 65272     if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
       
 65273   }
       
 65274 
       
 65275   if( i>=db->nDb ){
       
 65276     sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
       
 65277     goto detach_error;
       
 65278   }
       
 65279   if( i<2 ){
       
 65280     sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
       
 65281     goto detach_error;
       
 65282   }
       
 65283   if( !db->autoCommit ){
       
 65284     sqlite3_snprintf(sizeof(zErr), zErr,
       
 65285                      "cannot DETACH database within transaction");
       
 65286     goto detach_error;
       
 65287   }
       
 65288   if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
       
 65289     sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
       
 65290     goto detach_error;
       
 65291   }
       
 65292 
       
 65293   sqlite3BtreeClose(pDb->pBt);
       
 65294   pDb->pBt = 0;
       
 65295   pDb->pSchema = 0;
       
 65296   sqlite3ResetInternalSchema(db, 0);
       
 65297   return;
       
 65298 
       
 65299 detach_error:
       
 65300   sqlite3_result_error(context, zErr, -1);
       
 65301 }
       
 65302 
       
 65303 /*
       
 65304 ** This procedure generates VDBE code for a single invocation of either the
       
 65305 ** sqlite_detach() or sqlite_attach() SQL user functions.
       
 65306 */
       
 65307 static void codeAttach(
       
 65308   Parse *pParse,       /* The parser context */
       
 65309   int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
       
 65310   FuncDef *pFunc,      /* FuncDef wrapper for detachFunc() or attachFunc() */
       
 65311   Expr *pAuthArg,      /* Expression to pass to authorization callback */
       
 65312   Expr *pFilename,     /* Name of database file */
       
 65313   Expr *pDbname,       /* Name of the database to use internally */
       
 65314   Expr *pKey           /* Database key for encryption extension */
       
 65315 ){
       
 65316   int rc;
       
 65317   NameContext sName;
       
 65318   Vdbe *v;
       
 65319   sqlite3* db = pParse->db;
       
 65320   int regArgs;
       
 65321 
       
 65322   memset(&sName, 0, sizeof(NameContext));
       
 65323   sName.pParse = pParse;
       
 65324 
       
 65325   if( 
       
 65326       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
       
 65327       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
       
 65328       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
       
 65329   ){
       
 65330     pParse->nErr++;
       
 65331     goto attach_end;
       
 65332   }
       
 65333 
       
 65334 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 65335   if( pAuthArg ){
       
 65336     char *zAuthArg = pAuthArg->u.zToken;
       
 65337     if( NEVER(zAuthArg==0) ){
       
 65338       goto attach_end;
       
 65339     }
       
 65340     rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
       
 65341     if(rc!=SQLITE_OK ){
       
 65342       goto attach_end;
       
 65343     }
       
 65344   }
       
 65345 #endif /* SQLITE_OMIT_AUTHORIZATION */
       
 65346 
       
 65347 
       
 65348   v = sqlite3GetVdbe(pParse);
       
 65349   regArgs = sqlite3GetTempRange(pParse, 4);
       
 65350   sqlite3ExprCode(pParse, pFilename, regArgs);
       
 65351   sqlite3ExprCode(pParse, pDbname, regArgs+1);
       
 65352   sqlite3ExprCode(pParse, pKey, regArgs+2);
       
 65353 
       
 65354   assert( v || db->mallocFailed );
       
 65355   if( v ){
       
 65356     sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
       
 65357     assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
       
 65358     sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
       
 65359     sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
       
 65360 
       
 65361     /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
       
 65362     ** statement only). For DETACH, set it to false (expire all existing
       
 65363     ** statements).
       
 65364     */
       
 65365     sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
       
 65366   }
       
 65367   
       
 65368 attach_end:
       
 65369   sqlite3ExprDelete(db, pFilename);
       
 65370   sqlite3ExprDelete(db, pDbname);
       
 65371   sqlite3ExprDelete(db, pKey);
       
 65372 }
       
 65373 
       
 65374 /*
       
 65375 ** Called by the parser to compile a DETACH statement.
       
 65376 **
       
 65377 **     DETACH pDbname
       
 65378 */
       
 65379 SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
       
 65380   static FuncDef detach_func = {
       
 65381     1,                /* nArg */
       
 65382     SQLITE_UTF8,      /* iPrefEnc */
       
 65383     0,                /* flags */
       
 65384     0,                /* pUserData */
       
 65385     0,                /* pNext */
       
 65386     detachFunc,       /* xFunc */
       
 65387     0,                /* xStep */
       
 65388     0,                /* xFinalize */
       
 65389     "sqlite_detach",  /* zName */
       
 65390     0                 /* pHash */
       
 65391   };
       
 65392   codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
       
 65393 }
       
 65394 
       
 65395 /*
       
 65396 ** Called by the parser to compile an ATTACH statement.
       
 65397 **
       
 65398 **     ATTACH p AS pDbname KEY pKey
       
 65399 */
       
 65400 SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
       
 65401   static FuncDef attach_func = {
       
 65402     3,                /* nArg */
       
 65403     SQLITE_UTF8,      /* iPrefEnc */
       
 65404     0,                /* flags */
       
 65405     0,                /* pUserData */
       
 65406     0,                /* pNext */
       
 65407     attachFunc,       /* xFunc */
       
 65408     0,                /* xStep */
       
 65409     0,                /* xFinalize */
       
 65410     "sqlite_attach",  /* zName */
       
 65411     0                 /* pHash */
       
 65412   };
       
 65413   codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
       
 65414 }
       
 65415 #endif /* SQLITE_OMIT_ATTACH */
       
 65416 
       
 65417 /*
       
 65418 ** Initialize a DbFixer structure.  This routine must be called prior
       
 65419 ** to passing the structure to one of the sqliteFixAAAA() routines below.
       
 65420 **
       
 65421 ** The return value indicates whether or not fixation is required.  TRUE
       
 65422 ** means we do need to fix the database references, FALSE means we do not.
       
 65423 */
       
 65424 SQLITE_PRIVATE int sqlite3FixInit(
       
 65425   DbFixer *pFix,      /* The fixer to be initialized */
       
 65426   Parse *pParse,      /* Error messages will be written here */
       
 65427   int iDb,            /* This is the database that must be used */
       
 65428   const char *zType,  /* "view", "trigger", or "index" */
       
 65429   const Token *pName  /* Name of the view, trigger, or index */
       
 65430 ){
       
 65431   sqlite3 *db;
       
 65432 
       
 65433   if( NEVER(iDb<0) || iDb==1 ) return 0;
       
 65434   db = pParse->db;
       
 65435   assert( db->nDb>iDb );
       
 65436   pFix->pParse = pParse;
       
 65437   pFix->zDb = db->aDb[iDb].zName;
       
 65438   pFix->zType = zType;
       
 65439   pFix->pName = pName;
       
 65440   return 1;
       
 65441 }
       
 65442 
       
 65443 /*
       
 65444 ** The following set of routines walk through the parse tree and assign
       
 65445 ** a specific database to all table references where the database name
       
 65446 ** was left unspecified in the original SQL statement.  The pFix structure
       
 65447 ** must have been initialized by a prior call to sqlite3FixInit().
       
 65448 **
       
 65449 ** These routines are used to make sure that an index, trigger, or
       
 65450 ** view in one database does not refer to objects in a different database.
       
 65451 ** (Exception: indices, triggers, and views in the TEMP database are
       
 65452 ** allowed to refer to anything.)  If a reference is explicitly made
       
 65453 ** to an object in a different database, an error message is added to
       
 65454 ** pParse->zErrMsg and these routines return non-zero.  If everything
       
 65455 ** checks out, these routines return 0.
       
 65456 */
       
 65457 SQLITE_PRIVATE int sqlite3FixSrcList(
       
 65458   DbFixer *pFix,       /* Context of the fixation */
       
 65459   SrcList *pList       /* The Source list to check and modify */
       
 65460 ){
       
 65461   int i;
       
 65462   const char *zDb;
       
 65463   struct SrcList_item *pItem;
       
 65464 
       
 65465   if( NEVER(pList==0) ) return 0;
       
 65466   zDb = pFix->zDb;
       
 65467   for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
       
 65468     if( pItem->zDatabase==0 ){
       
 65469       pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb);
       
 65470     }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){
       
 65471       sqlite3ErrorMsg(pFix->pParse,
       
 65472          "%s %T cannot reference objects in database %s",
       
 65473          pFix->zType, pFix->pName, pItem->zDatabase);
       
 65474       return 1;
       
 65475     }
       
 65476 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
       
 65477     if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
       
 65478     if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
       
 65479 #endif
       
 65480   }
       
 65481   return 0;
       
 65482 }
       
 65483 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
       
 65484 SQLITE_PRIVATE int sqlite3FixSelect(
       
 65485   DbFixer *pFix,       /* Context of the fixation */
       
 65486   Select *pSelect      /* The SELECT statement to be fixed to one database */
       
 65487 ){
       
 65488   while( pSelect ){
       
 65489     if( sqlite3FixExprList(pFix, pSelect->pEList) ){
       
 65490       return 1;
       
 65491     }
       
 65492     if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
       
 65493       return 1;
       
 65494     }
       
 65495     if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
       
 65496       return 1;
       
 65497     }
       
 65498     if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
       
 65499       return 1;
       
 65500     }
       
 65501     pSelect = pSelect->pPrior;
       
 65502   }
       
 65503   return 0;
       
 65504 }
       
 65505 SQLITE_PRIVATE int sqlite3FixExpr(
       
 65506   DbFixer *pFix,     /* Context of the fixation */
       
 65507   Expr *pExpr        /* The expression to be fixed to one database */
       
 65508 ){
       
 65509   while( pExpr ){
       
 65510     if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ) break;
       
 65511     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
       
 65512       if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
       
 65513     }else{
       
 65514       if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
       
 65515     }
       
 65516     if( sqlite3FixExpr(pFix, pExpr->pRight) ){
       
 65517       return 1;
       
 65518     }
       
 65519     pExpr = pExpr->pLeft;
       
 65520   }
       
 65521   return 0;
       
 65522 }
       
 65523 SQLITE_PRIVATE int sqlite3FixExprList(
       
 65524   DbFixer *pFix,     /* Context of the fixation */
       
 65525   ExprList *pList    /* The expression to be fixed to one database */
       
 65526 ){
       
 65527   int i;
       
 65528   struct ExprList_item *pItem;
       
 65529   if( pList==0 ) return 0;
       
 65530   for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
       
 65531     if( sqlite3FixExpr(pFix, pItem->pExpr) ){
       
 65532       return 1;
       
 65533     }
       
 65534   }
       
 65535   return 0;
       
 65536 }
       
 65537 #endif
       
 65538 
       
 65539 #ifndef SQLITE_OMIT_TRIGGER
       
 65540 SQLITE_PRIVATE int sqlite3FixTriggerStep(
       
 65541   DbFixer *pFix,     /* Context of the fixation */
       
 65542   TriggerStep *pStep /* The trigger step be fixed to one database */
       
 65543 ){
       
 65544   while( pStep ){
       
 65545     if( sqlite3FixSelect(pFix, pStep->pSelect) ){
       
 65546       return 1;
       
 65547     }
       
 65548     if( sqlite3FixExpr(pFix, pStep->pWhere) ){
       
 65549       return 1;
       
 65550     }
       
 65551     if( sqlite3FixExprList(pFix, pStep->pExprList) ){
       
 65552       return 1;
       
 65553     }
       
 65554     pStep = pStep->pNext;
       
 65555   }
       
 65556   return 0;
       
 65557 }
       
 65558 #endif
       
 65559 
       
 65560 /************** End of attach.c **********************************************/
       
 65561 /************** Begin file auth.c ********************************************/
       
 65562 /*
       
 65563 ** 2003 January 11
       
 65564 **
       
 65565 ** The author disclaims copyright to this source code.  In place of
       
 65566 ** a legal notice, here is a blessing:
       
 65567 **
       
 65568 **    May you do good and not evil.
       
 65569 **    May you find forgiveness for yourself and forgive others.
       
 65570 **    May you share freely, never taking more than you give.
       
 65571 **
       
 65572 *************************************************************************
       
 65573 ** This file contains code used to implement the sqlite3_set_authorizer()
       
 65574 ** API.  This facility is an optional feature of the library.  Embedded
       
 65575 ** systems that do not need this facility may omit it by recompiling
       
 65576 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
       
 65577 **
       
 65578 ** $Id: auth.c,v 1.32 2009/07/02 18:40:35 danielk1977 Exp $
       
 65579 */
       
 65580 
       
 65581 /*
       
 65582 ** All of the code in this file may be omitted by defining a single
       
 65583 ** macro.
       
 65584 */
       
 65585 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 65586 
       
 65587 /*
       
 65588 ** Set or clear the access authorization function.
       
 65589 **
       
 65590 ** The access authorization function is be called during the compilation
       
 65591 ** phase to verify that the user has read and/or write access permission on
       
 65592 ** various fields of the database.  The first argument to the auth function
       
 65593 ** is a copy of the 3rd argument to this routine.  The second argument
       
 65594 ** to the auth function is one of these constants:
       
 65595 **
       
 65596 **       SQLITE_CREATE_INDEX
       
 65597 **       SQLITE_CREATE_TABLE
       
 65598 **       SQLITE_CREATE_TEMP_INDEX
       
 65599 **       SQLITE_CREATE_TEMP_TABLE
       
 65600 **       SQLITE_CREATE_TEMP_TRIGGER
       
 65601 **       SQLITE_CREATE_TEMP_VIEW
       
 65602 **       SQLITE_CREATE_TRIGGER
       
 65603 **       SQLITE_CREATE_VIEW
       
 65604 **       SQLITE_DELETE
       
 65605 **       SQLITE_DROP_INDEX
       
 65606 **       SQLITE_DROP_TABLE
       
 65607 **       SQLITE_DROP_TEMP_INDEX
       
 65608 **       SQLITE_DROP_TEMP_TABLE
       
 65609 **       SQLITE_DROP_TEMP_TRIGGER
       
 65610 **       SQLITE_DROP_TEMP_VIEW
       
 65611 **       SQLITE_DROP_TRIGGER
       
 65612 **       SQLITE_DROP_VIEW
       
 65613 **       SQLITE_INSERT
       
 65614 **       SQLITE_PRAGMA
       
 65615 **       SQLITE_READ
       
 65616 **       SQLITE_SELECT
       
 65617 **       SQLITE_TRANSACTION
       
 65618 **       SQLITE_UPDATE
       
 65619 **
       
 65620 ** The third and fourth arguments to the auth function are the name of
       
 65621 ** the table and the column that are being accessed.  The auth function
       
 65622 ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
       
 65623 ** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
       
 65624 ** means that the SQL statement will never-run - the sqlite3_exec() call
       
 65625 ** will return with an error.  SQLITE_IGNORE means that the SQL statement
       
 65626 ** should run but attempts to read the specified column will return NULL
       
 65627 ** and attempts to write the column will be ignored.
       
 65628 **
       
 65629 ** Setting the auth function to NULL disables this hook.  The default
       
 65630 ** setting of the auth function is NULL.
       
 65631 */
       
 65632 SQLITE_API int sqlite3_set_authorizer(
       
 65633   sqlite3 *db,
       
 65634   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
       
 65635   void *pArg
       
 65636 ){
       
 65637   sqlite3_mutex_enter(db->mutex);
       
 65638   db->xAuth = xAuth;
       
 65639   db->pAuthArg = pArg;
       
 65640   sqlite3ExpirePreparedStatements(db);
       
 65641   sqlite3_mutex_leave(db->mutex);
       
 65642   return SQLITE_OK;
       
 65643 }
       
 65644 
       
 65645 /*
       
 65646 ** Write an error message into pParse->zErrMsg that explains that the
       
 65647 ** user-supplied authorization function returned an illegal value.
       
 65648 */
       
 65649 static void sqliteAuthBadReturnCode(Parse *pParse){
       
 65650   sqlite3ErrorMsg(pParse, "authorizer malfunction");
       
 65651   pParse->rc = SQLITE_ERROR;
       
 65652 }
       
 65653 
       
 65654 /*
       
 65655 ** Invoke the authorization callback for permission to read column zCol from
       
 65656 ** table zTab in database zDb. This function assumes that an authorization
       
 65657 ** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
       
 65658 **
       
 65659 ** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
       
 65660 ** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
       
 65661 ** is treated as SQLITE_DENY. In this case an error is left in pParse.
       
 65662 */
       
 65663 SQLITE_PRIVATE int sqlite3AuthReadCol(
       
 65664   Parse *pParse,                  /* The parser context */
       
 65665   const char *zTab,               /* Table name */
       
 65666   const char *zCol,               /* Column name */
       
 65667   int iDb                         /* Index of containing database. */
       
 65668 ){
       
 65669   sqlite3 *db = pParse->db;       /* Database handle */
       
 65670   char *zDb = db->aDb[iDb].zName; /* Name of attached database */
       
 65671   int rc;                         /* Auth callback return code */
       
 65672 
       
 65673   rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
       
 65674   if( rc==SQLITE_DENY ){
       
 65675     if( db->nDb>2 || iDb!=0 ){
       
 65676       sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
       
 65677     }else{
       
 65678       sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
       
 65679     }
       
 65680     pParse->rc = SQLITE_AUTH;
       
 65681   }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
       
 65682     sqliteAuthBadReturnCode(pParse);
       
 65683   }
       
 65684   return rc;
       
 65685 }
       
 65686 
       
 65687 /*
       
 65688 ** The pExpr should be a TK_COLUMN expression.  The table referred to
       
 65689 ** is in pTabList or else it is the NEW or OLD table of a trigger.  
       
 65690 ** Check to see if it is OK to read this particular column.
       
 65691 **
       
 65692 ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN 
       
 65693 ** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
       
 65694 ** then generate an error.
       
 65695 */
       
 65696 SQLITE_PRIVATE void sqlite3AuthRead(
       
 65697   Parse *pParse,        /* The parser context */
       
 65698   Expr *pExpr,          /* The expression to check authorization on */
       
 65699   Schema *pSchema,      /* The schema of the expression */
       
 65700   SrcList *pTabList     /* All table that pExpr might refer to */
       
 65701 ){
       
 65702   sqlite3 *db = pParse->db;
       
 65703   Table *pTab = 0;      /* The table being read */
       
 65704   const char *zCol;     /* Name of the column of the table */
       
 65705   int iSrc;             /* Index in pTabList->a[] of table being read */
       
 65706   int iDb;              /* The index of the database the expression refers to */
       
 65707   int iCol;             /* Index of column in table */
       
 65708 
       
 65709   if( db->xAuth==0 ) return;
       
 65710   iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
       
 65711   if( iDb<0 ){
       
 65712     /* An attempt to read a column out of a subquery or other
       
 65713     ** temporary table. */
       
 65714     return;
       
 65715   }
       
 65716 
       
 65717   assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
       
 65718   if( pExpr->op==TK_TRIGGER ){
       
 65719     pTab = pParse->pTriggerTab;
       
 65720   }else{
       
 65721     assert( pTabList );
       
 65722     for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
       
 65723       if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
       
 65724         pTab = pTabList->a[iSrc].pTab;
       
 65725         break;
       
 65726       }
       
 65727     }
       
 65728   }
       
 65729   iCol = pExpr->iColumn;
       
 65730   if( NEVER(pTab==0) ) return;
       
 65731 
       
 65732   if( iCol>=0 ){
       
 65733     assert( iCol<pTab->nCol );
       
 65734     zCol = pTab->aCol[iCol].zName;
       
 65735   }else if( pTab->iPKey>=0 ){
       
 65736     assert( pTab->iPKey<pTab->nCol );
       
 65737     zCol = pTab->aCol[pTab->iPKey].zName;
       
 65738   }else{
       
 65739     zCol = "ROWID";
       
 65740   }
       
 65741   assert( iDb>=0 && iDb<db->nDb );
       
 65742   if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
       
 65743     pExpr->op = TK_NULL;
       
 65744   }
       
 65745 }
       
 65746 
       
 65747 /*
       
 65748 ** Do an authorization check using the code and arguments given.  Return
       
 65749 ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
       
 65750 ** is returned, then the error count and error message in pParse are
       
 65751 ** modified appropriately.
       
 65752 */
       
 65753 SQLITE_PRIVATE int sqlite3AuthCheck(
       
 65754   Parse *pParse,
       
 65755   int code,
       
 65756   const char *zArg1,
       
 65757   const char *zArg2,
       
 65758   const char *zArg3
       
 65759 ){
       
 65760   sqlite3 *db = pParse->db;
       
 65761   int rc;
       
 65762 
       
 65763   /* Don't do any authorization checks if the database is initialising
       
 65764   ** or if the parser is being invoked from within sqlite3_declare_vtab.
       
 65765   */
       
 65766   if( db->init.busy || IN_DECLARE_VTAB ){
       
 65767     return SQLITE_OK;
       
 65768   }
       
 65769 
       
 65770   if( db->xAuth==0 ){
       
 65771     return SQLITE_OK;
       
 65772   }
       
 65773   rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
       
 65774   if( rc==SQLITE_DENY ){
       
 65775     sqlite3ErrorMsg(pParse, "not authorized");
       
 65776     pParse->rc = SQLITE_AUTH;
       
 65777   }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
       
 65778     rc = SQLITE_DENY;
       
 65779     sqliteAuthBadReturnCode(pParse);
       
 65780   }
       
 65781   return rc;
       
 65782 }
       
 65783 
       
 65784 /*
       
 65785 ** Push an authorization context.  After this routine is called, the
       
 65786 ** zArg3 argument to authorization callbacks will be zContext until
       
 65787 ** popped.  Or if pParse==0, this routine is a no-op.
       
 65788 */
       
 65789 SQLITE_PRIVATE void sqlite3AuthContextPush(
       
 65790   Parse *pParse,
       
 65791   AuthContext *pContext, 
       
 65792   const char *zContext
       
 65793 ){
       
 65794   assert( pParse );
       
 65795   pContext->pParse = pParse;
       
 65796   pContext->zAuthContext = pParse->zAuthContext;
       
 65797   pParse->zAuthContext = zContext;
       
 65798 }
       
 65799 
       
 65800 /*
       
 65801 ** Pop an authorization context that was previously pushed
       
 65802 ** by sqlite3AuthContextPush
       
 65803 */
       
 65804 SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
       
 65805   if( pContext->pParse ){
       
 65806     pContext->pParse->zAuthContext = pContext->zAuthContext;
       
 65807     pContext->pParse = 0;
       
 65808   }
       
 65809 }
       
 65810 
       
 65811 #endif /* SQLITE_OMIT_AUTHORIZATION */
       
 65812 
       
 65813 /************** End of auth.c ************************************************/
       
 65814 /************** Begin file build.c *******************************************/
       
 65815 /*
       
 65816 ** 2001 September 15
       
 65817 **
       
 65818 ** The author disclaims copyright to this source code.  In place of
       
 65819 ** a legal notice, here is a blessing:
       
 65820 **
       
 65821 **    May you do good and not evil.
       
 65822 **    May you find forgiveness for yourself and forgive others.
       
 65823 **    May you share freely, never taking more than you give.
       
 65824 **
       
 65825 *************************************************************************
       
 65826 ** This file contains C code routines that are called by the SQLite parser
       
 65827 ** when syntax rules are reduced.  The routines in this file handle the
       
 65828 ** following kinds of SQL syntax:
       
 65829 **
       
 65830 **     CREATE TABLE
       
 65831 **     DROP TABLE
       
 65832 **     CREATE INDEX
       
 65833 **     DROP INDEX
       
 65834 **     creating ID lists
       
 65835 **     BEGIN TRANSACTION
       
 65836 **     COMMIT
       
 65837 **     ROLLBACK
       
 65838 **
       
 65839 ** $Id: build.c,v 1.557 2009/07/24 17:58:53 danielk1977 Exp $
       
 65840 */
       
 65841 
       
 65842 /*
       
 65843 ** This routine is called when a new SQL statement is beginning to
       
 65844 ** be parsed.  Initialize the pParse structure as needed.
       
 65845 */
       
 65846 SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
       
 65847   pParse->explain = (u8)explainFlag;
       
 65848   pParse->nVar = 0;
       
 65849 }
       
 65850 
       
 65851 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 65852 /*
       
 65853 ** The TableLock structure is only used by the sqlite3TableLock() and
       
 65854 ** codeTableLocks() functions.
       
 65855 */
       
 65856 struct TableLock {
       
 65857   int iDb;             /* The database containing the table to be locked */
       
 65858   int iTab;            /* The root page of the table to be locked */
       
 65859   u8 isWriteLock;      /* True for write lock.  False for a read lock */
       
 65860   const char *zName;   /* Name of the table */
       
 65861 };
       
 65862 
       
 65863 /*
       
 65864 ** Record the fact that we want to lock a table at run-time.  
       
 65865 **
       
 65866 ** The table to be locked has root page iTab and is found in database iDb.
       
 65867 ** A read or a write lock can be taken depending on isWritelock.
       
 65868 **
       
 65869 ** This routine just records the fact that the lock is desired.  The
       
 65870 ** code to make the lock occur is generated by a later call to
       
 65871 ** codeTableLocks() which occurs during sqlite3FinishCoding().
       
 65872 */
       
 65873 SQLITE_PRIVATE void sqlite3TableLock(
       
 65874   Parse *pParse,     /* Parsing context */
       
 65875   int iDb,           /* Index of the database containing the table to lock */
       
 65876   int iTab,          /* Root page number of the table to be locked */
       
 65877   u8 isWriteLock,    /* True for a write lock */
       
 65878   const char *zName  /* Name of the table to be locked */
       
 65879 ){
       
 65880   Parse *pToplevel = sqlite3ParseToplevel(pParse);
       
 65881   int i;
       
 65882   int nBytes;
       
 65883   TableLock *p;
       
 65884   assert( iDb>=0 );
       
 65885 
       
 65886   for(i=0; i<pToplevel->nTableLock; i++){
       
 65887     p = &pToplevel->aTableLock[i];
       
 65888     if( p->iDb==iDb && p->iTab==iTab ){
       
 65889       p->isWriteLock = (p->isWriteLock || isWriteLock);
       
 65890       return;
       
 65891     }
       
 65892   }
       
 65893 
       
 65894   nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
       
 65895   pToplevel->aTableLock =
       
 65896       sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
       
 65897   if( pToplevel->aTableLock ){
       
 65898     p = &pToplevel->aTableLock[pToplevel->nTableLock++];
       
 65899     p->iDb = iDb;
       
 65900     p->iTab = iTab;
       
 65901     p->isWriteLock = isWriteLock;
       
 65902     p->zName = zName;
       
 65903   }else{
       
 65904     pToplevel->nTableLock = 0;
       
 65905     pToplevel->db->mallocFailed = 1;
       
 65906   }
       
 65907 }
       
 65908 
       
 65909 /*
       
 65910 ** Code an OP_TableLock instruction for each table locked by the
       
 65911 ** statement (configured by calls to sqlite3TableLock()).
       
 65912 */
       
 65913 static void codeTableLocks(Parse *pParse){
       
 65914   int i;
       
 65915   Vdbe *pVdbe; 
       
 65916 
       
 65917   pVdbe = sqlite3GetVdbe(pParse);
       
 65918   assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
       
 65919 
       
 65920   for(i=0; i<pParse->nTableLock; i++){
       
 65921     TableLock *p = &pParse->aTableLock[i];
       
 65922     int p1 = p->iDb;
       
 65923     sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
       
 65924                       p->zName, P4_STATIC);
       
 65925   }
       
 65926 }
       
 65927 #else
       
 65928   #define codeTableLocks(x)
       
 65929 #endif
       
 65930 
       
 65931 /*
       
 65932 ** This routine is called after a single SQL statement has been
       
 65933 ** parsed and a VDBE program to execute that statement has been
       
 65934 ** prepared.  This routine puts the finishing touches on the
       
 65935 ** VDBE program and resets the pParse structure for the next
       
 65936 ** parse.
       
 65937 **
       
 65938 ** Note that if an error occurred, it might be the case that
       
 65939 ** no VDBE code was generated.
       
 65940 */
       
 65941 SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
       
 65942   sqlite3 *db;
       
 65943   Vdbe *v;
       
 65944 
       
 65945   db = pParse->db;
       
 65946   if( db->mallocFailed ) return;
       
 65947   if( pParse->nested ) return;
       
 65948   if( pParse->nErr ) return;
       
 65949 
       
 65950   /* Begin by generating some termination code at the end of the
       
 65951   ** vdbe program
       
 65952   */
       
 65953   v = sqlite3GetVdbe(pParse);
       
 65954   assert( !pParse->isMultiWrite 
       
 65955        || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
       
 65956   if( v ){
       
 65957     sqlite3VdbeAddOp0(v, OP_Halt);
       
 65958 
       
 65959     /* The cookie mask contains one bit for each database file open.
       
 65960     ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
       
 65961     ** set for each database that is used.  Generate code to start a
       
 65962     ** transaction on each used database and to verify the schema cookie
       
 65963     ** on each used database.
       
 65964     */
       
 65965     if( pParse->cookieGoto>0 ){
       
 65966       u32 mask;
       
 65967       int iDb;
       
 65968       sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
       
 65969       for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
       
 65970         if( (mask & pParse->cookieMask)==0 ) continue;
       
 65971         sqlite3VdbeUsesBtree(v, iDb);
       
 65972         sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
       
 65973         if( db->init.busy==0 ){
       
 65974           sqlite3VdbeAddOp2(v,OP_VerifyCookie, iDb, pParse->cookieValue[iDb]);
       
 65975         }
       
 65976       }
       
 65977 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 65978       {
       
 65979         int i;
       
 65980         for(i=0; i<pParse->nVtabLock; i++){
       
 65981           char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
       
 65982           sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
       
 65983         }
       
 65984         pParse->nVtabLock = 0;
       
 65985       }
       
 65986 #endif
       
 65987 
       
 65988       /* Once all the cookies have been verified and transactions opened, 
       
 65989       ** obtain the required table-locks. This is a no-op unless the 
       
 65990       ** shared-cache feature is enabled.
       
 65991       */
       
 65992       codeTableLocks(pParse);
       
 65993 
       
 65994       /* Initialize any AUTOINCREMENT data structures required.
       
 65995       */
       
 65996       sqlite3AutoincrementBegin(pParse);
       
 65997 
       
 65998       /* Finally, jump back to the beginning of the executable code. */
       
 65999       sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
       
 66000     }
       
 66001   }
       
 66002 
       
 66003 
       
 66004   /* Get the VDBE program ready for execution
       
 66005   */
       
 66006   if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
       
 66007 #ifdef SQLITE_DEBUG
       
 66008     FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
       
 66009     sqlite3VdbeTrace(v, trace);
       
 66010 #endif
       
 66011     assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
       
 66012     /* A minimum of one cursor is required if autoincrement is used
       
 66013     *  See ticket [a696379c1f08866] */
       
 66014     if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
       
 66015     sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem,
       
 66016                          pParse->nTab, pParse->nMaxArg, pParse->explain,
       
 66017                          pParse->isMultiWrite && pParse->mayAbort);
       
 66018     pParse->rc = SQLITE_DONE;
       
 66019     pParse->colNamesSet = 0;
       
 66020   }else if( pParse->rc==SQLITE_OK ){
       
 66021     pParse->rc = SQLITE_ERROR;
       
 66022   }
       
 66023   pParse->nTab = 0;
       
 66024   pParse->nMem = 0;
       
 66025   pParse->nSet = 0;
       
 66026   pParse->nVar = 0;
       
 66027   pParse->cookieMask = 0;
       
 66028   pParse->cookieGoto = 0;
       
 66029 }
       
 66030 
       
 66031 /*
       
 66032 ** Run the parser and code generator recursively in order to generate
       
 66033 ** code for the SQL statement given onto the end of the pParse context
       
 66034 ** currently under construction.  When the parser is run recursively
       
 66035 ** this way, the final OP_Halt is not appended and other initialization
       
 66036 ** and finalization steps are omitted because those are handling by the
       
 66037 ** outermost parser.
       
 66038 **
       
 66039 ** Not everything is nestable.  This facility is designed to permit
       
 66040 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER.  Use
       
 66041 ** care if you decide to try to use this routine for some other purposes.
       
 66042 */
       
 66043 SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
       
 66044   va_list ap;
       
 66045   char *zSql;
       
 66046   char *zErrMsg = 0;
       
 66047   sqlite3 *db = pParse->db;
       
 66048 # define SAVE_SZ  (sizeof(Parse) - offsetof(Parse,nVar))
       
 66049   char saveBuf[SAVE_SZ];
       
 66050 
       
 66051   if( pParse->nErr ) return;
       
 66052   assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
       
 66053   va_start(ap, zFormat);
       
 66054   zSql = sqlite3VMPrintf(db, zFormat, ap);
       
 66055   va_end(ap);
       
 66056   if( zSql==0 ){
       
 66057     return;   /* A malloc must have failed */
       
 66058   }
       
 66059   pParse->nested++;
       
 66060   memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
       
 66061   memset(&pParse->nVar, 0, SAVE_SZ);
       
 66062   sqlite3RunParser(pParse, zSql, &zErrMsg);
       
 66063   sqlite3DbFree(db, zErrMsg);
       
 66064   sqlite3DbFree(db, zSql);
       
 66065   memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
       
 66066   pParse->nested--;
       
 66067 }
       
 66068 
       
 66069 /*
       
 66070 ** Locate the in-memory structure that describes a particular database
       
 66071 ** table given the name of that table and (optionally) the name of the
       
 66072 ** database containing the table.  Return NULL if not found.
       
 66073 **
       
 66074 ** If zDatabase is 0, all databases are searched for the table and the
       
 66075 ** first matching table is returned.  (No checking for duplicate table
       
 66076 ** names is done.)  The search order is TEMP first, then MAIN, then any
       
 66077 ** auxiliary databases added using the ATTACH command.
       
 66078 **
       
 66079 ** See also sqlite3LocateTable().
       
 66080 */
       
 66081 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
       
 66082   Table *p = 0;
       
 66083   int i;
       
 66084   int nName;
       
 66085   assert( zName!=0 );
       
 66086   nName = sqlite3Strlen30(zName);
       
 66087   for(i=OMIT_TEMPDB; i<db->nDb; i++){
       
 66088     int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
       
 66089     if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
       
 66090     p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
       
 66091     if( p ) break;
       
 66092   }
       
 66093   return p;
       
 66094 }
       
 66095 
       
 66096 /*
       
 66097 ** Locate the in-memory structure that describes a particular database
       
 66098 ** table given the name of that table and (optionally) the name of the
       
 66099 ** database containing the table.  Return NULL if not found.  Also leave an
       
 66100 ** error message in pParse->zErrMsg.
       
 66101 **
       
 66102 ** The difference between this routine and sqlite3FindTable() is that this
       
 66103 ** routine leaves an error message in pParse->zErrMsg where
       
 66104 ** sqlite3FindTable() does not.
       
 66105 */
       
 66106 SQLITE_PRIVATE Table *sqlite3LocateTable(
       
 66107   Parse *pParse,         /* context in which to report errors */
       
 66108   int isView,            /* True if looking for a VIEW rather than a TABLE */
       
 66109   const char *zName,     /* Name of the table we are looking for */
       
 66110   const char *zDbase     /* Name of the database.  Might be NULL */
       
 66111 ){
       
 66112   Table *p;
       
 66113 
       
 66114   /* Read the database schema. If an error occurs, leave an error message
       
 66115   ** and code in pParse and return NULL. */
       
 66116   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
       
 66117     return 0;
       
 66118   }
       
 66119 
       
 66120   p = sqlite3FindTable(pParse->db, zName, zDbase);
       
 66121   if( p==0 ){
       
 66122     const char *zMsg = isView ? "no such view" : "no such table";
       
 66123     if( zDbase ){
       
 66124       sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
       
 66125     }else{
       
 66126       sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
       
 66127     }
       
 66128     pParse->checkSchema = 1;
       
 66129   }
       
 66130   return p;
       
 66131 }
       
 66132 
       
 66133 /*
       
 66134 ** Locate the in-memory structure that describes 
       
 66135 ** a particular index given the name of that index
       
 66136 ** and the name of the database that contains the index.
       
 66137 ** Return NULL if not found.
       
 66138 **
       
 66139 ** If zDatabase is 0, all databases are searched for the
       
 66140 ** table and the first matching index is returned.  (No checking
       
 66141 ** for duplicate index names is done.)  The search order is
       
 66142 ** TEMP first, then MAIN, then any auxiliary databases added
       
 66143 ** using the ATTACH command.
       
 66144 */
       
 66145 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
       
 66146   Index *p = 0;
       
 66147   int i;
       
 66148   int nName = sqlite3Strlen30(zName);
       
 66149   for(i=OMIT_TEMPDB; i<db->nDb; i++){
       
 66150     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
       
 66151     Schema *pSchema = db->aDb[j].pSchema;
       
 66152     assert( pSchema );
       
 66153     if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
       
 66154     p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
       
 66155     if( p ) break;
       
 66156   }
       
 66157   return p;
       
 66158 }
       
 66159 
       
 66160 /*
       
 66161 ** Reclaim the memory used by an index
       
 66162 */
       
 66163 static void freeIndex(Index *p){
       
 66164   sqlite3 *db = p->pTable->dbMem;
       
 66165 #ifndef SQLITE_OMIT_ANALYZE
       
 66166   sqlite3DeleteIndexSamples(p);
       
 66167 #endif
       
 66168   sqlite3DbFree(db, p->zColAff);
       
 66169   sqlite3DbFree(db, p);
       
 66170 }
       
 66171 
       
 66172 /*
       
 66173 ** Remove the given index from the index hash table, and free
       
 66174 ** its memory structures.
       
 66175 **
       
 66176 ** The index is removed from the database hash tables but
       
 66177 ** it is not unlinked from the Table that it indexes.
       
 66178 ** Unlinking from the Table must be done by the calling function.
       
 66179 */
       
 66180 static void sqlite3DeleteIndex(Index *p){
       
 66181   Index *pOld;
       
 66182   const char *zName = p->zName;
       
 66183 
       
 66184   pOld = sqlite3HashInsert(&p->pSchema->idxHash, zName,
       
 66185                            sqlite3Strlen30(zName), 0);
       
 66186   assert( pOld==0 || pOld==p );
       
 66187   freeIndex(p);
       
 66188 }
       
 66189 
       
 66190 /*
       
 66191 ** For the index called zIdxName which is found in the database iDb,
       
 66192 ** unlike that index from its Table then remove the index from
       
 66193 ** the index hash table and free all memory structures associated
       
 66194 ** with the index.
       
 66195 */
       
 66196 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
       
 66197   Index *pIndex;
       
 66198   int len;
       
 66199   Hash *pHash = &db->aDb[iDb].pSchema->idxHash;
       
 66200 
       
 66201   len = sqlite3Strlen30(zIdxName);
       
 66202   pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
       
 66203   if( pIndex ){
       
 66204     if( pIndex->pTable->pIndex==pIndex ){
       
 66205       pIndex->pTable->pIndex = pIndex->pNext;
       
 66206     }else{
       
 66207       Index *p;
       
 66208       /* Justification of ALWAYS();  The index must be on the list of
       
 66209       ** indices. */
       
 66210       p = pIndex->pTable->pIndex;
       
 66211       while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
       
 66212       if( ALWAYS(p && p->pNext==pIndex) ){
       
 66213         p->pNext = pIndex->pNext;
       
 66214       }
       
 66215     }
       
 66216     freeIndex(pIndex);
       
 66217   }
       
 66218   db->flags |= SQLITE_InternChanges;
       
 66219 }
       
 66220 
       
 66221 /*
       
 66222 ** Erase all schema information from the in-memory hash tables of
       
 66223 ** a single database.  This routine is called to reclaim memory
       
 66224 ** before the database closes.  It is also called during a rollback
       
 66225 ** if there were schema changes during the transaction or if a
       
 66226 ** schema-cookie mismatch occurs.
       
 66227 **
       
 66228 ** If iDb==0 then reset the internal schema tables for all database
       
 66229 ** files.  If iDb>=1 then reset the internal schema for only the
       
 66230 ** single file indicated.
       
 66231 */
       
 66232 SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
       
 66233   int i, j;
       
 66234   assert( iDb>=0 && iDb<db->nDb );
       
 66235 
       
 66236   if( iDb==0 ){
       
 66237     sqlite3BtreeEnterAll(db);
       
 66238   }
       
 66239   for(i=iDb; i<db->nDb; i++){
       
 66240     Db *pDb = &db->aDb[i];
       
 66241     if( pDb->pSchema ){
       
 66242       assert(i==1 || (pDb->pBt && sqlite3BtreeHoldsMutex(pDb->pBt)));
       
 66243       sqlite3SchemaFree(pDb->pSchema);
       
 66244     }
       
 66245     if( iDb>0 ) return;
       
 66246   }
       
 66247   assert( iDb==0 );
       
 66248   db->flags &= ~SQLITE_InternChanges;
       
 66249   sqlite3VtabUnlockList(db);
       
 66250   sqlite3BtreeLeaveAll(db);
       
 66251 
       
 66252   /* If one or more of the auxiliary database files has been closed,
       
 66253   ** then remove them from the auxiliary database list.  We take the
       
 66254   ** opportunity to do this here since we have just deleted all of the
       
 66255   ** schema hash tables and therefore do not have to make any changes
       
 66256   ** to any of those tables.
       
 66257   */
       
 66258   for(i=j=2; i<db->nDb; i++){
       
 66259     struct Db *pDb = &db->aDb[i];
       
 66260     if( pDb->pBt==0 ){
       
 66261       sqlite3DbFree(db, pDb->zName);
       
 66262       pDb->zName = 0;
       
 66263       continue;
       
 66264     }
       
 66265     if( j<i ){
       
 66266       db->aDb[j] = db->aDb[i];
       
 66267     }
       
 66268     j++;
       
 66269   }
       
 66270   memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
       
 66271   db->nDb = j;
       
 66272   if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
       
 66273     memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
       
 66274     sqlite3DbFree(db, db->aDb);
       
 66275     db->aDb = db->aDbStatic;
       
 66276   }
       
 66277 }
       
 66278 
       
 66279 /*
       
 66280 ** This routine is called when a commit occurs.
       
 66281 */
       
 66282 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
       
 66283   db->flags &= ~SQLITE_InternChanges;
       
 66284 }
       
 66285 
       
 66286 /*
       
 66287 ** Clear the column names from a table or view.
       
 66288 */
       
 66289 static void sqliteResetColumnNames(Table *pTable){
       
 66290   int i;
       
 66291   Column *pCol;
       
 66292   sqlite3 *db = pTable->dbMem;
       
 66293   testcase( db==0 );
       
 66294   assert( pTable!=0 );
       
 66295   if( (pCol = pTable->aCol)!=0 ){
       
 66296     for(i=0; i<pTable->nCol; i++, pCol++){
       
 66297       sqlite3DbFree(db, pCol->zName);
       
 66298       sqlite3ExprDelete(db, pCol->pDflt);
       
 66299       sqlite3DbFree(db, pCol->zDflt);
       
 66300       sqlite3DbFree(db, pCol->zType);
       
 66301       sqlite3DbFree(db, pCol->zColl);
       
 66302     }
       
 66303     sqlite3DbFree(db, pTable->aCol);
       
 66304   }
       
 66305   pTable->aCol = 0;
       
 66306   pTable->nCol = 0;
       
 66307 }
       
 66308 
       
 66309 /*
       
 66310 ** Remove the memory data structures associated with the given
       
 66311 ** Table.  No changes are made to disk by this routine.
       
 66312 **
       
 66313 ** This routine just deletes the data structure.  It does not unlink
       
 66314 ** the table data structure from the hash table.  But it does destroy
       
 66315 ** memory structures of the indices and foreign keys associated with 
       
 66316 ** the table.
       
 66317 */
       
 66318 SQLITE_PRIVATE void sqlite3DeleteTable(Table *pTable){
       
 66319   Index *pIndex, *pNext;
       
 66320   sqlite3 *db;
       
 66321 
       
 66322   if( pTable==0 ) return;
       
 66323   db = pTable->dbMem;
       
 66324   testcase( db==0 );
       
 66325 
       
 66326   /* Do not delete the table until the reference count reaches zero. */
       
 66327   pTable->nRef--;
       
 66328   if( pTable->nRef>0 ){
       
 66329     return;
       
 66330   }
       
 66331   assert( pTable->nRef==0 );
       
 66332 
       
 66333   /* Delete all indices associated with this table
       
 66334   */
       
 66335   for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
       
 66336     pNext = pIndex->pNext;
       
 66337     assert( pIndex->pSchema==pTable->pSchema );
       
 66338     sqlite3DeleteIndex(pIndex);
       
 66339   }
       
 66340 
       
 66341   /* Delete any foreign keys attached to this table. */
       
 66342   sqlite3FkDelete(pTable);
       
 66343 
       
 66344   /* Delete the Table structure itself.
       
 66345   */
       
 66346   sqliteResetColumnNames(pTable);
       
 66347   sqlite3DbFree(db, pTable->zName);
       
 66348   sqlite3DbFree(db, pTable->zColAff);
       
 66349   sqlite3SelectDelete(db, pTable->pSelect);
       
 66350 #ifndef SQLITE_OMIT_CHECK
       
 66351   sqlite3ExprDelete(db, pTable->pCheck);
       
 66352 #endif
       
 66353   sqlite3VtabClear(pTable);
       
 66354   sqlite3DbFree(db, pTable);
       
 66355 }
       
 66356 
       
 66357 /*
       
 66358 ** Unlink the given table from the hash tables and the delete the
       
 66359 ** table structure with all its indices and foreign keys.
       
 66360 */
       
 66361 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
       
 66362   Table *p;
       
 66363   Db *pDb;
       
 66364 
       
 66365   assert( db!=0 );
       
 66366   assert( iDb>=0 && iDb<db->nDb );
       
 66367   assert( zTabName && zTabName[0] );
       
 66368   pDb = &db->aDb[iDb];
       
 66369   p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
       
 66370                         sqlite3Strlen30(zTabName),0);
       
 66371   sqlite3DeleteTable(p);
       
 66372   db->flags |= SQLITE_InternChanges;
       
 66373 }
       
 66374 
       
 66375 /*
       
 66376 ** Given a token, return a string that consists of the text of that
       
 66377 ** token.  Space to hold the returned string
       
 66378 ** is obtained from sqliteMalloc() and must be freed by the calling
       
 66379 ** function.
       
 66380 **
       
 66381 ** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
       
 66382 ** surround the body of the token are removed.
       
 66383 **
       
 66384 ** Tokens are often just pointers into the original SQL text and so
       
 66385 ** are not \000 terminated and are not persistent.  The returned string
       
 66386 ** is \000 terminated and is persistent.
       
 66387 */
       
 66388 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
       
 66389   char *zName;
       
 66390   if( pName ){
       
 66391     zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
       
 66392     sqlite3Dequote(zName);
       
 66393   }else{
       
 66394     zName = 0;
       
 66395   }
       
 66396   return zName;
       
 66397 }
       
 66398 
       
 66399 /*
       
 66400 ** Open the sqlite_master table stored in database number iDb for
       
 66401 ** writing. The table is opened using cursor 0.
       
 66402 */
       
 66403 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
       
 66404   Vdbe *v = sqlite3GetVdbe(p);
       
 66405   sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
       
 66406   sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, MASTER_ROOT, iDb);
       
 66407   sqlite3VdbeChangeP4(v, -1, (char *)5, P4_INT32);  /* 5 column table */
       
 66408   if( p->nTab==0 ){
       
 66409     p->nTab = 1;
       
 66410   }
       
 66411 }
       
 66412 
       
 66413 /*
       
 66414 ** Parameter zName points to a nul-terminated buffer containing the name
       
 66415 ** of a database ("main", "temp" or the name of an attached db). This
       
 66416 ** function returns the index of the named database in db->aDb[], or
       
 66417 ** -1 if the named db cannot be found.
       
 66418 */
       
 66419 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
       
 66420   int i = -1;         /* Database number */
       
 66421   if( zName ){
       
 66422     Db *pDb;
       
 66423     int n = sqlite3Strlen30(zName);
       
 66424     for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
       
 66425       if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) && 
       
 66426           0==sqlite3StrICmp(pDb->zName, zName) ){
       
 66427         break;
       
 66428       }
       
 66429     }
       
 66430   }
       
 66431   return i;
       
 66432 }
       
 66433 
       
 66434 /*
       
 66435 ** The token *pName contains the name of a database (either "main" or
       
 66436 ** "temp" or the name of an attached db). This routine returns the
       
 66437 ** index of the named database in db->aDb[], or -1 if the named db 
       
 66438 ** does not exist.
       
 66439 */
       
 66440 SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
       
 66441   int i;                               /* Database number */
       
 66442   char *zName;                         /* Name we are searching for */
       
 66443   zName = sqlite3NameFromToken(db, pName);
       
 66444   i = sqlite3FindDbName(db, zName);
       
 66445   sqlite3DbFree(db, zName);
       
 66446   return i;
       
 66447 }
       
 66448 
       
 66449 /* The table or view or trigger name is passed to this routine via tokens
       
 66450 ** pName1 and pName2. If the table name was fully qualified, for example:
       
 66451 **
       
 66452 ** CREATE TABLE xxx.yyy (...);
       
 66453 ** 
       
 66454 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
       
 66455 ** the table name is not fully qualified, i.e.:
       
 66456 **
       
 66457 ** CREATE TABLE yyy(...);
       
 66458 **
       
 66459 ** Then pName1 is set to "yyy" and pName2 is "".
       
 66460 **
       
 66461 ** This routine sets the *ppUnqual pointer to point at the token (pName1 or
       
 66462 ** pName2) that stores the unqualified table name.  The index of the
       
 66463 ** database "xxx" is returned.
       
 66464 */
       
 66465 SQLITE_PRIVATE int sqlite3TwoPartName(
       
 66466   Parse *pParse,      /* Parsing and code generating context */
       
 66467   Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
       
 66468   Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
       
 66469   Token **pUnqual     /* Write the unqualified object name here */
       
 66470 ){
       
 66471   int iDb;                    /* Database holding the object */
       
 66472   sqlite3 *db = pParse->db;
       
 66473 
       
 66474   if( ALWAYS(pName2!=0) && pName2->n>0 ){
       
 66475     if( db->init.busy ) {
       
 66476       sqlite3ErrorMsg(pParse, "corrupt database");
       
 66477       pParse->nErr++;
       
 66478       return -1;
       
 66479     }
       
 66480     *pUnqual = pName2;
       
 66481     iDb = sqlite3FindDb(db, pName1);
       
 66482     if( iDb<0 ){
       
 66483       sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
       
 66484       pParse->nErr++;
       
 66485       return -1;
       
 66486     }
       
 66487   }else{
       
 66488     assert( db->init.iDb==0 || db->init.busy );
       
 66489     iDb = db->init.iDb;
       
 66490     *pUnqual = pName1;
       
 66491   }
       
 66492   return iDb;
       
 66493 }
       
 66494 
       
 66495 /*
       
 66496 ** This routine is used to check if the UTF-8 string zName is a legal
       
 66497 ** unqualified name for a new schema object (table, index, view or
       
 66498 ** trigger). All names are legal except those that begin with the string
       
 66499 ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
       
 66500 ** is reserved for internal use.
       
 66501 */
       
 66502 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
       
 66503   if( !pParse->db->init.busy && pParse->nested==0 
       
 66504           && (pParse->db->flags & SQLITE_WriteSchema)==0
       
 66505           && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
       
 66506     sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
       
 66507     return SQLITE_ERROR;
       
 66508   }
       
 66509   return SQLITE_OK;
       
 66510 }
       
 66511 
       
 66512 /*
       
 66513 ** Begin constructing a new table representation in memory.  This is
       
 66514 ** the first of several action routines that get called in response
       
 66515 ** to a CREATE TABLE statement.  In particular, this routine is called
       
 66516 ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
       
 66517 ** flag is true if the table should be stored in the auxiliary database
       
 66518 ** file instead of in the main database file.  This is normally the case
       
 66519 ** when the "TEMP" or "TEMPORARY" keyword occurs in between
       
 66520 ** CREATE and TABLE.
       
 66521 **
       
 66522 ** The new table record is initialized and put in pParse->pNewTable.
       
 66523 ** As more of the CREATE TABLE statement is parsed, additional action
       
 66524 ** routines will be called to add more information to this record.
       
 66525 ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
       
 66526 ** is called to complete the construction of the new table record.
       
 66527 */
       
 66528 SQLITE_PRIVATE void sqlite3StartTable(
       
 66529   Parse *pParse,   /* Parser context */
       
 66530   Token *pName1,   /* First part of the name of the table or view */
       
 66531   Token *pName2,   /* Second part of the name of the table or view */
       
 66532   int isTemp,      /* True if this is a TEMP table */
       
 66533   int isView,      /* True if this is a VIEW */
       
 66534   int isVirtual,   /* True if this is a VIRTUAL table */
       
 66535   int noErr        /* Do nothing if table already exists */
       
 66536 ){
       
 66537   Table *pTable;
       
 66538   char *zName = 0; /* The name of the new table */
       
 66539   sqlite3 *db = pParse->db;
       
 66540   Vdbe *v;
       
 66541   int iDb;         /* Database number to create the table in */
       
 66542   Token *pName;    /* Unqualified name of the table to create */
       
 66543 
       
 66544   /* The table or view name to create is passed to this routine via tokens
       
 66545   ** pName1 and pName2. If the table name was fully qualified, for example:
       
 66546   **
       
 66547   ** CREATE TABLE xxx.yyy (...);
       
 66548   ** 
       
 66549   ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
       
 66550   ** the table name is not fully qualified, i.e.:
       
 66551   **
       
 66552   ** CREATE TABLE yyy(...);
       
 66553   **
       
 66554   ** Then pName1 is set to "yyy" and pName2 is "".
       
 66555   **
       
 66556   ** The call below sets the pName pointer to point at the token (pName1 or
       
 66557   ** pName2) that stores the unqualified table name. The variable iDb is
       
 66558   ** set to the index of the database that the table or view is to be
       
 66559   ** created in.
       
 66560   */
       
 66561   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
       
 66562   if( iDb<0 ) return;
       
 66563   if( !OMIT_TEMPDB && isTemp && iDb>1 ){
       
 66564     /* If creating a temp table, the name may not be qualified */
       
 66565     sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
       
 66566     return;
       
 66567   }
       
 66568   if( !OMIT_TEMPDB && isTemp ) iDb = 1;
       
 66569 
       
 66570   pParse->sNameToken = *pName;
       
 66571   zName = sqlite3NameFromToken(db, pName);
       
 66572   if( zName==0 ) return;
       
 66573   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
       
 66574     goto begin_table_error;
       
 66575   }
       
 66576   if( db->init.iDb==1 ) isTemp = 1;
       
 66577 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 66578   assert( (isTemp & 1)==isTemp );
       
 66579   {
       
 66580     int code;
       
 66581     char *zDb = db->aDb[iDb].zName;
       
 66582     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
       
 66583       goto begin_table_error;
       
 66584     }
       
 66585     if( isView ){
       
 66586       if( !OMIT_TEMPDB && isTemp ){
       
 66587         code = SQLITE_CREATE_TEMP_VIEW;
       
 66588       }else{
       
 66589         code = SQLITE_CREATE_VIEW;
       
 66590       }
       
 66591     }else{
       
 66592       if( !OMIT_TEMPDB && isTemp ){
       
 66593         code = SQLITE_CREATE_TEMP_TABLE;
       
 66594       }else{
       
 66595         code = SQLITE_CREATE_TABLE;
       
 66596       }
       
 66597     }
       
 66598     if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
       
 66599       goto begin_table_error;
       
 66600     }
       
 66601   }
       
 66602 #endif
       
 66603 
       
 66604   /* Make sure the new table name does not collide with an existing
       
 66605   ** index or table name in the same database.  Issue an error message if
       
 66606   ** it does. The exception is if the statement being parsed was passed
       
 66607   ** to an sqlite3_declare_vtab() call. In that case only the column names
       
 66608   ** and types will be used, so there is no need to test for namespace
       
 66609   ** collisions.
       
 66610   */
       
 66611   if( !IN_DECLARE_VTAB ){
       
 66612     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
       
 66613       goto begin_table_error;
       
 66614     }
       
 66615     pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName);
       
 66616     if( pTable ){
       
 66617       if( !noErr ){
       
 66618         sqlite3ErrorMsg(pParse, "table %T already exists", pName);
       
 66619       }
       
 66620       goto begin_table_error;
       
 66621     }
       
 66622     if( sqlite3FindIndex(db, zName, 0)!=0 && (iDb==0 || !db->init.busy) ){
       
 66623       sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
       
 66624       goto begin_table_error;
       
 66625     }
       
 66626   }
       
 66627 
       
 66628   pTable = sqlite3DbMallocZero(db, sizeof(Table));
       
 66629   if( pTable==0 ){
       
 66630     db->mallocFailed = 1;
       
 66631     pParse->rc = SQLITE_NOMEM;
       
 66632     pParse->nErr++;
       
 66633     goto begin_table_error;
       
 66634   }
       
 66635   pTable->zName = zName;
       
 66636   pTable->iPKey = -1;
       
 66637   pTable->pSchema = db->aDb[iDb].pSchema;
       
 66638   pTable->nRef = 1;
       
 66639   pTable->dbMem = 0;
       
 66640   assert( pParse->pNewTable==0 );
       
 66641   pParse->pNewTable = pTable;
       
 66642 
       
 66643   /* If this is the magic sqlite_sequence table used by autoincrement,
       
 66644   ** then record a pointer to this table in the main database structure
       
 66645   ** so that INSERT can find the table easily.
       
 66646   */
       
 66647 #ifndef SQLITE_OMIT_AUTOINCREMENT
       
 66648   if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
       
 66649     pTable->pSchema->pSeqTab = pTable;
       
 66650   }
       
 66651 #endif
       
 66652 
       
 66653   /* Begin generating the code that will insert the table record into
       
 66654   ** the SQLITE_MASTER table.  Note in particular that we must go ahead
       
 66655   ** and allocate the record number for the table entry now.  Before any
       
 66656   ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
       
 66657   ** indices to be created and the table record must come before the 
       
 66658   ** indices.  Hence, the record number for the table must be allocated
       
 66659   ** now.
       
 66660   */
       
 66661   if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
       
 66662     int j1;
       
 66663     int fileFormat;
       
 66664     int reg1, reg2, reg3;
       
 66665     sqlite3BeginWriteOperation(pParse, 0, iDb);
       
 66666 
       
 66667 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 66668     if( isVirtual ){
       
 66669       sqlite3VdbeAddOp0(v, OP_VBegin);
       
 66670     }
       
 66671 #endif
       
 66672 
       
 66673     /* If the file format and encoding in the database have not been set, 
       
 66674     ** set them now.
       
 66675     */
       
 66676     reg1 = pParse->regRowid = ++pParse->nMem;
       
 66677     reg2 = pParse->regRoot = ++pParse->nMem;
       
 66678     reg3 = ++pParse->nMem;
       
 66679     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
       
 66680     sqlite3VdbeUsesBtree(v, iDb);
       
 66681     j1 = sqlite3VdbeAddOp1(v, OP_If, reg3);
       
 66682     fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
       
 66683                   1 : SQLITE_MAX_FILE_FORMAT;
       
 66684     sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
       
 66685     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
       
 66686     sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
       
 66687     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
       
 66688     sqlite3VdbeJumpHere(v, j1);
       
 66689 
       
 66690     /* This just creates a place-holder record in the sqlite_master table.
       
 66691     ** The record created does not contain anything yet.  It will be replaced
       
 66692     ** by the real entry in code generated at sqlite3EndTable().
       
 66693     **
       
 66694     ** The rowid for the new entry is left in register pParse->regRowid.
       
 66695     ** The root page number of the new table is left in reg pParse->regRoot.
       
 66696     ** The rowid and root page number values are needed by the code that
       
 66697     ** sqlite3EndTable will generate.
       
 66698     */
       
 66699 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
       
 66700     if( isView || isVirtual ){
       
 66701       sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
       
 66702     }else
       
 66703 #endif
       
 66704     {
       
 66705       sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
       
 66706     }
       
 66707     sqlite3OpenMasterTable(pParse, iDb);
       
 66708     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
       
 66709     sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
       
 66710     sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
       
 66711     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
       
 66712     sqlite3VdbeAddOp0(v, OP_Close);
       
 66713   }
       
 66714 
       
 66715   /* Normal (non-error) return. */
       
 66716   return;
       
 66717 
       
 66718   /* If an error occurs, we jump here */
       
 66719 begin_table_error:
       
 66720   sqlite3DbFree(db, zName);
       
 66721   return;
       
 66722 }
       
 66723 
       
 66724 /*
       
 66725 ** This macro is used to compare two strings in a case-insensitive manner.
       
 66726 ** It is slightly faster than calling sqlite3StrICmp() directly, but
       
 66727 ** produces larger code.
       
 66728 **
       
 66729 ** WARNING: This macro is not compatible with the strcmp() family. It
       
 66730 ** returns true if the two strings are equal, otherwise false.
       
 66731 */
       
 66732 #define STRICMP(x, y) (\
       
 66733 sqlite3UpperToLower[*(unsigned char *)(x)]==   \
       
 66734 sqlite3UpperToLower[*(unsigned char *)(y)]     \
       
 66735 && sqlite3StrICmp((x)+1,(y)+1)==0 )
       
 66736 
       
 66737 /*
       
 66738 ** Add a new column to the table currently being constructed.
       
 66739 **
       
 66740 ** The parser calls this routine once for each column declaration
       
 66741 ** in a CREATE TABLE statement.  sqlite3StartTable() gets called
       
 66742 ** first to get things going.  Then this routine is called for each
       
 66743 ** column.
       
 66744 */
       
 66745 SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
       
 66746   Table *p;
       
 66747   int i;
       
 66748   char *z;
       
 66749   Column *pCol;
       
 66750   sqlite3 *db = pParse->db;
       
 66751   if( (p = pParse->pNewTable)==0 ) return;
       
 66752 #if SQLITE_MAX_COLUMN
       
 66753   if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
       
 66754     sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
       
 66755     return;
       
 66756   }
       
 66757 #endif
       
 66758   z = sqlite3NameFromToken(db, pName);
       
 66759   if( z==0 ) return;
       
 66760   for(i=0; i<p->nCol; i++){
       
 66761     if( STRICMP(z, p->aCol[i].zName) ){
       
 66762       sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
       
 66763       sqlite3DbFree(db, z);
       
 66764       return;
       
 66765     }
       
 66766   }
       
 66767   if( (p->nCol & 0x7)==0 ){
       
 66768     Column *aNew;
       
 66769     aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
       
 66770     if( aNew==0 ){
       
 66771       sqlite3DbFree(db, z);
       
 66772       return;
       
 66773     }
       
 66774     p->aCol = aNew;
       
 66775   }
       
 66776   pCol = &p->aCol[p->nCol];
       
 66777   memset(pCol, 0, sizeof(p->aCol[0]));
       
 66778   pCol->zName = z;
       
 66779  
       
 66780   /* If there is no type specified, columns have the default affinity
       
 66781   ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
       
 66782   ** be called next to set pCol->affinity correctly.
       
 66783   */
       
 66784   pCol->affinity = SQLITE_AFF_NONE;
       
 66785   p->nCol++;
       
 66786 }
       
 66787 
       
 66788 /*
       
 66789 ** This routine is called by the parser while in the middle of
       
 66790 ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
       
 66791 ** been seen on a column.  This routine sets the notNull flag on
       
 66792 ** the column currently under construction.
       
 66793 */
       
 66794 SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
       
 66795   Table *p;
       
 66796   p = pParse->pNewTable;
       
 66797   if( p==0 || NEVER(p->nCol<1) ) return;
       
 66798   p->aCol[p->nCol-1].notNull = (u8)onError;
       
 66799 }
       
 66800 
       
 66801 /*
       
 66802 ** Scan the column type name zType (length nType) and return the
       
 66803 ** associated affinity type.
       
 66804 **
       
 66805 ** This routine does a case-independent search of zType for the 
       
 66806 ** substrings in the following table. If one of the substrings is
       
 66807 ** found, the corresponding affinity is returned. If zType contains
       
 66808 ** more than one of the substrings, entries toward the top of 
       
 66809 ** the table take priority. For example, if zType is 'BLOBINT', 
       
 66810 ** SQLITE_AFF_INTEGER is returned.
       
 66811 **
       
 66812 ** Substring     | Affinity
       
 66813 ** --------------------------------
       
 66814 ** 'INT'         | SQLITE_AFF_INTEGER
       
 66815 ** 'CHAR'        | SQLITE_AFF_TEXT
       
 66816 ** 'CLOB'        | SQLITE_AFF_TEXT
       
 66817 ** 'TEXT'        | SQLITE_AFF_TEXT
       
 66818 ** 'BLOB'        | SQLITE_AFF_NONE
       
 66819 ** 'REAL'        | SQLITE_AFF_REAL
       
 66820 ** 'FLOA'        | SQLITE_AFF_REAL
       
 66821 ** 'DOUB'        | SQLITE_AFF_REAL
       
 66822 **
       
 66823 ** If none of the substrings in the above table are found,
       
 66824 ** SQLITE_AFF_NUMERIC is returned.
       
 66825 */
       
 66826 SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn){
       
 66827   u32 h = 0;
       
 66828   char aff = SQLITE_AFF_NUMERIC;
       
 66829 
       
 66830   if( zIn ) while( zIn[0] ){
       
 66831     h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
       
 66832     zIn++;
       
 66833     if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
       
 66834       aff = SQLITE_AFF_TEXT; 
       
 66835     }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
       
 66836       aff = SQLITE_AFF_TEXT;
       
 66837     }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
       
 66838       aff = SQLITE_AFF_TEXT;
       
 66839     }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
       
 66840         && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
       
 66841       aff = SQLITE_AFF_NONE;
       
 66842 #ifndef SQLITE_OMIT_FLOATING_POINT
       
 66843     }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
       
 66844         && aff==SQLITE_AFF_NUMERIC ){
       
 66845       aff = SQLITE_AFF_REAL;
       
 66846     }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
       
 66847         && aff==SQLITE_AFF_NUMERIC ){
       
 66848       aff = SQLITE_AFF_REAL;
       
 66849     }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
       
 66850         && aff==SQLITE_AFF_NUMERIC ){
       
 66851       aff = SQLITE_AFF_REAL;
       
 66852 #endif
       
 66853     }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
       
 66854       aff = SQLITE_AFF_INTEGER;
       
 66855       break;
       
 66856     }
       
 66857   }
       
 66858 
       
 66859   return aff;
       
 66860 }
       
 66861 
       
 66862 /*
       
 66863 ** This routine is called by the parser while in the middle of
       
 66864 ** parsing a CREATE TABLE statement.  The pFirst token is the first
       
 66865 ** token in the sequence of tokens that describe the type of the
       
 66866 ** column currently under construction.   pLast is the last token
       
 66867 ** in the sequence.  Use this information to construct a string
       
 66868 ** that contains the typename of the column and store that string
       
 66869 ** in zType.
       
 66870 */ 
       
 66871 SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
       
 66872   Table *p;
       
 66873   Column *pCol;
       
 66874 
       
 66875   p = pParse->pNewTable;
       
 66876   if( p==0 || NEVER(p->nCol<1) ) return;
       
 66877   pCol = &p->aCol[p->nCol-1];
       
 66878   assert( pCol->zType==0 );
       
 66879   pCol->zType = sqlite3NameFromToken(pParse->db, pType);
       
 66880   pCol->affinity = sqlite3AffinityType(pCol->zType);
       
 66881 }
       
 66882 
       
 66883 /*
       
 66884 ** The expression is the default value for the most recently added column
       
 66885 ** of the table currently under construction.
       
 66886 **
       
 66887 ** Default value expressions must be constant.  Raise an exception if this
       
 66888 ** is not the case.
       
 66889 **
       
 66890 ** This routine is called by the parser while in the middle of
       
 66891 ** parsing a CREATE TABLE statement.
       
 66892 */
       
 66893 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
       
 66894   Table *p;
       
 66895   Column *pCol;
       
 66896   sqlite3 *db = pParse->db;
       
 66897   p = pParse->pNewTable;
       
 66898   if( p!=0 ){
       
 66899     pCol = &(p->aCol[p->nCol-1]);
       
 66900     if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
       
 66901       sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
       
 66902           pCol->zName);
       
 66903     }else{
       
 66904       /* A copy of pExpr is used instead of the original, as pExpr contains
       
 66905       ** tokens that point to volatile memory. The 'span' of the expression
       
 66906       ** is required by pragma table_info.
       
 66907       */
       
 66908       sqlite3ExprDelete(db, pCol->pDflt);
       
 66909       pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
       
 66910       sqlite3DbFree(db, pCol->zDflt);
       
 66911       pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
       
 66912                                      (int)(pSpan->zEnd - pSpan->zStart));
       
 66913     }
       
 66914   }
       
 66915   sqlite3ExprDelete(db, pSpan->pExpr);
       
 66916 }
       
 66917 
       
 66918 /*
       
 66919 ** Designate the PRIMARY KEY for the table.  pList is a list of names 
       
 66920 ** of columns that form the primary key.  If pList is NULL, then the
       
 66921 ** most recently added column of the table is the primary key.
       
 66922 **
       
 66923 ** A table can have at most one primary key.  If the table already has
       
 66924 ** a primary key (and this is the second primary key) then create an
       
 66925 ** error.
       
 66926 **
       
 66927 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
       
 66928 ** then we will try to use that column as the rowid.  Set the Table.iPKey
       
 66929 ** field of the table under construction to be the index of the
       
 66930 ** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
       
 66931 ** no INTEGER PRIMARY KEY.
       
 66932 **
       
 66933 ** If the key is not an INTEGER PRIMARY KEY, then create a unique
       
 66934 ** index for the key.  No index is created for INTEGER PRIMARY KEYs.
       
 66935 */
       
 66936 SQLITE_PRIVATE void sqlite3AddPrimaryKey(
       
 66937   Parse *pParse,    /* Parsing context */
       
 66938   ExprList *pList,  /* List of field names to be indexed */
       
 66939   int onError,      /* What to do with a uniqueness conflict */
       
 66940   int autoInc,      /* True if the AUTOINCREMENT keyword is present */
       
 66941   int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
       
 66942 ){
       
 66943   Table *pTab = pParse->pNewTable;
       
 66944   char *zType = 0;
       
 66945   int iCol = -1, i;
       
 66946   if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
       
 66947   if( pTab->tabFlags & TF_HasPrimaryKey ){
       
 66948     sqlite3ErrorMsg(pParse, 
       
 66949       "table \"%s\" has more than one primary key", pTab->zName);
       
 66950     goto primary_key_exit;
       
 66951   }
       
 66952   pTab->tabFlags |= TF_HasPrimaryKey;
       
 66953   if( pList==0 ){
       
 66954     iCol = pTab->nCol - 1;
       
 66955     pTab->aCol[iCol].isPrimKey = 1;
       
 66956   }else{
       
 66957     for(i=0; i<pList->nExpr; i++){
       
 66958       for(iCol=0; iCol<pTab->nCol; iCol++){
       
 66959         if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
       
 66960           break;
       
 66961         }
       
 66962       }
       
 66963       if( iCol<pTab->nCol ){
       
 66964         pTab->aCol[iCol].isPrimKey = 1;
       
 66965       }
       
 66966     }
       
 66967     if( pList->nExpr>1 ) iCol = -1;
       
 66968   }
       
 66969   if( iCol>=0 && iCol<pTab->nCol ){
       
 66970     zType = pTab->aCol[iCol].zType;
       
 66971   }
       
 66972   if( zType && sqlite3StrICmp(zType, "INTEGER")==0
       
 66973         && sortOrder==SQLITE_SO_ASC ){
       
 66974     pTab->iPKey = iCol;
       
 66975     pTab->keyConf = (u8)onError;
       
 66976     assert( autoInc==0 || autoInc==1 );
       
 66977     pTab->tabFlags |= autoInc*TF_Autoincrement;
       
 66978   }else if( autoInc ){
       
 66979 #ifndef SQLITE_OMIT_AUTOINCREMENT
       
 66980     sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
       
 66981        "INTEGER PRIMARY KEY");
       
 66982 #endif
       
 66983   }else{
       
 66984     Index *p;
       
 66985     p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
       
 66986     if( p ){
       
 66987       p->autoIndex = 2;
       
 66988     }
       
 66989     pList = 0;
       
 66990   }
       
 66991 
       
 66992 primary_key_exit:
       
 66993   sqlite3ExprListDelete(pParse->db, pList);
       
 66994   return;
       
 66995 }
       
 66996 
       
 66997 /*
       
 66998 ** Add a new CHECK constraint to the table currently under construction.
       
 66999 */
       
 67000 SQLITE_PRIVATE void sqlite3AddCheckConstraint(
       
 67001   Parse *pParse,    /* Parsing context */
       
 67002   Expr *pCheckExpr  /* The check expression */
       
 67003 ){
       
 67004   sqlite3 *db = pParse->db;
       
 67005 #ifndef SQLITE_OMIT_CHECK
       
 67006   Table *pTab = pParse->pNewTable;
       
 67007   if( pTab && !IN_DECLARE_VTAB ){
       
 67008     pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck, pCheckExpr);
       
 67009   }else
       
 67010 #endif
       
 67011   {
       
 67012     sqlite3ExprDelete(db, pCheckExpr);
       
 67013   }
       
 67014 }
       
 67015 
       
 67016 /*
       
 67017 ** Set the collation function of the most recently parsed table column
       
 67018 ** to the CollSeq given.
       
 67019 */
       
 67020 SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
       
 67021   Table *p;
       
 67022   int i;
       
 67023   char *zColl;              /* Dequoted name of collation sequence */
       
 67024   sqlite3 *db;
       
 67025 
       
 67026   if( (p = pParse->pNewTable)==0 ) return;
       
 67027   i = p->nCol-1;
       
 67028   db = pParse->db;
       
 67029   zColl = sqlite3NameFromToken(db, pToken);
       
 67030   if( !zColl ) return;
       
 67031 
       
 67032   if( sqlite3LocateCollSeq(pParse, zColl) ){
       
 67033     Index *pIdx;
       
 67034     p->aCol[i].zColl = zColl;
       
 67035   
       
 67036     /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
       
 67037     ** then an index may have been created on this column before the
       
 67038     ** collation type was added. Correct this if it is the case.
       
 67039     */
       
 67040     for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
       
 67041       assert( pIdx->nColumn==1 );
       
 67042       if( pIdx->aiColumn[0]==i ){
       
 67043         pIdx->azColl[0] = p->aCol[i].zColl;
       
 67044       }
       
 67045     }
       
 67046   }else{
       
 67047     sqlite3DbFree(db, zColl);
       
 67048   }
       
 67049 }
       
 67050 
       
 67051 /*
       
 67052 ** This function returns the collation sequence for database native text
       
 67053 ** encoding identified by the string zName, length nName.
       
 67054 **
       
 67055 ** If the requested collation sequence is not available, or not available
       
 67056 ** in the database native encoding, the collation factory is invoked to
       
 67057 ** request it. If the collation factory does not supply such a sequence,
       
 67058 ** and the sequence is available in another text encoding, then that is
       
 67059 ** returned instead.
       
 67060 **
       
 67061 ** If no versions of the requested collations sequence are available, or
       
 67062 ** another error occurs, NULL is returned and an error message written into
       
 67063 ** pParse.
       
 67064 **
       
 67065 ** This routine is a wrapper around sqlite3FindCollSeq().  This routine
       
 67066 ** invokes the collation factory if the named collation cannot be found
       
 67067 ** and generates an error message.
       
 67068 **
       
 67069 ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
       
 67070 */
       
 67071 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
       
 67072   sqlite3 *db = pParse->db;
       
 67073   u8 enc = ENC(db);
       
 67074   u8 initbusy = db->init.busy;
       
 67075   CollSeq *pColl;
       
 67076 
       
 67077   pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
       
 67078   if( !initbusy && (!pColl || !pColl->xCmp) ){
       
 67079     pColl = sqlite3GetCollSeq(db, enc, pColl, zName);
       
 67080     if( !pColl ){
       
 67081       sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
       
 67082     }
       
 67083   }
       
 67084 
       
 67085   return pColl;
       
 67086 }
       
 67087 
       
 67088 
       
 67089 /*
       
 67090 ** Generate code that will increment the schema cookie.
       
 67091 **
       
 67092 ** The schema cookie is used to determine when the schema for the
       
 67093 ** database changes.  After each schema change, the cookie value
       
 67094 ** changes.  When a process first reads the schema it records the
       
 67095 ** cookie.  Thereafter, whenever it goes to access the database,
       
 67096 ** it checks the cookie to make sure the schema has not changed
       
 67097 ** since it was last read.
       
 67098 **
       
 67099 ** This plan is not completely bullet-proof.  It is possible for
       
 67100 ** the schema to change multiple times and for the cookie to be
       
 67101 ** set back to prior value.  But schema changes are infrequent
       
 67102 ** and the probability of hitting the same cookie value is only
       
 67103 ** 1 chance in 2^32.  So we're safe enough.
       
 67104 */
       
 67105 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
       
 67106   int r1 = sqlite3GetTempReg(pParse);
       
 67107   sqlite3 *db = pParse->db;
       
 67108   Vdbe *v = pParse->pVdbe;
       
 67109   sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
       
 67110   sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
       
 67111   sqlite3ReleaseTempReg(pParse, r1);
       
 67112 }
       
 67113 
       
 67114 /*
       
 67115 ** Measure the number of characters needed to output the given
       
 67116 ** identifier.  The number returned includes any quotes used
       
 67117 ** but does not include the null terminator.
       
 67118 **
       
 67119 ** The estimate is conservative.  It might be larger that what is
       
 67120 ** really needed.
       
 67121 */
       
 67122 static int identLength(const char *z){
       
 67123   int n;
       
 67124   for(n=0; *z; n++, z++){
       
 67125     if( *z=='"' ){ n++; }
       
 67126   }
       
 67127   return n + 2;
       
 67128 }
       
 67129 
       
 67130 /*
       
 67131 ** The first parameter is a pointer to an output buffer. The second 
       
 67132 ** parameter is a pointer to an integer that contains the offset at
       
 67133 ** which to write into the output buffer. This function copies the
       
 67134 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
       
 67135 ** to the specified offset in the buffer and updates *pIdx to refer
       
 67136 ** to the first byte after the last byte written before returning.
       
 67137 ** 
       
 67138 ** If the string zSignedIdent consists entirely of alpha-numeric
       
 67139 ** characters, does not begin with a digit and is not an SQL keyword,
       
 67140 ** then it is copied to the output buffer exactly as it is. Otherwise,
       
 67141 ** it is quoted using double-quotes.
       
 67142 */
       
 67143 static void identPut(char *z, int *pIdx, char *zSignedIdent){
       
 67144   unsigned char *zIdent = (unsigned char*)zSignedIdent;
       
 67145   int i, j, needQuote;
       
 67146   i = *pIdx;
       
 67147 
       
 67148   for(j=0; zIdent[j]; j++){
       
 67149     if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
       
 67150   }
       
 67151   needQuote = sqlite3Isdigit(zIdent[0]) || sqlite3KeywordCode(zIdent, j)!=TK_ID;
       
 67152   if( !needQuote ){
       
 67153     needQuote = zIdent[j];
       
 67154   }
       
 67155 
       
 67156   if( needQuote ) z[i++] = '"';
       
 67157   for(j=0; zIdent[j]; j++){
       
 67158     z[i++] = zIdent[j];
       
 67159     if( zIdent[j]=='"' ) z[i++] = '"';
       
 67160   }
       
 67161   if( needQuote ) z[i++] = '"';
       
 67162   z[i] = 0;
       
 67163   *pIdx = i;
       
 67164 }
       
 67165 
       
 67166 /*
       
 67167 ** Generate a CREATE TABLE statement appropriate for the given
       
 67168 ** table.  Memory to hold the text of the statement is obtained
       
 67169 ** from sqliteMalloc() and must be freed by the calling function.
       
 67170 */
       
 67171 static char *createTableStmt(sqlite3 *db, Table *p){
       
 67172   int i, k, n;
       
 67173   char *zStmt;
       
 67174   char *zSep, *zSep2, *zEnd;
       
 67175   Column *pCol;
       
 67176   n = 0;
       
 67177   for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
       
 67178     n += identLength(pCol->zName) + 5;
       
 67179   }
       
 67180   n += identLength(p->zName);
       
 67181   if( n<50 ){ 
       
 67182     zSep = "";
       
 67183     zSep2 = ",";
       
 67184     zEnd = ")";
       
 67185   }else{
       
 67186     zSep = "\n  ";
       
 67187     zSep2 = ",\n  ";
       
 67188     zEnd = "\n)";
       
 67189   }
       
 67190   n += 35 + 6*p->nCol;
       
 67191   zStmt = sqlite3Malloc( n );
       
 67192   if( zStmt==0 ){
       
 67193     db->mallocFailed = 1;
       
 67194     return 0;
       
 67195   }
       
 67196   sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
       
 67197   k = sqlite3Strlen30(zStmt);
       
 67198   identPut(zStmt, &k, p->zName);
       
 67199   zStmt[k++] = '(';
       
 67200   for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
       
 67201     static const char * const azType[] = {
       
 67202         /* SQLITE_AFF_TEXT    */ " TEXT",
       
 67203         /* SQLITE_AFF_NONE    */ "",
       
 67204         /* SQLITE_AFF_NUMERIC */ " NUM",
       
 67205         /* SQLITE_AFF_INTEGER */ " INT",
       
 67206         /* SQLITE_AFF_REAL    */ " REAL"
       
 67207     };
       
 67208     int len;
       
 67209     const char *zType;
       
 67210 
       
 67211     sqlite3_snprintf(n-k, &zStmt[k], zSep);
       
 67212     k += sqlite3Strlen30(&zStmt[k]);
       
 67213     zSep = zSep2;
       
 67214     identPut(zStmt, &k, pCol->zName);
       
 67215     assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
       
 67216     assert( pCol->affinity-SQLITE_AFF_TEXT < sizeof(azType)/sizeof(azType[0]) );
       
 67217     testcase( pCol->affinity==SQLITE_AFF_TEXT );
       
 67218     testcase( pCol->affinity==SQLITE_AFF_NONE );
       
 67219     testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
       
 67220     testcase( pCol->affinity==SQLITE_AFF_INTEGER );
       
 67221     testcase( pCol->affinity==SQLITE_AFF_REAL );
       
 67222     
       
 67223     zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
       
 67224     len = sqlite3Strlen30(zType);
       
 67225     assert( pCol->affinity==SQLITE_AFF_NONE 
       
 67226             || pCol->affinity==sqlite3AffinityType(zType) );
       
 67227     memcpy(&zStmt[k], zType, len);
       
 67228     k += len;
       
 67229     assert( k<=n );
       
 67230   }
       
 67231   sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
       
 67232   return zStmt;
       
 67233 }
       
 67234 
       
 67235 /*
       
 67236 ** This routine is called to report the final ")" that terminates
       
 67237 ** a CREATE TABLE statement.
       
 67238 **
       
 67239 ** The table structure that other action routines have been building
       
 67240 ** is added to the internal hash tables, assuming no errors have
       
 67241 ** occurred.
       
 67242 **
       
 67243 ** An entry for the table is made in the master table on disk, unless
       
 67244 ** this is a temporary table or db->init.busy==1.  When db->init.busy==1
       
 67245 ** it means we are reading the sqlite_master table because we just
       
 67246 ** connected to the database or because the sqlite_master table has
       
 67247 ** recently changed, so the entry for this table already exists in
       
 67248 ** the sqlite_master table.  We do not want to create it again.
       
 67249 **
       
 67250 ** If the pSelect argument is not NULL, it means that this routine
       
 67251 ** was called to create a table generated from a 
       
 67252 ** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
       
 67253 ** the new table will match the result set of the SELECT.
       
 67254 */
       
 67255 SQLITE_PRIVATE void sqlite3EndTable(
       
 67256   Parse *pParse,          /* Parse context */
       
 67257   Token *pCons,           /* The ',' token after the last column defn. */
       
 67258   Token *pEnd,            /* The final ')' token in the CREATE TABLE */
       
 67259   Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
       
 67260 ){
       
 67261   Table *p;
       
 67262   sqlite3 *db = pParse->db;
       
 67263   int iDb;
       
 67264 
       
 67265   if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
       
 67266     return;
       
 67267   }
       
 67268   p = pParse->pNewTable;
       
 67269   if( p==0 ) return;
       
 67270 
       
 67271   assert( !db->init.busy || !pSelect );
       
 67272 
       
 67273   iDb = sqlite3SchemaToIndex(db, p->pSchema);
       
 67274 
       
 67275 #ifndef SQLITE_OMIT_CHECK
       
 67276   /* Resolve names in all CHECK constraint expressions.
       
 67277   */
       
 67278   if( p->pCheck ){
       
 67279     SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
       
 67280     NameContext sNC;                /* Name context for pParse->pNewTable */
       
 67281 
       
 67282     memset(&sNC, 0, sizeof(sNC));
       
 67283     memset(&sSrc, 0, sizeof(sSrc));
       
 67284     sSrc.nSrc = 1;
       
 67285     sSrc.a[0].zName = p->zName;
       
 67286     sSrc.a[0].pTab = p;
       
 67287     sSrc.a[0].iCursor = -1;
       
 67288     sNC.pParse = pParse;
       
 67289     sNC.pSrcList = &sSrc;
       
 67290     sNC.isCheck = 1;
       
 67291     if( sqlite3ResolveExprNames(&sNC, p->pCheck) ){
       
 67292       return;
       
 67293     }
       
 67294   }
       
 67295 #endif /* !defined(SQLITE_OMIT_CHECK) */
       
 67296 
       
 67297   /* If the db->init.busy is 1 it means we are reading the SQL off the
       
 67298   ** "sqlite_master" or "sqlite_temp_master" table on the disk.
       
 67299   ** So do not write to the disk again.  Extract the root page number
       
 67300   ** for the table from the db->init.newTnum field.  (The page number
       
 67301   ** should have been put there by the sqliteOpenCb routine.)
       
 67302   */
       
 67303   if( db->init.busy ){
       
 67304     p->tnum = db->init.newTnum;
       
 67305   }
       
 67306 
       
 67307   /* If not initializing, then create a record for the new table
       
 67308   ** in the SQLITE_MASTER table of the database.
       
 67309   **
       
 67310   ** If this is a TEMPORARY table, write the entry into the auxiliary
       
 67311   ** file instead of into the main database file.
       
 67312   */
       
 67313   if( !db->init.busy ){
       
 67314     int n;
       
 67315     Vdbe *v;
       
 67316     char *zType;    /* "view" or "table" */
       
 67317     char *zType2;   /* "VIEW" or "TABLE" */
       
 67318     char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
       
 67319 
       
 67320     v = sqlite3GetVdbe(pParse);
       
 67321     if( NEVER(v==0) ) return;
       
 67322 
       
 67323     sqlite3VdbeAddOp1(v, OP_Close, 0);
       
 67324 
       
 67325     /* 
       
 67326     ** Initialize zType for the new view or table.
       
 67327     */
       
 67328     if( p->pSelect==0 ){
       
 67329       /* A regular table */
       
 67330       zType = "table";
       
 67331       zType2 = "TABLE";
       
 67332 #ifndef SQLITE_OMIT_VIEW
       
 67333     }else{
       
 67334       /* A view */
       
 67335       zType = "view";
       
 67336       zType2 = "VIEW";
       
 67337 #endif
       
 67338     }
       
 67339 
       
 67340     /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
       
 67341     ** statement to populate the new table. The root-page number for the
       
 67342     ** new table is in register pParse->regRoot.
       
 67343     **
       
 67344     ** Once the SELECT has been coded by sqlite3Select(), it is in a
       
 67345     ** suitable state to query for the column names and types to be used
       
 67346     ** by the new table.
       
 67347     **
       
 67348     ** A shared-cache write-lock is not required to write to the new table,
       
 67349     ** as a schema-lock must have already been obtained to create it. Since
       
 67350     ** a schema-lock excludes all other database users, the write-lock would
       
 67351     ** be redundant.
       
 67352     */
       
 67353     if( pSelect ){
       
 67354       SelectDest dest;
       
 67355       Table *pSelTab;
       
 67356 
       
 67357       assert(pParse->nTab==1);
       
 67358       sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
       
 67359       sqlite3VdbeChangeP5(v, 1);
       
 67360       pParse->nTab = 2;
       
 67361       sqlite3SelectDestInit(&dest, SRT_Table, 1);
       
 67362       sqlite3Select(pParse, pSelect, &dest);
       
 67363       sqlite3VdbeAddOp1(v, OP_Close, 1);
       
 67364       if( pParse->nErr==0 ){
       
 67365         pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
       
 67366         if( pSelTab==0 ) return;
       
 67367         assert( p->aCol==0 );
       
 67368         p->nCol = pSelTab->nCol;
       
 67369         p->aCol = pSelTab->aCol;
       
 67370         pSelTab->nCol = 0;
       
 67371         pSelTab->aCol = 0;
       
 67372         sqlite3DeleteTable(pSelTab);
       
 67373       }
       
 67374     }
       
 67375 
       
 67376     /* Compute the complete text of the CREATE statement */
       
 67377     if( pSelect ){
       
 67378       zStmt = createTableStmt(db, p);
       
 67379     }else{
       
 67380       n = (int)(pEnd->z - pParse->sNameToken.z) + 1;
       
 67381       zStmt = sqlite3MPrintf(db, 
       
 67382           "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
       
 67383       );
       
 67384     }
       
 67385 
       
 67386     /* A slot for the record has already been allocated in the 
       
 67387     ** SQLITE_MASTER table.  We just need to update that slot with all
       
 67388     ** the information we've collected.
       
 67389     */
       
 67390     sqlite3NestedParse(pParse,
       
 67391       "UPDATE %Q.%s "
       
 67392          "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
       
 67393        "WHERE rowid=#%d",
       
 67394       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
       
 67395       zType,
       
 67396       p->zName,
       
 67397       p->zName,
       
 67398       pParse->regRoot,
       
 67399       zStmt,
       
 67400       pParse->regRowid
       
 67401     );
       
 67402     sqlite3DbFree(db, zStmt);
       
 67403     sqlite3ChangeCookie(pParse, iDb);
       
 67404 
       
 67405 #ifndef SQLITE_OMIT_AUTOINCREMENT
       
 67406     /* Check to see if we need to create an sqlite_sequence table for
       
 67407     ** keeping track of autoincrement keys.
       
 67408     */
       
 67409     if( p->tabFlags & TF_Autoincrement ){
       
 67410       Db *pDb = &db->aDb[iDb];
       
 67411       if( pDb->pSchema->pSeqTab==0 ){
       
 67412         sqlite3NestedParse(pParse,
       
 67413           "CREATE TABLE %Q.sqlite_sequence(name,seq)",
       
 67414           pDb->zName
       
 67415         );
       
 67416       }
       
 67417     }
       
 67418 #endif
       
 67419 
       
 67420     /* Reparse everything to update our internal data structures */
       
 67421     sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0,
       
 67422         sqlite3MPrintf(db, "tbl_name='%q'",p->zName), P4_DYNAMIC);
       
 67423   }
       
 67424 
       
 67425 
       
 67426   /* Add the table to the in-memory representation of the database.
       
 67427   */
       
 67428   if( db->init.busy ){
       
 67429     Table *pOld;
       
 67430     Schema *pSchema = p->pSchema;
       
 67431     pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
       
 67432                              sqlite3Strlen30(p->zName),p);
       
 67433     if( pOld ){
       
 67434       assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
       
 67435       db->mallocFailed = 1;
       
 67436       return;
       
 67437     }
       
 67438     pParse->pNewTable = 0;
       
 67439     db->nTable++;
       
 67440     db->flags |= SQLITE_InternChanges;
       
 67441 
       
 67442 #ifndef SQLITE_OMIT_ALTERTABLE
       
 67443     if( !p->pSelect ){
       
 67444       const char *zName = (const char *)pParse->sNameToken.z;
       
 67445       int nName;
       
 67446       assert( !pSelect && pCons && pEnd );
       
 67447       if( pCons->z==0 ){
       
 67448         pCons = pEnd;
       
 67449       }
       
 67450       nName = (int)((const char *)pCons->z - zName);
       
 67451       p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
       
 67452     }
       
 67453 #endif
       
 67454   }
       
 67455 }
       
 67456 
       
 67457 #ifndef SQLITE_OMIT_VIEW
       
 67458 /*
       
 67459 ** The parser calls this routine in order to create a new VIEW
       
 67460 */
       
 67461 SQLITE_PRIVATE void sqlite3CreateView(
       
 67462   Parse *pParse,     /* The parsing context */
       
 67463   Token *pBegin,     /* The CREATE token that begins the statement */
       
 67464   Token *pName1,     /* The token that holds the name of the view */
       
 67465   Token *pName2,     /* The token that holds the name of the view */
       
 67466   Select *pSelect,   /* A SELECT statement that will become the new view */
       
 67467   int isTemp,        /* TRUE for a TEMPORARY view */
       
 67468   int noErr          /* Suppress error messages if VIEW already exists */
       
 67469 ){
       
 67470   Table *p;
       
 67471   int n;
       
 67472   const char *z;
       
 67473   Token sEnd;
       
 67474   DbFixer sFix;
       
 67475   Token *pName;
       
 67476   int iDb;
       
 67477   sqlite3 *db = pParse->db;
       
 67478 
       
 67479   if( pParse->nVar>0 ){
       
 67480     sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
       
 67481     sqlite3SelectDelete(db, pSelect);
       
 67482     return;
       
 67483   }
       
 67484   sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
       
 67485   p = pParse->pNewTable;
       
 67486   if( p==0 ){
       
 67487     sqlite3SelectDelete(db, pSelect);
       
 67488     return;
       
 67489   }
       
 67490   assert( pParse->nErr==0 ); /* If sqlite3StartTable return non-NULL then
       
 67491                              ** there could not have been an error */
       
 67492   sqlite3TwoPartName(pParse, pName1, pName2, &pName);
       
 67493   iDb = sqlite3SchemaToIndex(db, p->pSchema);
       
 67494   if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName)
       
 67495     && sqlite3FixSelect(&sFix, pSelect)
       
 67496   ){
       
 67497     sqlite3SelectDelete(db, pSelect);
       
 67498     return;
       
 67499   }
       
 67500 
       
 67501   /* Make a copy of the entire SELECT statement that defines the view.
       
 67502   ** This will force all the Expr.token.z values to be dynamically
       
 67503   ** allocated rather than point to the input string - which means that
       
 67504   ** they will persist after the current sqlite3_exec() call returns.
       
 67505   */
       
 67506   p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
       
 67507   sqlite3SelectDelete(db, pSelect);
       
 67508   if( db->mallocFailed ){
       
 67509     return;
       
 67510   }
       
 67511   if( !db->init.busy ){
       
 67512     sqlite3ViewGetColumnNames(pParse, p);
       
 67513   }
       
 67514 
       
 67515   /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
       
 67516   ** the end.
       
 67517   */
       
 67518   sEnd = pParse->sLastToken;
       
 67519   if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
       
 67520     sEnd.z += sEnd.n;
       
 67521   }
       
 67522   sEnd.n = 0;
       
 67523   n = (int)(sEnd.z - pBegin->z);
       
 67524   z = pBegin->z;
       
 67525   while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
       
 67526   sEnd.z = &z[n-1];
       
 67527   sEnd.n = 1;
       
 67528 
       
 67529   /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
       
 67530   sqlite3EndTable(pParse, 0, &sEnd, 0);
       
 67531   return;
       
 67532 }
       
 67533 #endif /* SQLITE_OMIT_VIEW */
       
 67534 
       
 67535 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
       
 67536 /*
       
 67537 ** The Table structure pTable is really a VIEW.  Fill in the names of
       
 67538 ** the columns of the view in the pTable structure.  Return the number
       
 67539 ** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
       
 67540 */
       
 67541 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
       
 67542   Table *pSelTab;   /* A fake table from which we get the result set */
       
 67543   Select *pSel;     /* Copy of the SELECT that implements the view */
       
 67544   int nErr = 0;     /* Number of errors encountered */
       
 67545   int n;            /* Temporarily holds the number of cursors assigned */
       
 67546   sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
       
 67547   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
       
 67548 
       
 67549   assert( pTable );
       
 67550 
       
 67551 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 67552   if( sqlite3VtabCallConnect(pParse, pTable) ){
       
 67553     return SQLITE_ERROR;
       
 67554   }
       
 67555   if( IsVirtual(pTable) ) return 0;
       
 67556 #endif
       
 67557 
       
 67558 #ifndef SQLITE_OMIT_VIEW
       
 67559   /* A positive nCol means the columns names for this view are
       
 67560   ** already known.
       
 67561   */
       
 67562   if( pTable->nCol>0 ) return 0;
       
 67563 
       
 67564   /* A negative nCol is a special marker meaning that we are currently
       
 67565   ** trying to compute the column names.  If we enter this routine with
       
 67566   ** a negative nCol, it means two or more views form a loop, like this:
       
 67567   **
       
 67568   **     CREATE VIEW one AS SELECT * FROM two;
       
 67569   **     CREATE VIEW two AS SELECT * FROM one;
       
 67570   **
       
 67571   ** Actually, the error above is now caught prior to reaching this point.
       
 67572   ** But the following test is still important as it does come up
       
 67573   ** in the following:
       
 67574   ** 
       
 67575   **     CREATE TABLE main.ex1(a);
       
 67576   **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
       
 67577   **     SELECT * FROM temp.ex1;
       
 67578   */
       
 67579   if( pTable->nCol<0 ){
       
 67580     sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
       
 67581     return 1;
       
 67582   }
       
 67583   assert( pTable->nCol>=0 );
       
 67584 
       
 67585   /* If we get this far, it means we need to compute the table names.
       
 67586   ** Note that the call to sqlite3ResultSetOfSelect() will expand any
       
 67587   ** "*" elements in the results set of the view and will assign cursors
       
 67588   ** to the elements of the FROM clause.  But we do not want these changes
       
 67589   ** to be permanent.  So the computation is done on a copy of the SELECT
       
 67590   ** statement that defines the view.
       
 67591   */
       
 67592   assert( pTable->pSelect );
       
 67593   pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
       
 67594   if( pSel ){
       
 67595     u8 enableLookaside = db->lookaside.bEnabled;
       
 67596     n = pParse->nTab;
       
 67597     sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
       
 67598     pTable->nCol = -1;
       
 67599     db->lookaside.bEnabled = 0;
       
 67600 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 67601     xAuth = db->xAuth;
       
 67602     db->xAuth = 0;
       
 67603     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
       
 67604     db->xAuth = xAuth;
       
 67605 #else
       
 67606     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
       
 67607 #endif
       
 67608     db->lookaside.bEnabled = enableLookaside;
       
 67609     pParse->nTab = n;
       
 67610     if( pSelTab ){
       
 67611       assert( pTable->aCol==0 );
       
 67612       pTable->nCol = pSelTab->nCol;
       
 67613       pTable->aCol = pSelTab->aCol;
       
 67614       pSelTab->nCol = 0;
       
 67615       pSelTab->aCol = 0;
       
 67616       sqlite3DeleteTable(pSelTab);
       
 67617       pTable->pSchema->flags |= DB_UnresetViews;
       
 67618     }else{
       
 67619       pTable->nCol = 0;
       
 67620       nErr++;
       
 67621     }
       
 67622     sqlite3SelectDelete(db, pSel);
       
 67623   } else {
       
 67624     nErr++;
       
 67625   }
       
 67626 #endif /* SQLITE_OMIT_VIEW */
       
 67627   return nErr;  
       
 67628 }
       
 67629 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
       
 67630 
       
 67631 #ifndef SQLITE_OMIT_VIEW
       
 67632 /*
       
 67633 ** Clear the column names from every VIEW in database idx.
       
 67634 */
       
 67635 static void sqliteViewResetAll(sqlite3 *db, int idx){
       
 67636   HashElem *i;
       
 67637   if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
       
 67638   for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
       
 67639     Table *pTab = sqliteHashData(i);
       
 67640     if( pTab->pSelect ){
       
 67641       sqliteResetColumnNames(pTab);
       
 67642     }
       
 67643   }
       
 67644   DbClearProperty(db, idx, DB_UnresetViews);
       
 67645 }
       
 67646 #else
       
 67647 # define sqliteViewResetAll(A,B)
       
 67648 #endif /* SQLITE_OMIT_VIEW */
       
 67649 
       
 67650 /*
       
 67651 ** This function is called by the VDBE to adjust the internal schema
       
 67652 ** used by SQLite when the btree layer moves a table root page. The
       
 67653 ** root-page of a table or index in database iDb has changed from iFrom
       
 67654 ** to iTo.
       
 67655 **
       
 67656 ** Ticket #1728:  The symbol table might still contain information
       
 67657 ** on tables and/or indices that are the process of being deleted.
       
 67658 ** If you are unlucky, one of those deleted indices or tables might
       
 67659 ** have the same rootpage number as the real table or index that is
       
 67660 ** being moved.  So we cannot stop searching after the first match 
       
 67661 ** because the first match might be for one of the deleted indices
       
 67662 ** or tables and not the table/index that is actually being moved.
       
 67663 ** We must continue looping until all tables and indices with
       
 67664 ** rootpage==iFrom have been converted to have a rootpage of iTo
       
 67665 ** in order to be certain that we got the right one.
       
 67666 */
       
 67667 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 67668 SQLITE_PRIVATE void sqlite3RootPageMoved(Db *pDb, int iFrom, int iTo){
       
 67669   HashElem *pElem;
       
 67670   Hash *pHash;
       
 67671 
       
 67672   pHash = &pDb->pSchema->tblHash;
       
 67673   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
       
 67674     Table *pTab = sqliteHashData(pElem);
       
 67675     if( pTab->tnum==iFrom ){
       
 67676       pTab->tnum = iTo;
       
 67677     }
       
 67678   }
       
 67679   pHash = &pDb->pSchema->idxHash;
       
 67680   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
       
 67681     Index *pIdx = sqliteHashData(pElem);
       
 67682     if( pIdx->tnum==iFrom ){
       
 67683       pIdx->tnum = iTo;
       
 67684     }
       
 67685   }
       
 67686 }
       
 67687 #endif
       
 67688 
       
 67689 /*
       
 67690 ** Write code to erase the table with root-page iTable from database iDb.
       
 67691 ** Also write code to modify the sqlite_master table and internal schema
       
 67692 ** if a root-page of another table is moved by the btree-layer whilst
       
 67693 ** erasing iTable (this can happen with an auto-vacuum database).
       
 67694 */ 
       
 67695 static void destroyRootPage(Parse *pParse, int iTable, int iDb){
       
 67696   Vdbe *v = sqlite3GetVdbe(pParse);
       
 67697   int r1 = sqlite3GetTempReg(pParse);
       
 67698   sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
       
 67699   sqlite3MayAbort(pParse);
       
 67700 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 67701   /* OP_Destroy stores an in integer r1. If this integer
       
 67702   ** is non-zero, then it is the root page number of a table moved to
       
 67703   ** location iTable. The following code modifies the sqlite_master table to
       
 67704   ** reflect this.
       
 67705   **
       
 67706   ** The "#NNN" in the SQL is a special constant that means whatever value
       
 67707   ** is in register NNN.  See grammar rules associated with the TK_REGISTER
       
 67708   ** token for additional information.
       
 67709   */
       
 67710   sqlite3NestedParse(pParse, 
       
 67711      "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
       
 67712      pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
       
 67713 #endif
       
 67714   sqlite3ReleaseTempReg(pParse, r1);
       
 67715 }
       
 67716 
       
 67717 /*
       
 67718 ** Write VDBE code to erase table pTab and all associated indices on disk.
       
 67719 ** Code to update the sqlite_master tables and internal schema definitions
       
 67720 ** in case a root-page belonging to another table is moved by the btree layer
       
 67721 ** is also added (this can happen with an auto-vacuum database).
       
 67722 */
       
 67723 static void destroyTable(Parse *pParse, Table *pTab){
       
 67724 #ifdef SQLITE_OMIT_AUTOVACUUM
       
 67725   Index *pIdx;
       
 67726   int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
       
 67727   destroyRootPage(pParse, pTab->tnum, iDb);
       
 67728   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       
 67729     destroyRootPage(pParse, pIdx->tnum, iDb);
       
 67730   }
       
 67731 #else
       
 67732   /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
       
 67733   ** is not defined), then it is important to call OP_Destroy on the
       
 67734   ** table and index root-pages in order, starting with the numerically 
       
 67735   ** largest root-page number. This guarantees that none of the root-pages
       
 67736   ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
       
 67737   ** following were coded:
       
 67738   **
       
 67739   ** OP_Destroy 4 0
       
 67740   ** ...
       
 67741   ** OP_Destroy 5 0
       
 67742   **
       
 67743   ** and root page 5 happened to be the largest root-page number in the
       
 67744   ** database, then root page 5 would be moved to page 4 by the 
       
 67745   ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
       
 67746   ** a free-list page.
       
 67747   */
       
 67748   int iTab = pTab->tnum;
       
 67749   int iDestroyed = 0;
       
 67750 
       
 67751   while( 1 ){
       
 67752     Index *pIdx;
       
 67753     int iLargest = 0;
       
 67754 
       
 67755     if( iDestroyed==0 || iTab<iDestroyed ){
       
 67756       iLargest = iTab;
       
 67757     }
       
 67758     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       
 67759       int iIdx = pIdx->tnum;
       
 67760       assert( pIdx->pSchema==pTab->pSchema );
       
 67761       if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
       
 67762         iLargest = iIdx;
       
 67763       }
       
 67764     }
       
 67765     if( iLargest==0 ){
       
 67766       return;
       
 67767     }else{
       
 67768       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
       
 67769       destroyRootPage(pParse, iLargest, iDb);
       
 67770       iDestroyed = iLargest;
       
 67771     }
       
 67772   }
       
 67773 #endif
       
 67774 }
       
 67775 
       
 67776 /*
       
 67777 ** This routine is called to do the work of a DROP TABLE statement.
       
 67778 ** pName is the name of the table to be dropped.
       
 67779 */
       
 67780 SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
       
 67781   Table *pTab;
       
 67782   Vdbe *v;
       
 67783   sqlite3 *db = pParse->db;
       
 67784   int iDb;
       
 67785 
       
 67786   if( db->mallocFailed ){
       
 67787     goto exit_drop_table;
       
 67788   }
       
 67789   assert( pParse->nErr==0 );
       
 67790   assert( pName->nSrc==1 );
       
 67791   pTab = sqlite3LocateTable(pParse, isView, 
       
 67792                             pName->a[0].zName, pName->a[0].zDatabase);
       
 67793 
       
 67794   if( pTab==0 ){
       
 67795     if( noErr ){
       
 67796       sqlite3ErrorClear(pParse);
       
 67797     }
       
 67798     goto exit_drop_table;
       
 67799   }
       
 67800   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       
 67801   assert( iDb>=0 && iDb<db->nDb );
       
 67802 
       
 67803   /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
       
 67804   ** it is initialized.
       
 67805   */
       
 67806   if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
       
 67807     goto exit_drop_table;
       
 67808   }
       
 67809 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 67810   {
       
 67811     int code;
       
 67812     const char *zTab = SCHEMA_TABLE(iDb);
       
 67813     const char *zDb = db->aDb[iDb].zName;
       
 67814     const char *zArg2 = 0;
       
 67815     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
       
 67816       goto exit_drop_table;
       
 67817     }
       
 67818     if( isView ){
       
 67819       if( !OMIT_TEMPDB && iDb==1 ){
       
 67820         code = SQLITE_DROP_TEMP_VIEW;
       
 67821       }else{
       
 67822         code = SQLITE_DROP_VIEW;
       
 67823       }
       
 67824 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 67825     }else if( IsVirtual(pTab) ){
       
 67826       code = SQLITE_DROP_VTABLE;
       
 67827       zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
       
 67828 #endif
       
 67829     }else{
       
 67830       if( !OMIT_TEMPDB && iDb==1 ){
       
 67831         code = SQLITE_DROP_TEMP_TABLE;
       
 67832       }else{
       
 67833         code = SQLITE_DROP_TABLE;
       
 67834       }
       
 67835     }
       
 67836     if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
       
 67837       goto exit_drop_table;
       
 67838     }
       
 67839     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
       
 67840       goto exit_drop_table;
       
 67841     }
       
 67842   }
       
 67843 #endif
       
 67844   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
       
 67845     sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
       
 67846     goto exit_drop_table;
       
 67847   }
       
 67848 
       
 67849 #ifndef SQLITE_OMIT_VIEW
       
 67850   /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
       
 67851   ** on a table.
       
 67852   */
       
 67853   if( isView && pTab->pSelect==0 ){
       
 67854     sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
       
 67855     goto exit_drop_table;
       
 67856   }
       
 67857   if( !isView && pTab->pSelect ){
       
 67858     sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
       
 67859     goto exit_drop_table;
       
 67860   }
       
 67861 #endif
       
 67862 
       
 67863   /* Generate code to remove the table from the master table
       
 67864   ** on disk.
       
 67865   */
       
 67866   v = sqlite3GetVdbe(pParse);
       
 67867   if( v ){
       
 67868     Trigger *pTrigger;
       
 67869     Db *pDb = &db->aDb[iDb];
       
 67870     sqlite3BeginWriteOperation(pParse, 1, iDb);
       
 67871 
       
 67872 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 67873     if( IsVirtual(pTab) ){
       
 67874       sqlite3VdbeAddOp0(v, OP_VBegin);
       
 67875     }
       
 67876 #endif
       
 67877     sqlite3FkDropTable(pParse, pName, pTab);
       
 67878 
       
 67879     /* Drop all triggers associated with the table being dropped. Code
       
 67880     ** is generated to remove entries from sqlite_master and/or
       
 67881     ** sqlite_temp_master if required.
       
 67882     */
       
 67883     pTrigger = sqlite3TriggerList(pParse, pTab);
       
 67884     while( pTrigger ){
       
 67885       assert( pTrigger->pSchema==pTab->pSchema || 
       
 67886           pTrigger->pSchema==db->aDb[1].pSchema );
       
 67887       sqlite3DropTriggerPtr(pParse, pTrigger);
       
 67888       pTrigger = pTrigger->pNext;
       
 67889     }
       
 67890 
       
 67891 #ifndef SQLITE_OMIT_AUTOINCREMENT
       
 67892     /* Remove any entries of the sqlite_sequence table associated with
       
 67893     ** the table being dropped. This is done before the table is dropped
       
 67894     ** at the btree level, in case the sqlite_sequence table needs to
       
 67895     ** move as a result of the drop (can happen in auto-vacuum mode).
       
 67896     */
       
 67897     if( pTab->tabFlags & TF_Autoincrement ){
       
 67898       sqlite3NestedParse(pParse,
       
 67899         "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
       
 67900         pDb->zName, pTab->zName
       
 67901       );
       
 67902     }
       
 67903 #endif
       
 67904 
       
 67905     /* Drop all SQLITE_MASTER table and index entries that refer to the
       
 67906     ** table. The program name loops through the master table and deletes
       
 67907     ** every row that refers to a table of the same name as the one being
       
 67908     ** dropped. Triggers are handled seperately because a trigger can be
       
 67909     ** created in the temp database that refers to a table in another
       
 67910     ** database.
       
 67911     */
       
 67912     sqlite3NestedParse(pParse, 
       
 67913         "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
       
 67914         pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
       
 67915 
       
 67916     /* Drop any statistics from the sqlite_stat1 table, if it exists */
       
 67917     if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){
       
 67918       sqlite3NestedParse(pParse,
       
 67919         "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q", pDb->zName, pTab->zName
       
 67920       );
       
 67921     }
       
 67922 
       
 67923     if( !isView && !IsVirtual(pTab) ){
       
 67924       destroyTable(pParse, pTab);
       
 67925     }
       
 67926 
       
 67927     /* Remove the table entry from SQLite's internal schema and modify
       
 67928     ** the schema cookie.
       
 67929     */
       
 67930     if( IsVirtual(pTab) ){
       
 67931       sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
       
 67932     }
       
 67933     sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
       
 67934     sqlite3ChangeCookie(pParse, iDb);
       
 67935   }
       
 67936   sqliteViewResetAll(db, iDb);
       
 67937 
       
 67938 exit_drop_table:
       
 67939   sqlite3SrcListDelete(db, pName);
       
 67940 }
       
 67941 
       
 67942 /*
       
 67943 ** This routine is called to create a new foreign key on the table
       
 67944 ** currently under construction.  pFromCol determines which columns
       
 67945 ** in the current table point to the foreign key.  If pFromCol==0 then
       
 67946 ** connect the key to the last column inserted.  pTo is the name of
       
 67947 ** the table referred to.  pToCol is a list of tables in the other
       
 67948 ** pTo table that the foreign key points to.  flags contains all
       
 67949 ** information about the conflict resolution algorithms specified
       
 67950 ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
       
 67951 **
       
 67952 ** An FKey structure is created and added to the table currently
       
 67953 ** under construction in the pParse->pNewTable field.
       
 67954 **
       
 67955 ** The foreign key is set for IMMEDIATE processing.  A subsequent call
       
 67956 ** to sqlite3DeferForeignKey() might change this to DEFERRED.
       
 67957 */
       
 67958 SQLITE_PRIVATE void sqlite3CreateForeignKey(
       
 67959   Parse *pParse,       /* Parsing context */
       
 67960   ExprList *pFromCol,  /* Columns in this table that point to other table */
       
 67961   Token *pTo,          /* Name of the other table */
       
 67962   ExprList *pToCol,    /* Columns in the other table */
       
 67963   int flags            /* Conflict resolution algorithms. */
       
 67964 ){
       
 67965   sqlite3 *db = pParse->db;
       
 67966 #ifndef SQLITE_OMIT_FOREIGN_KEY
       
 67967   FKey *pFKey = 0;
       
 67968   FKey *pNextTo;
       
 67969   Table *p = pParse->pNewTable;
       
 67970   int nByte;
       
 67971   int i;
       
 67972   int nCol;
       
 67973   char *z;
       
 67974 
       
 67975   assert( pTo!=0 );
       
 67976   if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
       
 67977   if( pFromCol==0 ){
       
 67978     int iCol = p->nCol-1;
       
 67979     if( NEVER(iCol<0) ) goto fk_end;
       
 67980     if( pToCol && pToCol->nExpr!=1 ){
       
 67981       sqlite3ErrorMsg(pParse, "foreign key on %s"
       
 67982          " should reference only one column of table %T",
       
 67983          p->aCol[iCol].zName, pTo);
       
 67984       goto fk_end;
       
 67985     }
       
 67986     nCol = 1;
       
 67987   }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
       
 67988     sqlite3ErrorMsg(pParse,
       
 67989         "number of columns in foreign key does not match the number of "
       
 67990         "columns in the referenced table");
       
 67991     goto fk_end;
       
 67992   }else{
       
 67993     nCol = pFromCol->nExpr;
       
 67994   }
       
 67995   nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
       
 67996   if( pToCol ){
       
 67997     for(i=0; i<pToCol->nExpr; i++){
       
 67998       nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
       
 67999     }
       
 68000   }
       
 68001   pFKey = sqlite3DbMallocZero(db, nByte );
       
 68002   if( pFKey==0 ){
       
 68003     goto fk_end;
       
 68004   }
       
 68005   pFKey->pFrom = p;
       
 68006   pFKey->pNextFrom = p->pFKey;
       
 68007   z = (char*)&pFKey->aCol[nCol];
       
 68008   pFKey->zTo = z;
       
 68009   memcpy(z, pTo->z, pTo->n);
       
 68010   z[pTo->n] = 0;
       
 68011   sqlite3Dequote(z);
       
 68012   z += pTo->n+1;
       
 68013   pFKey->nCol = nCol;
       
 68014   if( pFromCol==0 ){
       
 68015     pFKey->aCol[0].iFrom = p->nCol-1;
       
 68016   }else{
       
 68017     for(i=0; i<nCol; i++){
       
 68018       int j;
       
 68019       for(j=0; j<p->nCol; j++){
       
 68020         if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
       
 68021           pFKey->aCol[i].iFrom = j;
       
 68022           break;
       
 68023         }
       
 68024       }
       
 68025       if( j>=p->nCol ){
       
 68026         sqlite3ErrorMsg(pParse, 
       
 68027           "unknown column \"%s\" in foreign key definition", 
       
 68028           pFromCol->a[i].zName);
       
 68029         goto fk_end;
       
 68030       }
       
 68031     }
       
 68032   }
       
 68033   if( pToCol ){
       
 68034     for(i=0; i<nCol; i++){
       
 68035       int n = sqlite3Strlen30(pToCol->a[i].zName);
       
 68036       pFKey->aCol[i].zCol = z;
       
 68037       memcpy(z, pToCol->a[i].zName, n);
       
 68038       z[n] = 0;
       
 68039       z += n+1;
       
 68040     }
       
 68041   }
       
 68042   pFKey->isDeferred = 0;
       
 68043   pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
       
 68044   pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
       
 68045 
       
 68046   pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 
       
 68047       pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
       
 68048   );
       
 68049   if( pNextTo==pFKey ){
       
 68050     db->mallocFailed = 1;
       
 68051     goto fk_end;
       
 68052   }
       
 68053   if( pNextTo ){
       
 68054     assert( pNextTo->pPrevTo==0 );
       
 68055     pFKey->pNextTo = pNextTo;
       
 68056     pNextTo->pPrevTo = pFKey;
       
 68057   }
       
 68058 
       
 68059   /* Link the foreign key to the table as the last step.
       
 68060   */
       
 68061   p->pFKey = pFKey;
       
 68062   pFKey = 0;
       
 68063 
       
 68064 fk_end:
       
 68065   sqlite3DbFree(db, pFKey);
       
 68066 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
       
 68067   sqlite3ExprListDelete(db, pFromCol);
       
 68068   sqlite3ExprListDelete(db, pToCol);
       
 68069 }
       
 68070 
       
 68071 /*
       
 68072 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
       
 68073 ** clause is seen as part of a foreign key definition.  The isDeferred
       
 68074 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
       
 68075 ** The behavior of the most recently created foreign key is adjusted
       
 68076 ** accordingly.
       
 68077 */
       
 68078 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
       
 68079 #ifndef SQLITE_OMIT_FOREIGN_KEY
       
 68080   Table *pTab;
       
 68081   FKey *pFKey;
       
 68082   if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
       
 68083   assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
       
 68084   pFKey->isDeferred = (u8)isDeferred;
       
 68085 #endif
       
 68086 }
       
 68087 
       
 68088 /*
       
 68089 ** Generate code that will erase and refill index *pIdx.  This is
       
 68090 ** used to initialize a newly created index or to recompute the
       
 68091 ** content of an index in response to a REINDEX command.
       
 68092 **
       
 68093 ** if memRootPage is not negative, it means that the index is newly
       
 68094 ** created.  The register specified by memRootPage contains the
       
 68095 ** root page number of the index.  If memRootPage is negative, then
       
 68096 ** the index already exists and must be cleared before being refilled and
       
 68097 ** the root page number of the index is taken from pIndex->tnum.
       
 68098 */
       
 68099 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
       
 68100   Table *pTab = pIndex->pTable;  /* The table that is indexed */
       
 68101   int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
       
 68102   int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
       
 68103   int addr1;                     /* Address of top of loop */
       
 68104   int tnum;                      /* Root page of index */
       
 68105   Vdbe *v;                       /* Generate code into this virtual machine */
       
 68106   KeyInfo *pKey;                 /* KeyInfo for index */
       
 68107   int regIdxKey;                 /* Registers containing the index key */
       
 68108   int regRecord;                 /* Register holding assemblied index record */
       
 68109   sqlite3 *db = pParse->db;      /* The database connection */
       
 68110   int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
       
 68111 
       
 68112 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 68113   if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
       
 68114       db->aDb[iDb].zName ) ){
       
 68115     return;
       
 68116   }
       
 68117 #endif
       
 68118 
       
 68119   /* Require a write-lock on the table to perform this operation */
       
 68120   sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
       
 68121 
       
 68122   v = sqlite3GetVdbe(pParse);
       
 68123   if( v==0 ) return;
       
 68124   if( memRootPage>=0 ){
       
 68125     tnum = memRootPage;
       
 68126   }else{
       
 68127     tnum = pIndex->tnum;
       
 68128     sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
       
 68129   }
       
 68130   pKey = sqlite3IndexKeyinfo(pParse, pIndex);
       
 68131   sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
       
 68132                     (char *)pKey, P4_KEYINFO_HANDOFF);
       
 68133   if( memRootPage>=0 ){
       
 68134     sqlite3VdbeChangeP5(v, 1);
       
 68135   }
       
 68136   sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
       
 68137   addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
       
 68138   regRecord = sqlite3GetTempReg(pParse);
       
 68139   regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
       
 68140   if( pIndex->onError!=OE_None ){
       
 68141     const int regRowid = regIdxKey + pIndex->nColumn;
       
 68142     const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
       
 68143     void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
       
 68144 
       
 68145     /* The registers accessed by the OP_IsUnique opcode were allocated
       
 68146     ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
       
 68147     ** call above. Just before that function was freed they were released
       
 68148     ** (made available to the compiler for reuse) using 
       
 68149     ** sqlite3ReleaseTempRange(). So in some ways having the OP_IsUnique
       
 68150     ** opcode use the values stored within seems dangerous. However, since
       
 68151     ** we can be sure that no other temp registers have been allocated
       
 68152     ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
       
 68153     */
       
 68154     sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
       
 68155     sqlite3HaltConstraint(
       
 68156         pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
       
 68157   }
       
 68158   sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
       
 68159   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
       
 68160   sqlite3ReleaseTempReg(pParse, regRecord);
       
 68161   sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
       
 68162   sqlite3VdbeJumpHere(v, addr1);
       
 68163   sqlite3VdbeAddOp1(v, OP_Close, iTab);
       
 68164   sqlite3VdbeAddOp1(v, OP_Close, iIdx);
       
 68165 }
       
 68166 
       
 68167 /*
       
 68168 ** Create a new index for an SQL table.  pName1.pName2 is the name of the index 
       
 68169 ** and pTblList is the name of the table that is to be indexed.  Both will 
       
 68170 ** be NULL for a primary key or an index that is created to satisfy a
       
 68171 ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
       
 68172 ** as the table to be indexed.  pParse->pNewTable is a table that is
       
 68173 ** currently being constructed by a CREATE TABLE statement.
       
 68174 **
       
 68175 ** pList is a list of columns to be indexed.  pList will be NULL if this
       
 68176 ** is a primary key or unique-constraint on the most recent column added
       
 68177 ** to the table currently under construction.  
       
 68178 **
       
 68179 ** If the index is created successfully, return a pointer to the new Index
       
 68180 ** structure. This is used by sqlite3AddPrimaryKey() to mark the index
       
 68181 ** as the tables primary key (Index.autoIndex==2).
       
 68182 */
       
 68183 SQLITE_PRIVATE Index *sqlite3CreateIndex(
       
 68184   Parse *pParse,     /* All information about this parse */
       
 68185   Token *pName1,     /* First part of index name. May be NULL */
       
 68186   Token *pName2,     /* Second part of index name. May be NULL */
       
 68187   SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
       
 68188   ExprList *pList,   /* A list of columns to be indexed */
       
 68189   int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
       
 68190   Token *pStart,     /* The CREATE token that begins this statement */
       
 68191   Token *pEnd,       /* The ")" that closes the CREATE INDEX statement */
       
 68192   int sortOrder,     /* Sort order of primary key when pList==NULL */
       
 68193   int ifNotExist     /* Omit error if index already exists */
       
 68194 ){
       
 68195   Index *pRet = 0;     /* Pointer to return */
       
 68196   Table *pTab = 0;     /* Table to be indexed */
       
 68197   Index *pIndex = 0;   /* The index to be created */
       
 68198   char *zName = 0;     /* Name of the index */
       
 68199   int nName;           /* Number of characters in zName */
       
 68200   int i, j;
       
 68201   Token nullId;        /* Fake token for an empty ID list */
       
 68202   DbFixer sFix;        /* For assigning database names to pTable */
       
 68203   int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
       
 68204   sqlite3 *db = pParse->db;
       
 68205   Db *pDb;             /* The specific table containing the indexed database */
       
 68206   int iDb;             /* Index of the database that is being written */
       
 68207   Token *pName = 0;    /* Unqualified name of the index to create */
       
 68208   struct ExprList_item *pListItem; /* For looping over pList */
       
 68209   int nCol;
       
 68210   int nExtra = 0;
       
 68211   char *zExtra;
       
 68212 
       
 68213   assert( pStart==0 || pEnd!=0 ); /* pEnd must be non-NULL if pStart is */
       
 68214   assert( pParse->nErr==0 );      /* Never called with prior errors */
       
 68215   if( db->mallocFailed || IN_DECLARE_VTAB ){
       
 68216     goto exit_create_index;
       
 68217   }
       
 68218   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
       
 68219     goto exit_create_index;
       
 68220   }
       
 68221 
       
 68222   /*
       
 68223   ** Find the table that is to be indexed.  Return early if not found.
       
 68224   */
       
 68225   if( pTblName!=0 ){
       
 68226 
       
 68227     /* Use the two-part index name to determine the database 
       
 68228     ** to search for the table. 'Fix' the table name to this db
       
 68229     ** before looking up the table.
       
 68230     */
       
 68231     assert( pName1 && pName2 );
       
 68232     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
       
 68233     if( iDb<0 ) goto exit_create_index;
       
 68234 
       
 68235 #ifndef SQLITE_OMIT_TEMPDB
       
 68236     /* If the index name was unqualified, check if the the table
       
 68237     ** is a temp table. If so, set the database to 1. Do not do this
       
 68238     ** if initialising a database schema.
       
 68239     */
       
 68240     if( !db->init.busy ){
       
 68241       pTab = sqlite3SrcListLookup(pParse, pTblName);
       
 68242       if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
       
 68243         iDb = 1;
       
 68244       }
       
 68245     }
       
 68246 #endif
       
 68247 
       
 68248     if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
       
 68249         sqlite3FixSrcList(&sFix, pTblName)
       
 68250     ){
       
 68251       /* Because the parser constructs pTblName from a single identifier,
       
 68252       ** sqlite3FixSrcList can never fail. */
       
 68253       assert(0);
       
 68254     }
       
 68255     pTab = sqlite3LocateTable(pParse, 0, pTblName->a[0].zName, 
       
 68256         pTblName->a[0].zDatabase);
       
 68257     if( !pTab || db->mallocFailed ) goto exit_create_index;
       
 68258     assert( db->aDb[iDb].pSchema==pTab->pSchema );
       
 68259   }else{
       
 68260     assert( pName==0 );
       
 68261     pTab = pParse->pNewTable;
       
 68262     if( !pTab ) goto exit_create_index;
       
 68263     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       
 68264   }
       
 68265   pDb = &db->aDb[iDb];
       
 68266 
       
 68267   assert( pTab!=0 );
       
 68268   assert( pParse->nErr==0 );
       
 68269   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
       
 68270        && memcmp(&pTab->zName[7],"altertab_",9)!=0 ){
       
 68271     sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
       
 68272     goto exit_create_index;
       
 68273   }
       
 68274 #ifndef SQLITE_OMIT_VIEW
       
 68275   if( pTab->pSelect ){
       
 68276     sqlite3ErrorMsg(pParse, "views may not be indexed");
       
 68277     goto exit_create_index;
       
 68278   }
       
 68279 #endif
       
 68280 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 68281   if( IsVirtual(pTab) ){
       
 68282     sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
       
 68283     goto exit_create_index;
       
 68284   }
       
 68285 #endif
       
 68286 
       
 68287   /*
       
 68288   ** Find the name of the index.  Make sure there is not already another
       
 68289   ** index or table with the same name.  
       
 68290   **
       
 68291   ** Exception:  If we are reading the names of permanent indices from the
       
 68292   ** sqlite_master table (because some other process changed the schema) and
       
 68293   ** one of the index names collides with the name of a temporary table or
       
 68294   ** index, then we will continue to process this index.
       
 68295   **
       
 68296   ** If pName==0 it means that we are
       
 68297   ** dealing with a primary key or UNIQUE constraint.  We have to invent our
       
 68298   ** own name.
       
 68299   */
       
 68300   if( pName ){
       
 68301     zName = sqlite3NameFromToken(db, pName);
       
 68302     if( zName==0 ) goto exit_create_index;
       
 68303     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
       
 68304       goto exit_create_index;
       
 68305     }
       
 68306     if( !db->init.busy ){
       
 68307       if( sqlite3FindTable(db, zName, 0)!=0 ){
       
 68308         sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
       
 68309         goto exit_create_index;
       
 68310       }
       
 68311     }
       
 68312     if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
       
 68313       if( !ifNotExist ){
       
 68314         sqlite3ErrorMsg(pParse, "index %s already exists", zName);
       
 68315       }
       
 68316       goto exit_create_index;
       
 68317     }
       
 68318   }else{
       
 68319     int n;
       
 68320     Index *pLoop;
       
 68321     for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
       
 68322     zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
       
 68323     if( zName==0 ){
       
 68324       goto exit_create_index;
       
 68325     }
       
 68326   }
       
 68327 
       
 68328   /* Check for authorization to create an index.
       
 68329   */
       
 68330 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 68331   {
       
 68332     const char *zDb = pDb->zName;
       
 68333     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
       
 68334       goto exit_create_index;
       
 68335     }
       
 68336     i = SQLITE_CREATE_INDEX;
       
 68337     if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
       
 68338     if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
       
 68339       goto exit_create_index;
       
 68340     }
       
 68341   }
       
 68342 #endif
       
 68343 
       
 68344   /* If pList==0, it means this routine was called to make a primary
       
 68345   ** key out of the last column added to the table under construction.
       
 68346   ** So create a fake list to simulate this.
       
 68347   */
       
 68348   if( pList==0 ){
       
 68349     nullId.z = pTab->aCol[pTab->nCol-1].zName;
       
 68350     nullId.n = sqlite3Strlen30((char*)nullId.z);
       
 68351     pList = sqlite3ExprListAppend(pParse, 0, 0);
       
 68352     if( pList==0 ) goto exit_create_index;
       
 68353     sqlite3ExprListSetName(pParse, pList, &nullId, 0);
       
 68354     pList->a[0].sortOrder = (u8)sortOrder;
       
 68355   }
       
 68356 
       
 68357   /* Figure out how many bytes of space are required to store explicitly
       
 68358   ** specified collation sequence names.
       
 68359   */
       
 68360   for(i=0; i<pList->nExpr; i++){
       
 68361     Expr *pExpr = pList->a[i].pExpr;
       
 68362     if( pExpr ){
       
 68363       CollSeq *pColl = pExpr->pColl;
       
 68364       /* Either pColl!=0 or there was an OOM failure.  But if an OOM
       
 68365       ** failure we have quit before reaching this point. */
       
 68366       if( ALWAYS(pColl) ){
       
 68367         nExtra += (1 + sqlite3Strlen30(pColl->zName));
       
 68368       }
       
 68369     }
       
 68370   }
       
 68371 
       
 68372   /* 
       
 68373   ** Allocate the index structure. 
       
 68374   */
       
 68375   nName = sqlite3Strlen30(zName);
       
 68376   nCol = pList->nExpr;
       
 68377   pIndex = sqlite3DbMallocZero(db, 
       
 68378       sizeof(Index) +              /* Index structure  */
       
 68379       sizeof(int)*nCol +           /* Index.aiColumn   */
       
 68380       sizeof(int)*(nCol+1) +       /* Index.aiRowEst   */
       
 68381       sizeof(char *)*nCol +        /* Index.azColl     */
       
 68382       sizeof(u8)*nCol +            /* Index.aSortOrder */
       
 68383       nName + 1 +                  /* Index.zName      */
       
 68384       nExtra                       /* Collation sequence names */
       
 68385   );
       
 68386   if( db->mallocFailed ){
       
 68387     goto exit_create_index;
       
 68388   }
       
 68389   pIndex->azColl = (char**)(&pIndex[1]);
       
 68390   pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
       
 68391   pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
       
 68392   pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
       
 68393   pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
       
 68394   zExtra = (char *)(&pIndex->zName[nName+1]);
       
 68395   memcpy(pIndex->zName, zName, nName+1);
       
 68396   pIndex->pTable = pTab;
       
 68397   pIndex->nColumn = pList->nExpr;
       
 68398   pIndex->onError = (u8)onError;
       
 68399   pIndex->autoIndex = (u8)(pName==0);
       
 68400   pIndex->pSchema = db->aDb[iDb].pSchema;
       
 68401 
       
 68402   /* Check to see if we should honor DESC requests on index columns
       
 68403   */
       
 68404   if( pDb->pSchema->file_format>=4 ){
       
 68405     sortOrderMask = -1;   /* Honor DESC */
       
 68406   }else{
       
 68407     sortOrderMask = 0;    /* Ignore DESC */
       
 68408   }
       
 68409 
       
 68410   /* Scan the names of the columns of the table to be indexed and
       
 68411   ** load the column indices into the Index structure.  Report an error
       
 68412   ** if any column is not found.
       
 68413   **
       
 68414   ** TODO:  Add a test to make sure that the same column is not named
       
 68415   ** more than once within the same index.  Only the first instance of
       
 68416   ** the column will ever be used by the optimizer.  Note that using the
       
 68417   ** same column more than once cannot be an error because that would 
       
 68418   ** break backwards compatibility - it needs to be a warning.
       
 68419   */
       
 68420   for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
       
 68421     const char *zColName = pListItem->zName;
       
 68422     Column *pTabCol;
       
 68423     int requestedSortOrder;
       
 68424     char *zColl;                   /* Collation sequence name */
       
 68425 
       
 68426     for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
       
 68427       if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
       
 68428     }
       
 68429     if( j>=pTab->nCol ){
       
 68430       sqlite3ErrorMsg(pParse, "table %s has no column named %s",
       
 68431         pTab->zName, zColName);
       
 68432       goto exit_create_index;
       
 68433     }
       
 68434     pIndex->aiColumn[i] = j;
       
 68435     /* Justification of the ALWAYS(pListItem->pExpr->pColl):  Because of
       
 68436     ** the way the "idxlist" non-terminal is constructed by the parser,
       
 68437     ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl
       
 68438     ** must exist or else there must have been an OOM error.  But if there
       
 68439     ** was an OOM error, we would never reach this point. */
       
 68440     if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){
       
 68441       int nColl;
       
 68442       zColl = pListItem->pExpr->pColl->zName;
       
 68443       nColl = sqlite3Strlen30(zColl) + 1;
       
 68444       assert( nExtra>=nColl );
       
 68445       memcpy(zExtra, zColl, nColl);
       
 68446       zColl = zExtra;
       
 68447       zExtra += nColl;
       
 68448       nExtra -= nColl;
       
 68449     }else{
       
 68450       zColl = pTab->aCol[j].zColl;
       
 68451       if( !zColl ){
       
 68452         zColl = db->pDfltColl->zName;
       
 68453       }
       
 68454     }
       
 68455     if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
       
 68456       goto exit_create_index;
       
 68457     }
       
 68458     pIndex->azColl[i] = zColl;
       
 68459     requestedSortOrder = pListItem->sortOrder & sortOrderMask;
       
 68460     pIndex->aSortOrder[i] = (u8)requestedSortOrder;
       
 68461   }
       
 68462   sqlite3DefaultRowEst(pIndex);
       
 68463 
       
 68464   if( pTab==pParse->pNewTable ){
       
 68465     /* This routine has been called to create an automatic index as a
       
 68466     ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
       
 68467     ** a PRIMARY KEY or UNIQUE clause following the column definitions.
       
 68468     ** i.e. one of:
       
 68469     **
       
 68470     ** CREATE TABLE t(x PRIMARY KEY, y);
       
 68471     ** CREATE TABLE t(x, y, UNIQUE(x, y));
       
 68472     **
       
 68473     ** Either way, check to see if the table already has such an index. If
       
 68474     ** so, don't bother creating this one. This only applies to
       
 68475     ** automatically created indices. Users can do as they wish with
       
 68476     ** explicit indices.
       
 68477     **
       
 68478     ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
       
 68479     ** (and thus suppressing the second one) even if they have different
       
 68480     ** sort orders.
       
 68481     **
       
 68482     ** If there are different collating sequences or if the columns of
       
 68483     ** the constraint occur in different orders, then the constraints are
       
 68484     ** considered distinct and both result in separate indices.
       
 68485     */
       
 68486     Index *pIdx;
       
 68487     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       
 68488       int k;
       
 68489       assert( pIdx->onError!=OE_None );
       
 68490       assert( pIdx->autoIndex );
       
 68491       assert( pIndex->onError!=OE_None );
       
 68492 
       
 68493       if( pIdx->nColumn!=pIndex->nColumn ) continue;
       
 68494       for(k=0; k<pIdx->nColumn; k++){
       
 68495         const char *z1;
       
 68496         const char *z2;
       
 68497         if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
       
 68498         z1 = pIdx->azColl[k];
       
 68499         z2 = pIndex->azColl[k];
       
 68500         if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
       
 68501       }
       
 68502       if( k==pIdx->nColumn ){
       
 68503         if( pIdx->onError!=pIndex->onError ){
       
 68504           /* This constraint creates the same index as a previous
       
 68505           ** constraint specified somewhere in the CREATE TABLE statement.
       
 68506           ** However the ON CONFLICT clauses are different. If both this 
       
 68507           ** constraint and the previous equivalent constraint have explicit
       
 68508           ** ON CONFLICT clauses this is an error. Otherwise, use the
       
 68509           ** explicitly specified behaviour for the index.
       
 68510           */
       
 68511           if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
       
 68512             sqlite3ErrorMsg(pParse, 
       
 68513                 "conflicting ON CONFLICT clauses specified", 0);
       
 68514           }
       
 68515           if( pIdx->onError==OE_Default ){
       
 68516             pIdx->onError = pIndex->onError;
       
 68517           }
       
 68518         }
       
 68519         goto exit_create_index;
       
 68520       }
       
 68521     }
       
 68522   }
       
 68523 
       
 68524   /* Link the new Index structure to its table and to the other
       
 68525   ** in-memory database structures. 
       
 68526   */
       
 68527   if( db->init.busy ){
       
 68528     Index *p;
       
 68529     p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
       
 68530                           pIndex->zName, sqlite3Strlen30(pIndex->zName),
       
 68531                           pIndex);
       
 68532     if( p ){
       
 68533       assert( p==pIndex );  /* Malloc must have failed */
       
 68534       db->mallocFailed = 1;
       
 68535       goto exit_create_index;
       
 68536     }
       
 68537     db->flags |= SQLITE_InternChanges;
       
 68538     if( pTblName!=0 ){
       
 68539       pIndex->tnum = db->init.newTnum;
       
 68540     }
       
 68541   }
       
 68542 
       
 68543   /* If the db->init.busy is 0 then create the index on disk.  This
       
 68544   ** involves writing the index into the master table and filling in the
       
 68545   ** index with the current table contents.
       
 68546   **
       
 68547   ** The db->init.busy is 0 when the user first enters a CREATE INDEX 
       
 68548   ** command.  db->init.busy is 1 when a database is opened and 
       
 68549   ** CREATE INDEX statements are read out of the master table.  In
       
 68550   ** the latter case the index already exists on disk, which is why
       
 68551   ** we don't want to recreate it.
       
 68552   **
       
 68553   ** If pTblName==0 it means this index is generated as a primary key
       
 68554   ** or UNIQUE constraint of a CREATE TABLE statement.  Since the table
       
 68555   ** has just been created, it contains no data and the index initialization
       
 68556   ** step can be skipped.
       
 68557   */
       
 68558   else{ /* if( db->init.busy==0 ) */
       
 68559     Vdbe *v;
       
 68560     char *zStmt;
       
 68561     int iMem = ++pParse->nMem;
       
 68562 
       
 68563     v = sqlite3GetVdbe(pParse);
       
 68564     if( v==0 ) goto exit_create_index;
       
 68565 
       
 68566 
       
 68567     /* Create the rootpage for the index
       
 68568     */
       
 68569     sqlite3BeginWriteOperation(pParse, 1, iDb);
       
 68570     sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
       
 68571 
       
 68572     /* Gather the complete text of the CREATE INDEX statement into
       
 68573     ** the zStmt variable
       
 68574     */
       
 68575     if( pStart ){
       
 68576       assert( pEnd!=0 );
       
 68577       /* A named index with an explicit CREATE INDEX statement */
       
 68578       zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
       
 68579         onError==OE_None ? "" : " UNIQUE",
       
 68580         pEnd->z - pName->z + 1,
       
 68581         pName->z);
       
 68582     }else{
       
 68583       /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
       
 68584       /* zStmt = sqlite3MPrintf(""); */
       
 68585       zStmt = 0;
       
 68586     }
       
 68587 
       
 68588     /* Add an entry in sqlite_master for this index
       
 68589     */
       
 68590     sqlite3NestedParse(pParse, 
       
 68591         "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
       
 68592         db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
       
 68593         pIndex->zName,
       
 68594         pTab->zName,
       
 68595         iMem,
       
 68596         zStmt
       
 68597     );
       
 68598     sqlite3DbFree(db, zStmt);
       
 68599 
       
 68600     /* Fill the index with data and reparse the schema. Code an OP_Expire
       
 68601     ** to invalidate all pre-compiled statements.
       
 68602     */
       
 68603     if( pTblName ){
       
 68604       sqlite3RefillIndex(pParse, pIndex, iMem);
       
 68605       sqlite3ChangeCookie(pParse, iDb);
       
 68606       sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0,
       
 68607          sqlite3MPrintf(db, "name='%q'", pIndex->zName), P4_DYNAMIC);
       
 68608       sqlite3VdbeAddOp1(v, OP_Expire, 0);
       
 68609     }
       
 68610   }
       
 68611 
       
 68612   /* When adding an index to the list of indices for a table, make
       
 68613   ** sure all indices labeled OE_Replace come after all those labeled
       
 68614   ** OE_Ignore.  This is necessary for the correct constraint check
       
 68615   ** processing (in sqlite3GenerateConstraintChecks()) as part of
       
 68616   ** UPDATE and INSERT statements.  
       
 68617   */
       
 68618   if( db->init.busy || pTblName==0 ){
       
 68619     if( onError!=OE_Replace || pTab->pIndex==0
       
 68620          || pTab->pIndex->onError==OE_Replace){
       
 68621       pIndex->pNext = pTab->pIndex;
       
 68622       pTab->pIndex = pIndex;
       
 68623     }else{
       
 68624       Index *pOther = pTab->pIndex;
       
 68625       while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
       
 68626         pOther = pOther->pNext;
       
 68627       }
       
 68628       pIndex->pNext = pOther->pNext;
       
 68629       pOther->pNext = pIndex;
       
 68630     }
       
 68631     pRet = pIndex;
       
 68632     pIndex = 0;
       
 68633   }
       
 68634 
       
 68635   /* Clean up before exiting */
       
 68636 exit_create_index:
       
 68637   if( pIndex ){
       
 68638     sqlite3_free(pIndex->zColAff);
       
 68639     sqlite3DbFree(db, pIndex);
       
 68640   }
       
 68641   sqlite3ExprListDelete(db, pList);
       
 68642   sqlite3SrcListDelete(db, pTblName);
       
 68643   sqlite3DbFree(db, zName);
       
 68644   return pRet;
       
 68645 }
       
 68646 
       
 68647 /*
       
 68648 ** Fill the Index.aiRowEst[] array with default information - information
       
 68649 ** to be used when we have not run the ANALYZE command.
       
 68650 **
       
 68651 ** aiRowEst[0] is suppose to contain the number of elements in the index.
       
 68652 ** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
       
 68653 ** number of rows in the table that match any particular value of the
       
 68654 ** first column of the index.  aiRowEst[2] is an estimate of the number
       
 68655 ** of rows that match any particular combiniation of the first 2 columns
       
 68656 ** of the index.  And so forth.  It must always be the case that
       
 68657 *
       
 68658 **           aiRowEst[N]<=aiRowEst[N-1]
       
 68659 **           aiRowEst[N]>=1
       
 68660 **
       
 68661 ** Apart from that, we have little to go on besides intuition as to
       
 68662 ** how aiRowEst[] should be initialized.  The numbers generated here
       
 68663 ** are based on typical values found in actual indices.
       
 68664 */
       
 68665 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
       
 68666   unsigned *a = pIdx->aiRowEst;
       
 68667   int i;
       
 68668   assert( a!=0 );
       
 68669   a[0] = 1000000;
       
 68670   for(i=pIdx->nColumn; i>=5; i--){
       
 68671     a[i] = 5;
       
 68672   }
       
 68673   while( i>=1 ){
       
 68674     a[i] = 11 - i;
       
 68675     i--;
       
 68676   }
       
 68677   if( pIdx->onError!=OE_None ){
       
 68678     a[pIdx->nColumn] = 1;
       
 68679   }
       
 68680 }
       
 68681 
       
 68682 /*
       
 68683 ** This routine will drop an existing named index.  This routine
       
 68684 ** implements the DROP INDEX statement.
       
 68685 */
       
 68686 SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
       
 68687   Index *pIndex;
       
 68688   Vdbe *v;
       
 68689   sqlite3 *db = pParse->db;
       
 68690   int iDb;
       
 68691 
       
 68692   assert( pParse->nErr==0 );   /* Never called with prior errors */
       
 68693   if( db->mallocFailed ){
       
 68694     goto exit_drop_index;
       
 68695   }
       
 68696   assert( pName->nSrc==1 );
       
 68697   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
       
 68698     goto exit_drop_index;
       
 68699   }
       
 68700   pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
       
 68701   if( pIndex==0 ){
       
 68702     if( !ifExists ){
       
 68703       sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
       
 68704     }
       
 68705     pParse->checkSchema = 1;
       
 68706     goto exit_drop_index;
       
 68707   }
       
 68708   if( pIndex->autoIndex ){
       
 68709     sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
       
 68710       "or PRIMARY KEY constraint cannot be dropped", 0);
       
 68711     goto exit_drop_index;
       
 68712   }
       
 68713   iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
       
 68714 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 68715   {
       
 68716     int code = SQLITE_DROP_INDEX;
       
 68717     Table *pTab = pIndex->pTable;
       
 68718     const char *zDb = db->aDb[iDb].zName;
       
 68719     const char *zTab = SCHEMA_TABLE(iDb);
       
 68720     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
       
 68721       goto exit_drop_index;
       
 68722     }
       
 68723     if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
       
 68724     if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
       
 68725       goto exit_drop_index;
       
 68726     }
       
 68727   }
       
 68728 #endif
       
 68729 
       
 68730   /* Generate code to remove the index and from the master table */
       
 68731   v = sqlite3GetVdbe(pParse);
       
 68732   if( v ){
       
 68733     sqlite3BeginWriteOperation(pParse, 1, iDb);
       
 68734     sqlite3NestedParse(pParse,
       
 68735        "DELETE FROM %Q.%s WHERE name=%Q",
       
 68736        db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
       
 68737        pIndex->zName
       
 68738     );
       
 68739     if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){
       
 68740       sqlite3NestedParse(pParse,
       
 68741         "DELETE FROM %Q.sqlite_stat1 WHERE idx=%Q",
       
 68742         db->aDb[iDb].zName, pIndex->zName
       
 68743       );
       
 68744     }
       
 68745     sqlite3ChangeCookie(pParse, iDb);
       
 68746     destroyRootPage(pParse, pIndex->tnum, iDb);
       
 68747     sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
       
 68748   }
       
 68749 
       
 68750 exit_drop_index:
       
 68751   sqlite3SrcListDelete(db, pName);
       
 68752 }
       
 68753 
       
 68754 /*
       
 68755 ** pArray is a pointer to an array of objects.  Each object in the
       
 68756 ** array is szEntry bytes in size.  This routine allocates a new
       
 68757 ** object on the end of the array.
       
 68758 **
       
 68759 ** *pnEntry is the number of entries already in use.  *pnAlloc is
       
 68760 ** the previously allocated size of the array.  initSize is the
       
 68761 ** suggested initial array size allocation.
       
 68762 **
       
 68763 ** The index of the new entry is returned in *pIdx.
       
 68764 **
       
 68765 ** This routine returns a pointer to the array of objects.  This
       
 68766 ** might be the same as the pArray parameter or it might be a different
       
 68767 ** pointer if the array was resized.
       
 68768 */
       
 68769 SQLITE_PRIVATE void *sqlite3ArrayAllocate(
       
 68770   sqlite3 *db,      /* Connection to notify of malloc failures */
       
 68771   void *pArray,     /* Array of objects.  Might be reallocated */
       
 68772   int szEntry,      /* Size of each object in the array */
       
 68773   int initSize,     /* Suggested initial allocation, in elements */
       
 68774   int *pnEntry,     /* Number of objects currently in use */
       
 68775   int *pnAlloc,     /* Current size of the allocation, in elements */
       
 68776   int *pIdx         /* Write the index of a new slot here */
       
 68777 ){
       
 68778   char *z;
       
 68779   if( *pnEntry >= *pnAlloc ){
       
 68780     void *pNew;
       
 68781     int newSize;
       
 68782     newSize = (*pnAlloc)*2 + initSize;
       
 68783     pNew = sqlite3DbRealloc(db, pArray, newSize*szEntry);
       
 68784     if( pNew==0 ){
       
 68785       *pIdx = -1;
       
 68786       return pArray;
       
 68787     }
       
 68788     *pnAlloc = sqlite3DbMallocSize(db, pNew)/szEntry;
       
 68789     pArray = pNew;
       
 68790   }
       
 68791   z = (char*)pArray;
       
 68792   memset(&z[*pnEntry * szEntry], 0, szEntry);
       
 68793   *pIdx = *pnEntry;
       
 68794   ++*pnEntry;
       
 68795   return pArray;
       
 68796 }
       
 68797 
       
 68798 /*
       
 68799 ** Append a new element to the given IdList.  Create a new IdList if
       
 68800 ** need be.
       
 68801 **
       
 68802 ** A new IdList is returned, or NULL if malloc() fails.
       
 68803 */
       
 68804 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
       
 68805   int i;
       
 68806   if( pList==0 ){
       
 68807     pList = sqlite3DbMallocZero(db, sizeof(IdList) );
       
 68808     if( pList==0 ) return 0;
       
 68809     pList->nAlloc = 0;
       
 68810   }
       
 68811   pList->a = sqlite3ArrayAllocate(
       
 68812       db,
       
 68813       pList->a,
       
 68814       sizeof(pList->a[0]),
       
 68815       5,
       
 68816       &pList->nId,
       
 68817       &pList->nAlloc,
       
 68818       &i
       
 68819   );
       
 68820   if( i<0 ){
       
 68821     sqlite3IdListDelete(db, pList);
       
 68822     return 0;
       
 68823   }
       
 68824   pList->a[i].zName = sqlite3NameFromToken(db, pToken);
       
 68825   return pList;
       
 68826 }
       
 68827 
       
 68828 /*
       
 68829 ** Delete an IdList.
       
 68830 */
       
 68831 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
       
 68832   int i;
       
 68833   if( pList==0 ) return;
       
 68834   for(i=0; i<pList->nId; i++){
       
 68835     sqlite3DbFree(db, pList->a[i].zName);
       
 68836   }
       
 68837   sqlite3DbFree(db, pList->a);
       
 68838   sqlite3DbFree(db, pList);
       
 68839 }
       
 68840 
       
 68841 /*
       
 68842 ** Return the index in pList of the identifier named zId.  Return -1
       
 68843 ** if not found.
       
 68844 */
       
 68845 SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
       
 68846   int i;
       
 68847   if( pList==0 ) return -1;
       
 68848   for(i=0; i<pList->nId; i++){
       
 68849     if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
       
 68850   }
       
 68851   return -1;
       
 68852 }
       
 68853 
       
 68854 /*
       
 68855 ** Expand the space allocated for the given SrcList object by
       
 68856 ** creating nExtra new slots beginning at iStart.  iStart is zero based.
       
 68857 ** New slots are zeroed.
       
 68858 **
       
 68859 ** For example, suppose a SrcList initially contains two entries: A,B.
       
 68860 ** To append 3 new entries onto the end, do this:
       
 68861 **
       
 68862 **    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
       
 68863 **
       
 68864 ** After the call above it would contain:  A, B, nil, nil, nil.
       
 68865 ** If the iStart argument had been 1 instead of 2, then the result
       
 68866 ** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
       
 68867 ** the iStart value would be 0.  The result then would
       
 68868 ** be: nil, nil, nil, A, B.
       
 68869 **
       
 68870 ** If a memory allocation fails the SrcList is unchanged.  The
       
 68871 ** db->mallocFailed flag will be set to true.
       
 68872 */
       
 68873 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
       
 68874   sqlite3 *db,       /* Database connection to notify of OOM errors */
       
 68875   SrcList *pSrc,     /* The SrcList to be enlarged */
       
 68876   int nExtra,        /* Number of new slots to add to pSrc->a[] */
       
 68877   int iStart         /* Index in pSrc->a[] of first new slot */
       
 68878 ){
       
 68879   int i;
       
 68880 
       
 68881   /* Sanity checking on calling parameters */
       
 68882   assert( iStart>=0 );
       
 68883   assert( nExtra>=1 );
       
 68884   assert( pSrc!=0 );
       
 68885   assert( iStart<=pSrc->nSrc );
       
 68886 
       
 68887   /* Allocate additional space if needed */
       
 68888   if( pSrc->nSrc+nExtra>pSrc->nAlloc ){
       
 68889     SrcList *pNew;
       
 68890     int nAlloc = pSrc->nSrc+nExtra;
       
 68891     int nGot;
       
 68892     pNew = sqlite3DbRealloc(db, pSrc,
       
 68893                sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
       
 68894     if( pNew==0 ){
       
 68895       assert( db->mallocFailed );
       
 68896       return pSrc;
       
 68897     }
       
 68898     pSrc = pNew;
       
 68899     nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
       
 68900     pSrc->nAlloc = (u16)nGot;
       
 68901   }
       
 68902 
       
 68903   /* Move existing slots that come after the newly inserted slots
       
 68904   ** out of the way */
       
 68905   for(i=pSrc->nSrc-1; i>=iStart; i--){
       
 68906     pSrc->a[i+nExtra] = pSrc->a[i];
       
 68907   }
       
 68908   pSrc->nSrc += (i16)nExtra;
       
 68909 
       
 68910   /* Zero the newly allocated slots */
       
 68911   memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
       
 68912   for(i=iStart; i<iStart+nExtra; i++){
       
 68913     pSrc->a[i].iCursor = -1;
       
 68914   }
       
 68915 
       
 68916   /* Return a pointer to the enlarged SrcList */
       
 68917   return pSrc;
       
 68918 }
       
 68919 
       
 68920 
       
 68921 /*
       
 68922 ** Append a new table name to the given SrcList.  Create a new SrcList if
       
 68923 ** need be.  A new entry is created in the SrcList even if pTable is NULL.
       
 68924 **
       
 68925 ** A SrcList is returned, or NULL if there is an OOM error.  The returned
       
 68926 ** SrcList might be the same as the SrcList that was input or it might be
       
 68927 ** a new one.  If an OOM error does occurs, then the prior value of pList
       
 68928 ** that is input to this routine is automatically freed.
       
 68929 **
       
 68930 ** If pDatabase is not null, it means that the table has an optional
       
 68931 ** database name prefix.  Like this:  "database.table".  The pDatabase
       
 68932 ** points to the table name and the pTable points to the database name.
       
 68933 ** The SrcList.a[].zName field is filled with the table name which might
       
 68934 ** come from pTable (if pDatabase is NULL) or from pDatabase.  
       
 68935 ** SrcList.a[].zDatabase is filled with the database name from pTable,
       
 68936 ** or with NULL if no database is specified.
       
 68937 **
       
 68938 ** In other words, if call like this:
       
 68939 **
       
 68940 **         sqlite3SrcListAppend(D,A,B,0);
       
 68941 **
       
 68942 ** Then B is a table name and the database name is unspecified.  If called
       
 68943 ** like this:
       
 68944 **
       
 68945 **         sqlite3SrcListAppend(D,A,B,C);
       
 68946 **
       
 68947 ** Then C is the table name and B is the database name.  If C is defined
       
 68948 ** then so is B.  In other words, we never have a case where:
       
 68949 **
       
 68950 **         sqlite3SrcListAppend(D,A,0,C);
       
 68951 **
       
 68952 ** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
       
 68953 ** before being added to the SrcList.
       
 68954 */
       
 68955 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
       
 68956   sqlite3 *db,        /* Connection to notify of malloc failures */
       
 68957   SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
       
 68958   Token *pTable,      /* Table to append */
       
 68959   Token *pDatabase    /* Database of the table */
       
 68960 ){
       
 68961   struct SrcList_item *pItem;
       
 68962   assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
       
 68963   if( pList==0 ){
       
 68964     pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
       
 68965     if( pList==0 ) return 0;
       
 68966     pList->nAlloc = 1;
       
 68967   }
       
 68968   pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
       
 68969   if( db->mallocFailed ){
       
 68970     sqlite3SrcListDelete(db, pList);
       
 68971     return 0;
       
 68972   }
       
 68973   pItem = &pList->a[pList->nSrc-1];
       
 68974   if( pDatabase && pDatabase->z==0 ){
       
 68975     pDatabase = 0;
       
 68976   }
       
 68977   if( pDatabase ){
       
 68978     Token *pTemp = pDatabase;
       
 68979     pDatabase = pTable;
       
 68980     pTable = pTemp;
       
 68981   }
       
 68982   pItem->zName = sqlite3NameFromToken(db, pTable);
       
 68983   pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
       
 68984   return pList;
       
 68985 }
       
 68986 
       
 68987 /*
       
 68988 ** Assign VdbeCursor index numbers to all tables in a SrcList
       
 68989 */
       
 68990 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
       
 68991   int i;
       
 68992   struct SrcList_item *pItem;
       
 68993   assert(pList || pParse->db->mallocFailed );
       
 68994   if( pList ){
       
 68995     for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
       
 68996       if( pItem->iCursor>=0 ) break;
       
 68997       pItem->iCursor = pParse->nTab++;
       
 68998       if( pItem->pSelect ){
       
 68999         sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
       
 69000       }
       
 69001     }
       
 69002   }
       
 69003 }
       
 69004 
       
 69005 /*
       
 69006 ** Delete an entire SrcList including all its substructure.
       
 69007 */
       
 69008 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
       
 69009   int i;
       
 69010   struct SrcList_item *pItem;
       
 69011   if( pList==0 ) return;
       
 69012   for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
       
 69013     sqlite3DbFree(db, pItem->zDatabase);
       
 69014     sqlite3DbFree(db, pItem->zName);
       
 69015     sqlite3DbFree(db, pItem->zAlias);
       
 69016     sqlite3DbFree(db, pItem->zIndex);
       
 69017     sqlite3DeleteTable(pItem->pTab);
       
 69018     sqlite3SelectDelete(db, pItem->pSelect);
       
 69019     sqlite3ExprDelete(db, pItem->pOn);
       
 69020     sqlite3IdListDelete(db, pItem->pUsing);
       
 69021   }
       
 69022   sqlite3DbFree(db, pList);
       
 69023 }
       
 69024 
       
 69025 /*
       
 69026 ** This routine is called by the parser to add a new term to the
       
 69027 ** end of a growing FROM clause.  The "p" parameter is the part of
       
 69028 ** the FROM clause that has already been constructed.  "p" is NULL
       
 69029 ** if this is the first term of the FROM clause.  pTable and pDatabase
       
 69030 ** are the name of the table and database named in the FROM clause term.
       
 69031 ** pDatabase is NULL if the database name qualifier is missing - the
       
 69032 ** usual case.  If the term has a alias, then pAlias points to the
       
 69033 ** alias token.  If the term is a subquery, then pSubquery is the
       
 69034 ** SELECT statement that the subquery encodes.  The pTable and
       
 69035 ** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
       
 69036 ** parameters are the content of the ON and USING clauses.
       
 69037 **
       
 69038 ** Return a new SrcList which encodes is the FROM with the new
       
 69039 ** term added.
       
 69040 */
       
 69041 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
       
 69042   Parse *pParse,          /* Parsing context */
       
 69043   SrcList *p,             /* The left part of the FROM clause already seen */
       
 69044   Token *pTable,          /* Name of the table to add to the FROM clause */
       
 69045   Token *pDatabase,       /* Name of the database containing pTable */
       
 69046   Token *pAlias,          /* The right-hand side of the AS subexpression */
       
 69047   Select *pSubquery,      /* A subquery used in place of a table name */
       
 69048   Expr *pOn,              /* The ON clause of a join */
       
 69049   IdList *pUsing          /* The USING clause of a join */
       
 69050 ){
       
 69051   struct SrcList_item *pItem;
       
 69052   sqlite3 *db = pParse->db;
       
 69053   if( !p && (pOn || pUsing) ){
       
 69054     sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", 
       
 69055       (pOn ? "ON" : "USING")
       
 69056     );
       
 69057     goto append_from_error;
       
 69058   }
       
 69059   p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
       
 69060   if( p==0 || NEVER(p->nSrc==0) ){
       
 69061     goto append_from_error;
       
 69062   }
       
 69063   pItem = &p->a[p->nSrc-1];
       
 69064   assert( pAlias!=0 );
       
 69065   if( pAlias->n ){
       
 69066     pItem->zAlias = sqlite3NameFromToken(db, pAlias);
       
 69067   }
       
 69068   pItem->pSelect = pSubquery;
       
 69069   pItem->pOn = pOn;
       
 69070   pItem->pUsing = pUsing;
       
 69071   return p;
       
 69072 
       
 69073  append_from_error:
       
 69074   assert( p==0 );
       
 69075   sqlite3ExprDelete(db, pOn);
       
 69076   sqlite3IdListDelete(db, pUsing);
       
 69077   sqlite3SelectDelete(db, pSubquery);
       
 69078   return 0;
       
 69079 }
       
 69080 
       
 69081 /*
       
 69082 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added 
       
 69083 ** element of the source-list passed as the second argument.
       
 69084 */
       
 69085 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
       
 69086   assert( pIndexedBy!=0 );
       
 69087   if( p && ALWAYS(p->nSrc>0) ){
       
 69088     struct SrcList_item *pItem = &p->a[p->nSrc-1];
       
 69089     assert( pItem->notIndexed==0 && pItem->zIndex==0 );
       
 69090     if( pIndexedBy->n==1 && !pIndexedBy->z ){
       
 69091       /* A "NOT INDEXED" clause was supplied. See parse.y 
       
 69092       ** construct "indexed_opt" for details. */
       
 69093       pItem->notIndexed = 1;
       
 69094     }else{
       
 69095       pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
       
 69096     }
       
 69097   }
       
 69098 }
       
 69099 
       
 69100 /*
       
 69101 ** When building up a FROM clause in the parser, the join operator
       
 69102 ** is initially attached to the left operand.  But the code generator
       
 69103 ** expects the join operator to be on the right operand.  This routine
       
 69104 ** Shifts all join operators from left to right for an entire FROM
       
 69105 ** clause.
       
 69106 **
       
 69107 ** Example: Suppose the join is like this:
       
 69108 **
       
 69109 **           A natural cross join B
       
 69110 **
       
 69111 ** The operator is "natural cross join".  The A and B operands are stored
       
 69112 ** in p->a[0] and p->a[1], respectively.  The parser initially stores the
       
 69113 ** operator with A.  This routine shifts that operator over to B.
       
 69114 */
       
 69115 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
       
 69116   if( p && p->a ){
       
 69117     int i;
       
 69118     for(i=p->nSrc-1; i>0; i--){
       
 69119       p->a[i].jointype = p->a[i-1].jointype;
       
 69120     }
       
 69121     p->a[0].jointype = 0;
       
 69122   }
       
 69123 }
       
 69124 
       
 69125 /*
       
 69126 ** Begin a transaction
       
 69127 */
       
 69128 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
       
 69129   sqlite3 *db;
       
 69130   Vdbe *v;
       
 69131   int i;
       
 69132 
       
 69133   assert( pParse!=0 );
       
 69134   db = pParse->db;
       
 69135   assert( db!=0 );
       
 69136 /*  if( db->aDb[0].pBt==0 ) return; */
       
 69137   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
       
 69138     return;
       
 69139   }
       
 69140   v = sqlite3GetVdbe(pParse);
       
 69141   if( !v ) return;
       
 69142   if( type!=TK_DEFERRED ){
       
 69143     for(i=0; i<db->nDb; i++){
       
 69144       sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
       
 69145       sqlite3VdbeUsesBtree(v, i);
       
 69146     }
       
 69147   }
       
 69148   sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
       
 69149 }
       
 69150 
       
 69151 /*
       
 69152 ** Commit a transaction
       
 69153 */
       
 69154 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
       
 69155   sqlite3 *db;
       
 69156   Vdbe *v;
       
 69157 
       
 69158   assert( pParse!=0 );
       
 69159   db = pParse->db;
       
 69160   assert( db!=0 );
       
 69161 /*  if( db->aDb[0].pBt==0 ) return; */
       
 69162   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
       
 69163     return;
       
 69164   }
       
 69165   v = sqlite3GetVdbe(pParse);
       
 69166   if( v ){
       
 69167     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
       
 69168   }
       
 69169 }
       
 69170 
       
 69171 /*
       
 69172 ** Rollback a transaction
       
 69173 */
       
 69174 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
       
 69175   sqlite3 *db;
       
 69176   Vdbe *v;
       
 69177 
       
 69178   assert( pParse!=0 );
       
 69179   db = pParse->db;
       
 69180   assert( db!=0 );
       
 69181 /*  if( db->aDb[0].pBt==0 ) return; */
       
 69182   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
       
 69183     return;
       
 69184   }
       
 69185   v = sqlite3GetVdbe(pParse);
       
 69186   if( v ){
       
 69187     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
       
 69188   }
       
 69189 }
       
 69190 
       
 69191 /*
       
 69192 ** This function is called by the parser when it parses a command to create,
       
 69193 ** release or rollback an SQL savepoint. 
       
 69194 */
       
 69195 SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
       
 69196   char *zName = sqlite3NameFromToken(pParse->db, pName);
       
 69197   if( zName ){
       
 69198     Vdbe *v = sqlite3GetVdbe(pParse);
       
 69199 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 69200     static const char *az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
       
 69201     assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
       
 69202 #endif
       
 69203     if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
       
 69204       sqlite3DbFree(pParse->db, zName);
       
 69205       return;
       
 69206     }
       
 69207     sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
       
 69208   }
       
 69209 }
       
 69210 
       
 69211 /*
       
 69212 ** Make sure the TEMP database is open and available for use.  Return
       
 69213 ** the number of errors.  Leave any error messages in the pParse structure.
       
 69214 */
       
 69215 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
       
 69216   sqlite3 *db = pParse->db;
       
 69217   if( db->aDb[1].pBt==0 && !pParse->explain ){
       
 69218     int rc;
       
 69219     static const int flags = 
       
 69220           SQLITE_OPEN_READWRITE |
       
 69221           SQLITE_OPEN_CREATE |
       
 69222           SQLITE_OPEN_EXCLUSIVE |
       
 69223           SQLITE_OPEN_DELETEONCLOSE |
       
 69224           SQLITE_OPEN_TEMP_DB;
       
 69225 
       
 69226     rc = sqlite3BtreeFactory(db, 0, 0, SQLITE_DEFAULT_CACHE_SIZE, flags,
       
 69227                                  &db->aDb[1].pBt);
       
 69228     if( rc!=SQLITE_OK ){
       
 69229       sqlite3ErrorMsg(pParse, "unable to open a temporary database "
       
 69230         "file for storing temporary tables");
       
 69231       pParse->rc = rc;
       
 69232       return 1;
       
 69233     }
       
 69234     assert( (db->flags & SQLITE_InTrans)==0 || db->autoCommit );
       
 69235     assert( db->aDb[1].pSchema );
       
 69236     sqlite3PagerJournalMode(sqlite3BtreePager(db->aDb[1].pBt),
       
 69237                             db->dfltJournalMode);
       
 69238   }
       
 69239   return 0;
       
 69240 }
       
 69241 
       
 69242 /*
       
 69243 ** Generate VDBE code that will verify the schema cookie and start
       
 69244 ** a read-transaction for all named database files.
       
 69245 **
       
 69246 ** It is important that all schema cookies be verified and all
       
 69247 ** read transactions be started before anything else happens in
       
 69248 ** the VDBE program.  But this routine can be called after much other
       
 69249 ** code has been generated.  So here is what we do:
       
 69250 **
       
 69251 ** The first time this routine is called, we code an OP_Goto that
       
 69252 ** will jump to a subroutine at the end of the program.  Then we
       
 69253 ** record every database that needs its schema verified in the
       
 69254 ** pParse->cookieMask field.  Later, after all other code has been
       
 69255 ** generated, the subroutine that does the cookie verifications and
       
 69256 ** starts the transactions will be coded and the OP_Goto P2 value
       
 69257 ** will be made to point to that subroutine.  The generation of the
       
 69258 ** cookie verification subroutine code happens in sqlite3FinishCoding().
       
 69259 **
       
 69260 ** If iDb<0 then code the OP_Goto only - don't set flag to verify the
       
 69261 ** schema on any databases.  This can be used to position the OP_Goto
       
 69262 ** early in the code, before we know if any database tables will be used.
       
 69263 */
       
 69264 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
       
 69265   Parse *pToplevel = sqlite3ParseToplevel(pParse);
       
 69266 
       
 69267   if( pToplevel->cookieGoto==0 ){
       
 69268     Vdbe *v = sqlite3GetVdbe(pToplevel);
       
 69269     if( v==0 ) return;  /* This only happens if there was a prior error */
       
 69270     pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
       
 69271   }
       
 69272   if( iDb>=0 ){
       
 69273     sqlite3 *db = pToplevel->db;
       
 69274     int mask;
       
 69275 
       
 69276     assert( iDb<db->nDb );
       
 69277     assert( db->aDb[iDb].pBt!=0 || iDb==1 );
       
 69278     assert( iDb<SQLITE_MAX_ATTACHED+2 );
       
 69279     mask = 1<<iDb;
       
 69280     if( (pToplevel->cookieMask & mask)==0 ){
       
 69281       pToplevel->cookieMask |= mask;
       
 69282       pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
       
 69283       if( !OMIT_TEMPDB && iDb==1 ){
       
 69284         sqlite3OpenTempDatabase(pToplevel);
       
 69285       }
       
 69286     }
       
 69287   }
       
 69288 }
       
 69289 
       
 69290 /*
       
 69291 ** Generate VDBE code that prepares for doing an operation that
       
 69292 ** might change the database.
       
 69293 **
       
 69294 ** This routine starts a new transaction if we are not already within
       
 69295 ** a transaction.  If we are already within a transaction, then a checkpoint
       
 69296 ** is set if the setStatement parameter is true.  A checkpoint should
       
 69297 ** be set for operations that might fail (due to a constraint) part of
       
 69298 ** the way through and which will need to undo some writes without having to
       
 69299 ** rollback the whole transaction.  For operations where all constraints
       
 69300 ** can be checked before any changes are made to the database, it is never
       
 69301 ** necessary to undo a write and the checkpoint should not be set.
       
 69302 */
       
 69303 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
       
 69304   Parse *pToplevel = sqlite3ParseToplevel(pParse);
       
 69305   sqlite3CodeVerifySchema(pParse, iDb);
       
 69306   pToplevel->writeMask |= 1<<iDb;
       
 69307   pToplevel->isMultiWrite |= setStatement;
       
 69308 }
       
 69309 
       
 69310 /*
       
 69311 ** Indicate that the statement currently under construction might write
       
 69312 ** more than one entry (example: deleting one row then inserting another,
       
 69313 ** inserting multiple rows in a table, or inserting a row and index entries.)
       
 69314 ** If an abort occurs after some of these writes have completed, then it will
       
 69315 ** be necessary to undo the completed writes.
       
 69316 */
       
 69317 SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
       
 69318   Parse *pToplevel = sqlite3ParseToplevel(pParse);
       
 69319   pToplevel->isMultiWrite = 1;
       
 69320 }
       
 69321 
       
 69322 /* 
       
 69323 ** The code generator calls this routine if is discovers that it is
       
 69324 ** possible to abort a statement prior to completion.  In order to 
       
 69325 ** perform this abort without corrupting the database, we need to make
       
 69326 ** sure that the statement is protected by a statement transaction.
       
 69327 **
       
 69328 ** Technically, we only need to set the mayAbort flag if the
       
 69329 ** isMultiWrite flag was previously set.  There is a time dependency
       
 69330 ** such that the abort must occur after the multiwrite.  This makes
       
 69331 ** some statements involving the REPLACE conflict resolution algorithm
       
 69332 ** go a little faster.  But taking advantage of this time dependency
       
 69333 ** makes it more difficult to prove that the code is correct (in 
       
 69334 ** particular, it prevents us from writing an effective
       
 69335 ** implementation of sqlite3AssertMayAbort()) and so we have chosen
       
 69336 ** to take the safe route and skip the optimization.
       
 69337 */
       
 69338 SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
       
 69339   Parse *pToplevel = sqlite3ParseToplevel(pParse);
       
 69340   pToplevel->mayAbort = 1;
       
 69341 }
       
 69342 
       
 69343 /*
       
 69344 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
       
 69345 ** error. The onError parameter determines which (if any) of the statement
       
 69346 ** and/or current transaction is rolled back.
       
 69347 */
       
 69348 SQLITE_PRIVATE void sqlite3HaltConstraint(Parse *pParse, int onError, char *p4, int p4type){
       
 69349   Vdbe *v = sqlite3GetVdbe(pParse);
       
 69350   if( onError==OE_Abort ){
       
 69351     sqlite3MayAbort(pParse);
       
 69352   }
       
 69353   sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, p4, p4type);
       
 69354 }
       
 69355 
       
 69356 /*
       
 69357 ** Check to see if pIndex uses the collating sequence pColl.  Return
       
 69358 ** true if it does and false if it does not.
       
 69359 */
       
 69360 #ifndef SQLITE_OMIT_REINDEX
       
 69361 static int collationMatch(const char *zColl, Index *pIndex){
       
 69362   int i;
       
 69363   assert( zColl!=0 );
       
 69364   for(i=0; i<pIndex->nColumn; i++){
       
 69365     const char *z = pIndex->azColl[i];
       
 69366     assert( z!=0 );
       
 69367     if( 0==sqlite3StrICmp(z, zColl) ){
       
 69368       return 1;
       
 69369     }
       
 69370   }
       
 69371   return 0;
       
 69372 }
       
 69373 #endif
       
 69374 
       
 69375 /*
       
 69376 ** Recompute all indices of pTab that use the collating sequence pColl.
       
 69377 ** If pColl==0 then recompute all indices of pTab.
       
 69378 */
       
 69379 #ifndef SQLITE_OMIT_REINDEX
       
 69380 static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
       
 69381   Index *pIndex;              /* An index associated with pTab */
       
 69382 
       
 69383   for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
       
 69384     if( zColl==0 || collationMatch(zColl, pIndex) ){
       
 69385       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
       
 69386       sqlite3BeginWriteOperation(pParse, 0, iDb);
       
 69387       sqlite3RefillIndex(pParse, pIndex, -1);
       
 69388     }
       
 69389   }
       
 69390 }
       
 69391 #endif
       
 69392 
       
 69393 /*
       
 69394 ** Recompute all indices of all tables in all databases where the
       
 69395 ** indices use the collating sequence pColl.  If pColl==0 then recompute
       
 69396 ** all indices everywhere.
       
 69397 */
       
 69398 #ifndef SQLITE_OMIT_REINDEX
       
 69399 static void reindexDatabases(Parse *pParse, char const *zColl){
       
 69400   Db *pDb;                    /* A single database */
       
 69401   int iDb;                    /* The database index number */
       
 69402   sqlite3 *db = pParse->db;   /* The database connection */
       
 69403   HashElem *k;                /* For looping over tables in pDb */
       
 69404   Table *pTab;                /* A table in the database */
       
 69405 
       
 69406   for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
       
 69407     assert( pDb!=0 );
       
 69408     for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
       
 69409       pTab = (Table*)sqliteHashData(k);
       
 69410       reindexTable(pParse, pTab, zColl);
       
 69411     }
       
 69412   }
       
 69413 }
       
 69414 #endif
       
 69415 
       
 69416 /*
       
 69417 ** Generate code for the REINDEX command.
       
 69418 **
       
 69419 **        REINDEX                            -- 1
       
 69420 **        REINDEX  <collation>               -- 2
       
 69421 **        REINDEX  ?<database>.?<tablename>  -- 3
       
 69422 **        REINDEX  ?<database>.?<indexname>  -- 4
       
 69423 **
       
 69424 ** Form 1 causes all indices in all attached databases to be rebuilt.
       
 69425 ** Form 2 rebuilds all indices in all databases that use the named
       
 69426 ** collating function.  Forms 3 and 4 rebuild the named index or all
       
 69427 ** indices associated with the named table.
       
 69428 */
       
 69429 #ifndef SQLITE_OMIT_REINDEX
       
 69430 SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
       
 69431   CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
       
 69432   char *z;                    /* Name of a table or index */
       
 69433   const char *zDb;            /* Name of the database */
       
 69434   Table *pTab;                /* A table in the database */
       
 69435   Index *pIndex;              /* An index associated with pTab */
       
 69436   int iDb;                    /* The database index number */
       
 69437   sqlite3 *db = pParse->db;   /* The database connection */
       
 69438   Token *pObjName;            /* Name of the table or index to be reindexed */
       
 69439 
       
 69440   /* Read the database schema. If an error occurs, leave an error message
       
 69441   ** and code in pParse and return NULL. */
       
 69442   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
       
 69443     return;
       
 69444   }
       
 69445 
       
 69446   if( pName1==0 ){
       
 69447     reindexDatabases(pParse, 0);
       
 69448     return;
       
 69449   }else if( NEVER(pName2==0) || pName2->z==0 ){
       
 69450     char *zColl;
       
 69451     assert( pName1->z );
       
 69452     zColl = sqlite3NameFromToken(pParse->db, pName1);
       
 69453     if( !zColl ) return;
       
 69454     pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
       
 69455     if( pColl ){
       
 69456       reindexDatabases(pParse, zColl);
       
 69457       sqlite3DbFree(db, zColl);
       
 69458       return;
       
 69459     }
       
 69460     sqlite3DbFree(db, zColl);
       
 69461   }
       
 69462   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
       
 69463   if( iDb<0 ) return;
       
 69464   z = sqlite3NameFromToken(db, pObjName);
       
 69465   if( z==0 ) return;
       
 69466   zDb = db->aDb[iDb].zName;
       
 69467   pTab = sqlite3FindTable(db, z, zDb);
       
 69468   if( pTab ){
       
 69469     reindexTable(pParse, pTab, 0);
       
 69470     sqlite3DbFree(db, z);
       
 69471     return;
       
 69472   }
       
 69473   pIndex = sqlite3FindIndex(db, z, zDb);
       
 69474   sqlite3DbFree(db, z);
       
 69475   if( pIndex ){
       
 69476     sqlite3BeginWriteOperation(pParse, 0, iDb);
       
 69477     sqlite3RefillIndex(pParse, pIndex, -1);
       
 69478     return;
       
 69479   }
       
 69480   sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
       
 69481 }
       
 69482 #endif
       
 69483 
       
 69484 /*
       
 69485 ** Return a dynamicly allocated KeyInfo structure that can be used
       
 69486 ** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
       
 69487 **
       
 69488 ** If successful, a pointer to the new structure is returned. In this case
       
 69489 ** the caller is responsible for calling sqlite3DbFree(db, ) on the returned 
       
 69490 ** pointer. If an error occurs (out of memory or missing collation 
       
 69491 ** sequence), NULL is returned and the state of pParse updated to reflect
       
 69492 ** the error.
       
 69493 */
       
 69494 SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){
       
 69495   int i;
       
 69496   int nCol = pIdx->nColumn;
       
 69497   int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol;
       
 69498   sqlite3 *db = pParse->db;
       
 69499   KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZero(db, nBytes);
       
 69500 
       
 69501   if( pKey ){
       
 69502     pKey->db = pParse->db;
       
 69503     pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]);
       
 69504     assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) );
       
 69505     for(i=0; i<nCol; i++){
       
 69506       char *zColl = pIdx->azColl[i];
       
 69507       assert( zColl );
       
 69508       pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl);
       
 69509       pKey->aSortOrder[i] = pIdx->aSortOrder[i];
       
 69510     }
       
 69511     pKey->nField = (u16)nCol;
       
 69512   }
       
 69513 
       
 69514   if( pParse->nErr ){
       
 69515     sqlite3DbFree(db, pKey);
       
 69516     pKey = 0;
       
 69517   }
       
 69518   return pKey;
       
 69519 }
       
 69520 
       
 69521 /************** End of build.c ***********************************************/
       
 69522 /************** Begin file callback.c ****************************************/
       
 69523 /*
       
 69524 ** 2005 May 23 
       
 69525 **
       
 69526 ** The author disclaims copyright to this source code.  In place of
       
 69527 ** a legal notice, here is a blessing:
       
 69528 **
       
 69529 **    May you do good and not evil.
       
 69530 **    May you find forgiveness for yourself and forgive others.
       
 69531 **    May you share freely, never taking more than you give.
       
 69532 **
       
 69533 *************************************************************************
       
 69534 **
       
 69535 ** This file contains functions used to access the internal hash tables
       
 69536 ** of user defined functions and collation sequences.
       
 69537 **
       
 69538 ** $Id: callback.c,v 1.42 2009/06/17 00:35:31 drh Exp $
       
 69539 */
       
 69540 
       
 69541 
       
 69542 /*
       
 69543 ** Invoke the 'collation needed' callback to request a collation sequence
       
 69544 ** in the encoding enc of name zName, length nName.
       
 69545 */
       
 69546 static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
       
 69547   assert( !db->xCollNeeded || !db->xCollNeeded16 );
       
 69548   if( db->xCollNeeded ){
       
 69549     char *zExternal = sqlite3DbStrDup(db, zName);
       
 69550     if( !zExternal ) return;
       
 69551     db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
       
 69552     sqlite3DbFree(db, zExternal);
       
 69553   }
       
 69554 #ifndef SQLITE_OMIT_UTF16
       
 69555   if( db->xCollNeeded16 ){
       
 69556     char const *zExternal;
       
 69557     sqlite3_value *pTmp = sqlite3ValueNew(db);
       
 69558     sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
       
 69559     zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
       
 69560     if( zExternal ){
       
 69561       db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
       
 69562     }
       
 69563     sqlite3ValueFree(pTmp);
       
 69564   }
       
 69565 #endif
       
 69566 }
       
 69567 
       
 69568 /*
       
 69569 ** This routine is called if the collation factory fails to deliver a
       
 69570 ** collation function in the best encoding but there may be other versions
       
 69571 ** of this collation function (for other text encodings) available. Use one
       
 69572 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
       
 69573 ** possible.
       
 69574 */
       
 69575 static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
       
 69576   CollSeq *pColl2;
       
 69577   char *z = pColl->zName;
       
 69578   int i;
       
 69579   static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
       
 69580   for(i=0; i<3; i++){
       
 69581     pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
       
 69582     if( pColl2->xCmp!=0 ){
       
 69583       memcpy(pColl, pColl2, sizeof(CollSeq));
       
 69584       pColl->xDel = 0;         /* Do not copy the destructor */
       
 69585       return SQLITE_OK;
       
 69586     }
       
 69587   }
       
 69588   return SQLITE_ERROR;
       
 69589 }
       
 69590 
       
 69591 /*
       
 69592 ** This function is responsible for invoking the collation factory callback
       
 69593 ** or substituting a collation sequence of a different encoding when the
       
 69594 ** requested collation sequence is not available in the desired encoding.
       
 69595 ** 
       
 69596 ** If it is not NULL, then pColl must point to the database native encoding 
       
 69597 ** collation sequence with name zName, length nName.
       
 69598 **
       
 69599 ** The return value is either the collation sequence to be used in database
       
 69600 ** db for collation type name zName, length nName, or NULL, if no collation
       
 69601 ** sequence can be found.
       
 69602 **
       
 69603 ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
       
 69604 */
       
 69605 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
       
 69606   sqlite3* db,          /* The database connection */
       
 69607   u8 enc,               /* The desired encoding for the collating sequence */
       
 69608   CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
       
 69609   const char *zName     /* Collating sequence name */
       
 69610 ){
       
 69611   CollSeq *p;
       
 69612 
       
 69613   p = pColl;
       
 69614   if( !p ){
       
 69615     p = sqlite3FindCollSeq(db, enc, zName, 0);
       
 69616   }
       
 69617   if( !p || !p->xCmp ){
       
 69618     /* No collation sequence of this type for this encoding is registered.
       
 69619     ** Call the collation factory to see if it can supply us with one.
       
 69620     */
       
 69621     callCollNeeded(db, enc, zName);
       
 69622     p = sqlite3FindCollSeq(db, enc, zName, 0);
       
 69623   }
       
 69624   if( p && !p->xCmp && synthCollSeq(db, p) ){
       
 69625     p = 0;
       
 69626   }
       
 69627   assert( !p || p->xCmp );
       
 69628   return p;
       
 69629 }
       
 69630 
       
 69631 /*
       
 69632 ** This routine is called on a collation sequence before it is used to
       
 69633 ** check that it is defined. An undefined collation sequence exists when
       
 69634 ** a database is loaded that contains references to collation sequences
       
 69635 ** that have not been defined by sqlite3_create_collation() etc.
       
 69636 **
       
 69637 ** If required, this routine calls the 'collation needed' callback to
       
 69638 ** request a definition of the collating sequence. If this doesn't work, 
       
 69639 ** an equivalent collating sequence that uses a text encoding different
       
 69640 ** from the main database is substituted, if one is available.
       
 69641 */
       
 69642 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
       
 69643   if( pColl ){
       
 69644     const char *zName = pColl->zName;
       
 69645     sqlite3 *db = pParse->db;
       
 69646     CollSeq *p = sqlite3GetCollSeq(db, ENC(db), pColl, zName);
       
 69647     if( !p ){
       
 69648       sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
       
 69649       pParse->nErr++;
       
 69650       return SQLITE_ERROR;
       
 69651     }
       
 69652     assert( p==pColl );
       
 69653   }
       
 69654   return SQLITE_OK;
       
 69655 }
       
 69656 
       
 69657 
       
 69658 
       
 69659 /*
       
 69660 ** Locate and return an entry from the db.aCollSeq hash table. If the entry
       
 69661 ** specified by zName and nName is not found and parameter 'create' is
       
 69662 ** true, then create a new entry. Otherwise return NULL.
       
 69663 **
       
 69664 ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
       
 69665 ** array of three CollSeq structures. The first is the collation sequence
       
 69666 ** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
       
 69667 **
       
 69668 ** Stored immediately after the three collation sequences is a copy of
       
 69669 ** the collation sequence name. A pointer to this string is stored in
       
 69670 ** each collation sequence structure.
       
 69671 */
       
 69672 static CollSeq *findCollSeqEntry(
       
 69673   sqlite3 *db,          /* Database connection */
       
 69674   const char *zName,    /* Name of the collating sequence */
       
 69675   int create            /* Create a new entry if true */
       
 69676 ){
       
 69677   CollSeq *pColl;
       
 69678   int nName = sqlite3Strlen30(zName);
       
 69679   pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
       
 69680 
       
 69681   if( 0==pColl && create ){
       
 69682     pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
       
 69683     if( pColl ){
       
 69684       CollSeq *pDel = 0;
       
 69685       pColl[0].zName = (char*)&pColl[3];
       
 69686       pColl[0].enc = SQLITE_UTF8;
       
 69687       pColl[1].zName = (char*)&pColl[3];
       
 69688       pColl[1].enc = SQLITE_UTF16LE;
       
 69689       pColl[2].zName = (char*)&pColl[3];
       
 69690       pColl[2].enc = SQLITE_UTF16BE;
       
 69691       memcpy(pColl[0].zName, zName, nName);
       
 69692       pColl[0].zName[nName] = 0;
       
 69693       pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
       
 69694 
       
 69695       /* If a malloc() failure occurred in sqlite3HashInsert(), it will 
       
 69696       ** return the pColl pointer to be deleted (because it wasn't added
       
 69697       ** to the hash table).
       
 69698       */
       
 69699       assert( pDel==0 || pDel==pColl );
       
 69700       if( pDel!=0 ){
       
 69701         db->mallocFailed = 1;
       
 69702         sqlite3DbFree(db, pDel);
       
 69703         pColl = 0;
       
 69704       }
       
 69705     }
       
 69706   }
       
 69707   return pColl;
       
 69708 }
       
 69709 
       
 69710 /*
       
 69711 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
       
 69712 ** Return the CollSeq* pointer for the collation sequence named zName
       
 69713 ** for the encoding 'enc' from the database 'db'.
       
 69714 **
       
 69715 ** If the entry specified is not found and 'create' is true, then create a
       
 69716 ** new entry.  Otherwise return NULL.
       
 69717 **
       
 69718 ** A separate function sqlite3LocateCollSeq() is a wrapper around
       
 69719 ** this routine.  sqlite3LocateCollSeq() invokes the collation factory
       
 69720 ** if necessary and generates an error message if the collating sequence
       
 69721 ** cannot be found.
       
 69722 **
       
 69723 ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
       
 69724 */
       
 69725 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
       
 69726   sqlite3 *db,
       
 69727   u8 enc,
       
 69728   const char *zName,
       
 69729   int create
       
 69730 ){
       
 69731   CollSeq *pColl;
       
 69732   if( zName ){
       
 69733     pColl = findCollSeqEntry(db, zName, create);
       
 69734   }else{
       
 69735     pColl = db->pDfltColl;
       
 69736   }
       
 69737   assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
       
 69738   assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
       
 69739   if( pColl ) pColl += enc-1;
       
 69740   return pColl;
       
 69741 }
       
 69742 
       
 69743 /* During the search for the best function definition, this procedure
       
 69744 ** is called to test how well the function passed as the first argument
       
 69745 ** matches the request for a function with nArg arguments in a system
       
 69746 ** that uses encoding enc. The value returned indicates how well the
       
 69747 ** request is matched. A higher value indicates a better match.
       
 69748 **
       
 69749 ** The returned value is always between 0 and 6, as follows:
       
 69750 **
       
 69751 ** 0: Not a match, or if nArg<0 and the function is has no implementation.
       
 69752 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
       
 69753 **    encoding is requested, or vice versa.
       
 69754 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
       
 69755 **    requested, or vice versa.
       
 69756 ** 3: A variable arguments function using the same text encoding.
       
 69757 ** 4: A function with the exact number of arguments requested that
       
 69758 **    prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
       
 69759 ** 5: A function with the exact number of arguments requested that
       
 69760 **    prefers UTF-16LE when UTF-16BE is requested, or vice versa.
       
 69761 ** 6: An exact match.
       
 69762 **
       
 69763 */
       
 69764 static int matchQuality(FuncDef *p, int nArg, u8 enc){
       
 69765   int match = 0;
       
 69766   if( p->nArg==-1 || p->nArg==nArg 
       
 69767    || (nArg==-1 && (p->xFunc!=0 || p->xStep!=0))
       
 69768   ){
       
 69769     match = 1;
       
 69770     if( p->nArg==nArg || nArg==-1 ){
       
 69771       match = 4;
       
 69772     }
       
 69773     if( enc==p->iPrefEnc ){
       
 69774       match += 2;
       
 69775     }
       
 69776     else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
       
 69777              (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
       
 69778       match += 1;
       
 69779     }
       
 69780   }
       
 69781   return match;
       
 69782 }
       
 69783 
       
 69784 /*
       
 69785 ** Search a FuncDefHash for a function with the given name.  Return
       
 69786 ** a pointer to the matching FuncDef if found, or 0 if there is no match.
       
 69787 */
       
 69788 static FuncDef *functionSearch(
       
 69789   FuncDefHash *pHash,  /* Hash table to search */
       
 69790   int h,               /* Hash of the name */
       
 69791   const char *zFunc,   /* Name of function */
       
 69792   int nFunc            /* Number of bytes in zFunc */
       
 69793 ){
       
 69794   FuncDef *p;
       
 69795   for(p=pHash->a[h]; p; p=p->pHash){
       
 69796     if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
       
 69797       return p;
       
 69798     }
       
 69799   }
       
 69800   return 0;
       
 69801 }
       
 69802 
       
 69803 /*
       
 69804 ** Insert a new FuncDef into a FuncDefHash hash table.
       
 69805 */
       
 69806 SQLITE_PRIVATE void sqlite3FuncDefInsert(
       
 69807   FuncDefHash *pHash,  /* The hash table into which to insert */
       
 69808   FuncDef *pDef        /* The function definition to insert */
       
 69809 ){
       
 69810   FuncDef *pOther;
       
 69811   int nName = sqlite3Strlen30(pDef->zName);
       
 69812   u8 c1 = (u8)pDef->zName[0];
       
 69813   int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
       
 69814   pOther = functionSearch(pHash, h, pDef->zName, nName);
       
 69815   if( pOther ){
       
 69816     assert( pOther!=pDef && pOther->pNext!=pDef );
       
 69817     pDef->pNext = pOther->pNext;
       
 69818     pOther->pNext = pDef;
       
 69819   }else{
       
 69820     pDef->pNext = 0;
       
 69821     pDef->pHash = pHash->a[h];
       
 69822     pHash->a[h] = pDef;
       
 69823   }
       
 69824 }
       
 69825   
       
 69826   
       
 69827 
       
 69828 /*
       
 69829 ** Locate a user function given a name, a number of arguments and a flag
       
 69830 ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
       
 69831 ** pointer to the FuncDef structure that defines that function, or return
       
 69832 ** NULL if the function does not exist.
       
 69833 **
       
 69834 ** If the createFlag argument is true, then a new (blank) FuncDef
       
 69835 ** structure is created and liked into the "db" structure if a
       
 69836 ** no matching function previously existed.  When createFlag is true
       
 69837 ** and the nArg parameter is -1, then only a function that accepts
       
 69838 ** any number of arguments will be returned.
       
 69839 **
       
 69840 ** If createFlag is false and nArg is -1, then the first valid
       
 69841 ** function found is returned.  A function is valid if either xFunc
       
 69842 ** or xStep is non-zero.
       
 69843 **
       
 69844 ** If createFlag is false, then a function with the required name and
       
 69845 ** number of arguments may be returned even if the eTextRep flag does not
       
 69846 ** match that requested.
       
 69847 */
       
 69848 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
       
 69849   sqlite3 *db,       /* An open database */
       
 69850   const char *zName, /* Name of the function.  Not null-terminated */
       
 69851   int nName,         /* Number of characters in the name */
       
 69852   int nArg,          /* Number of arguments.  -1 means any number */
       
 69853   u8 enc,            /* Preferred text encoding */
       
 69854   int createFlag     /* Create new entry if true and does not otherwise exist */
       
 69855 ){
       
 69856   FuncDef *p;         /* Iterator variable */
       
 69857   FuncDef *pBest = 0; /* Best match found so far */
       
 69858   int bestScore = 0;  /* Score of best match */
       
 69859   int h;              /* Hash value */
       
 69860 
       
 69861 
       
 69862   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
       
 69863   h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
       
 69864 
       
 69865   /* First search for a match amongst the application-defined functions.
       
 69866   */
       
 69867   p = functionSearch(&db->aFunc, h, zName, nName);
       
 69868   while( p ){
       
 69869     int score = matchQuality(p, nArg, enc);
       
 69870     if( score>bestScore ){
       
 69871       pBest = p;
       
 69872       bestScore = score;
       
 69873     }
       
 69874     p = p->pNext;
       
 69875   }
       
 69876 
       
 69877   /* If no match is found, search the built-in functions.
       
 69878   **
       
 69879   ** Except, if createFlag is true, that means that we are trying to
       
 69880   ** install a new function.  Whatever FuncDef structure is returned will
       
 69881   ** have fields overwritten with new information appropriate for the
       
 69882   ** new function.  But the FuncDefs for built-in functions are read-only.
       
 69883   ** So we must not search for built-ins when creating a new function.
       
 69884   */ 
       
 69885   if( !createFlag && !pBest ){
       
 69886     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
       
 69887     p = functionSearch(pHash, h, zName, nName);
       
 69888     while( p ){
       
 69889       int score = matchQuality(p, nArg, enc);
       
 69890       if( score>bestScore ){
       
 69891         pBest = p;
       
 69892         bestScore = score;
       
 69893       }
       
 69894       p = p->pNext;
       
 69895     }
       
 69896   }
       
 69897 
       
 69898   /* If the createFlag parameter is true and the search did not reveal an
       
 69899   ** exact match for the name, number of arguments and encoding, then add a
       
 69900   ** new entry to the hash table and return it.
       
 69901   */
       
 69902   if( createFlag && (bestScore<6 || pBest->nArg!=nArg) && 
       
 69903       (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
       
 69904     pBest->zName = (char *)&pBest[1];
       
 69905     pBest->nArg = (u16)nArg;
       
 69906     pBest->iPrefEnc = enc;
       
 69907     memcpy(pBest->zName, zName, nName);
       
 69908     pBest->zName[nName] = 0;
       
 69909     sqlite3FuncDefInsert(&db->aFunc, pBest);
       
 69910   }
       
 69911 
       
 69912   if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
       
 69913     return pBest;
       
 69914   }
       
 69915   return 0;
       
 69916 }
       
 69917 
       
 69918 /*
       
 69919 ** Free all resources held by the schema structure. The void* argument points
       
 69920 ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the 
       
 69921 ** pointer itself, it just cleans up subsiduary resources (i.e. the contents
       
 69922 ** of the schema hash tables).
       
 69923 **
       
 69924 ** The Schema.cache_size variable is not cleared.
       
 69925 */
       
 69926 SQLITE_PRIVATE void sqlite3SchemaFree(void *p){
       
 69927   Hash temp1;
       
 69928   Hash temp2;
       
 69929   HashElem *pElem;
       
 69930   Schema *pSchema = (Schema *)p;
       
 69931 
       
 69932   temp1 = pSchema->tblHash;
       
 69933   temp2 = pSchema->trigHash;
       
 69934   sqlite3HashInit(&pSchema->trigHash);
       
 69935   sqlite3HashClear(&pSchema->idxHash);
       
 69936   for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
       
 69937     sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
       
 69938   }
       
 69939   sqlite3HashClear(&temp2);
       
 69940   sqlite3HashInit(&pSchema->tblHash);
       
 69941   for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
       
 69942     Table *pTab = sqliteHashData(pElem);
       
 69943     assert( pTab->dbMem==0 );
       
 69944     sqlite3DeleteTable(pTab);
       
 69945   }
       
 69946   sqlite3HashClear(&temp1);
       
 69947   sqlite3HashClear(&pSchema->fkeyHash);
       
 69948   pSchema->pSeqTab = 0;
       
 69949   pSchema->flags &= ~DB_SchemaLoaded;
       
 69950 }
       
 69951 
       
 69952 /*
       
 69953 ** Find and return the schema associated with a BTree.  Create
       
 69954 ** a new one if necessary.
       
 69955 */
       
 69956 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
       
 69957   Schema * p;
       
 69958   if( pBt ){
       
 69959     p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaFree);
       
 69960   }else{
       
 69961     p = (Schema *)sqlite3MallocZero(sizeof(Schema));
       
 69962   }
       
 69963   if( !p ){
       
 69964     db->mallocFailed = 1;
       
 69965   }else if ( 0==p->file_format ){
       
 69966     sqlite3HashInit(&p->tblHash);
       
 69967     sqlite3HashInit(&p->idxHash);
       
 69968     sqlite3HashInit(&p->trigHash);
       
 69969     sqlite3HashInit(&p->fkeyHash);
       
 69970     p->enc = SQLITE_UTF8;
       
 69971   }
       
 69972   return p;
       
 69973 }
       
 69974 
       
 69975 /************** End of callback.c ********************************************/
       
 69976 /************** Begin file delete.c ******************************************/
       
 69977 /*
       
 69978 ** 2001 September 15
       
 69979 **
       
 69980 ** The author disclaims copyright to this source code.  In place of
       
 69981 ** a legal notice, here is a blessing:
       
 69982 **
       
 69983 **    May you do good and not evil.
       
 69984 **    May you find forgiveness for yourself and forgive others.
       
 69985 **    May you share freely, never taking more than you give.
       
 69986 **
       
 69987 *************************************************************************
       
 69988 ** This file contains C code routines that are called by the parser
       
 69989 ** in order to generate code for DELETE FROM statements.
       
 69990 **
       
 69991 ** $Id: delete.c,v 1.207 2009/08/08 18:01:08 drh Exp $
       
 69992 */
       
 69993 
       
 69994 /*
       
 69995 ** Look up every table that is named in pSrc.  If any table is not found,
       
 69996 ** add an error message to pParse->zErrMsg and return NULL.  If all tables
       
 69997 ** are found, return a pointer to the last table.
       
 69998 */
       
 69999 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
       
 70000   struct SrcList_item *pItem = pSrc->a;
       
 70001   Table *pTab;
       
 70002   assert( pItem && pSrc->nSrc==1 );
       
 70003   pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
       
 70004   sqlite3DeleteTable(pItem->pTab);
       
 70005   pItem->pTab = pTab;
       
 70006   if( pTab ){
       
 70007     pTab->nRef++;
       
 70008   }
       
 70009   if( sqlite3IndexedByLookup(pParse, pItem) ){
       
 70010     pTab = 0;
       
 70011   }
       
 70012   return pTab;
       
 70013 }
       
 70014 
       
 70015 /*
       
 70016 ** Check to make sure the given table is writable.  If it is not
       
 70017 ** writable, generate an error message and return 1.  If it is
       
 70018 ** writable return 0;
       
 70019 */
       
 70020 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
       
 70021   /* A table is not writable under the following circumstances:
       
 70022   **
       
 70023   **   1) It is a virtual table and no implementation of the xUpdate method
       
 70024   **      has been provided, or
       
 70025   **   2) It is a system table (i.e. sqlite_master), this call is not
       
 70026   **      part of a nested parse and writable_schema pragma has not 
       
 70027   **      been specified.
       
 70028   **
       
 70029   ** In either case leave an error message in pParse and return non-zero.
       
 70030   */
       
 70031   if( ( IsVirtual(pTab) 
       
 70032      && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
       
 70033    || ( (pTab->tabFlags & TF_Readonly)!=0
       
 70034      && (pParse->db->flags & SQLITE_WriteSchema)==0
       
 70035      && pParse->nested==0 )
       
 70036   ){
       
 70037     sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
       
 70038     return 1;
       
 70039   }
       
 70040 
       
 70041 #ifndef SQLITE_OMIT_VIEW
       
 70042   if( !viewOk && pTab->pSelect ){
       
 70043     sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
       
 70044     return 1;
       
 70045   }
       
 70046 #endif
       
 70047   return 0;
       
 70048 }
       
 70049 
       
 70050 
       
 70051 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
       
 70052 /*
       
 70053 ** Evaluate a view and store its result in an ephemeral table.  The
       
 70054 ** pWhere argument is an optional WHERE clause that restricts the
       
 70055 ** set of rows in the view that are to be added to the ephemeral table.
       
 70056 */
       
 70057 SQLITE_PRIVATE void sqlite3MaterializeView(
       
 70058   Parse *pParse,       /* Parsing context */
       
 70059   Table *pView,        /* View definition */
       
 70060   Expr *pWhere,        /* Optional WHERE clause to be added */
       
 70061   int iCur             /* Cursor number for ephemerial table */
       
 70062 ){
       
 70063   SelectDest dest;
       
 70064   Select *pDup;
       
 70065   sqlite3 *db = pParse->db;
       
 70066 
       
 70067   pDup = sqlite3SelectDup(db, pView->pSelect, 0);
       
 70068   if( pWhere ){
       
 70069     SrcList *pFrom;
       
 70070     
       
 70071     pWhere = sqlite3ExprDup(db, pWhere, 0);
       
 70072     pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
       
 70073     if( pFrom ){
       
 70074       assert( pFrom->nSrc==1 );
       
 70075       pFrom->a[0].zAlias = sqlite3DbStrDup(db, pView->zName);
       
 70076       pFrom->a[0].pSelect = pDup;
       
 70077       assert( pFrom->a[0].pOn==0 );
       
 70078       assert( pFrom->a[0].pUsing==0 );
       
 70079     }else{
       
 70080       sqlite3SelectDelete(db, pDup);
       
 70081     }
       
 70082     pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
       
 70083   }
       
 70084   sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
       
 70085   sqlite3Select(pParse, pDup, &dest);
       
 70086   sqlite3SelectDelete(db, pDup);
       
 70087 }
       
 70088 #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
       
 70089 
       
 70090 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
       
 70091 /*
       
 70092 ** Generate an expression tree to implement the WHERE, ORDER BY,
       
 70093 ** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
       
 70094 **
       
 70095 **     DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
       
 70096 **                            \__________________________/
       
 70097 **                               pLimitWhere (pInClause)
       
 70098 */
       
 70099 SQLITE_PRIVATE Expr *sqlite3LimitWhere(
       
 70100   Parse *pParse,               /* The parser context */
       
 70101   SrcList *pSrc,               /* the FROM clause -- which tables to scan */
       
 70102   Expr *pWhere,                /* The WHERE clause.  May be null */
       
 70103   ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
       
 70104   Expr *pLimit,                /* The LIMIT clause.  May be null */
       
 70105   Expr *pOffset,               /* The OFFSET clause.  May be null */
       
 70106   char *zStmtType              /* Either DELETE or UPDATE.  For error messages. */
       
 70107 ){
       
 70108   Expr *pWhereRowid = NULL;    /* WHERE rowid .. */
       
 70109   Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
       
 70110   Expr *pSelectRowid = NULL;   /* SELECT rowid ... */
       
 70111   ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
       
 70112   SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
       
 70113   Select *pSelect = NULL;      /* Complete SELECT tree */
       
 70114 
       
 70115   /* Check that there isn't an ORDER BY without a LIMIT clause.
       
 70116   */
       
 70117   if( pOrderBy && (pLimit == 0) ) {
       
 70118     sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
       
 70119     pParse->parseError = 1;
       
 70120     goto limit_where_cleanup_2;
       
 70121   }
       
 70122 
       
 70123   /* We only need to generate a select expression if there
       
 70124   ** is a limit/offset term to enforce.
       
 70125   */
       
 70126   if( pLimit == 0 ) {
       
 70127     /* if pLimit is null, pOffset will always be null as well. */
       
 70128     assert( pOffset == 0 );
       
 70129     return pWhere;
       
 70130   }
       
 70131 
       
 70132   /* Generate a select expression tree to enforce the limit/offset 
       
 70133   ** term for the DELETE or UPDATE statement.  For example:
       
 70134   **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
       
 70135   ** becomes:
       
 70136   **   DELETE FROM table_a WHERE rowid IN ( 
       
 70137   **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
       
 70138   **   );
       
 70139   */
       
 70140 
       
 70141   pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
       
 70142   if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
       
 70143   pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
       
 70144   if( pEList == 0 ) goto limit_where_cleanup_2;
       
 70145 
       
 70146   /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
       
 70147   ** and the SELECT subtree. */
       
 70148   pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
       
 70149   if( pSelectSrc == 0 ) {
       
 70150     sqlite3ExprListDelete(pParse->db, pEList);
       
 70151     goto limit_where_cleanup_2;
       
 70152   }
       
 70153 
       
 70154   /* generate the SELECT expression tree. */
       
 70155   pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
       
 70156                              pOrderBy,0,pLimit,pOffset);
       
 70157   if( pSelect == 0 ) return 0;
       
 70158 
       
 70159   /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
       
 70160   pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
       
 70161   if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
       
 70162   pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
       
 70163   if( pInClause == 0 ) goto limit_where_cleanup_1;
       
 70164 
       
 70165   pInClause->x.pSelect = pSelect;
       
 70166   pInClause->flags |= EP_xIsSelect;
       
 70167   sqlite3ExprSetHeight(pParse, pInClause);
       
 70168   return pInClause;
       
 70169 
       
 70170   /* something went wrong. clean up anything allocated. */
       
 70171 limit_where_cleanup_1:
       
 70172   sqlite3SelectDelete(pParse->db, pSelect);
       
 70173   return 0;
       
 70174 
       
 70175 limit_where_cleanup_2:
       
 70176   sqlite3ExprDelete(pParse->db, pWhere);
       
 70177   sqlite3ExprListDelete(pParse->db, pOrderBy);
       
 70178   sqlite3ExprDelete(pParse->db, pLimit);
       
 70179   sqlite3ExprDelete(pParse->db, pOffset);
       
 70180   return 0;
       
 70181 }
       
 70182 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
       
 70183 
       
 70184 /*
       
 70185 ** Generate code for a DELETE FROM statement.
       
 70186 **
       
 70187 **     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
       
 70188 **                 \________/       \________________/
       
 70189 **                  pTabList              pWhere
       
 70190 */
       
 70191 SQLITE_PRIVATE void sqlite3DeleteFrom(
       
 70192   Parse *pParse,         /* The parser context */
       
 70193   SrcList *pTabList,     /* The table from which we should delete things */
       
 70194   Expr *pWhere           /* The WHERE clause.  May be null */
       
 70195 ){
       
 70196   Vdbe *v;               /* The virtual database engine */
       
 70197   Table *pTab;           /* The table from which records will be deleted */
       
 70198   const char *zDb;       /* Name of database holding pTab */
       
 70199   int end, addr = 0;     /* A couple addresses of generated code */
       
 70200   int i;                 /* Loop counter */
       
 70201   WhereInfo *pWInfo;     /* Information about the WHERE clause */
       
 70202   Index *pIdx;           /* For looping over indices of the table */
       
 70203   int iCur;              /* VDBE Cursor number for pTab */
       
 70204   sqlite3 *db;           /* Main database structure */
       
 70205   AuthContext sContext;  /* Authorization context */
       
 70206   NameContext sNC;       /* Name context to resolve expressions in */
       
 70207   int iDb;               /* Database number */
       
 70208   int memCnt = -1;       /* Memory cell used for change counting */
       
 70209   int rcauth;            /* Value returned by authorization callback */
       
 70210 
       
 70211 #ifndef SQLITE_OMIT_TRIGGER
       
 70212   int isView;                  /* True if attempting to delete from a view */
       
 70213   Trigger *pTrigger;           /* List of table triggers, if required */
       
 70214 #endif
       
 70215 
       
 70216   memset(&sContext, 0, sizeof(sContext));
       
 70217   db = pParse->db;
       
 70218   if( pParse->nErr || db->mallocFailed ){
       
 70219     goto delete_from_cleanup;
       
 70220   }
       
 70221   assert( pTabList->nSrc==1 );
       
 70222 
       
 70223   /* Locate the table which we want to delete.  This table has to be
       
 70224   ** put in an SrcList structure because some of the subroutines we
       
 70225   ** will be calling are designed to work with multiple tables and expect
       
 70226   ** an SrcList* parameter instead of just a Table* parameter.
       
 70227   */
       
 70228   pTab = sqlite3SrcListLookup(pParse, pTabList);
       
 70229   if( pTab==0 )  goto delete_from_cleanup;
       
 70230 
       
 70231   /* Figure out if we have any triggers and if the table being
       
 70232   ** deleted from is a view
       
 70233   */
       
 70234 #ifndef SQLITE_OMIT_TRIGGER
       
 70235   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
       
 70236   isView = pTab->pSelect!=0;
       
 70237 #else
       
 70238 # define pTrigger 0
       
 70239 # define isView 0
       
 70240 #endif
       
 70241 #ifdef SQLITE_OMIT_VIEW
       
 70242 # undef isView
       
 70243 # define isView 0
       
 70244 #endif
       
 70245 
       
 70246   /* If pTab is really a view, make sure it has been initialized.
       
 70247   */
       
 70248   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
       
 70249     goto delete_from_cleanup;
       
 70250   }
       
 70251 
       
 70252   if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
       
 70253     goto delete_from_cleanup;
       
 70254   }
       
 70255   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       
 70256   assert( iDb<db->nDb );
       
 70257   zDb = db->aDb[iDb].zName;
       
 70258   rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
       
 70259   assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
       
 70260   if( rcauth==SQLITE_DENY ){
       
 70261     goto delete_from_cleanup;
       
 70262   }
       
 70263   assert(!isView || pTrigger);
       
 70264 
       
 70265   /* Assign  cursor number to the table and all its indices.
       
 70266   */
       
 70267   assert( pTabList->nSrc==1 );
       
 70268   iCur = pTabList->a[0].iCursor = pParse->nTab++;
       
 70269   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       
 70270     pParse->nTab++;
       
 70271   }
       
 70272 
       
 70273   /* Start the view context
       
 70274   */
       
 70275   if( isView ){
       
 70276     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
       
 70277   }
       
 70278 
       
 70279   /* Begin generating code.
       
 70280   */
       
 70281   v = sqlite3GetVdbe(pParse);
       
 70282   if( v==0 ){
       
 70283     goto delete_from_cleanup;
       
 70284   }
       
 70285   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
       
 70286   sqlite3BeginWriteOperation(pParse, 1, iDb);
       
 70287 
       
 70288   /* If we are trying to delete from a view, realize that view into
       
 70289   ** a ephemeral table.
       
 70290   */
       
 70291 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
       
 70292   if( isView ){
       
 70293     sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
       
 70294   }
       
 70295 #endif
       
 70296 
       
 70297   /* Resolve the column names in the WHERE clause.
       
 70298   */
       
 70299   memset(&sNC, 0, sizeof(sNC));
       
 70300   sNC.pParse = pParse;
       
 70301   sNC.pSrcList = pTabList;
       
 70302   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
       
 70303     goto delete_from_cleanup;
       
 70304   }
       
 70305 
       
 70306   /* Initialize the counter of the number of rows deleted, if
       
 70307   ** we are counting rows.
       
 70308   */
       
 70309   if( db->flags & SQLITE_CountRows ){
       
 70310     memCnt = ++pParse->nMem;
       
 70311     sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
       
 70312   }
       
 70313 
       
 70314 #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
       
 70315   /* Special case: A DELETE without a WHERE clause deletes everything.
       
 70316   ** It is easier just to erase the whole table. Prior to version 3.6.5,
       
 70317   ** this optimization caused the row change count (the value returned by 
       
 70318   ** API function sqlite3_count_changes) to be set incorrectly.  */
       
 70319   if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab) 
       
 70320    && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
       
 70321   ){
       
 70322     assert( !isView );
       
 70323     sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
       
 70324                       pTab->zName, P4_STATIC);
       
 70325     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       
 70326       assert( pIdx->pSchema==pTab->pSchema );
       
 70327       sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
       
 70328     }
       
 70329   }else
       
 70330 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
       
 70331   /* The usual case: There is a WHERE clause so we have to scan through
       
 70332   ** the table and pick which records to delete.
       
 70333   */
       
 70334   {
       
 70335     int iRowSet = ++pParse->nMem;   /* Register for rowset of rows to delete */
       
 70336     int iRowid = ++pParse->nMem;    /* Used for storing rowid values. */
       
 70337     int regRowid;                   /* Actual register containing rowids */
       
 70338 
       
 70339     /* Collect rowids of every row to be deleted.
       
 70340     */
       
 70341     sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
       
 70342     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,WHERE_DUPLICATES_OK);
       
 70343     if( pWInfo==0 ) goto delete_from_cleanup;
       
 70344     regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid, 0);
       
 70345     sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
       
 70346     if( db->flags & SQLITE_CountRows ){
       
 70347       sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
       
 70348     }
       
 70349     sqlite3WhereEnd(pWInfo);
       
 70350 
       
 70351     /* Delete every item whose key was written to the list during the
       
 70352     ** database scan.  We have to delete items after the scan is complete
       
 70353     ** because deleting an item can change the scan order.  */
       
 70354     end = sqlite3VdbeMakeLabel(v);
       
 70355 
       
 70356     /* Unless this is a view, open cursors for the table we are 
       
 70357     ** deleting from and all its indices. If this is a view, then the
       
 70358     ** only effect this statement has is to fire the INSTEAD OF 
       
 70359     ** triggers.  */
       
 70360     if( !isView ){
       
 70361       sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite);
       
 70362     }
       
 70363 
       
 70364     addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid);
       
 70365 
       
 70366     /* Delete the row */
       
 70367 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 70368     if( IsVirtual(pTab) ){
       
 70369       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
       
 70370       sqlite3VtabMakeWritable(pParse, pTab);
       
 70371       sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB);
       
 70372       sqlite3MayAbort(pParse);
       
 70373     }else
       
 70374 #endif
       
 70375     {
       
 70376       int count = (pParse->nested==0);    /* True to count changes */
       
 70377       sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, count, pTrigger, OE_Default);
       
 70378     }
       
 70379 
       
 70380     /* End of the delete loop */
       
 70381     sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
       
 70382     sqlite3VdbeResolveLabel(v, end);
       
 70383 
       
 70384     /* Close the cursors open on the table and its indexes. */
       
 70385     if( !isView && !IsVirtual(pTab) ){
       
 70386       for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
       
 70387         sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum);
       
 70388       }
       
 70389       sqlite3VdbeAddOp1(v, OP_Close, iCur);
       
 70390     }
       
 70391   }
       
 70392 
       
 70393   /* Update the sqlite_sequence table by storing the content of the
       
 70394   ** maximum rowid counter values recorded while inserting into
       
 70395   ** autoincrement tables.
       
 70396   */
       
 70397   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
       
 70398     sqlite3AutoincrementEnd(pParse);
       
 70399   }
       
 70400 
       
 70401   /* Return the number of rows that were deleted. If this routine is 
       
 70402   ** generating code because of a call to sqlite3NestedParse(), do not
       
 70403   ** invoke the callback function.
       
 70404   */
       
 70405   if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
       
 70406     sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
       
 70407     sqlite3VdbeSetNumCols(v, 1);
       
 70408     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
       
 70409   }
       
 70410 
       
 70411 delete_from_cleanup:
       
 70412   sqlite3AuthContextPop(&sContext);
       
 70413   sqlite3SrcListDelete(db, pTabList);
       
 70414   sqlite3ExprDelete(db, pWhere);
       
 70415   return;
       
 70416 }
       
 70417 /* Make sure "isView" and other macros defined above are undefined. Otherwise
       
 70418 ** thely may interfere with compilation of other functions in this file
       
 70419 ** (or in another file, if this file becomes part of the amalgamation).  */
       
 70420 #ifdef isView
       
 70421  #undef isView
       
 70422 #endif
       
 70423 #ifdef pTrigger
       
 70424  #undef pTrigger
       
 70425 #endif
       
 70426 
       
 70427 /*
       
 70428 ** This routine generates VDBE code that causes a single row of a
       
 70429 ** single table to be deleted.
       
 70430 **
       
 70431 ** The VDBE must be in a particular state when this routine is called.
       
 70432 ** These are the requirements:
       
 70433 **
       
 70434 **   1.  A read/write cursor pointing to pTab, the table containing the row
       
 70435 **       to be deleted, must be opened as cursor number $iCur.
       
 70436 **
       
 70437 **   2.  Read/write cursors for all indices of pTab must be open as
       
 70438 **       cursor number base+i for the i-th index.
       
 70439 **
       
 70440 **   3.  The record number of the row to be deleted must be stored in
       
 70441 **       memory cell iRowid.
       
 70442 **
       
 70443 ** This routine generates code to remove both the table record and all 
       
 70444 ** index entries that point to that record.
       
 70445 */
       
 70446 SQLITE_PRIVATE void sqlite3GenerateRowDelete(
       
 70447   Parse *pParse,     /* Parsing context */
       
 70448   Table *pTab,       /* Table containing the row to be deleted */
       
 70449   int iCur,          /* Cursor number for the table */
       
 70450   int iRowid,        /* Memory cell that contains the rowid to delete */
       
 70451   int count,         /* If non-zero, increment the row change counter */
       
 70452   Trigger *pTrigger, /* List of triggers to (potentially) fire */
       
 70453   int onconf         /* Default ON CONFLICT policy for triggers */
       
 70454 ){
       
 70455   Vdbe *v = pParse->pVdbe;        /* Vdbe */
       
 70456   int iOld = 0;                   /* First register in OLD.* array */
       
 70457   int iLabel;                     /* Label resolved to end of generated code */
       
 70458 
       
 70459   /* Vdbe is guaranteed to have been allocated by this stage. */
       
 70460   assert( v );
       
 70461 
       
 70462   /* Seek cursor iCur to the row to delete. If this row no longer exists 
       
 70463   ** (this can happen if a trigger program has already deleted it), do
       
 70464   ** not attempt to delete it or fire any DELETE triggers.  */
       
 70465   iLabel = sqlite3VdbeMakeLabel(v);
       
 70466   sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
       
 70467  
       
 70468   /* If there are any triggers to fire, allocate a range of registers to
       
 70469   ** use for the old.* references in the triggers.  */
       
 70470   if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
       
 70471     u32 mask;                     /* Mask of OLD.* columns in use */
       
 70472     int iCol;                     /* Iterator used while populating OLD.* */
       
 70473 
       
 70474     /* TODO: Could use temporary registers here. Also could attempt to
       
 70475     ** avoid copying the contents of the rowid register.  */
       
 70476     mask = sqlite3TriggerOldmask(pParse, pTrigger, 0, pTab, onconf);
       
 70477     mask |= sqlite3FkOldmask(pParse, pTab);
       
 70478     iOld = pParse->nMem+1;
       
 70479     pParse->nMem += (1 + pTab->nCol);
       
 70480 
       
 70481     /* Populate the OLD.* pseudo-table register array. These values will be 
       
 70482     ** used by any BEFORE and AFTER triggers that exist.  */
       
 70483     sqlite3VdbeAddOp2(v, OP_Copy, iRowid, iOld);
       
 70484     for(iCol=0; iCol<pTab->nCol; iCol++){
       
 70485       if( mask==0xffffffff || mask&(1<<iCol) ){
       
 70486         int iTarget = iOld + iCol + 1;
       
 70487         sqlite3VdbeAddOp3(v, OP_Column, iCur, iCol, iTarget);
       
 70488         sqlite3ColumnDefault(v, pTab, iCol, iTarget);
       
 70489       }
       
 70490     }
       
 70491 
       
 70492     /* Invoke BEFORE DELETE trigger programs. */
       
 70493     sqlite3CodeRowTrigger(pParse, pTrigger, 
       
 70494         TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
       
 70495     );
       
 70496 
       
 70497     /* Seek the cursor to the row to be deleted again. It may be that
       
 70498     ** the BEFORE triggers coded above have already removed the row
       
 70499     ** being deleted. Do not attempt to delete the row a second time, and 
       
 70500     ** do not fire AFTER triggers.  */
       
 70501     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
       
 70502 
       
 70503     /* Do FK processing. This call checks that any FK constraints that
       
 70504     ** refer to this table (i.e. constraints attached to other tables) 
       
 70505     ** are not violated by deleting this row.  */
       
 70506     sqlite3FkCheck(pParse, pTab, iOld, 0);
       
 70507   }
       
 70508 
       
 70509   /* Delete the index and table entries. Skip this step if pTab is really
       
 70510   ** a view (in which case the only effect of the DELETE statement is to
       
 70511   ** fire the INSTEAD OF triggers).  */ 
       
 70512   if( pTab->pSelect==0 ){
       
 70513     sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0);
       
 70514     sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
       
 70515     if( count ){
       
 70516       sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC);
       
 70517     }
       
 70518   }
       
 70519 
       
 70520   /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
       
 70521   ** handle rows (possibly in other tables) that refer via a foreign key
       
 70522   ** to the row just deleted. */ 
       
 70523   sqlite3FkActions(pParse, pTab, 0, iOld);
       
 70524 
       
 70525   /* Invoke AFTER DELETE trigger programs. */
       
 70526   sqlite3CodeRowTrigger(pParse, pTrigger, 
       
 70527       TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
       
 70528   );
       
 70529 
       
 70530   /* Jump here if the row had already been deleted before any BEFORE
       
 70531   ** trigger programs were invoked. Or if a trigger program throws a 
       
 70532   ** RAISE(IGNORE) exception.  */
       
 70533   sqlite3VdbeResolveLabel(v, iLabel);
       
 70534 }
       
 70535 
       
 70536 /*
       
 70537 ** This routine generates VDBE code that causes the deletion of all
       
 70538 ** index entries associated with a single row of a single table.
       
 70539 **
       
 70540 ** The VDBE must be in a particular state when this routine is called.
       
 70541 ** These are the requirements:
       
 70542 **
       
 70543 **   1.  A read/write cursor pointing to pTab, the table containing the row
       
 70544 **       to be deleted, must be opened as cursor number "iCur".
       
 70545 **
       
 70546 **   2.  Read/write cursors for all indices of pTab must be open as
       
 70547 **       cursor number iCur+i for the i-th index.
       
 70548 **
       
 70549 **   3.  The "iCur" cursor must be pointing to the row that is to be
       
 70550 **       deleted.
       
 70551 */
       
 70552 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
       
 70553   Parse *pParse,     /* Parsing and code generating context */
       
 70554   Table *pTab,       /* Table containing the row to be deleted */
       
 70555   int iCur,          /* Cursor number for the table */
       
 70556   int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
       
 70557 ){
       
 70558   int i;
       
 70559   Index *pIdx;
       
 70560   int r1;
       
 70561 
       
 70562   for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
       
 70563     if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue;
       
 70564     r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0);
       
 70565     sqlite3VdbeAddOp3(pParse->pVdbe, OP_IdxDelete, iCur+i, r1,pIdx->nColumn+1);
       
 70566   }
       
 70567 }
       
 70568 
       
 70569 /*
       
 70570 ** Generate code that will assemble an index key and put it in register
       
 70571 ** regOut.  The key with be for index pIdx which is an index on pTab.
       
 70572 ** iCur is the index of a cursor open on the pTab table and pointing to
       
 70573 ** the entry that needs indexing.
       
 70574 **
       
 70575 ** Return a register number which is the first in a block of
       
 70576 ** registers that holds the elements of the index key.  The
       
 70577 ** block of registers has already been deallocated by the time
       
 70578 ** this routine returns.
       
 70579 */
       
 70580 SQLITE_PRIVATE int sqlite3GenerateIndexKey(
       
 70581   Parse *pParse,     /* Parsing context */
       
 70582   Index *pIdx,       /* The index for which to generate a key */
       
 70583   int iCur,          /* Cursor number for the pIdx->pTable table */
       
 70584   int regOut,        /* Write the new index key to this register */
       
 70585   int doMakeRec      /* Run the OP_MakeRecord instruction if true */
       
 70586 ){
       
 70587   Vdbe *v = pParse->pVdbe;
       
 70588   int j;
       
 70589   Table *pTab = pIdx->pTable;
       
 70590   int regBase;
       
 70591   int nCol;
       
 70592 
       
 70593   nCol = pIdx->nColumn;
       
 70594   regBase = sqlite3GetTempRange(pParse, nCol+1);
       
 70595   sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol);
       
 70596   for(j=0; j<nCol; j++){
       
 70597     int idx = pIdx->aiColumn[j];
       
 70598     if( idx==pTab->iPKey ){
       
 70599       sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j);
       
 70600     }else{
       
 70601       sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j);
       
 70602       sqlite3ColumnDefault(v, pTab, idx, -1);
       
 70603     }
       
 70604   }
       
 70605   if( doMakeRec ){
       
 70606     sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
       
 70607     sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0);
       
 70608     sqlite3ExprCacheAffinityChange(pParse, regBase, nCol+1);
       
 70609   }
       
 70610   sqlite3ReleaseTempRange(pParse, regBase, nCol+1);
       
 70611   return regBase;
       
 70612 }
       
 70613 
       
 70614 
       
 70615 /************** End of delete.c **********************************************/
       
 70616 /************** Begin file func.c ********************************************/
       
 70617 /*
       
 70618 ** 2002 February 23
       
 70619 **
       
 70620 ** The author disclaims copyright to this source code.  In place of
       
 70621 ** a legal notice, here is a blessing:
       
 70622 **
       
 70623 **    May you do good and not evil.
       
 70624 **    May you find forgiveness for yourself and forgive others.
       
 70625 **    May you share freely, never taking more than you give.
       
 70626 **
       
 70627 *************************************************************************
       
 70628 ** This file contains the C functions that implement various SQL
       
 70629 ** functions of SQLite.  
       
 70630 **
       
 70631 ** There is only one exported symbol in this file - the function
       
 70632 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
       
 70633 ** All other code has file scope.
       
 70634 */
       
 70635 
       
 70636 /*
       
 70637 ** Return the collating function associated with a function.
       
 70638 */
       
 70639 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
       
 70640   return context->pColl;
       
 70641 }
       
 70642 
       
 70643 /*
       
 70644 ** Implementation of the non-aggregate min() and max() functions
       
 70645 */
       
 70646 static void minmaxFunc(
       
 70647   sqlite3_context *context,
       
 70648   int argc,
       
 70649   sqlite3_value **argv
       
 70650 ){
       
 70651   int i;
       
 70652   int mask;    /* 0 for min() or 0xffffffff for max() */
       
 70653   int iBest;
       
 70654   CollSeq *pColl;
       
 70655 
       
 70656   assert( argc>1 );
       
 70657   mask = sqlite3_user_data(context)==0 ? 0 : -1;
       
 70658   pColl = sqlite3GetFuncCollSeq(context);
       
 70659   assert( pColl );
       
 70660   assert( mask==-1 || mask==0 );
       
 70661   iBest = 0;
       
 70662   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
       
 70663   for(i=1; i<argc; i++){
       
 70664     if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
       
 70665     if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
       
 70666       testcase( mask==0 );
       
 70667       iBest = i;
       
 70668     }
       
 70669   }
       
 70670   sqlite3_result_value(context, argv[iBest]);
       
 70671 }
       
 70672 
       
 70673 /*
       
 70674 ** Return the type of the argument.
       
 70675 */
       
 70676 static void typeofFunc(
       
 70677   sqlite3_context *context,
       
 70678   int NotUsed,
       
 70679   sqlite3_value **argv
       
 70680 ){
       
 70681   const char *z = 0;
       
 70682   UNUSED_PARAMETER(NotUsed);
       
 70683   switch( sqlite3_value_type(argv[0]) ){
       
 70684     case SQLITE_INTEGER: z = "integer"; break;
       
 70685     case SQLITE_TEXT:    z = "text";    break;
       
 70686     case SQLITE_FLOAT:   z = "real";    break;
       
 70687     case SQLITE_BLOB:    z = "blob";    break;
       
 70688     default:             z = "null";    break;
       
 70689   }
       
 70690   sqlite3_result_text(context, z, -1, SQLITE_STATIC);
       
 70691 }
       
 70692 
       
 70693 
       
 70694 /*
       
 70695 ** Implementation of the length() function
       
 70696 */
       
 70697 static void lengthFunc(
       
 70698   sqlite3_context *context,
       
 70699   int argc,
       
 70700   sqlite3_value **argv
       
 70701 ){
       
 70702   int len;
       
 70703 
       
 70704   assert( argc==1 );
       
 70705   UNUSED_PARAMETER(argc);
       
 70706   switch( sqlite3_value_type(argv[0]) ){
       
 70707     case SQLITE_BLOB:
       
 70708     case SQLITE_INTEGER:
       
 70709     case SQLITE_FLOAT: {
       
 70710       sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
       
 70711       break;
       
 70712     }
       
 70713     case SQLITE_TEXT: {
       
 70714       const unsigned char *z = sqlite3_value_text(argv[0]);
       
 70715       if( z==0 ) return;
       
 70716       len = 0;
       
 70717       while( *z ){
       
 70718         len++;
       
 70719         SQLITE_SKIP_UTF8(z);
       
 70720       }
       
 70721       sqlite3_result_int(context, len);
       
 70722       break;
       
 70723     }
       
 70724     default: {
       
 70725       sqlite3_result_null(context);
       
 70726       break;
       
 70727     }
       
 70728   }
       
 70729 }
       
 70730 
       
 70731 /*
       
 70732 ** Implementation of the abs() function
       
 70733 */
       
 70734 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
       
 70735   assert( argc==1 );
       
 70736   UNUSED_PARAMETER(argc);
       
 70737   switch( sqlite3_value_type(argv[0]) ){
       
 70738     case SQLITE_INTEGER: {
       
 70739       i64 iVal = sqlite3_value_int64(argv[0]);
       
 70740       if( iVal<0 ){
       
 70741         if( (iVal<<1)==0 ){
       
 70742           sqlite3_result_error(context, "integer overflow", -1);
       
 70743           return;
       
 70744         }
       
 70745         iVal = -iVal;
       
 70746       } 
       
 70747       sqlite3_result_int64(context, iVal);
       
 70748       break;
       
 70749     }
       
 70750     case SQLITE_NULL: {
       
 70751       sqlite3_result_null(context);
       
 70752       break;
       
 70753     }
       
 70754     default: {
       
 70755       double rVal = sqlite3_value_double(argv[0]);
       
 70756       if( rVal<0 ) rVal = -rVal;
       
 70757       sqlite3_result_double(context, rVal);
       
 70758       break;
       
 70759     }
       
 70760   }
       
 70761 }
       
 70762 
       
 70763 /*
       
 70764 ** Implementation of the substr() function.
       
 70765 **
       
 70766 ** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
       
 70767 ** p1 is 1-indexed.  So substr(x,1,1) returns the first character
       
 70768 ** of x.  If x is text, then we actually count UTF-8 characters.
       
 70769 ** If x is a blob, then we count bytes.
       
 70770 **
       
 70771 ** If p1 is negative, then we begin abs(p1) from the end of x[].
       
 70772 */
       
 70773 static void substrFunc(
       
 70774   sqlite3_context *context,
       
 70775   int argc,
       
 70776   sqlite3_value **argv
       
 70777 ){
       
 70778   const unsigned char *z;
       
 70779   const unsigned char *z2;
       
 70780   int len;
       
 70781   int p0type;
       
 70782   i64 p1, p2;
       
 70783   int negP2 = 0;
       
 70784 
       
 70785   assert( argc==3 || argc==2 );
       
 70786   if( sqlite3_value_type(argv[1])==SQLITE_NULL
       
 70787    || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
       
 70788   ){
       
 70789     return;
       
 70790   }
       
 70791   p0type = sqlite3_value_type(argv[0]);
       
 70792   if( p0type==SQLITE_BLOB ){
       
 70793     len = sqlite3_value_bytes(argv[0]);
       
 70794     z = sqlite3_value_blob(argv[0]);
       
 70795     if( z==0 ) return;
       
 70796     assert( len==sqlite3_value_bytes(argv[0]) );
       
 70797   }else{
       
 70798     z = sqlite3_value_text(argv[0]);
       
 70799     if( z==0 ) return;
       
 70800     len = 0;
       
 70801     for(z2=z; *z2; len++){
       
 70802       SQLITE_SKIP_UTF8(z2);
       
 70803     }
       
 70804   }
       
 70805   p1 = sqlite3_value_int(argv[1]);
       
 70806   if( argc==3 ){
       
 70807     p2 = sqlite3_value_int(argv[2]);
       
 70808     if( p2<0 ){
       
 70809       p2 = -p2;
       
 70810       negP2 = 1;
       
 70811     }
       
 70812   }else{
       
 70813     p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
       
 70814   }
       
 70815   if( p1<0 ){
       
 70816     p1 += len;
       
 70817     if( p1<0 ){
       
 70818       p2 += p1;
       
 70819       if( p2<0 ) p2 = 0;
       
 70820       p1 = 0;
       
 70821     }
       
 70822   }else if( p1>0 ){
       
 70823     p1--;
       
 70824   }else if( p2>0 ){
       
 70825     p2--;
       
 70826   }
       
 70827   if( negP2 ){
       
 70828     p1 -= p2;
       
 70829     if( p1<0 ){
       
 70830       p2 += p1;
       
 70831       p1 = 0;
       
 70832     }
       
 70833   }
       
 70834   assert( p1>=0 && p2>=0 );
       
 70835   if( p1+p2>len ){
       
 70836     p2 = len-p1;
       
 70837     if( p2<0 ) p2 = 0;
       
 70838   }
       
 70839   if( p0type!=SQLITE_BLOB ){
       
 70840     while( *z && p1 ){
       
 70841       SQLITE_SKIP_UTF8(z);
       
 70842       p1--;
       
 70843     }
       
 70844     for(z2=z; *z2 && p2; p2--){
       
 70845       SQLITE_SKIP_UTF8(z2);
       
 70846     }
       
 70847     sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT);
       
 70848   }else{
       
 70849     sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT);
       
 70850   }
       
 70851 }
       
 70852 
       
 70853 /*
       
 70854 ** Implementation of the round() function
       
 70855 */
       
 70856 #ifndef SQLITE_OMIT_FLOATING_POINT
       
 70857 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
       
 70858   int n = 0;
       
 70859   double r;
       
 70860   char *zBuf;
       
 70861   assert( argc==1 || argc==2 );
       
 70862   if( argc==2 ){
       
 70863     if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
       
 70864     n = sqlite3_value_int(argv[1]);
       
 70865     if( n>30 ) n = 30;
       
 70866     if( n<0 ) n = 0;
       
 70867   }
       
 70868   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
       
 70869   r = sqlite3_value_double(argv[0]);
       
 70870   zBuf = sqlite3_mprintf("%.*f",n,r);
       
 70871   if( zBuf==0 ){
       
 70872     sqlite3_result_error_nomem(context);
       
 70873   }else{
       
 70874     sqlite3AtoF(zBuf, &r);
       
 70875     sqlite3_free(zBuf);
       
 70876     sqlite3_result_double(context, r);
       
 70877   }
       
 70878 }
       
 70879 #endif
       
 70880 
       
 70881 /*
       
 70882 ** Allocate nByte bytes of space using sqlite3_malloc(). If the
       
 70883 ** allocation fails, call sqlite3_result_error_nomem() to notify
       
 70884 ** the database handle that malloc() has failed and return NULL.
       
 70885 ** If nByte is larger than the maximum string or blob length, then
       
 70886 ** raise an SQLITE_TOOBIG exception and return NULL.
       
 70887 */
       
 70888 static void *contextMalloc(sqlite3_context *context, i64 nByte){
       
 70889   char *z;
       
 70890   sqlite3 *db = sqlite3_context_db_handle(context);
       
 70891   assert( nByte>0 );
       
 70892   testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
       
 70893   testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
       
 70894   if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
       
 70895     sqlite3_result_error_toobig(context);
       
 70896     z = 0;
       
 70897   }else{
       
 70898     z = sqlite3Malloc((int)nByte);
       
 70899     if( !z ){
       
 70900       sqlite3_result_error_nomem(context);
       
 70901     }
       
 70902   }
       
 70903   return z;
       
 70904 }
       
 70905 
       
 70906 /*
       
 70907 ** Implementation of the upper() and lower() SQL functions.
       
 70908 */
       
 70909 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
       
 70910   char *z1;
       
 70911   const char *z2;
       
 70912   int i, n;
       
 70913   UNUSED_PARAMETER(argc);
       
 70914   z2 = (char*)sqlite3_value_text(argv[0]);
       
 70915   n = sqlite3_value_bytes(argv[0]);
       
 70916   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
       
 70917   assert( z2==(char*)sqlite3_value_text(argv[0]) );
       
 70918   if( z2 ){
       
 70919     z1 = contextMalloc(context, ((i64)n)+1);
       
 70920     if( z1 ){
       
 70921       memcpy(z1, z2, n+1);
       
 70922       for(i=0; z1[i]; i++){
       
 70923         z1[i] = (char)sqlite3Toupper(z1[i]);
       
 70924       }
       
 70925       sqlite3_result_text(context, z1, -1, sqlite3_free);
       
 70926     }
       
 70927   }
       
 70928 }
       
 70929 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
       
 70930   u8 *z1;
       
 70931   const char *z2;
       
 70932   int i, n;
       
 70933   UNUSED_PARAMETER(argc);
       
 70934   z2 = (char*)sqlite3_value_text(argv[0]);
       
 70935   n = sqlite3_value_bytes(argv[0]);
       
 70936   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
       
 70937   assert( z2==(char*)sqlite3_value_text(argv[0]) );
       
 70938   if( z2 ){
       
 70939     z1 = contextMalloc(context, ((i64)n)+1);
       
 70940     if( z1 ){
       
 70941       memcpy(z1, z2, n+1);
       
 70942       for(i=0; z1[i]; i++){
       
 70943         z1[i] = sqlite3Tolower(z1[i]);
       
 70944       }
       
 70945       sqlite3_result_text(context, (char *)z1, -1, sqlite3_free);
       
 70946     }
       
 70947   }
       
 70948 }
       
 70949 
       
 70950 /*
       
 70951 ** Implementation of the IFNULL(), NVL(), and COALESCE() functions.  
       
 70952 ** All three do the same thing.  They return the first non-NULL
       
 70953 ** argument.
       
 70954 */
       
 70955 static void ifnullFunc(
       
 70956   sqlite3_context *context,
       
 70957   int argc,
       
 70958   sqlite3_value **argv
       
 70959 ){
       
 70960   int i;
       
 70961   for(i=0; i<argc; i++){
       
 70962     if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
       
 70963       sqlite3_result_value(context, argv[i]);
       
 70964       break;
       
 70965     }
       
 70966   }
       
 70967 }
       
 70968 
       
 70969 /*
       
 70970 ** Implementation of random().  Return a random integer.  
       
 70971 */
       
 70972 static void randomFunc(
       
 70973   sqlite3_context *context,
       
 70974   int NotUsed,
       
 70975   sqlite3_value **NotUsed2
       
 70976 ){
       
 70977   sqlite_int64 r;
       
 70978   UNUSED_PARAMETER2(NotUsed, NotUsed2);
       
 70979   sqlite3_randomness(sizeof(r), &r);
       
 70980   if( r<0 ){
       
 70981     /* We need to prevent a random number of 0x8000000000000000 
       
 70982     ** (or -9223372036854775808) since when you do abs() of that
       
 70983     ** number of you get the same value back again.  To do this
       
 70984     ** in a way that is testable, mask the sign bit off of negative
       
 70985     ** values, resulting in a positive value.  Then take the 
       
 70986     ** 2s complement of that positive value.  The end result can
       
 70987     ** therefore be no less than -9223372036854775807.
       
 70988     */
       
 70989     r = -(r ^ (((sqlite3_int64)1)<<63));
       
 70990   }
       
 70991   sqlite3_result_int64(context, r);
       
 70992 }
       
 70993 
       
 70994 /*
       
 70995 ** Implementation of randomblob(N).  Return a random blob
       
 70996 ** that is N bytes long.
       
 70997 */
       
 70998 static void randomBlob(
       
 70999   sqlite3_context *context,
       
 71000   int argc,
       
 71001   sqlite3_value **argv
       
 71002 ){
       
 71003   int n;
       
 71004   unsigned char *p;
       
 71005   assert( argc==1 );
       
 71006   UNUSED_PARAMETER(argc);
       
 71007   n = sqlite3_value_int(argv[0]);
       
 71008   if( n<1 ){
       
 71009     n = 1;
       
 71010   }
       
 71011   p = contextMalloc(context, n);
       
 71012   if( p ){
       
 71013     sqlite3_randomness(n, p);
       
 71014     sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
       
 71015   }
       
 71016 }
       
 71017 
       
 71018 /*
       
 71019 ** Implementation of the last_insert_rowid() SQL function.  The return
       
 71020 ** value is the same as the sqlite3_last_insert_rowid() API function.
       
 71021 */
       
 71022 static void last_insert_rowid(
       
 71023   sqlite3_context *context, 
       
 71024   int NotUsed, 
       
 71025   sqlite3_value **NotUsed2
       
 71026 ){
       
 71027   sqlite3 *db = sqlite3_context_db_handle(context);
       
 71028   UNUSED_PARAMETER2(NotUsed, NotUsed2);
       
 71029   sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
       
 71030 }
       
 71031 
       
 71032 /*
       
 71033 ** Implementation of the changes() SQL function.  The return value is the
       
 71034 ** same as the sqlite3_changes() API function.
       
 71035 */
       
 71036 static void changes(
       
 71037   sqlite3_context *context,
       
 71038   int NotUsed,
       
 71039   sqlite3_value **NotUsed2
       
 71040 ){
       
 71041   sqlite3 *db = sqlite3_context_db_handle(context);
       
 71042   UNUSED_PARAMETER2(NotUsed, NotUsed2);
       
 71043   sqlite3_result_int(context, sqlite3_changes(db));
       
 71044 }
       
 71045 
       
 71046 /*
       
 71047 ** Implementation of the total_changes() SQL function.  The return value is
       
 71048 ** the same as the sqlite3_total_changes() API function.
       
 71049 */
       
 71050 static void total_changes(
       
 71051   sqlite3_context *context,
       
 71052   int NotUsed,
       
 71053   sqlite3_value **NotUsed2
       
 71054 ){
       
 71055   sqlite3 *db = sqlite3_context_db_handle(context);
       
 71056   UNUSED_PARAMETER2(NotUsed, NotUsed2);
       
 71057   sqlite3_result_int(context, sqlite3_total_changes(db));
       
 71058 }
       
 71059 
       
 71060 /*
       
 71061 ** A structure defining how to do GLOB-style comparisons.
       
 71062 */
       
 71063 struct compareInfo {
       
 71064   u8 matchAll;
       
 71065   u8 matchOne;
       
 71066   u8 matchSet;
       
 71067   u8 noCase;
       
 71068 };
       
 71069 
       
 71070 /*
       
 71071 ** For LIKE and GLOB matching on EBCDIC machines, assume that every
       
 71072 ** character is exactly one byte in size.  Also, all characters are
       
 71073 ** able to participate in upper-case-to-lower-case mappings in EBCDIC
       
 71074 ** whereas only characters less than 0x80 do in ASCII.
       
 71075 */
       
 71076 #if defined(SQLITE_EBCDIC)
       
 71077 # define sqlite3Utf8Read(A,C)    (*(A++))
       
 71078 # define GlogUpperToLower(A)     A = sqlite3UpperToLower[A]
       
 71079 #else
       
 71080 # define GlogUpperToLower(A)     if( A<0x80 ){ A = sqlite3UpperToLower[A]; }
       
 71081 #endif
       
 71082 
       
 71083 static const struct compareInfo globInfo = { '*', '?', '[', 0 };
       
 71084 /* The correct SQL-92 behavior is for the LIKE operator to ignore
       
 71085 ** case.  Thus  'a' LIKE 'A' would be true. */
       
 71086 static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
       
 71087 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
       
 71088 ** is case sensitive causing 'a' LIKE 'A' to be false */
       
 71089 static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
       
 71090 
       
 71091 /*
       
 71092 ** Compare two UTF-8 strings for equality where the first string can
       
 71093 ** potentially be a "glob" expression.  Return true (1) if they
       
 71094 ** are the same and false (0) if they are different.
       
 71095 **
       
 71096 ** Globbing rules:
       
 71097 **
       
 71098 **      '*'       Matches any sequence of zero or more characters.
       
 71099 **
       
 71100 **      '?'       Matches exactly one character.
       
 71101 **
       
 71102 **     [...]      Matches one character from the enclosed list of
       
 71103 **                characters.
       
 71104 **
       
 71105 **     [^...]     Matches one character not in the enclosed list.
       
 71106 **
       
 71107 ** With the [...] and [^...] matching, a ']' character can be included
       
 71108 ** in the list by making it the first character after '[' or '^'.  A
       
 71109 ** range of characters can be specified using '-'.  Example:
       
 71110 ** "[a-z]" matches any single lower-case letter.  To match a '-', make
       
 71111 ** it the last character in the list.
       
 71112 **
       
 71113 ** This routine is usually quick, but can be N**2 in the worst case.
       
 71114 **
       
 71115 ** Hints: to match '*' or '?', put them in "[]".  Like this:
       
 71116 **
       
 71117 **         abc[*]xyz        Matches "abc*xyz" only
       
 71118 */
       
 71119 static int patternCompare(
       
 71120   const u8 *zPattern,              /* The glob pattern */
       
 71121   const u8 *zString,               /* The string to compare against the glob */
       
 71122   const struct compareInfo *pInfo, /* Information about how to do the compare */
       
 71123   const int esc                    /* The escape character */
       
 71124 ){
       
 71125   int c, c2;
       
 71126   int invert;
       
 71127   int seen;
       
 71128   u8 matchOne = pInfo->matchOne;
       
 71129   u8 matchAll = pInfo->matchAll;
       
 71130   u8 matchSet = pInfo->matchSet;
       
 71131   u8 noCase = pInfo->noCase; 
       
 71132   int prevEscape = 0;     /* True if the previous character was 'escape' */
       
 71133 
       
 71134   while( (c = sqlite3Utf8Read(zPattern,&zPattern))!=0 ){
       
 71135     if( !prevEscape && c==matchAll ){
       
 71136       while( (c=sqlite3Utf8Read(zPattern,&zPattern)) == matchAll
       
 71137                || c == matchOne ){
       
 71138         if( c==matchOne && sqlite3Utf8Read(zString, &zString)==0 ){
       
 71139           return 0;
       
 71140         }
       
 71141       }
       
 71142       if( c==0 ){
       
 71143         return 1;
       
 71144       }else if( c==esc ){
       
 71145         c = sqlite3Utf8Read(zPattern, &zPattern);
       
 71146         if( c==0 ){
       
 71147           return 0;
       
 71148         }
       
 71149       }else if( c==matchSet ){
       
 71150         assert( esc==0 );         /* This is GLOB, not LIKE */
       
 71151         assert( matchSet<0x80 );  /* '[' is a single-byte character */
       
 71152         while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
       
 71153           SQLITE_SKIP_UTF8(zString);
       
 71154         }
       
 71155         return *zString!=0;
       
 71156       }
       
 71157       while( (c2 = sqlite3Utf8Read(zString,&zString))!=0 ){
       
 71158         if( noCase ){
       
 71159           GlogUpperToLower(c2);
       
 71160           GlogUpperToLower(c);
       
 71161           while( c2 != 0 && c2 != c ){
       
 71162             c2 = sqlite3Utf8Read(zString, &zString);
       
 71163             GlogUpperToLower(c2);
       
 71164           }
       
 71165         }else{
       
 71166           while( c2 != 0 && c2 != c ){
       
 71167             c2 = sqlite3Utf8Read(zString, &zString);
       
 71168           }
       
 71169         }
       
 71170         if( c2==0 ) return 0;
       
 71171         if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
       
 71172       }
       
 71173       return 0;
       
 71174     }else if( !prevEscape && c==matchOne ){
       
 71175       if( sqlite3Utf8Read(zString, &zString)==0 ){
       
 71176         return 0;
       
 71177       }
       
 71178     }else if( c==matchSet ){
       
 71179       int prior_c = 0;
       
 71180       assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
       
 71181       seen = 0;
       
 71182       invert = 0;
       
 71183       c = sqlite3Utf8Read(zString, &zString);
       
 71184       if( c==0 ) return 0;
       
 71185       c2 = sqlite3Utf8Read(zPattern, &zPattern);
       
 71186       if( c2=='^' ){
       
 71187         invert = 1;
       
 71188         c2 = sqlite3Utf8Read(zPattern, &zPattern);
       
 71189       }
       
 71190       if( c2==']' ){
       
 71191         if( c==']' ) seen = 1;
       
 71192         c2 = sqlite3Utf8Read(zPattern, &zPattern);
       
 71193       }
       
 71194       while( c2 && c2!=']' ){
       
 71195         if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
       
 71196           c2 = sqlite3Utf8Read(zPattern, &zPattern);
       
 71197           if( c>=prior_c && c<=c2 ) seen = 1;
       
 71198           prior_c = 0;
       
 71199         }else{
       
 71200           if( c==c2 ){
       
 71201             seen = 1;
       
 71202           }
       
 71203           prior_c = c2;
       
 71204         }
       
 71205         c2 = sqlite3Utf8Read(zPattern, &zPattern);
       
 71206       }
       
 71207       if( c2==0 || (seen ^ invert)==0 ){
       
 71208         return 0;
       
 71209       }
       
 71210     }else if( esc==c && !prevEscape ){
       
 71211       prevEscape = 1;
       
 71212     }else{
       
 71213       c2 = sqlite3Utf8Read(zString, &zString);
       
 71214       if( noCase ){
       
 71215         GlogUpperToLower(c);
       
 71216         GlogUpperToLower(c2);
       
 71217       }
       
 71218       if( c!=c2 ){
       
 71219         return 0;
       
 71220       }
       
 71221       prevEscape = 0;
       
 71222     }
       
 71223   }
       
 71224   return *zString==0;
       
 71225 }
       
 71226 
       
 71227 /*
       
 71228 ** Count the number of times that the LIKE operator (or GLOB which is
       
 71229 ** just a variation of LIKE) gets called.  This is used for testing
       
 71230 ** only.
       
 71231 */
       
 71232 #ifdef SQLITE_TEST
       
 71233 SQLITE_API int sqlite3_like_count = 0;
       
 71234 #endif
       
 71235 
       
 71236 
       
 71237 /*
       
 71238 ** Implementation of the like() SQL function.  This function implements
       
 71239 ** the build-in LIKE operator.  The first argument to the function is the
       
 71240 ** pattern and the second argument is the string.  So, the SQL statements:
       
 71241 **
       
 71242 **       A LIKE B
       
 71243 **
       
 71244 ** is implemented as like(B,A).
       
 71245 **
       
 71246 ** This same function (with a different compareInfo structure) computes
       
 71247 ** the GLOB operator.
       
 71248 */
       
 71249 static void likeFunc(
       
 71250   sqlite3_context *context, 
       
 71251   int argc, 
       
 71252   sqlite3_value **argv
       
 71253 ){
       
 71254   const unsigned char *zA, *zB;
       
 71255   int escape = 0;
       
 71256   int nPat;
       
 71257   sqlite3 *db = sqlite3_context_db_handle(context);
       
 71258 
       
 71259   zB = sqlite3_value_text(argv[0]);
       
 71260   zA = sqlite3_value_text(argv[1]);
       
 71261 
       
 71262   /* Limit the length of the LIKE or GLOB pattern to avoid problems
       
 71263   ** of deep recursion and N*N behavior in patternCompare().
       
 71264   */
       
 71265   nPat = sqlite3_value_bytes(argv[0]);
       
 71266   testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
       
 71267   testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
       
 71268   if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
       
 71269     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
       
 71270     return;
       
 71271   }
       
 71272   assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
       
 71273 
       
 71274   if( argc==3 ){
       
 71275     /* The escape character string must consist of a single UTF-8 character.
       
 71276     ** Otherwise, return an error.
       
 71277     */
       
 71278     const unsigned char *zEsc = sqlite3_value_text(argv[2]);
       
 71279     if( zEsc==0 ) return;
       
 71280     if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
       
 71281       sqlite3_result_error(context, 
       
 71282           "ESCAPE expression must be a single character", -1);
       
 71283       return;
       
 71284     }
       
 71285     escape = sqlite3Utf8Read(zEsc, &zEsc);
       
 71286   }
       
 71287   if( zA && zB ){
       
 71288     struct compareInfo *pInfo = sqlite3_user_data(context);
       
 71289 #ifdef SQLITE_TEST
       
 71290     sqlite3_like_count++;
       
 71291 #endif
       
 71292     
       
 71293     sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
       
 71294   }
       
 71295 }
       
 71296 
       
 71297 /*
       
 71298 ** Implementation of the NULLIF(x,y) function.  The result is the first
       
 71299 ** argument if the arguments are different.  The result is NULL if the
       
 71300 ** arguments are equal to each other.
       
 71301 */
       
 71302 static void nullifFunc(
       
 71303   sqlite3_context *context,
       
 71304   int NotUsed,
       
 71305   sqlite3_value **argv
       
 71306 ){
       
 71307   CollSeq *pColl = sqlite3GetFuncCollSeq(context);
       
 71308   UNUSED_PARAMETER(NotUsed);
       
 71309   if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
       
 71310     sqlite3_result_value(context, argv[0]);
       
 71311   }
       
 71312 }
       
 71313 
       
 71314 /*
       
 71315 ** Implementation of the sqlite_version() function.  The result is the version
       
 71316 ** of the SQLite library that is running.
       
 71317 */
       
 71318 static void versionFunc(
       
 71319   sqlite3_context *context,
       
 71320   int NotUsed,
       
 71321   sqlite3_value **NotUsed2
       
 71322 ){
       
 71323   UNUSED_PARAMETER2(NotUsed, NotUsed2);
       
 71324   sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
       
 71325 }
       
 71326 
       
 71327 /*
       
 71328 ** Implementation of the sqlite_source_id() function. The result is a string
       
 71329 ** that identifies the particular version of the source code used to build
       
 71330 ** SQLite.
       
 71331 */
       
 71332 static void sourceidFunc(
       
 71333   sqlite3_context *context,
       
 71334   int NotUsed,
       
 71335   sqlite3_value **NotUsed2
       
 71336 ){
       
 71337   UNUSED_PARAMETER2(NotUsed, NotUsed2);
       
 71338   sqlite3_result_text(context, SQLITE_SOURCE_ID, -1, SQLITE_STATIC);
       
 71339 }
       
 71340 
       
 71341 /* Array for converting from half-bytes (nybbles) into ASCII hex
       
 71342 ** digits. */
       
 71343 static const char hexdigits[] = {
       
 71344   '0', '1', '2', '3', '4', '5', '6', '7',
       
 71345   '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 
       
 71346 };
       
 71347 
       
 71348 /*
       
 71349 ** EXPERIMENTAL - This is not an official function.  The interface may
       
 71350 ** change.  This function may disappear.  Do not write code that depends
       
 71351 ** on this function.
       
 71352 **
       
 71353 ** Implementation of the QUOTE() function.  This function takes a single
       
 71354 ** argument.  If the argument is numeric, the return value is the same as
       
 71355 ** the argument.  If the argument is NULL, the return value is the string
       
 71356 ** "NULL".  Otherwise, the argument is enclosed in single quotes with
       
 71357 ** single-quote escapes.
       
 71358 */
       
 71359 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
       
 71360   assert( argc==1 );
       
 71361   UNUSED_PARAMETER(argc);
       
 71362   switch( sqlite3_value_type(argv[0]) ){
       
 71363     case SQLITE_INTEGER:
       
 71364     case SQLITE_FLOAT: {
       
 71365       sqlite3_result_value(context, argv[0]);
       
 71366       break;
       
 71367     }
       
 71368     case SQLITE_BLOB: {
       
 71369       char *zText = 0;
       
 71370       char const *zBlob = sqlite3_value_blob(argv[0]);
       
 71371       int nBlob = sqlite3_value_bytes(argv[0]);
       
 71372       assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
       
 71373       zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 
       
 71374       if( zText ){
       
 71375         int i;
       
 71376         for(i=0; i<nBlob; i++){
       
 71377           zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
       
 71378           zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
       
 71379         }
       
 71380         zText[(nBlob*2)+2] = '\'';
       
 71381         zText[(nBlob*2)+3] = '\0';
       
 71382         zText[0] = 'X';
       
 71383         zText[1] = '\'';
       
 71384         sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
       
 71385         sqlite3_free(zText);
       
 71386       }
       
 71387       break;
       
 71388     }
       
 71389     case SQLITE_TEXT: {
       
 71390       int i,j;
       
 71391       u64 n;
       
 71392       const unsigned char *zArg = sqlite3_value_text(argv[0]);
       
 71393       char *z;
       
 71394 
       
 71395       if( zArg==0 ) return;
       
 71396       for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
       
 71397       z = contextMalloc(context, ((i64)i)+((i64)n)+3);
       
 71398       if( z ){
       
 71399         z[0] = '\'';
       
 71400         for(i=0, j=1; zArg[i]; i++){
       
 71401           z[j++] = zArg[i];
       
 71402           if( zArg[i]=='\'' ){
       
 71403             z[j++] = '\'';
       
 71404           }
       
 71405         }
       
 71406         z[j++] = '\'';
       
 71407         z[j] = 0;
       
 71408         sqlite3_result_text(context, z, j, sqlite3_free);
       
 71409       }
       
 71410       break;
       
 71411     }
       
 71412     default: {
       
 71413       assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
       
 71414       sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
       
 71415       break;
       
 71416     }
       
 71417   }
       
 71418 }
       
 71419 
       
 71420 /*
       
 71421 ** The hex() function.  Interpret the argument as a blob.  Return
       
 71422 ** a hexadecimal rendering as text.
       
 71423 */
       
 71424 static void hexFunc(
       
 71425   sqlite3_context *context,
       
 71426   int argc,
       
 71427   sqlite3_value **argv
       
 71428 ){
       
 71429   int i, n;
       
 71430   const unsigned char *pBlob;
       
 71431   char *zHex, *z;
       
 71432   assert( argc==1 );
       
 71433   UNUSED_PARAMETER(argc);
       
 71434   pBlob = sqlite3_value_blob(argv[0]);
       
 71435   n = sqlite3_value_bytes(argv[0]);
       
 71436   assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
       
 71437   z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
       
 71438   if( zHex ){
       
 71439     for(i=0; i<n; i++, pBlob++){
       
 71440       unsigned char c = *pBlob;
       
 71441       *(z++) = hexdigits[(c>>4)&0xf];
       
 71442       *(z++) = hexdigits[c&0xf];
       
 71443     }
       
 71444     *z = 0;
       
 71445     sqlite3_result_text(context, zHex, n*2, sqlite3_free);
       
 71446   }
       
 71447 }
       
 71448 
       
 71449 /*
       
 71450 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
       
 71451 */
       
 71452 static void zeroblobFunc(
       
 71453   sqlite3_context *context,
       
 71454   int argc,
       
 71455   sqlite3_value **argv
       
 71456 ){
       
 71457   i64 n;
       
 71458   sqlite3 *db = sqlite3_context_db_handle(context);
       
 71459   assert( argc==1 );
       
 71460   UNUSED_PARAMETER(argc);
       
 71461   n = sqlite3_value_int64(argv[0]);
       
 71462   testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] );
       
 71463   testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
       
 71464   if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
       
 71465     sqlite3_result_error_toobig(context);
       
 71466   }else{
       
 71467     sqlite3_result_zeroblob(context, (int)n);
       
 71468   }
       
 71469 }
       
 71470 
       
 71471 /*
       
 71472 ** The replace() function.  Three arguments are all strings: call
       
 71473 ** them A, B, and C. The result is also a string which is derived
       
 71474 ** from A by replacing every occurance of B with C.  The match
       
 71475 ** must be exact.  Collating sequences are not used.
       
 71476 */
       
 71477 static void replaceFunc(
       
 71478   sqlite3_context *context,
       
 71479   int argc,
       
 71480   sqlite3_value **argv
       
 71481 ){
       
 71482   const unsigned char *zStr;        /* The input string A */
       
 71483   const unsigned char *zPattern;    /* The pattern string B */
       
 71484   const unsigned char *zRep;        /* The replacement string C */
       
 71485   unsigned char *zOut;              /* The output */
       
 71486   int nStr;                /* Size of zStr */
       
 71487   int nPattern;            /* Size of zPattern */
       
 71488   int nRep;                /* Size of zRep */
       
 71489   i64 nOut;                /* Maximum size of zOut */
       
 71490   int loopLimit;           /* Last zStr[] that might match zPattern[] */
       
 71491   int i, j;                /* Loop counters */
       
 71492 
       
 71493   assert( argc==3 );
       
 71494   UNUSED_PARAMETER(argc);
       
 71495   zStr = sqlite3_value_text(argv[0]);
       
 71496   if( zStr==0 ) return;
       
 71497   nStr = sqlite3_value_bytes(argv[0]);
       
 71498   assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
       
 71499   zPattern = sqlite3_value_text(argv[1]);
       
 71500   if( zPattern==0 ){
       
 71501     assert( sqlite3_value_type(argv[1])==SQLITE_NULL
       
 71502             || sqlite3_context_db_handle(context)->mallocFailed );
       
 71503     return;
       
 71504   }
       
 71505   if( zPattern[0]==0 ){
       
 71506     assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
       
 71507     sqlite3_result_value(context, argv[0]);
       
 71508     return;
       
 71509   }
       
 71510   nPattern = sqlite3_value_bytes(argv[1]);
       
 71511   assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
       
 71512   zRep = sqlite3_value_text(argv[2]);
       
 71513   if( zRep==0 ) return;
       
 71514   nRep = sqlite3_value_bytes(argv[2]);
       
 71515   assert( zRep==sqlite3_value_text(argv[2]) );
       
 71516   nOut = nStr + 1;
       
 71517   assert( nOut<SQLITE_MAX_LENGTH );
       
 71518   zOut = contextMalloc(context, (i64)nOut);
       
 71519   if( zOut==0 ){
       
 71520     return;
       
 71521   }
       
 71522   loopLimit = nStr - nPattern;  
       
 71523   for(i=j=0; i<=loopLimit; i++){
       
 71524     if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
       
 71525       zOut[j++] = zStr[i];
       
 71526     }else{
       
 71527       u8 *zOld;
       
 71528       sqlite3 *db = sqlite3_context_db_handle(context);
       
 71529       nOut += nRep - nPattern;
       
 71530       testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
       
 71531       testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
       
 71532       if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
       
 71533         sqlite3_result_error_toobig(context);
       
 71534         sqlite3DbFree(db, zOut);
       
 71535         return;
       
 71536       }
       
 71537       zOld = zOut;
       
 71538       zOut = sqlite3_realloc(zOut, (int)nOut);
       
 71539       if( zOut==0 ){
       
 71540         sqlite3_result_error_nomem(context);
       
 71541         sqlite3DbFree(db, zOld);
       
 71542         return;
       
 71543       }
       
 71544       memcpy(&zOut[j], zRep, nRep);
       
 71545       j += nRep;
       
 71546       i += nPattern-1;
       
 71547     }
       
 71548   }
       
 71549   assert( j+nStr-i+1==nOut );
       
 71550   memcpy(&zOut[j], &zStr[i], nStr-i);
       
 71551   j += nStr - i;
       
 71552   assert( j<=nOut );
       
 71553   zOut[j] = 0;
       
 71554   sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
       
 71555 }
       
 71556 
       
 71557 /*
       
 71558 ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
       
 71559 ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
       
 71560 */
       
 71561 static void trimFunc(
       
 71562   sqlite3_context *context,
       
 71563   int argc,
       
 71564   sqlite3_value **argv
       
 71565 ){
       
 71566   const unsigned char *zIn;         /* Input string */
       
 71567   const unsigned char *zCharSet;    /* Set of characters to trim */
       
 71568   int nIn;                          /* Number of bytes in input */
       
 71569   int flags;                        /* 1: trimleft  2: trimright  3: trim */
       
 71570   int i;                            /* Loop counter */
       
 71571   unsigned char *aLen = 0;          /* Length of each character in zCharSet */
       
 71572   unsigned char **azChar = 0;       /* Individual characters in zCharSet */
       
 71573   int nChar;                        /* Number of characters in zCharSet */
       
 71574 
       
 71575   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
       
 71576     return;
       
 71577   }
       
 71578   zIn = sqlite3_value_text(argv[0]);
       
 71579   if( zIn==0 ) return;
       
 71580   nIn = sqlite3_value_bytes(argv[0]);
       
 71581   assert( zIn==sqlite3_value_text(argv[0]) );
       
 71582   if( argc==1 ){
       
 71583     static const unsigned char lenOne[] = { 1 };
       
 71584     static unsigned char * const azOne[] = { (u8*)" " };
       
 71585     nChar = 1;
       
 71586     aLen = (u8*)lenOne;
       
 71587     azChar = (unsigned char **)azOne;
       
 71588     zCharSet = 0;
       
 71589   }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
       
 71590     return;
       
 71591   }else{
       
 71592     const unsigned char *z;
       
 71593     for(z=zCharSet, nChar=0; *z; nChar++){
       
 71594       SQLITE_SKIP_UTF8(z);
       
 71595     }
       
 71596     if( nChar>0 ){
       
 71597       azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
       
 71598       if( azChar==0 ){
       
 71599         return;
       
 71600       }
       
 71601       aLen = (unsigned char*)&azChar[nChar];
       
 71602       for(z=zCharSet, nChar=0; *z; nChar++){
       
 71603         azChar[nChar] = (unsigned char *)z;
       
 71604         SQLITE_SKIP_UTF8(z);
       
 71605         aLen[nChar] = (u8)(z - azChar[nChar]);
       
 71606       }
       
 71607     }
       
 71608   }
       
 71609   if( nChar>0 ){
       
 71610     flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
       
 71611     if( flags & 1 ){
       
 71612       while( nIn>0 ){
       
 71613         int len = 0;
       
 71614         for(i=0; i<nChar; i++){
       
 71615           len = aLen[i];
       
 71616           if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
       
 71617         }
       
 71618         if( i>=nChar ) break;
       
 71619         zIn += len;
       
 71620         nIn -= len;
       
 71621       }
       
 71622     }
       
 71623     if( flags & 2 ){
       
 71624       while( nIn>0 ){
       
 71625         int len = 0;
       
 71626         for(i=0; i<nChar; i++){
       
 71627           len = aLen[i];
       
 71628           if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
       
 71629         }
       
 71630         if( i>=nChar ) break;
       
 71631         nIn -= len;
       
 71632       }
       
 71633     }
       
 71634     if( zCharSet ){
       
 71635       sqlite3_free(azChar);
       
 71636     }
       
 71637   }
       
 71638   sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
       
 71639 }
       
 71640 
       
 71641 
       
 71642 #ifdef SQLITE_SOUNDEX
       
 71643 /*
       
 71644 ** Compute the soundex encoding of a word.
       
 71645 */
       
 71646 static void soundexFunc(
       
 71647   sqlite3_context *context,
       
 71648   int argc,
       
 71649   sqlite3_value **argv
       
 71650 ){
       
 71651   char zResult[8];
       
 71652   const u8 *zIn;
       
 71653   int i, j;
       
 71654   static const unsigned char iCode[] = {
       
 71655     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       
 71656     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       
 71657     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       
 71658     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       
 71659     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
       
 71660     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
       
 71661     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
       
 71662     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
       
 71663   };
       
 71664   assert( argc==1 );
       
 71665   zIn = (u8*)sqlite3_value_text(argv[0]);
       
 71666   if( zIn==0 ) zIn = (u8*)"";
       
 71667   for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
       
 71668   if( zIn[i] ){
       
 71669     u8 prevcode = iCode[zIn[i]&0x7f];
       
 71670     zResult[0] = sqlite3Toupper(zIn[i]);
       
 71671     for(j=1; j<4 && zIn[i]; i++){
       
 71672       int code = iCode[zIn[i]&0x7f];
       
 71673       if( code>0 ){
       
 71674         if( code!=prevcode ){
       
 71675           prevcode = code;
       
 71676           zResult[j++] = code + '0';
       
 71677         }
       
 71678       }else{
       
 71679         prevcode = 0;
       
 71680       }
       
 71681     }
       
 71682     while( j<4 ){
       
 71683       zResult[j++] = '0';
       
 71684     }
       
 71685     zResult[j] = 0;
       
 71686     sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
       
 71687   }else{
       
 71688     sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
       
 71689   }
       
 71690 }
       
 71691 #endif
       
 71692 
       
 71693 #ifndef SQLITE_OMIT_LOAD_EXTENSION
       
 71694 /*
       
 71695 ** A function that loads a shared-library extension then returns NULL.
       
 71696 */
       
 71697 static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
       
 71698   const char *zFile = (const char *)sqlite3_value_text(argv[0]);
       
 71699   const char *zProc;
       
 71700   sqlite3 *db = sqlite3_context_db_handle(context);
       
 71701   char *zErrMsg = 0;
       
 71702 
       
 71703   if( argc==2 ){
       
 71704     zProc = (const char *)sqlite3_value_text(argv[1]);
       
 71705   }else{
       
 71706     zProc = 0;
       
 71707   }
       
 71708   if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
       
 71709     sqlite3_result_error(context, zErrMsg, -1);
       
 71710     sqlite3_free(zErrMsg);
       
 71711   }
       
 71712 }
       
 71713 #endif
       
 71714 
       
 71715 
       
 71716 /*
       
 71717 ** An instance of the following structure holds the context of a
       
 71718 ** sum() or avg() aggregate computation.
       
 71719 */
       
 71720 typedef struct SumCtx SumCtx;
       
 71721 struct SumCtx {
       
 71722   double rSum;      /* Floating point sum */
       
 71723   i64 iSum;         /* Integer sum */   
       
 71724   i64 cnt;          /* Number of elements summed */
       
 71725   u8 overflow;      /* True if integer overflow seen */
       
 71726   u8 approx;        /* True if non-integer value was input to the sum */
       
 71727 };
       
 71728 
       
 71729 /*
       
 71730 ** Routines used to compute the sum, average, and total.
       
 71731 **
       
 71732 ** The SUM() function follows the (broken) SQL standard which means
       
 71733 ** that it returns NULL if it sums over no inputs.  TOTAL returns
       
 71734 ** 0.0 in that case.  In addition, TOTAL always returns a float where
       
 71735 ** SUM might return an integer if it never encounters a floating point
       
 71736 ** value.  TOTAL never fails, but SUM might through an exception if
       
 71737 ** it overflows an integer.
       
 71738 */
       
 71739 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
       
 71740   SumCtx *p;
       
 71741   int type;
       
 71742   assert( argc==1 );
       
 71743   UNUSED_PARAMETER(argc);
       
 71744   p = sqlite3_aggregate_context(context, sizeof(*p));
       
 71745   type = sqlite3_value_numeric_type(argv[0]);
       
 71746   if( p && type!=SQLITE_NULL ){
       
 71747     p->cnt++;
       
 71748     if( type==SQLITE_INTEGER ){
       
 71749       i64 v = sqlite3_value_int64(argv[0]);
       
 71750       p->rSum += v;
       
 71751       if( (p->approx|p->overflow)==0 ){
       
 71752         i64 iNewSum = p->iSum + v;
       
 71753         int s1 = (int)(p->iSum >> (sizeof(i64)*8-1));
       
 71754         int s2 = (int)(v       >> (sizeof(i64)*8-1));
       
 71755         int s3 = (int)(iNewSum >> (sizeof(i64)*8-1));
       
 71756         p->overflow = ((s1&s2&~s3) | (~s1&~s2&s3))?1:0;
       
 71757         p->iSum = iNewSum;
       
 71758       }
       
 71759     }else{
       
 71760       p->rSum += sqlite3_value_double(argv[0]);
       
 71761       p->approx = 1;
       
 71762     }
       
 71763   }
       
 71764 }
       
 71765 static void sumFinalize(sqlite3_context *context){
       
 71766   SumCtx *p;
       
 71767   p = sqlite3_aggregate_context(context, 0);
       
 71768   if( p && p->cnt>0 ){
       
 71769     if( p->overflow ){
       
 71770       sqlite3_result_error(context,"integer overflow",-1);
       
 71771     }else if( p->approx ){
       
 71772       sqlite3_result_double(context, p->rSum);
       
 71773     }else{
       
 71774       sqlite3_result_int64(context, p->iSum);
       
 71775     }
       
 71776   }
       
 71777 }
       
 71778 static void avgFinalize(sqlite3_context *context){
       
 71779   SumCtx *p;
       
 71780   p = sqlite3_aggregate_context(context, 0);
       
 71781   if( p && p->cnt>0 ){
       
 71782     sqlite3_result_double(context, p->rSum/(double)p->cnt);
       
 71783   }
       
 71784 }
       
 71785 static void totalFinalize(sqlite3_context *context){
       
 71786   SumCtx *p;
       
 71787   p = sqlite3_aggregate_context(context, 0);
       
 71788   /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
       
 71789   sqlite3_result_double(context, p ? p->rSum : (double)0);
       
 71790 }
       
 71791 
       
 71792 /*
       
 71793 ** The following structure keeps track of state information for the
       
 71794 ** count() aggregate function.
       
 71795 */
       
 71796 typedef struct CountCtx CountCtx;
       
 71797 struct CountCtx {
       
 71798   i64 n;
       
 71799 };
       
 71800 
       
 71801 /*
       
 71802 ** Routines to implement the count() aggregate function.
       
 71803 */
       
 71804 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
       
 71805   CountCtx *p;
       
 71806   p = sqlite3_aggregate_context(context, sizeof(*p));
       
 71807   if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
       
 71808     p->n++;
       
 71809   }
       
 71810 
       
 71811 #ifndef SQLITE_OMIT_DEPRECATED
       
 71812   /* The sqlite3_aggregate_count() function is deprecated.  But just to make
       
 71813   ** sure it still operates correctly, verify that its count agrees with our 
       
 71814   ** internal count when using count(*) and when the total count can be
       
 71815   ** expressed as a 32-bit integer. */
       
 71816   assert( argc==1 || p==0 || p->n>0x7fffffff
       
 71817           || p->n==sqlite3_aggregate_count(context) );
       
 71818 #endif
       
 71819 }   
       
 71820 static void countFinalize(sqlite3_context *context){
       
 71821   CountCtx *p;
       
 71822   p = sqlite3_aggregate_context(context, 0);
       
 71823   sqlite3_result_int64(context, p ? p->n : 0);
       
 71824 }
       
 71825 
       
 71826 /*
       
 71827 ** Routines to implement min() and max() aggregate functions.
       
 71828 */
       
 71829 static void minmaxStep(
       
 71830   sqlite3_context *context, 
       
 71831   int NotUsed, 
       
 71832   sqlite3_value **argv
       
 71833 ){
       
 71834   Mem *pArg  = (Mem *)argv[0];
       
 71835   Mem *pBest;
       
 71836   UNUSED_PARAMETER(NotUsed);
       
 71837 
       
 71838   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
       
 71839   pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
       
 71840   if( !pBest ) return;
       
 71841 
       
 71842   if( pBest->flags ){
       
 71843     int max;
       
 71844     int cmp;
       
 71845     CollSeq *pColl = sqlite3GetFuncCollSeq(context);
       
 71846     /* This step function is used for both the min() and max() aggregates,
       
 71847     ** the only difference between the two being that the sense of the
       
 71848     ** comparison is inverted. For the max() aggregate, the
       
 71849     ** sqlite3_user_data() function returns (void *)-1. For min() it
       
 71850     ** returns (void *)db, where db is the sqlite3* database pointer.
       
 71851     ** Therefore the next statement sets variable 'max' to 1 for the max()
       
 71852     ** aggregate, or 0 for min().
       
 71853     */
       
 71854     max = sqlite3_user_data(context)!=0;
       
 71855     cmp = sqlite3MemCompare(pBest, pArg, pColl);
       
 71856     if( (max && cmp<0) || (!max && cmp>0) ){
       
 71857       sqlite3VdbeMemCopy(pBest, pArg);
       
 71858     }
       
 71859   }else{
       
 71860     sqlite3VdbeMemCopy(pBest, pArg);
       
 71861   }
       
 71862 }
       
 71863 static void minMaxFinalize(sqlite3_context *context){
       
 71864   sqlite3_value *pRes;
       
 71865   pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
       
 71866   if( pRes ){
       
 71867     if( ALWAYS(pRes->flags) ){
       
 71868       sqlite3_result_value(context, pRes);
       
 71869     }
       
 71870     sqlite3VdbeMemRelease(pRes);
       
 71871   }
       
 71872 }
       
 71873 
       
 71874 /*
       
 71875 ** group_concat(EXPR, ?SEPARATOR?)
       
 71876 */
       
 71877 static void groupConcatStep(
       
 71878   sqlite3_context *context,
       
 71879   int argc,
       
 71880   sqlite3_value **argv
       
 71881 ){
       
 71882   const char *zVal;
       
 71883   StrAccum *pAccum;
       
 71884   const char *zSep;
       
 71885   int nVal, nSep;
       
 71886   assert( argc==1 || argc==2 );
       
 71887   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
       
 71888   pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
       
 71889 
       
 71890   if( pAccum ){
       
 71891     sqlite3 *db = sqlite3_context_db_handle(context);
       
 71892     int firstTerm = pAccum->useMalloc==0;
       
 71893     pAccum->useMalloc = 1;
       
 71894     pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
       
 71895     if( !firstTerm ){
       
 71896       if( argc==2 ){
       
 71897         zSep = (char*)sqlite3_value_text(argv[1]);
       
 71898         nSep = sqlite3_value_bytes(argv[1]);
       
 71899       }else{
       
 71900         zSep = ",";
       
 71901         nSep = 1;
       
 71902       }
       
 71903       sqlite3StrAccumAppend(pAccum, zSep, nSep);
       
 71904     }
       
 71905     zVal = (char*)sqlite3_value_text(argv[0]);
       
 71906     nVal = sqlite3_value_bytes(argv[0]);
       
 71907     sqlite3StrAccumAppend(pAccum, zVal, nVal);
       
 71908   }
       
 71909 }
       
 71910 static void groupConcatFinalize(sqlite3_context *context){
       
 71911   StrAccum *pAccum;
       
 71912   pAccum = sqlite3_aggregate_context(context, 0);
       
 71913   if( pAccum ){
       
 71914     if( pAccum->tooBig ){
       
 71915       sqlite3_result_error_toobig(context);
       
 71916     }else if( pAccum->mallocFailed ){
       
 71917       sqlite3_result_error_nomem(context);
       
 71918     }else{    
       
 71919       sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 
       
 71920                           sqlite3_free);
       
 71921     }
       
 71922   }
       
 71923 }
       
 71924 
       
 71925 /*
       
 71926 ** This function registered all of the above C functions as SQL
       
 71927 ** functions.  This should be the only routine in this file with
       
 71928 ** external linkage.
       
 71929 */
       
 71930 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
       
 71931 #ifndef SQLITE_OMIT_ALTERTABLE
       
 71932   sqlite3AlterFunctions(db);
       
 71933 #endif
       
 71934   if( !db->mallocFailed ){
       
 71935     int rc = sqlite3_overload_function(db, "MATCH", 2);
       
 71936     assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
       
 71937     if( rc==SQLITE_NOMEM ){
       
 71938       db->mallocFailed = 1;
       
 71939     }
       
 71940   }
       
 71941 }
       
 71942 
       
 71943 /*
       
 71944 ** Set the LIKEOPT flag on the 2-argument function with the given name.
       
 71945 */
       
 71946 static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
       
 71947   FuncDef *pDef;
       
 71948   pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
       
 71949                              2, SQLITE_UTF8, 0);
       
 71950   if( ALWAYS(pDef) ){
       
 71951     pDef->flags = flagVal;
       
 71952   }
       
 71953 }
       
 71954 
       
 71955 /*
       
 71956 ** Register the built-in LIKE and GLOB functions.  The caseSensitive
       
 71957 ** parameter determines whether or not the LIKE operator is case
       
 71958 ** sensitive.  GLOB is always case sensitive.
       
 71959 */
       
 71960 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
       
 71961   struct compareInfo *pInfo;
       
 71962   if( caseSensitive ){
       
 71963     pInfo = (struct compareInfo*)&likeInfoAlt;
       
 71964   }else{
       
 71965     pInfo = (struct compareInfo*)&likeInfoNorm;
       
 71966   }
       
 71967   sqlite3CreateFunc(db, "like", 2, SQLITE_ANY, pInfo, likeFunc, 0, 0);
       
 71968   sqlite3CreateFunc(db, "like", 3, SQLITE_ANY, pInfo, likeFunc, 0, 0);
       
 71969   sqlite3CreateFunc(db, "glob", 2, SQLITE_ANY, 
       
 71970       (struct compareInfo*)&globInfo, likeFunc, 0,0);
       
 71971   setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
       
 71972   setLikeOptFlag(db, "like", 
       
 71973       caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
       
 71974 }
       
 71975 
       
 71976 /*
       
 71977 ** pExpr points to an expression which implements a function.  If
       
 71978 ** it is appropriate to apply the LIKE optimization to that function
       
 71979 ** then set aWc[0] through aWc[2] to the wildcard characters and
       
 71980 ** return TRUE.  If the function is not a LIKE-style function then
       
 71981 ** return FALSE.
       
 71982 */
       
 71983 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
       
 71984   FuncDef *pDef;
       
 71985   if( pExpr->op!=TK_FUNCTION 
       
 71986    || !pExpr->x.pList 
       
 71987    || pExpr->x.pList->nExpr!=2
       
 71988   ){
       
 71989     return 0;
       
 71990   }
       
 71991   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
       
 71992   pDef = sqlite3FindFunction(db, pExpr->u.zToken, 
       
 71993                              sqlite3Strlen30(pExpr->u.zToken),
       
 71994                              2, SQLITE_UTF8, 0);
       
 71995   if( NEVER(pDef==0) || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
       
 71996     return 0;
       
 71997   }
       
 71998 
       
 71999   /* The memcpy() statement assumes that the wildcard characters are
       
 72000   ** the first three statements in the compareInfo structure.  The
       
 72001   ** asserts() that follow verify that assumption
       
 72002   */
       
 72003   memcpy(aWc, pDef->pUserData, 3);
       
 72004   assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
       
 72005   assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
       
 72006   assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
       
 72007   *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
       
 72008   return 1;
       
 72009 }
       
 72010 
       
 72011 /*
       
 72012 ** All all of the FuncDef structures in the aBuiltinFunc[] array above
       
 72013 ** to the global function hash table.  This occurs at start-time (as
       
 72014 ** a consequence of calling sqlite3_initialize()).
       
 72015 **
       
 72016 ** After this routine runs
       
 72017 */
       
 72018 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
       
 72019   /*
       
 72020   ** The following array holds FuncDef structures for all of the functions
       
 72021   ** defined in this file.
       
 72022   **
       
 72023   ** The array cannot be constant since changes are made to the
       
 72024   ** FuncDef.pHash elements at start-time.  The elements of this array
       
 72025   ** are read-only after initialization is complete.
       
 72026   */
       
 72027   static SQLITE_WSD FuncDef aBuiltinFunc[] = {
       
 72028     FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
       
 72029     FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
       
 72030     FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
       
 72031     FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
       
 72032     FUNCTION(trim,               1, 3, 0, trimFunc         ),
       
 72033     FUNCTION(trim,               2, 3, 0, trimFunc         ),
       
 72034     FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
       
 72035     FUNCTION(min,                0, 0, 1, 0                ),
       
 72036     AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
       
 72037     FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
       
 72038     FUNCTION(max,                0, 1, 1, 0                ),
       
 72039     AGGREGATE(max,               1, 1, 1, minmaxStep,      minMaxFinalize ),
       
 72040     FUNCTION(typeof,             1, 0, 0, typeofFunc       ),
       
 72041     FUNCTION(length,             1, 0, 0, lengthFunc       ),
       
 72042     FUNCTION(substr,             2, 0, 0, substrFunc       ),
       
 72043     FUNCTION(substr,             3, 0, 0, substrFunc       ),
       
 72044     FUNCTION(abs,                1, 0, 0, absFunc          ),
       
 72045 #ifndef SQLITE_OMIT_FLOATING_POINT
       
 72046     FUNCTION(round,              1, 0, 0, roundFunc        ),
       
 72047     FUNCTION(round,              2, 0, 0, roundFunc        ),
       
 72048 #endif
       
 72049     FUNCTION(upper,              1, 0, 0, upperFunc        ),
       
 72050     FUNCTION(lower,              1, 0, 0, lowerFunc        ),
       
 72051     FUNCTION(coalesce,           1, 0, 0, 0                ),
       
 72052     FUNCTION(coalesce,          -1, 0, 0, ifnullFunc       ),
       
 72053     FUNCTION(coalesce,           0, 0, 0, 0                ),
       
 72054     FUNCTION(hex,                1, 0, 0, hexFunc          ),
       
 72055     FUNCTION(ifnull,             2, 0, 1, ifnullFunc       ),
       
 72056     FUNCTION(random,             0, 0, 0, randomFunc       ),
       
 72057     FUNCTION(randomblob,         1, 0, 0, randomBlob       ),
       
 72058     FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
       
 72059     FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
       
 72060     FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
       
 72061     FUNCTION(quote,              1, 0, 0, quoteFunc        ),
       
 72062     FUNCTION(last_insert_rowid,  0, 0, 0, last_insert_rowid),
       
 72063     FUNCTION(changes,            0, 0, 0, changes          ),
       
 72064     FUNCTION(total_changes,      0, 0, 0, total_changes    ),
       
 72065     FUNCTION(replace,            3, 0, 0, replaceFunc      ),
       
 72066     FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
       
 72067   #ifdef SQLITE_SOUNDEX
       
 72068     FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
       
 72069   #endif
       
 72070   #ifndef SQLITE_OMIT_LOAD_EXTENSION
       
 72071     FUNCTION(load_extension,     1, 0, 0, loadExt          ),
       
 72072     FUNCTION(load_extension,     2, 0, 0, loadExt          ),
       
 72073   #endif
       
 72074     AGGREGATE(sum,               1, 0, 0, sumStep,         sumFinalize    ),
       
 72075     AGGREGATE(total,             1, 0, 0, sumStep,         totalFinalize    ),
       
 72076     AGGREGATE(avg,               1, 0, 0, sumStep,         avgFinalize    ),
       
 72077  /* AGGREGATE(count,             0, 0, 0, countStep,       countFinalize  ), */
       
 72078     {0,SQLITE_UTF8,SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0},
       
 72079     AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
       
 72080     AGGREGATE(group_concat,      1, 0, 0, groupConcatStep, groupConcatFinalize),
       
 72081     AGGREGATE(group_concat,      2, 0, 0, groupConcatStep, groupConcatFinalize),
       
 72082   
       
 72083     LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
       
 72084   #ifdef SQLITE_CASE_SENSITIVE_LIKE
       
 72085     LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
       
 72086     LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
       
 72087   #else
       
 72088     LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
       
 72089     LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
       
 72090   #endif
       
 72091   };
       
 72092 
       
 72093   int i;
       
 72094   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
       
 72095   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
       
 72096 
       
 72097   for(i=0; i<ArraySize(aBuiltinFunc); i++){
       
 72098     sqlite3FuncDefInsert(pHash, &aFunc[i]);
       
 72099   }
       
 72100   sqlite3RegisterDateTimeFunctions();
       
 72101 }
       
 72102 
       
 72103 /************** End of func.c ************************************************/
       
 72104 /************** Begin file fkey.c ********************************************/
       
 72105 /*
       
 72106 **
       
 72107 ** The author disclaims copyright to this source code.  In place of
       
 72108 ** a legal notice, here is a blessing:
       
 72109 **
       
 72110 **    May you do good and not evil.
       
 72111 **    May you find forgiveness for yourself and forgive others.
       
 72112 **    May you share freely, never taking more than you give.
       
 72113 **
       
 72114 *************************************************************************
       
 72115 ** This file contains code used by the compiler to add foreign key
       
 72116 ** support to compiled SQL statements.
       
 72117 */
       
 72118 
       
 72119 #ifndef SQLITE_OMIT_FOREIGN_KEY
       
 72120 #ifndef SQLITE_OMIT_TRIGGER
       
 72121 
       
 72122 /*
       
 72123 ** Deferred and Immediate FKs
       
 72124 ** --------------------------
       
 72125 **
       
 72126 ** Foreign keys in SQLite come in two flavours: deferred and immediate.
       
 72127 ** If an immediate foreign key constraint is violated, SQLITE_CONSTRAINT
       
 72128 ** is returned and the current statement transaction rolled back. If a 
       
 72129 ** deferred foreign key constraint is violated, no action is taken 
       
 72130 ** immediately. However if the application attempts to commit the 
       
 72131 ** transaction before fixing the constraint violation, the attempt fails.
       
 72132 **
       
 72133 ** Deferred constraints are implemented using a simple counter associated
       
 72134 ** with the database handle. The counter is set to zero each time a 
       
 72135 ** database transaction is opened. Each time a statement is executed 
       
 72136 ** that causes a foreign key violation, the counter is incremented. Each
       
 72137 ** time a statement is executed that removes an existing violation from
       
 72138 ** the database, the counter is decremented. When the transaction is
       
 72139 ** committed, the commit fails if the current value of the counter is
       
 72140 ** greater than zero. This scheme has two big drawbacks:
       
 72141 **
       
 72142 **   * When a commit fails due to a deferred foreign key constraint, 
       
 72143 **     there is no way to tell which foreign constraint is not satisfied,
       
 72144 **     or which row it is not satisfied for.
       
 72145 **
       
 72146 **   * If the database contains foreign key violations when the 
       
 72147 **     transaction is opened, this may cause the mechanism to malfunction.
       
 72148 **
       
 72149 ** Despite these problems, this approach is adopted as it seems simpler
       
 72150 ** than the alternatives.
       
 72151 **
       
 72152 ** INSERT operations:
       
 72153 **
       
 72154 **   I.1) For each FK for which the table is the child table, search
       
 72155 **        the parent table for a match. If none is found increment the
       
 72156 **        constraint counter.
       
 72157 **
       
 72158 **   I.2) For each FK for which the table is the parent table, 
       
 72159 **        search the child table for rows that correspond to the new
       
 72160 **        row in the parent table. Decrement the counter for each row
       
 72161 **        found (as the constraint is now satisfied).
       
 72162 **
       
 72163 ** DELETE operations:
       
 72164 **
       
 72165 **   D.1) For each FK for which the table is the child table, 
       
 72166 **        search the parent table for a row that corresponds to the 
       
 72167 **        deleted row in the child table. If such a row is not found, 
       
 72168 **        decrement the counter.
       
 72169 **
       
 72170 **   D.2) For each FK for which the table is the parent table, search 
       
 72171 **        the child table for rows that correspond to the deleted row 
       
 72172 **        in the parent table. For each found increment the counter.
       
 72173 **
       
 72174 ** UPDATE operations:
       
 72175 **
       
 72176 **   An UPDATE command requires that all 4 steps above are taken, but only
       
 72177 **   for FK constraints for which the affected columns are actually 
       
 72178 **   modified (values must be compared at runtime).
       
 72179 **
       
 72180 ** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
       
 72181 ** This simplifies the implementation a bit.
       
 72182 **
       
 72183 ** For the purposes of immediate FK constraints, the OR REPLACE conflict
       
 72184 ** resolution is considered to delete rows before the new row is inserted.
       
 72185 ** If a delete caused by OR REPLACE violates an FK constraint, an exception
       
 72186 ** is thrown, even if the FK constraint would be satisfied after the new 
       
 72187 ** row is inserted.
       
 72188 **
       
 72189 ** Immediate constraints are usually handled similarly. The only difference 
       
 72190 ** is that the counter used is stored as part of each individual statement
       
 72191 ** object (struct Vdbe). If, after the statement has run, its immediate
       
 72192 ** constraint counter is greater than zero, it returns SQLITE_CONSTRAINT
       
 72193 ** and the statement transaction is rolled back. An exception is an INSERT
       
 72194 ** statement that inserts a single row only (no triggers). In this case,
       
 72195 ** instead of using a counter, an exception is thrown immediately if the
       
 72196 ** INSERT violates a foreign key constraint. This is necessary as such
       
 72197 ** an INSERT does not open a statement transaction.
       
 72198 **
       
 72199 ** TODO: How should dropping a table be handled? How should renaming a 
       
 72200 ** table be handled?
       
 72201 **
       
 72202 **
       
 72203 ** Query API Notes
       
 72204 ** ---------------
       
 72205 **
       
 72206 ** Before coding an UPDATE or DELETE row operation, the code-generator
       
 72207 ** for those two operations needs to know whether or not the operation
       
 72208 ** requires any FK processing and, if so, which columns of the original
       
 72209 ** row are required by the FK processing VDBE code (i.e. if FKs were
       
 72210 ** implemented using triggers, which of the old.* columns would be 
       
 72211 ** accessed). No information is required by the code-generator before
       
 72212 ** coding an INSERT operation. The functions used by the UPDATE/DELETE
       
 72213 ** generation code to query for this information are:
       
 72214 **
       
 72215 **   sqlite3FkRequired() - Test to see if FK processing is required.
       
 72216 **   sqlite3FkOldmask()  - Query for the set of required old.* columns.
       
 72217 **
       
 72218 **
       
 72219 ** Externally accessible module functions
       
 72220 ** --------------------------------------
       
 72221 **
       
 72222 **   sqlite3FkCheck()    - Check for foreign key violations.
       
 72223 **   sqlite3FkActions()  - Code triggers for ON UPDATE/ON DELETE actions.
       
 72224 **   sqlite3FkDelete()   - Delete an FKey structure.
       
 72225 */
       
 72226 
       
 72227 /*
       
 72228 ** VDBE Calling Convention
       
 72229 ** -----------------------
       
 72230 **
       
 72231 ** Example:
       
 72232 **
       
 72233 **   For the following INSERT statement:
       
 72234 **
       
 72235 **     CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
       
 72236 **     INSERT INTO t1 VALUES(1, 2, 3.1);
       
 72237 **
       
 72238 **   Register (x):        2    (type integer)
       
 72239 **   Register (x+1):      1    (type integer)
       
 72240 **   Register (x+2):      NULL (type NULL)
       
 72241 **   Register (x+3):      3.1  (type real)
       
 72242 */
       
 72243 
       
 72244 /*
       
 72245 ** A foreign key constraint requires that the key columns in the parent
       
 72246 ** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
       
 72247 ** Given that pParent is the parent table for foreign key constraint pFKey, 
       
 72248 ** search the schema a unique index on the parent key columns. 
       
 72249 **
       
 72250 ** If successful, zero is returned. If the parent key is an INTEGER PRIMARY 
       
 72251 ** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx 
       
 72252 ** is set to point to the unique index. 
       
 72253 ** 
       
 72254 ** If the parent key consists of a single column (the foreign key constraint
       
 72255 ** is not a composite foreign key), output variable *paiCol is set to NULL.
       
 72256 ** Otherwise, it is set to point to an allocated array of size N, where
       
 72257 ** N is the number of columns in the parent key. The first element of the
       
 72258 ** array is the index of the child table column that is mapped by the FK
       
 72259 ** constraint to the parent table column stored in the left-most column
       
 72260 ** of index *ppIdx. The second element of the array is the index of the
       
 72261 ** child table column that corresponds to the second left-most column of
       
 72262 ** *ppIdx, and so on.
       
 72263 **
       
 72264 ** If the required index cannot be found, either because:
       
 72265 **
       
 72266 **   1) The named parent key columns do not exist, or
       
 72267 **
       
 72268 **   2) The named parent key columns do exist, but are not subject to a
       
 72269 **      UNIQUE or PRIMARY KEY constraint, or
       
 72270 **
       
 72271 **   3) No parent key columns were provided explicitly as part of the
       
 72272 **      foreign key definition, and the parent table does not have a
       
 72273 **      PRIMARY KEY, or
       
 72274 **
       
 72275 **   4) No parent key columns were provided explicitly as part of the
       
 72276 **      foreign key definition, and the PRIMARY KEY of the parent table 
       
 72277 **      consists of a a different number of columns to the child key in 
       
 72278 **      the child table.
       
 72279 **
       
 72280 ** then non-zero is returned, and a "foreign key mismatch" error loaded
       
 72281 ** into pParse. If an OOM error occurs, non-zero is returned and the
       
 72282 ** pParse->db->mallocFailed flag is set.
       
 72283 */
       
 72284 static int locateFkeyIndex(
       
 72285   Parse *pParse,                  /* Parse context to store any error in */
       
 72286   Table *pParent,                 /* Parent table of FK constraint pFKey */
       
 72287   FKey *pFKey,                    /* Foreign key to find index for */
       
 72288   Index **ppIdx,                  /* OUT: Unique index on parent table */
       
 72289   int **paiCol                    /* OUT: Map of index columns in pFKey */
       
 72290 ){
       
 72291   Index *pIdx = 0;                    /* Value to return via *ppIdx */
       
 72292   int *aiCol = 0;                     /* Value to return via *paiCol */
       
 72293   int nCol = pFKey->nCol;             /* Number of columns in parent key */
       
 72294   char *zKey = pFKey->aCol[0].zCol;   /* Name of left-most parent key column */
       
 72295 
       
 72296   /* The caller is responsible for zeroing output parameters. */
       
 72297   assert( ppIdx && *ppIdx==0 );
       
 72298   assert( !paiCol || *paiCol==0 );
       
 72299   assert( pParse );
       
 72300 
       
 72301   /* If this is a non-composite (single column) foreign key, check if it 
       
 72302   ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx 
       
 72303   ** and *paiCol set to zero and return early. 
       
 72304   **
       
 72305   ** Otherwise, for a composite foreign key (more than one column), allocate
       
 72306   ** space for the aiCol array (returned via output parameter *paiCol).
       
 72307   ** Non-composite foreign keys do not require the aiCol array.
       
 72308   */
       
 72309   if( nCol==1 ){
       
 72310     /* The FK maps to the IPK if any of the following are true:
       
 72311     **
       
 72312     **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly 
       
 72313     **      mapped to the primary key of table pParent, or
       
 72314     **   2) The FK is explicitly mapped to a column declared as INTEGER
       
 72315     **      PRIMARY KEY.
       
 72316     */
       
 72317     if( pParent->iPKey>=0 ){
       
 72318       if( !zKey ) return 0;
       
 72319       if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
       
 72320     }
       
 72321   }else if( paiCol ){
       
 72322     assert( nCol>1 );
       
 72323     aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
       
 72324     if( !aiCol ) return 1;
       
 72325     *paiCol = aiCol;
       
 72326   }
       
 72327 
       
 72328   for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
       
 72329     if( pIdx->nColumn==nCol && pIdx->onError!=OE_None ){ 
       
 72330       /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
       
 72331       ** of columns. If each indexed column corresponds to a foreign key
       
 72332       ** column of pFKey, then this index is a winner.  */
       
 72333 
       
 72334       if( zKey==0 ){
       
 72335         /* If zKey is NULL, then this foreign key is implicitly mapped to 
       
 72336         ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be 
       
 72337         ** identified by the test (Index.autoIndex==2).  */
       
 72338         if( pIdx->autoIndex==2 ){
       
 72339           if( aiCol ){
       
 72340             int i;
       
 72341             for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
       
 72342           }
       
 72343           break;
       
 72344         }
       
 72345       }else{
       
 72346         /* If zKey is non-NULL, then this foreign key was declared to
       
 72347         ** map to an explicit list of columns in table pParent. Check if this
       
 72348         ** index matches those columns. Also, check that the index uses
       
 72349         ** the default collation sequences for each column. */
       
 72350         int i, j;
       
 72351         for(i=0; i<nCol; i++){
       
 72352           int iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
       
 72353           char *zDfltColl;                  /* Def. collation for column */
       
 72354           char *zIdxCol;                    /* Name of indexed column */
       
 72355 
       
 72356           /* If the index uses a collation sequence that is different from
       
 72357           ** the default collation sequence for the column, this index is
       
 72358           ** unusable. Bail out early in this case.  */
       
 72359           zDfltColl = pParent->aCol[iCol].zColl;
       
 72360           if( !zDfltColl ){
       
 72361             zDfltColl = "BINARY";
       
 72362           }
       
 72363           if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
       
 72364 
       
 72365           zIdxCol = pParent->aCol[iCol].zName;
       
 72366           for(j=0; j<nCol; j++){
       
 72367             if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
       
 72368               if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
       
 72369               break;
       
 72370             }
       
 72371           }
       
 72372           if( j==nCol ) break;
       
 72373         }
       
 72374         if( i==nCol ) break;      /* pIdx is usable */
       
 72375       }
       
 72376     }
       
 72377   }
       
 72378 
       
 72379   if( !pIdx ){
       
 72380     if( !pParse->disableTriggers ){
       
 72381       sqlite3ErrorMsg(pParse, "foreign key mismatch");
       
 72382     }
       
 72383     sqlite3DbFree(pParse->db, aiCol);
       
 72384     return 1;
       
 72385   }
       
 72386 
       
 72387   *ppIdx = pIdx;
       
 72388   return 0;
       
 72389 }
       
 72390 
       
 72391 /*
       
 72392 ** This function is called when a row is inserted into or deleted from the 
       
 72393 ** child table of foreign key constraint pFKey. If an SQL UPDATE is executed 
       
 72394 ** on the child table of pFKey, this function is invoked twice for each row
       
 72395 ** affected - once to "delete" the old row, and then again to "insert" the
       
 72396 ** new row.
       
 72397 **
       
 72398 ** Each time it is called, this function generates VDBE code to locate the
       
 72399 ** row in the parent table that corresponds to the row being inserted into 
       
 72400 ** or deleted from the child table. If the parent row can be found, no 
       
 72401 ** special action is taken. Otherwise, if the parent row can *not* be
       
 72402 ** found in the parent table:
       
 72403 **
       
 72404 **   Operation | FK type   | Action taken
       
 72405 **   --------------------------------------------------------------------------
       
 72406 **   INSERT      immediate   Increment the "immediate constraint counter".
       
 72407 **
       
 72408 **   DELETE      immediate   Decrement the "immediate constraint counter".
       
 72409 **
       
 72410 **   INSERT      deferred    Increment the "deferred constraint counter".
       
 72411 **
       
 72412 **   DELETE      deferred    Decrement the "deferred constraint counter".
       
 72413 **
       
 72414 ** These operations are identified in the comment at the top of this file 
       
 72415 ** (fkey.c) as "I.1" and "D.1".
       
 72416 */
       
 72417 static void fkLookupParent(
       
 72418   Parse *pParse,        /* Parse context */
       
 72419   int iDb,              /* Index of database housing pTab */
       
 72420   Table *pTab,          /* Parent table of FK pFKey */
       
 72421   Index *pIdx,          /* Unique index on parent key columns in pTab */
       
 72422   FKey *pFKey,          /* Foreign key constraint */
       
 72423   int *aiCol,           /* Map from parent key columns to child table columns */
       
 72424   int regData,          /* Address of array containing child table row */
       
 72425   int nIncr,            /* Increment constraint counter by this */
       
 72426   int isIgnore          /* If true, pretend pTab contains all NULL values */
       
 72427 ){
       
 72428   int i;                                    /* Iterator variable */
       
 72429   Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
       
 72430   int iCur = pParse->nTab - 1;              /* Cursor number to use */
       
 72431   int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
       
 72432 
       
 72433   /* If nIncr is less than zero, then check at runtime if there are any
       
 72434   ** outstanding constraints to resolve. If there are not, there is no need
       
 72435   ** to check if deleting this row resolves any outstanding violations.
       
 72436   **
       
 72437   ** Check if any of the key columns in the child table row are NULL. If 
       
 72438   ** any are, then the constraint is considered satisfied. No need to 
       
 72439   ** search for a matching row in the parent table.  */
       
 72440   if( nIncr<0 ){
       
 72441     sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
       
 72442   }
       
 72443   for(i=0; i<pFKey->nCol; i++){
       
 72444     int iReg = aiCol[i] + regData + 1;
       
 72445     sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk);
       
 72446   }
       
 72447 
       
 72448   if( isIgnore==0 ){
       
 72449     if( pIdx==0 ){
       
 72450       /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
       
 72451       ** column of the parent table (table pTab).  */
       
 72452       int iMustBeInt;               /* Address of MustBeInt instruction */
       
 72453       int regTemp = sqlite3GetTempReg(pParse);
       
 72454   
       
 72455       /* Invoke MustBeInt to coerce the child key value to an integer (i.e. 
       
 72456       ** apply the affinity of the parent key). If this fails, then there
       
 72457       ** is no matching parent key. Before using MustBeInt, make a copy of
       
 72458       ** the value. Otherwise, the value inserted into the child key column
       
 72459       ** will have INTEGER affinity applied to it, which may not be correct.  */
       
 72460       sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
       
 72461       iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
       
 72462   
       
 72463       /* If the parent table is the same as the child table, and we are about
       
 72464       ** to increment the constraint-counter (i.e. this is an INSERT operation),
       
 72465       ** then check if the row being inserted matches itself. If so, do not
       
 72466       ** increment the constraint-counter.  */
       
 72467       if( pTab==pFKey->pFrom && nIncr==1 ){
       
 72468         sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp);
       
 72469       }
       
 72470   
       
 72471       sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
       
 72472       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp);
       
 72473       sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
       
 72474       sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
       
 72475       sqlite3VdbeJumpHere(v, iMustBeInt);
       
 72476       sqlite3ReleaseTempReg(pParse, regTemp);
       
 72477     }else{
       
 72478       int nCol = pFKey->nCol;
       
 72479       int regTemp = sqlite3GetTempRange(pParse, nCol);
       
 72480       int regRec = sqlite3GetTempReg(pParse);
       
 72481       KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
       
 72482   
       
 72483       sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
       
 72484       sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
       
 72485       for(i=0; i<nCol; i++){
       
 72486         sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[i]+1+regData, regTemp+i);
       
 72487       }
       
 72488   
       
 72489       /* If the parent table is the same as the child table, and we are about
       
 72490       ** to increment the constraint-counter (i.e. this is an INSERT operation),
       
 72491       ** then check if the row being inserted matches itself. If so, do not
       
 72492       ** increment the constraint-counter.  */
       
 72493       if( pTab==pFKey->pFrom && nIncr==1 ){
       
 72494         int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
       
 72495         for(i=0; i<nCol; i++){
       
 72496           int iChild = aiCol[i]+1+regData;
       
 72497           int iParent = pIdx->aiColumn[i]+1+regData;
       
 72498           sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent);
       
 72499         }
       
 72500         sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
       
 72501       }
       
 72502   
       
 72503       sqlite3VdbeAddOp3(v, OP_MakeRecord, regTemp, nCol, regRec);
       
 72504       sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0);
       
 72505       sqlite3VdbeAddOp3(v, OP_Found, iCur, iOk, regRec);
       
 72506   
       
 72507       sqlite3ReleaseTempReg(pParse, regRec);
       
 72508       sqlite3ReleaseTempRange(pParse, regTemp, nCol);
       
 72509     }
       
 72510   }
       
 72511 
       
 72512   if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
       
 72513     /* Special case: If this is an INSERT statement that will insert exactly
       
 72514     ** one row into the table, raise a constraint immediately instead of
       
 72515     ** incrementing a counter. This is necessary as the VM code is being
       
 72516     ** generated for will not open a statement transaction.  */
       
 72517     assert( nIncr==1 );
       
 72518     sqlite3HaltConstraint(
       
 72519         pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
       
 72520     );
       
 72521   }else{
       
 72522     if( nIncr>0 && pFKey->isDeferred==0 ){
       
 72523       sqlite3ParseToplevel(pParse)->mayAbort = 1;
       
 72524     }
       
 72525     sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
       
 72526   }
       
 72527 
       
 72528   sqlite3VdbeResolveLabel(v, iOk);
       
 72529   sqlite3VdbeAddOp1(v, OP_Close, iCur);
       
 72530 }
       
 72531 
       
 72532 /*
       
 72533 ** This function is called to generate code executed when a row is deleted
       
 72534 ** from the parent table of foreign key constraint pFKey and, if pFKey is 
       
 72535 ** deferred, when a row is inserted into the same table. When generating
       
 72536 ** code for an SQL UPDATE operation, this function may be called twice -
       
 72537 ** once to "delete" the old row and once to "insert" the new row.
       
 72538 **
       
 72539 ** The code generated by this function scans through the rows in the child
       
 72540 ** table that correspond to the parent table row being deleted or inserted.
       
 72541 ** For each child row found, one of the following actions is taken:
       
 72542 **
       
 72543 **   Operation | FK type   | Action taken
       
 72544 **   --------------------------------------------------------------------------
       
 72545 **   DELETE      immediate   Increment the "immediate constraint counter".
       
 72546 **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
       
 72547 **                           throw a "foreign key constraint failed" exception.
       
 72548 **
       
 72549 **   INSERT      immediate   Decrement the "immediate constraint counter".
       
 72550 **
       
 72551 **   DELETE      deferred    Increment the "deferred constraint counter".
       
 72552 **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
       
 72553 **                           throw a "foreign key constraint failed" exception.
       
 72554 **
       
 72555 **   INSERT      deferred    Decrement the "deferred constraint counter".
       
 72556 **
       
 72557 ** These operations are identified in the comment at the top of this file 
       
 72558 ** (fkey.c) as "I.2" and "D.2".
       
 72559 */
       
 72560 static void fkScanChildren(
       
 72561   Parse *pParse,                  /* Parse context */
       
 72562   SrcList *pSrc,                  /* SrcList containing the table to scan */
       
 72563   Table *pTab,
       
 72564   Index *pIdx,                    /* Foreign key index */
       
 72565   FKey *pFKey,                    /* Foreign key relationship */
       
 72566   int *aiCol,                     /* Map from pIdx cols to child table cols */
       
 72567   int regData,                    /* Referenced table data starts here */
       
 72568   int nIncr                       /* Amount to increment deferred counter by */
       
 72569 ){
       
 72570   sqlite3 *db = pParse->db;       /* Database handle */
       
 72571   int i;                          /* Iterator variable */
       
 72572   Expr *pWhere = 0;               /* WHERE clause to scan with */
       
 72573   NameContext sNameContext;       /* Context used to resolve WHERE clause */
       
 72574   WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
       
 72575   int iFkIfZero = 0;              /* Address of OP_FkIfZero */
       
 72576   Vdbe *v = sqlite3GetVdbe(pParse);
       
 72577 
       
 72578   assert( !pIdx || pIdx->pTable==pTab );
       
 72579 
       
 72580   if( nIncr<0 ){
       
 72581     iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
       
 72582   }
       
 72583 
       
 72584   /* Create an Expr object representing an SQL expression like:
       
 72585   **
       
 72586   **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
       
 72587   **
       
 72588   ** The collation sequence used for the comparison should be that of
       
 72589   ** the parent key columns. The affinity of the parent key column should
       
 72590   ** be applied to each child key value before the comparison takes place.
       
 72591   */
       
 72592   for(i=0; i<pFKey->nCol; i++){
       
 72593     Expr *pLeft;                  /* Value from parent table row */
       
 72594     Expr *pRight;                 /* Column ref to child table */
       
 72595     Expr *pEq;                    /* Expression (pLeft = pRight) */
       
 72596     int iCol;                     /* Index of column in child table */ 
       
 72597     const char *zCol;             /* Name of column in child table */
       
 72598 
       
 72599     pLeft = sqlite3Expr(db, TK_REGISTER, 0);
       
 72600     if( pLeft ){
       
 72601       /* Set the collation sequence and affinity of the LHS of each TK_EQ
       
 72602       ** expression to the parent key column defaults.  */
       
 72603       if( pIdx ){
       
 72604         Column *pCol;
       
 72605         iCol = pIdx->aiColumn[i];
       
 72606         pCol = &pIdx->pTable->aCol[iCol];
       
 72607         pLeft->iTable = regData+iCol+1;
       
 72608         pLeft->affinity = pCol->affinity;
       
 72609         pLeft->pColl = sqlite3LocateCollSeq(pParse, pCol->zColl);
       
 72610       }else{
       
 72611         pLeft->iTable = regData;
       
 72612         pLeft->affinity = SQLITE_AFF_INTEGER;
       
 72613       }
       
 72614     }
       
 72615     iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
       
 72616     assert( iCol>=0 );
       
 72617     zCol = pFKey->pFrom->aCol[iCol].zName;
       
 72618     pRight = sqlite3Expr(db, TK_ID, zCol);
       
 72619     pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
       
 72620     pWhere = sqlite3ExprAnd(db, pWhere, pEq);
       
 72621   }
       
 72622 
       
 72623   /* If the child table is the same as the parent table, and this scan
       
 72624   ** is taking place as part of a DELETE operation (operation D.2), omit the
       
 72625   ** row being deleted from the scan by adding ($rowid != rowid) to the WHERE 
       
 72626   ** clause, where $rowid is the rowid of the row being deleted.  */
       
 72627   if( pTab==pFKey->pFrom && nIncr>0 ){
       
 72628     Expr *pEq;                    /* Expression (pLeft = pRight) */
       
 72629     Expr *pLeft;                  /* Value from parent table row */
       
 72630     Expr *pRight;                 /* Column ref to child table */
       
 72631     pLeft = sqlite3Expr(db, TK_REGISTER, 0);
       
 72632     pRight = sqlite3Expr(db, TK_COLUMN, 0);
       
 72633     if( pLeft && pRight ){
       
 72634       pLeft->iTable = regData;
       
 72635       pLeft->affinity = SQLITE_AFF_INTEGER;
       
 72636       pRight->iTable = pSrc->a[0].iCursor;
       
 72637       pRight->iColumn = -1;
       
 72638     }
       
 72639     pEq = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
       
 72640     pWhere = sqlite3ExprAnd(db, pWhere, pEq);
       
 72641   }
       
 72642 
       
 72643   /* Resolve the references in the WHERE clause. */
       
 72644   memset(&sNameContext, 0, sizeof(NameContext));
       
 72645   sNameContext.pSrcList = pSrc;
       
 72646   sNameContext.pParse = pParse;
       
 72647   sqlite3ResolveExprNames(&sNameContext, pWhere);
       
 72648 
       
 72649   /* Create VDBE to loop through the entries in pSrc that match the WHERE
       
 72650   ** clause. If the constraint is not deferred, throw an exception for
       
 72651   ** each row found. Otherwise, for deferred constraints, increment the
       
 72652   ** deferred constraint counter by nIncr for each row selected.  */
       
 72653   pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0);
       
 72654   if( nIncr>0 && pFKey->isDeferred==0 ){
       
 72655     sqlite3ParseToplevel(pParse)->mayAbort = 1;
       
 72656   }
       
 72657   sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
       
 72658   if( pWInfo ){
       
 72659     sqlite3WhereEnd(pWInfo);
       
 72660   }
       
 72661 
       
 72662   /* Clean up the WHERE clause constructed above. */
       
 72663   sqlite3ExprDelete(db, pWhere);
       
 72664   if( iFkIfZero ){
       
 72665     sqlite3VdbeJumpHere(v, iFkIfZero);
       
 72666   }
       
 72667 }
       
 72668 
       
 72669 /*
       
 72670 ** This function returns a pointer to the head of a linked list of FK
       
 72671 ** constraints for which table pTab is the parent table. For example,
       
 72672 ** given the following schema:
       
 72673 **
       
 72674 **   CREATE TABLE t1(a PRIMARY KEY);
       
 72675 **   CREATE TABLE t2(b REFERENCES t1(a);
       
 72676 **
       
 72677 ** Calling this function with table "t1" as an argument returns a pointer
       
 72678 ** to the FKey structure representing the foreign key constraint on table
       
 72679 ** "t2". Calling this function with "t2" as the argument would return a
       
 72680 ** NULL pointer (as there are no FK constraints for which t2 is the parent
       
 72681 ** table).
       
 72682 */
       
 72683 SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
       
 72684   int nName = sqlite3Strlen30(pTab->zName);
       
 72685   return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName, nName);
       
 72686 }
       
 72687 
       
 72688 /*
       
 72689 ** The second argument is a Trigger structure allocated by the 
       
 72690 ** fkActionTrigger() routine. This function deletes the Trigger structure
       
 72691 ** and all of its sub-components.
       
 72692 **
       
 72693 ** The Trigger structure or any of its sub-components may be allocated from
       
 72694 ** the lookaside buffer belonging to database handle dbMem.
       
 72695 */
       
 72696 static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
       
 72697   if( p ){
       
 72698     TriggerStep *pStep = p->step_list;
       
 72699     sqlite3ExprDelete(dbMem, pStep->pWhere);
       
 72700     sqlite3ExprListDelete(dbMem, pStep->pExprList);
       
 72701     sqlite3SelectDelete(dbMem, pStep->pSelect);
       
 72702     sqlite3ExprDelete(dbMem, p->pWhen);
       
 72703     sqlite3DbFree(dbMem, p);
       
 72704   }
       
 72705 }
       
 72706 
       
 72707 /*
       
 72708 ** This function is called to generate code that runs when table pTab is
       
 72709 ** being dropped from the database. The SrcList passed as the second argument
       
 72710 ** to this function contains a single entry guaranteed to resolve to
       
 72711 ** table pTab.
       
 72712 **
       
 72713 ** Normally, no code is required. However, if either
       
 72714 **
       
 72715 **   (a) The table is the parent table of a FK constraint, or
       
 72716 **   (b) The table is the child table of a deferred FK constraint and it is
       
 72717 **       determined at runtime that there are outstanding deferred FK 
       
 72718 **       constraint violations in the database,
       
 72719 **
       
 72720 ** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
       
 72721 ** the table from the database. Triggers are disabled while running this
       
 72722 ** DELETE, but foreign key actions are not.
       
 72723 */
       
 72724 SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
       
 72725   sqlite3 *db = pParse->db;
       
 72726   if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
       
 72727     int iSkip = 0;
       
 72728     Vdbe *v = sqlite3GetVdbe(pParse);
       
 72729 
       
 72730     assert( v );                  /* VDBE has already been allocated */
       
 72731     if( sqlite3FkReferences(pTab)==0 ){
       
 72732       /* Search for a deferred foreign key constraint for which this table
       
 72733       ** is the child table. If one cannot be found, return without 
       
 72734       ** generating any VDBE code. If one can be found, then jump over
       
 72735       ** the entire DELETE if there are no outstanding deferred constraints
       
 72736       ** when this statement is run.  */
       
 72737       FKey *p;
       
 72738       for(p=pTab->pFKey; p; p=p->pNextFrom){
       
 72739         if( p->isDeferred ) break;
       
 72740       }
       
 72741       if( !p ) return;
       
 72742       iSkip = sqlite3VdbeMakeLabel(v);
       
 72743       sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip);
       
 72744     }
       
 72745 
       
 72746     pParse->disableTriggers = 1;
       
 72747     sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
       
 72748     pParse->disableTriggers = 0;
       
 72749 
       
 72750     /* If the DELETE has generated immediate foreign key constraint 
       
 72751     ** violations, halt the VDBE and return an error at this point, before
       
 72752     ** any modifications to the schema are made. This is because statement
       
 72753     ** transactions are not able to rollback schema changes.  */
       
 72754     sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
       
 72755     sqlite3HaltConstraint(
       
 72756         pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
       
 72757     );
       
 72758 
       
 72759     if( iSkip ){
       
 72760       sqlite3VdbeResolveLabel(v, iSkip);
       
 72761     }
       
 72762   }
       
 72763 }
       
 72764 
       
 72765 /*
       
 72766 ** This function is called when inserting, deleting or updating a row of
       
 72767 ** table pTab to generate VDBE code to perform foreign key constraint 
       
 72768 ** processing for the operation.
       
 72769 **
       
 72770 ** For a DELETE operation, parameter regOld is passed the index of the
       
 72771 ** first register in an array of (pTab->nCol+1) registers containing the
       
 72772 ** rowid of the row being deleted, followed by each of the column values
       
 72773 ** of the row being deleted, from left to right. Parameter regNew is passed
       
 72774 ** zero in this case.
       
 72775 **
       
 72776 ** For an INSERT operation, regOld is passed zero and regNew is passed the
       
 72777 ** first register of an array of (pTab->nCol+1) registers containing the new
       
 72778 ** row data.
       
 72779 **
       
 72780 ** For an UPDATE operation, this function is called twice. Once before
       
 72781 ** the original record is deleted from the table using the calling convention
       
 72782 ** described for DELETE. Then again after the original record is deleted
       
 72783 ** but before the new record is inserted using the INSERT convention. 
       
 72784 */
       
 72785 SQLITE_PRIVATE void sqlite3FkCheck(
       
 72786   Parse *pParse,                  /* Parse context */
       
 72787   Table *pTab,                    /* Row is being deleted from this table */ 
       
 72788   int regOld,                     /* Previous row data is stored here */
       
 72789   int regNew                      /* New row data is stored here */
       
 72790 ){
       
 72791   sqlite3 *db = pParse->db;       /* Database handle */
       
 72792   Vdbe *v;                        /* VM to write code to */
       
 72793   FKey *pFKey;                    /* Used to iterate through FKs */
       
 72794   int iDb;                        /* Index of database containing pTab */
       
 72795   const char *zDb;                /* Name of database containing pTab */
       
 72796   int isIgnoreErrors = pParse->disableTriggers;
       
 72797 
       
 72798   /* Exactly one of regOld and regNew should be non-zero. */
       
 72799   assert( (regOld==0)!=(regNew==0) );
       
 72800 
       
 72801   /* If foreign-keys are disabled, this function is a no-op. */
       
 72802   if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
       
 72803 
       
 72804   v = sqlite3GetVdbe(pParse);
       
 72805   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       
 72806   zDb = db->aDb[iDb].zName;
       
 72807 
       
 72808   /* Loop through all the foreign key constraints for which pTab is the
       
 72809   ** child table (the table that the foreign key definition is part of).  */
       
 72810   for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
       
 72811     Table *pTo;                   /* Parent table of foreign key pFKey */
       
 72812     Index *pIdx = 0;              /* Index on key columns in pTo */
       
 72813     int *aiFree = 0;
       
 72814     int *aiCol;
       
 72815     int iCol;
       
 72816     int i;
       
 72817     int isIgnore = 0;
       
 72818 
       
 72819     /* Find the parent table of this foreign key. Also find a unique index 
       
 72820     ** on the parent key columns in the parent table. If either of these 
       
 72821     ** schema items cannot be located, set an error in pParse and return 
       
 72822     ** early.  */
       
 72823     if( pParse->disableTriggers ){
       
 72824       pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
       
 72825     }else{
       
 72826       pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
       
 72827     }
       
 72828     if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
       
 72829       if( !isIgnoreErrors || db->mallocFailed ) return;
       
 72830       continue;
       
 72831     }
       
 72832     assert( pFKey->nCol==1 || (aiFree && pIdx) );
       
 72833 
       
 72834     if( aiFree ){
       
 72835       aiCol = aiFree;
       
 72836     }else{
       
 72837       iCol = pFKey->aCol[0].iFrom;
       
 72838       aiCol = &iCol;
       
 72839     }
       
 72840     for(i=0; i<pFKey->nCol; i++){
       
 72841       if( aiCol[i]==pTab->iPKey ){
       
 72842         aiCol[i] = -1;
       
 72843       }
       
 72844 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 72845       /* Request permission to read the parent key columns. If the 
       
 72846       ** authorization callback returns SQLITE_IGNORE, behave as if any
       
 72847       ** values read from the parent table are NULL. */
       
 72848       if( db->xAuth ){
       
 72849         int rcauth;
       
 72850         char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
       
 72851         rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
       
 72852         isIgnore = (rcauth==SQLITE_IGNORE);
       
 72853       }
       
 72854 #endif
       
 72855     }
       
 72856 
       
 72857     /* Take a shared-cache advisory read-lock on the parent table. Allocate 
       
 72858     ** a cursor to use to search the unique index on the parent key columns 
       
 72859     ** in the parent table.  */
       
 72860     sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
       
 72861     pParse->nTab++;
       
 72862 
       
 72863     if( regOld!=0 ){
       
 72864       /* A row is being removed from the child table. Search for the parent.
       
 72865       ** If the parent does not exist, removing the child row resolves an 
       
 72866       ** outstanding foreign key constraint violation. */
       
 72867       fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1,isIgnore);
       
 72868     }
       
 72869     if( regNew!=0 ){
       
 72870       /* A row is being added to the child table. If a parent row cannot
       
 72871       ** be found, adding the child row has violated the FK constraint. */ 
       
 72872       fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore);
       
 72873     }
       
 72874 
       
 72875     sqlite3DbFree(db, aiFree);
       
 72876   }
       
 72877 
       
 72878   /* Loop through all the foreign key constraints that refer to this table */
       
 72879   for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
       
 72880     Index *pIdx = 0;              /* Foreign key index for pFKey */
       
 72881     SrcList *pSrc;
       
 72882     int *aiCol = 0;
       
 72883 
       
 72884     if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
       
 72885       assert( regOld==0 && regNew!=0 );
       
 72886       /* Inserting a single row into a parent table cannot cause an immediate
       
 72887       ** foreign key violation. So do nothing in this case.  */
       
 72888       continue;
       
 72889     }
       
 72890 
       
 72891     if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
       
 72892       if( !isIgnoreErrors || db->mallocFailed ) return;
       
 72893       continue;
       
 72894     }
       
 72895     assert( aiCol || pFKey->nCol==1 );
       
 72896 
       
 72897     /* Create a SrcList structure containing a single table (the table 
       
 72898     ** the foreign key that refers to this table is attached to). This
       
 72899     ** is required for the sqlite3WhereXXX() interface.  */
       
 72900     pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
       
 72901     if( pSrc ){
       
 72902       struct SrcList_item *pItem = pSrc->a;
       
 72903       pItem->pTab = pFKey->pFrom;
       
 72904       pItem->zName = pFKey->pFrom->zName;
       
 72905       pItem->pTab->nRef++;
       
 72906       pItem->iCursor = pParse->nTab++;
       
 72907   
       
 72908       if( regNew!=0 ){
       
 72909         fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
       
 72910       }
       
 72911       if( regOld!=0 ){
       
 72912         /* If there is a RESTRICT action configured for the current operation
       
 72913         ** on the parent table of this FK, then throw an exception 
       
 72914         ** immediately if the FK constraint is violated, even if this is a
       
 72915         ** deferred trigger. That's what RESTRICT means. To defer checking
       
 72916         ** the constraint, the FK should specify NO ACTION (represented
       
 72917         ** using OE_None). NO ACTION is the default.  */
       
 72918         fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
       
 72919       }
       
 72920       pItem->zName = 0;
       
 72921       sqlite3SrcListDelete(db, pSrc);
       
 72922     }
       
 72923     sqlite3DbFree(db, aiCol);
       
 72924   }
       
 72925 }
       
 72926 
       
 72927 #define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
       
 72928 
       
 72929 /*
       
 72930 ** This function is called before generating code to update or delete a 
       
 72931 ** row contained in table pTab.
       
 72932 */
       
 72933 SQLITE_PRIVATE u32 sqlite3FkOldmask(
       
 72934   Parse *pParse,                  /* Parse context */
       
 72935   Table *pTab                     /* Table being modified */
       
 72936 ){
       
 72937   u32 mask = 0;
       
 72938   if( pParse->db->flags&SQLITE_ForeignKeys ){
       
 72939     FKey *p;
       
 72940     int i;
       
 72941     for(p=pTab->pFKey; p; p=p->pNextFrom){
       
 72942       for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
       
 72943     }
       
 72944     for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
       
 72945       Index *pIdx = 0;
       
 72946       locateFkeyIndex(pParse, pTab, p, &pIdx, 0);
       
 72947       if( pIdx ){
       
 72948         for(i=0; i<pIdx->nColumn; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
       
 72949       }
       
 72950     }
       
 72951   }
       
 72952   return mask;
       
 72953 }
       
 72954 
       
 72955 /*
       
 72956 ** This function is called before generating code to update or delete a 
       
 72957 ** row contained in table pTab. If the operation is a DELETE, then
       
 72958 ** parameter aChange is passed a NULL value. For an UPDATE, aChange points
       
 72959 ** to an array of size N, where N is the number of columns in table pTab.
       
 72960 ** If the i'th column is not modified by the UPDATE, then the corresponding 
       
 72961 ** entry in the aChange[] array is set to -1. If the column is modified,
       
 72962 ** the value is 0 or greater. Parameter chngRowid is set to true if the
       
 72963 ** UPDATE statement modifies the rowid fields of the table.
       
 72964 **
       
 72965 ** If any foreign key processing will be required, this function returns
       
 72966 ** true. If there is no foreign key related processing, this function 
       
 72967 ** returns false.
       
 72968 */
       
 72969 SQLITE_PRIVATE int sqlite3FkRequired(
       
 72970   Parse *pParse,                  /* Parse context */
       
 72971   Table *pTab,                    /* Table being modified */
       
 72972   int *aChange,                   /* Non-NULL for UPDATE operations */
       
 72973   int chngRowid                   /* True for UPDATE that affects rowid */
       
 72974 ){
       
 72975   if( pParse->db->flags&SQLITE_ForeignKeys ){
       
 72976     if( !aChange ){
       
 72977       /* A DELETE operation. Foreign key processing is required if the 
       
 72978       ** table in question is either the child or parent table for any 
       
 72979       ** foreign key constraint.  */
       
 72980       return (sqlite3FkReferences(pTab) || pTab->pFKey);
       
 72981     }else{
       
 72982       /* This is an UPDATE. Foreign key processing is only required if the
       
 72983       ** operation modifies one or more child or parent key columns. */
       
 72984       int i;
       
 72985       FKey *p;
       
 72986 
       
 72987       /* Check if any child key columns are being modified. */
       
 72988       for(p=pTab->pFKey; p; p=p->pNextFrom){
       
 72989         for(i=0; i<p->nCol; i++){
       
 72990           int iChildKey = p->aCol[i].iFrom;
       
 72991           if( aChange[iChildKey]>=0 ) return 1;
       
 72992           if( iChildKey==pTab->iPKey && chngRowid ) return 1;
       
 72993         }
       
 72994       }
       
 72995 
       
 72996       /* Check if any parent key columns are being modified. */
       
 72997       for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
       
 72998         for(i=0; i<p->nCol; i++){
       
 72999           char *zKey = p->aCol[i].zCol;
       
 73000           int iKey;
       
 73001           for(iKey=0; iKey<pTab->nCol; iKey++){
       
 73002             Column *pCol = &pTab->aCol[iKey];
       
 73003             if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey) : pCol->isPrimKey) ){
       
 73004               if( aChange[iKey]>=0 ) return 1;
       
 73005               if( iKey==pTab->iPKey && chngRowid ) return 1;
       
 73006             }
       
 73007           }
       
 73008         }
       
 73009       }
       
 73010     }
       
 73011   }
       
 73012   return 0;
       
 73013 }
       
 73014 
       
 73015 /*
       
 73016 ** This function is called when an UPDATE or DELETE operation is being 
       
 73017 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
       
 73018 ** If the current operation is an UPDATE, then the pChanges parameter is
       
 73019 ** passed a pointer to the list of columns being modified. If it is a
       
 73020 ** DELETE, pChanges is passed a NULL pointer.
       
 73021 **
       
 73022 ** It returns a pointer to a Trigger structure containing a trigger
       
 73023 ** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
       
 73024 ** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
       
 73025 ** returned (these actions require no special handling by the triggers
       
 73026 ** sub-system, code for them is created by fkScanChildren()).
       
 73027 **
       
 73028 ** For example, if pFKey is the foreign key and pTab is table "p" in 
       
 73029 ** the following schema:
       
 73030 **
       
 73031 **   CREATE TABLE p(pk PRIMARY KEY);
       
 73032 **   CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
       
 73033 **
       
 73034 ** then the returned trigger structure is equivalent to:
       
 73035 **
       
 73036 **   CREATE TRIGGER ... DELETE ON p BEGIN
       
 73037 **     DELETE FROM c WHERE ck = old.pk;
       
 73038 **   END;
       
 73039 **
       
 73040 ** The returned pointer is cached as part of the foreign key object. It
       
 73041 ** is eventually freed along with the rest of the foreign key object by 
       
 73042 ** sqlite3FkDelete().
       
 73043 */
       
 73044 static Trigger *fkActionTrigger(
       
 73045   Parse *pParse,                  /* Parse context */
       
 73046   Table *pTab,                    /* Table being updated or deleted from */
       
 73047   FKey *pFKey,                    /* Foreign key to get action for */
       
 73048   ExprList *pChanges              /* Change-list for UPDATE, NULL for DELETE */
       
 73049 ){
       
 73050   sqlite3 *db = pParse->db;       /* Database handle */
       
 73051   int action;                     /* One of OE_None, OE_Cascade etc. */
       
 73052   Trigger *pTrigger;              /* Trigger definition to return */
       
 73053   int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
       
 73054 
       
 73055   action = pFKey->aAction[iAction];
       
 73056   pTrigger = pFKey->apTrigger[iAction];
       
 73057 
       
 73058   if( action!=OE_None && !pTrigger ){
       
 73059     u8 enableLookaside;           /* Copy of db->lookaside.bEnabled */
       
 73060     char const *zFrom;            /* Name of child table */
       
 73061     int nFrom;                    /* Length in bytes of zFrom */
       
 73062     Index *pIdx = 0;              /* Parent key index for this FK */
       
 73063     int *aiCol = 0;               /* child table cols -> parent key cols */
       
 73064     TriggerStep *pStep = 0;        /* First (only) step of trigger program */
       
 73065     Expr *pWhere = 0;             /* WHERE clause of trigger step */
       
 73066     ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
       
 73067     Select *pSelect = 0;          /* If RESTRICT, "SELECT RAISE(...)" */
       
 73068     int i;                        /* Iterator variable */
       
 73069     Expr *pWhen = 0;              /* WHEN clause for the trigger */
       
 73070 
       
 73071     if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
       
 73072     assert( aiCol || pFKey->nCol==1 );
       
 73073 
       
 73074     for(i=0; i<pFKey->nCol; i++){
       
 73075       Token tOld = { "old", 3 };  /* Literal "old" token */
       
 73076       Token tNew = { "new", 3 };  /* Literal "new" token */
       
 73077       Token tFromCol;             /* Name of column in child table */
       
 73078       Token tToCol;               /* Name of column in parent table */
       
 73079       int iFromCol;               /* Idx of column in child table */
       
 73080       Expr *pEq;                  /* tFromCol = OLD.tToCol */
       
 73081 
       
 73082       iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
       
 73083       assert( iFromCol>=0 );
       
 73084       tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid";
       
 73085       tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
       
 73086 
       
 73087       tToCol.n = sqlite3Strlen30(tToCol.z);
       
 73088       tFromCol.n = sqlite3Strlen30(tFromCol.z);
       
 73089 
       
 73090       /* Create the expression "OLD.zToCol = zFromCol". It is important
       
 73091       ** that the "OLD.zToCol" term is on the LHS of the = operator, so
       
 73092       ** that the affinity and collation sequence associated with the
       
 73093       ** parent table are used for the comparison. */
       
 73094       pEq = sqlite3PExpr(pParse, TK_EQ,
       
 73095           sqlite3PExpr(pParse, TK_DOT, 
       
 73096             sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
       
 73097             sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
       
 73098           , 0),
       
 73099           sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol)
       
 73100       , 0);
       
 73101       pWhere = sqlite3ExprAnd(db, pWhere, pEq);
       
 73102 
       
 73103       /* For ON UPDATE, construct the next term of the WHEN clause.
       
 73104       ** The final WHEN clause will be like this:
       
 73105       **
       
 73106       **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
       
 73107       */
       
 73108       if( pChanges ){
       
 73109         pEq = sqlite3PExpr(pParse, TK_IS,
       
 73110             sqlite3PExpr(pParse, TK_DOT, 
       
 73111               sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
       
 73112               sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
       
 73113               0),
       
 73114             sqlite3PExpr(pParse, TK_DOT, 
       
 73115               sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
       
 73116               sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
       
 73117               0),
       
 73118             0);
       
 73119         pWhen = sqlite3ExprAnd(db, pWhen, pEq);
       
 73120       }
       
 73121   
       
 73122       if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
       
 73123         Expr *pNew;
       
 73124         if( action==OE_Cascade ){
       
 73125           pNew = sqlite3PExpr(pParse, TK_DOT, 
       
 73126             sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
       
 73127             sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
       
 73128           , 0);
       
 73129         }else if( action==OE_SetDflt ){
       
 73130           Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
       
 73131           if( pDflt ){
       
 73132             pNew = sqlite3ExprDup(db, pDflt, 0);
       
 73133           }else{
       
 73134             pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
       
 73135           }
       
 73136         }else{
       
 73137           pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
       
 73138         }
       
 73139         pList = sqlite3ExprListAppend(pParse, pList, pNew);
       
 73140         sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
       
 73141       }
       
 73142     }
       
 73143     sqlite3DbFree(db, aiCol);
       
 73144 
       
 73145     zFrom = pFKey->pFrom->zName;
       
 73146     nFrom = sqlite3Strlen30(zFrom);
       
 73147 
       
 73148     if( action==OE_Restrict ){
       
 73149       Token tFrom;
       
 73150       Expr *pRaise; 
       
 73151 
       
 73152       tFrom.z = zFrom;
       
 73153       tFrom.n = nFrom;
       
 73154       pRaise = sqlite3Expr(db, TK_RAISE, "foreign key constraint failed");
       
 73155       if( pRaise ){
       
 73156         pRaise->affinity = OE_Abort;
       
 73157       }
       
 73158       pSelect = sqlite3SelectNew(pParse, 
       
 73159           sqlite3ExprListAppend(pParse, 0, pRaise),
       
 73160           sqlite3SrcListAppend(db, 0, &tFrom, 0),
       
 73161           pWhere,
       
 73162           0, 0, 0, 0, 0, 0
       
 73163       );
       
 73164       pWhere = 0;
       
 73165     }
       
 73166 
       
 73167     /* In the current implementation, pTab->dbMem==0 for all tables except
       
 73168     ** for temporary tables used to describe subqueries.  And temporary
       
 73169     ** tables do not have foreign key constraints.  Hence, pTab->dbMem
       
 73170     ** should always be 0 there.
       
 73171     */
       
 73172     enableLookaside = db->lookaside.bEnabled;
       
 73173     db->lookaside.bEnabled = 0;
       
 73174 
       
 73175     pTrigger = (Trigger *)sqlite3DbMallocZero(db, 
       
 73176         sizeof(Trigger) +         /* struct Trigger */
       
 73177         sizeof(TriggerStep) +     /* Single step in trigger program */
       
 73178         nFrom + 1                 /* Space for pStep->target.z */
       
 73179     );
       
 73180     if( pTrigger ){
       
 73181       pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
       
 73182       pStep->target.z = (char *)&pStep[1];
       
 73183       pStep->target.n = nFrom;
       
 73184       memcpy((char *)pStep->target.z, zFrom, nFrom);
       
 73185   
       
 73186       pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
       
 73187       pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
       
 73188       pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
       
 73189       if( pWhen ){
       
 73190         pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
       
 73191         pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
       
 73192       }
       
 73193     }
       
 73194 
       
 73195     /* Re-enable the lookaside buffer, if it was disabled earlier. */
       
 73196     db->lookaside.bEnabled = enableLookaside;
       
 73197 
       
 73198     sqlite3ExprDelete(db, pWhere);
       
 73199     sqlite3ExprDelete(db, pWhen);
       
 73200     sqlite3ExprListDelete(db, pList);
       
 73201     sqlite3SelectDelete(db, pSelect);
       
 73202     if( db->mallocFailed==1 ){
       
 73203       fkTriggerDelete(db, pTrigger);
       
 73204       return 0;
       
 73205     }
       
 73206 
       
 73207     switch( action ){
       
 73208       case OE_Restrict:
       
 73209         pStep->op = TK_SELECT; 
       
 73210         break;
       
 73211       case OE_Cascade: 
       
 73212         if( !pChanges ){ 
       
 73213           pStep->op = TK_DELETE; 
       
 73214           break; 
       
 73215         }
       
 73216       default:
       
 73217         pStep->op = TK_UPDATE;
       
 73218     }
       
 73219     pStep->pTrig = pTrigger;
       
 73220     pTrigger->pSchema = pTab->pSchema;
       
 73221     pTrigger->pTabSchema = pTab->pSchema;
       
 73222     pFKey->apTrigger[iAction] = pTrigger;
       
 73223     pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
       
 73224   }
       
 73225 
       
 73226   return pTrigger;
       
 73227 }
       
 73228 
       
 73229 /*
       
 73230 ** This function is called when deleting or updating a row to implement
       
 73231 ** any required CASCADE, SET NULL or SET DEFAULT actions.
       
 73232 */
       
 73233 SQLITE_PRIVATE void sqlite3FkActions(
       
 73234   Parse *pParse,                  /* Parse context */
       
 73235   Table *pTab,                    /* Table being updated or deleted from */
       
 73236   ExprList *pChanges,             /* Change-list for UPDATE, NULL for DELETE */
       
 73237   int regOld                      /* Address of array containing old row */
       
 73238 ){
       
 73239   /* If foreign-key support is enabled, iterate through all FKs that 
       
 73240   ** refer to table pTab. If there is an action associated with the FK 
       
 73241   ** for this operation (either update or delete), invoke the associated 
       
 73242   ** trigger sub-program.  */
       
 73243   if( pParse->db->flags&SQLITE_ForeignKeys ){
       
 73244     FKey *pFKey;                  /* Iterator variable */
       
 73245     for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
       
 73246       Trigger *pAction = fkActionTrigger(pParse, pTab, pFKey, pChanges);
       
 73247       if( pAction ){
       
 73248         sqlite3CodeRowTriggerDirect(pParse, pAction, pTab, regOld, OE_Abort, 0);
       
 73249       }
       
 73250     }
       
 73251   }
       
 73252 }
       
 73253 
       
 73254 #endif /* ifndef SQLITE_OMIT_TRIGGER */
       
 73255 
       
 73256 /*
       
 73257 ** Free all memory associated with foreign key definitions attached to
       
 73258 ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
       
 73259 ** hash table.
       
 73260 */
       
 73261 SQLITE_PRIVATE void sqlite3FkDelete(Table *pTab){
       
 73262   FKey *pFKey;                    /* Iterator variable */
       
 73263   FKey *pNext;                    /* Copy of pFKey->pNextFrom */
       
 73264 
       
 73265   for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
       
 73266 
       
 73267     /* Remove the FK from the fkeyHash hash table. */
       
 73268     if( pFKey->pPrevTo ){
       
 73269       pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
       
 73270     }else{
       
 73271       void *data = (void *)pFKey->pNextTo;
       
 73272       const char *z = (data ? pFKey->pNextTo->zTo : pFKey->zTo);
       
 73273       sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), data);
       
 73274     }
       
 73275     if( pFKey->pNextTo ){
       
 73276       pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
       
 73277     }
       
 73278 
       
 73279     /* Delete any triggers created to implement actions for this FK. */
       
 73280 #ifndef SQLITE_OMIT_TRIGGER
       
 73281     fkTriggerDelete(pTab->dbMem, pFKey->apTrigger[0]);
       
 73282     fkTriggerDelete(pTab->dbMem, pFKey->apTrigger[1]);
       
 73283 #endif
       
 73284 
       
 73285     /* EV: R-30323-21917 Each foreign key constraint in SQLite is
       
 73286     ** classified as either immediate or deferred.
       
 73287     */
       
 73288     assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
       
 73289 
       
 73290     pNext = pFKey->pNextFrom;
       
 73291     sqlite3DbFree(pTab->dbMem, pFKey);
       
 73292   }
       
 73293 }
       
 73294 #endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
       
 73295 
       
 73296 /************** End of fkey.c ************************************************/
       
 73297 /************** Begin file insert.c ******************************************/
       
 73298 /*
       
 73299 ** 2001 September 15
       
 73300 **
       
 73301 ** The author disclaims copyright to this source code.  In place of
       
 73302 ** a legal notice, here is a blessing:
       
 73303 **
       
 73304 **    May you do good and not evil.
       
 73305 **    May you find forgiveness for yourself and forgive others.
       
 73306 **    May you share freely, never taking more than you give.
       
 73307 **
       
 73308 *************************************************************************
       
 73309 ** This file contains C code routines that are called by the parser
       
 73310 ** to handle INSERT statements in SQLite.
       
 73311 **
       
 73312 ** $Id: insert.c,v 1.270 2009/07/24 17:58:53 danielk1977 Exp $
       
 73313 */
       
 73314 
       
 73315 /*
       
 73316 ** Generate code that will open a table for reading.
       
 73317 */
       
 73318 SQLITE_PRIVATE void sqlite3OpenTable(
       
 73319   Parse *p,       /* Generate code into this VDBE */
       
 73320   int iCur,       /* The cursor number of the table */
       
 73321   int iDb,        /* The database index in sqlite3.aDb[] */
       
 73322   Table *pTab,    /* The table to be opened */
       
 73323   int opcode      /* OP_OpenRead or OP_OpenWrite */
       
 73324 ){
       
 73325   Vdbe *v;
       
 73326   if( IsVirtual(pTab) ) return;
       
 73327   v = sqlite3GetVdbe(p);
       
 73328   assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
       
 73329   sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName);
       
 73330   sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb);
       
 73331   sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32);
       
 73332   VdbeComment((v, "%s", pTab->zName));
       
 73333 }
       
 73334 
       
 73335 /*
       
 73336 ** Return a pointer to the column affinity string associated with index
       
 73337 ** pIdx. A column affinity string has one character for each column in 
       
 73338 ** the table, according to the affinity of the column:
       
 73339 **
       
 73340 **  Character      Column affinity
       
 73341 **  ------------------------------
       
 73342 **  'a'            TEXT
       
 73343 **  'b'            NONE
       
 73344 **  'c'            NUMERIC
       
 73345 **  'd'            INTEGER
       
 73346 **  'e'            REAL
       
 73347 **
       
 73348 ** An extra 'b' is appended to the end of the string to cover the
       
 73349 ** rowid that appears as the last column in every index.
       
 73350 **
       
 73351 ** Memory for the buffer containing the column index affinity string
       
 73352 ** is managed along with the rest of the Index structure. It will be
       
 73353 ** released when sqlite3DeleteIndex() is called.
       
 73354 */
       
 73355 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
       
 73356   if( !pIdx->zColAff ){
       
 73357     /* The first time a column affinity string for a particular index is
       
 73358     ** required, it is allocated and populated here. It is then stored as
       
 73359     ** a member of the Index structure for subsequent use.
       
 73360     **
       
 73361     ** The column affinity string will eventually be deleted by
       
 73362     ** sqliteDeleteIndex() when the Index structure itself is cleaned
       
 73363     ** up.
       
 73364     */
       
 73365     int n;
       
 73366     Table *pTab = pIdx->pTable;
       
 73367     sqlite3 *db = sqlite3VdbeDb(v);
       
 73368     pIdx->zColAff = (char *)sqlite3Malloc(pIdx->nColumn+2);
       
 73369     if( !pIdx->zColAff ){
       
 73370       db->mallocFailed = 1;
       
 73371       return 0;
       
 73372     }
       
 73373     for(n=0; n<pIdx->nColumn; n++){
       
 73374       pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
       
 73375     }
       
 73376     pIdx->zColAff[n++] = SQLITE_AFF_NONE;
       
 73377     pIdx->zColAff[n] = 0;
       
 73378   }
       
 73379  
       
 73380   return pIdx->zColAff;
       
 73381 }
       
 73382 
       
 73383 /*
       
 73384 ** Set P4 of the most recently inserted opcode to a column affinity
       
 73385 ** string for table pTab. A column affinity string has one character
       
 73386 ** for each column indexed by the index, according to the affinity of the
       
 73387 ** column:
       
 73388 **
       
 73389 **  Character      Column affinity
       
 73390 **  ------------------------------
       
 73391 **  'a'            TEXT
       
 73392 **  'b'            NONE
       
 73393 **  'c'            NUMERIC
       
 73394 **  'd'            INTEGER
       
 73395 **  'e'            REAL
       
 73396 */
       
 73397 SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
       
 73398   /* The first time a column affinity string for a particular table
       
 73399   ** is required, it is allocated and populated here. It is then 
       
 73400   ** stored as a member of the Table structure for subsequent use.
       
 73401   **
       
 73402   ** The column affinity string will eventually be deleted by
       
 73403   ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
       
 73404   */
       
 73405   if( !pTab->zColAff ){
       
 73406     char *zColAff;
       
 73407     int i;
       
 73408     sqlite3 *db = sqlite3VdbeDb(v);
       
 73409 
       
 73410     zColAff = (char *)sqlite3Malloc(pTab->nCol+1);
       
 73411     if( !zColAff ){
       
 73412       db->mallocFailed = 1;
       
 73413       return;
       
 73414     }
       
 73415 
       
 73416     for(i=0; i<pTab->nCol; i++){
       
 73417       zColAff[i] = pTab->aCol[i].affinity;
       
 73418     }
       
 73419     zColAff[pTab->nCol] = '\0';
       
 73420 
       
 73421     pTab->zColAff = zColAff;
       
 73422   }
       
 73423 
       
 73424   sqlite3VdbeChangeP4(v, -1, pTab->zColAff, 0);
       
 73425 }
       
 73426 
       
 73427 /*
       
 73428 ** Return non-zero if the table pTab in database iDb or any of its indices
       
 73429 ** have been opened at any point in the VDBE program beginning at location
       
 73430 ** iStartAddr throught the end of the program.  This is used to see if 
       
 73431 ** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can 
       
 73432 ** run without using temporary table for the results of the SELECT. 
       
 73433 */
       
 73434 static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){
       
 73435   Vdbe *v = sqlite3GetVdbe(p);
       
 73436   int i;
       
 73437   int iEnd = sqlite3VdbeCurrentAddr(v);
       
 73438 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 73439   VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
       
 73440 #endif
       
 73441 
       
 73442   for(i=iStartAddr; i<iEnd; i++){
       
 73443     VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
       
 73444     assert( pOp!=0 );
       
 73445     if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
       
 73446       Index *pIndex;
       
 73447       int tnum = pOp->p2;
       
 73448       if( tnum==pTab->tnum ){
       
 73449         return 1;
       
 73450       }
       
 73451       for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
       
 73452         if( tnum==pIndex->tnum ){
       
 73453           return 1;
       
 73454         }
       
 73455       }
       
 73456     }
       
 73457 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 73458     if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
       
 73459       assert( pOp->p4.pVtab!=0 );
       
 73460       assert( pOp->p4type==P4_VTAB );
       
 73461       return 1;
       
 73462     }
       
 73463 #endif
       
 73464   }
       
 73465   return 0;
       
 73466 }
       
 73467 
       
 73468 #ifndef SQLITE_OMIT_AUTOINCREMENT
       
 73469 /*
       
 73470 ** Locate or create an AutoincInfo structure associated with table pTab
       
 73471 ** which is in database iDb.  Return the register number for the register
       
 73472 ** that holds the maximum rowid.
       
 73473 **
       
 73474 ** There is at most one AutoincInfo structure per table even if the
       
 73475 ** same table is autoincremented multiple times due to inserts within
       
 73476 ** triggers.  A new AutoincInfo structure is created if this is the
       
 73477 ** first use of table pTab.  On 2nd and subsequent uses, the original
       
 73478 ** AutoincInfo structure is used.
       
 73479 **
       
 73480 ** Three memory locations are allocated:
       
 73481 **
       
 73482 **   (1)  Register to hold the name of the pTab table.
       
 73483 **   (2)  Register to hold the maximum ROWID of pTab.
       
 73484 **   (3)  Register to hold the rowid in sqlite_sequence of pTab
       
 73485 **
       
 73486 ** The 2nd register is the one that is returned.  That is all the
       
 73487 ** insert routine needs to know about.
       
 73488 */
       
 73489 static int autoIncBegin(
       
 73490   Parse *pParse,      /* Parsing context */
       
 73491   int iDb,            /* Index of the database holding pTab */
       
 73492   Table *pTab         /* The table we are writing to */
       
 73493 ){
       
 73494   int memId = 0;      /* Register holding maximum rowid */
       
 73495   if( pTab->tabFlags & TF_Autoincrement ){
       
 73496     Parse *pToplevel = sqlite3ParseToplevel(pParse);
       
 73497     AutoincInfo *pInfo;
       
 73498 
       
 73499     pInfo = pToplevel->pAinc;
       
 73500     while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
       
 73501     if( pInfo==0 ){
       
 73502       pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
       
 73503       if( pInfo==0 ) return 0;
       
 73504       pInfo->pNext = pToplevel->pAinc;
       
 73505       pToplevel->pAinc = pInfo;
       
 73506       pInfo->pTab = pTab;
       
 73507       pInfo->iDb = iDb;
       
 73508       pToplevel->nMem++;                  /* Register to hold name of table */
       
 73509       pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
       
 73510       pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
       
 73511     }
       
 73512     memId = pInfo->regCtr;
       
 73513   }
       
 73514   return memId;
       
 73515 }
       
 73516 
       
 73517 /*
       
 73518 ** This routine generates code that will initialize all of the
       
 73519 ** register used by the autoincrement tracker.  
       
 73520 */
       
 73521 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
       
 73522   AutoincInfo *p;            /* Information about an AUTOINCREMENT */
       
 73523   sqlite3 *db = pParse->db;  /* The database connection */
       
 73524   Db *pDb;                   /* Database only autoinc table */
       
 73525   int memId;                 /* Register holding max rowid */
       
 73526   int addr;                  /* A VDBE address */
       
 73527   Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
       
 73528 
       
 73529   /* This routine is never called during trigger-generation.  It is
       
 73530   ** only called from the top-level */
       
 73531   assert( pParse->pTriggerTab==0 );
       
 73532   assert( pParse==sqlite3ParseToplevel(pParse) );
       
 73533 
       
 73534   assert( v );   /* We failed long ago if this is not so */
       
 73535   for(p = pParse->pAinc; p; p = p->pNext){
       
 73536     pDb = &db->aDb[p->iDb];
       
 73537     memId = p->regCtr;
       
 73538     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
       
 73539     addr = sqlite3VdbeCurrentAddr(v);
       
 73540     sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
       
 73541     sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
       
 73542     sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
       
 73543     sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
       
 73544     sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
       
 73545     sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
       
 73546     sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
       
 73547     sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
       
 73548     sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
       
 73549     sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
       
 73550     sqlite3VdbeAddOp0(v, OP_Close);
       
 73551   }
       
 73552 }
       
 73553 
       
 73554 /*
       
 73555 ** Update the maximum rowid for an autoincrement calculation.
       
 73556 **
       
 73557 ** This routine should be called when the top of the stack holds a
       
 73558 ** new rowid that is about to be inserted.  If that new rowid is
       
 73559 ** larger than the maximum rowid in the memId memory cell, then the
       
 73560 ** memory cell is updated.  The stack is unchanged.
       
 73561 */
       
 73562 static void autoIncStep(Parse *pParse, int memId, int regRowid){
       
 73563   if( memId>0 ){
       
 73564     sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
       
 73565   }
       
 73566 }
       
 73567 
       
 73568 /*
       
 73569 ** This routine generates the code needed to write autoincrement
       
 73570 ** maximum rowid values back into the sqlite_sequence register.
       
 73571 ** Every statement that might do an INSERT into an autoincrement
       
 73572 ** table (either directly or through triggers) needs to call this
       
 73573 ** routine just before the "exit" code.
       
 73574 */
       
 73575 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
       
 73576   AutoincInfo *p;
       
 73577   Vdbe *v = pParse->pVdbe;
       
 73578   sqlite3 *db = pParse->db;
       
 73579 
       
 73580   assert( v );
       
 73581   for(p = pParse->pAinc; p; p = p->pNext){
       
 73582     Db *pDb = &db->aDb[p->iDb];
       
 73583     int j1, j2, j3, j4, j5;
       
 73584     int iRec;
       
 73585     int memId = p->regCtr;
       
 73586 
       
 73587     iRec = sqlite3GetTempReg(pParse);
       
 73588     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
       
 73589     j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
       
 73590     j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
       
 73591     j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
       
 73592     j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
       
 73593     sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
       
 73594     sqlite3VdbeJumpHere(v, j2);
       
 73595     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
       
 73596     j5 = sqlite3VdbeAddOp0(v, OP_Goto);
       
 73597     sqlite3VdbeJumpHere(v, j4);
       
 73598     sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
       
 73599     sqlite3VdbeJumpHere(v, j1);
       
 73600     sqlite3VdbeJumpHere(v, j5);
       
 73601     sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
       
 73602     sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
       
 73603     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
       
 73604     sqlite3VdbeAddOp0(v, OP_Close);
       
 73605     sqlite3ReleaseTempReg(pParse, iRec);
       
 73606   }
       
 73607 }
       
 73608 #else
       
 73609 /*
       
 73610 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
       
 73611 ** above are all no-ops
       
 73612 */
       
 73613 # define autoIncBegin(A,B,C) (0)
       
 73614 # define autoIncStep(A,B,C)
       
 73615 #endif /* SQLITE_OMIT_AUTOINCREMENT */
       
 73616 
       
 73617 
       
 73618 /* Forward declaration */
       
 73619 static int xferOptimization(
       
 73620   Parse *pParse,        /* Parser context */
       
 73621   Table *pDest,         /* The table we are inserting into */
       
 73622   Select *pSelect,      /* A SELECT statement to use as the data source */
       
 73623   int onError,          /* How to handle constraint errors */
       
 73624   int iDbDest           /* The database of pDest */
       
 73625 );
       
 73626 
       
 73627 /*
       
 73628 ** This routine is call to handle SQL of the following forms:
       
 73629 **
       
 73630 **    insert into TABLE (IDLIST) values(EXPRLIST)
       
 73631 **    insert into TABLE (IDLIST) select
       
 73632 **
       
 73633 ** The IDLIST following the table name is always optional.  If omitted,
       
 73634 ** then a list of all columns for the table is substituted.  The IDLIST
       
 73635 ** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
       
 73636 **
       
 73637 ** The pList parameter holds EXPRLIST in the first form of the INSERT
       
 73638 ** statement above, and pSelect is NULL.  For the second form, pList is
       
 73639 ** NULL and pSelect is a pointer to the select statement used to generate
       
 73640 ** data for the insert.
       
 73641 **
       
 73642 ** The code generated follows one of four templates.  For a simple
       
 73643 ** select with data coming from a VALUES clause, the code executes
       
 73644 ** once straight down through.  Pseudo-code follows (we call this
       
 73645 ** the "1st template"):
       
 73646 **
       
 73647 **         open write cursor to <table> and its indices
       
 73648 **         puts VALUES clause expressions onto the stack
       
 73649 **         write the resulting record into <table>
       
 73650 **         cleanup
       
 73651 **
       
 73652 ** The three remaining templates assume the statement is of the form
       
 73653 **
       
 73654 **   INSERT INTO <table> SELECT ...
       
 73655 **
       
 73656 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
       
 73657 ** in other words if the SELECT pulls all columns from a single table
       
 73658 ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
       
 73659 ** if <table2> and <table1> are distinct tables but have identical
       
 73660 ** schemas, including all the same indices, then a special optimization
       
 73661 ** is invoked that copies raw records from <table2> over to <table1>.
       
 73662 ** See the xferOptimization() function for the implementation of this
       
 73663 ** template.  This is the 2nd template.
       
 73664 **
       
 73665 **         open a write cursor to <table>
       
 73666 **         open read cursor on <table2>
       
 73667 **         transfer all records in <table2> over to <table>
       
 73668 **         close cursors
       
 73669 **         foreach index on <table>
       
 73670 **           open a write cursor on the <table> index
       
 73671 **           open a read cursor on the corresponding <table2> index
       
 73672 **           transfer all records from the read to the write cursors
       
 73673 **           close cursors
       
 73674 **         end foreach
       
 73675 **
       
 73676 ** The 3rd template is for when the second template does not apply
       
 73677 ** and the SELECT clause does not read from <table> at any time.
       
 73678 ** The generated code follows this template:
       
 73679 **
       
 73680 **         EOF <- 0
       
 73681 **         X <- A
       
 73682 **         goto B
       
 73683 **      A: setup for the SELECT
       
 73684 **         loop over the rows in the SELECT
       
 73685 **           load values into registers R..R+n
       
 73686 **           yield X
       
 73687 **         end loop
       
 73688 **         cleanup after the SELECT
       
 73689 **         EOF <- 1
       
 73690 **         yield X
       
 73691 **         goto A
       
 73692 **      B: open write cursor to <table> and its indices
       
 73693 **      C: yield X
       
 73694 **         if EOF goto D
       
 73695 **         insert the select result into <table> from R..R+n
       
 73696 **         goto C
       
 73697 **      D: cleanup
       
 73698 **
       
 73699 ** The 4th template is used if the insert statement takes its
       
 73700 ** values from a SELECT but the data is being inserted into a table
       
 73701 ** that is also read as part of the SELECT.  In the third form,
       
 73702 ** we have to use a intermediate table to store the results of
       
 73703 ** the select.  The template is like this:
       
 73704 **
       
 73705 **         EOF <- 0
       
 73706 **         X <- A
       
 73707 **         goto B
       
 73708 **      A: setup for the SELECT
       
 73709 **         loop over the tables in the SELECT
       
 73710 **           load value into register R..R+n
       
 73711 **           yield X
       
 73712 **         end loop
       
 73713 **         cleanup after the SELECT
       
 73714 **         EOF <- 1
       
 73715 **         yield X
       
 73716 **         halt-error
       
 73717 **      B: open temp table
       
 73718 **      L: yield X
       
 73719 **         if EOF goto M
       
 73720 **         insert row from R..R+n into temp table
       
 73721 **         goto L
       
 73722 **      M: open write cursor to <table> and its indices
       
 73723 **         rewind temp table
       
 73724 **      C: loop over rows of intermediate table
       
 73725 **           transfer values form intermediate table into <table>
       
 73726 **         end loop
       
 73727 **      D: cleanup
       
 73728 */
       
 73729 SQLITE_PRIVATE void sqlite3Insert(
       
 73730   Parse *pParse,        /* Parser context */
       
 73731   SrcList *pTabList,    /* Name of table into which we are inserting */
       
 73732   ExprList *pList,      /* List of values to be inserted */
       
 73733   Select *pSelect,      /* A SELECT statement to use as the data source */
       
 73734   IdList *pColumn,      /* Column names corresponding to IDLIST. */
       
 73735   int onError           /* How to handle constraint errors */
       
 73736 ){
       
 73737   sqlite3 *db;          /* The main database structure */
       
 73738   Table *pTab;          /* The table to insert into.  aka TABLE */
       
 73739   char *zTab;           /* Name of the table into which we are inserting */
       
 73740   const char *zDb;      /* Name of the database holding this table */
       
 73741   int i, j, idx;        /* Loop counters */
       
 73742   Vdbe *v;              /* Generate code into this virtual machine */
       
 73743   Index *pIdx;          /* For looping over indices of the table */
       
 73744   int nColumn;          /* Number of columns in the data */
       
 73745   int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
       
 73746   int baseCur = 0;      /* VDBE Cursor number for pTab */
       
 73747   int keyColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
       
 73748   int endOfLoop;        /* Label for the end of the insertion loop */
       
 73749   int useTempTable = 0; /* Store SELECT results in intermediate table */
       
 73750   int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
       
 73751   int addrInsTop = 0;   /* Jump to label "D" */
       
 73752   int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
       
 73753   int addrSelect = 0;   /* Address of coroutine that implements the SELECT */
       
 73754   SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
       
 73755   int iDb;              /* Index of database holding TABLE */
       
 73756   Db *pDb;              /* The database containing table being inserted into */
       
 73757   int appendFlag = 0;   /* True if the insert is likely to be an append */
       
 73758 
       
 73759   /* Register allocations */
       
 73760   int regFromSelect = 0;/* Base register for data coming from SELECT */
       
 73761   int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
       
 73762   int regRowCount = 0;  /* Memory cell used for the row counter */
       
 73763   int regIns;           /* Block of regs holding rowid+data being inserted */
       
 73764   int regRowid;         /* registers holding insert rowid */
       
 73765   int regData;          /* register holding first column to insert */
       
 73766   int regRecord;        /* Holds the assemblied row record */
       
 73767   int regEof = 0;       /* Register recording end of SELECT data */
       
 73768   int *aRegIdx = 0;     /* One register allocated to each index */
       
 73769 
       
 73770 #ifndef SQLITE_OMIT_TRIGGER
       
 73771   int isView;                 /* True if attempting to insert into a view */
       
 73772   Trigger *pTrigger;          /* List of triggers on pTab, if required */
       
 73773   int tmask;                  /* Mask of trigger times */
       
 73774 #endif
       
 73775 
       
 73776   db = pParse->db;
       
 73777   memset(&dest, 0, sizeof(dest));
       
 73778   if( pParse->nErr || db->mallocFailed ){
       
 73779     goto insert_cleanup;
       
 73780   }
       
 73781 
       
 73782   /* Locate the table into which we will be inserting new information.
       
 73783   */
       
 73784   assert( pTabList->nSrc==1 );
       
 73785   zTab = pTabList->a[0].zName;
       
 73786   if( NEVER(zTab==0) ) goto insert_cleanup;
       
 73787   pTab = sqlite3SrcListLookup(pParse, pTabList);
       
 73788   if( pTab==0 ){
       
 73789     goto insert_cleanup;
       
 73790   }
       
 73791   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       
 73792   assert( iDb<db->nDb );
       
 73793   pDb = &db->aDb[iDb];
       
 73794   zDb = pDb->zName;
       
 73795   if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
       
 73796     goto insert_cleanup;
       
 73797   }
       
 73798 
       
 73799   /* Figure out if we have any triggers and if the table being
       
 73800   ** inserted into is a view
       
 73801   */
       
 73802 #ifndef SQLITE_OMIT_TRIGGER
       
 73803   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
       
 73804   isView = pTab->pSelect!=0;
       
 73805 #else
       
 73806 # define pTrigger 0
       
 73807 # define tmask 0
       
 73808 # define isView 0
       
 73809 #endif
       
 73810 #ifdef SQLITE_OMIT_VIEW
       
 73811 # undef isView
       
 73812 # define isView 0
       
 73813 #endif
       
 73814   assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
       
 73815 
       
 73816   /* If pTab is really a view, make sure it has been initialized.
       
 73817   ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual 
       
 73818   ** module table).
       
 73819   */
       
 73820   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
       
 73821     goto insert_cleanup;
       
 73822   }
       
 73823 
       
 73824   /* Ensure that:
       
 73825   *  (a) the table is not read-only, 
       
 73826   *  (b) that if it is a view then ON INSERT triggers exist
       
 73827   */
       
 73828   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
       
 73829     goto insert_cleanup;
       
 73830   }
       
 73831 
       
 73832   /* Allocate a VDBE
       
 73833   */
       
 73834   v = sqlite3GetVdbe(pParse);
       
 73835   if( v==0 ) goto insert_cleanup;
       
 73836   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
       
 73837   sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
       
 73838 
       
 73839 #ifndef SQLITE_OMIT_XFER_OPT
       
 73840   /* If the statement is of the form
       
 73841   **
       
 73842   **       INSERT INTO <table1> SELECT * FROM <table2>;
       
 73843   **
       
 73844   ** Then special optimizations can be applied that make the transfer
       
 73845   ** very fast and which reduce fragmentation of indices.
       
 73846   **
       
 73847   ** This is the 2nd template.
       
 73848   */
       
 73849   if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
       
 73850     assert( !pTrigger );
       
 73851     assert( pList==0 );
       
 73852     goto insert_end;
       
 73853   }
       
 73854 #endif /* SQLITE_OMIT_XFER_OPT */
       
 73855 
       
 73856   /* If this is an AUTOINCREMENT table, look up the sequence number in the
       
 73857   ** sqlite_sequence table and store it in memory cell regAutoinc.
       
 73858   */
       
 73859   regAutoinc = autoIncBegin(pParse, iDb, pTab);
       
 73860 
       
 73861   /* Figure out how many columns of data are supplied.  If the data
       
 73862   ** is coming from a SELECT statement, then generate a co-routine that
       
 73863   ** produces a single row of the SELECT on each invocation.  The
       
 73864   ** co-routine is the common header to the 3rd and 4th templates.
       
 73865   */
       
 73866   if( pSelect ){
       
 73867     /* Data is coming from a SELECT.  Generate code to implement that SELECT
       
 73868     ** as a co-routine.  The code is common to both the 3rd and 4th
       
 73869     ** templates:
       
 73870     **
       
 73871     **         EOF <- 0
       
 73872     **         X <- A
       
 73873     **         goto B
       
 73874     **      A: setup for the SELECT
       
 73875     **         loop over the tables in the SELECT
       
 73876     **           load value into register R..R+n
       
 73877     **           yield X
       
 73878     **         end loop
       
 73879     **         cleanup after the SELECT
       
 73880     **         EOF <- 1
       
 73881     **         yield X
       
 73882     **         halt-error
       
 73883     **
       
 73884     ** On each invocation of the co-routine, it puts a single row of the
       
 73885     ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1.
       
 73886     ** (These output registers are allocated by sqlite3Select().)  When
       
 73887     ** the SELECT completes, it sets the EOF flag stored in regEof.
       
 73888     */
       
 73889     int rc, j1;
       
 73890 
       
 73891     regEof = ++pParse->nMem;
       
 73892     sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);      /* EOF <- 0 */
       
 73893     VdbeComment((v, "SELECT eof flag"));
       
 73894     sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem);
       
 73895     addrSelect = sqlite3VdbeCurrentAddr(v)+2;
       
 73896     sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm);
       
 73897     j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
       
 73898     VdbeComment((v, "Jump over SELECT coroutine"));
       
 73899 
       
 73900     /* Resolve the expressions in the SELECT statement and execute it. */
       
 73901     rc = sqlite3Select(pParse, pSelect, &dest);
       
 73902     assert( pParse->nErr==0 || rc );
       
 73903     if( rc || NEVER(pParse->nErr) || db->mallocFailed ){
       
 73904       goto insert_cleanup;
       
 73905     }
       
 73906     sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);         /* EOF <- 1 */
       
 73907     sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);   /* yield X */
       
 73908     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
       
 73909     VdbeComment((v, "End of SELECT coroutine"));
       
 73910     sqlite3VdbeJumpHere(v, j1);                          /* label B: */
       
 73911 
       
 73912     regFromSelect = dest.iMem;
       
 73913     assert( pSelect->pEList );
       
 73914     nColumn = pSelect->pEList->nExpr;
       
 73915     assert( dest.nMem==nColumn );
       
 73916 
       
 73917     /* Set useTempTable to TRUE if the result of the SELECT statement
       
 73918     ** should be written into a temporary table (template 4).  Set to
       
 73919     ** FALSE if each* row of the SELECT can be written directly into
       
 73920     ** the destination table (template 3).
       
 73921     **
       
 73922     ** A temp table must be used if the table being updated is also one
       
 73923     ** of the tables being read by the SELECT statement.  Also use a 
       
 73924     ** temp table in the case of row triggers.
       
 73925     */
       
 73926     if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
       
 73927       useTempTable = 1;
       
 73928     }
       
 73929 
       
 73930     if( useTempTable ){
       
 73931       /* Invoke the coroutine to extract information from the SELECT
       
 73932       ** and add it to a transient table srcTab.  The code generated
       
 73933       ** here is from the 4th template:
       
 73934       **
       
 73935       **      B: open temp table
       
 73936       **      L: yield X
       
 73937       **         if EOF goto M
       
 73938       **         insert row from R..R+n into temp table
       
 73939       **         goto L
       
 73940       **      M: ...
       
 73941       */
       
 73942       int regRec;          /* Register to hold packed record */
       
 73943       int regTempRowid;    /* Register to hold temp table ROWID */
       
 73944       int addrTop;         /* Label "L" */
       
 73945       int addrIf;          /* Address of jump to M */
       
 73946 
       
 73947       srcTab = pParse->nTab++;
       
 73948       regRec = sqlite3GetTempReg(pParse);
       
 73949       regTempRowid = sqlite3GetTempReg(pParse);
       
 73950       sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
       
 73951       addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
       
 73952       addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
       
 73953       sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
       
 73954       sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
       
 73955       sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
       
 73956       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
       
 73957       sqlite3VdbeJumpHere(v, addrIf);
       
 73958       sqlite3ReleaseTempReg(pParse, regRec);
       
 73959       sqlite3ReleaseTempReg(pParse, regTempRowid);
       
 73960     }
       
 73961   }else{
       
 73962     /* This is the case if the data for the INSERT is coming from a VALUES
       
 73963     ** clause
       
 73964     */
       
 73965     NameContext sNC;
       
 73966     memset(&sNC, 0, sizeof(sNC));
       
 73967     sNC.pParse = pParse;
       
 73968     srcTab = -1;
       
 73969     assert( useTempTable==0 );
       
 73970     nColumn = pList ? pList->nExpr : 0;
       
 73971     for(i=0; i<nColumn; i++){
       
 73972       if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
       
 73973         goto insert_cleanup;
       
 73974       }
       
 73975     }
       
 73976   }
       
 73977 
       
 73978   /* Make sure the number of columns in the source data matches the number
       
 73979   ** of columns to be inserted into the table.
       
 73980   */
       
 73981   if( IsVirtual(pTab) ){
       
 73982     for(i=0; i<pTab->nCol; i++){
       
 73983       nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
       
 73984     }
       
 73985   }
       
 73986   if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
       
 73987     sqlite3ErrorMsg(pParse, 
       
 73988        "table %S has %d columns but %d values were supplied",
       
 73989        pTabList, 0, pTab->nCol-nHidden, nColumn);
       
 73990     goto insert_cleanup;
       
 73991   }
       
 73992   if( pColumn!=0 && nColumn!=pColumn->nId ){
       
 73993     sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
       
 73994     goto insert_cleanup;
       
 73995   }
       
 73996 
       
 73997   /* If the INSERT statement included an IDLIST term, then make sure
       
 73998   ** all elements of the IDLIST really are columns of the table and 
       
 73999   ** remember the column indices.
       
 74000   **
       
 74001   ** If the table has an INTEGER PRIMARY KEY column and that column
       
 74002   ** is named in the IDLIST, then record in the keyColumn variable
       
 74003   ** the index into IDLIST of the primary key column.  keyColumn is
       
 74004   ** the index of the primary key as it appears in IDLIST, not as
       
 74005   ** is appears in the original table.  (The index of the primary
       
 74006   ** key in the original table is pTab->iPKey.)
       
 74007   */
       
 74008   if( pColumn ){
       
 74009     for(i=0; i<pColumn->nId; i++){
       
 74010       pColumn->a[i].idx = -1;
       
 74011     }
       
 74012     for(i=0; i<pColumn->nId; i++){
       
 74013       for(j=0; j<pTab->nCol; j++){
       
 74014         if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
       
 74015           pColumn->a[i].idx = j;
       
 74016           if( j==pTab->iPKey ){
       
 74017             keyColumn = i;
       
 74018           }
       
 74019           break;
       
 74020         }
       
 74021       }
       
 74022       if( j>=pTab->nCol ){
       
 74023         if( sqlite3IsRowid(pColumn->a[i].zName) ){
       
 74024           keyColumn = i;
       
 74025         }else{
       
 74026           sqlite3ErrorMsg(pParse, "table %S has no column named %s",
       
 74027               pTabList, 0, pColumn->a[i].zName);
       
 74028           pParse->nErr++;
       
 74029           goto insert_cleanup;
       
 74030         }
       
 74031       }
       
 74032     }
       
 74033   }
       
 74034 
       
 74035   /* If there is no IDLIST term but the table has an integer primary
       
 74036   ** key, the set the keyColumn variable to the primary key column index
       
 74037   ** in the original table definition.
       
 74038   */
       
 74039   if( pColumn==0 && nColumn>0 ){
       
 74040     keyColumn = pTab->iPKey;
       
 74041   }
       
 74042     
       
 74043   /* Initialize the count of rows to be inserted
       
 74044   */
       
 74045   if( db->flags & SQLITE_CountRows ){
       
 74046     regRowCount = ++pParse->nMem;
       
 74047     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
       
 74048   }
       
 74049 
       
 74050   /* If this is not a view, open the table and and all indices */
       
 74051   if( !isView ){
       
 74052     int nIdx;
       
 74053 
       
 74054     baseCur = pParse->nTab;
       
 74055     nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite);
       
 74056     aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
       
 74057     if( aRegIdx==0 ){
       
 74058       goto insert_cleanup;
       
 74059     }
       
 74060     for(i=0; i<nIdx; i++){
       
 74061       aRegIdx[i] = ++pParse->nMem;
       
 74062     }
       
 74063   }
       
 74064 
       
 74065   /* This is the top of the main insertion loop */
       
 74066   if( useTempTable ){
       
 74067     /* This block codes the top of loop only.  The complete loop is the
       
 74068     ** following pseudocode (template 4):
       
 74069     **
       
 74070     **         rewind temp table
       
 74071     **      C: loop over rows of intermediate table
       
 74072     **           transfer values form intermediate table into <table>
       
 74073     **         end loop
       
 74074     **      D: ...
       
 74075     */
       
 74076     addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
       
 74077     addrCont = sqlite3VdbeCurrentAddr(v);
       
 74078   }else if( pSelect ){
       
 74079     /* This block codes the top of loop only.  The complete loop is the
       
 74080     ** following pseudocode (template 3):
       
 74081     **
       
 74082     **      C: yield X
       
 74083     **         if EOF goto D
       
 74084     **         insert the select result into <table> from R..R+n
       
 74085     **         goto C
       
 74086     **      D: ...
       
 74087     */
       
 74088     addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
       
 74089     addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
       
 74090   }
       
 74091 
       
 74092   /* Allocate registers for holding the rowid of the new row,
       
 74093   ** the content of the new row, and the assemblied row record.
       
 74094   */
       
 74095   regRecord = ++pParse->nMem;
       
 74096   regRowid = regIns = pParse->nMem+1;
       
 74097   pParse->nMem += pTab->nCol + 1;
       
 74098   if( IsVirtual(pTab) ){
       
 74099     regRowid++;
       
 74100     pParse->nMem++;
       
 74101   }
       
 74102   regData = regRowid+1;
       
 74103 
       
 74104   /* Run the BEFORE and INSTEAD OF triggers, if there are any
       
 74105   */
       
 74106   endOfLoop = sqlite3VdbeMakeLabel(v);
       
 74107   if( tmask & TRIGGER_BEFORE ){
       
 74108     int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
       
 74109 
       
 74110     /* build the NEW.* reference row.  Note that if there is an INTEGER
       
 74111     ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
       
 74112     ** translated into a unique ID for the row.  But on a BEFORE trigger,
       
 74113     ** we do not know what the unique ID will be (because the insert has
       
 74114     ** not happened yet) so we substitute a rowid of -1
       
 74115     */
       
 74116     if( keyColumn<0 ){
       
 74117       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
       
 74118     }else{
       
 74119       int j1;
       
 74120       if( useTempTable ){
       
 74121         sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols);
       
 74122       }else{
       
 74123         assert( pSelect==0 );  /* Otherwise useTempTable is true */
       
 74124         sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols);
       
 74125       }
       
 74126       j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
       
 74127       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
       
 74128       sqlite3VdbeJumpHere(v, j1);
       
 74129       sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
       
 74130     }
       
 74131 
       
 74132     /* Cannot have triggers on a virtual table. If it were possible,
       
 74133     ** this block would have to account for hidden column.
       
 74134     */
       
 74135     assert( !IsVirtual(pTab) );
       
 74136 
       
 74137     /* Create the new column data
       
 74138     */
       
 74139     for(i=0; i<pTab->nCol; i++){
       
 74140       if( pColumn==0 ){
       
 74141         j = i;
       
 74142       }else{
       
 74143         for(j=0; j<pColumn->nId; j++){
       
 74144           if( pColumn->a[j].idx==i ) break;
       
 74145         }
       
 74146       }
       
 74147       if( pColumn && j>=pColumn->nId ){
       
 74148         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
       
 74149       }else if( useTempTable ){
       
 74150         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); 
       
 74151       }else{
       
 74152         assert( pSelect==0 ); /* Otherwise useTempTable is true */
       
 74153         sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
       
 74154       }
       
 74155     }
       
 74156 
       
 74157     /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
       
 74158     ** do not attempt any conversions before assembling the record.
       
 74159     ** If this is a real table, attempt conversions as required by the
       
 74160     ** table column affinities.
       
 74161     */
       
 74162     if( !isView ){
       
 74163       sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
       
 74164       sqlite3TableAffinityStr(v, pTab);
       
 74165     }
       
 74166 
       
 74167     /* Fire BEFORE or INSTEAD OF triggers */
       
 74168     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, 
       
 74169         pTab, regCols-pTab->nCol-1, onError, endOfLoop);
       
 74170 
       
 74171     sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
       
 74172   }
       
 74173 
       
 74174   /* Push the record number for the new entry onto the stack.  The
       
 74175   ** record number is a randomly generate integer created by NewRowid
       
 74176   ** except when the table has an INTEGER PRIMARY KEY column, in which
       
 74177   ** case the record number is the same as that column. 
       
 74178   */
       
 74179   if( !isView ){
       
 74180     if( IsVirtual(pTab) ){
       
 74181       /* The row that the VUpdate opcode will delete: none */
       
 74182       sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
       
 74183     }
       
 74184     if( keyColumn>=0 ){
       
 74185       if( useTempTable ){
       
 74186         sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid);
       
 74187       }else if( pSelect ){
       
 74188         sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid);
       
 74189       }else{
       
 74190         VdbeOp *pOp;
       
 74191         sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid);
       
 74192         pOp = sqlite3VdbeGetOp(v, -1);
       
 74193         if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
       
 74194           appendFlag = 1;
       
 74195           pOp->opcode = OP_NewRowid;
       
 74196           pOp->p1 = baseCur;
       
 74197           pOp->p2 = regRowid;
       
 74198           pOp->p3 = regAutoinc;
       
 74199         }
       
 74200       }
       
 74201       /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
       
 74202       ** to generate a unique primary key value.
       
 74203       */
       
 74204       if( !appendFlag ){
       
 74205         int j1;
       
 74206         if( !IsVirtual(pTab) ){
       
 74207           j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
       
 74208           sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
       
 74209           sqlite3VdbeJumpHere(v, j1);
       
 74210         }else{
       
 74211           j1 = sqlite3VdbeCurrentAddr(v);
       
 74212           sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
       
 74213         }
       
 74214         sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
       
 74215       }
       
 74216     }else if( IsVirtual(pTab) ){
       
 74217       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
       
 74218     }else{
       
 74219       sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
       
 74220       appendFlag = 1;
       
 74221     }
       
 74222     autoIncStep(pParse, regAutoinc, regRowid);
       
 74223 
       
 74224     /* Push onto the stack, data for all columns of the new entry, beginning
       
 74225     ** with the first column.
       
 74226     */
       
 74227     nHidden = 0;
       
 74228     for(i=0; i<pTab->nCol; i++){
       
 74229       int iRegStore = regRowid+1+i;
       
 74230       if( i==pTab->iPKey ){
       
 74231         /* The value of the INTEGER PRIMARY KEY column is always a NULL.
       
 74232         ** Whenever this column is read, the record number will be substituted
       
 74233         ** in its place.  So will fill this column with a NULL to avoid
       
 74234         ** taking up data space with information that will never be used. */
       
 74235         sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
       
 74236         continue;
       
 74237       }
       
 74238       if( pColumn==0 ){
       
 74239         if( IsHiddenColumn(&pTab->aCol[i]) ){
       
 74240           assert( IsVirtual(pTab) );
       
 74241           j = -1;
       
 74242           nHidden++;
       
 74243         }else{
       
 74244           j = i - nHidden;
       
 74245         }
       
 74246       }else{
       
 74247         for(j=0; j<pColumn->nId; j++){
       
 74248           if( pColumn->a[j].idx==i ) break;
       
 74249         }
       
 74250       }
       
 74251       if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
       
 74252         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
       
 74253       }else if( useTempTable ){
       
 74254         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); 
       
 74255       }else if( pSelect ){
       
 74256         sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
       
 74257       }else{
       
 74258         sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
       
 74259       }
       
 74260     }
       
 74261 
       
 74262     /* Generate code to check constraints and generate index keys and
       
 74263     ** do the insertion.
       
 74264     */
       
 74265 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 74266     if( IsVirtual(pTab) ){
       
 74267       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
       
 74268       sqlite3VtabMakeWritable(pParse, pTab);
       
 74269       sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
       
 74270       sqlite3MayAbort(pParse);
       
 74271     }else
       
 74272 #endif
       
 74273     {
       
 74274       int isReplace;    /* Set to true if constraints may cause a replace */
       
 74275       sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
       
 74276           keyColumn>=0, 0, onError, endOfLoop, &isReplace
       
 74277       );
       
 74278       sqlite3FkCheck(pParse, pTab, 0, regIns);
       
 74279       sqlite3CompleteInsertion(
       
 74280           pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0
       
 74281       );
       
 74282     }
       
 74283   }
       
 74284 
       
 74285   /* Update the count of rows that are inserted
       
 74286   */
       
 74287   if( (db->flags & SQLITE_CountRows)!=0 ){
       
 74288     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
       
 74289   }
       
 74290 
       
 74291   if( pTrigger ){
       
 74292     /* Code AFTER triggers */
       
 74293     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, 
       
 74294         pTab, regData-2-pTab->nCol, onError, endOfLoop);
       
 74295   }
       
 74296 
       
 74297   /* The bottom of the main insertion loop, if the data source
       
 74298   ** is a SELECT statement.
       
 74299   */
       
 74300   sqlite3VdbeResolveLabel(v, endOfLoop);
       
 74301   if( useTempTable ){
       
 74302     sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
       
 74303     sqlite3VdbeJumpHere(v, addrInsTop);
       
 74304     sqlite3VdbeAddOp1(v, OP_Close, srcTab);
       
 74305   }else if( pSelect ){
       
 74306     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
       
 74307     sqlite3VdbeJumpHere(v, addrInsTop);
       
 74308   }
       
 74309 
       
 74310   if( !IsVirtual(pTab) && !isView ){
       
 74311     /* Close all tables opened */
       
 74312     sqlite3VdbeAddOp1(v, OP_Close, baseCur);
       
 74313     for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
       
 74314       sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
       
 74315     }
       
 74316   }
       
 74317 
       
 74318 insert_end:
       
 74319   /* Update the sqlite_sequence table by storing the content of the
       
 74320   ** maximum rowid counter values recorded while inserting into
       
 74321   ** autoincrement tables.
       
 74322   */
       
 74323   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
       
 74324     sqlite3AutoincrementEnd(pParse);
       
 74325   }
       
 74326 
       
 74327   /*
       
 74328   ** Return the number of rows inserted. If this routine is 
       
 74329   ** generating code because of a call to sqlite3NestedParse(), do not
       
 74330   ** invoke the callback function.
       
 74331   */
       
 74332   if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
       
 74333     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
       
 74334     sqlite3VdbeSetNumCols(v, 1);
       
 74335     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
       
 74336   }
       
 74337 
       
 74338 insert_cleanup:
       
 74339   sqlite3SrcListDelete(db, pTabList);
       
 74340   sqlite3ExprListDelete(db, pList);
       
 74341   sqlite3SelectDelete(db, pSelect);
       
 74342   sqlite3IdListDelete(db, pColumn);
       
 74343   sqlite3DbFree(db, aRegIdx);
       
 74344 }
       
 74345 
       
 74346 /* Make sure "isView" and other macros defined above are undefined. Otherwise
       
 74347 ** thely may interfere with compilation of other functions in this file
       
 74348 ** (or in another file, if this file becomes part of the amalgamation).  */
       
 74349 #ifdef isView
       
 74350  #undef isView
       
 74351 #endif
       
 74352 #ifdef pTrigger
       
 74353  #undef pTrigger
       
 74354 #endif
       
 74355 #ifdef tmask
       
 74356  #undef tmask
       
 74357 #endif
       
 74358 
       
 74359 
       
 74360 /*
       
 74361 ** Generate code to do constraint checks prior to an INSERT or an UPDATE.
       
 74362 **
       
 74363 ** The input is a range of consecutive registers as follows:
       
 74364 **
       
 74365 **    1.  The rowid of the row after the update.
       
 74366 **
       
 74367 **    2.  The data in the first column of the entry after the update.
       
 74368 **
       
 74369 **    i.  Data from middle columns...
       
 74370 **
       
 74371 **    N.  The data in the last column of the entry after the update.
       
 74372 **
       
 74373 ** The regRowid parameter is the index of the register containing (1).
       
 74374 **
       
 74375 ** If isUpdate is true and rowidChng is non-zero, then rowidChng contains
       
 74376 ** the address of a register containing the rowid before the update takes
       
 74377 ** place. isUpdate is true for UPDATEs and false for INSERTs. If isUpdate
       
 74378 ** is false, indicating an INSERT statement, then a non-zero rowidChng 
       
 74379 ** indicates that the rowid was explicitly specified as part of the
       
 74380 ** INSERT statement. If rowidChng is false, it means that  the rowid is
       
 74381 ** computed automatically in an insert or that the rowid value is not 
       
 74382 ** modified by an update.
       
 74383 **
       
 74384 ** The code generated by this routine store new index entries into
       
 74385 ** registers identified by aRegIdx[].  No index entry is created for
       
 74386 ** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
       
 74387 ** the same as the order of indices on the linked list of indices
       
 74388 ** attached to the table.
       
 74389 **
       
 74390 ** This routine also generates code to check constraints.  NOT NULL,
       
 74391 ** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
       
 74392 ** then the appropriate action is performed.  There are five possible
       
 74393 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
       
 74394 **
       
 74395 **  Constraint type  Action       What Happens
       
 74396 **  ---------------  ----------   ----------------------------------------
       
 74397 **  any              ROLLBACK     The current transaction is rolled back and
       
 74398 **                                sqlite3_exec() returns immediately with a
       
 74399 **                                return code of SQLITE_CONSTRAINT.
       
 74400 **
       
 74401 **  any              ABORT        Back out changes from the current command
       
 74402 **                                only (do not do a complete rollback) then
       
 74403 **                                cause sqlite3_exec() to return immediately
       
 74404 **                                with SQLITE_CONSTRAINT.
       
 74405 **
       
 74406 **  any              FAIL         Sqlite_exec() returns immediately with a
       
 74407 **                                return code of SQLITE_CONSTRAINT.  The
       
 74408 **                                transaction is not rolled back and any
       
 74409 **                                prior changes are retained.
       
 74410 **
       
 74411 **  any              IGNORE       The record number and data is popped from
       
 74412 **                                the stack and there is an immediate jump
       
 74413 **                                to label ignoreDest.
       
 74414 **
       
 74415 **  NOT NULL         REPLACE      The NULL value is replace by the default
       
 74416 **                                value for that column.  If the default value
       
 74417 **                                is NULL, the action is the same as ABORT.
       
 74418 **
       
 74419 **  UNIQUE           REPLACE      The other row that conflicts with the row
       
 74420 **                                being inserted is removed.
       
 74421 **
       
 74422 **  CHECK            REPLACE      Illegal.  The results in an exception.
       
 74423 **
       
 74424 ** Which action to take is determined by the overrideError parameter.
       
 74425 ** Or if overrideError==OE_Default, then the pParse->onError parameter
       
 74426 ** is used.  Or if pParse->onError==OE_Default then the onError value
       
 74427 ** for the constraint is used.
       
 74428 **
       
 74429 ** The calling routine must open a read/write cursor for pTab with
       
 74430 ** cursor number "baseCur".  All indices of pTab must also have open
       
 74431 ** read/write cursors with cursor number baseCur+i for the i-th cursor.
       
 74432 ** Except, if there is no possibility of a REPLACE action then
       
 74433 ** cursors do not need to be open for indices where aRegIdx[i]==0.
       
 74434 */
       
 74435 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
       
 74436   Parse *pParse,      /* The parser context */
       
 74437   Table *pTab,        /* the table into which we are inserting */
       
 74438   int baseCur,        /* Index of a read/write cursor pointing at pTab */
       
 74439   int regRowid,       /* Index of the range of input registers */
       
 74440   int *aRegIdx,       /* Register used by each index.  0 for unused indices */
       
 74441   int rowidChng,      /* True if the rowid might collide with existing entry */
       
 74442   int isUpdate,       /* True for UPDATE, False for INSERT */
       
 74443   int overrideError,  /* Override onError to this if not OE_Default */
       
 74444   int ignoreDest,     /* Jump to this label on an OE_Ignore resolution */
       
 74445   int *pbMayReplace   /* OUT: Set to true if constraint may cause a replace */
       
 74446 ){
       
 74447   int i;              /* loop counter */
       
 74448   Vdbe *v;            /* VDBE under constrution */
       
 74449   int nCol;           /* Number of columns */
       
 74450   int onError;        /* Conflict resolution strategy */
       
 74451   int j1;             /* Addresss of jump instruction */
       
 74452   int j2 = 0, j3;     /* Addresses of jump instructions */
       
 74453   int regData;        /* Register containing first data column */
       
 74454   int iCur;           /* Table cursor number */
       
 74455   Index *pIdx;         /* Pointer to one of the indices */
       
 74456   int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
       
 74457   int regOldRowid = (rowidChng && isUpdate) ? rowidChng : regRowid;
       
 74458 
       
 74459   v = sqlite3GetVdbe(pParse);
       
 74460   assert( v!=0 );
       
 74461   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
       
 74462   nCol = pTab->nCol;
       
 74463   regData = regRowid + 1;
       
 74464 
       
 74465   /* Test all NOT NULL constraints.
       
 74466   */
       
 74467   for(i=0; i<nCol; i++){
       
 74468     if( i==pTab->iPKey ){
       
 74469       continue;
       
 74470     }
       
 74471     onError = pTab->aCol[i].notNull;
       
 74472     if( onError==OE_None ) continue;
       
 74473     if( overrideError!=OE_Default ){
       
 74474       onError = overrideError;
       
 74475     }else if( onError==OE_Default ){
       
 74476       onError = OE_Abort;
       
 74477     }
       
 74478     if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
       
 74479       onError = OE_Abort;
       
 74480     }
       
 74481     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
       
 74482         || onError==OE_Ignore || onError==OE_Replace );
       
 74483     switch( onError ){
       
 74484       case OE_Abort:
       
 74485         sqlite3MayAbort(pParse);
       
 74486       case OE_Rollback:
       
 74487       case OE_Fail: {
       
 74488         char *zMsg;
       
 74489         j1 = sqlite3VdbeAddOp3(v, OP_HaltIfNull,
       
 74490                                   SQLITE_CONSTRAINT, onError, regData+i);
       
 74491         zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL",
       
 74492                               pTab->zName, pTab->aCol[i].zName);
       
 74493         sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
       
 74494         break;
       
 74495       }
       
 74496       case OE_Ignore: {
       
 74497         sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
       
 74498         break;
       
 74499       }
       
 74500       default: {
       
 74501         assert( onError==OE_Replace );
       
 74502         j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i);
       
 74503         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i);
       
 74504         sqlite3VdbeJumpHere(v, j1);
       
 74505         break;
       
 74506       }
       
 74507     }
       
 74508   }
       
 74509 
       
 74510   /* Test all CHECK constraints
       
 74511   */
       
 74512 #ifndef SQLITE_OMIT_CHECK
       
 74513   if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
       
 74514     int allOk = sqlite3VdbeMakeLabel(v);
       
 74515     pParse->ckBase = regData;
       
 74516     sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL);
       
 74517     onError = overrideError!=OE_Default ? overrideError : OE_Abort;
       
 74518     if( onError==OE_Ignore ){
       
 74519       sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
       
 74520     }else{
       
 74521       sqlite3HaltConstraint(pParse, onError, 0, 0);
       
 74522     }
       
 74523     sqlite3VdbeResolveLabel(v, allOk);
       
 74524   }
       
 74525 #endif /* !defined(SQLITE_OMIT_CHECK) */
       
 74526 
       
 74527   /* If we have an INTEGER PRIMARY KEY, make sure the primary key
       
 74528   ** of the new record does not previously exist.  Except, if this
       
 74529   ** is an UPDATE and the primary key is not changing, that is OK.
       
 74530   */
       
 74531   if( rowidChng ){
       
 74532     onError = pTab->keyConf;
       
 74533     if( overrideError!=OE_Default ){
       
 74534       onError = overrideError;
       
 74535     }else if( onError==OE_Default ){
       
 74536       onError = OE_Abort;
       
 74537     }
       
 74538     
       
 74539     if( isUpdate ){
       
 74540       j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
       
 74541     }
       
 74542     j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
       
 74543     switch( onError ){
       
 74544       default: {
       
 74545         onError = OE_Abort;
       
 74546         /* Fall thru into the next case */
       
 74547       }
       
 74548       case OE_Rollback:
       
 74549       case OE_Abort:
       
 74550       case OE_Fail: {
       
 74551         sqlite3HaltConstraint(
       
 74552           pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
       
 74553         break;
       
 74554       }
       
 74555       case OE_Replace: {
       
 74556         /* If there are DELETE triggers on this table and the
       
 74557         ** recursive-triggers flag is set, call GenerateRowDelete() to
       
 74558         ** remove the conflicting row from the the table. This will fire
       
 74559         ** the triggers and remove both the table and index b-tree entries.
       
 74560         **
       
 74561         ** Otherwise, if there are no triggers or the recursive-triggers
       
 74562         ** flag is not set, call GenerateRowIndexDelete(). This removes
       
 74563         ** the index b-tree entries only. The table b-tree entry will be 
       
 74564         ** replaced by the new entry when it is inserted.  */
       
 74565         Trigger *pTrigger = 0;
       
 74566         if( pParse->db->flags&SQLITE_RecTriggers ){
       
 74567           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
       
 74568         }
       
 74569         sqlite3MultiWrite(pParse);
       
 74570         if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
       
 74571           sqlite3GenerateRowDelete(
       
 74572               pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
       
 74573           );
       
 74574         }else{
       
 74575           sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
       
 74576         }
       
 74577         seenReplace = 1;
       
 74578         break;
       
 74579       }
       
 74580       case OE_Ignore: {
       
 74581         assert( seenReplace==0 );
       
 74582         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
       
 74583         break;
       
 74584       }
       
 74585     }
       
 74586     sqlite3VdbeJumpHere(v, j3);
       
 74587     if( isUpdate ){
       
 74588       sqlite3VdbeJumpHere(v, j2);
       
 74589     }
       
 74590   }
       
 74591 
       
 74592   /* Test all UNIQUE constraints by creating entries for each UNIQUE
       
 74593   ** index and making sure that duplicate entries do not already exist.
       
 74594   ** Add the new records to the indices as we go.
       
 74595   */
       
 74596   for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
       
 74597     int regIdx;
       
 74598     int regR;
       
 74599 
       
 74600     if( aRegIdx[iCur]==0 ) continue;  /* Skip unused indices */
       
 74601 
       
 74602     /* Create a key for accessing the index entry */
       
 74603     regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
       
 74604     for(i=0; i<pIdx->nColumn; i++){
       
 74605       int idx = pIdx->aiColumn[i];
       
 74606       if( idx==pTab->iPKey ){
       
 74607         sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
       
 74608       }else{
       
 74609         sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
       
 74610       }
       
 74611     }
       
 74612     sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
       
 74613     sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
       
 74614     sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0);
       
 74615     sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
       
 74616 
       
 74617     /* Find out what action to take in case there is an indexing conflict */
       
 74618     onError = pIdx->onError;
       
 74619     if( onError==OE_None ){ 
       
 74620       sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
       
 74621       continue;  /* pIdx is not a UNIQUE index */
       
 74622     }
       
 74623     if( overrideError!=OE_Default ){
       
 74624       onError = overrideError;
       
 74625     }else if( onError==OE_Default ){
       
 74626       onError = OE_Abort;
       
 74627     }
       
 74628     if( seenReplace ){
       
 74629       if( onError==OE_Ignore ) onError = OE_Replace;
       
 74630       else if( onError==OE_Fail ) onError = OE_Abort;
       
 74631     }
       
 74632     
       
 74633     /* Check to see if the new index entry will be unique */
       
 74634     regR = sqlite3GetTempReg(pParse);
       
 74635     sqlite3VdbeAddOp2(v, OP_SCopy, regOldRowid, regR);
       
 74636     j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0,
       
 74637                            regR, SQLITE_INT_TO_PTR(regIdx),
       
 74638                            P4_INT32);
       
 74639     sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
       
 74640 
       
 74641     /* Generate code that executes if the new index entry is not unique */
       
 74642     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
       
 74643         || onError==OE_Ignore || onError==OE_Replace );
       
 74644     switch( onError ){
       
 74645       case OE_Rollback:
       
 74646       case OE_Abort:
       
 74647       case OE_Fail: {
       
 74648         int j;
       
 74649         StrAccum errMsg;
       
 74650         const char *zSep;
       
 74651         char *zErr;
       
 74652 
       
 74653         sqlite3StrAccumInit(&errMsg, 0, 0, 200);
       
 74654         errMsg.db = pParse->db;
       
 74655         zSep = pIdx->nColumn>1 ? "columns " : "column ";
       
 74656         for(j=0; j<pIdx->nColumn; j++){
       
 74657           char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
       
 74658           sqlite3StrAccumAppend(&errMsg, zSep, -1);
       
 74659           zSep = ", ";
       
 74660           sqlite3StrAccumAppend(&errMsg, zCol, -1);
       
 74661         }
       
 74662         sqlite3StrAccumAppend(&errMsg,
       
 74663             pIdx->nColumn>1 ? " are not unique" : " is not unique", -1);
       
 74664         zErr = sqlite3StrAccumFinish(&errMsg);
       
 74665         sqlite3HaltConstraint(pParse, onError, zErr, 0);
       
 74666         sqlite3DbFree(errMsg.db, zErr);
       
 74667         break;
       
 74668       }
       
 74669       case OE_Ignore: {
       
 74670         assert( seenReplace==0 );
       
 74671         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
       
 74672         break;
       
 74673       }
       
 74674       default: {
       
 74675         Trigger *pTrigger = 0;
       
 74676         assert( onError==OE_Replace );
       
 74677         sqlite3MultiWrite(pParse);
       
 74678         if( pParse->db->flags&SQLITE_RecTriggers ){
       
 74679           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
       
 74680         }
       
 74681         sqlite3GenerateRowDelete(
       
 74682             pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
       
 74683         );
       
 74684         seenReplace = 1;
       
 74685         break;
       
 74686       }
       
 74687     }
       
 74688     sqlite3VdbeJumpHere(v, j3);
       
 74689     sqlite3ReleaseTempReg(pParse, regR);
       
 74690   }
       
 74691   
       
 74692   if( pbMayReplace ){
       
 74693     *pbMayReplace = seenReplace;
       
 74694   }
       
 74695 }
       
 74696 
       
 74697 /*
       
 74698 ** This routine generates code to finish the INSERT or UPDATE operation
       
 74699 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
       
 74700 ** A consecutive range of registers starting at regRowid contains the
       
 74701 ** rowid and the content to be inserted.
       
 74702 **
       
 74703 ** The arguments to this routine should be the same as the first six
       
 74704 ** arguments to sqlite3GenerateConstraintChecks.
       
 74705 */
       
 74706 SQLITE_PRIVATE void sqlite3CompleteInsertion(
       
 74707   Parse *pParse,      /* The parser context */
       
 74708   Table *pTab,        /* the table into which we are inserting */
       
 74709   int baseCur,        /* Index of a read/write cursor pointing at pTab */
       
 74710   int regRowid,       /* Range of content */
       
 74711   int *aRegIdx,       /* Register used by each index.  0 for unused indices */
       
 74712   int isUpdate,       /* True for UPDATE, False for INSERT */
       
 74713   int appendBias,     /* True if this is likely to be an append */
       
 74714   int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
       
 74715 ){
       
 74716   int i;
       
 74717   Vdbe *v;
       
 74718   int nIdx;
       
 74719   Index *pIdx;
       
 74720   u8 pik_flags;
       
 74721   int regData;
       
 74722   int regRec;
       
 74723 
       
 74724   v = sqlite3GetVdbe(pParse);
       
 74725   assert( v!=0 );
       
 74726   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
       
 74727   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
       
 74728   for(i=nIdx-1; i>=0; i--){
       
 74729     if( aRegIdx[i]==0 ) continue;
       
 74730     sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]);
       
 74731     if( useSeekResult ){
       
 74732       sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
       
 74733     }
       
 74734   }
       
 74735   regData = regRowid + 1;
       
 74736   regRec = sqlite3GetTempReg(pParse);
       
 74737   sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
       
 74738   sqlite3TableAffinityStr(v, pTab);
       
 74739   sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
       
 74740   if( pParse->nested ){
       
 74741     pik_flags = 0;
       
 74742   }else{
       
 74743     pik_flags = OPFLAG_NCHANGE;
       
 74744     pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
       
 74745   }
       
 74746   if( appendBias ){
       
 74747     pik_flags |= OPFLAG_APPEND;
       
 74748   }
       
 74749   if( useSeekResult ){
       
 74750     pik_flags |= OPFLAG_USESEEKRESULT;
       
 74751   }
       
 74752   sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
       
 74753   if( !pParse->nested ){
       
 74754     sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC);
       
 74755   }
       
 74756   sqlite3VdbeChangeP5(v, pik_flags);
       
 74757 }
       
 74758 
       
 74759 /*
       
 74760 ** Generate code that will open cursors for a table and for all
       
 74761 ** indices of that table.  The "baseCur" parameter is the cursor number used
       
 74762 ** for the table.  Indices are opened on subsequent cursors.
       
 74763 **
       
 74764 ** Return the number of indices on the table.
       
 74765 */
       
 74766 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
       
 74767   Parse *pParse,   /* Parsing context */
       
 74768   Table *pTab,     /* Table to be opened */
       
 74769   int baseCur,     /* Cursor number assigned to the table */
       
 74770   int op           /* OP_OpenRead or OP_OpenWrite */
       
 74771 ){
       
 74772   int i;
       
 74773   int iDb;
       
 74774   Index *pIdx;
       
 74775   Vdbe *v;
       
 74776 
       
 74777   if( IsVirtual(pTab) ) return 0;
       
 74778   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
       
 74779   v = sqlite3GetVdbe(pParse);
       
 74780   assert( v!=0 );
       
 74781   sqlite3OpenTable(pParse, baseCur, iDb, pTab, op);
       
 74782   for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
       
 74783     KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
       
 74784     assert( pIdx->pSchema==pTab->pSchema );
       
 74785     sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb,
       
 74786                       (char*)pKey, P4_KEYINFO_HANDOFF);
       
 74787     VdbeComment((v, "%s", pIdx->zName));
       
 74788   }
       
 74789   if( pParse->nTab<baseCur+i ){
       
 74790     pParse->nTab = baseCur+i;
       
 74791   }
       
 74792   return i-1;
       
 74793 }
       
 74794 
       
 74795 
       
 74796 #ifdef SQLITE_TEST
       
 74797 /*
       
 74798 ** The following global variable is incremented whenever the
       
 74799 ** transfer optimization is used.  This is used for testing
       
 74800 ** purposes only - to make sure the transfer optimization really
       
 74801 ** is happening when it is suppose to.
       
 74802 */
       
 74803 SQLITE_API int sqlite3_xferopt_count;
       
 74804 #endif /* SQLITE_TEST */
       
 74805 
       
 74806 
       
 74807 #ifndef SQLITE_OMIT_XFER_OPT
       
 74808 /*
       
 74809 ** Check to collation names to see if they are compatible.
       
 74810 */
       
 74811 static int xferCompatibleCollation(const char *z1, const char *z2){
       
 74812   if( z1==0 ){
       
 74813     return z2==0;
       
 74814   }
       
 74815   if( z2==0 ){
       
 74816     return 0;
       
 74817   }
       
 74818   return sqlite3StrICmp(z1, z2)==0;
       
 74819 }
       
 74820 
       
 74821 
       
 74822 /*
       
 74823 ** Check to see if index pSrc is compatible as a source of data
       
 74824 ** for index pDest in an insert transfer optimization.  The rules
       
 74825 ** for a compatible index:
       
 74826 **
       
 74827 **    *   The index is over the same set of columns
       
 74828 **    *   The same DESC and ASC markings occurs on all columns
       
 74829 **    *   The same onError processing (OE_Abort, OE_Ignore, etc)
       
 74830 **    *   The same collating sequence on each column
       
 74831 */
       
 74832 static int xferCompatibleIndex(Index *pDest, Index *pSrc){
       
 74833   int i;
       
 74834   assert( pDest && pSrc );
       
 74835   assert( pDest->pTable!=pSrc->pTable );
       
 74836   if( pDest->nColumn!=pSrc->nColumn ){
       
 74837     return 0;   /* Different number of columns */
       
 74838   }
       
 74839   if( pDest->onError!=pSrc->onError ){
       
 74840     return 0;   /* Different conflict resolution strategies */
       
 74841   }
       
 74842   for(i=0; i<pSrc->nColumn; i++){
       
 74843     if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
       
 74844       return 0;   /* Different columns indexed */
       
 74845     }
       
 74846     if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
       
 74847       return 0;   /* Different sort orders */
       
 74848     }
       
 74849     if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
       
 74850       return 0;   /* Different collating sequences */
       
 74851     }
       
 74852   }
       
 74853 
       
 74854   /* If no test above fails then the indices must be compatible */
       
 74855   return 1;
       
 74856 }
       
 74857 
       
 74858 /*
       
 74859 ** Attempt the transfer optimization on INSERTs of the form
       
 74860 **
       
 74861 **     INSERT INTO tab1 SELECT * FROM tab2;
       
 74862 **
       
 74863 ** This optimization is only attempted if
       
 74864 **
       
 74865 **    (1)  tab1 and tab2 have identical schemas including all the
       
 74866 **         same indices and constraints
       
 74867 **
       
 74868 **    (2)  tab1 and tab2 are different tables
       
 74869 **
       
 74870 **    (3)  There must be no triggers on tab1
       
 74871 **
       
 74872 **    (4)  The result set of the SELECT statement is "*"
       
 74873 **
       
 74874 **    (5)  The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY,
       
 74875 **         or LIMIT clause.
       
 74876 **
       
 74877 **    (6)  The SELECT statement is a simple (not a compound) select that
       
 74878 **         contains only tab2 in its FROM clause
       
 74879 **
       
 74880 ** This method for implementing the INSERT transfers raw records from
       
 74881 ** tab2 over to tab1.  The columns are not decoded.  Raw records from
       
 74882 ** the indices of tab2 are transfered to tab1 as well.  In so doing,
       
 74883 ** the resulting tab1 has much less fragmentation.
       
 74884 **
       
 74885 ** This routine returns TRUE if the optimization is attempted.  If any
       
 74886 ** of the conditions above fail so that the optimization should not
       
 74887 ** be attempted, then this routine returns FALSE.
       
 74888 */
       
 74889 static int xferOptimization(
       
 74890   Parse *pParse,        /* Parser context */
       
 74891   Table *pDest,         /* The table we are inserting into */
       
 74892   Select *pSelect,      /* A SELECT statement to use as the data source */
       
 74893   int onError,          /* How to handle constraint errors */
       
 74894   int iDbDest           /* The database of pDest */
       
 74895 ){
       
 74896   ExprList *pEList;                /* The result set of the SELECT */
       
 74897   Table *pSrc;                     /* The table in the FROM clause of SELECT */
       
 74898   Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
       
 74899   struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
       
 74900   int i;                           /* Loop counter */
       
 74901   int iDbSrc;                      /* The database of pSrc */
       
 74902   int iSrc, iDest;                 /* Cursors from source and destination */
       
 74903   int addr1, addr2;                /* Loop addresses */
       
 74904   int emptyDestTest;               /* Address of test for empty pDest */
       
 74905   int emptySrcTest;                /* Address of test for empty pSrc */
       
 74906   Vdbe *v;                         /* The VDBE we are building */
       
 74907   KeyInfo *pKey;                   /* Key information for an index */
       
 74908   int regAutoinc;                  /* Memory register used by AUTOINC */
       
 74909   int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
       
 74910   int regData, regRowid;           /* Registers holding data and rowid */
       
 74911 
       
 74912   if( pSelect==0 ){
       
 74913     return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
       
 74914   }
       
 74915   if( sqlite3TriggerList(pParse, pDest) ){
       
 74916     return 0;   /* tab1 must not have triggers */
       
 74917   }
       
 74918 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 74919   if( pDest->tabFlags & TF_Virtual ){
       
 74920     return 0;   /* tab1 must not be a virtual table */
       
 74921   }
       
 74922 #endif
       
 74923   if( onError==OE_Default ){
       
 74924     onError = OE_Abort;
       
 74925   }
       
 74926   if( onError!=OE_Abort && onError!=OE_Rollback ){
       
 74927     return 0;   /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */
       
 74928   }
       
 74929   assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
       
 74930   if( pSelect->pSrc->nSrc!=1 ){
       
 74931     return 0;   /* FROM clause must have exactly one term */
       
 74932   }
       
 74933   if( pSelect->pSrc->a[0].pSelect ){
       
 74934     return 0;   /* FROM clause cannot contain a subquery */
       
 74935   }
       
 74936   if( pSelect->pWhere ){
       
 74937     return 0;   /* SELECT may not have a WHERE clause */
       
 74938   }
       
 74939   if( pSelect->pOrderBy ){
       
 74940     return 0;   /* SELECT may not have an ORDER BY clause */
       
 74941   }
       
 74942   /* Do not need to test for a HAVING clause.  If HAVING is present but
       
 74943   ** there is no ORDER BY, we will get an error. */
       
 74944   if( pSelect->pGroupBy ){
       
 74945     return 0;   /* SELECT may not have a GROUP BY clause */
       
 74946   }
       
 74947   if( pSelect->pLimit ){
       
 74948     return 0;   /* SELECT may not have a LIMIT clause */
       
 74949   }
       
 74950   assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
       
 74951   if( pSelect->pPrior ){
       
 74952     return 0;   /* SELECT may not be a compound query */
       
 74953   }
       
 74954   if( pSelect->selFlags & SF_Distinct ){
       
 74955     return 0;   /* SELECT may not be DISTINCT */
       
 74956   }
       
 74957   pEList = pSelect->pEList;
       
 74958   assert( pEList!=0 );
       
 74959   if( pEList->nExpr!=1 ){
       
 74960     return 0;   /* The result set must have exactly one column */
       
 74961   }
       
 74962   assert( pEList->a[0].pExpr );
       
 74963   if( pEList->a[0].pExpr->op!=TK_ALL ){
       
 74964     return 0;   /* The result set must be the special operator "*" */
       
 74965   }
       
 74966 
       
 74967   /* At this point we have established that the statement is of the
       
 74968   ** correct syntactic form to participate in this optimization.  Now
       
 74969   ** we have to check the semantics.
       
 74970   */
       
 74971   pItem = pSelect->pSrc->a;
       
 74972   pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
       
 74973   if( pSrc==0 ){
       
 74974     return 0;   /* FROM clause does not contain a real table */
       
 74975   }
       
 74976   if( pSrc==pDest ){
       
 74977     return 0;   /* tab1 and tab2 may not be the same table */
       
 74978   }
       
 74979 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 74980   if( pSrc->tabFlags & TF_Virtual ){
       
 74981     return 0;   /* tab2 must not be a virtual table */
       
 74982   }
       
 74983 #endif
       
 74984   if( pSrc->pSelect ){
       
 74985     return 0;   /* tab2 may not be a view */
       
 74986   }
       
 74987   if( pDest->nCol!=pSrc->nCol ){
       
 74988     return 0;   /* Number of columns must be the same in tab1 and tab2 */
       
 74989   }
       
 74990   if( pDest->iPKey!=pSrc->iPKey ){
       
 74991     return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
       
 74992   }
       
 74993   for(i=0; i<pDest->nCol; i++){
       
 74994     if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
       
 74995       return 0;    /* Affinity must be the same on all columns */
       
 74996     }
       
 74997     if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
       
 74998       return 0;    /* Collating sequence must be the same on all columns */
       
 74999     }
       
 75000     if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
       
 75001       return 0;    /* tab2 must be NOT NULL if tab1 is */
       
 75002     }
       
 75003   }
       
 75004   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
       
 75005     if( pDestIdx->onError!=OE_None ){
       
 75006       destHasUniqueIdx = 1;
       
 75007     }
       
 75008     for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
       
 75009       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
       
 75010     }
       
 75011     if( pSrcIdx==0 ){
       
 75012       return 0;    /* pDestIdx has no corresponding index in pSrc */
       
 75013     }
       
 75014   }
       
 75015 #ifndef SQLITE_OMIT_CHECK
       
 75016   if( pDest->pCheck && !sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
       
 75017     return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
       
 75018   }
       
 75019 #endif
       
 75020 
       
 75021   /* If we get this far, it means either:
       
 75022   **
       
 75023   **    *   We can always do the transfer if the table contains an
       
 75024   **        an integer primary key
       
 75025   **
       
 75026   **    *   We can conditionally do the transfer if the destination
       
 75027   **        table is empty.
       
 75028   */
       
 75029 #ifdef SQLITE_TEST
       
 75030   sqlite3_xferopt_count++;
       
 75031 #endif
       
 75032   iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
       
 75033   v = sqlite3GetVdbe(pParse);
       
 75034   sqlite3CodeVerifySchema(pParse, iDbSrc);
       
 75035   iSrc = pParse->nTab++;
       
 75036   iDest = pParse->nTab++;
       
 75037   regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
       
 75038   sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
       
 75039   if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){
       
 75040     /* If tables do not have an INTEGER PRIMARY KEY and there
       
 75041     ** are indices to be copied and the destination is not empty,
       
 75042     ** we have to disallow the transfer optimization because the
       
 75043     ** the rowids might change which will mess up indexing.
       
 75044     **
       
 75045     ** Or if the destination has a UNIQUE index and is not empty,
       
 75046     ** we also disallow the transfer optimization because we cannot
       
 75047     ** insure that all entries in the union of DEST and SRC will be
       
 75048     ** unique.
       
 75049     */
       
 75050     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
       
 75051     emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
       
 75052     sqlite3VdbeJumpHere(v, addr1);
       
 75053   }else{
       
 75054     emptyDestTest = 0;
       
 75055   }
       
 75056   sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
       
 75057   emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
       
 75058   regData = sqlite3GetTempReg(pParse);
       
 75059   regRowid = sqlite3GetTempReg(pParse);
       
 75060   if( pDest->iPKey>=0 ){
       
 75061     addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
       
 75062     addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
       
 75063     sqlite3HaltConstraint(
       
 75064         pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
       
 75065     sqlite3VdbeJumpHere(v, addr2);
       
 75066     autoIncStep(pParse, regAutoinc, regRowid);
       
 75067   }else if( pDest->pIndex==0 ){
       
 75068     addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
       
 75069   }else{
       
 75070     addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
       
 75071     assert( (pDest->tabFlags & TF_Autoincrement)==0 );
       
 75072   }
       
 75073   sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
       
 75074   sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
       
 75075   sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
       
 75076   sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
       
 75077   sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
       
 75078   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
       
 75079     for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
       
 75080       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
       
 75081     }
       
 75082     assert( pSrcIdx );
       
 75083     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
       
 75084     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
       
 75085     pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
       
 75086     sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc,
       
 75087                       (char*)pKey, P4_KEYINFO_HANDOFF);
       
 75088     VdbeComment((v, "%s", pSrcIdx->zName));
       
 75089     pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
       
 75090     sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest,
       
 75091                       (char*)pKey, P4_KEYINFO_HANDOFF);
       
 75092     VdbeComment((v, "%s", pDestIdx->zName));
       
 75093     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
       
 75094     sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
       
 75095     sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
       
 75096     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
       
 75097     sqlite3VdbeJumpHere(v, addr1);
       
 75098   }
       
 75099   sqlite3VdbeJumpHere(v, emptySrcTest);
       
 75100   sqlite3ReleaseTempReg(pParse, regRowid);
       
 75101   sqlite3ReleaseTempReg(pParse, regData);
       
 75102   sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
       
 75103   sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
       
 75104   if( emptyDestTest ){
       
 75105     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
       
 75106     sqlite3VdbeJumpHere(v, emptyDestTest);
       
 75107     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
       
 75108     return 0;
       
 75109   }else{
       
 75110     return 1;
       
 75111   }
       
 75112 }
       
 75113 #endif /* SQLITE_OMIT_XFER_OPT */
       
 75114 
       
 75115 /************** End of insert.c **********************************************/
       
 75116 /************** Begin file legacy.c ******************************************/
       
 75117 /*
       
 75118 ** 2001 September 15
       
 75119 **
       
 75120 ** The author disclaims copyright to this source code.  In place of
       
 75121 ** a legal notice, here is a blessing:
       
 75122 **
       
 75123 **    May you do good and not evil.
       
 75124 **    May you find forgiveness for yourself and forgive others.
       
 75125 **    May you share freely, never taking more than you give.
       
 75126 **
       
 75127 *************************************************************************
       
 75128 ** Main file for the SQLite library.  The routines in this file
       
 75129 ** implement the programmer interface to the library.  Routines in
       
 75130 ** other files are for internal use by SQLite and should not be
       
 75131 ** accessed by users of the library.
       
 75132 **
       
 75133 ** $Id: legacy.c,v 1.35 2009/08/07 16:56:00 danielk1977 Exp $
       
 75134 */
       
 75135 
       
 75136 
       
 75137 /*
       
 75138 ** Execute SQL code.  Return one of the SQLITE_ success/failure
       
 75139 ** codes.  Also write an error message into memory obtained from
       
 75140 ** malloc() and make *pzErrMsg point to that message.
       
 75141 **
       
 75142 ** If the SQL is a query, then for each row in the query result
       
 75143 ** the xCallback() function is called.  pArg becomes the first
       
 75144 ** argument to xCallback().  If xCallback=NULL then no callback
       
 75145 ** is invoked, even for queries.
       
 75146 */
       
 75147 SQLITE_API int sqlite3_exec(
       
 75148   sqlite3 *db,                /* The database on which the SQL executes */
       
 75149   const char *zSql,           /* The SQL to be executed */
       
 75150   sqlite3_callback xCallback, /* Invoke this callback routine */
       
 75151   void *pArg,                 /* First argument to xCallback() */
       
 75152   char **pzErrMsg             /* Write error messages here */
       
 75153 ){
       
 75154   int rc = SQLITE_OK;         /* Return code */
       
 75155   const char *zLeftover;      /* Tail of unprocessed SQL */
       
 75156   sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
       
 75157   char **azCols = 0;          /* Names of result columns */
       
 75158   int nRetry = 0;             /* Number of retry attempts */
       
 75159   int callbackIsInit;         /* True if callback data is initialized */
       
 75160 
       
 75161   if( zSql==0 ) zSql = "";
       
 75162 
       
 75163   sqlite3_mutex_enter(db->mutex);
       
 75164   sqlite3Error(db, SQLITE_OK, 0);
       
 75165   while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
       
 75166     int nCol;
       
 75167     char **azVals = 0;
       
 75168 
       
 75169     pStmt = 0;
       
 75170     rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
       
 75171     assert( rc==SQLITE_OK || pStmt==0 );
       
 75172     if( rc!=SQLITE_OK ){
       
 75173       continue;
       
 75174     }
       
 75175     if( !pStmt ){
       
 75176       /* this happens for a comment or white-space */
       
 75177       zSql = zLeftover;
       
 75178       continue;
       
 75179     }
       
 75180 
       
 75181     callbackIsInit = 0;
       
 75182     nCol = sqlite3_column_count(pStmt);
       
 75183 
       
 75184     while( 1 ){
       
 75185       int i;
       
 75186       rc = sqlite3_step(pStmt);
       
 75187 
       
 75188       /* Invoke the callback function if required */
       
 75189       if( xCallback && (SQLITE_ROW==rc || 
       
 75190           (SQLITE_DONE==rc && !callbackIsInit
       
 75191                            && db->flags&SQLITE_NullCallback)) ){
       
 75192         if( !callbackIsInit ){
       
 75193           azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
       
 75194           if( azCols==0 ){
       
 75195             goto exec_out;
       
 75196           }
       
 75197           for(i=0; i<nCol; i++){
       
 75198             azCols[i] = (char *)sqlite3_column_name(pStmt, i);
       
 75199             /* sqlite3VdbeSetColName() installs column names as UTF8
       
 75200             ** strings so there is no way for sqlite3_column_name() to fail. */
       
 75201             assert( azCols[i]!=0 );
       
 75202           }
       
 75203           callbackIsInit = 1;
       
 75204         }
       
 75205         if( rc==SQLITE_ROW ){
       
 75206           azVals = &azCols[nCol];
       
 75207           for(i=0; i<nCol; i++){
       
 75208             azVals[i] = (char *)sqlite3_column_text(pStmt, i);
       
 75209             if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
       
 75210               db->mallocFailed = 1;
       
 75211               goto exec_out;
       
 75212             }
       
 75213           }
       
 75214         }
       
 75215         if( xCallback(pArg, nCol, azVals, azCols) ){
       
 75216           rc = SQLITE_ABORT;
       
 75217           sqlite3VdbeFinalize((Vdbe *)pStmt);
       
 75218           pStmt = 0;
       
 75219           sqlite3Error(db, SQLITE_ABORT, 0);
       
 75220           goto exec_out;
       
 75221         }
       
 75222       }
       
 75223 
       
 75224       if( rc!=SQLITE_ROW ){
       
 75225         rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
       
 75226         pStmt = 0;
       
 75227         if( rc!=SQLITE_SCHEMA ){
       
 75228           nRetry = 0;
       
 75229           zSql = zLeftover;
       
 75230           while( sqlite3Isspace(zSql[0]) ) zSql++;
       
 75231         }
       
 75232         break;
       
 75233       }
       
 75234     }
       
 75235 
       
 75236     sqlite3DbFree(db, azCols);
       
 75237     azCols = 0;
       
 75238   }
       
 75239 
       
 75240 exec_out:
       
 75241   if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
       
 75242   sqlite3DbFree(db, azCols);
       
 75243 
       
 75244   rc = sqlite3ApiExit(db, rc);
       
 75245   if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
       
 75246     int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
       
 75247     *pzErrMsg = sqlite3Malloc(nErrMsg);
       
 75248     if( *pzErrMsg ){
       
 75249       memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
       
 75250     }else{
       
 75251       rc = SQLITE_NOMEM;
       
 75252       sqlite3Error(db, SQLITE_NOMEM, 0);
       
 75253     }
       
 75254   }else if( pzErrMsg ){
       
 75255     *pzErrMsg = 0;
       
 75256   }
       
 75257 
       
 75258   assert( (rc&db->errMask)==rc );
       
 75259   sqlite3_mutex_leave(db->mutex);
       
 75260   return rc;
       
 75261 }
       
 75262 
       
 75263 /************** End of legacy.c **********************************************/
       
 75264 /************** Begin file loadext.c *****************************************/
       
 75265 /*
       
 75266 ** 2006 June 7
       
 75267 **
       
 75268 ** The author disclaims copyright to this source code.  In place of
       
 75269 ** a legal notice, here is a blessing:
       
 75270 **
       
 75271 **    May you do good and not evil.
       
 75272 **    May you find forgiveness for yourself and forgive others.
       
 75273 **    May you share freely, never taking more than you give.
       
 75274 **
       
 75275 *************************************************************************
       
 75276 ** This file contains code used to dynamically load extensions into
       
 75277 ** the SQLite library.
       
 75278 **
       
 75279 ** $Id: loadext.c,v 1.60 2009/06/03 01:24:54 drh Exp $
       
 75280 */
       
 75281 
       
 75282 #ifndef SQLITE_CORE
       
 75283   #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
       
 75284 #endif
       
 75285 /************** Include sqlite3ext.h in the middle of loadext.c **************/
       
 75286 /************** Begin file sqlite3ext.h **************************************/
       
 75287 /*
       
 75288 ** 2006 June 7
       
 75289 **
       
 75290 ** The author disclaims copyright to this source code.  In place of
       
 75291 ** a legal notice, here is a blessing:
       
 75292 **
       
 75293 **    May you do good and not evil.
       
 75294 **    May you find forgiveness for yourself and forgive others.
       
 75295 **    May you share freely, never taking more than you give.
       
 75296 **
       
 75297 *************************************************************************
       
 75298 ** This header file defines the SQLite interface for use by
       
 75299 ** shared libraries that want to be imported as extensions into
       
 75300 ** an SQLite instance.  Shared libraries that intend to be loaded
       
 75301 ** as extensions by SQLite should #include this file instead of 
       
 75302 ** sqlite3.h.
       
 75303 **
       
 75304 ** @(#) $Id: sqlite3ext.h,v 1.25 2008/10/12 00:27:54 shane Exp $
       
 75305 */
       
 75306 #ifndef _SQLITE3EXT_H_
       
 75307 #define _SQLITE3EXT_H_
       
 75308 
       
 75309 typedef struct sqlite3_api_routines sqlite3_api_routines;
       
 75310 
       
 75311 /*
       
 75312 ** The following structure holds pointers to all of the SQLite API
       
 75313 ** routines.
       
 75314 **
       
 75315 ** WARNING:  In order to maintain backwards compatibility, add new
       
 75316 ** interfaces to the end of this structure only.  If you insert new
       
 75317 ** interfaces in the middle of this structure, then older different
       
 75318 ** versions of SQLite will not be able to load each others' shared
       
 75319 ** libraries!
       
 75320 */
       
 75321 struct sqlite3_api_routines {
       
 75322   void * (*aggregate_context)(sqlite3_context*,int nBytes);
       
 75323   int  (*aggregate_count)(sqlite3_context*);
       
 75324   int  (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
       
 75325   int  (*bind_double)(sqlite3_stmt*,int,double);
       
 75326   int  (*bind_int)(sqlite3_stmt*,int,int);
       
 75327   int  (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
       
 75328   int  (*bind_null)(sqlite3_stmt*,int);
       
 75329   int  (*bind_parameter_count)(sqlite3_stmt*);
       
 75330   int  (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
       
 75331   const char * (*bind_parameter_name)(sqlite3_stmt*,int);
       
 75332   int  (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
       
 75333   int  (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
       
 75334   int  (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
       
 75335   int  (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
       
 75336   int  (*busy_timeout)(sqlite3*,int ms);
       
 75337   int  (*changes)(sqlite3*);
       
 75338   int  (*close)(sqlite3*);
       
 75339   int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const char*));
       
 75340   int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const void*));
       
 75341   const void * (*column_blob)(sqlite3_stmt*,int iCol);
       
 75342   int  (*column_bytes)(sqlite3_stmt*,int iCol);
       
 75343   int  (*column_bytes16)(sqlite3_stmt*,int iCol);
       
 75344   int  (*column_count)(sqlite3_stmt*pStmt);
       
 75345   const char * (*column_database_name)(sqlite3_stmt*,int);
       
 75346   const void * (*column_database_name16)(sqlite3_stmt*,int);
       
 75347   const char * (*column_decltype)(sqlite3_stmt*,int i);
       
 75348   const void * (*column_decltype16)(sqlite3_stmt*,int);
       
 75349   double  (*column_double)(sqlite3_stmt*,int iCol);
       
 75350   int  (*column_int)(sqlite3_stmt*,int iCol);
       
 75351   sqlite_int64  (*column_int64)(sqlite3_stmt*,int iCol);
       
 75352   const char * (*column_name)(sqlite3_stmt*,int);
       
 75353   const void * (*column_name16)(sqlite3_stmt*,int);
       
 75354   const char * (*column_origin_name)(sqlite3_stmt*,int);
       
 75355   const void * (*column_origin_name16)(sqlite3_stmt*,int);
       
 75356   const char * (*column_table_name)(sqlite3_stmt*,int);
       
 75357   const void * (*column_table_name16)(sqlite3_stmt*,int);
       
 75358   const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
       
 75359   const void * (*column_text16)(sqlite3_stmt*,int iCol);
       
 75360   int  (*column_type)(sqlite3_stmt*,int iCol);
       
 75361   sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
       
 75362   void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
       
 75363   int  (*complete)(const char*sql);
       
 75364   int  (*complete16)(const void*sql);
       
 75365   int  (*create_collation)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*));
       
 75366   int  (*create_collation16)(sqlite3*,const void*,int,void*,int(*)(void*,int,const void*,int,const void*));
       
 75367   int  (*create_function)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*));
       
 75368   int  (*create_function16)(sqlite3*,const void*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*));
       
 75369   int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
       
 75370   int  (*data_count)(sqlite3_stmt*pStmt);
       
 75371   sqlite3 * (*db_handle)(sqlite3_stmt*);
       
 75372   int (*declare_vtab)(sqlite3*,const char*);
       
 75373   int  (*enable_shared_cache)(int);
       
 75374   int  (*errcode)(sqlite3*db);
       
 75375   const char * (*errmsg)(sqlite3*);
       
 75376   const void * (*errmsg16)(sqlite3*);
       
 75377   int  (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
       
 75378   int  (*expired)(sqlite3_stmt*);
       
 75379   int  (*finalize)(sqlite3_stmt*pStmt);
       
 75380   void  (*free)(void*);
       
 75381   void  (*free_table)(char**result);
       
 75382   int  (*get_autocommit)(sqlite3*);
       
 75383   void * (*get_auxdata)(sqlite3_context*,int);
       
 75384   int  (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
       
 75385   int  (*global_recover)(void);
       
 75386   void  (*interruptx)(sqlite3*);
       
 75387   sqlite_int64  (*last_insert_rowid)(sqlite3*);
       
 75388   const char * (*libversion)(void);
       
 75389   int  (*libversion_number)(void);
       
 75390   void *(*malloc)(int);
       
 75391   char * (*mprintf)(const char*,...);
       
 75392   int  (*open)(const char*,sqlite3**);
       
 75393   int  (*open16)(const void*,sqlite3**);
       
 75394   int  (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
       
 75395   int  (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
       
 75396   void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
       
 75397   void  (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
       
 75398   void *(*realloc)(void*,int);
       
 75399   int  (*reset)(sqlite3_stmt*pStmt);
       
 75400   void  (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
       
 75401   void  (*result_double)(sqlite3_context*,double);
       
 75402   void  (*result_error)(sqlite3_context*,const char*,int);
       
 75403   void  (*result_error16)(sqlite3_context*,const void*,int);
       
 75404   void  (*result_int)(sqlite3_context*,int);
       
 75405   void  (*result_int64)(sqlite3_context*,sqlite_int64);
       
 75406   void  (*result_null)(sqlite3_context*);
       
 75407   void  (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
       
 75408   void  (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
       
 75409   void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
       
 75410   void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
       
 75411   void  (*result_value)(sqlite3_context*,sqlite3_value*);
       
 75412   void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
       
 75413   int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,const char*,const char*),void*);
       
 75414   void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
       
 75415   char * (*snprintf)(int,char*,const char*,...);
       
 75416   int  (*step)(sqlite3_stmt*);
       
 75417   int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,char const**,char const**,int*,int*,int*);
       
 75418   void  (*thread_cleanup)(void);
       
 75419   int  (*total_changes)(sqlite3*);
       
 75420   void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
       
 75421   int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
       
 75422   void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,sqlite_int64),void*);
       
 75423   void * (*user_data)(sqlite3_context*);
       
 75424   const void * (*value_blob)(sqlite3_value*);
       
 75425   int  (*value_bytes)(sqlite3_value*);
       
 75426   int  (*value_bytes16)(sqlite3_value*);
       
 75427   double  (*value_double)(sqlite3_value*);
       
 75428   int  (*value_int)(sqlite3_value*);
       
 75429   sqlite_int64  (*value_int64)(sqlite3_value*);
       
 75430   int  (*value_numeric_type)(sqlite3_value*);
       
 75431   const unsigned char * (*value_text)(sqlite3_value*);
       
 75432   const void * (*value_text16)(sqlite3_value*);
       
 75433   const void * (*value_text16be)(sqlite3_value*);
       
 75434   const void * (*value_text16le)(sqlite3_value*);
       
 75435   int  (*value_type)(sqlite3_value*);
       
 75436   char *(*vmprintf)(const char*,va_list);
       
 75437   /* Added ??? */
       
 75438   int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
       
 75439   /* Added by 3.3.13 */
       
 75440   int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
       
 75441   int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
       
 75442   int (*clear_bindings)(sqlite3_stmt*);
       
 75443   /* Added by 3.4.1 */
       
 75444   int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,void (*xDestroy)(void *));
       
 75445   /* Added by 3.5.0 */
       
 75446   int (*bind_zeroblob)(sqlite3_stmt*,int,int);
       
 75447   int (*blob_bytes)(sqlite3_blob*);
       
 75448   int (*blob_close)(sqlite3_blob*);
       
 75449   int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,int,sqlite3_blob**);
       
 75450   int (*blob_read)(sqlite3_blob*,void*,int,int);
       
 75451   int (*blob_write)(sqlite3_blob*,const void*,int,int);
       
 75452   int (*create_collation_v2)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*),void(*)(void*));
       
 75453   int (*file_control)(sqlite3*,const char*,int,void*);
       
 75454   sqlite3_int64 (*memory_highwater)(int);
       
 75455   sqlite3_int64 (*memory_used)(void);
       
 75456   sqlite3_mutex *(*mutex_alloc)(int);
       
 75457   void (*mutex_enter)(sqlite3_mutex*);
       
 75458   void (*mutex_free)(sqlite3_mutex*);
       
 75459   void (*mutex_leave)(sqlite3_mutex*);
       
 75460   int (*mutex_try)(sqlite3_mutex*);
       
 75461   int (*open_v2)(const char*,sqlite3**,int,const char*);
       
 75462   int (*release_memory)(int);
       
 75463   void (*result_error_nomem)(sqlite3_context*);
       
 75464   void (*result_error_toobig)(sqlite3_context*);
       
 75465   int (*sleep)(int);
       
 75466   void (*soft_heap_limit)(int);
       
 75467   sqlite3_vfs *(*vfs_find)(const char*);
       
 75468   int (*vfs_register)(sqlite3_vfs*,int);
       
 75469   int (*vfs_unregister)(sqlite3_vfs*);
       
 75470   int (*xthreadsafe)(void);
       
 75471   void (*result_zeroblob)(sqlite3_context*,int);
       
 75472   void (*result_error_code)(sqlite3_context*,int);
       
 75473   int (*test_control)(int, ...);
       
 75474   void (*randomness)(int,void*);
       
 75475   sqlite3 *(*context_db_handle)(sqlite3_context*);
       
 75476   int (*extended_result_codes)(sqlite3*,int);
       
 75477   int (*limit)(sqlite3*,int,int);
       
 75478   sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
       
 75479   const char *(*sql)(sqlite3_stmt*);
       
 75480   int (*status)(int,int*,int*,int);
       
 75481 };
       
 75482 
       
 75483 /*
       
 75484 ** The following macros redefine the API routines so that they are
       
 75485 ** redirected throught the global sqlite3_api structure.
       
 75486 **
       
 75487 ** This header file is also used by the loadext.c source file
       
 75488 ** (part of the main SQLite library - not an extension) so that
       
 75489 ** it can get access to the sqlite3_api_routines structure
       
 75490 ** definition.  But the main library does not want to redefine
       
 75491 ** the API.  So the redefinition macros are only valid if the
       
 75492 ** SQLITE_CORE macros is undefined.
       
 75493 */
       
 75494 #ifndef SQLITE_CORE
       
 75495 #define sqlite3_aggregate_context      sqlite3_api->aggregate_context
       
 75496 #ifndef SQLITE_OMIT_DEPRECATED
       
 75497 #define sqlite3_aggregate_count        sqlite3_api->aggregate_count
       
 75498 #endif
       
 75499 #define sqlite3_bind_blob              sqlite3_api->bind_blob
       
 75500 #define sqlite3_bind_double            sqlite3_api->bind_double
       
 75501 #define sqlite3_bind_int               sqlite3_api->bind_int
       
 75502 #define sqlite3_bind_int64             sqlite3_api->bind_int64
       
 75503 #define sqlite3_bind_null              sqlite3_api->bind_null
       
 75504 #define sqlite3_bind_parameter_count   sqlite3_api->bind_parameter_count
       
 75505 #define sqlite3_bind_parameter_index   sqlite3_api->bind_parameter_index
       
 75506 #define sqlite3_bind_parameter_name    sqlite3_api->bind_parameter_name
       
 75507 #define sqlite3_bind_text              sqlite3_api->bind_text
       
 75508 #define sqlite3_bind_text16            sqlite3_api->bind_text16
       
 75509 #define sqlite3_bind_value             sqlite3_api->bind_value
       
 75510 #define sqlite3_busy_handler           sqlite3_api->busy_handler
       
 75511 #define sqlite3_busy_timeout           sqlite3_api->busy_timeout
       
 75512 #define sqlite3_changes                sqlite3_api->changes
       
 75513 #define sqlite3_close                  sqlite3_api->close
       
 75514 #define sqlite3_collation_needed       sqlite3_api->collation_needed
       
 75515 #define sqlite3_collation_needed16     sqlite3_api->collation_needed16
       
 75516 #define sqlite3_column_blob            sqlite3_api->column_blob
       
 75517 #define sqlite3_column_bytes           sqlite3_api->column_bytes
       
 75518 #define sqlite3_column_bytes16         sqlite3_api->column_bytes16
       
 75519 #define sqlite3_column_count           sqlite3_api->column_count
       
 75520 #define sqlite3_column_database_name   sqlite3_api->column_database_name
       
 75521 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
       
 75522 #define sqlite3_column_decltype        sqlite3_api->column_decltype
       
 75523 #define sqlite3_column_decltype16      sqlite3_api->column_decltype16
       
 75524 #define sqlite3_column_double          sqlite3_api->column_double
       
 75525 #define sqlite3_column_int             sqlite3_api->column_int
       
 75526 #define sqlite3_column_int64           sqlite3_api->column_int64
       
 75527 #define sqlite3_column_name            sqlite3_api->column_name
       
 75528 #define sqlite3_column_name16          sqlite3_api->column_name16
       
 75529 #define sqlite3_column_origin_name     sqlite3_api->column_origin_name
       
 75530 #define sqlite3_column_origin_name16   sqlite3_api->column_origin_name16
       
 75531 #define sqlite3_column_table_name      sqlite3_api->column_table_name
       
 75532 #define sqlite3_column_table_name16    sqlite3_api->column_table_name16
       
 75533 #define sqlite3_column_text            sqlite3_api->column_text
       
 75534 #define sqlite3_column_text16          sqlite3_api->column_text16
       
 75535 #define sqlite3_column_type            sqlite3_api->column_type
       
 75536 #define sqlite3_column_value           sqlite3_api->column_value
       
 75537 #define sqlite3_commit_hook            sqlite3_api->commit_hook
       
 75538 #define sqlite3_complete               sqlite3_api->complete
       
 75539 #define sqlite3_complete16             sqlite3_api->complete16
       
 75540 #define sqlite3_create_collation       sqlite3_api->create_collation
       
 75541 #define sqlite3_create_collation16     sqlite3_api->create_collation16
       
 75542 #define sqlite3_create_function        sqlite3_api->create_function
       
 75543 #define sqlite3_create_function16      sqlite3_api->create_function16
       
 75544 #define sqlite3_create_module          sqlite3_api->create_module
       
 75545 #define sqlite3_create_module_v2       sqlite3_api->create_module_v2
       
 75546 #define sqlite3_data_count             sqlite3_api->data_count
       
 75547 #define sqlite3_db_handle              sqlite3_api->db_handle
       
 75548 #define sqlite3_declare_vtab           sqlite3_api->declare_vtab
       
 75549 #define sqlite3_enable_shared_cache    sqlite3_api->enable_shared_cache
       
 75550 #define sqlite3_errcode                sqlite3_api->errcode
       
 75551 #define sqlite3_errmsg                 sqlite3_api->errmsg
       
 75552 #define sqlite3_errmsg16               sqlite3_api->errmsg16
       
 75553 #define sqlite3_exec                   sqlite3_api->exec
       
 75554 #ifndef SQLITE_OMIT_DEPRECATED
       
 75555 #define sqlite3_expired                sqlite3_api->expired
       
 75556 #endif
       
 75557 #define sqlite3_finalize               sqlite3_api->finalize
       
 75558 #define sqlite3_free                   sqlite3_api->free
       
 75559 #define sqlite3_free_table             sqlite3_api->free_table
       
 75560 #define sqlite3_get_autocommit         sqlite3_api->get_autocommit
       
 75561 #define sqlite3_get_auxdata            sqlite3_api->get_auxdata
       
 75562 #define sqlite3_get_table              sqlite3_api->get_table
       
 75563 #ifndef SQLITE_OMIT_DEPRECATED
       
 75564 #define sqlite3_global_recover         sqlite3_api->global_recover
       
 75565 #endif
       
 75566 #define sqlite3_interrupt              sqlite3_api->interruptx
       
 75567 #define sqlite3_last_insert_rowid      sqlite3_api->last_insert_rowid
       
 75568 #define sqlite3_libversion             sqlite3_api->libversion
       
 75569 #define sqlite3_libversion_number      sqlite3_api->libversion_number
       
 75570 #define sqlite3_malloc                 sqlite3_api->malloc
       
 75571 #define sqlite3_mprintf                sqlite3_api->mprintf
       
 75572 #define sqlite3_open                   sqlite3_api->open
       
 75573 #define sqlite3_open16                 sqlite3_api->open16
       
 75574 #define sqlite3_prepare                sqlite3_api->prepare
       
 75575 #define sqlite3_prepare16              sqlite3_api->prepare16
       
 75576 #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
       
 75577 #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
       
 75578 #define sqlite3_profile                sqlite3_api->profile
       
 75579 #define sqlite3_progress_handler       sqlite3_api->progress_handler
       
 75580 #define sqlite3_realloc                sqlite3_api->realloc
       
 75581 #define sqlite3_reset                  sqlite3_api->reset
       
 75582 #define sqlite3_result_blob            sqlite3_api->result_blob
       
 75583 #define sqlite3_result_double          sqlite3_api->result_double
       
 75584 #define sqlite3_result_error           sqlite3_api->result_error
       
 75585 #define sqlite3_result_error16         sqlite3_api->result_error16
       
 75586 #define sqlite3_result_int             sqlite3_api->result_int
       
 75587 #define sqlite3_result_int64           sqlite3_api->result_int64
       
 75588 #define sqlite3_result_null            sqlite3_api->result_null
       
 75589 #define sqlite3_result_text            sqlite3_api->result_text
       
 75590 #define sqlite3_result_text16          sqlite3_api->result_text16
       
 75591 #define sqlite3_result_text16be        sqlite3_api->result_text16be
       
 75592 #define sqlite3_result_text16le        sqlite3_api->result_text16le
       
 75593 #define sqlite3_result_value           sqlite3_api->result_value
       
 75594 #define sqlite3_rollback_hook          sqlite3_api->rollback_hook
       
 75595 #define sqlite3_set_authorizer         sqlite3_api->set_authorizer
       
 75596 #define sqlite3_set_auxdata            sqlite3_api->set_auxdata
       
 75597 #define sqlite3_snprintf               sqlite3_api->snprintf
       
 75598 #define sqlite3_step                   sqlite3_api->step
       
 75599 #define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
       
 75600 #define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
       
 75601 #define sqlite3_total_changes          sqlite3_api->total_changes
       
 75602 #define sqlite3_trace                  sqlite3_api->trace
       
 75603 #ifndef SQLITE_OMIT_DEPRECATED
       
 75604 #define sqlite3_transfer_bindings      sqlite3_api->transfer_bindings
       
 75605 #endif
       
 75606 #define sqlite3_update_hook            sqlite3_api->update_hook
       
 75607 #define sqlite3_user_data              sqlite3_api->user_data
       
 75608 #define sqlite3_value_blob             sqlite3_api->value_blob
       
 75609 #define sqlite3_value_bytes            sqlite3_api->value_bytes
       
 75610 #define sqlite3_value_bytes16          sqlite3_api->value_bytes16
       
 75611 #define sqlite3_value_double           sqlite3_api->value_double
       
 75612 #define sqlite3_value_int              sqlite3_api->value_int
       
 75613 #define sqlite3_value_int64            sqlite3_api->value_int64
       
 75614 #define sqlite3_value_numeric_type     sqlite3_api->value_numeric_type
       
 75615 #define sqlite3_value_text             sqlite3_api->value_text
       
 75616 #define sqlite3_value_text16           sqlite3_api->value_text16
       
 75617 #define sqlite3_value_text16be         sqlite3_api->value_text16be
       
 75618 #define sqlite3_value_text16le         sqlite3_api->value_text16le
       
 75619 #define sqlite3_value_type             sqlite3_api->value_type
       
 75620 #define sqlite3_vmprintf               sqlite3_api->vmprintf
       
 75621 #define sqlite3_overload_function      sqlite3_api->overload_function
       
 75622 #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
       
 75623 #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
       
 75624 #define sqlite3_clear_bindings         sqlite3_api->clear_bindings
       
 75625 #define sqlite3_bind_zeroblob          sqlite3_api->bind_zeroblob
       
 75626 #define sqlite3_blob_bytes             sqlite3_api->blob_bytes
       
 75627 #define sqlite3_blob_close             sqlite3_api->blob_close
       
 75628 #define sqlite3_blob_open              sqlite3_api->blob_open
       
 75629 #define sqlite3_blob_read              sqlite3_api->blob_read
       
 75630 #define sqlite3_blob_write             sqlite3_api->blob_write
       
 75631 #define sqlite3_create_collation_v2    sqlite3_api->create_collation_v2
       
 75632 #define sqlite3_file_control           sqlite3_api->file_control
       
 75633 #define sqlite3_memory_highwater       sqlite3_api->memory_highwater
       
 75634 #define sqlite3_memory_used            sqlite3_api->memory_used
       
 75635 #define sqlite3_mutex_alloc            sqlite3_api->mutex_alloc
       
 75636 #define sqlite3_mutex_enter            sqlite3_api->mutex_enter
       
 75637 #define sqlite3_mutex_free             sqlite3_api->mutex_free
       
 75638 #define sqlite3_mutex_leave            sqlite3_api->mutex_leave
       
 75639 #define sqlite3_mutex_try              sqlite3_api->mutex_try
       
 75640 #define sqlite3_open_v2                sqlite3_api->open_v2
       
 75641 #define sqlite3_release_memory         sqlite3_api->release_memory
       
 75642 #define sqlite3_result_error_nomem     sqlite3_api->result_error_nomem
       
 75643 #define sqlite3_result_error_toobig    sqlite3_api->result_error_toobig
       
 75644 #define sqlite3_sleep                  sqlite3_api->sleep
       
 75645 #define sqlite3_soft_heap_limit        sqlite3_api->soft_heap_limit
       
 75646 #define sqlite3_vfs_find               sqlite3_api->vfs_find
       
 75647 #define sqlite3_vfs_register           sqlite3_api->vfs_register
       
 75648 #define sqlite3_vfs_unregister         sqlite3_api->vfs_unregister
       
 75649 #define sqlite3_threadsafe             sqlite3_api->xthreadsafe
       
 75650 #define sqlite3_result_zeroblob        sqlite3_api->result_zeroblob
       
 75651 #define sqlite3_result_error_code      sqlite3_api->result_error_code
       
 75652 #define sqlite3_test_control           sqlite3_api->test_control
       
 75653 #define sqlite3_randomness             sqlite3_api->randomness
       
 75654 #define sqlite3_context_db_handle      sqlite3_api->context_db_handle
       
 75655 #define sqlite3_extended_result_codes  sqlite3_api->extended_result_codes
       
 75656 #define sqlite3_limit                  sqlite3_api->limit
       
 75657 #define sqlite3_next_stmt              sqlite3_api->next_stmt
       
 75658 #define sqlite3_sql                    sqlite3_api->sql
       
 75659 #define sqlite3_status                 sqlite3_api->status
       
 75660 #endif /* SQLITE_CORE */
       
 75661 
       
 75662 #define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api = 0;
       
 75663 #define SQLITE_EXTENSION_INIT2(v)  sqlite3_api = v;
       
 75664 
       
 75665 #endif /* _SQLITE3EXT_H_ */
       
 75666 
       
 75667 /************** End of sqlite3ext.h ******************************************/
       
 75668 /************** Continuing where we left off in loadext.c ********************/
       
 75669 
       
 75670 #ifndef SQLITE_OMIT_LOAD_EXTENSION
       
 75671 
       
 75672 /*
       
 75673 ** Some API routines are omitted when various features are
       
 75674 ** excluded from a build of SQLite.  Substitute a NULL pointer
       
 75675 ** for any missing APIs.
       
 75676 */
       
 75677 #ifndef SQLITE_ENABLE_COLUMN_METADATA
       
 75678 # define sqlite3_column_database_name   0
       
 75679 # define sqlite3_column_database_name16 0
       
 75680 # define sqlite3_column_table_name      0
       
 75681 # define sqlite3_column_table_name16    0
       
 75682 # define sqlite3_column_origin_name     0
       
 75683 # define sqlite3_column_origin_name16   0
       
 75684 # define sqlite3_table_column_metadata  0
       
 75685 #endif
       
 75686 
       
 75687 #ifdef SQLITE_OMIT_AUTHORIZATION
       
 75688 # define sqlite3_set_authorizer         0
       
 75689 #endif
       
 75690 
       
 75691 #ifdef SQLITE_OMIT_UTF16
       
 75692 # define sqlite3_bind_text16            0
       
 75693 # define sqlite3_collation_needed16     0
       
 75694 # define sqlite3_column_decltype16      0
       
 75695 # define sqlite3_column_name16          0
       
 75696 # define sqlite3_column_text16          0
       
 75697 # define sqlite3_complete16             0
       
 75698 # define sqlite3_create_collation16     0
       
 75699 # define sqlite3_create_function16      0
       
 75700 # define sqlite3_errmsg16               0
       
 75701 # define sqlite3_open16                 0
       
 75702 # define sqlite3_prepare16              0
       
 75703 # define sqlite3_prepare16_v2           0
       
 75704 # define sqlite3_result_error16         0
       
 75705 # define sqlite3_result_text16          0
       
 75706 # define sqlite3_result_text16be        0
       
 75707 # define sqlite3_result_text16le        0
       
 75708 # define sqlite3_value_text16           0
       
 75709 # define sqlite3_value_text16be         0
       
 75710 # define sqlite3_value_text16le         0
       
 75711 # define sqlite3_column_database_name16 0
       
 75712 # define sqlite3_column_table_name16    0
       
 75713 # define sqlite3_column_origin_name16   0
       
 75714 #endif
       
 75715 
       
 75716 #ifdef SQLITE_OMIT_COMPLETE
       
 75717 # define sqlite3_complete 0
       
 75718 # define sqlite3_complete16 0
       
 75719 #endif
       
 75720 
       
 75721 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
       
 75722 # define sqlite3_progress_handler 0
       
 75723 #endif
       
 75724 
       
 75725 #ifdef SQLITE_OMIT_VIRTUALTABLE
       
 75726 # define sqlite3_create_module 0
       
 75727 # define sqlite3_create_module_v2 0
       
 75728 # define sqlite3_declare_vtab 0
       
 75729 #endif
       
 75730 
       
 75731 #ifdef SQLITE_OMIT_SHARED_CACHE
       
 75732 # define sqlite3_enable_shared_cache 0
       
 75733 #endif
       
 75734 
       
 75735 #ifdef SQLITE_OMIT_TRACE
       
 75736 # define sqlite3_profile       0
       
 75737 # define sqlite3_trace         0
       
 75738 #endif
       
 75739 
       
 75740 #ifdef SQLITE_OMIT_GET_TABLE
       
 75741 # define sqlite3_free_table    0
       
 75742 # define sqlite3_get_table     0
       
 75743 #endif
       
 75744 
       
 75745 #ifdef SQLITE_OMIT_INCRBLOB
       
 75746 #define sqlite3_bind_zeroblob  0
       
 75747 #define sqlite3_blob_bytes     0
       
 75748 #define sqlite3_blob_close     0
       
 75749 #define sqlite3_blob_open      0
       
 75750 #define sqlite3_blob_read      0
       
 75751 #define sqlite3_blob_write     0
       
 75752 #endif
       
 75753 
       
 75754 /*
       
 75755 ** The following structure contains pointers to all SQLite API routines.
       
 75756 ** A pointer to this structure is passed into extensions when they are
       
 75757 ** loaded so that the extension can make calls back into the SQLite
       
 75758 ** library.
       
 75759 **
       
 75760 ** When adding new APIs, add them to the bottom of this structure
       
 75761 ** in order to preserve backwards compatibility.
       
 75762 **
       
 75763 ** Extensions that use newer APIs should first call the
       
 75764 ** sqlite3_libversion_number() to make sure that the API they
       
 75765 ** intend to use is supported by the library.  Extensions should
       
 75766 ** also check to make sure that the pointer to the function is
       
 75767 ** not NULL before calling it.
       
 75768 */
       
 75769 static const sqlite3_api_routines sqlite3Apis = {
       
 75770   sqlite3_aggregate_context,
       
 75771 #ifndef SQLITE_OMIT_DEPRECATED
       
 75772   sqlite3_aggregate_count,
       
 75773 #else
       
 75774   0,
       
 75775 #endif
       
 75776   sqlite3_bind_blob,
       
 75777   sqlite3_bind_double,
       
 75778   sqlite3_bind_int,
       
 75779   sqlite3_bind_int64,
       
 75780   sqlite3_bind_null,
       
 75781   sqlite3_bind_parameter_count,
       
 75782   sqlite3_bind_parameter_index,
       
 75783   sqlite3_bind_parameter_name,
       
 75784   sqlite3_bind_text,
       
 75785   sqlite3_bind_text16,
       
 75786   sqlite3_bind_value,
       
 75787   sqlite3_busy_handler,
       
 75788   sqlite3_busy_timeout,
       
 75789   sqlite3_changes,
       
 75790   sqlite3_close,
       
 75791   sqlite3_collation_needed,
       
 75792   sqlite3_collation_needed16,
       
 75793   sqlite3_column_blob,
       
 75794   sqlite3_column_bytes,
       
 75795   sqlite3_column_bytes16,
       
 75796   sqlite3_column_count,
       
 75797   sqlite3_column_database_name,
       
 75798   sqlite3_column_database_name16,
       
 75799   sqlite3_column_decltype,
       
 75800   sqlite3_column_decltype16,
       
 75801   sqlite3_column_double,
       
 75802   sqlite3_column_int,
       
 75803   sqlite3_column_int64,
       
 75804   sqlite3_column_name,
       
 75805   sqlite3_column_name16,
       
 75806   sqlite3_column_origin_name,
       
 75807   sqlite3_column_origin_name16,
       
 75808   sqlite3_column_table_name,
       
 75809   sqlite3_column_table_name16,
       
 75810   sqlite3_column_text,
       
 75811   sqlite3_column_text16,
       
 75812   sqlite3_column_type,
       
 75813   sqlite3_column_value,
       
 75814   sqlite3_commit_hook,
       
 75815   sqlite3_complete,
       
 75816   sqlite3_complete16,
       
 75817   sqlite3_create_collation,
       
 75818   sqlite3_create_collation16,
       
 75819   sqlite3_create_function,
       
 75820   sqlite3_create_function16,
       
 75821   sqlite3_create_module,
       
 75822   sqlite3_data_count,
       
 75823   sqlite3_db_handle,
       
 75824   sqlite3_declare_vtab,
       
 75825   sqlite3_enable_shared_cache,
       
 75826   sqlite3_errcode,
       
 75827   sqlite3_errmsg,
       
 75828   sqlite3_errmsg16,
       
 75829   sqlite3_exec,
       
 75830 #ifndef SQLITE_OMIT_DEPRECATED
       
 75831   sqlite3_expired,
       
 75832 #else
       
 75833   0,
       
 75834 #endif
       
 75835   sqlite3_finalize,
       
 75836   sqlite3_free,
       
 75837   sqlite3_free_table,
       
 75838   sqlite3_get_autocommit,
       
 75839   sqlite3_get_auxdata,
       
 75840   sqlite3_get_table,
       
 75841   0,     /* Was sqlite3_global_recover(), but that function is deprecated */
       
 75842   sqlite3_interrupt,
       
 75843   sqlite3_last_insert_rowid,
       
 75844   sqlite3_libversion,
       
 75845   sqlite3_libversion_number,
       
 75846   sqlite3_malloc,
       
 75847   sqlite3_mprintf,
       
 75848   sqlite3_open,
       
 75849   sqlite3_open16,
       
 75850   sqlite3_prepare,
       
 75851   sqlite3_prepare16,
       
 75852   sqlite3_profile,
       
 75853   sqlite3_progress_handler,
       
 75854   sqlite3_realloc,
       
 75855   sqlite3_reset,
       
 75856   sqlite3_result_blob,
       
 75857   sqlite3_result_double,
       
 75858   sqlite3_result_error,
       
 75859   sqlite3_result_error16,
       
 75860   sqlite3_result_int,
       
 75861   sqlite3_result_int64,
       
 75862   sqlite3_result_null,
       
 75863   sqlite3_result_text,
       
 75864   sqlite3_result_text16,
       
 75865   sqlite3_result_text16be,
       
 75866   sqlite3_result_text16le,
       
 75867   sqlite3_result_value,
       
 75868   sqlite3_rollback_hook,
       
 75869   sqlite3_set_authorizer,
       
 75870   sqlite3_set_auxdata,
       
 75871   sqlite3_snprintf,
       
 75872   sqlite3_step,
       
 75873   sqlite3_table_column_metadata,
       
 75874 #ifndef SQLITE_OMIT_DEPRECATED
       
 75875   sqlite3_thread_cleanup,
       
 75876 #else
       
 75877   0,
       
 75878 #endif
       
 75879   sqlite3_total_changes,
       
 75880   sqlite3_trace,
       
 75881 #ifndef SQLITE_OMIT_DEPRECATED
       
 75882   sqlite3_transfer_bindings,
       
 75883 #else
       
 75884   0,
       
 75885 #endif
       
 75886   sqlite3_update_hook,
       
 75887   sqlite3_user_data,
       
 75888   sqlite3_value_blob,
       
 75889   sqlite3_value_bytes,
       
 75890   sqlite3_value_bytes16,
       
 75891   sqlite3_value_double,
       
 75892   sqlite3_value_int,
       
 75893   sqlite3_value_int64,
       
 75894   sqlite3_value_numeric_type,
       
 75895   sqlite3_value_text,
       
 75896   sqlite3_value_text16,
       
 75897   sqlite3_value_text16be,
       
 75898   sqlite3_value_text16le,
       
 75899   sqlite3_value_type,
       
 75900   sqlite3_vmprintf,
       
 75901   /*
       
 75902   ** The original API set ends here.  All extensions can call any
       
 75903   ** of the APIs above provided that the pointer is not NULL.  But
       
 75904   ** before calling APIs that follow, extension should check the
       
 75905   ** sqlite3_libversion_number() to make sure they are dealing with
       
 75906   ** a library that is new enough to support that API.
       
 75907   *************************************************************************
       
 75908   */
       
 75909   sqlite3_overload_function,
       
 75910 
       
 75911   /*
       
 75912   ** Added after 3.3.13
       
 75913   */
       
 75914   sqlite3_prepare_v2,
       
 75915   sqlite3_prepare16_v2,
       
 75916   sqlite3_clear_bindings,
       
 75917 
       
 75918   /*
       
 75919   ** Added for 3.4.1
       
 75920   */
       
 75921   sqlite3_create_module_v2,
       
 75922 
       
 75923   /*
       
 75924   ** Added for 3.5.0
       
 75925   */
       
 75926   sqlite3_bind_zeroblob,
       
 75927   sqlite3_blob_bytes,
       
 75928   sqlite3_blob_close,
       
 75929   sqlite3_blob_open,
       
 75930   sqlite3_blob_read,
       
 75931   sqlite3_blob_write,
       
 75932   sqlite3_create_collation_v2,
       
 75933   sqlite3_file_control,
       
 75934   sqlite3_memory_highwater,
       
 75935   sqlite3_memory_used,
       
 75936 #ifdef SQLITE_MUTEX_OMIT
       
 75937   0, 
       
 75938   0, 
       
 75939   0,
       
 75940   0,
       
 75941   0,
       
 75942 #else
       
 75943   sqlite3_mutex_alloc,
       
 75944   sqlite3_mutex_enter,
       
 75945   sqlite3_mutex_free,
       
 75946   sqlite3_mutex_leave,
       
 75947   sqlite3_mutex_try,
       
 75948 #endif
       
 75949   sqlite3_open_v2,
       
 75950   sqlite3_release_memory,
       
 75951   sqlite3_result_error_nomem,
       
 75952   sqlite3_result_error_toobig,
       
 75953   sqlite3_sleep,
       
 75954   sqlite3_soft_heap_limit,
       
 75955   sqlite3_vfs_find,
       
 75956   sqlite3_vfs_register,
       
 75957   sqlite3_vfs_unregister,
       
 75958 
       
 75959   /*
       
 75960   ** Added for 3.5.8
       
 75961   */
       
 75962   sqlite3_threadsafe,
       
 75963   sqlite3_result_zeroblob,
       
 75964   sqlite3_result_error_code,
       
 75965   sqlite3_test_control,
       
 75966   sqlite3_randomness,
       
 75967   sqlite3_context_db_handle,
       
 75968 
       
 75969   /*
       
 75970   ** Added for 3.6.0
       
 75971   */
       
 75972   sqlite3_extended_result_codes,
       
 75973   sqlite3_limit,
       
 75974   sqlite3_next_stmt,
       
 75975   sqlite3_sql,
       
 75976   sqlite3_status,
       
 75977 };
       
 75978 
       
 75979 /*
       
 75980 ** Attempt to load an SQLite extension library contained in the file
       
 75981 ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
       
 75982 ** default entry point name (sqlite3_extension_init) is used.  Use
       
 75983 ** of the default name is recommended.
       
 75984 **
       
 75985 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
       
 75986 **
       
 75987 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with 
       
 75988 ** error message text.  The calling function should free this memory
       
 75989 ** by calling sqlite3DbFree(db, ).
       
 75990 */
       
 75991 static int sqlite3LoadExtension(
       
 75992   sqlite3 *db,          /* Load the extension into this database connection */
       
 75993   const char *zFile,    /* Name of the shared library containing extension */
       
 75994   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
       
 75995   char **pzErrMsg       /* Put error message here if not 0 */
       
 75996 ){
       
 75997   sqlite3_vfs *pVfs = db->pVfs;
       
 75998   void *handle;
       
 75999   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
       
 76000   char *zErrmsg = 0;
       
 76001   void **aHandle;
       
 76002   const int nMsg = 300;
       
 76003 
       
 76004   if( pzErrMsg ) *pzErrMsg = 0;
       
 76005 
       
 76006   /* Ticket #1863.  To avoid a creating security problems for older
       
 76007   ** applications that relink against newer versions of SQLite, the
       
 76008   ** ability to run load_extension is turned off by default.  One
       
 76009   ** must call sqlite3_enable_load_extension() to turn on extension
       
 76010   ** loading.  Otherwise you get the following error.
       
 76011   */
       
 76012   if( (db->flags & SQLITE_LoadExtension)==0 ){
       
 76013     if( pzErrMsg ){
       
 76014       *pzErrMsg = sqlite3_mprintf("not authorized");
       
 76015     }
       
 76016     return SQLITE_ERROR;
       
 76017   }
       
 76018 
       
 76019   if( zProc==0 ){
       
 76020     zProc = "sqlite3_extension_init";
       
 76021   }
       
 76022 
       
 76023   handle = sqlite3OsDlOpen(pVfs, zFile);
       
 76024   if( handle==0 ){
       
 76025     if( pzErrMsg ){
       
 76026       zErrmsg = sqlite3StackAllocZero(db, nMsg);
       
 76027       if( zErrmsg ){
       
 76028         sqlite3_snprintf(nMsg, zErrmsg, 
       
 76029             "unable to open shared library [%s]", zFile);
       
 76030         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
       
 76031         *pzErrMsg = sqlite3DbStrDup(0, zErrmsg);
       
 76032         sqlite3StackFree(db, zErrmsg);
       
 76033       }
       
 76034     }
       
 76035     return SQLITE_ERROR;
       
 76036   }
       
 76037   xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
       
 76038                    sqlite3OsDlSym(pVfs, handle, zProc);
       
 76039   if( xInit==0 ){
       
 76040     if( pzErrMsg ){
       
 76041       zErrmsg = sqlite3StackAllocZero(db, nMsg);
       
 76042       if( zErrmsg ){
       
 76043         sqlite3_snprintf(nMsg, zErrmsg,
       
 76044             "no entry point [%s] in shared library [%s]", zProc,zFile);
       
 76045         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
       
 76046         *pzErrMsg = sqlite3DbStrDup(0, zErrmsg);
       
 76047         sqlite3StackFree(db, zErrmsg);
       
 76048       }
       
 76049       sqlite3OsDlClose(pVfs, handle);
       
 76050     }
       
 76051     return SQLITE_ERROR;
       
 76052   }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){
       
 76053     if( pzErrMsg ){
       
 76054       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
       
 76055     }
       
 76056     sqlite3_free(zErrmsg);
       
 76057     sqlite3OsDlClose(pVfs, handle);
       
 76058     return SQLITE_ERROR;
       
 76059   }
       
 76060 
       
 76061   /* Append the new shared library handle to the db->aExtension array. */
       
 76062   aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
       
 76063   if( aHandle==0 ){
       
 76064     return SQLITE_NOMEM;
       
 76065   }
       
 76066   if( db->nExtension>0 ){
       
 76067     memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
       
 76068   }
       
 76069   sqlite3DbFree(db, db->aExtension);
       
 76070   db->aExtension = aHandle;
       
 76071 
       
 76072   db->aExtension[db->nExtension++] = handle;
       
 76073   return SQLITE_OK;
       
 76074 }
       
 76075 SQLITE_API int sqlite3_load_extension(
       
 76076   sqlite3 *db,          /* Load the extension into this database connection */
       
 76077   const char *zFile,    /* Name of the shared library containing extension */
       
 76078   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
       
 76079   char **pzErrMsg       /* Put error message here if not 0 */
       
 76080 ){
       
 76081   int rc;
       
 76082   sqlite3_mutex_enter(db->mutex);
       
 76083   rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
       
 76084   rc = sqlite3ApiExit(db, rc);
       
 76085   sqlite3_mutex_leave(db->mutex);
       
 76086   return rc;
       
 76087 }
       
 76088 
       
 76089 /*
       
 76090 ** Call this routine when the database connection is closing in order
       
 76091 ** to clean up loaded extensions
       
 76092 */
       
 76093 SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
       
 76094   int i;
       
 76095   assert( sqlite3_mutex_held(db->mutex) );
       
 76096   for(i=0; i<db->nExtension; i++){
       
 76097     sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
       
 76098   }
       
 76099   sqlite3DbFree(db, db->aExtension);
       
 76100 }
       
 76101 
       
 76102 /*
       
 76103 ** Enable or disable extension loading.  Extension loading is disabled by
       
 76104 ** default so as not to open security holes in older applications.
       
 76105 */
       
 76106 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
       
 76107   sqlite3_mutex_enter(db->mutex);
       
 76108   if( onoff ){
       
 76109     db->flags |= SQLITE_LoadExtension;
       
 76110   }else{
       
 76111     db->flags &= ~SQLITE_LoadExtension;
       
 76112   }
       
 76113   sqlite3_mutex_leave(db->mutex);
       
 76114   return SQLITE_OK;
       
 76115 }
       
 76116 
       
 76117 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
       
 76118 
       
 76119 /*
       
 76120 ** The auto-extension code added regardless of whether or not extension
       
 76121 ** loading is supported.  We need a dummy sqlite3Apis pointer for that
       
 76122 ** code if regular extension loading is not available.  This is that
       
 76123 ** dummy pointer.
       
 76124 */
       
 76125 #ifdef SQLITE_OMIT_LOAD_EXTENSION
       
 76126 static const sqlite3_api_routines sqlite3Apis = { 0 };
       
 76127 #endif
       
 76128 
       
 76129 
       
 76130 /*
       
 76131 ** The following object holds the list of automatically loaded
       
 76132 ** extensions.
       
 76133 **
       
 76134 ** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
       
 76135 ** mutex must be held while accessing this list.
       
 76136 */
       
 76137 typedef struct sqlite3AutoExtList sqlite3AutoExtList;
       
 76138 static SQLITE_WSD struct sqlite3AutoExtList {
       
 76139   int nExt;              /* Number of entries in aExt[] */          
       
 76140   void (**aExt)(void);   /* Pointers to the extension init functions */
       
 76141 } sqlite3Autoext = { 0, 0 };
       
 76142 
       
 76143 /* The "wsdAutoext" macro will resolve to the autoextension
       
 76144 ** state vector.  If writable static data is unsupported on the target,
       
 76145 ** we have to locate the state vector at run-time.  In the more common
       
 76146 ** case where writable static data is supported, wsdStat can refer directly
       
 76147 ** to the "sqlite3Autoext" state vector declared above.
       
 76148 */
       
 76149 #ifdef SQLITE_OMIT_WSD
       
 76150 # define wsdAutoextInit \
       
 76151   sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
       
 76152 # define wsdAutoext x[0]
       
 76153 #else
       
 76154 # define wsdAutoextInit
       
 76155 # define wsdAutoext sqlite3Autoext
       
 76156 #endif
       
 76157 
       
 76158 
       
 76159 /*
       
 76160 ** Register a statically linked extension that is automatically
       
 76161 ** loaded by every new database connection.
       
 76162 */
       
 76163 SQLITE_API int sqlite3_auto_extension(void (*xInit)(void)){
       
 76164   int rc = SQLITE_OK;
       
 76165 #ifndef SQLITE_OMIT_AUTOINIT
       
 76166   rc = sqlite3_initialize();
       
 76167   if( rc ){
       
 76168     return rc;
       
 76169   }else
       
 76170 #endif
       
 76171   {
       
 76172     int i;
       
 76173 #if SQLITE_THREADSAFE
       
 76174     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
       
 76175 #endif
       
 76176     wsdAutoextInit;
       
 76177     sqlite3_mutex_enter(mutex);
       
 76178     for(i=0; i<wsdAutoext.nExt; i++){
       
 76179       if( wsdAutoext.aExt[i]==xInit ) break;
       
 76180     }
       
 76181     if( i==wsdAutoext.nExt ){
       
 76182       int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
       
 76183       void (**aNew)(void);
       
 76184       aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
       
 76185       if( aNew==0 ){
       
 76186         rc = SQLITE_NOMEM;
       
 76187       }else{
       
 76188         wsdAutoext.aExt = aNew;
       
 76189         wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
       
 76190         wsdAutoext.nExt++;
       
 76191       }
       
 76192     }
       
 76193     sqlite3_mutex_leave(mutex);
       
 76194     assert( (rc&0xff)==rc );
       
 76195     return rc;
       
 76196   }
       
 76197 }
       
 76198 
       
 76199 /*
       
 76200 ** Reset the automatic extension loading mechanism.
       
 76201 */
       
 76202 SQLITE_API void sqlite3_reset_auto_extension(void){
       
 76203 #ifndef SQLITE_OMIT_AUTOINIT
       
 76204   if( sqlite3_initialize()==SQLITE_OK )
       
 76205 #endif
       
 76206   {
       
 76207 #if SQLITE_THREADSAFE
       
 76208     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
       
 76209 #endif
       
 76210     wsdAutoextInit;
       
 76211     sqlite3_mutex_enter(mutex);
       
 76212     sqlite3_free(wsdAutoext.aExt);
       
 76213     wsdAutoext.aExt = 0;
       
 76214     wsdAutoext.nExt = 0;
       
 76215     sqlite3_mutex_leave(mutex);
       
 76216   }
       
 76217 }
       
 76218 
       
 76219 /*
       
 76220 ** Load all automatic extensions.
       
 76221 **
       
 76222 ** If anything goes wrong, set an error in the database connection.
       
 76223 */
       
 76224 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
       
 76225   int i;
       
 76226   int go = 1;
       
 76227   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
       
 76228 
       
 76229   wsdAutoextInit;
       
 76230   if( wsdAutoext.nExt==0 ){
       
 76231     /* Common case: early out without every having to acquire a mutex */
       
 76232     return;
       
 76233   }
       
 76234   for(i=0; go; i++){
       
 76235     char *zErrmsg;
       
 76236 #if SQLITE_THREADSAFE
       
 76237     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
       
 76238 #endif
       
 76239     sqlite3_mutex_enter(mutex);
       
 76240     if( i>=wsdAutoext.nExt ){
       
 76241       xInit = 0;
       
 76242       go = 0;
       
 76243     }else{
       
 76244       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
       
 76245               wsdAutoext.aExt[i];
       
 76246     }
       
 76247     sqlite3_mutex_leave(mutex);
       
 76248     zErrmsg = 0;
       
 76249     if( xInit && xInit(db, &zErrmsg, &sqlite3Apis) ){
       
 76250       sqlite3Error(db, SQLITE_ERROR,
       
 76251             "automatic extension loading failed: %s", zErrmsg);
       
 76252       go = 0;
       
 76253     }
       
 76254     sqlite3_free(zErrmsg);
       
 76255   }
       
 76256 }
       
 76257 
       
 76258 /************** End of loadext.c *********************************************/
       
 76259 /************** Begin file pragma.c ******************************************/
       
 76260 /*
       
 76261 ** 2003 April 6
       
 76262 **
       
 76263 ** The author disclaims copyright to this source code.  In place of
       
 76264 ** a legal notice, here is a blessing:
       
 76265 **
       
 76266 **    May you do good and not evil.
       
 76267 **    May you find forgiveness for yourself and forgive others.
       
 76268 **    May you share freely, never taking more than you give.
       
 76269 **
       
 76270 *************************************************************************
       
 76271 ** This file contains code used to implement the PRAGMA command.
       
 76272 **
       
 76273 ** $Id: pragma.c,v 1.214 2009/07/02 07:47:33 danielk1977 Exp $
       
 76274 */
       
 76275 
       
 76276 /* Ignore this whole file if pragmas are disabled
       
 76277 */
       
 76278 #if !defined(SQLITE_OMIT_PRAGMA)
       
 76279 
       
 76280 /*
       
 76281 ** Interpret the given string as a safety level.  Return 0 for OFF,
       
 76282 ** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or 
       
 76283 ** unrecognized string argument.
       
 76284 **
       
 76285 ** Note that the values returned are one less that the values that
       
 76286 ** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
       
 76287 ** to support legacy SQL code.  The safety level used to be boolean
       
 76288 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
       
 76289 */
       
 76290 static u8 getSafetyLevel(const char *z){
       
 76291                              /* 123456789 123456789 */
       
 76292   static const char zText[] = "onoffalseyestruefull";
       
 76293   static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
       
 76294   static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
       
 76295   static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
       
 76296   int i, n;
       
 76297   if( sqlite3Isdigit(*z) ){
       
 76298     return (u8)atoi(z);
       
 76299   }
       
 76300   n = sqlite3Strlen30(z);
       
 76301   for(i=0; i<ArraySize(iLength); i++){
       
 76302     if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
       
 76303       return iValue[i];
       
 76304     }
       
 76305   }
       
 76306   return 1;
       
 76307 }
       
 76308 
       
 76309 /*
       
 76310 ** Interpret the given string as a boolean value.
       
 76311 */
       
 76312 static u8 getBoolean(const char *z){
       
 76313   return getSafetyLevel(z)&1;
       
 76314 }
       
 76315 
       
 76316 /*
       
 76317 ** Interpret the given string as a locking mode value.
       
 76318 */
       
 76319 static int getLockingMode(const char *z){
       
 76320   if( z ){
       
 76321     if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
       
 76322     if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
       
 76323   }
       
 76324   return PAGER_LOCKINGMODE_QUERY;
       
 76325 }
       
 76326 
       
 76327 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 76328 /*
       
 76329 ** Interpret the given string as an auto-vacuum mode value.
       
 76330 **
       
 76331 ** The following strings, "none", "full" and "incremental" are 
       
 76332 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
       
 76333 */
       
 76334 static int getAutoVacuum(const char *z){
       
 76335   int i;
       
 76336   if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
       
 76337   if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
       
 76338   if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
       
 76339   i = atoi(z);
       
 76340   return (u8)((i>=0&&i<=2)?i:0);
       
 76341 }
       
 76342 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
       
 76343 
       
 76344 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
       
 76345 /*
       
 76346 ** Interpret the given string as a temp db location. Return 1 for file
       
 76347 ** backed temporary databases, 2 for the Red-Black tree in memory database
       
 76348 ** and 0 to use the compile-time default.
       
 76349 */
       
 76350 static int getTempStore(const char *z){
       
 76351   if( z[0]>='0' && z[0]<='2' ){
       
 76352     return z[0] - '0';
       
 76353   }else if( sqlite3StrICmp(z, "file")==0 ){
       
 76354     return 1;
       
 76355   }else if( sqlite3StrICmp(z, "memory")==0 ){
       
 76356     return 2;
       
 76357   }else{
       
 76358     return 0;
       
 76359   }
       
 76360 }
       
 76361 #endif /* SQLITE_PAGER_PRAGMAS */
       
 76362 
       
 76363 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
       
 76364 /*
       
 76365 ** Invalidate temp storage, either when the temp storage is changed
       
 76366 ** from default, or when 'file' and the temp_store_directory has changed
       
 76367 */
       
 76368 static int invalidateTempStorage(Parse *pParse){
       
 76369   sqlite3 *db = pParse->db;
       
 76370   if( db->aDb[1].pBt!=0 ){
       
 76371     if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
       
 76372       sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
       
 76373         "from within a transaction");
       
 76374       return SQLITE_ERROR;
       
 76375     }
       
 76376     sqlite3BtreeClose(db->aDb[1].pBt);
       
 76377     db->aDb[1].pBt = 0;
       
 76378     sqlite3ResetInternalSchema(db, 0);
       
 76379   }
       
 76380   return SQLITE_OK;
       
 76381 }
       
 76382 #endif /* SQLITE_PAGER_PRAGMAS */
       
 76383 
       
 76384 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
       
 76385 /*
       
 76386 ** If the TEMP database is open, close it and mark the database schema
       
 76387 ** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
       
 76388 ** or DEFAULT_TEMP_STORE pragmas.
       
 76389 */
       
 76390 static int changeTempStorage(Parse *pParse, const char *zStorageType){
       
 76391   int ts = getTempStore(zStorageType);
       
 76392   sqlite3 *db = pParse->db;
       
 76393   if( db->temp_store==ts ) return SQLITE_OK;
       
 76394   if( invalidateTempStorage( pParse ) != SQLITE_OK ){
       
 76395     return SQLITE_ERROR;
       
 76396   }
       
 76397   db->temp_store = (u8)ts;
       
 76398   return SQLITE_OK;
       
 76399 }
       
 76400 #endif /* SQLITE_PAGER_PRAGMAS */
       
 76401 
       
 76402 /*
       
 76403 ** Generate code to return a single integer value.
       
 76404 */
       
 76405 static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
       
 76406   Vdbe *v = sqlite3GetVdbe(pParse);
       
 76407   int mem = ++pParse->nMem;
       
 76408   i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
       
 76409   if( pI64 ){
       
 76410     memcpy(pI64, &value, sizeof(value));
       
 76411   }
       
 76412   sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
       
 76413   sqlite3VdbeSetNumCols(v, 1);
       
 76414   sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
       
 76415   sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
       
 76416 }
       
 76417 
       
 76418 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
       
 76419 /*
       
 76420 ** Check to see if zRight and zLeft refer to a pragma that queries
       
 76421 ** or changes one of the flags in db->flags.  Return 1 if so and 0 if not.
       
 76422 ** Also, implement the pragma.
       
 76423 */
       
 76424 static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
       
 76425   static const struct sPragmaType {
       
 76426     const char *zName;  /* Name of the pragma */
       
 76427     int mask;           /* Mask for the db->flags value */
       
 76428   } aPragma[] = {
       
 76429     { "full_column_names",        SQLITE_FullColNames  },
       
 76430     { "short_column_names",       SQLITE_ShortColNames },
       
 76431     { "count_changes",            SQLITE_CountRows     },
       
 76432     { "empty_result_callbacks",   SQLITE_NullCallback  },
       
 76433     { "legacy_file_format",       SQLITE_LegacyFileFmt },
       
 76434     { "fullfsync",                SQLITE_FullFSync     },
       
 76435     { "reverse_unordered_selects", SQLITE_ReverseOrder  },
       
 76436 #ifdef SQLITE_DEBUG
       
 76437     { "sql_trace",                SQLITE_SqlTrace      },
       
 76438     { "vdbe_listing",             SQLITE_VdbeListing   },
       
 76439     { "vdbe_trace",               SQLITE_VdbeTrace     },
       
 76440 #endif
       
 76441 #ifndef SQLITE_OMIT_CHECK
       
 76442     { "ignore_check_constraints", SQLITE_IgnoreChecks  },
       
 76443 #endif
       
 76444     /* The following is VERY experimental */
       
 76445     { "writable_schema",          SQLITE_WriteSchema|SQLITE_RecoveryMode },
       
 76446     { "omit_readlock",            SQLITE_NoReadlock    },
       
 76447 
       
 76448     /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
       
 76449     ** flag if there are any active statements. */
       
 76450     { "read_uncommitted",         SQLITE_ReadUncommitted },
       
 76451     { "recursive_triggers",       SQLITE_RecTriggers },
       
 76452 
       
 76453     /* This flag may only be set if both foreign-key and trigger support
       
 76454     ** are present in the build.  */
       
 76455 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
       
 76456     { "foreign_keys",             SQLITE_ForeignKeys },
       
 76457 #endif
       
 76458   };
       
 76459   int i;
       
 76460   const struct sPragmaType *p;
       
 76461   for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
       
 76462     if( sqlite3StrICmp(zLeft, p->zName)==0 ){
       
 76463       sqlite3 *db = pParse->db;
       
 76464       Vdbe *v;
       
 76465       v = sqlite3GetVdbe(pParse);
       
 76466       assert( v!=0 );  /* Already allocated by sqlite3Pragma() */
       
 76467       if( ALWAYS(v) ){
       
 76468         if( zRight==0 ){
       
 76469           returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
       
 76470         }else{
       
 76471           int mask = p->mask;          /* Mask of bits to set or clear. */
       
 76472           if( db->autoCommit==0 ){
       
 76473             /* Foreign key support may not be enabled or disabled while not
       
 76474             ** in auto-commit mode.  */
       
 76475             mask &= ~(SQLITE_ForeignKeys);
       
 76476           }
       
 76477 
       
 76478           if( getBoolean(zRight) ){
       
 76479             db->flags |= mask;
       
 76480           }else{
       
 76481             db->flags &= ~mask;
       
 76482           }
       
 76483 
       
 76484           /* Many of the flag-pragmas modify the code generated by the SQL 
       
 76485           ** compiler (eg. count_changes). So add an opcode to expire all
       
 76486           ** compiled SQL statements after modifying a pragma value.
       
 76487           */
       
 76488           sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
       
 76489         }
       
 76490       }
       
 76491 
       
 76492       return 1;
       
 76493     }
       
 76494   }
       
 76495   return 0;
       
 76496 }
       
 76497 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
       
 76498 
       
 76499 /*
       
 76500 ** Return a human-readable name for a constraint resolution action.
       
 76501 */
       
 76502 #ifndef SQLITE_OMIT_FOREIGN_KEY
       
 76503 static const char *actionName(u8 action){
       
 76504   const char *zName;
       
 76505   switch( action ){
       
 76506     case OE_SetNull:  zName = "SET NULL";        break;
       
 76507     case OE_SetDflt:  zName = "SET DEFAULT";     break;
       
 76508     case OE_Cascade:  zName = "CASCADE";         break;
       
 76509     case OE_Restrict: zName = "RESTRICT";        break;
       
 76510     default:          zName = "NO ACTION";  
       
 76511                       assert( action==OE_None ); break;
       
 76512   }
       
 76513   return zName;
       
 76514 }
       
 76515 #endif
       
 76516 
       
 76517 /*
       
 76518 ** Process a pragma statement.  
       
 76519 **
       
 76520 ** Pragmas are of this form:
       
 76521 **
       
 76522 **      PRAGMA [database.]id [= value]
       
 76523 **
       
 76524 ** The identifier might also be a string.  The value is a string, and
       
 76525 ** identifier, or a number.  If minusFlag is true, then the value is
       
 76526 ** a number that was preceded by a minus sign.
       
 76527 **
       
 76528 ** If the left side is "database.id" then pId1 is the database name
       
 76529 ** and pId2 is the id.  If the left side is just "id" then pId1 is the
       
 76530 ** id and pId2 is any empty string.
       
 76531 */
       
 76532 SQLITE_PRIVATE void sqlite3Pragma(
       
 76533   Parse *pParse, 
       
 76534   Token *pId1,        /* First part of [database.]id field */
       
 76535   Token *pId2,        /* Second part of [database.]id field, or NULL */
       
 76536   Token *pValue,      /* Token for <value>, or NULL */
       
 76537   int minusFlag       /* True if a '-' sign preceded <value> */
       
 76538 ){
       
 76539   char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
       
 76540   char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
       
 76541   const char *zDb = 0;   /* The database name */
       
 76542   Token *pId;            /* Pointer to <id> token */
       
 76543   int iDb;               /* Database index for <database> */
       
 76544   sqlite3 *db = pParse->db;
       
 76545   Db *pDb;
       
 76546   Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
       
 76547   if( v==0 ) return;
       
 76548   pParse->nMem = 2;
       
 76549 
       
 76550   /* Interpret the [database.] part of the pragma statement. iDb is the
       
 76551   ** index of the database this pragma is being applied to in db.aDb[]. */
       
 76552   iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
       
 76553   if( iDb<0 ) return;
       
 76554   pDb = &db->aDb[iDb];
       
 76555 
       
 76556   /* If the temp database has been explicitly named as part of the 
       
 76557   ** pragma, make sure it is open. 
       
 76558   */
       
 76559   if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
       
 76560     return;
       
 76561   }
       
 76562 
       
 76563   zLeft = sqlite3NameFromToken(db, pId);
       
 76564   if( !zLeft ) return;
       
 76565   if( minusFlag ){
       
 76566     zRight = sqlite3MPrintf(db, "-%T", pValue);
       
 76567   }else{
       
 76568     zRight = sqlite3NameFromToken(db, pValue);
       
 76569   }
       
 76570 
       
 76571   assert( pId2 );
       
 76572   zDb = pId2->n>0 ? pDb->zName : 0;
       
 76573   if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
       
 76574     goto pragma_out;
       
 76575   }
       
 76576  
       
 76577 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
       
 76578   /*
       
 76579   **  PRAGMA [database.]default_cache_size
       
 76580   **  PRAGMA [database.]default_cache_size=N
       
 76581   **
       
 76582   ** The first form reports the current persistent setting for the
       
 76583   ** page cache size.  The value returned is the maximum number of
       
 76584   ** pages in the page cache.  The second form sets both the current
       
 76585   ** page cache size value and the persistent page cache size value
       
 76586   ** stored in the database file.
       
 76587   **
       
 76588   ** The default cache size is stored in meta-value 2 of page 1 of the
       
 76589   ** database file.  The cache size is actually the absolute value of
       
 76590   ** this memory location.  The sign of meta-value 2 determines the
       
 76591   ** synchronous setting.  A negative value means synchronous is off
       
 76592   ** and a positive value means synchronous is on.
       
 76593   */
       
 76594   if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
       
 76595     static const VdbeOpList getCacheSize[] = {
       
 76596       { OP_Transaction, 0, 0,        0},                         /* 0 */
       
 76597       { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
       
 76598       { OP_IfPos,       1, 7,        0},
       
 76599       { OP_Integer,     0, 2,        0},
       
 76600       { OP_Subtract,    1, 2,        1},
       
 76601       { OP_IfPos,       1, 7,        0},
       
 76602       { OP_Integer,     0, 1,        0},                         /* 6 */
       
 76603       { OP_ResultRow,   1, 1,        0},
       
 76604     };
       
 76605     int addr;
       
 76606     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
       
 76607     sqlite3VdbeUsesBtree(v, iDb);
       
 76608     if( !zRight ){
       
 76609       sqlite3VdbeSetNumCols(v, 1);
       
 76610       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
       
 76611       pParse->nMem += 2;
       
 76612       addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
       
 76613       sqlite3VdbeChangeP1(v, addr, iDb);
       
 76614       sqlite3VdbeChangeP1(v, addr+1, iDb);
       
 76615       sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
       
 76616     }else{
       
 76617       int size = atoi(zRight);
       
 76618       if( size<0 ) size = -size;
       
 76619       sqlite3BeginWriteOperation(pParse, 0, iDb);
       
 76620       sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
       
 76621       sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, 2, BTREE_DEFAULT_CACHE_SIZE);
       
 76622       addr = sqlite3VdbeAddOp2(v, OP_IfPos, 2, 0);
       
 76623       sqlite3VdbeAddOp2(v, OP_Integer, -size, 1);
       
 76624       sqlite3VdbeJumpHere(v, addr);
       
 76625       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
       
 76626       pDb->pSchema->cache_size = size;
       
 76627       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
       
 76628     }
       
 76629   }else
       
 76630 
       
 76631   /*
       
 76632   **  PRAGMA [database.]page_size
       
 76633   **  PRAGMA [database.]page_size=N
       
 76634   **
       
 76635   ** The first form reports the current setting for the
       
 76636   ** database page size in bytes.  The second form sets the
       
 76637   ** database page size value.  The value can only be set if
       
 76638   ** the database has not yet been created.
       
 76639   */
       
 76640   if( sqlite3StrICmp(zLeft,"page_size")==0 ){
       
 76641     Btree *pBt = pDb->pBt;
       
 76642     assert( pBt!=0 );
       
 76643     if( !zRight ){
       
 76644       int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
       
 76645       returnSingleInt(pParse, "page_size", size);
       
 76646     }else{
       
 76647       /* Malloc may fail when setting the page-size, as there is an internal
       
 76648       ** buffer that the pager module resizes using sqlite3_realloc().
       
 76649       */
       
 76650       db->nextPagesize = atoi(zRight);
       
 76651       if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
       
 76652         db->mallocFailed = 1;
       
 76653       }
       
 76654     }
       
 76655   }else
       
 76656 
       
 76657   /*
       
 76658   **  PRAGMA [database.]max_page_count
       
 76659   **  PRAGMA [database.]max_page_count=N
       
 76660   **
       
 76661   ** The first form reports the current setting for the
       
 76662   ** maximum number of pages in the database file.  The 
       
 76663   ** second form attempts to change this setting.  Both
       
 76664   ** forms return the current setting.
       
 76665   */
       
 76666   if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){
       
 76667     Btree *pBt = pDb->pBt;
       
 76668     int newMax = 0;
       
 76669     assert( pBt!=0 );
       
 76670     if( zRight ){
       
 76671       newMax = atoi(zRight);
       
 76672     }
       
 76673     if( ALWAYS(pBt) ){
       
 76674       newMax = sqlite3BtreeMaxPageCount(pBt, newMax);
       
 76675     }
       
 76676     returnSingleInt(pParse, "max_page_count", newMax);
       
 76677   }else
       
 76678 
       
 76679   /*
       
 76680   **  PRAGMA [database.]page_count
       
 76681   **
       
 76682   ** Return the number of pages in the specified database.
       
 76683   */
       
 76684   if( sqlite3StrICmp(zLeft,"page_count")==0 ){
       
 76685     int iReg;
       
 76686     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
       
 76687     sqlite3CodeVerifySchema(pParse, iDb);
       
 76688     iReg = ++pParse->nMem;
       
 76689     sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
       
 76690     sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
       
 76691     sqlite3VdbeSetNumCols(v, 1);
       
 76692     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "page_count", SQLITE_STATIC);
       
 76693   }else
       
 76694 
       
 76695   /*
       
 76696   **  PRAGMA [database.]locking_mode
       
 76697   **  PRAGMA [database.]locking_mode = (normal|exclusive)
       
 76698   */
       
 76699   if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
       
 76700     const char *zRet = "normal";
       
 76701     int eMode = getLockingMode(zRight);
       
 76702 
       
 76703     if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
       
 76704       /* Simple "PRAGMA locking_mode;" statement. This is a query for
       
 76705       ** the current default locking mode (which may be different to
       
 76706       ** the locking-mode of the main database).
       
 76707       */
       
 76708       eMode = db->dfltLockMode;
       
 76709     }else{
       
 76710       Pager *pPager;
       
 76711       if( pId2->n==0 ){
       
 76712         /* This indicates that no database name was specified as part
       
 76713         ** of the PRAGMA command. In this case the locking-mode must be
       
 76714         ** set on all attached databases, as well as the main db file.
       
 76715         **
       
 76716         ** Also, the sqlite3.dfltLockMode variable is set so that
       
 76717         ** any subsequently attached databases also use the specified
       
 76718         ** locking mode.
       
 76719         */
       
 76720         int ii;
       
 76721         assert(pDb==&db->aDb[0]);
       
 76722         for(ii=2; ii<db->nDb; ii++){
       
 76723           pPager = sqlite3BtreePager(db->aDb[ii].pBt);
       
 76724           sqlite3PagerLockingMode(pPager, eMode);
       
 76725         }
       
 76726         db->dfltLockMode = (u8)eMode;
       
 76727       }
       
 76728       pPager = sqlite3BtreePager(pDb->pBt);
       
 76729       eMode = sqlite3PagerLockingMode(pPager, eMode);
       
 76730     }
       
 76731 
       
 76732     assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
       
 76733     if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
       
 76734       zRet = "exclusive";
       
 76735     }
       
 76736     sqlite3VdbeSetNumCols(v, 1);
       
 76737     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
       
 76738     sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
       
 76739     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
       
 76740   }else
       
 76741 
       
 76742   /*
       
 76743   **  PRAGMA [database.]journal_mode
       
 76744   **  PRAGMA [database.]journal_mode = (delete|persist|off|truncate|memory)
       
 76745   */
       
 76746   if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
       
 76747     int eMode;
       
 76748     static char * const azModeName[] = {
       
 76749       "delete", "persist", "off", "truncate", "memory"
       
 76750     };
       
 76751 
       
 76752     if( zRight==0 ){
       
 76753       eMode = PAGER_JOURNALMODE_QUERY;
       
 76754     }else{
       
 76755       int n = sqlite3Strlen30(zRight);
       
 76756       eMode = sizeof(azModeName)/sizeof(azModeName[0]) - 1;
       
 76757       while( eMode>=0 && sqlite3StrNICmp(zRight, azModeName[eMode], n)!=0 ){
       
 76758         eMode--;
       
 76759       }
       
 76760     }
       
 76761     if( pId2->n==0 && eMode==PAGER_JOURNALMODE_QUERY ){
       
 76762       /* Simple "PRAGMA journal_mode;" statement. This is a query for
       
 76763       ** the current default journal mode (which may be different to
       
 76764       ** the journal-mode of the main database).
       
 76765       */
       
 76766       eMode = db->dfltJournalMode;
       
 76767     }else{
       
 76768       Pager *pPager;
       
 76769       if( pId2->n==0 ){
       
 76770         /* This indicates that no database name was specified as part
       
 76771         ** of the PRAGMA command. In this case the journal-mode must be
       
 76772         ** set on all attached databases, as well as the main db file.
       
 76773         **
       
 76774         ** Also, the sqlite3.dfltJournalMode variable is set so that
       
 76775         ** any subsequently attached databases also use the specified
       
 76776         ** journal mode.
       
 76777         */
       
 76778         int ii;
       
 76779         assert(pDb==&db->aDb[0]);
       
 76780         for(ii=1; ii<db->nDb; ii++){
       
 76781           if( db->aDb[ii].pBt ){
       
 76782             pPager = sqlite3BtreePager(db->aDb[ii].pBt);
       
 76783             sqlite3PagerJournalMode(pPager, eMode);
       
 76784           }
       
 76785         }
       
 76786         db->dfltJournalMode = (u8)eMode;
       
 76787       }
       
 76788       pPager = sqlite3BtreePager(pDb->pBt);
       
 76789       eMode = sqlite3PagerJournalMode(pPager, eMode);
       
 76790     }
       
 76791     assert( eMode==PAGER_JOURNALMODE_DELETE
       
 76792               || eMode==PAGER_JOURNALMODE_TRUNCATE
       
 76793               || eMode==PAGER_JOURNALMODE_PERSIST
       
 76794               || eMode==PAGER_JOURNALMODE_OFF
       
 76795               || eMode==PAGER_JOURNALMODE_MEMORY );
       
 76796     sqlite3VdbeSetNumCols(v, 1);
       
 76797     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
       
 76798     sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, 
       
 76799            azModeName[eMode], P4_STATIC);
       
 76800     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
       
 76801   }else
       
 76802 
       
 76803   /*
       
 76804   **  PRAGMA [database.]journal_size_limit
       
 76805   **  PRAGMA [database.]journal_size_limit=N
       
 76806   **
       
 76807   ** Get or set the size limit on rollback journal files.
       
 76808   */
       
 76809   if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
       
 76810     Pager *pPager = sqlite3BtreePager(pDb->pBt);
       
 76811     i64 iLimit = -2;
       
 76812     if( zRight ){
       
 76813       sqlite3Atoi64(zRight, &iLimit);
       
 76814       if( iLimit<-1 ) iLimit = -1;
       
 76815     }
       
 76816     iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
       
 76817     returnSingleInt(pParse, "journal_size_limit", iLimit);
       
 76818   }else
       
 76819 
       
 76820 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
       
 76821 
       
 76822   /*
       
 76823   **  PRAGMA [database.]auto_vacuum
       
 76824   **  PRAGMA [database.]auto_vacuum=N
       
 76825   **
       
 76826   ** Get or set the value of the database 'auto-vacuum' parameter.
       
 76827   ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
       
 76828   */
       
 76829 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 76830   if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
       
 76831     Btree *pBt = pDb->pBt;
       
 76832     assert( pBt!=0 );
       
 76833     if( sqlite3ReadSchema(pParse) ){
       
 76834       goto pragma_out;
       
 76835     }
       
 76836     if( !zRight ){
       
 76837       int auto_vacuum;
       
 76838       if( ALWAYS(pBt) ){
       
 76839          auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
       
 76840       }else{
       
 76841          auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
       
 76842       }
       
 76843       returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
       
 76844     }else{
       
 76845       int eAuto = getAutoVacuum(zRight);
       
 76846       assert( eAuto>=0 && eAuto<=2 );
       
 76847       db->nextAutovac = (u8)eAuto;
       
 76848       if( ALWAYS(eAuto>=0) ){
       
 76849         /* Call SetAutoVacuum() to set initialize the internal auto and
       
 76850         ** incr-vacuum flags. This is required in case this connection
       
 76851         ** creates the database file. It is important that it is created
       
 76852         ** as an auto-vacuum capable db.
       
 76853         */
       
 76854         int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
       
 76855         if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
       
 76856           /* When setting the auto_vacuum mode to either "full" or 
       
 76857           ** "incremental", write the value of meta[6] in the database
       
 76858           ** file. Before writing to meta[6], check that meta[3] indicates
       
 76859           ** that this really is an auto-vacuum capable database.
       
 76860           */
       
 76861           static const VdbeOpList setMeta6[] = {
       
 76862             { OP_Transaction,    0,         1,                 0},    /* 0 */
       
 76863             { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
       
 76864             { OP_If,             1,         0,                 0},    /* 2 */
       
 76865             { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
       
 76866             { OP_Integer,        0,         1,                 0},    /* 4 */
       
 76867             { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
       
 76868           };
       
 76869           int iAddr;
       
 76870           iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
       
 76871           sqlite3VdbeChangeP1(v, iAddr, iDb);
       
 76872           sqlite3VdbeChangeP1(v, iAddr+1, iDb);
       
 76873           sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
       
 76874           sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
       
 76875           sqlite3VdbeChangeP1(v, iAddr+5, iDb);
       
 76876           sqlite3VdbeUsesBtree(v, iDb);
       
 76877         }
       
 76878       }
       
 76879     }
       
 76880   }else
       
 76881 #endif
       
 76882 
       
 76883   /*
       
 76884   **  PRAGMA [database.]incremental_vacuum(N)
       
 76885   **
       
 76886   ** Do N steps of incremental vacuuming on a database.
       
 76887   */
       
 76888 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 76889   if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
       
 76890     int iLimit, addr;
       
 76891     if( sqlite3ReadSchema(pParse) ){
       
 76892       goto pragma_out;
       
 76893     }
       
 76894     if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
       
 76895       iLimit = 0x7fffffff;
       
 76896     }
       
 76897     sqlite3BeginWriteOperation(pParse, 0, iDb);
       
 76898     sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
       
 76899     addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
       
 76900     sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
       
 76901     sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
       
 76902     sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
       
 76903     sqlite3VdbeJumpHere(v, addr);
       
 76904   }else
       
 76905 #endif
       
 76906 
       
 76907 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
       
 76908   /*
       
 76909   **  PRAGMA [database.]cache_size
       
 76910   **  PRAGMA [database.]cache_size=N
       
 76911   **
       
 76912   ** The first form reports the current local setting for the
       
 76913   ** page cache size.  The local setting can be different from
       
 76914   ** the persistent cache size value that is stored in the database
       
 76915   ** file itself.  The value returned is the maximum number of
       
 76916   ** pages in the page cache.  The second form sets the local
       
 76917   ** page cache size value.  It does not change the persistent
       
 76918   ** cache size stored on the disk so the cache size will revert
       
 76919   ** to its default value when the database is closed and reopened.
       
 76920   ** N should be a positive integer.
       
 76921   */
       
 76922   if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
       
 76923     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
       
 76924     if( !zRight ){
       
 76925       returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
       
 76926     }else{
       
 76927       int size = atoi(zRight);
       
 76928       if( size<0 ) size = -size;
       
 76929       pDb->pSchema->cache_size = size;
       
 76930       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
       
 76931     }
       
 76932   }else
       
 76933 
       
 76934   /*
       
 76935   **   PRAGMA temp_store
       
 76936   **   PRAGMA temp_store = "default"|"memory"|"file"
       
 76937   **
       
 76938   ** Return or set the local value of the temp_store flag.  Changing
       
 76939   ** the local value does not make changes to the disk file and the default
       
 76940   ** value will be restored the next time the database is opened.
       
 76941   **
       
 76942   ** Note that it is possible for the library compile-time options to
       
 76943   ** override this setting
       
 76944   */
       
 76945   if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
       
 76946     if( !zRight ){
       
 76947       returnSingleInt(pParse, "temp_store", db->temp_store);
       
 76948     }else{
       
 76949       changeTempStorage(pParse, zRight);
       
 76950     }
       
 76951   }else
       
 76952 
       
 76953   /*
       
 76954   **   PRAGMA temp_store_directory
       
 76955   **   PRAGMA temp_store_directory = ""|"directory_name"
       
 76956   **
       
 76957   ** Return or set the local value of the temp_store_directory flag.  Changing
       
 76958   ** the value sets a specific directory to be used for temporary files.
       
 76959   ** Setting to a null string reverts to the default temporary directory search.
       
 76960   ** If temporary directory is changed, then invalidateTempStorage.
       
 76961   **
       
 76962   */
       
 76963   if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
       
 76964     if( !zRight ){
       
 76965       if( sqlite3_temp_directory ){
       
 76966         sqlite3VdbeSetNumCols(v, 1);
       
 76967         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
       
 76968             "temp_store_directory", SQLITE_STATIC);
       
 76969         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
       
 76970         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
       
 76971       }
       
 76972     }else{
       
 76973 #ifndef SQLITE_OMIT_WSD
       
 76974       if( zRight[0] ){
       
 76975         int rc;
       
 76976         int res;
       
 76977         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
       
 76978         if( rc!=SQLITE_OK || res==0 ){
       
 76979           sqlite3ErrorMsg(pParse, "not a writable directory");
       
 76980           goto pragma_out;
       
 76981         }
       
 76982       }
       
 76983       if( SQLITE_TEMP_STORE==0
       
 76984        || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
       
 76985        || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
       
 76986       ){
       
 76987         invalidateTempStorage(pParse);
       
 76988       }
       
 76989       sqlite3_free(sqlite3_temp_directory);
       
 76990       if( zRight[0] ){
       
 76991         sqlite3_temp_directory = sqlite3DbStrDup(0, zRight);
       
 76992       }else{
       
 76993         sqlite3_temp_directory = 0;
       
 76994       }
       
 76995 #endif /* SQLITE_OMIT_WSD */
       
 76996     }
       
 76997   }else
       
 76998 
       
 76999 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
       
 77000 #  if defined(__APPLE__)
       
 77001 #    define SQLITE_ENABLE_LOCKING_STYLE 1
       
 77002 #  else
       
 77003 #    define SQLITE_ENABLE_LOCKING_STYLE 0
       
 77004 #  endif
       
 77005 #endif
       
 77006 #if SQLITE_ENABLE_LOCKING_STYLE
       
 77007   /*
       
 77008    **   PRAGMA [database.]lock_proxy_file
       
 77009    **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
       
 77010    **
       
 77011    ** Return or set the value of the lock_proxy_file flag.  Changing
       
 77012    ** the value sets a specific file to be used for database access locks.
       
 77013    **
       
 77014    */
       
 77015   if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
       
 77016     if( !zRight ){
       
 77017       Pager *pPager = sqlite3BtreePager(pDb->pBt);
       
 77018       char *proxy_file_path = NULL;
       
 77019       sqlite3_file *pFile = sqlite3PagerFile(pPager);
       
 77020       sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE, 
       
 77021                            &proxy_file_path);
       
 77022       
       
 77023       if( proxy_file_path ){
       
 77024         sqlite3VdbeSetNumCols(v, 1);
       
 77025         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
       
 77026                               "lock_proxy_file", SQLITE_STATIC);
       
 77027         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
       
 77028         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
       
 77029       }
       
 77030     }else{
       
 77031       Pager *pPager = sqlite3BtreePager(pDb->pBt);
       
 77032       sqlite3_file *pFile = sqlite3PagerFile(pPager);
       
 77033       int res;
       
 77034       if( zRight[0] ){
       
 77035         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 
       
 77036                                      zRight);
       
 77037       } else {
       
 77038         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 
       
 77039                                      NULL);
       
 77040       }
       
 77041       if( res!=SQLITE_OK ){
       
 77042         sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
       
 77043         goto pragma_out;
       
 77044       }
       
 77045     }
       
 77046   }else
       
 77047 #endif /* SQLITE_ENABLE_LOCKING_STYLE */      
       
 77048     
       
 77049   /*
       
 77050   **   PRAGMA [database.]synchronous
       
 77051   **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
       
 77052   **
       
 77053   ** Return or set the local value of the synchronous flag.  Changing
       
 77054   ** the local value does not make changes to the disk file and the
       
 77055   ** default value will be restored the next time the database is
       
 77056   ** opened.
       
 77057   */
       
 77058   if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
       
 77059     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
       
 77060     if( !zRight ){
       
 77061       returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
       
 77062     }else{
       
 77063       if( !db->autoCommit ){
       
 77064         sqlite3ErrorMsg(pParse, 
       
 77065             "Safety level may not be changed inside a transaction");
       
 77066       }else{
       
 77067         pDb->safety_level = getSafetyLevel(zRight)+1;
       
 77068       }
       
 77069     }
       
 77070   }else
       
 77071 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
       
 77072 
       
 77073 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
       
 77074   if( flagPragma(pParse, zLeft, zRight) ){
       
 77075     /* The flagPragma() subroutine also generates any necessary code
       
 77076     ** there is nothing more to do here */
       
 77077   }else
       
 77078 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
       
 77079 
       
 77080 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
       
 77081   /*
       
 77082   **   PRAGMA table_info(<table>)
       
 77083   **
       
 77084   ** Return a single row for each column of the named table. The columns of
       
 77085   ** the returned data set are:
       
 77086   **
       
 77087   ** cid:        Column id (numbered from left to right, starting at 0)
       
 77088   ** name:       Column name
       
 77089   ** type:       Column declaration type.
       
 77090   ** notnull:    True if 'NOT NULL' is part of column declaration
       
 77091   ** dflt_value: The default value for the column, if any.
       
 77092   */
       
 77093   if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
       
 77094     Table *pTab;
       
 77095     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
       
 77096     pTab = sqlite3FindTable(db, zRight, zDb);
       
 77097     if( pTab ){
       
 77098       int i;
       
 77099       int nHidden = 0;
       
 77100       Column *pCol;
       
 77101       sqlite3VdbeSetNumCols(v, 6);
       
 77102       pParse->nMem = 6;
       
 77103       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
       
 77104       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
       
 77105       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
       
 77106       sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
       
 77107       sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
       
 77108       sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
       
 77109       sqlite3ViewGetColumnNames(pParse, pTab);
       
 77110       for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
       
 77111         if( IsHiddenColumn(pCol) ){
       
 77112           nHidden++;
       
 77113           continue;
       
 77114         }
       
 77115         sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
       
 77116         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
       
 77117         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
       
 77118            pCol->zType ? pCol->zType : "", 0);
       
 77119         sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
       
 77120         if( pCol->zDflt ){
       
 77121           sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
       
 77122         }else{
       
 77123           sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
       
 77124         }
       
 77125         sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
       
 77126         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
       
 77127       }
       
 77128     }
       
 77129   }else
       
 77130 
       
 77131   if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
       
 77132     Index *pIdx;
       
 77133     Table *pTab;
       
 77134     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
       
 77135     pIdx = sqlite3FindIndex(db, zRight, zDb);
       
 77136     if( pIdx ){
       
 77137       int i;
       
 77138       pTab = pIdx->pTable;
       
 77139       sqlite3VdbeSetNumCols(v, 3);
       
 77140       pParse->nMem = 3;
       
 77141       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
       
 77142       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
       
 77143       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
       
 77144       for(i=0; i<pIdx->nColumn; i++){
       
 77145         int cnum = pIdx->aiColumn[i];
       
 77146         sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
       
 77147         sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
       
 77148         assert( pTab->nCol>cnum );
       
 77149         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
       
 77150         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
       
 77151       }
       
 77152     }
       
 77153   }else
       
 77154 
       
 77155   if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
       
 77156     Index *pIdx;
       
 77157     Table *pTab;
       
 77158     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
       
 77159     pTab = sqlite3FindTable(db, zRight, zDb);
       
 77160     if( pTab ){
       
 77161       v = sqlite3GetVdbe(pParse);
       
 77162       pIdx = pTab->pIndex;
       
 77163       if( pIdx ){
       
 77164         int i = 0; 
       
 77165         sqlite3VdbeSetNumCols(v, 3);
       
 77166         pParse->nMem = 3;
       
 77167         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
       
 77168         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
       
 77169         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
       
 77170         while(pIdx){
       
 77171           sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
       
 77172           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
       
 77173           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
       
 77174           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
       
 77175           ++i;
       
 77176           pIdx = pIdx->pNext;
       
 77177         }
       
 77178       }
       
 77179     }
       
 77180   }else
       
 77181 
       
 77182   if( sqlite3StrICmp(zLeft, "database_list")==0 ){
       
 77183     int i;
       
 77184     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
       
 77185     sqlite3VdbeSetNumCols(v, 3);
       
 77186     pParse->nMem = 3;
       
 77187     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
       
 77188     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
       
 77189     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
       
 77190     for(i=0; i<db->nDb; i++){
       
 77191       if( db->aDb[i].pBt==0 ) continue;
       
 77192       assert( db->aDb[i].zName!=0 );
       
 77193       sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
       
 77194       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
       
 77195       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
       
 77196            sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
       
 77197       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
       
 77198     }
       
 77199   }else
       
 77200 
       
 77201   if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
       
 77202     int i = 0;
       
 77203     HashElem *p;
       
 77204     sqlite3VdbeSetNumCols(v, 2);
       
 77205     pParse->nMem = 2;
       
 77206     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
       
 77207     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
       
 77208     for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
       
 77209       CollSeq *pColl = (CollSeq *)sqliteHashData(p);
       
 77210       sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
       
 77211       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
       
 77212       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
       
 77213     }
       
 77214   }else
       
 77215 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
       
 77216 
       
 77217 #ifndef SQLITE_OMIT_FOREIGN_KEY
       
 77218   if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
       
 77219     FKey *pFK;
       
 77220     Table *pTab;
       
 77221     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
       
 77222     pTab = sqlite3FindTable(db, zRight, zDb);
       
 77223     if( pTab ){
       
 77224       v = sqlite3GetVdbe(pParse);
       
 77225       pFK = pTab->pFKey;
       
 77226       if( pFK ){
       
 77227         int i = 0; 
       
 77228         sqlite3VdbeSetNumCols(v, 8);
       
 77229         pParse->nMem = 8;
       
 77230         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
       
 77231         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
       
 77232         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
       
 77233         sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
       
 77234         sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
       
 77235         sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
       
 77236         sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
       
 77237         sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
       
 77238         while(pFK){
       
 77239           int j;
       
 77240           for(j=0; j<pFK->nCol; j++){
       
 77241             char *zCol = pFK->aCol[j].zCol;
       
 77242             char *zOnDelete = (char *)actionName(pFK->aAction[0]);
       
 77243             char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
       
 77244             sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
       
 77245             sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
       
 77246             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
       
 77247             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
       
 77248                               pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
       
 77249             sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
       
 77250             sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
       
 77251             sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
       
 77252             sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
       
 77253             sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
       
 77254           }
       
 77255           ++i;
       
 77256           pFK = pFK->pNextFrom;
       
 77257         }
       
 77258       }
       
 77259     }
       
 77260   }else
       
 77261 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
       
 77262 
       
 77263 #ifndef NDEBUG
       
 77264   if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
       
 77265     if( zRight ){
       
 77266       if( getBoolean(zRight) ){
       
 77267         sqlite3ParserTrace(stderr, "parser: ");
       
 77268       }else{
       
 77269         sqlite3ParserTrace(0, 0);
       
 77270       }
       
 77271     }
       
 77272   }else
       
 77273 #endif
       
 77274 
       
 77275   /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
       
 77276   ** used will be case sensitive or not depending on the RHS.
       
 77277   */
       
 77278   if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
       
 77279     if( zRight ){
       
 77280       sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
       
 77281     }
       
 77282   }else
       
 77283 
       
 77284 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
       
 77285 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
       
 77286 #endif
       
 77287 
       
 77288 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
       
 77289   /* Pragma "quick_check" is an experimental reduced version of 
       
 77290   ** integrity_check designed to detect most database corruption
       
 77291   ** without most of the overhead of a full integrity-check.
       
 77292   */
       
 77293   if( sqlite3StrICmp(zLeft, "integrity_check")==0
       
 77294    || sqlite3StrICmp(zLeft, "quick_check")==0 
       
 77295   ){
       
 77296     int i, j, addr, mxErr;
       
 77297 
       
 77298     /* Code that appears at the end of the integrity check.  If no error
       
 77299     ** messages have been generated, output OK.  Otherwise output the
       
 77300     ** error message
       
 77301     */
       
 77302     static const VdbeOpList endCode[] = {
       
 77303       { OP_AddImm,      1, 0,        0},    /* 0 */
       
 77304       { OP_IfNeg,       1, 0,        0},    /* 1 */
       
 77305       { OP_String8,     0, 3,        0},    /* 2 */
       
 77306       { OP_ResultRow,   3, 1,        0},
       
 77307     };
       
 77308 
       
 77309     int isQuick = (zLeft[0]=='q');
       
 77310 
       
 77311     /* Initialize the VDBE program */
       
 77312     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
       
 77313     pParse->nMem = 6;
       
 77314     sqlite3VdbeSetNumCols(v, 1);
       
 77315     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
       
 77316 
       
 77317     /* Set the maximum error count */
       
 77318     mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
       
 77319     if( zRight ){
       
 77320       mxErr = atoi(zRight);
       
 77321       if( mxErr<=0 ){
       
 77322         mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
       
 77323       }
       
 77324     }
       
 77325     sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
       
 77326 
       
 77327     /* Do an integrity check on each database file */
       
 77328     for(i=0; i<db->nDb; i++){
       
 77329       HashElem *x;
       
 77330       Hash *pTbls;
       
 77331       int cnt = 0;
       
 77332 
       
 77333       if( OMIT_TEMPDB && i==1 ) continue;
       
 77334 
       
 77335       sqlite3CodeVerifySchema(pParse, i);
       
 77336       addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
       
 77337       sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
       
 77338       sqlite3VdbeJumpHere(v, addr);
       
 77339 
       
 77340       /* Do an integrity check of the B-Tree
       
 77341       **
       
 77342       ** Begin by filling registers 2, 3, ... with the root pages numbers
       
 77343       ** for all tables and indices in the database.
       
 77344       */
       
 77345       pTbls = &db->aDb[i].pSchema->tblHash;
       
 77346       for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
       
 77347         Table *pTab = sqliteHashData(x);
       
 77348         Index *pIdx;
       
 77349         sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
       
 77350         cnt++;
       
 77351         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       
 77352           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
       
 77353           cnt++;
       
 77354         }
       
 77355       }
       
 77356 
       
 77357       /* Make sure sufficient number of registers have been allocated */
       
 77358       if( pParse->nMem < cnt+4 ){
       
 77359         pParse->nMem = cnt+4;
       
 77360       }
       
 77361 
       
 77362       /* Do the b-tree integrity checks */
       
 77363       sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
       
 77364       sqlite3VdbeChangeP5(v, (u8)i);
       
 77365       addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
       
 77366       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
       
 77367          sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
       
 77368          P4_DYNAMIC);
       
 77369       sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
       
 77370       sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
       
 77371       sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
       
 77372       sqlite3VdbeJumpHere(v, addr);
       
 77373 
       
 77374       /* Make sure all the indices are constructed correctly.
       
 77375       */
       
 77376       for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
       
 77377         Table *pTab = sqliteHashData(x);
       
 77378         Index *pIdx;
       
 77379         int loopTop;
       
 77380 
       
 77381         if( pTab->pIndex==0 ) continue;
       
 77382         addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
       
 77383         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
       
 77384         sqlite3VdbeJumpHere(v, addr);
       
 77385         sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
       
 77386         sqlite3VdbeAddOp2(v, OP_Integer, 0, 2);  /* reg(2) will count entries */
       
 77387         loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
       
 77388         sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1);   /* increment entry count */
       
 77389         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
       
 77390           int jmp2;
       
 77391           static const VdbeOpList idxErr[] = {
       
 77392             { OP_AddImm,      1, -1,  0},
       
 77393             { OP_String8,     0,  3,  0},    /* 1 */
       
 77394             { OP_Rowid,       1,  4,  0},
       
 77395             { OP_String8,     0,  5,  0},    /* 3 */
       
 77396             { OP_String8,     0,  6,  0},    /* 4 */
       
 77397             { OP_Concat,      4,  3,  3},
       
 77398             { OP_Concat,      5,  3,  3},
       
 77399             { OP_Concat,      6,  3,  3},
       
 77400             { OP_ResultRow,   3,  1,  0},
       
 77401             { OP_IfPos,       1,  0,  0},    /* 9 */
       
 77402             { OP_Halt,        0,  0,  0},
       
 77403           };
       
 77404           sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 1);
       
 77405           jmp2 = sqlite3VdbeAddOp3(v, OP_Found, j+2, 0, 3);
       
 77406           addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
       
 77407           sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
       
 77408           sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
       
 77409           sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC);
       
 77410           sqlite3VdbeJumpHere(v, addr+9);
       
 77411           sqlite3VdbeJumpHere(v, jmp2);
       
 77412         }
       
 77413         sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
       
 77414         sqlite3VdbeJumpHere(v, loopTop);
       
 77415         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
       
 77416           static const VdbeOpList cntIdx[] = {
       
 77417              { OP_Integer,      0,  3,  0},
       
 77418              { OP_Rewind,       0,  0,  0},  /* 1 */
       
 77419              { OP_AddImm,       3,  1,  0},
       
 77420              { OP_Next,         0,  0,  0},  /* 3 */
       
 77421              { OP_Eq,           2,  0,  3},  /* 4 */
       
 77422              { OP_AddImm,       1, -1,  0},
       
 77423              { OP_String8,      0,  2,  0},  /* 6 */
       
 77424              { OP_String8,      0,  3,  0},  /* 7 */
       
 77425              { OP_Concat,       3,  2,  2},
       
 77426              { OP_ResultRow,    2,  1,  0},
       
 77427           };
       
 77428           addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
       
 77429           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
       
 77430           sqlite3VdbeJumpHere(v, addr);
       
 77431           addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
       
 77432           sqlite3VdbeChangeP1(v, addr+1, j+2);
       
 77433           sqlite3VdbeChangeP2(v, addr+1, addr+4);
       
 77434           sqlite3VdbeChangeP1(v, addr+3, j+2);
       
 77435           sqlite3VdbeChangeP2(v, addr+3, addr+2);
       
 77436           sqlite3VdbeJumpHere(v, addr+4);
       
 77437           sqlite3VdbeChangeP4(v, addr+6, 
       
 77438                      "wrong # of entries in index ", P4_STATIC);
       
 77439           sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC);
       
 77440         }
       
 77441       } 
       
 77442     }
       
 77443     addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
       
 77444     sqlite3VdbeChangeP2(v, addr, -mxErr);
       
 77445     sqlite3VdbeJumpHere(v, addr+1);
       
 77446     sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
       
 77447   }else
       
 77448 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
       
 77449 
       
 77450 #ifndef SQLITE_OMIT_UTF16
       
 77451   /*
       
 77452   **   PRAGMA encoding
       
 77453   **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
       
 77454   **
       
 77455   ** In its first form, this pragma returns the encoding of the main
       
 77456   ** database. If the database is not initialized, it is initialized now.
       
 77457   **
       
 77458   ** The second form of this pragma is a no-op if the main database file
       
 77459   ** has not already been initialized. In this case it sets the default
       
 77460   ** encoding that will be used for the main database file if a new file
       
 77461   ** is created. If an existing main database file is opened, then the
       
 77462   ** default text encoding for the existing database is used.
       
 77463   ** 
       
 77464   ** In all cases new databases created using the ATTACH command are
       
 77465   ** created to use the same default text encoding as the main database. If
       
 77466   ** the main database has not been initialized and/or created when ATTACH
       
 77467   ** is executed, this is done before the ATTACH operation.
       
 77468   **
       
 77469   ** In the second form this pragma sets the text encoding to be used in
       
 77470   ** new database files created using this database handle. It is only
       
 77471   ** useful if invoked immediately after the main database i
       
 77472   */
       
 77473   if( sqlite3StrICmp(zLeft, "encoding")==0 ){
       
 77474     static const struct EncName {
       
 77475       char *zName;
       
 77476       u8 enc;
       
 77477     } encnames[] = {
       
 77478       { "UTF8",     SQLITE_UTF8        },
       
 77479       { "UTF-8",    SQLITE_UTF8        },  /* Must be element [1] */
       
 77480       { "UTF-16le", SQLITE_UTF16LE     },  /* Must be element [2] */
       
 77481       { "UTF-16be", SQLITE_UTF16BE     },  /* Must be element [3] */
       
 77482       { "UTF16le",  SQLITE_UTF16LE     },
       
 77483       { "UTF16be",  SQLITE_UTF16BE     },
       
 77484       { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
       
 77485       { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
       
 77486       { 0, 0 }
       
 77487     };
       
 77488     const struct EncName *pEnc;
       
 77489     if( !zRight ){    /* "PRAGMA encoding" */
       
 77490       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
       
 77491       sqlite3VdbeSetNumCols(v, 1);
       
 77492       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
       
 77493       sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
       
 77494       assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
       
 77495       assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
       
 77496       assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
       
 77497       sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
       
 77498       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
       
 77499     }else{                        /* "PRAGMA encoding = XXX" */
       
 77500       /* Only change the value of sqlite.enc if the database handle is not
       
 77501       ** initialized. If the main database exists, the new sqlite.enc value
       
 77502       ** will be overwritten when the schema is next loaded. If it does not
       
 77503       ** already exists, it will be created to use the new encoding value.
       
 77504       */
       
 77505       if( 
       
 77506         !(DbHasProperty(db, 0, DB_SchemaLoaded)) || 
       
 77507         DbHasProperty(db, 0, DB_Empty) 
       
 77508       ){
       
 77509         for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
       
 77510           if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
       
 77511             ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
       
 77512             break;
       
 77513           }
       
 77514         }
       
 77515         if( !pEnc->zName ){
       
 77516           sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
       
 77517         }
       
 77518       }
       
 77519     }
       
 77520   }else
       
 77521 #endif /* SQLITE_OMIT_UTF16 */
       
 77522 
       
 77523 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
       
 77524   /*
       
 77525   **   PRAGMA [database.]schema_version
       
 77526   **   PRAGMA [database.]schema_version = <integer>
       
 77527   **
       
 77528   **   PRAGMA [database.]user_version
       
 77529   **   PRAGMA [database.]user_version = <integer>
       
 77530   **
       
 77531   ** The pragma's schema_version and user_version are used to set or get
       
 77532   ** the value of the schema-version and user-version, respectively. Both
       
 77533   ** the schema-version and the user-version are 32-bit signed integers
       
 77534   ** stored in the database header.
       
 77535   **
       
 77536   ** The schema-cookie is usually only manipulated internally by SQLite. It
       
 77537   ** is incremented by SQLite whenever the database schema is modified (by
       
 77538   ** creating or dropping a table or index). The schema version is used by
       
 77539   ** SQLite each time a query is executed to ensure that the internal cache
       
 77540   ** of the schema used when compiling the SQL query matches the schema of
       
 77541   ** the database against which the compiled query is actually executed.
       
 77542   ** Subverting this mechanism by using "PRAGMA schema_version" to modify
       
 77543   ** the schema-version is potentially dangerous and may lead to program
       
 77544   ** crashes or database corruption. Use with caution!
       
 77545   **
       
 77546   ** The user-version is not used internally by SQLite. It may be used by
       
 77547   ** applications for any purpose.
       
 77548   */
       
 77549   if( sqlite3StrICmp(zLeft, "schema_version")==0 
       
 77550    || sqlite3StrICmp(zLeft, "user_version")==0 
       
 77551    || sqlite3StrICmp(zLeft, "freelist_count")==0 
       
 77552   ){
       
 77553     int iCookie;   /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
       
 77554     sqlite3VdbeUsesBtree(v, iDb);
       
 77555     switch( zLeft[0] ){
       
 77556       case 'f': case 'F':
       
 77557         iCookie = BTREE_FREE_PAGE_COUNT;
       
 77558         break;
       
 77559       case 's': case 'S':
       
 77560         iCookie = BTREE_SCHEMA_VERSION;
       
 77561         break;
       
 77562       default:
       
 77563         iCookie = BTREE_USER_VERSION;
       
 77564         break;
       
 77565     }
       
 77566 
       
 77567     if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
       
 77568       /* Write the specified cookie value */
       
 77569       static const VdbeOpList setCookie[] = {
       
 77570         { OP_Transaction,    0,  1,  0},    /* 0 */
       
 77571         { OP_Integer,        0,  1,  0},    /* 1 */
       
 77572         { OP_SetCookie,      0,  0,  1},    /* 2 */
       
 77573       };
       
 77574       int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
       
 77575       sqlite3VdbeChangeP1(v, addr, iDb);
       
 77576       sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
       
 77577       sqlite3VdbeChangeP1(v, addr+2, iDb);
       
 77578       sqlite3VdbeChangeP2(v, addr+2, iCookie);
       
 77579     }else{
       
 77580       /* Read the specified cookie value */
       
 77581       static const VdbeOpList readCookie[] = {
       
 77582         { OP_Transaction,     0,  0,  0},    /* 0 */
       
 77583         { OP_ReadCookie,      0,  1,  0},    /* 1 */
       
 77584         { OP_ResultRow,       1,  1,  0}
       
 77585       };
       
 77586       int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
       
 77587       sqlite3VdbeChangeP1(v, addr, iDb);
       
 77588       sqlite3VdbeChangeP1(v, addr+1, iDb);
       
 77589       sqlite3VdbeChangeP3(v, addr+1, iCookie);
       
 77590       sqlite3VdbeSetNumCols(v, 1);
       
 77591       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
       
 77592     }
       
 77593   }else
       
 77594 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
       
 77595 
       
 77596 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
       
 77597   /*
       
 77598   ** Report the current state of file logs for all databases
       
 77599   */
       
 77600   if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
       
 77601     static const char *const azLockName[] = {
       
 77602       "unlocked", "shared", "reserved", "pending", "exclusive"
       
 77603     };
       
 77604     int i;
       
 77605     sqlite3VdbeSetNumCols(v, 2);
       
 77606     pParse->nMem = 2;
       
 77607     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
       
 77608     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
       
 77609     for(i=0; i<db->nDb; i++){
       
 77610       Btree *pBt;
       
 77611       Pager *pPager;
       
 77612       const char *zState = "unknown";
       
 77613       int j;
       
 77614       if( db->aDb[i].zName==0 ) continue;
       
 77615       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
       
 77616       pBt = db->aDb[i].pBt;
       
 77617       if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
       
 77618         zState = "closed";
       
 77619       }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, 
       
 77620                                      SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
       
 77621          zState = azLockName[j];
       
 77622       }
       
 77623       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
       
 77624       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
       
 77625     }
       
 77626 
       
 77627   }else
       
 77628 #endif
       
 77629 
       
 77630 #if SQLITE_HAS_CODEC
       
 77631   if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
       
 77632     sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
       
 77633   }else
       
 77634   if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
       
 77635     sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
       
 77636   }else
       
 77637   if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
       
 77638                  sqlite3StrICmp(zLeft, "hexrekey")==0) ){
       
 77639     int i, h1, h2;
       
 77640     char zKey[40];
       
 77641     for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
       
 77642       h1 += 9*(1&(h1>>6));
       
 77643       h2 += 9*(1&(h2>>6));
       
 77644       zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
       
 77645     }
       
 77646     if( (zLeft[3] & 0xf)==0xb ){
       
 77647       sqlite3_key(db, zKey, i/2);
       
 77648     }else{
       
 77649       sqlite3_rekey(db, zKey, i/2);
       
 77650     }
       
 77651   }else
       
 77652 #endif
       
 77653 #if SQLITE_HAS_CODEC || defined(SQLITE_ENABLE_CEROD)
       
 77654   if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
       
 77655 #if SQLITE_HAS_CODEC
       
 77656     if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
       
 77657       extern void sqlite3_activate_see(const char*);
       
 77658       sqlite3_activate_see(&zRight[4]);
       
 77659     }
       
 77660 #endif
       
 77661 #ifdef SQLITE_ENABLE_CEROD
       
 77662     if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
       
 77663       extern void sqlite3_activate_cerod(const char*);
       
 77664       sqlite3_activate_cerod(&zRight[6]);
       
 77665     }
       
 77666 #endif
       
 77667   }else
       
 77668 #endif
       
 77669 
       
 77670  
       
 77671   {/* Empty ELSE clause */}
       
 77672 
       
 77673   /* Code an OP_Expire at the end of each PRAGMA program to cause
       
 77674   ** the VDBE implementing the pragma to expire. Most (all?) pragmas
       
 77675   ** are only valid for a single execution.
       
 77676   */
       
 77677   sqlite3VdbeAddOp2(v, OP_Expire, 1, 0);
       
 77678 
       
 77679   /*
       
 77680   ** Reset the safety level, in case the fullfsync flag or synchronous
       
 77681   ** setting changed.
       
 77682   */
       
 77683 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
       
 77684   if( db->autoCommit ){
       
 77685     sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
       
 77686                (db->flags&SQLITE_FullFSync)!=0);
       
 77687   }
       
 77688 #endif
       
 77689 pragma_out:
       
 77690   sqlite3DbFree(db, zLeft);
       
 77691   sqlite3DbFree(db, zRight);
       
 77692 }
       
 77693 
       
 77694 #endif /* SQLITE_OMIT_PRAGMA */
       
 77695 
       
 77696 /************** End of pragma.c **********************************************/
       
 77697 /************** Begin file prepare.c *****************************************/
       
 77698 /*
       
 77699 ** 2005 May 25
       
 77700 **
       
 77701 ** The author disclaims copyright to this source code.  In place of
       
 77702 ** a legal notice, here is a blessing:
       
 77703 **
       
 77704 **    May you do good and not evil.
       
 77705 **    May you find forgiveness for yourself and forgive others.
       
 77706 **    May you share freely, never taking more than you give.
       
 77707 **
       
 77708 *************************************************************************
       
 77709 ** This file contains the implementation of the sqlite3_prepare()
       
 77710 ** interface, and routines that contribute to loading the database schema
       
 77711 ** from disk.
       
 77712 **
       
 77713 ** $Id: prepare.c,v 1.131 2009/08/06 17:43:31 drh Exp $
       
 77714 */
       
 77715 
       
 77716 /*
       
 77717 ** Fill the InitData structure with an error message that indicates
       
 77718 ** that the database is corrupt.
       
 77719 */
       
 77720 static void corruptSchema(
       
 77721   InitData *pData,     /* Initialization context */
       
 77722   const char *zObj,    /* Object being parsed at the point of error */
       
 77723   const char *zExtra   /* Error information */
       
 77724 ){
       
 77725   sqlite3 *db = pData->db;
       
 77726   if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
       
 77727     if( zObj==0 ) zObj = "?";
       
 77728     sqlite3SetString(pData->pzErrMsg, db,
       
 77729       "malformed database schema (%s)", zObj);
       
 77730     if( zExtra ){
       
 77731       *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg, 
       
 77732                                  "%s - %s", *pData->pzErrMsg, zExtra);
       
 77733     }
       
 77734   }
       
 77735   pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT;
       
 77736 }
       
 77737 
       
 77738 /*
       
 77739 ** This is the callback routine for the code that initializes the
       
 77740 ** database.  See sqlite3Init() below for additional information.
       
 77741 ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
       
 77742 **
       
 77743 ** Each callback contains the following information:
       
 77744 **
       
 77745 **     argv[0] = name of thing being created
       
 77746 **     argv[1] = root page number for table or index. 0 for trigger or view.
       
 77747 **     argv[2] = SQL text for the CREATE statement.
       
 77748 **
       
 77749 */
       
 77750 SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
       
 77751   InitData *pData = (InitData*)pInit;
       
 77752   sqlite3 *db = pData->db;
       
 77753   int iDb = pData->iDb;
       
 77754 
       
 77755   assert( argc==3 );
       
 77756   UNUSED_PARAMETER2(NotUsed, argc);
       
 77757   assert( sqlite3_mutex_held(db->mutex) );
       
 77758   DbClearProperty(db, iDb, DB_Empty);
       
 77759   if( db->mallocFailed ){
       
 77760     corruptSchema(pData, argv[0], 0);
       
 77761     return 1;
       
 77762   }
       
 77763 
       
 77764   assert( iDb>=0 && iDb<db->nDb );
       
 77765   if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
       
 77766   if( argv[1]==0 ){
       
 77767     corruptSchema(pData, argv[0], 0);
       
 77768   }else if( argv[2] && argv[2][0] ){
       
 77769     /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
       
 77770     ** But because db->init.busy is set to 1, no VDBE code is generated
       
 77771     ** or executed.  All the parser does is build the internal data
       
 77772     ** structures that describe the table, index, or view.
       
 77773     */
       
 77774     char *zErr;
       
 77775     int rc;
       
 77776     assert( db->init.busy );
       
 77777     db->init.iDb = iDb;
       
 77778     db->init.newTnum = atoi(argv[1]);
       
 77779     db->init.orphanTrigger = 0;
       
 77780     rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
       
 77781     db->init.iDb = 0;
       
 77782     assert( rc!=SQLITE_OK || zErr==0 );
       
 77783     if( SQLITE_OK!=rc ){
       
 77784       if( db->init.orphanTrigger ){
       
 77785         assert( iDb==1 );
       
 77786       }else{
       
 77787         pData->rc = rc;
       
 77788         if( rc==SQLITE_NOMEM ){
       
 77789           db->mallocFailed = 1;
       
 77790         }else if( rc!=SQLITE_INTERRUPT && rc!=SQLITE_LOCKED ){
       
 77791           corruptSchema(pData, argv[0], zErr);
       
 77792         }
       
 77793       }
       
 77794       sqlite3DbFree(db, zErr);
       
 77795     }
       
 77796   }else if( argv[0]==0 ){
       
 77797     corruptSchema(pData, 0, 0);
       
 77798   }else{
       
 77799     /* If the SQL column is blank it means this is an index that
       
 77800     ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
       
 77801     ** constraint for a CREATE TABLE.  The index should have already
       
 77802     ** been created when we processed the CREATE TABLE.  All we have
       
 77803     ** to do here is record the root page number for that index.
       
 77804     */
       
 77805     Index *pIndex;
       
 77806     pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
       
 77807     if( pIndex==0 ){
       
 77808       /* This can occur if there exists an index on a TEMP table which
       
 77809       ** has the same name as another index on a permanent index.  Since
       
 77810       ** the permanent table is hidden by the TEMP table, we can also
       
 77811       ** safely ignore the index on the permanent table.
       
 77812       */
       
 77813       /* Do Nothing */;
       
 77814     }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
       
 77815       corruptSchema(pData, argv[0], "invalid rootpage");
       
 77816     }
       
 77817   }
       
 77818   return 0;
       
 77819 }
       
 77820 
       
 77821 /*
       
 77822 ** Attempt to read the database schema and initialize internal
       
 77823 ** data structures for a single database file.  The index of the
       
 77824 ** database file is given by iDb.  iDb==0 is used for the main
       
 77825 ** database.  iDb==1 should never be used.  iDb>=2 is used for
       
 77826 ** auxiliary databases.  Return one of the SQLITE_ error codes to
       
 77827 ** indicate success or failure.
       
 77828 */
       
 77829 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
       
 77830   int rc;
       
 77831   int i;
       
 77832   int size;
       
 77833   Table *pTab;
       
 77834   Db *pDb;
       
 77835   char const *azArg[4];
       
 77836   int meta[5];
       
 77837   InitData initData;
       
 77838   char const *zMasterSchema;
       
 77839   char const *zMasterName = SCHEMA_TABLE(iDb);
       
 77840   int openedTransaction = 0;
       
 77841 
       
 77842   /*
       
 77843   ** The master database table has a structure like this
       
 77844   */
       
 77845   static const char master_schema[] = 
       
 77846      "CREATE TABLE sqlite_master(\n"
       
 77847      "  type text,\n"
       
 77848      "  name text,\n"
       
 77849      "  tbl_name text,\n"
       
 77850      "  rootpage integer,\n"
       
 77851      "  sql text\n"
       
 77852      ")"
       
 77853   ;
       
 77854 #ifndef SQLITE_OMIT_TEMPDB
       
 77855   static const char temp_master_schema[] = 
       
 77856      "CREATE TEMP TABLE sqlite_temp_master(\n"
       
 77857      "  type text,\n"
       
 77858      "  name text,\n"
       
 77859      "  tbl_name text,\n"
       
 77860      "  rootpage integer,\n"
       
 77861      "  sql text\n"
       
 77862      ")"
       
 77863   ;
       
 77864 #else
       
 77865   #define temp_master_schema 0
       
 77866 #endif
       
 77867 
       
 77868   assert( iDb>=0 && iDb<db->nDb );
       
 77869   assert( db->aDb[iDb].pSchema );
       
 77870   assert( sqlite3_mutex_held(db->mutex) );
       
 77871   assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
       
 77872 
       
 77873   /* zMasterSchema and zInitScript are set to point at the master schema
       
 77874   ** and initialisation script appropriate for the database being
       
 77875   ** initialised. zMasterName is the name of the master table.
       
 77876   */
       
 77877   if( !OMIT_TEMPDB && iDb==1 ){
       
 77878     zMasterSchema = temp_master_schema;
       
 77879   }else{
       
 77880     zMasterSchema = master_schema;
       
 77881   }
       
 77882   zMasterName = SCHEMA_TABLE(iDb);
       
 77883 
       
 77884   /* Construct the schema tables.  */
       
 77885   azArg[0] = zMasterName;
       
 77886   azArg[1] = "1";
       
 77887   azArg[2] = zMasterSchema;
       
 77888   azArg[3] = 0;
       
 77889   initData.db = db;
       
 77890   initData.iDb = iDb;
       
 77891   initData.rc = SQLITE_OK;
       
 77892   initData.pzErrMsg = pzErrMsg;
       
 77893   (void)sqlite3SafetyOff(db);
       
 77894   sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
       
 77895   (void)sqlite3SafetyOn(db);
       
 77896   if( initData.rc ){
       
 77897     rc = initData.rc;
       
 77898     goto error_out;
       
 77899   }
       
 77900   pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
       
 77901   if( ALWAYS(pTab) ){
       
 77902     pTab->tabFlags |= TF_Readonly;
       
 77903   }
       
 77904 
       
 77905   /* Create a cursor to hold the database open
       
 77906   */
       
 77907   pDb = &db->aDb[iDb];
       
 77908   if( pDb->pBt==0 ){
       
 77909     if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
       
 77910       DbSetProperty(db, 1, DB_SchemaLoaded);
       
 77911     }
       
 77912     return SQLITE_OK;
       
 77913   }
       
 77914 
       
 77915   /* If there is not already a read-only (or read-write) transaction opened
       
 77916   ** on the b-tree database, open one now. If a transaction is opened, it 
       
 77917   ** will be closed before this function returns.  */
       
 77918   sqlite3BtreeEnter(pDb->pBt);
       
 77919   if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
       
 77920     rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
       
 77921     if( rc!=SQLITE_OK ){
       
 77922       sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
       
 77923       goto initone_error_out;
       
 77924     }
       
 77925     openedTransaction = 1;
       
 77926   }
       
 77927 
       
 77928   /* Get the database meta information.
       
 77929   **
       
 77930   ** Meta values are as follows:
       
 77931   **    meta[0]   Schema cookie.  Changes with each schema change.
       
 77932   **    meta[1]   File format of schema layer.
       
 77933   **    meta[2]   Size of the page cache.
       
 77934   **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
       
 77935   **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
       
 77936   **    meta[5]   User version
       
 77937   **    meta[6]   Incremental vacuum mode
       
 77938   **    meta[7]   unused
       
 77939   **    meta[8]   unused
       
 77940   **    meta[9]   unused
       
 77941   **
       
 77942   ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
       
 77943   ** the possible values of meta[4].
       
 77944   */
       
 77945   for(i=0; i<ArraySize(meta); i++){
       
 77946     sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
       
 77947   }
       
 77948   pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
       
 77949 
       
 77950   /* If opening a non-empty database, check the text encoding. For the
       
 77951   ** main database, set sqlite3.enc to the encoding of the main database.
       
 77952   ** For an attached db, it is an error if the encoding is not the same
       
 77953   ** as sqlite3.enc.
       
 77954   */
       
 77955   if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
       
 77956     if( iDb==0 ){
       
 77957       u8 encoding;
       
 77958       /* If opening the main database, set ENC(db). */
       
 77959       encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
       
 77960       if( encoding==0 ) encoding = SQLITE_UTF8;
       
 77961       ENC(db) = encoding;
       
 77962       db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
       
 77963     }else{
       
 77964       /* If opening an attached database, the encoding much match ENC(db) */
       
 77965       if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
       
 77966         sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
       
 77967             " text encoding as main database");
       
 77968         rc = SQLITE_ERROR;
       
 77969         goto initone_error_out;
       
 77970       }
       
 77971     }
       
 77972   }else{
       
 77973     DbSetProperty(db, iDb, DB_Empty);
       
 77974   }
       
 77975   pDb->pSchema->enc = ENC(db);
       
 77976 
       
 77977   if( pDb->pSchema->cache_size==0 ){
       
 77978     size = meta[BTREE_DEFAULT_CACHE_SIZE-1];
       
 77979     if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
       
 77980     if( size<0 ) size = -size;
       
 77981     pDb->pSchema->cache_size = size;
       
 77982     sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
       
 77983   }
       
 77984 
       
 77985   /*
       
 77986   ** file_format==1    Version 3.0.0.
       
 77987   ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
       
 77988   ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
       
 77989   ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
       
 77990   */
       
 77991   pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
       
 77992   if( pDb->pSchema->file_format==0 ){
       
 77993     pDb->pSchema->file_format = 1;
       
 77994   }
       
 77995   if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
       
 77996     sqlite3SetString(pzErrMsg, db, "unsupported file format");
       
 77997     rc = SQLITE_ERROR;
       
 77998     goto initone_error_out;
       
 77999   }
       
 78000 
       
 78001   /* Ticket #2804:  When we open a database in the newer file format,
       
 78002   ** clear the legacy_file_format pragma flag so that a VACUUM will
       
 78003   ** not downgrade the database and thus invalidate any descending
       
 78004   ** indices that the user might have created.
       
 78005   */
       
 78006   if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
       
 78007     db->flags &= ~SQLITE_LegacyFileFmt;
       
 78008   }
       
 78009 
       
 78010   /* Read the schema information out of the schema tables
       
 78011   */
       
 78012   assert( db->init.busy );
       
 78013   {
       
 78014     char *zSql;
       
 78015     zSql = sqlite3MPrintf(db, 
       
 78016         "SELECT name, rootpage, sql FROM '%q'.%s",
       
 78017         db->aDb[iDb].zName, zMasterName);
       
 78018     (void)sqlite3SafetyOff(db);
       
 78019 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 78020     {
       
 78021       int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
       
 78022       xAuth = db->xAuth;
       
 78023       db->xAuth = 0;
       
 78024 #endif
       
 78025       rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
       
 78026 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 78027       db->xAuth = xAuth;
       
 78028     }
       
 78029 #endif
       
 78030     if( rc==SQLITE_OK ) rc = initData.rc;
       
 78031     (void)sqlite3SafetyOn(db);
       
 78032     sqlite3DbFree(db, zSql);
       
 78033 #ifndef SQLITE_OMIT_ANALYZE
       
 78034     if( rc==SQLITE_OK ){
       
 78035       sqlite3AnalysisLoad(db, iDb);
       
 78036     }
       
 78037 #endif
       
 78038   }
       
 78039   if( db->mallocFailed ){
       
 78040     rc = SQLITE_NOMEM;
       
 78041     sqlite3ResetInternalSchema(db, 0);
       
 78042   }
       
 78043   if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
       
 78044     /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
       
 78045     ** the schema loaded, even if errors occurred. In this situation the 
       
 78046     ** current sqlite3_prepare() operation will fail, but the following one
       
 78047     ** will attempt to compile the supplied statement against whatever subset
       
 78048     ** of the schema was loaded before the error occurred. The primary
       
 78049     ** purpose of this is to allow access to the sqlite_master table
       
 78050     ** even when its contents have been corrupted.
       
 78051     */
       
 78052     DbSetProperty(db, iDb, DB_SchemaLoaded);
       
 78053     rc = SQLITE_OK;
       
 78054   }
       
 78055 
       
 78056   /* Jump here for an error that occurs after successfully allocating
       
 78057   ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
       
 78058   ** before that point, jump to error_out.
       
 78059   */
       
 78060 initone_error_out:
       
 78061   if( openedTransaction ){
       
 78062     sqlite3BtreeCommit(pDb->pBt);
       
 78063   }
       
 78064   sqlite3BtreeLeave(pDb->pBt);
       
 78065 
       
 78066 error_out:
       
 78067   if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
       
 78068     db->mallocFailed = 1;
       
 78069   }
       
 78070   return rc;
       
 78071 }
       
 78072 
       
 78073 /*
       
 78074 ** Initialize all database files - the main database file, the file
       
 78075 ** used to store temporary tables, and any additional database files
       
 78076 ** created using ATTACH statements.  Return a success code.  If an
       
 78077 ** error occurs, write an error message into *pzErrMsg.
       
 78078 **
       
 78079 ** After a database is initialized, the DB_SchemaLoaded bit is set
       
 78080 ** bit is set in the flags field of the Db structure. If the database
       
 78081 ** file was of zero-length, then the DB_Empty flag is also set.
       
 78082 */
       
 78083 SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
       
 78084   int i, rc;
       
 78085   int commit_internal = !(db->flags&SQLITE_InternChanges);
       
 78086   
       
 78087   assert( sqlite3_mutex_held(db->mutex) );
       
 78088   rc = SQLITE_OK;
       
 78089   db->init.busy = 1;
       
 78090   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
       
 78091     if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
       
 78092     rc = sqlite3InitOne(db, i, pzErrMsg);
       
 78093     if( rc ){
       
 78094       sqlite3ResetInternalSchema(db, i);
       
 78095     }
       
 78096   }
       
 78097 
       
 78098   /* Once all the other databases have been initialised, load the schema
       
 78099   ** for the TEMP database. This is loaded last, as the TEMP database
       
 78100   ** schema may contain references to objects in other databases.
       
 78101   */
       
 78102 #ifndef SQLITE_OMIT_TEMPDB
       
 78103   if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
       
 78104                     && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
       
 78105     rc = sqlite3InitOne(db, 1, pzErrMsg);
       
 78106     if( rc ){
       
 78107       sqlite3ResetInternalSchema(db, 1);
       
 78108     }
       
 78109   }
       
 78110 #endif
       
 78111 
       
 78112   db->init.busy = 0;
       
 78113   if( rc==SQLITE_OK && commit_internal ){
       
 78114     sqlite3CommitInternalChanges(db);
       
 78115   }
       
 78116 
       
 78117   return rc; 
       
 78118 }
       
 78119 
       
 78120 /*
       
 78121 ** This routine is a no-op if the database schema is already initialised.
       
 78122 ** Otherwise, the schema is loaded. An error code is returned.
       
 78123 */
       
 78124 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
       
 78125   int rc = SQLITE_OK;
       
 78126   sqlite3 *db = pParse->db;
       
 78127   assert( sqlite3_mutex_held(db->mutex) );
       
 78128   if( !db->init.busy ){
       
 78129     rc = sqlite3Init(db, &pParse->zErrMsg);
       
 78130   }
       
 78131   if( rc!=SQLITE_OK ){
       
 78132     pParse->rc = rc;
       
 78133     pParse->nErr++;
       
 78134   }
       
 78135   return rc;
       
 78136 }
       
 78137 
       
 78138 
       
 78139 /*
       
 78140 ** Check schema cookies in all databases.  If any cookie is out
       
 78141 ** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
       
 78142 ** make no changes to pParse->rc.
       
 78143 */
       
 78144 static void schemaIsValid(Parse *pParse){
       
 78145   sqlite3 *db = pParse->db;
       
 78146   int iDb;
       
 78147   int rc;
       
 78148   int cookie;
       
 78149 
       
 78150   assert( pParse->checkSchema );
       
 78151   assert( sqlite3_mutex_held(db->mutex) );
       
 78152   for(iDb=0; iDb<db->nDb; iDb++){
       
 78153     int openedTransaction = 0;         /* True if a transaction is opened */
       
 78154     Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
       
 78155     if( pBt==0 ) continue;
       
 78156 
       
 78157     /* If there is not already a read-only (or read-write) transaction opened
       
 78158     ** on the b-tree database, open one now. If a transaction is opened, it 
       
 78159     ** will be closed immediately after reading the meta-value. */
       
 78160     if( !sqlite3BtreeIsInReadTrans(pBt) ){
       
 78161       rc = sqlite3BtreeBeginTrans(pBt, 0);
       
 78162       if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
       
 78163         db->mallocFailed = 1;
       
 78164       }
       
 78165       if( rc!=SQLITE_OK ) return;
       
 78166       openedTransaction = 1;
       
 78167     }
       
 78168 
       
 78169     /* Read the schema cookie from the database. If it does not match the 
       
 78170     ** value stored as part of the in the in-memory schema representation,
       
 78171     ** set Parse.rc to SQLITE_SCHEMA. */
       
 78172     sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
       
 78173     if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
       
 78174       pParse->rc = SQLITE_SCHEMA;
       
 78175     }
       
 78176 
       
 78177     /* Close the transaction, if one was opened. */
       
 78178     if( openedTransaction ){
       
 78179       sqlite3BtreeCommit(pBt);
       
 78180     }
       
 78181   }
       
 78182 }
       
 78183 
       
 78184 /*
       
 78185 ** Convert a schema pointer into the iDb index that indicates
       
 78186 ** which database file in db->aDb[] the schema refers to.
       
 78187 **
       
 78188 ** If the same database is attached more than once, the first
       
 78189 ** attached database is returned.
       
 78190 */
       
 78191 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
       
 78192   int i = -1000000;
       
 78193 
       
 78194   /* If pSchema is NULL, then return -1000000. This happens when code in 
       
 78195   ** expr.c is trying to resolve a reference to a transient table (i.e. one
       
 78196   ** created by a sub-select). In this case the return value of this 
       
 78197   ** function should never be used.
       
 78198   **
       
 78199   ** We return -1000000 instead of the more usual -1 simply because using
       
 78200   ** -1000000 as the incorrect index into db->aDb[] is much 
       
 78201   ** more likely to cause a segfault than -1 (of course there are assert()
       
 78202   ** statements too, but it never hurts to play the odds).
       
 78203   */
       
 78204   assert( sqlite3_mutex_held(db->mutex) );
       
 78205   if( pSchema ){
       
 78206     for(i=0; ALWAYS(i<db->nDb); i++){
       
 78207       if( db->aDb[i].pSchema==pSchema ){
       
 78208         break;
       
 78209       }
       
 78210     }
       
 78211     assert( i>=0 && i<db->nDb );
       
 78212   }
       
 78213   return i;
       
 78214 }
       
 78215 
       
 78216 /*
       
 78217 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
       
 78218 */
       
 78219 static int sqlite3Prepare(
       
 78220   sqlite3 *db,              /* Database handle. */
       
 78221   const char *zSql,         /* UTF-8 encoded SQL statement. */
       
 78222   int nBytes,               /* Length of zSql in bytes. */
       
 78223   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
       
 78224   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
       
 78225   const char **pzTail       /* OUT: End of parsed string */
       
 78226 ){
       
 78227   Parse *pParse;            /* Parsing context */
       
 78228   char *zErrMsg = 0;        /* Error message */
       
 78229   int rc = SQLITE_OK;       /* Result code */
       
 78230   int i;                    /* Loop counter */
       
 78231 
       
 78232   /* Allocate the parsing context */
       
 78233   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
       
 78234   if( pParse==0 ){
       
 78235     rc = SQLITE_NOMEM;
       
 78236     goto end_prepare;
       
 78237   }
       
 78238 
       
 78239   if( sqlite3SafetyOn(db) ){
       
 78240     rc = SQLITE_MISUSE;
       
 78241     goto end_prepare;
       
 78242   }
       
 78243   assert( ppStmt && *ppStmt==0 );
       
 78244   assert( !db->mallocFailed );
       
 78245   assert( sqlite3_mutex_held(db->mutex) );
       
 78246 
       
 78247   /* Check to verify that it is possible to get a read lock on all
       
 78248   ** database schemas.  The inability to get a read lock indicates that
       
 78249   ** some other database connection is holding a write-lock, which in
       
 78250   ** turn means that the other connection has made uncommitted changes
       
 78251   ** to the schema.
       
 78252   **
       
 78253   ** Were we to proceed and prepare the statement against the uncommitted
       
 78254   ** schema changes and if those schema changes are subsequently rolled
       
 78255   ** back and different changes are made in their place, then when this
       
 78256   ** prepared statement goes to run the schema cookie would fail to detect
       
 78257   ** the schema change.  Disaster would follow.
       
 78258   **
       
 78259   ** This thread is currently holding mutexes on all Btrees (because
       
 78260   ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
       
 78261   ** is not possible for another thread to start a new schema change
       
 78262   ** while this routine is running.  Hence, we do not need to hold 
       
 78263   ** locks on the schema, we just need to make sure nobody else is 
       
 78264   ** holding them.
       
 78265   **
       
 78266   ** Note that setting READ_UNCOMMITTED overrides most lock detection,
       
 78267   ** but it does *not* override schema lock detection, so this all still
       
 78268   ** works even if READ_UNCOMMITTED is set.
       
 78269   */
       
 78270   for(i=0; i<db->nDb; i++) {
       
 78271     Btree *pBt = db->aDb[i].pBt;
       
 78272     if( pBt ){
       
 78273       assert( sqlite3BtreeHoldsMutex(pBt) );
       
 78274       rc = sqlite3BtreeSchemaLocked(pBt);
       
 78275       if( rc ){
       
 78276         const char *zDb = db->aDb[i].zName;
       
 78277         sqlite3Error(db, rc, "database schema is locked: %s", zDb);
       
 78278         (void)sqlite3SafetyOff(db);
       
 78279         testcase( db->flags & SQLITE_ReadUncommitted );
       
 78280         goto end_prepare;
       
 78281       }
       
 78282     }
       
 78283   }
       
 78284 
       
 78285   sqlite3VtabUnlockList(db);
       
 78286 
       
 78287   pParse->db = db;
       
 78288   if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
       
 78289     char *zSqlCopy;
       
 78290     int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
       
 78291     testcase( nBytes==mxLen );
       
 78292     testcase( nBytes==mxLen+1 );
       
 78293     if( nBytes>mxLen ){
       
 78294       sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
       
 78295       (void)sqlite3SafetyOff(db);
       
 78296       rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
       
 78297       goto end_prepare;
       
 78298     }
       
 78299     zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
       
 78300     if( zSqlCopy ){
       
 78301       sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
       
 78302       sqlite3DbFree(db, zSqlCopy);
       
 78303       pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
       
 78304     }else{
       
 78305       pParse->zTail = &zSql[nBytes];
       
 78306     }
       
 78307   }else{
       
 78308     sqlite3RunParser(pParse, zSql, &zErrMsg);
       
 78309   }
       
 78310 
       
 78311   if( db->mallocFailed ){
       
 78312     pParse->rc = SQLITE_NOMEM;
       
 78313   }
       
 78314   if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
       
 78315   if( pParse->checkSchema ){
       
 78316     schemaIsValid(pParse);
       
 78317   }
       
 78318   if( pParse->rc==SQLITE_SCHEMA ){
       
 78319     sqlite3ResetInternalSchema(db, 0);
       
 78320   }
       
 78321   if( db->mallocFailed ){
       
 78322     pParse->rc = SQLITE_NOMEM;
       
 78323   }
       
 78324   if( pzTail ){
       
 78325     *pzTail = pParse->zTail;
       
 78326   }
       
 78327   rc = pParse->rc;
       
 78328 
       
 78329 #ifndef SQLITE_OMIT_EXPLAIN
       
 78330   if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
       
 78331     static const char * const azColName[] = {
       
 78332        "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
       
 78333        "order", "from", "detail"
       
 78334     };
       
 78335     int iFirst, mx;
       
 78336     if( pParse->explain==2 ){
       
 78337       sqlite3VdbeSetNumCols(pParse->pVdbe, 3);
       
 78338       iFirst = 8;
       
 78339       mx = 11;
       
 78340     }else{
       
 78341       sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
       
 78342       iFirst = 0;
       
 78343       mx = 8;
       
 78344     }
       
 78345     for(i=iFirst; i<mx; i++){
       
 78346       sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
       
 78347                             azColName[i], SQLITE_STATIC);
       
 78348     }
       
 78349   }
       
 78350 #endif
       
 78351 
       
 78352   if( sqlite3SafetyOff(db) ){
       
 78353     rc = SQLITE_MISUSE;
       
 78354   }
       
 78355 
       
 78356   assert( db->init.busy==0 || saveSqlFlag==0 );
       
 78357   if( db->init.busy==0 ){
       
 78358     Vdbe *pVdbe = pParse->pVdbe;
       
 78359     sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
       
 78360   }
       
 78361   if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
       
 78362     sqlite3VdbeFinalize(pParse->pVdbe);
       
 78363     assert(!(*ppStmt));
       
 78364   }else{
       
 78365     *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
       
 78366   }
       
 78367 
       
 78368   if( zErrMsg ){
       
 78369     sqlite3Error(db, rc, "%s", zErrMsg);
       
 78370     sqlite3DbFree(db, zErrMsg);
       
 78371   }else{
       
 78372     sqlite3Error(db, rc, 0);
       
 78373   }
       
 78374 
       
 78375   /* Delete any TriggerPrg structures allocated while parsing this statement. */
       
 78376   while( pParse->pTriggerPrg ){
       
 78377     TriggerPrg *pT = pParse->pTriggerPrg;
       
 78378     pParse->pTriggerPrg = pT->pNext;
       
 78379     sqlite3VdbeProgramDelete(db, pT->pProgram, 0);
       
 78380     sqlite3DbFree(db, pT);
       
 78381   }
       
 78382 
       
 78383 end_prepare:
       
 78384 
       
 78385   sqlite3StackFree(db, pParse);
       
 78386   rc = sqlite3ApiExit(db, rc);
       
 78387   assert( (rc&db->errMask)==rc );
       
 78388   return rc;
       
 78389 }
       
 78390 static int sqlite3LockAndPrepare(
       
 78391   sqlite3 *db,              /* Database handle. */
       
 78392   const char *zSql,         /* UTF-8 encoded SQL statement. */
       
 78393   int nBytes,               /* Length of zSql in bytes. */
       
 78394   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
       
 78395   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
       
 78396   const char **pzTail       /* OUT: End of parsed string */
       
 78397 ){
       
 78398   int rc;
       
 78399   assert( ppStmt!=0 );
       
 78400   *ppStmt = 0;
       
 78401   if( !sqlite3SafetyCheckOk(db) ){
       
 78402     return SQLITE_MISUSE;
       
 78403   }
       
 78404   sqlite3_mutex_enter(db->mutex);
       
 78405   sqlite3BtreeEnterAll(db);
       
 78406   rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail);
       
 78407   if( rc==SQLITE_SCHEMA ){
       
 78408     sqlite3_finalize(*ppStmt);
       
 78409     rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail);
       
 78410   }
       
 78411   sqlite3BtreeLeaveAll(db);
       
 78412   sqlite3_mutex_leave(db->mutex);
       
 78413   return rc;
       
 78414 }
       
 78415 
       
 78416 /*
       
 78417 ** Rerun the compilation of a statement after a schema change.
       
 78418 **
       
 78419 ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
       
 78420 ** if the statement cannot be recompiled because another connection has
       
 78421 ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
       
 78422 ** occurs, return SQLITE_SCHEMA.
       
 78423 */
       
 78424 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
       
 78425   int rc;
       
 78426   sqlite3_stmt *pNew;
       
 78427   const char *zSql;
       
 78428   sqlite3 *db;
       
 78429 
       
 78430   assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
       
 78431   zSql = sqlite3_sql((sqlite3_stmt *)p);
       
 78432   assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
       
 78433   db = sqlite3VdbeDb(p);
       
 78434   assert( sqlite3_mutex_held(db->mutex) );
       
 78435   rc = sqlite3LockAndPrepare(db, zSql, -1, 0, &pNew, 0);
       
 78436   if( rc ){
       
 78437     if( rc==SQLITE_NOMEM ){
       
 78438       db->mallocFailed = 1;
       
 78439     }
       
 78440     assert( pNew==0 );
       
 78441     return (rc==SQLITE_LOCKED) ? SQLITE_LOCKED : SQLITE_SCHEMA;
       
 78442   }else{
       
 78443     assert( pNew!=0 );
       
 78444   }
       
 78445   sqlite3VdbeSwap((Vdbe*)pNew, p);
       
 78446   sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
       
 78447   sqlite3VdbeResetStepResult((Vdbe*)pNew);
       
 78448   sqlite3VdbeFinalize((Vdbe*)pNew);
       
 78449   return SQLITE_OK;
       
 78450 }
       
 78451 
       
 78452 
       
 78453 /*
       
 78454 ** Two versions of the official API.  Legacy and new use.  In the legacy
       
 78455 ** version, the original SQL text is not saved in the prepared statement
       
 78456 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
       
 78457 ** sqlite3_step().  In the new version, the original SQL text is retained
       
 78458 ** and the statement is automatically recompiled if an schema change
       
 78459 ** occurs.
       
 78460 */
       
 78461 SQLITE_API int sqlite3_prepare(
       
 78462   sqlite3 *db,              /* Database handle. */
       
 78463   const char *zSql,         /* UTF-8 encoded SQL statement. */
       
 78464   int nBytes,               /* Length of zSql in bytes. */
       
 78465   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
       
 78466   const char **pzTail       /* OUT: End of parsed string */
       
 78467 ){
       
 78468   int rc;
       
 78469   rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,ppStmt,pzTail);
       
 78470   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
       
 78471   return rc;
       
 78472 }
       
 78473 SQLITE_API int sqlite3_prepare_v2(
       
 78474   sqlite3 *db,              /* Database handle. */
       
 78475   const char *zSql,         /* UTF-8 encoded SQL statement. */
       
 78476   int nBytes,               /* Length of zSql in bytes. */
       
 78477   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
       
 78478   const char **pzTail       /* OUT: End of parsed string */
       
 78479 ){
       
 78480   int rc;
       
 78481   rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,ppStmt,pzTail);
       
 78482   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
       
 78483   return rc;
       
 78484 }
       
 78485 
       
 78486 
       
 78487 #ifndef SQLITE_OMIT_UTF16
       
 78488 /*
       
 78489 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
       
 78490 */
       
 78491 static int sqlite3Prepare16(
       
 78492   sqlite3 *db,              /* Database handle. */ 
       
 78493   const void *zSql,         /* UTF-8 encoded SQL statement. */
       
 78494   int nBytes,               /* Length of zSql in bytes. */
       
 78495   int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
       
 78496   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
       
 78497   const void **pzTail       /* OUT: End of parsed string */
       
 78498 ){
       
 78499   /* This function currently works by first transforming the UTF-16
       
 78500   ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
       
 78501   ** tricky bit is figuring out the pointer to return in *pzTail.
       
 78502   */
       
 78503   char *zSql8;
       
 78504   const char *zTail8 = 0;
       
 78505   int rc = SQLITE_OK;
       
 78506 
       
 78507   assert( ppStmt );
       
 78508   *ppStmt = 0;
       
 78509   if( !sqlite3SafetyCheckOk(db) ){
       
 78510     return SQLITE_MISUSE;
       
 78511   }
       
 78512   sqlite3_mutex_enter(db->mutex);
       
 78513   zSql8 = sqlite3Utf16to8(db, zSql, nBytes);
       
 78514   if( zSql8 ){
       
 78515     rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, ppStmt, &zTail8);
       
 78516   }
       
 78517 
       
 78518   if( zTail8 && pzTail ){
       
 78519     /* If sqlite3_prepare returns a tail pointer, we calculate the
       
 78520     ** equivalent pointer into the UTF-16 string by counting the unicode
       
 78521     ** characters between zSql8 and zTail8, and then returning a pointer
       
 78522     ** the same number of characters into the UTF-16 string.
       
 78523     */
       
 78524     int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
       
 78525     *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
       
 78526   }
       
 78527   sqlite3DbFree(db, zSql8); 
       
 78528   rc = sqlite3ApiExit(db, rc);
       
 78529   sqlite3_mutex_leave(db->mutex);
       
 78530   return rc;
       
 78531 }
       
 78532 
       
 78533 /*
       
 78534 ** Two versions of the official API.  Legacy and new use.  In the legacy
       
 78535 ** version, the original SQL text is not saved in the prepared statement
       
 78536 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
       
 78537 ** sqlite3_step().  In the new version, the original SQL text is retained
       
 78538 ** and the statement is automatically recompiled if an schema change
       
 78539 ** occurs.
       
 78540 */
       
 78541 SQLITE_API int sqlite3_prepare16(
       
 78542   sqlite3 *db,              /* Database handle. */ 
       
 78543   const void *zSql,         /* UTF-8 encoded SQL statement. */
       
 78544   int nBytes,               /* Length of zSql in bytes. */
       
 78545   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
       
 78546   const void **pzTail       /* OUT: End of parsed string */
       
 78547 ){
       
 78548   int rc;
       
 78549   rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
       
 78550   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
       
 78551   return rc;
       
 78552 }
       
 78553 SQLITE_API int sqlite3_prepare16_v2(
       
 78554   sqlite3 *db,              /* Database handle. */ 
       
 78555   const void *zSql,         /* UTF-8 encoded SQL statement. */
       
 78556   int nBytes,               /* Length of zSql in bytes. */
       
 78557   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
       
 78558   const void **pzTail       /* OUT: End of parsed string */
       
 78559 ){
       
 78560   int rc;
       
 78561   rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
       
 78562   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
       
 78563   return rc;
       
 78564 }
       
 78565 
       
 78566 #endif /* SQLITE_OMIT_UTF16 */
       
 78567 
       
 78568 /************** End of prepare.c *********************************************/
       
 78569 /************** Begin file select.c ******************************************/
       
 78570 /*
       
 78571 ** 2001 September 15
       
 78572 **
       
 78573 ** The author disclaims copyright to this source code.  In place of
       
 78574 ** a legal notice, here is a blessing:
       
 78575 **
       
 78576 **    May you do good and not evil.
       
 78577 **    May you find forgiveness for yourself and forgive others.
       
 78578 **    May you share freely, never taking more than you give.
       
 78579 **
       
 78580 *************************************************************************
       
 78581 ** This file contains C code routines that are called by the parser
       
 78582 ** to handle SELECT statements in SQLite.
       
 78583 **
       
 78584 ** $Id: select.c,v 1.526 2009/08/01 15:09:58 drh Exp $
       
 78585 */
       
 78586 
       
 78587 
       
 78588 /*
       
 78589 ** Delete all the content of a Select structure but do not deallocate
       
 78590 ** the select structure itself.
       
 78591 */
       
 78592 static void clearSelect(sqlite3 *db, Select *p){
       
 78593   sqlite3ExprListDelete(db, p->pEList);
       
 78594   sqlite3SrcListDelete(db, p->pSrc);
       
 78595   sqlite3ExprDelete(db, p->pWhere);
       
 78596   sqlite3ExprListDelete(db, p->pGroupBy);
       
 78597   sqlite3ExprDelete(db, p->pHaving);
       
 78598   sqlite3ExprListDelete(db, p->pOrderBy);
       
 78599   sqlite3SelectDelete(db, p->pPrior);
       
 78600   sqlite3ExprDelete(db, p->pLimit);
       
 78601   sqlite3ExprDelete(db, p->pOffset);
       
 78602 }
       
 78603 
       
 78604 /*
       
 78605 ** Initialize a SelectDest structure.
       
 78606 */
       
 78607 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
       
 78608   pDest->eDest = (u8)eDest;
       
 78609   pDest->iParm = iParm;
       
 78610   pDest->affinity = 0;
       
 78611   pDest->iMem = 0;
       
 78612   pDest->nMem = 0;
       
 78613 }
       
 78614 
       
 78615 
       
 78616 /*
       
 78617 ** Allocate a new Select structure and return a pointer to that
       
 78618 ** structure.
       
 78619 */
       
 78620 SQLITE_PRIVATE Select *sqlite3SelectNew(
       
 78621   Parse *pParse,        /* Parsing context */
       
 78622   ExprList *pEList,     /* which columns to include in the result */
       
 78623   SrcList *pSrc,        /* the FROM clause -- which tables to scan */
       
 78624   Expr *pWhere,         /* the WHERE clause */
       
 78625   ExprList *pGroupBy,   /* the GROUP BY clause */
       
 78626   Expr *pHaving,        /* the HAVING clause */
       
 78627   ExprList *pOrderBy,   /* the ORDER BY clause */
       
 78628   int isDistinct,       /* true if the DISTINCT keyword is present */
       
 78629   Expr *pLimit,         /* LIMIT value.  NULL means not used */
       
 78630   Expr *pOffset         /* OFFSET value.  NULL means no offset */
       
 78631 ){
       
 78632   Select *pNew;
       
 78633   Select standin;
       
 78634   sqlite3 *db = pParse->db;
       
 78635   pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
       
 78636   assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
       
 78637   if( pNew==0 ){
       
 78638     pNew = &standin;
       
 78639     memset(pNew, 0, sizeof(*pNew));
       
 78640   }
       
 78641   if( pEList==0 ){
       
 78642     pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
       
 78643   }
       
 78644   pNew->pEList = pEList;
       
 78645   pNew->pSrc = pSrc;
       
 78646   pNew->pWhere = pWhere;
       
 78647   pNew->pGroupBy = pGroupBy;
       
 78648   pNew->pHaving = pHaving;
       
 78649   pNew->pOrderBy = pOrderBy;
       
 78650   pNew->selFlags = isDistinct ? SF_Distinct : 0;
       
 78651   pNew->op = TK_SELECT;
       
 78652   pNew->pLimit = pLimit;
       
 78653   pNew->pOffset = pOffset;
       
 78654   assert( pOffset==0 || pLimit!=0 );
       
 78655   pNew->addrOpenEphm[0] = -1;
       
 78656   pNew->addrOpenEphm[1] = -1;
       
 78657   pNew->addrOpenEphm[2] = -1;
       
 78658   if( db->mallocFailed ) {
       
 78659     clearSelect(db, pNew);
       
 78660     if( pNew!=&standin ) sqlite3DbFree(db, pNew);
       
 78661     pNew = 0;
       
 78662   }
       
 78663   return pNew;
       
 78664 }
       
 78665 
       
 78666 /*
       
 78667 ** Delete the given Select structure and all of its substructures.
       
 78668 */
       
 78669 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
       
 78670   if( p ){
       
 78671     clearSelect(db, p);
       
 78672     sqlite3DbFree(db, p);
       
 78673   }
       
 78674 }
       
 78675 
       
 78676 /*
       
 78677 ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
       
 78678 ** type of join.  Return an integer constant that expresses that type
       
 78679 ** in terms of the following bit values:
       
 78680 **
       
 78681 **     JT_INNER
       
 78682 **     JT_CROSS
       
 78683 **     JT_OUTER
       
 78684 **     JT_NATURAL
       
 78685 **     JT_LEFT
       
 78686 **     JT_RIGHT
       
 78687 **
       
 78688 ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
       
 78689 **
       
 78690 ** If an illegal or unsupported join type is seen, then still return
       
 78691 ** a join type, but put an error in the pParse structure.
       
 78692 */
       
 78693 SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
       
 78694   int jointype = 0;
       
 78695   Token *apAll[3];
       
 78696   Token *p;
       
 78697                              /*   0123456789 123456789 123456789 123 */
       
 78698   static const char zKeyText[] = "naturaleftouterightfullinnercross";
       
 78699   static const struct {
       
 78700     u8 i;        /* Beginning of keyword text in zKeyText[] */
       
 78701     u8 nChar;    /* Length of the keyword in characters */
       
 78702     u8 code;     /* Join type mask */
       
 78703   } aKeyword[] = {
       
 78704     /* natural */ { 0,  7, JT_NATURAL                },
       
 78705     /* left    */ { 6,  4, JT_LEFT|JT_OUTER          },
       
 78706     /* outer   */ { 10, 5, JT_OUTER                  },
       
 78707     /* right   */ { 14, 5, JT_RIGHT|JT_OUTER         },
       
 78708     /* full    */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
       
 78709     /* inner   */ { 23, 5, JT_INNER                  },
       
 78710     /* cross   */ { 28, 5, JT_INNER|JT_CROSS         },
       
 78711   };
       
 78712   int i, j;
       
 78713   apAll[0] = pA;
       
 78714   apAll[1] = pB;
       
 78715   apAll[2] = pC;
       
 78716   for(i=0; i<3 && apAll[i]; i++){
       
 78717     p = apAll[i];
       
 78718     for(j=0; j<ArraySize(aKeyword); j++){
       
 78719       if( p->n==aKeyword[j].nChar 
       
 78720           && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
       
 78721         jointype |= aKeyword[j].code;
       
 78722         break;
       
 78723       }
       
 78724     }
       
 78725     testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
       
 78726     if( j>=ArraySize(aKeyword) ){
       
 78727       jointype |= JT_ERROR;
       
 78728       break;
       
 78729     }
       
 78730   }
       
 78731   if(
       
 78732      (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
       
 78733      (jointype & JT_ERROR)!=0
       
 78734   ){
       
 78735     const char *zSp = " ";
       
 78736     assert( pB!=0 );
       
 78737     if( pC==0 ){ zSp++; }
       
 78738     sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
       
 78739        "%T %T%s%T", pA, pB, zSp, pC);
       
 78740     jointype = JT_INNER;
       
 78741   }else if( (jointype & JT_OUTER)!=0 
       
 78742          && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
       
 78743     sqlite3ErrorMsg(pParse, 
       
 78744       "RIGHT and FULL OUTER JOINs are not currently supported");
       
 78745     jointype = JT_INNER;
       
 78746   }
       
 78747   return jointype;
       
 78748 }
       
 78749 
       
 78750 /*
       
 78751 ** Return the index of a column in a table.  Return -1 if the column
       
 78752 ** is not contained in the table.
       
 78753 */
       
 78754 static int columnIndex(Table *pTab, const char *zCol){
       
 78755   int i;
       
 78756   for(i=0; i<pTab->nCol; i++){
       
 78757     if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
       
 78758   }
       
 78759   return -1;
       
 78760 }
       
 78761 
       
 78762 /*
       
 78763 ** Create an expression node for an identifier with the name of zName
       
 78764 */
       
 78765 SQLITE_PRIVATE Expr *sqlite3CreateIdExpr(Parse *pParse, const char *zName){
       
 78766   return sqlite3Expr(pParse->db, TK_ID, zName);
       
 78767 }
       
 78768 
       
 78769 /*
       
 78770 ** Add a term to the WHERE expression in *ppExpr that requires the
       
 78771 ** zCol column to be equal in the two tables pTab1 and pTab2.
       
 78772 */
       
 78773 static void addWhereTerm(
       
 78774   Parse *pParse,           /* Parsing context */
       
 78775   const char *zCol,        /* Name of the column */
       
 78776   const Table *pTab1,      /* First table */
       
 78777   const char *zAlias1,     /* Alias for first table.  May be NULL */
       
 78778   const Table *pTab2,      /* Second table */
       
 78779   const char *zAlias2,     /* Alias for second table.  May be NULL */
       
 78780   int iRightJoinTable,     /* VDBE cursor for the right table */
       
 78781   Expr **ppExpr,           /* Add the equality term to this expression */
       
 78782   int isOuterJoin          /* True if dealing with an OUTER join */
       
 78783 ){
       
 78784   Expr *pE1a, *pE1b, *pE1c;
       
 78785   Expr *pE2a, *pE2b, *pE2c;
       
 78786   Expr *pE;
       
 78787 
       
 78788   pE1a = sqlite3CreateIdExpr(pParse, zCol);
       
 78789   pE2a = sqlite3CreateIdExpr(pParse, zCol);
       
 78790   if( zAlias1==0 ){
       
 78791     zAlias1 = pTab1->zName;
       
 78792   }
       
 78793   pE1b = sqlite3CreateIdExpr(pParse, zAlias1);
       
 78794   if( zAlias2==0 ){
       
 78795     zAlias2 = pTab2->zName;
       
 78796   }
       
 78797   pE2b = sqlite3CreateIdExpr(pParse, zAlias2);
       
 78798   pE1c = sqlite3PExpr(pParse, TK_DOT, pE1b, pE1a, 0);
       
 78799   pE2c = sqlite3PExpr(pParse, TK_DOT, pE2b, pE2a, 0);
       
 78800   pE = sqlite3PExpr(pParse, TK_EQ, pE1c, pE2c, 0);
       
 78801   if( pE && isOuterJoin ){
       
 78802     ExprSetProperty(pE, EP_FromJoin);
       
 78803     assert( !ExprHasAnyProperty(pE, EP_TokenOnly|EP_Reduced) );
       
 78804     ExprSetIrreducible(pE);
       
 78805     pE->iRightJoinTable = (i16)iRightJoinTable;
       
 78806   }
       
 78807   *ppExpr = sqlite3ExprAnd(pParse->db,*ppExpr, pE);
       
 78808 }
       
 78809 
       
 78810 /*
       
 78811 ** Set the EP_FromJoin property on all terms of the given expression.
       
 78812 ** And set the Expr.iRightJoinTable to iTable for every term in the
       
 78813 ** expression.
       
 78814 **
       
 78815 ** The EP_FromJoin property is used on terms of an expression to tell
       
 78816 ** the LEFT OUTER JOIN processing logic that this term is part of the
       
 78817 ** join restriction specified in the ON or USING clause and not a part
       
 78818 ** of the more general WHERE clause.  These terms are moved over to the
       
 78819 ** WHERE clause during join processing but we need to remember that they
       
 78820 ** originated in the ON or USING clause.
       
 78821 **
       
 78822 ** The Expr.iRightJoinTable tells the WHERE clause processing that the
       
 78823 ** expression depends on table iRightJoinTable even if that table is not
       
 78824 ** explicitly mentioned in the expression.  That information is needed
       
 78825 ** for cases like this:
       
 78826 **
       
 78827 **    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
       
 78828 **
       
 78829 ** The where clause needs to defer the handling of the t1.x=5
       
 78830 ** term until after the t2 loop of the join.  In that way, a
       
 78831 ** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
       
 78832 ** defer the handling of t1.x=5, it will be processed immediately
       
 78833 ** after the t1 loop and rows with t1.x!=5 will never appear in
       
 78834 ** the output, which is incorrect.
       
 78835 */
       
 78836 static void setJoinExpr(Expr *p, int iTable){
       
 78837   while( p ){
       
 78838     ExprSetProperty(p, EP_FromJoin);
       
 78839     assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
       
 78840     ExprSetIrreducible(p);
       
 78841     p->iRightJoinTable = (i16)iTable;
       
 78842     setJoinExpr(p->pLeft, iTable);
       
 78843     p = p->pRight;
       
 78844   } 
       
 78845 }
       
 78846 
       
 78847 /*
       
 78848 ** This routine processes the join information for a SELECT statement.
       
 78849 ** ON and USING clauses are converted into extra terms of the WHERE clause.
       
 78850 ** NATURAL joins also create extra WHERE clause terms.
       
 78851 **
       
 78852 ** The terms of a FROM clause are contained in the Select.pSrc structure.
       
 78853 ** The left most table is the first entry in Select.pSrc.  The right-most
       
 78854 ** table is the last entry.  The join operator is held in the entry to
       
 78855 ** the left.  Thus entry 0 contains the join operator for the join between
       
 78856 ** entries 0 and 1.  Any ON or USING clauses associated with the join are
       
 78857 ** also attached to the left entry.
       
 78858 **
       
 78859 ** This routine returns the number of errors encountered.
       
 78860 */
       
 78861 static int sqliteProcessJoin(Parse *pParse, Select *p){
       
 78862   SrcList *pSrc;                  /* All tables in the FROM clause */
       
 78863   int i, j;                       /* Loop counters */
       
 78864   struct SrcList_item *pLeft;     /* Left table being joined */
       
 78865   struct SrcList_item *pRight;    /* Right table being joined */
       
 78866 
       
 78867   pSrc = p->pSrc;
       
 78868   pLeft = &pSrc->a[0];
       
 78869   pRight = &pLeft[1];
       
 78870   for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
       
 78871     Table *pLeftTab = pLeft->pTab;
       
 78872     Table *pRightTab = pRight->pTab;
       
 78873     int isOuter;
       
 78874 
       
 78875     if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
       
 78876     isOuter = (pRight->jointype & JT_OUTER)!=0;
       
 78877 
       
 78878     /* When the NATURAL keyword is present, add WHERE clause terms for
       
 78879     ** every column that the two tables have in common.
       
 78880     */
       
 78881     if( pRight->jointype & JT_NATURAL ){
       
 78882       if( pRight->pOn || pRight->pUsing ){
       
 78883         sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
       
 78884            "an ON or USING clause", 0);
       
 78885         return 1;
       
 78886       }
       
 78887       for(j=0; j<pLeftTab->nCol; j++){
       
 78888         char *zName = pLeftTab->aCol[j].zName;
       
 78889         if( columnIndex(pRightTab, zName)>=0 ){
       
 78890           addWhereTerm(pParse, zName, pLeftTab, pLeft->zAlias, 
       
 78891                               pRightTab, pRight->zAlias,
       
 78892                               pRight->iCursor, &p->pWhere, isOuter);
       
 78893           
       
 78894         }
       
 78895       }
       
 78896     }
       
 78897 
       
 78898     /* Disallow both ON and USING clauses in the same join
       
 78899     */
       
 78900     if( pRight->pOn && pRight->pUsing ){
       
 78901       sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
       
 78902         "clauses in the same join");
       
 78903       return 1;
       
 78904     }
       
 78905 
       
 78906     /* Add the ON clause to the end of the WHERE clause, connected by
       
 78907     ** an AND operator.
       
 78908     */
       
 78909     if( pRight->pOn ){
       
 78910       if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
       
 78911       p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
       
 78912       pRight->pOn = 0;
       
 78913     }
       
 78914 
       
 78915     /* Create extra terms on the WHERE clause for each column named
       
 78916     ** in the USING clause.  Example: If the two tables to be joined are 
       
 78917     ** A and B and the USING clause names X, Y, and Z, then add this
       
 78918     ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
       
 78919     ** Report an error if any column mentioned in the USING clause is
       
 78920     ** not contained in both tables to be joined.
       
 78921     */
       
 78922     if( pRight->pUsing ){
       
 78923       IdList *pList = pRight->pUsing;
       
 78924       for(j=0; j<pList->nId; j++){
       
 78925         char *zName = pList->a[j].zName;
       
 78926         if( columnIndex(pLeftTab, zName)<0 || columnIndex(pRightTab, zName)<0 ){
       
 78927           sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
       
 78928             "not present in both tables", zName);
       
 78929           return 1;
       
 78930         }
       
 78931         addWhereTerm(pParse, zName, pLeftTab, pLeft->zAlias, 
       
 78932                             pRightTab, pRight->zAlias,
       
 78933                             pRight->iCursor, &p->pWhere, isOuter);
       
 78934       }
       
 78935     }
       
 78936   }
       
 78937   return 0;
       
 78938 }
       
 78939 
       
 78940 /*
       
 78941 ** Insert code into "v" that will push the record on the top of the
       
 78942 ** stack into the sorter.
       
 78943 */
       
 78944 static void pushOntoSorter(
       
 78945   Parse *pParse,         /* Parser context */
       
 78946   ExprList *pOrderBy,    /* The ORDER BY clause */
       
 78947   Select *pSelect,       /* The whole SELECT statement */
       
 78948   int regData            /* Register holding data to be sorted */
       
 78949 ){
       
 78950   Vdbe *v = pParse->pVdbe;
       
 78951   int nExpr = pOrderBy->nExpr;
       
 78952   int regBase = sqlite3GetTempRange(pParse, nExpr+2);
       
 78953   int regRecord = sqlite3GetTempReg(pParse);
       
 78954   sqlite3ExprCacheClear(pParse);
       
 78955   sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
       
 78956   sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
       
 78957   sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
       
 78958   sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
       
 78959   sqlite3VdbeAddOp2(v, OP_IdxInsert, pOrderBy->iECursor, regRecord);
       
 78960   sqlite3ReleaseTempReg(pParse, regRecord);
       
 78961   sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
       
 78962   if( pSelect->iLimit ){
       
 78963     int addr1, addr2;
       
 78964     int iLimit;
       
 78965     if( pSelect->iOffset ){
       
 78966       iLimit = pSelect->iOffset+1;
       
 78967     }else{
       
 78968       iLimit = pSelect->iLimit;
       
 78969     }
       
 78970     addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit);
       
 78971     sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
       
 78972     addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
       
 78973     sqlite3VdbeJumpHere(v, addr1);
       
 78974     sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
       
 78975     sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
       
 78976     sqlite3VdbeJumpHere(v, addr2);
       
 78977     pSelect->iLimit = 0;
       
 78978   }
       
 78979 }
       
 78980 
       
 78981 /*
       
 78982 ** Add code to implement the OFFSET
       
 78983 */
       
 78984 static void codeOffset(
       
 78985   Vdbe *v,          /* Generate code into this VM */
       
 78986   Select *p,        /* The SELECT statement being coded */
       
 78987   int iContinue     /* Jump here to skip the current record */
       
 78988 ){
       
 78989   if( p->iOffset && iContinue!=0 ){
       
 78990     int addr;
       
 78991     sqlite3VdbeAddOp2(v, OP_AddImm, p->iOffset, -1);
       
 78992     addr = sqlite3VdbeAddOp1(v, OP_IfNeg, p->iOffset);
       
 78993     sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
       
 78994     VdbeComment((v, "skip OFFSET records"));
       
 78995     sqlite3VdbeJumpHere(v, addr);
       
 78996   }
       
 78997 }
       
 78998 
       
 78999 /*
       
 79000 ** Add code that will check to make sure the N registers starting at iMem
       
 79001 ** form a distinct entry.  iTab is a sorting index that holds previously
       
 79002 ** seen combinations of the N values.  A new entry is made in iTab
       
 79003 ** if the current N values are new.
       
 79004 **
       
 79005 ** A jump to addrRepeat is made and the N+1 values are popped from the
       
 79006 ** stack if the top N elements are not distinct.
       
 79007 */
       
 79008 static void codeDistinct(
       
 79009   Parse *pParse,     /* Parsing and code generating context */
       
 79010   int iTab,          /* A sorting index used to test for distinctness */
       
 79011   int addrRepeat,    /* Jump to here if not distinct */
       
 79012   int N,             /* Number of elements */
       
 79013   int iMem           /* First element */
       
 79014 ){
       
 79015   Vdbe *v;
       
 79016   int r1;
       
 79017 
       
 79018   v = pParse->pVdbe;
       
 79019   r1 = sqlite3GetTempReg(pParse);
       
 79020   sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
       
 79021   sqlite3VdbeAddOp3(v, OP_Found, iTab, addrRepeat, r1);
       
 79022   sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
       
 79023   sqlite3ReleaseTempReg(pParse, r1);
       
 79024 }
       
 79025 
       
 79026 /*
       
 79027 ** Generate an error message when a SELECT is used within a subexpression
       
 79028 ** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
       
 79029 ** column.  We do this in a subroutine because the error occurs in multiple
       
 79030 ** places.
       
 79031 */
       
 79032 static int checkForMultiColumnSelectError(
       
 79033   Parse *pParse,       /* Parse context. */
       
 79034   SelectDest *pDest,   /* Destination of SELECT results */
       
 79035   int nExpr            /* Number of result columns returned by SELECT */
       
 79036 ){
       
 79037   int eDest = pDest->eDest;
       
 79038   if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
       
 79039     sqlite3ErrorMsg(pParse, "only a single result allowed for "
       
 79040        "a SELECT that is part of an expression");
       
 79041     return 1;
       
 79042   }else{
       
 79043     return 0;
       
 79044   }
       
 79045 }
       
 79046 
       
 79047 /*
       
 79048 ** This routine generates the code for the inside of the inner loop
       
 79049 ** of a SELECT.
       
 79050 **
       
 79051 ** If srcTab and nColumn are both zero, then the pEList expressions
       
 79052 ** are evaluated in order to get the data for this row.  If nColumn>0
       
 79053 ** then data is pulled from srcTab and pEList is used only to get the
       
 79054 ** datatypes for each column.
       
 79055 */
       
 79056 static void selectInnerLoop(
       
 79057   Parse *pParse,          /* The parser context */
       
 79058   Select *p,              /* The complete select statement being coded */
       
 79059   ExprList *pEList,       /* List of values being extracted */
       
 79060   int srcTab,             /* Pull data from this table */
       
 79061   int nColumn,            /* Number of columns in the source table */
       
 79062   ExprList *pOrderBy,     /* If not NULL, sort results using this key */
       
 79063   int distinct,           /* If >=0, make sure results are distinct */
       
 79064   SelectDest *pDest,      /* How to dispose of the results */
       
 79065   int iContinue,          /* Jump here to continue with next row */
       
 79066   int iBreak              /* Jump here to break out of the inner loop */
       
 79067 ){
       
 79068   Vdbe *v = pParse->pVdbe;
       
 79069   int i;
       
 79070   int hasDistinct;        /* True if the DISTINCT keyword is present */
       
 79071   int regResult;              /* Start of memory holding result set */
       
 79072   int eDest = pDest->eDest;   /* How to dispose of results */
       
 79073   int iParm = pDest->iParm;   /* First argument to disposal method */
       
 79074   int nResultCol;             /* Number of result columns */
       
 79075 
       
 79076   assert( v );
       
 79077   if( NEVER(v==0) ) return;
       
 79078   assert( pEList!=0 );
       
 79079   hasDistinct = distinct>=0;
       
 79080   if( pOrderBy==0 && !hasDistinct ){
       
 79081     codeOffset(v, p, iContinue);
       
 79082   }
       
 79083 
       
 79084   /* Pull the requested columns.
       
 79085   */
       
 79086   if( nColumn>0 ){
       
 79087     nResultCol = nColumn;
       
 79088   }else{
       
 79089     nResultCol = pEList->nExpr;
       
 79090   }
       
 79091   if( pDest->iMem==0 ){
       
 79092     pDest->iMem = pParse->nMem+1;
       
 79093     pDest->nMem = nResultCol;
       
 79094     pParse->nMem += nResultCol;
       
 79095   }else{ 
       
 79096     assert( pDest->nMem==nResultCol );
       
 79097   }
       
 79098   regResult = pDest->iMem;
       
 79099   if( nColumn>0 ){
       
 79100     for(i=0; i<nColumn; i++){
       
 79101       sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
       
 79102     }
       
 79103   }else if( eDest!=SRT_Exists ){
       
 79104     /* If the destination is an EXISTS(...) expression, the actual
       
 79105     ** values returned by the SELECT are not required.
       
 79106     */
       
 79107     sqlite3ExprCacheClear(pParse);
       
 79108     sqlite3ExprCodeExprList(pParse, pEList, regResult, eDest==SRT_Output);
       
 79109   }
       
 79110   nColumn = nResultCol;
       
 79111 
       
 79112   /* If the DISTINCT keyword was present on the SELECT statement
       
 79113   ** and this row has been seen before, then do not make this row
       
 79114   ** part of the result.
       
 79115   */
       
 79116   if( hasDistinct ){
       
 79117     assert( pEList!=0 );
       
 79118     assert( pEList->nExpr==nColumn );
       
 79119     codeDistinct(pParse, distinct, iContinue, nColumn, regResult);
       
 79120     if( pOrderBy==0 ){
       
 79121       codeOffset(v, p, iContinue);
       
 79122     }
       
 79123   }
       
 79124 
       
 79125   if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
       
 79126     return;
       
 79127   }
       
 79128 
       
 79129   switch( eDest ){
       
 79130     /* In this mode, write each query result to the key of the temporary
       
 79131     ** table iParm.
       
 79132     */
       
 79133 #ifndef SQLITE_OMIT_COMPOUND_SELECT
       
 79134     case SRT_Union: {
       
 79135       int r1;
       
 79136       r1 = sqlite3GetTempReg(pParse);
       
 79137       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
       
 79138       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
       
 79139       sqlite3ReleaseTempReg(pParse, r1);
       
 79140       break;
       
 79141     }
       
 79142 
       
 79143     /* Construct a record from the query result, but instead of
       
 79144     ** saving that record, use it as a key to delete elements from
       
 79145     ** the temporary table iParm.
       
 79146     */
       
 79147     case SRT_Except: {
       
 79148       sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nColumn);
       
 79149       break;
       
 79150     }
       
 79151 #endif
       
 79152 
       
 79153     /* Store the result as data using a unique key.
       
 79154     */
       
 79155     case SRT_Table:
       
 79156     case SRT_EphemTab: {
       
 79157       int r1 = sqlite3GetTempReg(pParse);
       
 79158       testcase( eDest==SRT_Table );
       
 79159       testcase( eDest==SRT_EphemTab );
       
 79160       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
       
 79161       if( pOrderBy ){
       
 79162         pushOntoSorter(pParse, pOrderBy, p, r1);
       
 79163       }else{
       
 79164         int r2 = sqlite3GetTempReg(pParse);
       
 79165         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
       
 79166         sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
       
 79167         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
       
 79168         sqlite3ReleaseTempReg(pParse, r2);
       
 79169       }
       
 79170       sqlite3ReleaseTempReg(pParse, r1);
       
 79171       break;
       
 79172     }
       
 79173 
       
 79174 #ifndef SQLITE_OMIT_SUBQUERY
       
 79175     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
       
 79176     ** then there should be a single item on the stack.  Write this
       
 79177     ** item into the set table with bogus data.
       
 79178     */
       
 79179     case SRT_Set: {
       
 79180       assert( nColumn==1 );
       
 79181       p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affinity);
       
 79182       if( pOrderBy ){
       
 79183         /* At first glance you would think we could optimize out the
       
 79184         ** ORDER BY in this case since the order of entries in the set
       
 79185         ** does not matter.  But there might be a LIMIT clause, in which
       
 79186         ** case the order does matter */
       
 79187         pushOntoSorter(pParse, pOrderBy, p, regResult);
       
 79188       }else{
       
 79189         int r1 = sqlite3GetTempReg(pParse);
       
 79190         sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, 1, r1, &p->affinity, 1);
       
 79191         sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
       
 79192         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
       
 79193         sqlite3ReleaseTempReg(pParse, r1);
       
 79194       }
       
 79195       break;
       
 79196     }
       
 79197 
       
 79198     /* If any row exist in the result set, record that fact and abort.
       
 79199     */
       
 79200     case SRT_Exists: {
       
 79201       sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
       
 79202       /* The LIMIT clause will terminate the loop for us */
       
 79203       break;
       
 79204     }
       
 79205 
       
 79206     /* If this is a scalar select that is part of an expression, then
       
 79207     ** store the results in the appropriate memory cell and break out
       
 79208     ** of the scan loop.
       
 79209     */
       
 79210     case SRT_Mem: {
       
 79211       assert( nColumn==1 );
       
 79212       if( pOrderBy ){
       
 79213         pushOntoSorter(pParse, pOrderBy, p, regResult);
       
 79214       }else{
       
 79215         sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
       
 79216         /* The LIMIT clause will jump out of the loop for us */
       
 79217       }
       
 79218       break;
       
 79219     }
       
 79220 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
       
 79221 
       
 79222     /* Send the data to the callback function or to a subroutine.  In the
       
 79223     ** case of a subroutine, the subroutine itself is responsible for
       
 79224     ** popping the data from the stack.
       
 79225     */
       
 79226     case SRT_Coroutine:
       
 79227     case SRT_Output: {
       
 79228       testcase( eDest==SRT_Coroutine );
       
 79229       testcase( eDest==SRT_Output );
       
 79230       if( pOrderBy ){
       
 79231         int r1 = sqlite3GetTempReg(pParse);
       
 79232         sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
       
 79233         pushOntoSorter(pParse, pOrderBy, p, r1);
       
 79234         sqlite3ReleaseTempReg(pParse, r1);
       
 79235       }else if( eDest==SRT_Coroutine ){
       
 79236         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
       
 79237       }else{
       
 79238         sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn);
       
 79239         sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn);
       
 79240       }
       
 79241       break;
       
 79242     }
       
 79243 
       
 79244 #if !defined(SQLITE_OMIT_TRIGGER)
       
 79245     /* Discard the results.  This is used for SELECT statements inside
       
 79246     ** the body of a TRIGGER.  The purpose of such selects is to call
       
 79247     ** user-defined functions that have side effects.  We do not care
       
 79248     ** about the actual results of the select.
       
 79249     */
       
 79250     default: {
       
 79251       assert( eDest==SRT_Discard );
       
 79252       break;
       
 79253     }
       
 79254 #endif
       
 79255   }
       
 79256 
       
 79257   /* Jump to the end of the loop if the LIMIT is reached.
       
 79258   */
       
 79259   if( p->iLimit ){
       
 79260     assert( pOrderBy==0 );  /* If there is an ORDER BY, the call to
       
 79261                             ** pushOntoSorter() would have cleared p->iLimit */
       
 79262     sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
       
 79263     sqlite3VdbeAddOp2(v, OP_IfZero, p->iLimit, iBreak);
       
 79264   }
       
 79265 }
       
 79266 
       
 79267 /*
       
 79268 ** Given an expression list, generate a KeyInfo structure that records
       
 79269 ** the collating sequence for each expression in that expression list.
       
 79270 **
       
 79271 ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
       
 79272 ** KeyInfo structure is appropriate for initializing a virtual index to
       
 79273 ** implement that clause.  If the ExprList is the result set of a SELECT
       
 79274 ** then the KeyInfo structure is appropriate for initializing a virtual
       
 79275 ** index to implement a DISTINCT test.
       
 79276 **
       
 79277 ** Space to hold the KeyInfo structure is obtain from malloc.  The calling
       
 79278 ** function is responsible for seeing that this structure is eventually
       
 79279 ** freed.  Add the KeyInfo structure to the P4 field of an opcode using
       
 79280 ** P4_KEYINFO_HANDOFF is the usual way of dealing with this.
       
 79281 */
       
 79282 static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){
       
 79283   sqlite3 *db = pParse->db;
       
 79284   int nExpr;
       
 79285   KeyInfo *pInfo;
       
 79286   struct ExprList_item *pItem;
       
 79287   int i;
       
 79288 
       
 79289   nExpr = pList->nExpr;
       
 79290   pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) );
       
 79291   if( pInfo ){
       
 79292     pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr];
       
 79293     pInfo->nField = (u16)nExpr;
       
 79294     pInfo->enc = ENC(db);
       
 79295     pInfo->db = db;
       
 79296     for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
       
 79297       CollSeq *pColl;
       
 79298       pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
       
 79299       if( !pColl ){
       
 79300         pColl = db->pDfltColl;
       
 79301       }
       
 79302       pInfo->aColl[i] = pColl;
       
 79303       pInfo->aSortOrder[i] = pItem->sortOrder;
       
 79304     }
       
 79305   }
       
 79306   return pInfo;
       
 79307 }
       
 79308 
       
 79309 
       
 79310 /*
       
 79311 ** If the inner loop was generated using a non-null pOrderBy argument,
       
 79312 ** then the results were placed in a sorter.  After the loop is terminated
       
 79313 ** we need to run the sorter and output the results.  The following
       
 79314 ** routine generates the code needed to do that.
       
 79315 */
       
 79316 static void generateSortTail(
       
 79317   Parse *pParse,    /* Parsing context */
       
 79318   Select *p,        /* The SELECT statement */
       
 79319   Vdbe *v,          /* Generate code into this VDBE */
       
 79320   int nColumn,      /* Number of columns of data */
       
 79321   SelectDest *pDest /* Write the sorted results here */
       
 79322 ){
       
 79323   int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
       
 79324   int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
       
 79325   int addr;
       
 79326   int iTab;
       
 79327   int pseudoTab = 0;
       
 79328   ExprList *pOrderBy = p->pOrderBy;
       
 79329 
       
 79330   int eDest = pDest->eDest;
       
 79331   int iParm = pDest->iParm;
       
 79332 
       
 79333   int regRow;
       
 79334   int regRowid;
       
 79335 
       
 79336   iTab = pOrderBy->iECursor;
       
 79337   regRow = sqlite3GetTempReg(pParse);
       
 79338   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
       
 79339     pseudoTab = pParse->nTab++;
       
 79340     sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
       
 79341     regRowid = 0;
       
 79342   }else{
       
 79343     regRowid = sqlite3GetTempReg(pParse);
       
 79344   }
       
 79345   addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
       
 79346   codeOffset(v, p, addrContinue);
       
 79347   sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr + 1, regRow);
       
 79348   switch( eDest ){
       
 79349     case SRT_Table:
       
 79350     case SRT_EphemTab: {
       
 79351       testcase( eDest==SRT_Table );
       
 79352       testcase( eDest==SRT_EphemTab );
       
 79353       sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
       
 79354       sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
       
 79355       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
       
 79356       break;
       
 79357     }
       
 79358 #ifndef SQLITE_OMIT_SUBQUERY
       
 79359     case SRT_Set: {
       
 79360       assert( nColumn==1 );
       
 79361       sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, &p->affinity, 1);
       
 79362       sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
       
 79363       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
       
 79364       break;
       
 79365     }
       
 79366     case SRT_Mem: {
       
 79367       assert( nColumn==1 );
       
 79368       sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
       
 79369       /* The LIMIT clause will terminate the loop for us */
       
 79370       break;
       
 79371     }
       
 79372 #endif
       
 79373     default: {
       
 79374       int i;
       
 79375       assert( eDest==SRT_Output || eDest==SRT_Coroutine ); 
       
 79376       testcase( eDest==SRT_Output );
       
 79377       testcase( eDest==SRT_Coroutine );
       
 79378       for(i=0; i<nColumn; i++){
       
 79379         assert( regRow!=pDest->iMem+i );
       
 79380         sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iMem+i);
       
 79381         if( i==0 ){
       
 79382           sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
       
 79383         }
       
 79384       }
       
 79385       if( eDest==SRT_Output ){
       
 79386         sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iMem, nColumn);
       
 79387         sqlite3ExprCacheAffinityChange(pParse, pDest->iMem, nColumn);
       
 79388       }else{
       
 79389         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
       
 79390       }
       
 79391       break;
       
 79392     }
       
 79393   }
       
 79394   sqlite3ReleaseTempReg(pParse, regRow);
       
 79395   sqlite3ReleaseTempReg(pParse, regRowid);
       
 79396 
       
 79397   /* LIMIT has been implemented by the pushOntoSorter() routine.
       
 79398   */
       
 79399   assert( p->iLimit==0 );
       
 79400 
       
 79401   /* The bottom of the loop
       
 79402   */
       
 79403   sqlite3VdbeResolveLabel(v, addrContinue);
       
 79404   sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
       
 79405   sqlite3VdbeResolveLabel(v, addrBreak);
       
 79406   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
       
 79407     sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
       
 79408   }
       
 79409 }
       
 79410 
       
 79411 /*
       
 79412 ** Return a pointer to a string containing the 'declaration type' of the
       
 79413 ** expression pExpr. The string may be treated as static by the caller.
       
 79414 **
       
 79415 ** The declaration type is the exact datatype definition extracted from the
       
 79416 ** original CREATE TABLE statement if the expression is a column. The
       
 79417 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
       
 79418 ** is considered a column can be complex in the presence of subqueries. The
       
 79419 ** result-set expression in all of the following SELECT statements is 
       
 79420 ** considered a column by this function.
       
 79421 **
       
 79422 **   SELECT col FROM tbl;
       
 79423 **   SELECT (SELECT col FROM tbl;
       
 79424 **   SELECT (SELECT col FROM tbl);
       
 79425 **   SELECT abc FROM (SELECT col AS abc FROM tbl);
       
 79426 ** 
       
 79427 ** The declaration type for any expression other than a column is NULL.
       
 79428 */
       
 79429 static const char *columnType(
       
 79430   NameContext *pNC, 
       
 79431   Expr *pExpr,
       
 79432   const char **pzOriginDb,
       
 79433   const char **pzOriginTab,
       
 79434   const char **pzOriginCol
       
 79435 ){
       
 79436   char const *zType = 0;
       
 79437   char const *zOriginDb = 0;
       
 79438   char const *zOriginTab = 0;
       
 79439   char const *zOriginCol = 0;
       
 79440   int j;
       
 79441   if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
       
 79442 
       
 79443   switch( pExpr->op ){
       
 79444     case TK_AGG_COLUMN:
       
 79445     case TK_COLUMN: {
       
 79446       /* The expression is a column. Locate the table the column is being
       
 79447       ** extracted from in NameContext.pSrcList. This table may be real
       
 79448       ** database table or a subquery.
       
 79449       */
       
 79450       Table *pTab = 0;            /* Table structure column is extracted from */
       
 79451       Select *pS = 0;             /* Select the column is extracted from */
       
 79452       int iCol = pExpr->iColumn;  /* Index of column in pTab */
       
 79453       testcase( pExpr->op==TK_AGG_COLUMN );
       
 79454       testcase( pExpr->op==TK_COLUMN );
       
 79455       while( pNC && !pTab ){
       
 79456         SrcList *pTabList = pNC->pSrcList;
       
 79457         for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
       
 79458         if( j<pTabList->nSrc ){
       
 79459           pTab = pTabList->a[j].pTab;
       
 79460           pS = pTabList->a[j].pSelect;
       
 79461         }else{
       
 79462           pNC = pNC->pNext;
       
 79463         }
       
 79464       }
       
 79465 
       
 79466       if( pTab==0 ){
       
 79467         /* At one time, code such as "SELECT new.x" within a trigger would
       
 79468         ** cause this condition to run.  Since then, we have restructured how
       
 79469         ** trigger code is generated and so this condition is no longer 
       
 79470         ** possible. However, it can still be true for statements like
       
 79471         ** the following:
       
 79472         **
       
 79473         **   CREATE TABLE t1(col INTEGER);
       
 79474         **   SELECT (SELECT t1.col) FROM FROM t1;
       
 79475         **
       
 79476         ** when columnType() is called on the expression "t1.col" in the 
       
 79477         ** sub-select. In this case, set the column type to NULL, even
       
 79478         ** though it should really be "INTEGER".
       
 79479         **
       
 79480         ** This is not a problem, as the column type of "t1.col" is never
       
 79481         ** used. When columnType() is called on the expression 
       
 79482         ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
       
 79483         ** branch below.  */
       
 79484         break;
       
 79485       }
       
 79486 
       
 79487       assert( pTab && pExpr->pTab==pTab );
       
 79488       if( pS ){
       
 79489         /* The "table" is actually a sub-select or a view in the FROM clause
       
 79490         ** of the SELECT statement. Return the declaration type and origin
       
 79491         ** data for the result-set column of the sub-select.
       
 79492         */
       
 79493         if( ALWAYS(iCol>=0 && iCol<pS->pEList->nExpr) ){
       
 79494           /* If iCol is less than zero, then the expression requests the
       
 79495           ** rowid of the sub-select or view. This expression is legal (see 
       
 79496           ** test case misc2.2.2) - it always evaluates to NULL.
       
 79497           */
       
 79498           NameContext sNC;
       
 79499           Expr *p = pS->pEList->a[iCol].pExpr;
       
 79500           sNC.pSrcList = pS->pSrc;
       
 79501           sNC.pNext = pNC;
       
 79502           sNC.pParse = pNC->pParse;
       
 79503           zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 
       
 79504         }
       
 79505       }else if( ALWAYS(pTab->pSchema) ){
       
 79506         /* A real table */
       
 79507         assert( !pS );
       
 79508         if( iCol<0 ) iCol = pTab->iPKey;
       
 79509         assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
       
 79510         if( iCol<0 ){
       
 79511           zType = "INTEGER";
       
 79512           zOriginCol = "rowid";
       
 79513         }else{
       
 79514           zType = pTab->aCol[iCol].zType;
       
 79515           zOriginCol = pTab->aCol[iCol].zName;
       
 79516         }
       
 79517         zOriginTab = pTab->zName;
       
 79518         if( pNC->pParse ){
       
 79519           int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
       
 79520           zOriginDb = pNC->pParse->db->aDb[iDb].zName;
       
 79521         }
       
 79522       }
       
 79523       break;
       
 79524     }
       
 79525 #ifndef SQLITE_OMIT_SUBQUERY
       
 79526     case TK_SELECT: {
       
 79527       /* The expression is a sub-select. Return the declaration type and
       
 79528       ** origin info for the single column in the result set of the SELECT
       
 79529       ** statement.
       
 79530       */
       
 79531       NameContext sNC;
       
 79532       Select *pS = pExpr->x.pSelect;
       
 79533       Expr *p = pS->pEList->a[0].pExpr;
       
 79534       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
       
 79535       sNC.pSrcList = pS->pSrc;
       
 79536       sNC.pNext = pNC;
       
 79537       sNC.pParse = pNC->pParse;
       
 79538       zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 
       
 79539       break;
       
 79540     }
       
 79541 #endif
       
 79542   }
       
 79543   
       
 79544   if( pzOriginDb ){
       
 79545     assert( pzOriginTab && pzOriginCol );
       
 79546     *pzOriginDb = zOriginDb;
       
 79547     *pzOriginTab = zOriginTab;
       
 79548     *pzOriginCol = zOriginCol;
       
 79549   }
       
 79550   return zType;
       
 79551 }
       
 79552 
       
 79553 /*
       
 79554 ** Generate code that will tell the VDBE the declaration types of columns
       
 79555 ** in the result set.
       
 79556 */
       
 79557 static void generateColumnTypes(
       
 79558   Parse *pParse,      /* Parser context */
       
 79559   SrcList *pTabList,  /* List of tables */
       
 79560   ExprList *pEList    /* Expressions defining the result set */
       
 79561 ){
       
 79562 #ifndef SQLITE_OMIT_DECLTYPE
       
 79563   Vdbe *v = pParse->pVdbe;
       
 79564   int i;
       
 79565   NameContext sNC;
       
 79566   sNC.pSrcList = pTabList;
       
 79567   sNC.pParse = pParse;
       
 79568   for(i=0; i<pEList->nExpr; i++){
       
 79569     Expr *p = pEList->a[i].pExpr;
       
 79570     const char *zType;
       
 79571 #ifdef SQLITE_ENABLE_COLUMN_METADATA
       
 79572     const char *zOrigDb = 0;
       
 79573     const char *zOrigTab = 0;
       
 79574     const char *zOrigCol = 0;
       
 79575     zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
       
 79576 
       
 79577     /* The vdbe must make its own copy of the column-type and other 
       
 79578     ** column specific strings, in case the schema is reset before this
       
 79579     ** virtual machine is deleted.
       
 79580     */
       
 79581     sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
       
 79582     sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
       
 79583     sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
       
 79584 #else
       
 79585     zType = columnType(&sNC, p, 0, 0, 0);
       
 79586 #endif
       
 79587     sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
       
 79588   }
       
 79589 #endif /* SQLITE_OMIT_DECLTYPE */
       
 79590 }
       
 79591 
       
 79592 /*
       
 79593 ** Generate code that will tell the VDBE the names of columns
       
 79594 ** in the result set.  This information is used to provide the
       
 79595 ** azCol[] values in the callback.
       
 79596 */
       
 79597 static void generateColumnNames(
       
 79598   Parse *pParse,      /* Parser context */
       
 79599   SrcList *pTabList,  /* List of tables */
       
 79600   ExprList *pEList    /* Expressions defining the result set */
       
 79601 ){
       
 79602   Vdbe *v = pParse->pVdbe;
       
 79603   int i, j;
       
 79604   sqlite3 *db = pParse->db;
       
 79605   int fullNames, shortNames;
       
 79606 
       
 79607 #ifndef SQLITE_OMIT_EXPLAIN
       
 79608   /* If this is an EXPLAIN, skip this step */
       
 79609   if( pParse->explain ){
       
 79610     return;
       
 79611   }
       
 79612 #endif
       
 79613 
       
 79614   if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
       
 79615   pParse->colNamesSet = 1;
       
 79616   fullNames = (db->flags & SQLITE_FullColNames)!=0;
       
 79617   shortNames = (db->flags & SQLITE_ShortColNames)!=0;
       
 79618   sqlite3VdbeSetNumCols(v, pEList->nExpr);
       
 79619   for(i=0; i<pEList->nExpr; i++){
       
 79620     Expr *p;
       
 79621     p = pEList->a[i].pExpr;
       
 79622     if( NEVER(p==0) ) continue;
       
 79623     if( pEList->a[i].zName ){
       
 79624       char *zName = pEList->a[i].zName;
       
 79625       sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
       
 79626     }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){
       
 79627       Table *pTab;
       
 79628       char *zCol;
       
 79629       int iCol = p->iColumn;
       
 79630       for(j=0; ALWAYS(j<pTabList->nSrc); j++){
       
 79631         if( pTabList->a[j].iCursor==p->iTable ) break;
       
 79632       }
       
 79633       assert( j<pTabList->nSrc );
       
 79634       pTab = pTabList->a[j].pTab;
       
 79635       if( iCol<0 ) iCol = pTab->iPKey;
       
 79636       assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
       
 79637       if( iCol<0 ){
       
 79638         zCol = "rowid";
       
 79639       }else{
       
 79640         zCol = pTab->aCol[iCol].zName;
       
 79641       }
       
 79642       if( !shortNames && !fullNames ){
       
 79643         sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
       
 79644             sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
       
 79645       }else if( fullNames ){
       
 79646         char *zName = 0;
       
 79647         zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
       
 79648         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
       
 79649       }else{
       
 79650         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
       
 79651       }
       
 79652     }else{
       
 79653       sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
       
 79654           sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
       
 79655     }
       
 79656   }
       
 79657   generateColumnTypes(pParse, pTabList, pEList);
       
 79658 }
       
 79659 
       
 79660 #ifndef SQLITE_OMIT_COMPOUND_SELECT
       
 79661 /*
       
 79662 ** Name of the connection operator, used for error messages.
       
 79663 */
       
 79664 static const char *selectOpName(int id){
       
 79665   char *z;
       
 79666   switch( id ){
       
 79667     case TK_ALL:       z = "UNION ALL";   break;
       
 79668     case TK_INTERSECT: z = "INTERSECT";   break;
       
 79669     case TK_EXCEPT:    z = "EXCEPT";      break;
       
 79670     default:           z = "UNION";       break;
       
 79671   }
       
 79672   return z;
       
 79673 }
       
 79674 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
       
 79675 
       
 79676 /*
       
 79677 ** Given a an expression list (which is really the list of expressions
       
 79678 ** that form the result set of a SELECT statement) compute appropriate
       
 79679 ** column names for a table that would hold the expression list.
       
 79680 **
       
 79681 ** All column names will be unique.
       
 79682 **
       
 79683 ** Only the column names are computed.  Column.zType, Column.zColl,
       
 79684 ** and other fields of Column are zeroed.
       
 79685 **
       
 79686 ** Return SQLITE_OK on success.  If a memory allocation error occurs,
       
 79687 ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
       
 79688 */
       
 79689 static int selectColumnsFromExprList(
       
 79690   Parse *pParse,          /* Parsing context */
       
 79691   ExprList *pEList,       /* Expr list from which to derive column names */
       
 79692   int *pnCol,             /* Write the number of columns here */
       
 79693   Column **paCol          /* Write the new column list here */
       
 79694 ){
       
 79695   sqlite3 *db = pParse->db;   /* Database connection */
       
 79696   int i, j;                   /* Loop counters */
       
 79697   int cnt;                    /* Index added to make the name unique */
       
 79698   Column *aCol, *pCol;        /* For looping over result columns */
       
 79699   int nCol;                   /* Number of columns in the result set */
       
 79700   Expr *p;                    /* Expression for a single result column */
       
 79701   char *zName;                /* Column name */
       
 79702   int nName;                  /* Size of name in zName[] */
       
 79703 
       
 79704   *pnCol = nCol = pEList->nExpr;
       
 79705   aCol = *paCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
       
 79706   if( aCol==0 ) return SQLITE_NOMEM;
       
 79707   for(i=0, pCol=aCol; i<nCol; i++, pCol++){
       
 79708     /* Get an appropriate name for the column
       
 79709     */
       
 79710     p = pEList->a[i].pExpr;
       
 79711     assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
       
 79712                || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
       
 79713     if( (zName = pEList->a[i].zName)!=0 ){
       
 79714       /* If the column contains an "AS <name>" phrase, use <name> as the name */
       
 79715       zName = sqlite3DbStrDup(db, zName);
       
 79716     }else{
       
 79717       Expr *pColExpr = p;  /* The expression that is the result column name */
       
 79718       Table *pTab;         /* Table associated with this expression */
       
 79719       while( pColExpr->op==TK_DOT ) pColExpr = pColExpr->pRight;
       
 79720       if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
       
 79721         /* For columns use the column name name */
       
 79722         int iCol = pColExpr->iColumn;
       
 79723         pTab = pColExpr->pTab;
       
 79724         if( iCol<0 ) iCol = pTab->iPKey;
       
 79725         zName = sqlite3MPrintf(db, "%s",
       
 79726                  iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
       
 79727       }else if( pColExpr->op==TK_ID ){
       
 79728         assert( !ExprHasProperty(pColExpr, EP_IntValue) );
       
 79729         zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
       
 79730       }else{
       
 79731         /* Use the original text of the column expression as its name */
       
 79732         zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
       
 79733       }
       
 79734     }
       
 79735     if( db->mallocFailed ){
       
 79736       sqlite3DbFree(db, zName);
       
 79737       break;
       
 79738     }
       
 79739 
       
 79740     /* Make sure the column name is unique.  If the name is not unique,
       
 79741     ** append a integer to the name so that it becomes unique.
       
 79742     */
       
 79743     nName = sqlite3Strlen30(zName);
       
 79744     for(j=cnt=0; j<i; j++){
       
 79745       if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
       
 79746         char *zNewName;
       
 79747         zName[nName] = 0;
       
 79748         zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
       
 79749         sqlite3DbFree(db, zName);
       
 79750         zName = zNewName;
       
 79751         j = -1;
       
 79752         if( zName==0 ) break;
       
 79753       }
       
 79754     }
       
 79755     pCol->zName = zName;
       
 79756   }
       
 79757   if( db->mallocFailed ){
       
 79758     for(j=0; j<i; j++){
       
 79759       sqlite3DbFree(db, aCol[j].zName);
       
 79760     }
       
 79761     sqlite3DbFree(db, aCol);
       
 79762     *paCol = 0;
       
 79763     *pnCol = 0;
       
 79764     return SQLITE_NOMEM;
       
 79765   }
       
 79766   return SQLITE_OK;
       
 79767 }
       
 79768 
       
 79769 /*
       
 79770 ** Add type and collation information to a column list based on
       
 79771 ** a SELECT statement.
       
 79772 ** 
       
 79773 ** The column list presumably came from selectColumnNamesFromExprList().
       
 79774 ** The column list has only names, not types or collations.  This
       
 79775 ** routine goes through and adds the types and collations.
       
 79776 **
       
 79777 ** This routine requires that all identifiers in the SELECT
       
 79778 ** statement be resolved.
       
 79779 */
       
 79780 static void selectAddColumnTypeAndCollation(
       
 79781   Parse *pParse,        /* Parsing contexts */
       
 79782   int nCol,             /* Number of columns */
       
 79783   Column *aCol,         /* List of columns */
       
 79784   Select *pSelect       /* SELECT used to determine types and collations */
       
 79785 ){
       
 79786   sqlite3 *db = pParse->db;
       
 79787   NameContext sNC;
       
 79788   Column *pCol;
       
 79789   CollSeq *pColl;
       
 79790   int i;
       
 79791   Expr *p;
       
 79792   struct ExprList_item *a;
       
 79793 
       
 79794   assert( pSelect!=0 );
       
 79795   assert( (pSelect->selFlags & SF_Resolved)!=0 );
       
 79796   assert( nCol==pSelect->pEList->nExpr || db->mallocFailed );
       
 79797   if( db->mallocFailed ) return;
       
 79798   memset(&sNC, 0, sizeof(sNC));
       
 79799   sNC.pSrcList = pSelect->pSrc;
       
 79800   a = pSelect->pEList->a;
       
 79801   for(i=0, pCol=aCol; i<nCol; i++, pCol++){
       
 79802     p = a[i].pExpr;
       
 79803     pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
       
 79804     pCol->affinity = sqlite3ExprAffinity(p);
       
 79805     if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
       
 79806     pColl = sqlite3ExprCollSeq(pParse, p);
       
 79807     if( pColl ){
       
 79808       pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
       
 79809     }
       
 79810   }
       
 79811 }
       
 79812 
       
 79813 /*
       
 79814 ** Given a SELECT statement, generate a Table structure that describes
       
 79815 ** the result set of that SELECT.
       
 79816 */
       
 79817 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
       
 79818   Table *pTab;
       
 79819   sqlite3 *db = pParse->db;
       
 79820   int savedFlags;
       
 79821 
       
 79822   savedFlags = db->flags;
       
 79823   db->flags &= ~SQLITE_FullColNames;
       
 79824   db->flags |= SQLITE_ShortColNames;
       
 79825   sqlite3SelectPrep(pParse, pSelect, 0);
       
 79826   if( pParse->nErr ) return 0;
       
 79827   while( pSelect->pPrior ) pSelect = pSelect->pPrior;
       
 79828   db->flags = savedFlags;
       
 79829   pTab = sqlite3DbMallocZero(db, sizeof(Table) );
       
 79830   if( pTab==0 ){
       
 79831     return 0;
       
 79832   }
       
 79833   /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
       
 79834   ** is disabled, so we might as well hard-code pTab->dbMem to NULL. */
       
 79835   assert( db->lookaside.bEnabled==0 );
       
 79836   pTab->dbMem = 0;
       
 79837   pTab->nRef = 1;
       
 79838   pTab->zName = 0;
       
 79839   selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
       
 79840   selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSelect);
       
 79841   pTab->iPKey = -1;
       
 79842   if( db->mallocFailed ){
       
 79843     sqlite3DeleteTable(pTab);
       
 79844     return 0;
       
 79845   }
       
 79846   return pTab;
       
 79847 }
       
 79848 
       
 79849 /*
       
 79850 ** Get a VDBE for the given parser context.  Create a new one if necessary.
       
 79851 ** If an error occurs, return NULL and leave a message in pParse.
       
 79852 */
       
 79853 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
       
 79854   Vdbe *v = pParse->pVdbe;
       
 79855   if( v==0 ){
       
 79856     v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);
       
 79857 #ifndef SQLITE_OMIT_TRACE
       
 79858     if( v ){
       
 79859       sqlite3VdbeAddOp0(v, OP_Trace);
       
 79860     }
       
 79861 #endif
       
 79862   }
       
 79863   return v;
       
 79864 }
       
 79865 
       
 79866 
       
 79867 /*
       
 79868 ** Compute the iLimit and iOffset fields of the SELECT based on the
       
 79869 ** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
       
 79870 ** that appear in the original SQL statement after the LIMIT and OFFSET
       
 79871 ** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset 
       
 79872 ** are the integer memory register numbers for counters used to compute 
       
 79873 ** the limit and offset.  If there is no limit and/or offset, then 
       
 79874 ** iLimit and iOffset are negative.
       
 79875 **
       
 79876 ** This routine changes the values of iLimit and iOffset only if
       
 79877 ** a limit or offset is defined by pLimit and pOffset.  iLimit and
       
 79878 ** iOffset should have been preset to appropriate default values
       
 79879 ** (usually but not always -1) prior to calling this routine.
       
 79880 ** Only if pLimit!=0 or pOffset!=0 do the limit registers get
       
 79881 ** redefined.  The UNION ALL operator uses this property to force
       
 79882 ** the reuse of the same limit and offset registers across multiple
       
 79883 ** SELECT statements.
       
 79884 */
       
 79885 static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
       
 79886   Vdbe *v = 0;
       
 79887   int iLimit = 0;
       
 79888   int iOffset;
       
 79889   int addr1;
       
 79890   if( p->iLimit ) return;
       
 79891 
       
 79892   /* 
       
 79893   ** "LIMIT -1" always shows all rows.  There is some
       
 79894   ** contraversy about what the correct behavior should be.
       
 79895   ** The current implementation interprets "LIMIT 0" to mean
       
 79896   ** no rows.
       
 79897   */
       
 79898   sqlite3ExprCacheClear(pParse);
       
 79899   assert( p->pOffset==0 || p->pLimit!=0 );
       
 79900   if( p->pLimit ){
       
 79901     p->iLimit = iLimit = ++pParse->nMem;
       
 79902     v = sqlite3GetVdbe(pParse);
       
 79903     if( NEVER(v==0) ) return;  /* VDBE should have already been allocated */
       
 79904     sqlite3ExprCode(pParse, p->pLimit, iLimit);
       
 79905     sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit);
       
 79906     VdbeComment((v, "LIMIT counter"));
       
 79907     sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak);
       
 79908     if( p->pOffset ){
       
 79909       p->iOffset = iOffset = ++pParse->nMem;
       
 79910       pParse->nMem++;   /* Allocate an extra register for limit+offset */
       
 79911       sqlite3ExprCode(pParse, p->pOffset, iOffset);
       
 79912       sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset);
       
 79913       VdbeComment((v, "OFFSET counter"));
       
 79914       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset);
       
 79915       sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
       
 79916       sqlite3VdbeJumpHere(v, addr1);
       
 79917       sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
       
 79918       VdbeComment((v, "LIMIT+OFFSET"));
       
 79919       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit);
       
 79920       sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
       
 79921       sqlite3VdbeJumpHere(v, addr1);
       
 79922     }
       
 79923   }
       
 79924 }
       
 79925 
       
 79926 #ifndef SQLITE_OMIT_COMPOUND_SELECT
       
 79927 /*
       
 79928 ** Return the appropriate collating sequence for the iCol-th column of
       
 79929 ** the result set for the compound-select statement "p".  Return NULL if
       
 79930 ** the column has no default collating sequence.
       
 79931 **
       
 79932 ** The collating sequence for the compound select is taken from the
       
 79933 ** left-most term of the select that has a collating sequence.
       
 79934 */
       
 79935 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
       
 79936   CollSeq *pRet;
       
 79937   if( p->pPrior ){
       
 79938     pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
       
 79939   }else{
       
 79940     pRet = 0;
       
 79941   }
       
 79942   assert( iCol>=0 );
       
 79943   if( pRet==0 && iCol<p->pEList->nExpr ){
       
 79944     pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
       
 79945   }
       
 79946   return pRet;
       
 79947 }
       
 79948 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
       
 79949 
       
 79950 /* Forward reference */
       
 79951 static int multiSelectOrderBy(
       
 79952   Parse *pParse,        /* Parsing context */
       
 79953   Select *p,            /* The right-most of SELECTs to be coded */
       
 79954   SelectDest *pDest     /* What to do with query results */
       
 79955 );
       
 79956 
       
 79957 
       
 79958 #ifndef SQLITE_OMIT_COMPOUND_SELECT
       
 79959 /*
       
 79960 ** This routine is called to process a compound query form from
       
 79961 ** two or more separate queries using UNION, UNION ALL, EXCEPT, or
       
 79962 ** INTERSECT
       
 79963 **
       
 79964 ** "p" points to the right-most of the two queries.  the query on the
       
 79965 ** left is p->pPrior.  The left query could also be a compound query
       
 79966 ** in which case this routine will be called recursively. 
       
 79967 **
       
 79968 ** The results of the total query are to be written into a destination
       
 79969 ** of type eDest with parameter iParm.
       
 79970 **
       
 79971 ** Example 1:  Consider a three-way compound SQL statement.
       
 79972 **
       
 79973 **     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
       
 79974 **
       
 79975 ** This statement is parsed up as follows:
       
 79976 **
       
 79977 **     SELECT c FROM t3
       
 79978 **      |
       
 79979 **      `----->  SELECT b FROM t2
       
 79980 **                |
       
 79981 **                `------>  SELECT a FROM t1
       
 79982 **
       
 79983 ** The arrows in the diagram above represent the Select.pPrior pointer.
       
 79984 ** So if this routine is called with p equal to the t3 query, then
       
 79985 ** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
       
 79986 **
       
 79987 ** Notice that because of the way SQLite parses compound SELECTs, the
       
 79988 ** individual selects always group from left to right.
       
 79989 */
       
 79990 static int multiSelect(
       
 79991   Parse *pParse,        /* Parsing context */
       
 79992   Select *p,            /* The right-most of SELECTs to be coded */
       
 79993   SelectDest *pDest     /* What to do with query results */
       
 79994 ){
       
 79995   int rc = SQLITE_OK;   /* Success code from a subroutine */
       
 79996   Select *pPrior;       /* Another SELECT immediately to our left */
       
 79997   Vdbe *v;              /* Generate code to this VDBE */
       
 79998   SelectDest dest;      /* Alternative data destination */
       
 79999   Select *pDelete = 0;  /* Chain of simple selects to delete */
       
 80000   sqlite3 *db;          /* Database connection */
       
 80001 
       
 80002   /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
       
 80003   ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
       
 80004   */
       
 80005   assert( p && p->pPrior );  /* Calling function guarantees this much */
       
 80006   db = pParse->db;
       
 80007   pPrior = p->pPrior;
       
 80008   assert( pPrior->pRightmost!=pPrior );
       
 80009   assert( pPrior->pRightmost==p->pRightmost );
       
 80010   dest = *pDest;
       
 80011   if( pPrior->pOrderBy ){
       
 80012     sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
       
 80013       selectOpName(p->op));
       
 80014     rc = 1;
       
 80015     goto multi_select_end;
       
 80016   }
       
 80017   if( pPrior->pLimit ){
       
 80018     sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
       
 80019       selectOpName(p->op));
       
 80020     rc = 1;
       
 80021     goto multi_select_end;
       
 80022   }
       
 80023 
       
 80024   v = sqlite3GetVdbe(pParse);
       
 80025   assert( v!=0 );  /* The VDBE already created by calling function */
       
 80026 
       
 80027   /* Create the destination temporary table if necessary
       
 80028   */
       
 80029   if( dest.eDest==SRT_EphemTab ){
       
 80030     assert( p->pEList );
       
 80031     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iParm, p->pEList->nExpr);
       
 80032     dest.eDest = SRT_Table;
       
 80033   }
       
 80034 
       
 80035   /* Make sure all SELECTs in the statement have the same number of elements
       
 80036   ** in their result sets.
       
 80037   */
       
 80038   assert( p->pEList && pPrior->pEList );
       
 80039   if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
       
 80040     sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
       
 80041       " do not have the same number of result columns", selectOpName(p->op));
       
 80042     rc = 1;
       
 80043     goto multi_select_end;
       
 80044   }
       
 80045 
       
 80046   /* Compound SELECTs that have an ORDER BY clause are handled separately.
       
 80047   */
       
 80048   if( p->pOrderBy ){
       
 80049     return multiSelectOrderBy(pParse, p, pDest);
       
 80050   }
       
 80051 
       
 80052   /* Generate code for the left and right SELECT statements.
       
 80053   */
       
 80054   switch( p->op ){
       
 80055     case TK_ALL: {
       
 80056       int addr = 0;
       
 80057       assert( !pPrior->pLimit );
       
 80058       pPrior->pLimit = p->pLimit;
       
 80059       pPrior->pOffset = p->pOffset;
       
 80060       rc = sqlite3Select(pParse, pPrior, &dest);
       
 80061       p->pLimit = 0;
       
 80062       p->pOffset = 0;
       
 80063       if( rc ){
       
 80064         goto multi_select_end;
       
 80065       }
       
 80066       p->pPrior = 0;
       
 80067       p->iLimit = pPrior->iLimit;
       
 80068       p->iOffset = pPrior->iOffset;
       
 80069       if( p->iLimit ){
       
 80070         addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit);
       
 80071         VdbeComment((v, "Jump ahead if LIMIT reached"));
       
 80072       }
       
 80073       rc = sqlite3Select(pParse, p, &dest);
       
 80074       testcase( rc!=SQLITE_OK );
       
 80075       pDelete = p->pPrior;
       
 80076       p->pPrior = pPrior;
       
 80077       if( addr ){
       
 80078         sqlite3VdbeJumpHere(v, addr);
       
 80079       }
       
 80080       break;
       
 80081     }
       
 80082     case TK_EXCEPT:
       
 80083     case TK_UNION: {
       
 80084       int unionTab;    /* Cursor number of the temporary table holding result */
       
 80085       u8 op = 0;       /* One of the SRT_ operations to apply to self */
       
 80086       int priorOp;     /* The SRT_ operation to apply to prior selects */
       
 80087       Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
       
 80088       int addr;
       
 80089       SelectDest uniondest;
       
 80090 
       
 80091       testcase( p->op==TK_EXCEPT );
       
 80092       testcase( p->op==TK_UNION );
       
 80093       priorOp = SRT_Union;
       
 80094       if( dest.eDest==priorOp && ALWAYS(!p->pLimit &&!p->pOffset) ){
       
 80095         /* We can reuse a temporary table generated by a SELECT to our
       
 80096         ** right.
       
 80097         */
       
 80098         assert( p->pRightmost!=p );  /* Can only happen for leftward elements
       
 80099                                      ** of a 3-way or more compound */
       
 80100         assert( p->pLimit==0 );      /* Not allowed on leftward elements */
       
 80101         assert( p->pOffset==0 );     /* Not allowed on leftward elements */
       
 80102         unionTab = dest.iParm;
       
 80103       }else{
       
 80104         /* We will need to create our own temporary table to hold the
       
 80105         ** intermediate results.
       
 80106         */
       
 80107         unionTab = pParse->nTab++;
       
 80108         assert( p->pOrderBy==0 );
       
 80109         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
       
 80110         assert( p->addrOpenEphm[0] == -1 );
       
 80111         p->addrOpenEphm[0] = addr;
       
 80112         p->pRightmost->selFlags |= SF_UsesEphemeral;
       
 80113         assert( p->pEList );
       
 80114       }
       
 80115 
       
 80116       /* Code the SELECT statements to our left
       
 80117       */
       
 80118       assert( !pPrior->pOrderBy );
       
 80119       sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
       
 80120       rc = sqlite3Select(pParse, pPrior, &uniondest);
       
 80121       if( rc ){
       
 80122         goto multi_select_end;
       
 80123       }
       
 80124 
       
 80125       /* Code the current SELECT statement
       
 80126       */
       
 80127       if( p->op==TK_EXCEPT ){
       
 80128         op = SRT_Except;
       
 80129       }else{
       
 80130         assert( p->op==TK_UNION );
       
 80131         op = SRT_Union;
       
 80132       }
       
 80133       p->pPrior = 0;
       
 80134       pLimit = p->pLimit;
       
 80135       p->pLimit = 0;
       
 80136       pOffset = p->pOffset;
       
 80137       p->pOffset = 0;
       
 80138       uniondest.eDest = op;
       
 80139       rc = sqlite3Select(pParse, p, &uniondest);
       
 80140       testcase( rc!=SQLITE_OK );
       
 80141       /* Query flattening in sqlite3Select() might refill p->pOrderBy.
       
 80142       ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
       
 80143       sqlite3ExprListDelete(db, p->pOrderBy);
       
 80144       pDelete = p->pPrior;
       
 80145       p->pPrior = pPrior;
       
 80146       p->pOrderBy = 0;
       
 80147       sqlite3ExprDelete(db, p->pLimit);
       
 80148       p->pLimit = pLimit;
       
 80149       p->pOffset = pOffset;
       
 80150       p->iLimit = 0;
       
 80151       p->iOffset = 0;
       
 80152 
       
 80153       /* Convert the data in the temporary table into whatever form
       
 80154       ** it is that we currently need.
       
 80155       */
       
 80156       assert( unionTab==dest.iParm || dest.eDest!=priorOp );
       
 80157       if( dest.eDest!=priorOp ){
       
 80158         int iCont, iBreak, iStart;
       
 80159         assert( p->pEList );
       
 80160         if( dest.eDest==SRT_Output ){
       
 80161           Select *pFirst = p;
       
 80162           while( pFirst->pPrior ) pFirst = pFirst->pPrior;
       
 80163           generateColumnNames(pParse, 0, pFirst->pEList);
       
 80164         }
       
 80165         iBreak = sqlite3VdbeMakeLabel(v);
       
 80166         iCont = sqlite3VdbeMakeLabel(v);
       
 80167         computeLimitRegisters(pParse, p, iBreak);
       
 80168         sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
       
 80169         iStart = sqlite3VdbeCurrentAddr(v);
       
 80170         selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
       
 80171                         0, -1, &dest, iCont, iBreak);
       
 80172         sqlite3VdbeResolveLabel(v, iCont);
       
 80173         sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
       
 80174         sqlite3VdbeResolveLabel(v, iBreak);
       
 80175         sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
       
 80176       }
       
 80177       break;
       
 80178     }
       
 80179     default: assert( p->op==TK_INTERSECT ); {
       
 80180       int tab1, tab2;
       
 80181       int iCont, iBreak, iStart;
       
 80182       Expr *pLimit, *pOffset;
       
 80183       int addr;
       
 80184       SelectDest intersectdest;
       
 80185       int r1;
       
 80186 
       
 80187       /* INTERSECT is different from the others since it requires
       
 80188       ** two temporary tables.  Hence it has its own case.  Begin
       
 80189       ** by allocating the tables we will need.
       
 80190       */
       
 80191       tab1 = pParse->nTab++;
       
 80192       tab2 = pParse->nTab++;
       
 80193       assert( p->pOrderBy==0 );
       
 80194 
       
 80195       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
       
 80196       assert( p->addrOpenEphm[0] == -1 );
       
 80197       p->addrOpenEphm[0] = addr;
       
 80198       p->pRightmost->selFlags |= SF_UsesEphemeral;
       
 80199       assert( p->pEList );
       
 80200 
       
 80201       /* Code the SELECTs to our left into temporary table "tab1".
       
 80202       */
       
 80203       sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
       
 80204       rc = sqlite3Select(pParse, pPrior, &intersectdest);
       
 80205       if( rc ){
       
 80206         goto multi_select_end;
       
 80207       }
       
 80208 
       
 80209       /* Code the current SELECT into temporary table "tab2"
       
 80210       */
       
 80211       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
       
 80212       assert( p->addrOpenEphm[1] == -1 );
       
 80213       p->addrOpenEphm[1] = addr;
       
 80214       p->pPrior = 0;
       
 80215       pLimit = p->pLimit;
       
 80216       p->pLimit = 0;
       
 80217       pOffset = p->pOffset;
       
 80218       p->pOffset = 0;
       
 80219       intersectdest.iParm = tab2;
       
 80220       rc = sqlite3Select(pParse, p, &intersectdest);
       
 80221       testcase( rc!=SQLITE_OK );
       
 80222       pDelete = p->pPrior;
       
 80223       p->pPrior = pPrior;
       
 80224       sqlite3ExprDelete(db, p->pLimit);
       
 80225       p->pLimit = pLimit;
       
 80226       p->pOffset = pOffset;
       
 80227 
       
 80228       /* Generate code to take the intersection of the two temporary
       
 80229       ** tables.
       
 80230       */
       
 80231       assert( p->pEList );
       
 80232       if( dest.eDest==SRT_Output ){
       
 80233         Select *pFirst = p;
       
 80234         while( pFirst->pPrior ) pFirst = pFirst->pPrior;
       
 80235         generateColumnNames(pParse, 0, pFirst->pEList);
       
 80236       }
       
 80237       iBreak = sqlite3VdbeMakeLabel(v);
       
 80238       iCont = sqlite3VdbeMakeLabel(v);
       
 80239       computeLimitRegisters(pParse, p, iBreak);
       
 80240       sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak);
       
 80241       r1 = sqlite3GetTempReg(pParse);
       
 80242       iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
       
 80243       sqlite3VdbeAddOp3(v, OP_NotFound, tab2, iCont, r1);
       
 80244       sqlite3ReleaseTempReg(pParse, r1);
       
 80245       selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
       
 80246                       0, -1, &dest, iCont, iBreak);
       
 80247       sqlite3VdbeResolveLabel(v, iCont);
       
 80248       sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
       
 80249       sqlite3VdbeResolveLabel(v, iBreak);
       
 80250       sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
       
 80251       sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
       
 80252       break;
       
 80253     }
       
 80254   }
       
 80255 
       
 80256   /* Compute collating sequences used by 
       
 80257   ** temporary tables needed to implement the compound select.
       
 80258   ** Attach the KeyInfo structure to all temporary tables.
       
 80259   **
       
 80260   ** This section is run by the right-most SELECT statement only.
       
 80261   ** SELECT statements to the left always skip this part.  The right-most
       
 80262   ** SELECT might also skip this part if it has no ORDER BY clause and
       
 80263   ** no temp tables are required.
       
 80264   */
       
 80265   if( p->selFlags & SF_UsesEphemeral ){
       
 80266     int i;                        /* Loop counter */
       
 80267     KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
       
 80268     Select *pLoop;                /* For looping through SELECT statements */
       
 80269     CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
       
 80270     int nCol;                     /* Number of columns in result set */
       
 80271 
       
 80272     assert( p->pRightmost==p );
       
 80273     nCol = p->pEList->nExpr;
       
 80274     pKeyInfo = sqlite3DbMallocZero(db,
       
 80275                        sizeof(*pKeyInfo)+nCol*(sizeof(CollSeq*) + 1));
       
 80276     if( !pKeyInfo ){
       
 80277       rc = SQLITE_NOMEM;
       
 80278       goto multi_select_end;
       
 80279     }
       
 80280 
       
 80281     pKeyInfo->enc = ENC(db);
       
 80282     pKeyInfo->nField = (u16)nCol;
       
 80283 
       
 80284     for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
       
 80285       *apColl = multiSelectCollSeq(pParse, p, i);
       
 80286       if( 0==*apColl ){
       
 80287         *apColl = db->pDfltColl;
       
 80288       }
       
 80289     }
       
 80290 
       
 80291     for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
       
 80292       for(i=0; i<2; i++){
       
 80293         int addr = pLoop->addrOpenEphm[i];
       
 80294         if( addr<0 ){
       
 80295           /* If [0] is unused then [1] is also unused.  So we can
       
 80296           ** always safely abort as soon as the first unused slot is found */
       
 80297           assert( pLoop->addrOpenEphm[1]<0 );
       
 80298           break;
       
 80299         }
       
 80300         sqlite3VdbeChangeP2(v, addr, nCol);
       
 80301         sqlite3VdbeChangeP4(v, addr, (char*)pKeyInfo, P4_KEYINFO);
       
 80302         pLoop->addrOpenEphm[i] = -1;
       
 80303       }
       
 80304     }
       
 80305     sqlite3DbFree(db, pKeyInfo);
       
 80306   }
       
 80307 
       
 80308 multi_select_end:
       
 80309   pDest->iMem = dest.iMem;
       
 80310   pDest->nMem = dest.nMem;
       
 80311   sqlite3SelectDelete(db, pDelete);
       
 80312   return rc;
       
 80313 }
       
 80314 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
       
 80315 
       
 80316 /*
       
 80317 ** Code an output subroutine for a coroutine implementation of a
       
 80318 ** SELECT statment.
       
 80319 **
       
 80320 ** The data to be output is contained in pIn->iMem.  There are
       
 80321 ** pIn->nMem columns to be output.  pDest is where the output should
       
 80322 ** be sent.
       
 80323 **
       
 80324 ** regReturn is the number of the register holding the subroutine
       
 80325 ** return address.
       
 80326 **
       
 80327 ** If regPrev>0 then it is a the first register in a vector that
       
 80328 ** records the previous output.  mem[regPrev] is a flag that is false
       
 80329 ** if there has been no previous output.  If regPrev>0 then code is
       
 80330 ** generated to suppress duplicates.  pKeyInfo is used for comparing
       
 80331 ** keys.
       
 80332 **
       
 80333 ** If the LIMIT found in p->iLimit is reached, jump immediately to
       
 80334 ** iBreak.
       
 80335 */
       
 80336 static int generateOutputSubroutine(
       
 80337   Parse *pParse,          /* Parsing context */
       
 80338   Select *p,              /* The SELECT statement */
       
 80339   SelectDest *pIn,        /* Coroutine supplying data */
       
 80340   SelectDest *pDest,      /* Where to send the data */
       
 80341   int regReturn,          /* The return address register */
       
 80342   int regPrev,            /* Previous result register.  No uniqueness if 0 */
       
 80343   KeyInfo *pKeyInfo,      /* For comparing with previous entry */
       
 80344   int p4type,             /* The p4 type for pKeyInfo */
       
 80345   int iBreak              /* Jump here if we hit the LIMIT */
       
 80346 ){
       
 80347   Vdbe *v = pParse->pVdbe;
       
 80348   int iContinue;
       
 80349   int addr;
       
 80350 
       
 80351   addr = sqlite3VdbeCurrentAddr(v);
       
 80352   iContinue = sqlite3VdbeMakeLabel(v);
       
 80353 
       
 80354   /* Suppress duplicates for UNION, EXCEPT, and INTERSECT 
       
 80355   */
       
 80356   if( regPrev ){
       
 80357     int j1, j2;
       
 80358     j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
       
 80359     j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iMem, regPrev+1, pIn->nMem,
       
 80360                               (char*)pKeyInfo, p4type);
       
 80361     sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
       
 80362     sqlite3VdbeJumpHere(v, j1);
       
 80363     sqlite3ExprCodeCopy(pParse, pIn->iMem, regPrev+1, pIn->nMem);
       
 80364     sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
       
 80365   }
       
 80366   if( pParse->db->mallocFailed ) return 0;
       
 80367 
       
 80368   /* Suppress the the first OFFSET entries if there is an OFFSET clause
       
 80369   */
       
 80370   codeOffset(v, p, iContinue);
       
 80371 
       
 80372   switch( pDest->eDest ){
       
 80373     /* Store the result as data using a unique key.
       
 80374     */
       
 80375     case SRT_Table:
       
 80376     case SRT_EphemTab: {
       
 80377       int r1 = sqlite3GetTempReg(pParse);
       
 80378       int r2 = sqlite3GetTempReg(pParse);
       
 80379       testcase( pDest->eDest==SRT_Table );
       
 80380       testcase( pDest->eDest==SRT_EphemTab );
       
 80381       sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iMem, pIn->nMem, r1);
       
 80382       sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iParm, r2);
       
 80383       sqlite3VdbeAddOp3(v, OP_Insert, pDest->iParm, r1, r2);
       
 80384       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
       
 80385       sqlite3ReleaseTempReg(pParse, r2);
       
 80386       sqlite3ReleaseTempReg(pParse, r1);
       
 80387       break;
       
 80388     }
       
 80389 
       
 80390 #ifndef SQLITE_OMIT_SUBQUERY
       
 80391     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
       
 80392     ** then there should be a single item on the stack.  Write this
       
 80393     ** item into the set table with bogus data.
       
 80394     */
       
 80395     case SRT_Set: {
       
 80396       int r1;
       
 80397       assert( pIn->nMem==1 );
       
 80398       p->affinity = 
       
 80399          sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affinity);
       
 80400       r1 = sqlite3GetTempReg(pParse);
       
 80401       sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iMem, 1, r1, &p->affinity, 1);
       
 80402       sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, 1);
       
 80403       sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iParm, r1);
       
 80404       sqlite3ReleaseTempReg(pParse, r1);
       
 80405       break;
       
 80406     }
       
 80407 
       
 80408 #if 0  /* Never occurs on an ORDER BY query */
       
 80409     /* If any row exist in the result set, record that fact and abort.
       
 80410     */
       
 80411     case SRT_Exists: {
       
 80412       sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iParm);
       
 80413       /* The LIMIT clause will terminate the loop for us */
       
 80414       break;
       
 80415     }
       
 80416 #endif
       
 80417 
       
 80418     /* If this is a scalar select that is part of an expression, then
       
 80419     ** store the results in the appropriate memory cell and break out
       
 80420     ** of the scan loop.
       
 80421     */
       
 80422     case SRT_Mem: {
       
 80423       assert( pIn->nMem==1 );
       
 80424       sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iParm, 1);
       
 80425       /* The LIMIT clause will jump out of the loop for us */
       
 80426       break;
       
 80427     }
       
 80428 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
       
 80429 
       
 80430     /* The results are stored in a sequence of registers
       
 80431     ** starting at pDest->iMem.  Then the co-routine yields.
       
 80432     */
       
 80433     case SRT_Coroutine: {
       
 80434       if( pDest->iMem==0 ){
       
 80435         pDest->iMem = sqlite3GetTempRange(pParse, pIn->nMem);
       
 80436         pDest->nMem = pIn->nMem;
       
 80437       }
       
 80438       sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iMem, pDest->nMem);
       
 80439       sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
       
 80440       break;
       
 80441     }
       
 80442 
       
 80443     /* If none of the above, then the result destination must be
       
 80444     ** SRT_Output.  This routine is never called with any other
       
 80445     ** destination other than the ones handled above or SRT_Output.
       
 80446     **
       
 80447     ** For SRT_Output, results are stored in a sequence of registers.  
       
 80448     ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
       
 80449     ** return the next row of result.
       
 80450     */
       
 80451     default: {
       
 80452       assert( pDest->eDest==SRT_Output );
       
 80453       sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iMem, pIn->nMem);
       
 80454       sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, pIn->nMem);
       
 80455       break;
       
 80456     }
       
 80457   }
       
 80458 
       
 80459   /* Jump to the end of the loop if the LIMIT is reached.
       
 80460   */
       
 80461   if( p->iLimit ){
       
 80462     sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
       
 80463     sqlite3VdbeAddOp2(v, OP_IfZero, p->iLimit, iBreak);
       
 80464   }
       
 80465 
       
 80466   /* Generate the subroutine return
       
 80467   */
       
 80468   sqlite3VdbeResolveLabel(v, iContinue);
       
 80469   sqlite3VdbeAddOp1(v, OP_Return, regReturn);
       
 80470 
       
 80471   return addr;
       
 80472 }
       
 80473 
       
 80474 /*
       
 80475 ** Alternative compound select code generator for cases when there
       
 80476 ** is an ORDER BY clause.
       
 80477 **
       
 80478 ** We assume a query of the following form:
       
 80479 **
       
 80480 **      <selectA>  <operator>  <selectB>  ORDER BY <orderbylist>
       
 80481 **
       
 80482 ** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT.  The idea
       
 80483 ** is to code both <selectA> and <selectB> with the ORDER BY clause as
       
 80484 ** co-routines.  Then run the co-routines in parallel and merge the results
       
 80485 ** into the output.  In addition to the two coroutines (called selectA and
       
 80486 ** selectB) there are 7 subroutines:
       
 80487 **
       
 80488 **    outA:    Move the output of the selectA coroutine into the output
       
 80489 **             of the compound query.
       
 80490 **
       
 80491 **    outB:    Move the output of the selectB coroutine into the output
       
 80492 **             of the compound query.  (Only generated for UNION and
       
 80493 **             UNION ALL.  EXCEPT and INSERTSECT never output a row that
       
 80494 **             appears only in B.)
       
 80495 **
       
 80496 **    AltB:    Called when there is data from both coroutines and A<B.
       
 80497 **
       
 80498 **    AeqB:    Called when there is data from both coroutines and A==B.
       
 80499 **
       
 80500 **    AgtB:    Called when there is data from both coroutines and A>B.
       
 80501 **
       
 80502 **    EofA:    Called when data is exhausted from selectA.
       
 80503 **
       
 80504 **    EofB:    Called when data is exhausted from selectB.
       
 80505 **
       
 80506 ** The implementation of the latter five subroutines depend on which 
       
 80507 ** <operator> is used:
       
 80508 **
       
 80509 **
       
 80510 **             UNION ALL         UNION            EXCEPT          INTERSECT
       
 80511 **          -------------  -----------------  --------------  -----------------
       
 80512 **   AltB:   outA, nextA      outA, nextA       outA, nextA         nextA
       
 80513 **
       
 80514 **   AeqB:   outA, nextA         nextA             nextA         outA, nextA
       
 80515 **
       
 80516 **   AgtB:   outB, nextB      outB, nextB          nextB            nextB
       
 80517 **
       
 80518 **   EofA:   outB, nextB      outB, nextB          halt             halt
       
 80519 **
       
 80520 **   EofB:   outA, nextA      outA, nextA       outA, nextA         halt
       
 80521 **
       
 80522 ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
       
 80523 ** causes an immediate jump to EofA and an EOF on B following nextB causes
       
 80524 ** an immediate jump to EofB.  Within EofA and EofB, and EOF on entry or
       
 80525 ** following nextX causes a jump to the end of the select processing.
       
 80526 **
       
 80527 ** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
       
 80528 ** within the output subroutine.  The regPrev register set holds the previously
       
 80529 ** output value.  A comparison is made against this value and the output
       
 80530 ** is skipped if the next results would be the same as the previous.
       
 80531 **
       
 80532 ** The implementation plan is to implement the two coroutines and seven
       
 80533 ** subroutines first, then put the control logic at the bottom.  Like this:
       
 80534 **
       
 80535 **          goto Init
       
 80536 **     coA: coroutine for left query (A)
       
 80537 **     coB: coroutine for right query (B)
       
 80538 **    outA: output one row of A
       
 80539 **    outB: output one row of B (UNION and UNION ALL only)
       
 80540 **    EofA: ...
       
 80541 **    EofB: ...
       
 80542 **    AltB: ...
       
 80543 **    AeqB: ...
       
 80544 **    AgtB: ...
       
 80545 **    Init: initialize coroutine registers
       
 80546 **          yield coA
       
 80547 **          if eof(A) goto EofA
       
 80548 **          yield coB
       
 80549 **          if eof(B) goto EofB
       
 80550 **    Cmpr: Compare A, B
       
 80551 **          Jump AltB, AeqB, AgtB
       
 80552 **     End: ...
       
 80553 **
       
 80554 ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
       
 80555 ** actually called using Gosub and they do not Return.  EofA and EofB loop
       
 80556 ** until all data is exhausted then jump to the "end" labe.  AltB, AeqB,
       
 80557 ** and AgtB jump to either L2 or to one of EofA or EofB.
       
 80558 */
       
 80559 #ifndef SQLITE_OMIT_COMPOUND_SELECT
       
 80560 static int multiSelectOrderBy(
       
 80561   Parse *pParse,        /* Parsing context */
       
 80562   Select *p,            /* The right-most of SELECTs to be coded */
       
 80563   SelectDest *pDest     /* What to do with query results */
       
 80564 ){
       
 80565   int i, j;             /* Loop counters */
       
 80566   Select *pPrior;       /* Another SELECT immediately to our left */
       
 80567   Vdbe *v;              /* Generate code to this VDBE */
       
 80568   SelectDest destA;     /* Destination for coroutine A */
       
 80569   SelectDest destB;     /* Destination for coroutine B */
       
 80570   int regAddrA;         /* Address register for select-A coroutine */
       
 80571   int regEofA;          /* Flag to indicate when select-A is complete */
       
 80572   int regAddrB;         /* Address register for select-B coroutine */
       
 80573   int regEofB;          /* Flag to indicate when select-B is complete */
       
 80574   int addrSelectA;      /* Address of the select-A coroutine */
       
 80575   int addrSelectB;      /* Address of the select-B coroutine */
       
 80576   int regOutA;          /* Address register for the output-A subroutine */
       
 80577   int regOutB;          /* Address register for the output-B subroutine */
       
 80578   int addrOutA;         /* Address of the output-A subroutine */
       
 80579   int addrOutB = 0;     /* Address of the output-B subroutine */
       
 80580   int addrEofA;         /* Address of the select-A-exhausted subroutine */
       
 80581   int addrEofB;         /* Address of the select-B-exhausted subroutine */
       
 80582   int addrAltB;         /* Address of the A<B subroutine */
       
 80583   int addrAeqB;         /* Address of the A==B subroutine */
       
 80584   int addrAgtB;         /* Address of the A>B subroutine */
       
 80585   int regLimitA;        /* Limit register for select-A */
       
 80586   int regLimitB;        /* Limit register for select-A */
       
 80587   int regPrev;          /* A range of registers to hold previous output */
       
 80588   int savedLimit;       /* Saved value of p->iLimit */
       
 80589   int savedOffset;      /* Saved value of p->iOffset */
       
 80590   int labelCmpr;        /* Label for the start of the merge algorithm */
       
 80591   int labelEnd;         /* Label for the end of the overall SELECT stmt */
       
 80592   int j1;               /* Jump instructions that get retargetted */
       
 80593   int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
       
 80594   KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
       
 80595   KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
       
 80596   sqlite3 *db;          /* Database connection */
       
 80597   ExprList *pOrderBy;   /* The ORDER BY clause */
       
 80598   int nOrderBy;         /* Number of terms in the ORDER BY clause */
       
 80599   int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
       
 80600 
       
 80601   assert( p->pOrderBy!=0 );
       
 80602   assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
       
 80603   db = pParse->db;
       
 80604   v = pParse->pVdbe;
       
 80605   assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
       
 80606   labelEnd = sqlite3VdbeMakeLabel(v);
       
 80607   labelCmpr = sqlite3VdbeMakeLabel(v);
       
 80608 
       
 80609 
       
 80610   /* Patch up the ORDER BY clause
       
 80611   */
       
 80612   op = p->op;  
       
 80613   pPrior = p->pPrior;
       
 80614   assert( pPrior->pOrderBy==0 );
       
 80615   pOrderBy = p->pOrderBy;
       
 80616   assert( pOrderBy );
       
 80617   nOrderBy = pOrderBy->nExpr;
       
 80618 
       
 80619   /* For operators other than UNION ALL we have to make sure that
       
 80620   ** the ORDER BY clause covers every term of the result set.  Add
       
 80621   ** terms to the ORDER BY clause as necessary.
       
 80622   */
       
 80623   if( op!=TK_ALL ){
       
 80624     for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
       
 80625       struct ExprList_item *pItem;
       
 80626       for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
       
 80627         assert( pItem->iCol>0 );
       
 80628         if( pItem->iCol==i ) break;
       
 80629       }
       
 80630       if( j==nOrderBy ){
       
 80631         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
       
 80632         if( pNew==0 ) return SQLITE_NOMEM;
       
 80633         pNew->flags |= EP_IntValue;
       
 80634         pNew->u.iValue = i;
       
 80635         pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
       
 80636         pOrderBy->a[nOrderBy++].iCol = (u16)i;
       
 80637       }
       
 80638     }
       
 80639   }
       
 80640 
       
 80641   /* Compute the comparison permutation and keyinfo that is used with
       
 80642   ** the permutation used to determine if the next
       
 80643   ** row of results comes from selectA or selectB.  Also add explicit
       
 80644   ** collations to the ORDER BY clause terms so that when the subqueries
       
 80645   ** to the right and the left are evaluated, they use the correct
       
 80646   ** collation.
       
 80647   */
       
 80648   aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
       
 80649   if( aPermute ){
       
 80650     struct ExprList_item *pItem;
       
 80651     for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
       
 80652       assert( pItem->iCol>0  && pItem->iCol<=p->pEList->nExpr );
       
 80653       aPermute[i] = pItem->iCol - 1;
       
 80654     }
       
 80655     pKeyMerge =
       
 80656       sqlite3DbMallocRaw(db, sizeof(*pKeyMerge)+nOrderBy*(sizeof(CollSeq*)+1));
       
 80657     if( pKeyMerge ){
       
 80658       pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy];
       
 80659       pKeyMerge->nField = (u16)nOrderBy;
       
 80660       pKeyMerge->enc = ENC(db);
       
 80661       for(i=0; i<nOrderBy; i++){
       
 80662         CollSeq *pColl;
       
 80663         Expr *pTerm = pOrderBy->a[i].pExpr;
       
 80664         if( pTerm->flags & EP_ExpCollate ){
       
 80665           pColl = pTerm->pColl;
       
 80666         }else{
       
 80667           pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
       
 80668           pTerm->flags |= EP_ExpCollate;
       
 80669           pTerm->pColl = pColl;
       
 80670         }
       
 80671         pKeyMerge->aColl[i] = pColl;
       
 80672         pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
       
 80673       }
       
 80674     }
       
 80675   }else{
       
 80676     pKeyMerge = 0;
       
 80677   }
       
 80678 
       
 80679   /* Reattach the ORDER BY clause to the query.
       
 80680   */
       
 80681   p->pOrderBy = pOrderBy;
       
 80682   pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
       
 80683 
       
 80684   /* Allocate a range of temporary registers and the KeyInfo needed
       
 80685   ** for the logic that removes duplicate result rows when the
       
 80686   ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
       
 80687   */
       
 80688   if( op==TK_ALL ){
       
 80689     regPrev = 0;
       
 80690   }else{
       
 80691     int nExpr = p->pEList->nExpr;
       
 80692     assert( nOrderBy>=nExpr || db->mallocFailed );
       
 80693     regPrev = sqlite3GetTempRange(pParse, nExpr+1);
       
 80694     sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
       
 80695     pKeyDup = sqlite3DbMallocZero(db,
       
 80696                   sizeof(*pKeyDup) + nExpr*(sizeof(CollSeq*)+1) );
       
 80697     if( pKeyDup ){
       
 80698       pKeyDup->aSortOrder = (u8*)&pKeyDup->aColl[nExpr];
       
 80699       pKeyDup->nField = (u16)nExpr;
       
 80700       pKeyDup->enc = ENC(db);
       
 80701       for(i=0; i<nExpr; i++){
       
 80702         pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
       
 80703         pKeyDup->aSortOrder[i] = 0;
       
 80704       }
       
 80705     }
       
 80706   }
       
 80707  
       
 80708   /* Separate the left and the right query from one another
       
 80709   */
       
 80710   p->pPrior = 0;
       
 80711   pPrior->pRightmost = 0;
       
 80712   sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
       
 80713   if( pPrior->pPrior==0 ){
       
 80714     sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
       
 80715   }
       
 80716 
       
 80717   /* Compute the limit registers */
       
 80718   computeLimitRegisters(pParse, p, labelEnd);
       
 80719   if( p->iLimit && op==TK_ALL ){
       
 80720     regLimitA = ++pParse->nMem;
       
 80721     regLimitB = ++pParse->nMem;
       
 80722     sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
       
 80723                                   regLimitA);
       
 80724     sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
       
 80725   }else{
       
 80726     regLimitA = regLimitB = 0;
       
 80727   }
       
 80728   sqlite3ExprDelete(db, p->pLimit);
       
 80729   p->pLimit = 0;
       
 80730   sqlite3ExprDelete(db, p->pOffset);
       
 80731   p->pOffset = 0;
       
 80732 
       
 80733   regAddrA = ++pParse->nMem;
       
 80734   regEofA = ++pParse->nMem;
       
 80735   regAddrB = ++pParse->nMem;
       
 80736   regEofB = ++pParse->nMem;
       
 80737   regOutA = ++pParse->nMem;
       
 80738   regOutB = ++pParse->nMem;
       
 80739   sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
       
 80740   sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
       
 80741 
       
 80742   /* Jump past the various subroutines and coroutines to the main
       
 80743   ** merge loop
       
 80744   */
       
 80745   j1 = sqlite3VdbeAddOp0(v, OP_Goto);
       
 80746   addrSelectA = sqlite3VdbeCurrentAddr(v);
       
 80747 
       
 80748 
       
 80749   /* Generate a coroutine to evaluate the SELECT statement to the
       
 80750   ** left of the compound operator - the "A" select.
       
 80751   */
       
 80752   VdbeNoopComment((v, "Begin coroutine for left SELECT"));
       
 80753   pPrior->iLimit = regLimitA;
       
 80754   sqlite3Select(pParse, pPrior, &destA);
       
 80755   sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofA);
       
 80756   sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
       
 80757   VdbeNoopComment((v, "End coroutine for left SELECT"));
       
 80758 
       
 80759   /* Generate a coroutine to evaluate the SELECT statement on 
       
 80760   ** the right - the "B" select
       
 80761   */
       
 80762   addrSelectB = sqlite3VdbeCurrentAddr(v);
       
 80763   VdbeNoopComment((v, "Begin coroutine for right SELECT"));
       
 80764   savedLimit = p->iLimit;
       
 80765   savedOffset = p->iOffset;
       
 80766   p->iLimit = regLimitB;
       
 80767   p->iOffset = 0;  
       
 80768   sqlite3Select(pParse, p, &destB);
       
 80769   p->iLimit = savedLimit;
       
 80770   p->iOffset = savedOffset;
       
 80771   sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofB);
       
 80772   sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
       
 80773   VdbeNoopComment((v, "End coroutine for right SELECT"));
       
 80774 
       
 80775   /* Generate a subroutine that outputs the current row of the A
       
 80776   ** select as the next output row of the compound select.
       
 80777   */
       
 80778   VdbeNoopComment((v, "Output routine for A"));
       
 80779   addrOutA = generateOutputSubroutine(pParse,
       
 80780                  p, &destA, pDest, regOutA,
       
 80781                  regPrev, pKeyDup, P4_KEYINFO_HANDOFF, labelEnd);
       
 80782   
       
 80783   /* Generate a subroutine that outputs the current row of the B
       
 80784   ** select as the next output row of the compound select.
       
 80785   */
       
 80786   if( op==TK_ALL || op==TK_UNION ){
       
 80787     VdbeNoopComment((v, "Output routine for B"));
       
 80788     addrOutB = generateOutputSubroutine(pParse,
       
 80789                  p, &destB, pDest, regOutB,
       
 80790                  regPrev, pKeyDup, P4_KEYINFO_STATIC, labelEnd);
       
 80791   }
       
 80792 
       
 80793   /* Generate a subroutine to run when the results from select A
       
 80794   ** are exhausted and only data in select B remains.
       
 80795   */
       
 80796   VdbeNoopComment((v, "eof-A subroutine"));
       
 80797   if( op==TK_EXCEPT || op==TK_INTERSECT ){
       
 80798     addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd);
       
 80799   }else{  
       
 80800     addrEofA = sqlite3VdbeAddOp2(v, OP_If, regEofB, labelEnd);
       
 80801     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
       
 80802     sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
       
 80803     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
       
 80804   }
       
 80805 
       
 80806   /* Generate a subroutine to run when the results from select B
       
 80807   ** are exhausted and only data in select A remains.
       
 80808   */
       
 80809   if( op==TK_INTERSECT ){
       
 80810     addrEofB = addrEofA;
       
 80811   }else{  
       
 80812     VdbeNoopComment((v, "eof-B subroutine"));
       
 80813     addrEofB = sqlite3VdbeAddOp2(v, OP_If, regEofA, labelEnd);
       
 80814     sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
       
 80815     sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
       
 80816     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
       
 80817   }
       
 80818 
       
 80819   /* Generate code to handle the case of A<B
       
 80820   */
       
 80821   VdbeNoopComment((v, "A-lt-B subroutine"));
       
 80822   addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
       
 80823   sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
       
 80824   sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
       
 80825   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
       
 80826 
       
 80827   /* Generate code to handle the case of A==B
       
 80828   */
       
 80829   if( op==TK_ALL ){
       
 80830     addrAeqB = addrAltB;
       
 80831   }else if( op==TK_INTERSECT ){
       
 80832     addrAeqB = addrAltB;
       
 80833     addrAltB++;
       
 80834   }else{
       
 80835     VdbeNoopComment((v, "A-eq-B subroutine"));
       
 80836     addrAeqB =
       
 80837     sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
       
 80838     sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
       
 80839     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
       
 80840   }
       
 80841 
       
 80842   /* Generate code to handle the case of A>B
       
 80843   */
       
 80844   VdbeNoopComment((v, "A-gt-B subroutine"));
       
 80845   addrAgtB = sqlite3VdbeCurrentAddr(v);
       
 80846   if( op==TK_ALL || op==TK_UNION ){
       
 80847     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
       
 80848   }
       
 80849   sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
       
 80850   sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
       
 80851   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
       
 80852 
       
 80853   /* This code runs once to initialize everything.
       
 80854   */
       
 80855   sqlite3VdbeJumpHere(v, j1);
       
 80856   sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofA);
       
 80857   sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofB);
       
 80858   sqlite3VdbeAddOp2(v, OP_Gosub, regAddrA, addrSelectA);
       
 80859   sqlite3VdbeAddOp2(v, OP_Gosub, regAddrB, addrSelectB);
       
 80860   sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
       
 80861   sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
       
 80862 
       
 80863   /* Implement the main merge loop
       
 80864   */
       
 80865   sqlite3VdbeResolveLabel(v, labelCmpr);
       
 80866   sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
       
 80867   sqlite3VdbeAddOp4(v, OP_Compare, destA.iMem, destB.iMem, nOrderBy,
       
 80868                          (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
       
 80869   sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
       
 80870 
       
 80871   /* Release temporary registers
       
 80872   */
       
 80873   if( regPrev ){
       
 80874     sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1);
       
 80875   }
       
 80876 
       
 80877   /* Jump to the this point in order to terminate the query.
       
 80878   */
       
 80879   sqlite3VdbeResolveLabel(v, labelEnd);
       
 80880 
       
 80881   /* Set the number of output columns
       
 80882   */
       
 80883   if( pDest->eDest==SRT_Output ){
       
 80884     Select *pFirst = pPrior;
       
 80885     while( pFirst->pPrior ) pFirst = pFirst->pPrior;
       
 80886     generateColumnNames(pParse, 0, pFirst->pEList);
       
 80887   }
       
 80888 
       
 80889   /* Reassembly the compound query so that it will be freed correctly
       
 80890   ** by the calling function */
       
 80891   if( p->pPrior ){
       
 80892     sqlite3SelectDelete(db, p->pPrior);
       
 80893   }
       
 80894   p->pPrior = pPrior;
       
 80895 
       
 80896   /*** TBD:  Insert subroutine calls to close cursors on incomplete
       
 80897   **** subqueries ****/
       
 80898   return SQLITE_OK;
       
 80899 }
       
 80900 #endif
       
 80901 
       
 80902 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
       
 80903 /* Forward Declarations */
       
 80904 static void substExprList(sqlite3*, ExprList*, int, ExprList*);
       
 80905 static void substSelect(sqlite3*, Select *, int, ExprList *);
       
 80906 
       
 80907 /*
       
 80908 ** Scan through the expression pExpr.  Replace every reference to
       
 80909 ** a column in table number iTable with a copy of the iColumn-th
       
 80910 ** entry in pEList.  (But leave references to the ROWID column 
       
 80911 ** unchanged.)
       
 80912 **
       
 80913 ** This routine is part of the flattening procedure.  A subquery
       
 80914 ** whose result set is defined by pEList appears as entry in the
       
 80915 ** FROM clause of a SELECT such that the VDBE cursor assigned to that
       
 80916 ** FORM clause entry is iTable.  This routine make the necessary 
       
 80917 ** changes to pExpr so that it refers directly to the source table
       
 80918 ** of the subquery rather the result set of the subquery.
       
 80919 */
       
 80920 static Expr *substExpr(
       
 80921   sqlite3 *db,        /* Report malloc errors to this connection */
       
 80922   Expr *pExpr,        /* Expr in which substitution occurs */
       
 80923   int iTable,         /* Table to be substituted */
       
 80924   ExprList *pEList    /* Substitute expressions */
       
 80925 ){
       
 80926   if( pExpr==0 ) return 0;
       
 80927   if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
       
 80928     if( pExpr->iColumn<0 ){
       
 80929       pExpr->op = TK_NULL;
       
 80930     }else{
       
 80931       Expr *pNew;
       
 80932       assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
       
 80933       assert( pExpr->pLeft==0 && pExpr->pRight==0 );
       
 80934       pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
       
 80935       if( pNew && pExpr->pColl ){
       
 80936         pNew->pColl = pExpr->pColl;
       
 80937       }
       
 80938       sqlite3ExprDelete(db, pExpr);
       
 80939       pExpr = pNew;
       
 80940     }
       
 80941   }else{
       
 80942     pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
       
 80943     pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
       
 80944     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
       
 80945       substSelect(db, pExpr->x.pSelect, iTable, pEList);
       
 80946     }else{
       
 80947       substExprList(db, pExpr->x.pList, iTable, pEList);
       
 80948     }
       
 80949   }
       
 80950   return pExpr;
       
 80951 }
       
 80952 static void substExprList(
       
 80953   sqlite3 *db,         /* Report malloc errors here */
       
 80954   ExprList *pList,     /* List to scan and in which to make substitutes */
       
 80955   int iTable,          /* Table to be substituted */
       
 80956   ExprList *pEList     /* Substitute values */
       
 80957 ){
       
 80958   int i;
       
 80959   if( pList==0 ) return;
       
 80960   for(i=0; i<pList->nExpr; i++){
       
 80961     pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
       
 80962   }
       
 80963 }
       
 80964 static void substSelect(
       
 80965   sqlite3 *db,         /* Report malloc errors here */
       
 80966   Select *p,           /* SELECT statement in which to make substitutions */
       
 80967   int iTable,          /* Table to be replaced */
       
 80968   ExprList *pEList     /* Substitute values */
       
 80969 ){
       
 80970   SrcList *pSrc;
       
 80971   struct SrcList_item *pItem;
       
 80972   int i;
       
 80973   if( !p ) return;
       
 80974   substExprList(db, p->pEList, iTable, pEList);
       
 80975   substExprList(db, p->pGroupBy, iTable, pEList);
       
 80976   substExprList(db, p->pOrderBy, iTable, pEList);
       
 80977   p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
       
 80978   p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
       
 80979   substSelect(db, p->pPrior, iTable, pEList);
       
 80980   pSrc = p->pSrc;
       
 80981   assert( pSrc );  /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
       
 80982   if( ALWAYS(pSrc) ){
       
 80983     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
       
 80984       substSelect(db, pItem->pSelect, iTable, pEList);
       
 80985     }
       
 80986   }
       
 80987 }
       
 80988 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
       
 80989 
       
 80990 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
       
 80991 /*
       
 80992 ** This routine attempts to flatten subqueries in order to speed
       
 80993 ** execution.  It returns 1 if it makes changes and 0 if no flattening
       
 80994 ** occurs.
       
 80995 **
       
 80996 ** To understand the concept of flattening, consider the following
       
 80997 ** query:
       
 80998 **
       
 80999 **     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
       
 81000 **
       
 81001 ** The default way of implementing this query is to execute the
       
 81002 ** subquery first and store the results in a temporary table, then
       
 81003 ** run the outer query on that temporary table.  This requires two
       
 81004 ** passes over the data.  Furthermore, because the temporary table
       
 81005 ** has no indices, the WHERE clause on the outer query cannot be
       
 81006 ** optimized.
       
 81007 **
       
 81008 ** This routine attempts to rewrite queries such as the above into
       
 81009 ** a single flat select, like this:
       
 81010 **
       
 81011 **     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
       
 81012 **
       
 81013 ** The code generated for this simpification gives the same result
       
 81014 ** but only has to scan the data once.  And because indices might 
       
 81015 ** exist on the table t1, a complete scan of the data might be
       
 81016 ** avoided.
       
 81017 **
       
 81018 ** Flattening is only attempted if all of the following are true:
       
 81019 **
       
 81020 **   (1)  The subquery and the outer query do not both use aggregates.
       
 81021 **
       
 81022 **   (2)  The subquery is not an aggregate or the outer query is not a join.
       
 81023 **
       
 81024 **   (3)  The subquery is not the right operand of a left outer join
       
 81025 **        (Originally ticket #306.  Strenghtened by ticket #3300)
       
 81026 **
       
 81027 **   (4)  The subquery is not DISTINCT or the outer query is not a join.
       
 81028 **
       
 81029 **   (5)  The subquery is not DISTINCT or the outer query does not use
       
 81030 **        aggregates.
       
 81031 **
       
 81032 **   (6)  The subquery does not use aggregates or the outer query is not
       
 81033 **        DISTINCT.
       
 81034 **
       
 81035 **   (7)  The subquery has a FROM clause.
       
 81036 **
       
 81037 **   (8)  The subquery does not use LIMIT or the outer query is not a join.
       
 81038 **
       
 81039 **   (9)  The subquery does not use LIMIT or the outer query does not use
       
 81040 **        aggregates.
       
 81041 **
       
 81042 **  (10)  The subquery does not use aggregates or the outer query does not
       
 81043 **        use LIMIT.
       
 81044 **
       
 81045 **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
       
 81046 **
       
 81047 **  (12)  Not implemented.  Subsumed into restriction (3).  Was previously
       
 81048 **        a separate restriction deriving from ticket #350.
       
 81049 **
       
 81050 **  (13)  The subquery and outer query do not both use LIMIT
       
 81051 **
       
 81052 **  (14)  The subquery does not use OFFSET
       
 81053 **
       
 81054 **  (15)  The outer query is not part of a compound select or the
       
 81055 **        subquery does not have both an ORDER BY and a LIMIT clause.
       
 81056 **        (See ticket #2339)
       
 81057 **
       
 81058 **  (16)  The outer query is not an aggregate or the subquery does
       
 81059 **        not contain ORDER BY.  (Ticket #2942)  This used to not matter
       
 81060 **        until we introduced the group_concat() function.  
       
 81061 **
       
 81062 **  (17)  The sub-query is not a compound select, or it is a UNION ALL 
       
 81063 **        compound clause made up entirely of non-aggregate queries, and 
       
 81064 **        the parent query:
       
 81065 **
       
 81066 **          * is not itself part of a compound select,
       
 81067 **          * is not an aggregate or DISTINCT query, and
       
 81068 **          * has no other tables or sub-selects in the FROM clause.
       
 81069 **
       
 81070 **        The parent and sub-query may contain WHERE clauses. Subject to
       
 81071 **        rules (11), (13) and (14), they may also contain ORDER BY,
       
 81072 **        LIMIT and OFFSET clauses.
       
 81073 **
       
 81074 **  (18)  If the sub-query is a compound select, then all terms of the
       
 81075 **        ORDER by clause of the parent must be simple references to 
       
 81076 **        columns of the sub-query.
       
 81077 **
       
 81078 **  (19)  The subquery does not use LIMIT or the outer query does not
       
 81079 **        have a WHERE clause.
       
 81080 **
       
 81081 **  (20)  If the sub-query is a compound select, then it must not use
       
 81082 **        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
       
 81083 **        somewhat by saying that the terms of the ORDER BY clause must
       
 81084 **        appear as unmodified result columns in the outer query.  But
       
 81085 **        have other optimizations in mind to deal with that case.
       
 81086 **
       
 81087 ** In this routine, the "p" parameter is a pointer to the outer query.
       
 81088 ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
       
 81089 ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
       
 81090 **
       
 81091 ** If flattening is not attempted, this routine is a no-op and returns 0.
       
 81092 ** If flattening is attempted this routine returns 1.
       
 81093 **
       
 81094 ** All of the expression analysis must occur on both the outer query and
       
 81095 ** the subquery before this routine runs.
       
 81096 */
       
 81097 static int flattenSubquery(
       
 81098   Parse *pParse,       /* Parsing context */
       
 81099   Select *p,           /* The parent or outer SELECT statement */
       
 81100   int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
       
 81101   int isAgg,           /* True if outer SELECT uses aggregate functions */
       
 81102   int subqueryIsAgg    /* True if the subquery uses aggregate functions */
       
 81103 ){
       
 81104   const char *zSavedAuthContext = pParse->zAuthContext;
       
 81105   Select *pParent;
       
 81106   Select *pSub;       /* The inner query or "subquery" */
       
 81107   Select *pSub1;      /* Pointer to the rightmost select in sub-query */
       
 81108   SrcList *pSrc;      /* The FROM clause of the outer query */
       
 81109   SrcList *pSubSrc;   /* The FROM clause of the subquery */
       
 81110   ExprList *pList;    /* The result set of the outer query */
       
 81111   int iParent;        /* VDBE cursor number of the pSub result set temp table */
       
 81112   int i;              /* Loop counter */
       
 81113   Expr *pWhere;                    /* The WHERE clause */
       
 81114   struct SrcList_item *pSubitem;   /* The subquery */
       
 81115   sqlite3 *db = pParse->db;
       
 81116 
       
 81117   /* Check to see if flattening is permitted.  Return 0 if not.
       
 81118   */
       
 81119   assert( p!=0 );
       
 81120   assert( p->pPrior==0 );  /* Unable to flatten compound queries */
       
 81121   pSrc = p->pSrc;
       
 81122   assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
       
 81123   pSubitem = &pSrc->a[iFrom];
       
 81124   iParent = pSubitem->iCursor;
       
 81125   pSub = pSubitem->pSelect;
       
 81126   assert( pSub!=0 );
       
 81127   if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
       
 81128   if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
       
 81129   pSubSrc = pSub->pSrc;
       
 81130   assert( pSubSrc );
       
 81131   /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
       
 81132   ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
       
 81133   ** because they could be computed at compile-time.  But when LIMIT and OFFSET
       
 81134   ** became arbitrary expressions, we were forced to add restrictions (13)
       
 81135   ** and (14). */
       
 81136   if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
       
 81137   if( pSub->pOffset ) return 0;                          /* Restriction (14) */
       
 81138   if( p->pRightmost && pSub->pLimit && pSub->pOrderBy ){
       
 81139     return 0;                                            /* Restriction (15) */
       
 81140   }
       
 81141   if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
       
 81142   if( ((pSub->selFlags & SF_Distinct)!=0 || pSub->pLimit) 
       
 81143          && (pSrc->nSrc>1 || isAgg) ){          /* Restrictions (4)(5)(8)(9) */
       
 81144      return 0;       
       
 81145   }
       
 81146   if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
       
 81147      return 0;         /* Restriction (6)  */
       
 81148   }
       
 81149   if( p->pOrderBy && pSub->pOrderBy ){
       
 81150      return 0;                                           /* Restriction (11) */
       
 81151   }
       
 81152   if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
       
 81153   if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
       
 81154 
       
 81155   /* OBSOLETE COMMENT 1:
       
 81156   ** Restriction 3:  If the subquery is a join, make sure the subquery is 
       
 81157   ** not used as the right operand of an outer join.  Examples of why this
       
 81158   ** is not allowed:
       
 81159   **
       
 81160   **         t1 LEFT OUTER JOIN (t2 JOIN t3)
       
 81161   **
       
 81162   ** If we flatten the above, we would get
       
 81163   **
       
 81164   **         (t1 LEFT OUTER JOIN t2) JOIN t3
       
 81165   **
       
 81166   ** which is not at all the same thing.
       
 81167   **
       
 81168   ** OBSOLETE COMMENT 2:
       
 81169   ** Restriction 12:  If the subquery is the right operand of a left outer
       
 81170   ** join, make sure the subquery has no WHERE clause.
       
 81171   ** An examples of why this is not allowed:
       
 81172   **
       
 81173   **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
       
 81174   **
       
 81175   ** If we flatten the above, we would get
       
 81176   **
       
 81177   **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
       
 81178   **
       
 81179   ** But the t2.x>0 test will always fail on a NULL row of t2, which
       
 81180   ** effectively converts the OUTER JOIN into an INNER JOIN.
       
 81181   **
       
 81182   ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
       
 81183   ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
       
 81184   ** is fraught with danger.  Best to avoid the whole thing.  If the
       
 81185   ** subquery is the right term of a LEFT JOIN, then do not flatten.
       
 81186   */
       
 81187   if( (pSubitem->jointype & JT_OUTER)!=0 ){
       
 81188     return 0;
       
 81189   }
       
 81190 
       
 81191   /* Restriction 17: If the sub-query is a compound SELECT, then it must
       
 81192   ** use only the UNION ALL operator. And none of the simple select queries
       
 81193   ** that make up the compound SELECT are allowed to be aggregate or distinct
       
 81194   ** queries.
       
 81195   */
       
 81196   if( pSub->pPrior ){
       
 81197     if( pSub->pOrderBy ){
       
 81198       return 0;  /* Restriction 20 */
       
 81199     }
       
 81200     if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
       
 81201       return 0;
       
 81202     }
       
 81203     for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
       
 81204       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
       
 81205       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
       
 81206       if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
       
 81207        || (pSub1->pPrior && pSub1->op!=TK_ALL) 
       
 81208        || NEVER(pSub1->pSrc==0) || pSub1->pSrc->nSrc!=1
       
 81209       ){
       
 81210         return 0;
       
 81211       }
       
 81212     }
       
 81213 
       
 81214     /* Restriction 18. */
       
 81215     if( p->pOrderBy ){
       
 81216       int ii;
       
 81217       for(ii=0; ii<p->pOrderBy->nExpr; ii++){
       
 81218         if( p->pOrderBy->a[ii].iCol==0 ) return 0;
       
 81219       }
       
 81220     }
       
 81221   }
       
 81222 
       
 81223   /***** If we reach this point, flattening is permitted. *****/
       
 81224 
       
 81225   /* Authorize the subquery */
       
 81226   pParse->zAuthContext = pSubitem->zName;
       
 81227   sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
       
 81228   pParse->zAuthContext = zSavedAuthContext;
       
 81229 
       
 81230   /* If the sub-query is a compound SELECT statement, then (by restrictions
       
 81231   ** 17 and 18 above) it must be a UNION ALL and the parent query must 
       
 81232   ** be of the form:
       
 81233   **
       
 81234   **     SELECT <expr-list> FROM (<sub-query>) <where-clause> 
       
 81235   **
       
 81236   ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
       
 81237   ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or 
       
 81238   ** OFFSET clauses and joins them to the left-hand-side of the original
       
 81239   ** using UNION ALL operators. In this case N is the number of simple
       
 81240   ** select statements in the compound sub-query.
       
 81241   **
       
 81242   ** Example:
       
 81243   **
       
 81244   **     SELECT a+1 FROM (
       
 81245   **        SELECT x FROM tab
       
 81246   **        UNION ALL
       
 81247   **        SELECT y FROM tab
       
 81248   **        UNION ALL
       
 81249   **        SELECT abs(z*2) FROM tab2
       
 81250   **     ) WHERE a!=5 ORDER BY 1
       
 81251   **
       
 81252   ** Transformed into:
       
 81253   **
       
 81254   **     SELECT x+1 FROM tab WHERE x+1!=5
       
 81255   **     UNION ALL
       
 81256   **     SELECT y+1 FROM tab WHERE y+1!=5
       
 81257   **     UNION ALL
       
 81258   **     SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
       
 81259   **     ORDER BY 1
       
 81260   **
       
 81261   ** We call this the "compound-subquery flattening".
       
 81262   */
       
 81263   for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
       
 81264     Select *pNew;
       
 81265     ExprList *pOrderBy = p->pOrderBy;
       
 81266     Expr *pLimit = p->pLimit;
       
 81267     Select *pPrior = p->pPrior;
       
 81268     p->pOrderBy = 0;
       
 81269     p->pSrc = 0;
       
 81270     p->pPrior = 0;
       
 81271     p->pLimit = 0;
       
 81272     pNew = sqlite3SelectDup(db, p, 0);
       
 81273     p->pLimit = pLimit;
       
 81274     p->pOrderBy = pOrderBy;
       
 81275     p->pSrc = pSrc;
       
 81276     p->op = TK_ALL;
       
 81277     p->pRightmost = 0;
       
 81278     if( pNew==0 ){
       
 81279       pNew = pPrior;
       
 81280     }else{
       
 81281       pNew->pPrior = pPrior;
       
 81282       pNew->pRightmost = 0;
       
 81283     }
       
 81284     p->pPrior = pNew;
       
 81285     if( db->mallocFailed ) return 1;
       
 81286   }
       
 81287 
       
 81288   /* Begin flattening the iFrom-th entry of the FROM clause 
       
 81289   ** in the outer query.
       
 81290   */
       
 81291   pSub = pSub1 = pSubitem->pSelect;
       
 81292 
       
 81293   /* Delete the transient table structure associated with the
       
 81294   ** subquery
       
 81295   */
       
 81296   sqlite3DbFree(db, pSubitem->zDatabase);
       
 81297   sqlite3DbFree(db, pSubitem->zName);
       
 81298   sqlite3DbFree(db, pSubitem->zAlias);
       
 81299   pSubitem->zDatabase = 0;
       
 81300   pSubitem->zName = 0;
       
 81301   pSubitem->zAlias = 0;
       
 81302   pSubitem->pSelect = 0;
       
 81303 
       
 81304   /* Defer deleting the Table object associated with the
       
 81305   ** subquery until code generation is
       
 81306   ** complete, since there may still exist Expr.pTab entries that
       
 81307   ** refer to the subquery even after flattening.  Ticket #3346.
       
 81308   **
       
 81309   ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
       
 81310   */
       
 81311   if( ALWAYS(pSubitem->pTab!=0) ){
       
 81312     Table *pTabToDel = pSubitem->pTab;
       
 81313     if( pTabToDel->nRef==1 ){
       
 81314       Parse *pToplevel = sqlite3ParseToplevel(pParse);
       
 81315       pTabToDel->pNextZombie = pToplevel->pZombieTab;
       
 81316       pToplevel->pZombieTab = pTabToDel;
       
 81317     }else{
       
 81318       pTabToDel->nRef--;
       
 81319     }
       
 81320     pSubitem->pTab = 0;
       
 81321   }
       
 81322 
       
 81323   /* The following loop runs once for each term in a compound-subquery
       
 81324   ** flattening (as described above).  If we are doing a different kind
       
 81325   ** of flattening - a flattening other than a compound-subquery flattening -
       
 81326   ** then this loop only runs once.
       
 81327   **
       
 81328   ** This loop moves all of the FROM elements of the subquery into the
       
 81329   ** the FROM clause of the outer query.  Before doing this, remember
       
 81330   ** the cursor number for the original outer query FROM element in
       
 81331   ** iParent.  The iParent cursor will never be used.  Subsequent code
       
 81332   ** will scan expressions looking for iParent references and replace
       
 81333   ** those references with expressions that resolve to the subquery FROM
       
 81334   ** elements we are now copying in.
       
 81335   */
       
 81336   for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
       
 81337     int nSubSrc;
       
 81338     u8 jointype = 0;
       
 81339     pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
       
 81340     nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
       
 81341     pSrc = pParent->pSrc;     /* FROM clause of the outer query */
       
 81342 
       
 81343     if( pSrc ){
       
 81344       assert( pParent==p );  /* First time through the loop */
       
 81345       jointype = pSubitem->jointype;
       
 81346     }else{
       
 81347       assert( pParent!=p );  /* 2nd and subsequent times through the loop */
       
 81348       pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
       
 81349       if( pSrc==0 ){
       
 81350         assert( db->mallocFailed );
       
 81351         break;
       
 81352       }
       
 81353     }
       
 81354 
       
 81355     /* The subquery uses a single slot of the FROM clause of the outer
       
 81356     ** query.  If the subquery has more than one element in its FROM clause,
       
 81357     ** then expand the outer query to make space for it to hold all elements
       
 81358     ** of the subquery.
       
 81359     **
       
 81360     ** Example:
       
 81361     **
       
 81362     **    SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
       
 81363     **
       
 81364     ** The outer query has 3 slots in its FROM clause.  One slot of the
       
 81365     ** outer query (the middle slot) is used by the subquery.  The next
       
 81366     ** block of code will expand the out query to 4 slots.  The middle
       
 81367     ** slot is expanded to two slots in order to make space for the
       
 81368     ** two elements in the FROM clause of the subquery.
       
 81369     */
       
 81370     if( nSubSrc>1 ){
       
 81371       pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
       
 81372       if( db->mallocFailed ){
       
 81373         break;
       
 81374       }
       
 81375     }
       
 81376 
       
 81377     /* Transfer the FROM clause terms from the subquery into the
       
 81378     ** outer query.
       
 81379     */
       
 81380     for(i=0; i<nSubSrc; i++){
       
 81381       sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
       
 81382       pSrc->a[i+iFrom] = pSubSrc->a[i];
       
 81383       memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
       
 81384     }
       
 81385     pSrc->a[iFrom].jointype = jointype;
       
 81386   
       
 81387     /* Now begin substituting subquery result set expressions for 
       
 81388     ** references to the iParent in the outer query.
       
 81389     ** 
       
 81390     ** Example:
       
 81391     **
       
 81392     **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
       
 81393     **   \                     \_____________ subquery __________/          /
       
 81394     **    \_____________________ outer query ______________________________/
       
 81395     **
       
 81396     ** We look at every expression in the outer query and every place we see
       
 81397     ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
       
 81398     */
       
 81399     pList = pParent->pEList;
       
 81400     for(i=0; i<pList->nExpr; i++){
       
 81401       if( pList->a[i].zName==0 ){
       
 81402         const char *zSpan = pList->a[i].zSpan;
       
 81403         if( ALWAYS(zSpan) ){
       
 81404           pList->a[i].zName = sqlite3DbStrDup(db, zSpan);
       
 81405         }
       
 81406       }
       
 81407     }
       
 81408     substExprList(db, pParent->pEList, iParent, pSub->pEList);
       
 81409     if( isAgg ){
       
 81410       substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
       
 81411       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
       
 81412     }
       
 81413     if( pSub->pOrderBy ){
       
 81414       assert( pParent->pOrderBy==0 );
       
 81415       pParent->pOrderBy = pSub->pOrderBy;
       
 81416       pSub->pOrderBy = 0;
       
 81417     }else if( pParent->pOrderBy ){
       
 81418       substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
       
 81419     }
       
 81420     if( pSub->pWhere ){
       
 81421       pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
       
 81422     }else{
       
 81423       pWhere = 0;
       
 81424     }
       
 81425     if( subqueryIsAgg ){
       
 81426       assert( pParent->pHaving==0 );
       
 81427       pParent->pHaving = pParent->pWhere;
       
 81428       pParent->pWhere = pWhere;
       
 81429       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
       
 81430       pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving, 
       
 81431                                   sqlite3ExprDup(db, pSub->pHaving, 0));
       
 81432       assert( pParent->pGroupBy==0 );
       
 81433       pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
       
 81434     }else{
       
 81435       pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
       
 81436       pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
       
 81437     }
       
 81438   
       
 81439     /* The flattened query is distinct if either the inner or the
       
 81440     ** outer query is distinct. 
       
 81441     */
       
 81442     pParent->selFlags |= pSub->selFlags & SF_Distinct;
       
 81443   
       
 81444     /*
       
 81445     ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
       
 81446     **
       
 81447     ** One is tempted to try to add a and b to combine the limits.  But this
       
 81448     ** does not work if either limit is negative.
       
 81449     */
       
 81450     if( pSub->pLimit ){
       
 81451       pParent->pLimit = pSub->pLimit;
       
 81452       pSub->pLimit = 0;
       
 81453     }
       
 81454   }
       
 81455 
       
 81456   /* Finially, delete what is left of the subquery and return
       
 81457   ** success.
       
 81458   */
       
 81459   sqlite3SelectDelete(db, pSub1);
       
 81460 
       
 81461   return 1;
       
 81462 }
       
 81463 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
       
 81464 
       
 81465 /*
       
 81466 ** Analyze the SELECT statement passed as an argument to see if it
       
 81467 ** is a min() or max() query. Return WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX if 
       
 81468 ** it is, or 0 otherwise. At present, a query is considered to be
       
 81469 ** a min()/max() query if:
       
 81470 **
       
 81471 **   1. There is a single object in the FROM clause.
       
 81472 **
       
 81473 **   2. There is a single expression in the result set, and it is
       
 81474 **      either min(x) or max(x), where x is a column reference.
       
 81475 */
       
 81476 static u8 minMaxQuery(Select *p){
       
 81477   Expr *pExpr;
       
 81478   ExprList *pEList = p->pEList;
       
 81479 
       
 81480   if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
       
 81481   pExpr = pEList->a[0].pExpr;
       
 81482   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
       
 81483   if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0;
       
 81484   pEList = pExpr->x.pList;
       
 81485   if( pEList==0 || pEList->nExpr!=1 ) return 0;
       
 81486   if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL;
       
 81487   assert( !ExprHasProperty(pExpr, EP_IntValue) );
       
 81488   if( sqlite3StrICmp(pExpr->u.zToken,"min")==0 ){
       
 81489     return WHERE_ORDERBY_MIN;
       
 81490   }else if( sqlite3StrICmp(pExpr->u.zToken,"max")==0 ){
       
 81491     return WHERE_ORDERBY_MAX;
       
 81492   }
       
 81493   return WHERE_ORDERBY_NORMAL;
       
 81494 }
       
 81495 
       
 81496 /*
       
 81497 ** The select statement passed as the first argument is an aggregate query.
       
 81498 ** The second argment is the associated aggregate-info object. This 
       
 81499 ** function tests if the SELECT is of the form:
       
 81500 **
       
 81501 **   SELECT count(*) FROM <tbl>
       
 81502 **
       
 81503 ** where table is a database table, not a sub-select or view. If the query
       
 81504 ** does match this pattern, then a pointer to the Table object representing
       
 81505 ** <tbl> is returned. Otherwise, 0 is returned.
       
 81506 */
       
 81507 static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
       
 81508   Table *pTab;
       
 81509   Expr *pExpr;
       
 81510 
       
 81511   assert( !p->pGroupBy );
       
 81512 
       
 81513   if( p->pWhere || p->pEList->nExpr!=1 
       
 81514    || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
       
 81515   ){
       
 81516     return 0;
       
 81517   }
       
 81518   pTab = p->pSrc->a[0].pTab;
       
 81519   pExpr = p->pEList->a[0].pExpr;
       
 81520   assert( pTab && !pTab->pSelect && pExpr );
       
 81521 
       
 81522   if( IsVirtual(pTab) ) return 0;
       
 81523   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
       
 81524   if( (pAggInfo->aFunc[0].pFunc->flags&SQLITE_FUNC_COUNT)==0 ) return 0;
       
 81525   if( pExpr->flags&EP_Distinct ) return 0;
       
 81526 
       
 81527   return pTab;
       
 81528 }
       
 81529 
       
 81530 /*
       
 81531 ** If the source-list item passed as an argument was augmented with an
       
 81532 ** INDEXED BY clause, then try to locate the specified index. If there
       
 81533 ** was such a clause and the named index cannot be found, return 
       
 81534 ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate 
       
 81535 ** pFrom->pIndex and return SQLITE_OK.
       
 81536 */
       
 81537 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
       
 81538   if( pFrom->pTab && pFrom->zIndex ){
       
 81539     Table *pTab = pFrom->pTab;
       
 81540     char *zIndex = pFrom->zIndex;
       
 81541     Index *pIdx;
       
 81542     for(pIdx=pTab->pIndex; 
       
 81543         pIdx && sqlite3StrICmp(pIdx->zName, zIndex); 
       
 81544         pIdx=pIdx->pNext
       
 81545     );
       
 81546     if( !pIdx ){
       
 81547       sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
       
 81548       return SQLITE_ERROR;
       
 81549     }
       
 81550     pFrom->pIndex = pIdx;
       
 81551   }
       
 81552   return SQLITE_OK;
       
 81553 }
       
 81554 
       
 81555 /*
       
 81556 ** This routine is a Walker callback for "expanding" a SELECT statement.
       
 81557 ** "Expanding" means to do the following:
       
 81558 **
       
 81559 **    (1)  Make sure VDBE cursor numbers have been assigned to every
       
 81560 **         element of the FROM clause.
       
 81561 **
       
 81562 **    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that 
       
 81563 **         defines FROM clause.  When views appear in the FROM clause,
       
 81564 **         fill pTabList->a[].pSelect with a copy of the SELECT statement
       
 81565 **         that implements the view.  A copy is made of the view's SELECT
       
 81566 **         statement so that we can freely modify or delete that statement
       
 81567 **         without worrying about messing up the presistent representation
       
 81568 **         of the view.
       
 81569 **
       
 81570 **    (3)  Add terms to the WHERE clause to accomodate the NATURAL keyword
       
 81571 **         on joins and the ON and USING clause of joins.
       
 81572 **
       
 81573 **    (4)  Scan the list of columns in the result set (pEList) looking
       
 81574 **         for instances of the "*" operator or the TABLE.* operator.
       
 81575 **         If found, expand each "*" to be every column in every table
       
 81576 **         and TABLE.* to be every column in TABLE.
       
 81577 **
       
 81578 */
       
 81579 static int selectExpander(Walker *pWalker, Select *p){
       
 81580   Parse *pParse = pWalker->pParse;
       
 81581   int i, j, k;
       
 81582   SrcList *pTabList;
       
 81583   ExprList *pEList;
       
 81584   struct SrcList_item *pFrom;
       
 81585   sqlite3 *db = pParse->db;
       
 81586 
       
 81587   if( db->mallocFailed  ){
       
 81588     return WRC_Abort;
       
 81589   }
       
 81590   if( NEVER(p->pSrc==0) || (p->selFlags & SF_Expanded)!=0 ){
       
 81591     return WRC_Prune;
       
 81592   }
       
 81593   p->selFlags |= SF_Expanded;
       
 81594   pTabList = p->pSrc;
       
 81595   pEList = p->pEList;
       
 81596 
       
 81597   /* Make sure cursor numbers have been assigned to all entries in
       
 81598   ** the FROM clause of the SELECT statement.
       
 81599   */
       
 81600   sqlite3SrcListAssignCursors(pParse, pTabList);
       
 81601 
       
 81602   /* Look up every table named in the FROM clause of the select.  If
       
 81603   ** an entry of the FROM clause is a subquery instead of a table or view,
       
 81604   ** then create a transient table structure to describe the subquery.
       
 81605   */
       
 81606   for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
       
 81607     Table *pTab;
       
 81608     if( pFrom->pTab!=0 ){
       
 81609       /* This statement has already been prepared.  There is no need
       
 81610       ** to go further. */
       
 81611       assert( i==0 );
       
 81612       return WRC_Prune;
       
 81613     }
       
 81614     if( pFrom->zName==0 ){
       
 81615 #ifndef SQLITE_OMIT_SUBQUERY
       
 81616       Select *pSel = pFrom->pSelect;
       
 81617       /* A sub-query in the FROM clause of a SELECT */
       
 81618       assert( pSel!=0 );
       
 81619       assert( pFrom->pTab==0 );
       
 81620       sqlite3WalkSelect(pWalker, pSel);
       
 81621       pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
       
 81622       if( pTab==0 ) return WRC_Abort;
       
 81623       pTab->dbMem = db->lookaside.bEnabled ? db : 0;
       
 81624       pTab->nRef = 1;
       
 81625       pTab->zName = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pTab);
       
 81626       while( pSel->pPrior ){ pSel = pSel->pPrior; }
       
 81627       selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
       
 81628       pTab->iPKey = -1;
       
 81629       pTab->tabFlags |= TF_Ephemeral;
       
 81630 #endif
       
 81631     }else{
       
 81632       /* An ordinary table or view name in the FROM clause */
       
 81633       assert( pFrom->pTab==0 );
       
 81634       pFrom->pTab = pTab = 
       
 81635         sqlite3LocateTable(pParse,0,pFrom->zName,pFrom->zDatabase);
       
 81636       if( pTab==0 ) return WRC_Abort;
       
 81637       pTab->nRef++;
       
 81638 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
       
 81639       if( pTab->pSelect || IsVirtual(pTab) ){
       
 81640         /* We reach here if the named table is a really a view */
       
 81641         if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
       
 81642         assert( pFrom->pSelect==0 );
       
 81643         pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
       
 81644         sqlite3WalkSelect(pWalker, pFrom->pSelect);
       
 81645       }
       
 81646 #endif
       
 81647     }
       
 81648 
       
 81649     /* Locate the index named by the INDEXED BY clause, if any. */
       
 81650     if( sqlite3IndexedByLookup(pParse, pFrom) ){
       
 81651       return WRC_Abort;
       
 81652     }
       
 81653   }
       
 81654 
       
 81655   /* Process NATURAL keywords, and ON and USING clauses of joins.
       
 81656   */
       
 81657   if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
       
 81658     return WRC_Abort;
       
 81659   }
       
 81660 
       
 81661   /* For every "*" that occurs in the column list, insert the names of
       
 81662   ** all columns in all tables.  And for every TABLE.* insert the names
       
 81663   ** of all columns in TABLE.  The parser inserted a special expression
       
 81664   ** with the TK_ALL operator for each "*" that it found in the column list.
       
 81665   ** The following code just has to locate the TK_ALL expressions and expand
       
 81666   ** each one to the list of all columns in all tables.
       
 81667   **
       
 81668   ** The first loop just checks to see if there are any "*" operators
       
 81669   ** that need expanding.
       
 81670   */
       
 81671   for(k=0; k<pEList->nExpr; k++){
       
 81672     Expr *pE = pEList->a[k].pExpr;
       
 81673     if( pE->op==TK_ALL ) break;
       
 81674     assert( pE->op!=TK_DOT || pE->pRight!=0 );
       
 81675     assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
       
 81676     if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
       
 81677   }
       
 81678   if( k<pEList->nExpr ){
       
 81679     /*
       
 81680     ** If we get here it means the result set contains one or more "*"
       
 81681     ** operators that need to be expanded.  Loop through each expression
       
 81682     ** in the result set and expand them one by one.
       
 81683     */
       
 81684     struct ExprList_item *a = pEList->a;
       
 81685     ExprList *pNew = 0;
       
 81686     int flags = pParse->db->flags;
       
 81687     int longNames = (flags & SQLITE_FullColNames)!=0
       
 81688                       && (flags & SQLITE_ShortColNames)==0;
       
 81689 
       
 81690     for(k=0; k<pEList->nExpr; k++){
       
 81691       Expr *pE = a[k].pExpr;
       
 81692       assert( pE->op!=TK_DOT || pE->pRight!=0 );
       
 81693       if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){
       
 81694         /* This particular expression does not need to be expanded.
       
 81695         */
       
 81696         pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
       
 81697         if( pNew ){
       
 81698           pNew->a[pNew->nExpr-1].zName = a[k].zName;
       
 81699           pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
       
 81700           a[k].zName = 0;
       
 81701           a[k].zSpan = 0;
       
 81702         }
       
 81703         a[k].pExpr = 0;
       
 81704       }else{
       
 81705         /* This expression is a "*" or a "TABLE.*" and needs to be
       
 81706         ** expanded. */
       
 81707         int tableSeen = 0;      /* Set to 1 when TABLE matches */
       
 81708         char *zTName;            /* text of name of TABLE */
       
 81709         if( pE->op==TK_DOT ){
       
 81710           assert( pE->pLeft!=0 );
       
 81711           assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
       
 81712           zTName = pE->pLeft->u.zToken;
       
 81713         }else{
       
 81714           zTName = 0;
       
 81715         }
       
 81716         for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
       
 81717           Table *pTab = pFrom->pTab;
       
 81718           char *zTabName = pFrom->zAlias;
       
 81719           if( zTabName==0 ){
       
 81720             zTabName = pTab->zName;
       
 81721           }
       
 81722           if( db->mallocFailed ) break;
       
 81723           if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
       
 81724             continue;
       
 81725           }
       
 81726           tableSeen = 1;
       
 81727           for(j=0; j<pTab->nCol; j++){
       
 81728             Expr *pExpr, *pRight;
       
 81729             char *zName = pTab->aCol[j].zName;
       
 81730             char *zColname;  /* The computed column name */
       
 81731             char *zToFree;   /* Malloced string that needs to be freed */
       
 81732             Token sColname;  /* Computed column name as a token */
       
 81733 
       
 81734             /* If a column is marked as 'hidden' (currently only possible
       
 81735             ** for virtual tables), do not include it in the expanded
       
 81736             ** result-set list.
       
 81737             */
       
 81738             if( IsHiddenColumn(&pTab->aCol[j]) ){
       
 81739               assert(IsVirtual(pTab));
       
 81740               continue;
       
 81741             }
       
 81742 
       
 81743             if( i>0 && zTName==0 ){
       
 81744               struct SrcList_item *pLeft = &pTabList->a[i-1];
       
 81745               if( (pLeft[1].jointype & JT_NATURAL)!=0 &&
       
 81746                         columnIndex(pLeft->pTab, zName)>=0 ){
       
 81747                 /* In a NATURAL join, omit the join columns from the 
       
 81748                 ** table on the right */
       
 81749                 continue;
       
 81750               }
       
 81751               if( sqlite3IdListIndex(pLeft[1].pUsing, zName)>=0 ){
       
 81752                 /* In a join with a USING clause, omit columns in the
       
 81753                 ** using clause from the table on the right. */
       
 81754                 continue;
       
 81755               }
       
 81756             }
       
 81757             pRight = sqlite3Expr(db, TK_ID, zName);
       
 81758             zColname = zName;
       
 81759             zToFree = 0;
       
 81760             if( longNames || pTabList->nSrc>1 ){
       
 81761               Expr *pLeft;
       
 81762               pLeft = sqlite3Expr(db, TK_ID, zTabName);
       
 81763               pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
       
 81764               if( longNames ){
       
 81765                 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
       
 81766                 zToFree = zColname;
       
 81767               }
       
 81768             }else{
       
 81769               pExpr = pRight;
       
 81770             }
       
 81771             pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
       
 81772             sColname.z = zColname;
       
 81773             sColname.n = sqlite3Strlen30(zColname);
       
 81774             sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
       
 81775             sqlite3DbFree(db, zToFree);
       
 81776           }
       
 81777         }
       
 81778         if( !tableSeen ){
       
 81779           if( zTName ){
       
 81780             sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
       
 81781           }else{
       
 81782             sqlite3ErrorMsg(pParse, "no tables specified");
       
 81783           }
       
 81784         }
       
 81785       }
       
 81786     }
       
 81787     sqlite3ExprListDelete(db, pEList);
       
 81788     p->pEList = pNew;
       
 81789   }
       
 81790 #if SQLITE_MAX_COLUMN
       
 81791   if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
       
 81792     sqlite3ErrorMsg(pParse, "too many columns in result set");
       
 81793   }
       
 81794 #endif
       
 81795   return WRC_Continue;
       
 81796 }
       
 81797 
       
 81798 /*
       
 81799 ** No-op routine for the parse-tree walker.
       
 81800 **
       
 81801 ** When this routine is the Walker.xExprCallback then expression trees
       
 81802 ** are walked without any actions being taken at each node.  Presumably,
       
 81803 ** when this routine is used for Walker.xExprCallback then 
       
 81804 ** Walker.xSelectCallback is set to do something useful for every 
       
 81805 ** subquery in the parser tree.
       
 81806 */
       
 81807 static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
       
 81808   UNUSED_PARAMETER2(NotUsed, NotUsed2);
       
 81809   return WRC_Continue;
       
 81810 }
       
 81811 
       
 81812 /*
       
 81813 ** This routine "expands" a SELECT statement and all of its subqueries.
       
 81814 ** For additional information on what it means to "expand" a SELECT
       
 81815 ** statement, see the comment on the selectExpand worker callback above.
       
 81816 **
       
 81817 ** Expanding a SELECT statement is the first step in processing a
       
 81818 ** SELECT statement.  The SELECT statement must be expanded before
       
 81819 ** name resolution is performed.
       
 81820 **
       
 81821 ** If anything goes wrong, an error message is written into pParse.
       
 81822 ** The calling function can detect the problem by looking at pParse->nErr
       
 81823 ** and/or pParse->db->mallocFailed.
       
 81824 */
       
 81825 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
       
 81826   Walker w;
       
 81827   w.xSelectCallback = selectExpander;
       
 81828   w.xExprCallback = exprWalkNoop;
       
 81829   w.pParse = pParse;
       
 81830   sqlite3WalkSelect(&w, pSelect);
       
 81831 }
       
 81832 
       
 81833 
       
 81834 #ifndef SQLITE_OMIT_SUBQUERY
       
 81835 /*
       
 81836 ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
       
 81837 ** interface.
       
 81838 **
       
 81839 ** For each FROM-clause subquery, add Column.zType and Column.zColl
       
 81840 ** information to the Table structure that represents the result set
       
 81841 ** of that subquery.
       
 81842 **
       
 81843 ** The Table structure that represents the result set was constructed
       
 81844 ** by selectExpander() but the type and collation information was omitted
       
 81845 ** at that point because identifiers had not yet been resolved.  This
       
 81846 ** routine is called after identifier resolution.
       
 81847 */
       
 81848 static int selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
       
 81849   Parse *pParse;
       
 81850   int i;
       
 81851   SrcList *pTabList;
       
 81852   struct SrcList_item *pFrom;
       
 81853 
       
 81854   assert( p->selFlags & SF_Resolved );
       
 81855   assert( (p->selFlags & SF_HasTypeInfo)==0 );
       
 81856   p->selFlags |= SF_HasTypeInfo;
       
 81857   pParse = pWalker->pParse;
       
 81858   pTabList = p->pSrc;
       
 81859   for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
       
 81860     Table *pTab = pFrom->pTab;
       
 81861     if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
       
 81862       /* A sub-query in the FROM clause of a SELECT */
       
 81863       Select *pSel = pFrom->pSelect;
       
 81864       assert( pSel );
       
 81865       while( pSel->pPrior ) pSel = pSel->pPrior;
       
 81866       selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
       
 81867     }
       
 81868   }
       
 81869   return WRC_Continue;
       
 81870 }
       
 81871 #endif
       
 81872 
       
 81873 
       
 81874 /*
       
 81875 ** This routine adds datatype and collating sequence information to
       
 81876 ** the Table structures of all FROM-clause subqueries in a
       
 81877 ** SELECT statement.
       
 81878 **
       
 81879 ** Use this routine after name resolution.
       
 81880 */
       
 81881 static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
       
 81882 #ifndef SQLITE_OMIT_SUBQUERY
       
 81883   Walker w;
       
 81884   w.xSelectCallback = selectAddSubqueryTypeInfo;
       
 81885   w.xExprCallback = exprWalkNoop;
       
 81886   w.pParse = pParse;
       
 81887   sqlite3WalkSelect(&w, pSelect);
       
 81888 #endif
       
 81889 }
       
 81890 
       
 81891 
       
 81892 /*
       
 81893 ** This routine sets of a SELECT statement for processing.  The
       
 81894 ** following is accomplished:
       
 81895 **
       
 81896 **     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
       
 81897 **     *  Ephemeral Table objects are created for all FROM-clause subqueries.
       
 81898 **     *  ON and USING clauses are shifted into WHERE statements
       
 81899 **     *  Wildcards "*" and "TABLE.*" in result sets are expanded.
       
 81900 **     *  Identifiers in expression are matched to tables.
       
 81901 **
       
 81902 ** This routine acts recursively on all subqueries within the SELECT.
       
 81903 */
       
 81904 SQLITE_PRIVATE void sqlite3SelectPrep(
       
 81905   Parse *pParse,         /* The parser context */
       
 81906   Select *p,             /* The SELECT statement being coded. */
       
 81907   NameContext *pOuterNC  /* Name context for container */
       
 81908 ){
       
 81909   sqlite3 *db;
       
 81910   if( NEVER(p==0) ) return;
       
 81911   db = pParse->db;
       
 81912   if( p->selFlags & SF_HasTypeInfo ) return;
       
 81913   sqlite3SelectExpand(pParse, p);
       
 81914   if( pParse->nErr || db->mallocFailed ) return;
       
 81915   sqlite3ResolveSelectNames(pParse, p, pOuterNC);
       
 81916   if( pParse->nErr || db->mallocFailed ) return;
       
 81917   sqlite3SelectAddTypeInfo(pParse, p);
       
 81918 }
       
 81919 
       
 81920 /*
       
 81921 ** Reset the aggregate accumulator.
       
 81922 **
       
 81923 ** The aggregate accumulator is a set of memory cells that hold
       
 81924 ** intermediate results while calculating an aggregate.  This
       
 81925 ** routine simply stores NULLs in all of those memory cells.
       
 81926 */
       
 81927 static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
       
 81928   Vdbe *v = pParse->pVdbe;
       
 81929   int i;
       
 81930   struct AggInfo_func *pFunc;
       
 81931   if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){
       
 81932     return;
       
 81933   }
       
 81934   for(i=0; i<pAggInfo->nColumn; i++){
       
 81935     sqlite3VdbeAddOp2(v, OP_Null, 0, pAggInfo->aCol[i].iMem);
       
 81936   }
       
 81937   for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
       
 81938     sqlite3VdbeAddOp2(v, OP_Null, 0, pFunc->iMem);
       
 81939     if( pFunc->iDistinct>=0 ){
       
 81940       Expr *pE = pFunc->pExpr;
       
 81941       assert( !ExprHasProperty(pE, EP_xIsSelect) );
       
 81942       if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
       
 81943         sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
       
 81944            "argument");
       
 81945         pFunc->iDistinct = -1;
       
 81946       }else{
       
 81947         KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList);
       
 81948         sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
       
 81949                           (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
       
 81950       }
       
 81951     }
       
 81952   }
       
 81953 }
       
 81954 
       
 81955 /*
       
 81956 ** Invoke the OP_AggFinalize opcode for every aggregate function
       
 81957 ** in the AggInfo structure.
       
 81958 */
       
 81959 static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
       
 81960   Vdbe *v = pParse->pVdbe;
       
 81961   int i;
       
 81962   struct AggInfo_func *pF;
       
 81963   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
       
 81964     ExprList *pList = pF->pExpr->x.pList;
       
 81965     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
       
 81966     sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
       
 81967                       (void*)pF->pFunc, P4_FUNCDEF);
       
 81968   }
       
 81969 }
       
 81970 
       
 81971 /*
       
 81972 ** Update the accumulator memory cells for an aggregate based on
       
 81973 ** the current cursor position.
       
 81974 */
       
 81975 static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
       
 81976   Vdbe *v = pParse->pVdbe;
       
 81977   int i;
       
 81978   struct AggInfo_func *pF;
       
 81979   struct AggInfo_col *pC;
       
 81980 
       
 81981   pAggInfo->directMode = 1;
       
 81982   sqlite3ExprCacheClear(pParse);
       
 81983   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
       
 81984     int nArg;
       
 81985     int addrNext = 0;
       
 81986     int regAgg;
       
 81987     ExprList *pList = pF->pExpr->x.pList;
       
 81988     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
       
 81989     if( pList ){
       
 81990       nArg = pList->nExpr;
       
 81991       regAgg = sqlite3GetTempRange(pParse, nArg);
       
 81992       sqlite3ExprCodeExprList(pParse, pList, regAgg, 0);
       
 81993     }else{
       
 81994       nArg = 0;
       
 81995       regAgg = 0;
       
 81996     }
       
 81997     if( pF->iDistinct>=0 ){
       
 81998       addrNext = sqlite3VdbeMakeLabel(v);
       
 81999       assert( nArg==1 );
       
 82000       codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
       
 82001     }
       
 82002     if( pF->pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
       
 82003       CollSeq *pColl = 0;
       
 82004       struct ExprList_item *pItem;
       
 82005       int j;
       
 82006       assert( pList!=0 );  /* pList!=0 if pF->pFunc has NEEDCOLL */
       
 82007       for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
       
 82008         pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
       
 82009       }
       
 82010       if( !pColl ){
       
 82011         pColl = pParse->db->pDfltColl;
       
 82012       }
       
 82013       sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
       
 82014     }
       
 82015     sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
       
 82016                       (void*)pF->pFunc, P4_FUNCDEF);
       
 82017     sqlite3VdbeChangeP5(v, (u8)nArg);
       
 82018     sqlite3ReleaseTempRange(pParse, regAgg, nArg);
       
 82019     sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
       
 82020     if( addrNext ){
       
 82021       sqlite3VdbeResolveLabel(v, addrNext);
       
 82022       sqlite3ExprCacheClear(pParse);
       
 82023     }
       
 82024   }
       
 82025   for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
       
 82026     sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
       
 82027   }
       
 82028   pAggInfo->directMode = 0;
       
 82029   sqlite3ExprCacheClear(pParse);
       
 82030 }
       
 82031 
       
 82032 /*
       
 82033 ** Generate code for the SELECT statement given in the p argument.  
       
 82034 **
       
 82035 ** The results are distributed in various ways depending on the
       
 82036 ** contents of the SelectDest structure pointed to by argument pDest
       
 82037 ** as follows:
       
 82038 **
       
 82039 **     pDest->eDest    Result
       
 82040 **     ------------    -------------------------------------------
       
 82041 **     SRT_Output      Generate a row of output (using the OP_ResultRow
       
 82042 **                     opcode) for each row in the result set.
       
 82043 **
       
 82044 **     SRT_Mem         Only valid if the result is a single column.
       
 82045 **                     Store the first column of the first result row
       
 82046 **                     in register pDest->iParm then abandon the rest
       
 82047 **                     of the query.  This destination implies "LIMIT 1".
       
 82048 **
       
 82049 **     SRT_Set         The result must be a single column.  Store each
       
 82050 **                     row of result as the key in table pDest->iParm. 
       
 82051 **                     Apply the affinity pDest->affinity before storing
       
 82052 **                     results.  Used to implement "IN (SELECT ...)".
       
 82053 **
       
 82054 **     SRT_Union       Store results as a key in a temporary table pDest->iParm.
       
 82055 **
       
 82056 **     SRT_Except      Remove results from the temporary table pDest->iParm.
       
 82057 **
       
 82058 **     SRT_Table       Store results in temporary table pDest->iParm.
       
 82059 **                     This is like SRT_EphemTab except that the table
       
 82060 **                     is assumed to already be open.
       
 82061 **
       
 82062 **     SRT_EphemTab    Create an temporary table pDest->iParm and store
       
 82063 **                     the result there. The cursor is left open after
       
 82064 **                     returning.  This is like SRT_Table except that
       
 82065 **                     this destination uses OP_OpenEphemeral to create
       
 82066 **                     the table first.
       
 82067 **
       
 82068 **     SRT_Coroutine   Generate a co-routine that returns a new row of
       
 82069 **                     results each time it is invoked.  The entry point
       
 82070 **                     of the co-routine is stored in register pDest->iParm.
       
 82071 **
       
 82072 **     SRT_Exists      Store a 1 in memory cell pDest->iParm if the result
       
 82073 **                     set is not empty.
       
 82074 **
       
 82075 **     SRT_Discard     Throw the results away.  This is used by SELECT
       
 82076 **                     statements within triggers whose only purpose is
       
 82077 **                     the side-effects of functions.
       
 82078 **
       
 82079 ** This routine returns the number of errors.  If any errors are
       
 82080 ** encountered, then an appropriate error message is left in
       
 82081 ** pParse->zErrMsg.
       
 82082 **
       
 82083 ** This routine does NOT free the Select structure passed in.  The
       
 82084 ** calling function needs to do that.
       
 82085 */
       
 82086 SQLITE_PRIVATE int sqlite3Select(
       
 82087   Parse *pParse,         /* The parser context */
       
 82088   Select *p,             /* The SELECT statement being coded. */
       
 82089   SelectDest *pDest      /* What to do with the query results */
       
 82090 ){
       
 82091   int i, j;              /* Loop counters */
       
 82092   WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
       
 82093   Vdbe *v;               /* The virtual machine under construction */
       
 82094   int isAgg;             /* True for select lists like "count(*)" */
       
 82095   ExprList *pEList;      /* List of columns to extract. */
       
 82096   SrcList *pTabList;     /* List of tables to select from */
       
 82097   Expr *pWhere;          /* The WHERE clause.  May be NULL */
       
 82098   ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
       
 82099   ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
       
 82100   Expr *pHaving;         /* The HAVING clause.  May be NULL */
       
 82101   int isDistinct;        /* True if the DISTINCT keyword is present */
       
 82102   int distinct;          /* Table to use for the distinct set */
       
 82103   int rc = 1;            /* Value to return from this function */
       
 82104   int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
       
 82105   AggInfo sAggInfo;      /* Information used by aggregate queries */
       
 82106   int iEnd;              /* Address of the end of the query */
       
 82107   sqlite3 *db;           /* The database connection */
       
 82108 
       
 82109   db = pParse->db;
       
 82110   if( p==0 || db->mallocFailed || pParse->nErr ){
       
 82111     return 1;
       
 82112   }
       
 82113   if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
       
 82114   memset(&sAggInfo, 0, sizeof(sAggInfo));
       
 82115 
       
 82116   if( IgnorableOrderby(pDest) ){
       
 82117     assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || 
       
 82118            pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard);
       
 82119     /* If ORDER BY makes no difference in the output then neither does
       
 82120     ** DISTINCT so it can be removed too. */
       
 82121     sqlite3ExprListDelete(db, p->pOrderBy);
       
 82122     p->pOrderBy = 0;
       
 82123     p->selFlags &= ~SF_Distinct;
       
 82124   }
       
 82125   sqlite3SelectPrep(pParse, p, 0);
       
 82126   pOrderBy = p->pOrderBy;
       
 82127   pTabList = p->pSrc;
       
 82128   pEList = p->pEList;
       
 82129   if( pParse->nErr || db->mallocFailed ){
       
 82130     goto select_end;
       
 82131   }
       
 82132   isAgg = (p->selFlags & SF_Aggregate)!=0;
       
 82133   assert( pEList!=0 );
       
 82134 
       
 82135   /* Begin generating code.
       
 82136   */
       
 82137   v = sqlite3GetVdbe(pParse);
       
 82138   if( v==0 ) goto select_end;
       
 82139 
       
 82140   /* Generate code for all sub-queries in the FROM clause
       
 82141   */
       
 82142 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
       
 82143   for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
       
 82144     struct SrcList_item *pItem = &pTabList->a[i];
       
 82145     SelectDest dest;
       
 82146     Select *pSub = pItem->pSelect;
       
 82147     int isAggSub;
       
 82148 
       
 82149     if( pSub==0 || pItem->isPopulated ) continue;
       
 82150 
       
 82151     /* Increment Parse.nHeight by the height of the largest expression
       
 82152     ** tree refered to by this, the parent select. The child select
       
 82153     ** may contain expression trees of at most
       
 82154     ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
       
 82155     ** more conservative than necessary, but much easier than enforcing
       
 82156     ** an exact limit.
       
 82157     */
       
 82158     pParse->nHeight += sqlite3SelectExprHeight(p);
       
 82159 
       
 82160     /* Check to see if the subquery can be absorbed into the parent. */
       
 82161     isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
       
 82162     if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
       
 82163       if( isAggSub ){
       
 82164         isAgg = 1;
       
 82165         p->selFlags |= SF_Aggregate;
       
 82166       }
       
 82167       i = -1;
       
 82168     }else{
       
 82169       sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
       
 82170       assert( pItem->isPopulated==0 );
       
 82171       sqlite3Select(pParse, pSub, &dest);
       
 82172       pItem->isPopulated = 1;
       
 82173     }
       
 82174     if( /*pParse->nErr ||*/ db->mallocFailed ){
       
 82175       goto select_end;
       
 82176     }
       
 82177     pParse->nHeight -= sqlite3SelectExprHeight(p);
       
 82178     pTabList = p->pSrc;
       
 82179     if( !IgnorableOrderby(pDest) ){
       
 82180       pOrderBy = p->pOrderBy;
       
 82181     }
       
 82182   }
       
 82183   pEList = p->pEList;
       
 82184 #endif
       
 82185   pWhere = p->pWhere;
       
 82186   pGroupBy = p->pGroupBy;
       
 82187   pHaving = p->pHaving;
       
 82188   isDistinct = (p->selFlags & SF_Distinct)!=0;
       
 82189 
       
 82190 #ifndef SQLITE_OMIT_COMPOUND_SELECT
       
 82191   /* If there is are a sequence of queries, do the earlier ones first.
       
 82192   */
       
 82193   if( p->pPrior ){
       
 82194     if( p->pRightmost==0 ){
       
 82195       Select *pLoop, *pRight = 0;
       
 82196       int cnt = 0;
       
 82197       int mxSelect;
       
 82198       for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
       
 82199         pLoop->pRightmost = p;
       
 82200         pLoop->pNext = pRight;
       
 82201         pRight = pLoop;
       
 82202       }
       
 82203       mxSelect = db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
       
 82204       if( mxSelect && cnt>mxSelect ){
       
 82205         sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
       
 82206         return 1;
       
 82207       }
       
 82208     }
       
 82209     return multiSelect(pParse, p, pDest);
       
 82210   }
       
 82211 #endif
       
 82212 
       
 82213   /* If writing to memory or generating a set
       
 82214   ** only a single column may be output.
       
 82215   */
       
 82216 #ifndef SQLITE_OMIT_SUBQUERY
       
 82217   if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
       
 82218     goto select_end;
       
 82219   }
       
 82220 #endif
       
 82221 
       
 82222   /* If possible, rewrite the query to use GROUP BY instead of DISTINCT.
       
 82223   ** GROUP BY might use an index, DISTINCT never does.
       
 82224   */
       
 82225   assert( p->pGroupBy==0 || (p->selFlags & SF_Aggregate)!=0 );
       
 82226   if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ){
       
 82227     p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
       
 82228     pGroupBy = p->pGroupBy;
       
 82229     p->selFlags &= ~SF_Distinct;
       
 82230     isDistinct = 0;
       
 82231   }
       
 82232 
       
 82233   /* If there is an ORDER BY clause, then this sorting
       
 82234   ** index might end up being unused if the data can be 
       
 82235   ** extracted in pre-sorted order.  If that is the case, then the
       
 82236   ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
       
 82237   ** we figure out that the sorting index is not needed.  The addrSortIndex
       
 82238   ** variable is used to facilitate that change.
       
 82239   */
       
 82240   if( pOrderBy ){
       
 82241     KeyInfo *pKeyInfo;
       
 82242     pKeyInfo = keyInfoFromExprList(pParse, pOrderBy);
       
 82243     pOrderBy->iECursor = pParse->nTab++;
       
 82244     p->addrOpenEphm[2] = addrSortIndex =
       
 82245       sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
       
 82246                            pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
       
 82247                            (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
       
 82248   }else{
       
 82249     addrSortIndex = -1;
       
 82250   }
       
 82251 
       
 82252   /* If the output is destined for a temporary table, open that table.
       
 82253   */
       
 82254   if( pDest->eDest==SRT_EphemTab ){
       
 82255     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iParm, pEList->nExpr);
       
 82256   }
       
 82257 
       
 82258   /* Set the limiter.
       
 82259   */
       
 82260   iEnd = sqlite3VdbeMakeLabel(v);
       
 82261   computeLimitRegisters(pParse, p, iEnd);
       
 82262 
       
 82263   /* Open a virtual index to use for the distinct set.
       
 82264   */
       
 82265   if( isDistinct ){
       
 82266     KeyInfo *pKeyInfo;
       
 82267     assert( isAgg || pGroupBy );
       
 82268     distinct = pParse->nTab++;
       
 82269     pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
       
 82270     sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0,
       
 82271                         (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
       
 82272   }else{
       
 82273     distinct = -1;
       
 82274   }
       
 82275 
       
 82276   /* Aggregate and non-aggregate queries are handled differently */
       
 82277   if( !isAgg && pGroupBy==0 ){
       
 82278     /* This case is for non-aggregate queries
       
 82279     ** Begin the database scan
       
 82280     */
       
 82281     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, 0);
       
 82282     if( pWInfo==0 ) goto select_end;
       
 82283 
       
 82284     /* If sorting index that was created by a prior OP_OpenEphemeral 
       
 82285     ** instruction ended up not being needed, then change the OP_OpenEphemeral
       
 82286     ** into an OP_Noop.
       
 82287     */
       
 82288     if( addrSortIndex>=0 && pOrderBy==0 ){
       
 82289       sqlite3VdbeChangeToNoop(v, addrSortIndex, 1);
       
 82290       p->addrOpenEphm[2] = -1;
       
 82291     }
       
 82292 
       
 82293     /* Use the standard inner loop
       
 82294     */
       
 82295     assert(!isDistinct);
       
 82296     selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, -1, pDest,
       
 82297                     pWInfo->iContinue, pWInfo->iBreak);
       
 82298 
       
 82299     /* End the database scan loop.
       
 82300     */
       
 82301     sqlite3WhereEnd(pWInfo);
       
 82302   }else{
       
 82303     /* This is the processing for aggregate queries */
       
 82304     NameContext sNC;    /* Name context for processing aggregate information */
       
 82305     int iAMem;          /* First Mem address for storing current GROUP BY */
       
 82306     int iBMem;          /* First Mem address for previous GROUP BY */
       
 82307     int iUseFlag;       /* Mem address holding flag indicating that at least
       
 82308                         ** one row of the input to the aggregator has been
       
 82309                         ** processed */
       
 82310     int iAbortFlag;     /* Mem address which causes query abort if positive */
       
 82311     int groupBySort;    /* Rows come from source in GROUP BY order */
       
 82312     int addrEnd;        /* End of processing for this SELECT */
       
 82313 
       
 82314     /* Remove any and all aliases between the result set and the
       
 82315     ** GROUP BY clause.
       
 82316     */
       
 82317     if( pGroupBy ){
       
 82318       int k;                        /* Loop counter */
       
 82319       struct ExprList_item *pItem;  /* For looping over expression in a list */
       
 82320 
       
 82321       for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
       
 82322         pItem->iAlias = 0;
       
 82323       }
       
 82324       for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
       
 82325         pItem->iAlias = 0;
       
 82326       }
       
 82327     }
       
 82328 
       
 82329  
       
 82330     /* Create a label to jump to when we want to abort the query */
       
 82331     addrEnd = sqlite3VdbeMakeLabel(v);
       
 82332 
       
 82333     /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
       
 82334     ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
       
 82335     ** SELECT statement.
       
 82336     */
       
 82337     memset(&sNC, 0, sizeof(sNC));
       
 82338     sNC.pParse = pParse;
       
 82339     sNC.pSrcList = pTabList;
       
 82340     sNC.pAggInfo = &sAggInfo;
       
 82341     sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
       
 82342     sAggInfo.pGroupBy = pGroupBy;
       
 82343     sqlite3ExprAnalyzeAggList(&sNC, pEList);
       
 82344     sqlite3ExprAnalyzeAggList(&sNC, pOrderBy);
       
 82345     if( pHaving ){
       
 82346       sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
       
 82347     }
       
 82348     sAggInfo.nAccumulator = sAggInfo.nColumn;
       
 82349     for(i=0; i<sAggInfo.nFunc; i++){
       
 82350       assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
       
 82351       sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
       
 82352     }
       
 82353     if( db->mallocFailed ) goto select_end;
       
 82354 
       
 82355     /* Processing for aggregates with GROUP BY is very different and
       
 82356     ** much more complex than aggregates without a GROUP BY.
       
 82357     */
       
 82358     if( pGroupBy ){
       
 82359       KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
       
 82360       int j1;             /* A-vs-B comparision jump */
       
 82361       int addrOutputRow;  /* Start of subroutine that outputs a result row */
       
 82362       int regOutputRow;   /* Return address register for output subroutine */
       
 82363       int addrSetAbort;   /* Set the abort flag and return */
       
 82364       int addrTopOfLoop;  /* Top of the input loop */
       
 82365       int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
       
 82366       int addrReset;      /* Subroutine for resetting the accumulator */
       
 82367       int regReset;       /* Return address register for reset subroutine */
       
 82368 
       
 82369       /* If there is a GROUP BY clause we might need a sorting index to
       
 82370       ** implement it.  Allocate that sorting index now.  If it turns out
       
 82371       ** that we do not need it after all, the OpenEphemeral instruction
       
 82372       ** will be converted into a Noop.  
       
 82373       */
       
 82374       sAggInfo.sortingIdx = pParse->nTab++;
       
 82375       pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
       
 82376       addrSortingIdx = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, 
       
 82377           sAggInfo.sortingIdx, sAggInfo.nSortingColumn, 
       
 82378           0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
       
 82379 
       
 82380       /* Initialize memory locations used by GROUP BY aggregate processing
       
 82381       */
       
 82382       iUseFlag = ++pParse->nMem;
       
 82383       iAbortFlag = ++pParse->nMem;
       
 82384       regOutputRow = ++pParse->nMem;
       
 82385       addrOutputRow = sqlite3VdbeMakeLabel(v);
       
 82386       regReset = ++pParse->nMem;
       
 82387       addrReset = sqlite3VdbeMakeLabel(v);
       
 82388       iAMem = pParse->nMem + 1;
       
 82389       pParse->nMem += pGroupBy->nExpr;
       
 82390       iBMem = pParse->nMem + 1;
       
 82391       pParse->nMem += pGroupBy->nExpr;
       
 82392       sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
       
 82393       VdbeComment((v, "clear abort flag"));
       
 82394       sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
       
 82395       VdbeComment((v, "indicate accumulator empty"));
       
 82396 
       
 82397       /* Begin a loop that will extract all source rows in GROUP BY order.
       
 82398       ** This might involve two separate loops with an OP_Sort in between, or
       
 82399       ** it might be a single loop that uses an index to extract information
       
 82400       ** in the right order to begin with.
       
 82401       */
       
 82402       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
       
 82403       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0);
       
 82404       if( pWInfo==0 ) goto select_end;
       
 82405       if( pGroupBy==0 ){
       
 82406         /* The optimizer is able to deliver rows in group by order so
       
 82407         ** we do not have to sort.  The OP_OpenEphemeral table will be
       
 82408         ** cancelled later because we still need to use the pKeyInfo
       
 82409         */
       
 82410         pGroupBy = p->pGroupBy;
       
 82411         groupBySort = 0;
       
 82412       }else{
       
 82413         /* Rows are coming out in undetermined order.  We have to push
       
 82414         ** each row into a sorting index, terminate the first loop,
       
 82415         ** then loop over the sorting index in order to get the output
       
 82416         ** in sorted order
       
 82417         */
       
 82418         int regBase;
       
 82419         int regRecord;
       
 82420         int nCol;
       
 82421         int nGroupBy;
       
 82422 
       
 82423         groupBySort = 1;
       
 82424         nGroupBy = pGroupBy->nExpr;
       
 82425         nCol = nGroupBy + 1;
       
 82426         j = nGroupBy+1;
       
 82427         for(i=0; i<sAggInfo.nColumn; i++){
       
 82428           if( sAggInfo.aCol[i].iSorterColumn>=j ){
       
 82429             nCol++;
       
 82430             j++;
       
 82431           }
       
 82432         }
       
 82433         regBase = sqlite3GetTempRange(pParse, nCol);
       
 82434         sqlite3ExprCacheClear(pParse);
       
 82435         sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0);
       
 82436         sqlite3VdbeAddOp2(v, OP_Sequence, sAggInfo.sortingIdx,regBase+nGroupBy);
       
 82437         j = nGroupBy+1;
       
 82438         for(i=0; i<sAggInfo.nColumn; i++){
       
 82439           struct AggInfo_col *pCol = &sAggInfo.aCol[i];
       
 82440           if( pCol->iSorterColumn>=j ){
       
 82441             int r1 = j + regBase;
       
 82442             int r2;
       
 82443 
       
 82444             r2 = sqlite3ExprCodeGetColumn(pParse, 
       
 82445                                pCol->pTab, pCol->iColumn, pCol->iTable, r1, 0);
       
 82446             if( r1!=r2 ){
       
 82447               sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
       
 82448             }
       
 82449             j++;
       
 82450           }
       
 82451         }
       
 82452         regRecord = sqlite3GetTempReg(pParse);
       
 82453         sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
       
 82454         sqlite3VdbeAddOp2(v, OP_IdxInsert, sAggInfo.sortingIdx, regRecord);
       
 82455         sqlite3ReleaseTempReg(pParse, regRecord);
       
 82456         sqlite3ReleaseTempRange(pParse, regBase, nCol);
       
 82457         sqlite3WhereEnd(pWInfo);
       
 82458         sqlite3VdbeAddOp2(v, OP_Sort, sAggInfo.sortingIdx, addrEnd);
       
 82459         VdbeComment((v, "GROUP BY sort"));
       
 82460         sAggInfo.useSortingIdx = 1;
       
 82461         sqlite3ExprCacheClear(pParse);
       
 82462       }
       
 82463 
       
 82464       /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
       
 82465       ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
       
 82466       ** Then compare the current GROUP BY terms against the GROUP BY terms
       
 82467       ** from the previous row currently stored in a0, a1, a2...
       
 82468       */
       
 82469       addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
       
 82470       sqlite3ExprCacheClear(pParse);
       
 82471       for(j=0; j<pGroupBy->nExpr; j++){
       
 82472         if( groupBySort ){
       
 82473           sqlite3VdbeAddOp3(v, OP_Column, sAggInfo.sortingIdx, j, iBMem+j);
       
 82474         }else{
       
 82475           sAggInfo.directMode = 1;
       
 82476           sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
       
 82477         }
       
 82478       }
       
 82479       sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
       
 82480                           (char*)pKeyInfo, P4_KEYINFO);
       
 82481       j1 = sqlite3VdbeCurrentAddr(v);
       
 82482       sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1);
       
 82483 
       
 82484       /* Generate code that runs whenever the GROUP BY changes.
       
 82485       ** Changes in the GROUP BY are detected by the previous code
       
 82486       ** block.  If there were no changes, this block is skipped.
       
 82487       **
       
 82488       ** This code copies current group by terms in b0,b1,b2,...
       
 82489       ** over to a0,a1,a2.  It then calls the output subroutine
       
 82490       ** and resets the aggregate accumulator registers in preparation
       
 82491       ** for the next GROUP BY batch.
       
 82492       */
       
 82493       sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
       
 82494       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
       
 82495       VdbeComment((v, "output one row"));
       
 82496       sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd);
       
 82497       VdbeComment((v, "check abort flag"));
       
 82498       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
       
 82499       VdbeComment((v, "reset accumulator"));
       
 82500 
       
 82501       /* Update the aggregate accumulators based on the content of
       
 82502       ** the current row
       
 82503       */
       
 82504       sqlite3VdbeJumpHere(v, j1);
       
 82505       updateAccumulator(pParse, &sAggInfo);
       
 82506       sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
       
 82507       VdbeComment((v, "indicate data in accumulator"));
       
 82508 
       
 82509       /* End of the loop
       
 82510       */
       
 82511       if( groupBySort ){
       
 82512         sqlite3VdbeAddOp2(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop);
       
 82513       }else{
       
 82514         sqlite3WhereEnd(pWInfo);
       
 82515         sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1);
       
 82516       }
       
 82517 
       
 82518       /* Output the final row of result
       
 82519       */
       
 82520       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
       
 82521       VdbeComment((v, "output final row"));
       
 82522 
       
 82523       /* Jump over the subroutines
       
 82524       */
       
 82525       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd);
       
 82526 
       
 82527       /* Generate a subroutine that outputs a single row of the result
       
 82528       ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
       
 82529       ** is less than or equal to zero, the subroutine is a no-op.  If
       
 82530       ** the processing calls for the query to abort, this subroutine
       
 82531       ** increments the iAbortFlag memory location before returning in
       
 82532       ** order to signal the caller to abort.
       
 82533       */
       
 82534       addrSetAbort = sqlite3VdbeCurrentAddr(v);
       
 82535       sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
       
 82536       VdbeComment((v, "set abort flag"));
       
 82537       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
       
 82538       sqlite3VdbeResolveLabel(v, addrOutputRow);
       
 82539       addrOutputRow = sqlite3VdbeCurrentAddr(v);
       
 82540       sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
       
 82541       VdbeComment((v, "Groupby result generator entry point"));
       
 82542       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
       
 82543       finalizeAggFunctions(pParse, &sAggInfo);
       
 82544       sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
       
 82545       selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
       
 82546                       distinct, pDest,
       
 82547                       addrOutputRow+1, addrSetAbort);
       
 82548       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
       
 82549       VdbeComment((v, "end groupby result generator"));
       
 82550 
       
 82551       /* Generate a subroutine that will reset the group-by accumulator
       
 82552       */
       
 82553       sqlite3VdbeResolveLabel(v, addrReset);
       
 82554       resetAccumulator(pParse, &sAggInfo);
       
 82555       sqlite3VdbeAddOp1(v, OP_Return, regReset);
       
 82556      
       
 82557     } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
       
 82558     else {
       
 82559       ExprList *pDel = 0;
       
 82560 #ifndef SQLITE_OMIT_BTREECOUNT
       
 82561       Table *pTab;
       
 82562       if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
       
 82563         /* If isSimpleCount() returns a pointer to a Table structure, then
       
 82564         ** the SQL statement is of the form:
       
 82565         **
       
 82566         **   SELECT count(*) FROM <tbl>
       
 82567         **
       
 82568         ** where the Table structure returned represents table <tbl>.
       
 82569         **
       
 82570         ** This statement is so common that it is optimized specially. The
       
 82571         ** OP_Count instruction is executed either on the intkey table that
       
 82572         ** contains the data for table <tbl> or on one of its indexes. It
       
 82573         ** is better to execute the op on an index, as indexes are almost
       
 82574         ** always spread across less pages than their corresponding tables.
       
 82575         */
       
 82576         const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
       
 82577         const int iCsr = pParse->nTab++;     /* Cursor to scan b-tree */
       
 82578         Index *pIdx;                         /* Iterator variable */
       
 82579         KeyInfo *pKeyInfo = 0;               /* Keyinfo for scanned index */
       
 82580         Index *pBest = 0;                    /* Best index found so far */
       
 82581         int iRoot = pTab->tnum;              /* Root page of scanned b-tree */
       
 82582 
       
 82583         sqlite3CodeVerifySchema(pParse, iDb);
       
 82584         sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
       
 82585 
       
 82586         /* Search for the index that has the least amount of columns. If
       
 82587         ** there is such an index, and it has less columns than the table
       
 82588         ** does, then we can assume that it consumes less space on disk and
       
 82589         ** will therefore be cheaper to scan to determine the query result.
       
 82590         ** In this case set iRoot to the root page number of the index b-tree
       
 82591         ** and pKeyInfo to the KeyInfo structure required to navigate the
       
 82592         ** index.
       
 82593         **
       
 82594         ** In practice the KeyInfo structure will not be used. It is only 
       
 82595         ** passed to keep OP_OpenRead happy.
       
 82596         */
       
 82597         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       
 82598           if( !pBest || pIdx->nColumn<pBest->nColumn ){
       
 82599             pBest = pIdx;
       
 82600           }
       
 82601         }
       
 82602         if( pBest && pBest->nColumn<pTab->nCol ){
       
 82603           iRoot = pBest->tnum;
       
 82604           pKeyInfo = sqlite3IndexKeyinfo(pParse, pBest);
       
 82605         }
       
 82606 
       
 82607         /* Open a read-only cursor, execute the OP_Count, close the cursor. */
       
 82608         sqlite3VdbeAddOp3(v, OP_OpenRead, iCsr, iRoot, iDb);
       
 82609         if( pKeyInfo ){
       
 82610           sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO_HANDOFF);
       
 82611         }
       
 82612         sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
       
 82613         sqlite3VdbeAddOp1(v, OP_Close, iCsr);
       
 82614       }else
       
 82615 #endif /* SQLITE_OMIT_BTREECOUNT */
       
 82616       {
       
 82617         /* Check if the query is of one of the following forms:
       
 82618         **
       
 82619         **   SELECT min(x) FROM ...
       
 82620         **   SELECT max(x) FROM ...
       
 82621         **
       
 82622         ** If it is, then ask the code in where.c to attempt to sort results
       
 82623         ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause. 
       
 82624         ** If where.c is able to produce results sorted in this order, then
       
 82625         ** add vdbe code to break out of the processing loop after the 
       
 82626         ** first iteration (since the first iteration of the loop is 
       
 82627         ** guaranteed to operate on the row with the minimum or maximum 
       
 82628         ** value of x, the only row required).
       
 82629         **
       
 82630         ** A special flag must be passed to sqlite3WhereBegin() to slightly
       
 82631         ** modify behaviour as follows:
       
 82632         **
       
 82633         **   + If the query is a "SELECT min(x)", then the loop coded by
       
 82634         **     where.c should not iterate over any values with a NULL value
       
 82635         **     for x.
       
 82636         **
       
 82637         **   + The optimizer code in where.c (the thing that decides which
       
 82638         **     index or indices to use) should place a different priority on 
       
 82639         **     satisfying the 'ORDER BY' clause than it does in other cases.
       
 82640         **     Refer to code and comments in where.c for details.
       
 82641         */
       
 82642         ExprList *pMinMax = 0;
       
 82643         u8 flag = minMaxQuery(p);
       
 82644         if( flag ){
       
 82645           assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
       
 82646           pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
       
 82647           pDel = pMinMax;
       
 82648           if( pMinMax && !db->mallocFailed ){
       
 82649             pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
       
 82650             pMinMax->a[0].pExpr->op = TK_COLUMN;
       
 82651           }
       
 82652         }
       
 82653   
       
 82654         /* This case runs if the aggregate has no GROUP BY clause.  The
       
 82655         ** processing is much simpler since there is only a single row
       
 82656         ** of output.
       
 82657         */
       
 82658         resetAccumulator(pParse, &sAggInfo);
       
 82659         pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax, flag);
       
 82660         if( pWInfo==0 ){
       
 82661           sqlite3ExprListDelete(db, pDel);
       
 82662           goto select_end;
       
 82663         }
       
 82664         updateAccumulator(pParse, &sAggInfo);
       
 82665         if( !pMinMax && flag ){
       
 82666           sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
       
 82667           VdbeComment((v, "%s() by index",
       
 82668                 (flag==WHERE_ORDERBY_MIN?"min":"max")));
       
 82669         }
       
 82670         sqlite3WhereEnd(pWInfo);
       
 82671         finalizeAggFunctions(pParse, &sAggInfo);
       
 82672       }
       
 82673 
       
 82674       pOrderBy = 0;
       
 82675       sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
       
 82676       selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1, 
       
 82677                       pDest, addrEnd, addrEnd);
       
 82678       sqlite3ExprListDelete(db, pDel);
       
 82679     }
       
 82680     sqlite3VdbeResolveLabel(v, addrEnd);
       
 82681     
       
 82682   } /* endif aggregate query */
       
 82683 
       
 82684   /* If there is an ORDER BY clause, then we need to sort the results
       
 82685   ** and send them to the callback one by one.
       
 82686   */
       
 82687   if( pOrderBy ){
       
 82688     generateSortTail(pParse, p, v, pEList->nExpr, pDest);
       
 82689   }
       
 82690 
       
 82691   /* Jump here to skip this query
       
 82692   */
       
 82693   sqlite3VdbeResolveLabel(v, iEnd);
       
 82694 
       
 82695   /* The SELECT was successfully coded.   Set the return code to 0
       
 82696   ** to indicate no errors.
       
 82697   */
       
 82698   rc = 0;
       
 82699 
       
 82700   /* Control jumps to here if an error is encountered above, or upon
       
 82701   ** successful coding of the SELECT.
       
 82702   */
       
 82703 select_end:
       
 82704 
       
 82705   /* Identify column names if results of the SELECT are to be output.
       
 82706   */
       
 82707   if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
       
 82708     generateColumnNames(pParse, pTabList, pEList);
       
 82709   }
       
 82710 
       
 82711   sqlite3DbFree(db, sAggInfo.aCol);
       
 82712   sqlite3DbFree(db, sAggInfo.aFunc);
       
 82713   return rc;
       
 82714 }
       
 82715 
       
 82716 #if defined(SQLITE_DEBUG)
       
 82717 /*
       
 82718 *******************************************************************************
       
 82719 ** The following code is used for testing and debugging only.  The code
       
 82720 ** that follows does not appear in normal builds.
       
 82721 **
       
 82722 ** These routines are used to print out the content of all or part of a 
       
 82723 ** parse structures such as Select or Expr.  Such printouts are useful
       
 82724 ** for helping to understand what is happening inside the code generator
       
 82725 ** during the execution of complex SELECT statements.
       
 82726 **
       
 82727 ** These routine are not called anywhere from within the normal
       
 82728 ** code base.  Then are intended to be called from within the debugger
       
 82729 ** or from temporary "printf" statements inserted for debugging.
       
 82730 */
       
 82731 SQLITE_PRIVATE void sqlite3PrintExpr(Expr *p){
       
 82732   if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
       
 82733     sqlite3DebugPrintf("(%s", p->u.zToken);
       
 82734   }else{
       
 82735     sqlite3DebugPrintf("(%d", p->op);
       
 82736   }
       
 82737   if( p->pLeft ){
       
 82738     sqlite3DebugPrintf(" ");
       
 82739     sqlite3PrintExpr(p->pLeft);
       
 82740   }
       
 82741   if( p->pRight ){
       
 82742     sqlite3DebugPrintf(" ");
       
 82743     sqlite3PrintExpr(p->pRight);
       
 82744   }
       
 82745   sqlite3DebugPrintf(")");
       
 82746 }
       
 82747 SQLITE_PRIVATE void sqlite3PrintExprList(ExprList *pList){
       
 82748   int i;
       
 82749   for(i=0; i<pList->nExpr; i++){
       
 82750     sqlite3PrintExpr(pList->a[i].pExpr);
       
 82751     if( i<pList->nExpr-1 ){
       
 82752       sqlite3DebugPrintf(", ");
       
 82753     }
       
 82754   }
       
 82755 }
       
 82756 SQLITE_PRIVATE void sqlite3PrintSelect(Select *p, int indent){
       
 82757   sqlite3DebugPrintf("%*sSELECT(%p) ", indent, "", p);
       
 82758   sqlite3PrintExprList(p->pEList);
       
 82759   sqlite3DebugPrintf("\n");
       
 82760   if( p->pSrc ){
       
 82761     char *zPrefix;
       
 82762     int i;
       
 82763     zPrefix = "FROM";
       
 82764     for(i=0; i<p->pSrc->nSrc; i++){
       
 82765       struct SrcList_item *pItem = &p->pSrc->a[i];
       
 82766       sqlite3DebugPrintf("%*s ", indent+6, zPrefix);
       
 82767       zPrefix = "";
       
 82768       if( pItem->pSelect ){
       
 82769         sqlite3DebugPrintf("(\n");
       
 82770         sqlite3PrintSelect(pItem->pSelect, indent+10);
       
 82771         sqlite3DebugPrintf("%*s)", indent+8, "");
       
 82772       }else if( pItem->zName ){
       
 82773         sqlite3DebugPrintf("%s", pItem->zName);
       
 82774       }
       
 82775       if( pItem->pTab ){
       
 82776         sqlite3DebugPrintf("(table: %s)", pItem->pTab->zName);
       
 82777       }
       
 82778       if( pItem->zAlias ){
       
 82779         sqlite3DebugPrintf(" AS %s", pItem->zAlias);
       
 82780       }
       
 82781       if( i<p->pSrc->nSrc-1 ){
       
 82782         sqlite3DebugPrintf(",");
       
 82783       }
       
 82784       sqlite3DebugPrintf("\n");
       
 82785     }
       
 82786   }
       
 82787   if( p->pWhere ){
       
 82788     sqlite3DebugPrintf("%*s WHERE ", indent, "");
       
 82789     sqlite3PrintExpr(p->pWhere);
       
 82790     sqlite3DebugPrintf("\n");
       
 82791   }
       
 82792   if( p->pGroupBy ){
       
 82793     sqlite3DebugPrintf("%*s GROUP BY ", indent, "");
       
 82794     sqlite3PrintExprList(p->pGroupBy);
       
 82795     sqlite3DebugPrintf("\n");
       
 82796   }
       
 82797   if( p->pHaving ){
       
 82798     sqlite3DebugPrintf("%*s HAVING ", indent, "");
       
 82799     sqlite3PrintExpr(p->pHaving);
       
 82800     sqlite3DebugPrintf("\n");
       
 82801   }
       
 82802   if( p->pOrderBy ){
       
 82803     sqlite3DebugPrintf("%*s ORDER BY ", indent, "");
       
 82804     sqlite3PrintExprList(p->pOrderBy);
       
 82805     sqlite3DebugPrintf("\n");
       
 82806   }
       
 82807 }
       
 82808 /* End of the structure debug printing code
       
 82809 *****************************************************************************/
       
 82810 #endif /* defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
       
 82811 
       
 82812 /************** End of select.c **********************************************/
       
 82813 /************** Begin file table.c *******************************************/
       
 82814 /*
       
 82815 ** 2001 September 15
       
 82816 **
       
 82817 ** The author disclaims copyright to this source code.  In place of
       
 82818 ** a legal notice, here is a blessing:
       
 82819 **
       
 82820 **    May you do good and not evil.
       
 82821 **    May you find forgiveness for yourself and forgive others.
       
 82822 **    May you share freely, never taking more than you give.
       
 82823 **
       
 82824 *************************************************************************
       
 82825 ** This file contains the sqlite3_get_table() and sqlite3_free_table()
       
 82826 ** interface routines.  These are just wrappers around the main
       
 82827 ** interface routine of sqlite3_exec().
       
 82828 **
       
 82829 ** These routines are in a separate files so that they will not be linked
       
 82830 ** if they are not used.
       
 82831 **
       
 82832 ** $Id: table.c,v 1.40 2009/04/10 14:28:00 drh Exp $
       
 82833 */
       
 82834 
       
 82835 #ifndef SQLITE_OMIT_GET_TABLE
       
 82836 
       
 82837 /*
       
 82838 ** This structure is used to pass data from sqlite3_get_table() through
       
 82839 ** to the callback function is uses to build the result.
       
 82840 */
       
 82841 typedef struct TabResult {
       
 82842   char **azResult;   /* Accumulated output */
       
 82843   char *zErrMsg;     /* Error message text, if an error occurs */
       
 82844   int nAlloc;        /* Slots allocated for azResult[] */
       
 82845   int nRow;          /* Number of rows in the result */
       
 82846   int nColumn;       /* Number of columns in the result */
       
 82847   int nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
       
 82848   int rc;            /* Return code from sqlite3_exec() */
       
 82849 } TabResult;
       
 82850 
       
 82851 /*
       
 82852 ** This routine is called once for each row in the result table.  Its job
       
 82853 ** is to fill in the TabResult structure appropriately, allocating new
       
 82854 ** memory as necessary.
       
 82855 */
       
 82856 static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
       
 82857   TabResult *p = (TabResult*)pArg;  /* Result accumulator */
       
 82858   int need;                         /* Slots needed in p->azResult[] */
       
 82859   int i;                            /* Loop counter */
       
 82860   char *z;                          /* A single column of result */
       
 82861 
       
 82862   /* Make sure there is enough space in p->azResult to hold everything
       
 82863   ** we need to remember from this invocation of the callback.
       
 82864   */
       
 82865   if( p->nRow==0 && argv!=0 ){
       
 82866     need = nCol*2;
       
 82867   }else{
       
 82868     need = nCol;
       
 82869   }
       
 82870   if( p->nData + need > p->nAlloc ){
       
 82871     char **azNew;
       
 82872     p->nAlloc = p->nAlloc*2 + need;
       
 82873     azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
       
 82874     if( azNew==0 ) goto malloc_failed;
       
 82875     p->azResult = azNew;
       
 82876   }
       
 82877 
       
 82878   /* If this is the first row, then generate an extra row containing
       
 82879   ** the names of all columns.
       
 82880   */
       
 82881   if( p->nRow==0 ){
       
 82882     p->nColumn = nCol;
       
 82883     for(i=0; i<nCol; i++){
       
 82884       z = sqlite3_mprintf("%s", colv[i]);
       
 82885       if( z==0 ) goto malloc_failed;
       
 82886       p->azResult[p->nData++] = z;
       
 82887     }
       
 82888   }else if( p->nColumn!=nCol ){
       
 82889     sqlite3_free(p->zErrMsg);
       
 82890     p->zErrMsg = sqlite3_mprintf(
       
 82891        "sqlite3_get_table() called with two or more incompatible queries"
       
 82892     );
       
 82893     p->rc = SQLITE_ERROR;
       
 82894     return 1;
       
 82895   }
       
 82896 
       
 82897   /* Copy over the row data
       
 82898   */
       
 82899   if( argv!=0 ){
       
 82900     for(i=0; i<nCol; i++){
       
 82901       if( argv[i]==0 ){
       
 82902         z = 0;
       
 82903       }else{
       
 82904         int n = sqlite3Strlen30(argv[i])+1;
       
 82905         z = sqlite3_malloc( n );
       
 82906         if( z==0 ) goto malloc_failed;
       
 82907         memcpy(z, argv[i], n);
       
 82908       }
       
 82909       p->azResult[p->nData++] = z;
       
 82910     }
       
 82911     p->nRow++;
       
 82912   }
       
 82913   return 0;
       
 82914 
       
 82915 malloc_failed:
       
 82916   p->rc = SQLITE_NOMEM;
       
 82917   return 1;
       
 82918 }
       
 82919 
       
 82920 /*
       
 82921 ** Query the database.  But instead of invoking a callback for each row,
       
 82922 ** malloc() for space to hold the result and return the entire results
       
 82923 ** at the conclusion of the call.
       
 82924 **
       
 82925 ** The result that is written to ***pazResult is held in memory obtained
       
 82926 ** from malloc().  But the caller cannot free this memory directly.  
       
 82927 ** Instead, the entire table should be passed to sqlite3_free_table() when
       
 82928 ** the calling procedure is finished using it.
       
 82929 */
       
 82930 SQLITE_API int sqlite3_get_table(
       
 82931   sqlite3 *db,                /* The database on which the SQL executes */
       
 82932   const char *zSql,           /* The SQL to be executed */
       
 82933   char ***pazResult,          /* Write the result table here */
       
 82934   int *pnRow,                 /* Write the number of rows in the result here */
       
 82935   int *pnColumn,              /* Write the number of columns of result here */
       
 82936   char **pzErrMsg             /* Write error messages here */
       
 82937 ){
       
 82938   int rc;
       
 82939   TabResult res;
       
 82940 
       
 82941   *pazResult = 0;
       
 82942   if( pnColumn ) *pnColumn = 0;
       
 82943   if( pnRow ) *pnRow = 0;
       
 82944   if( pzErrMsg ) *pzErrMsg = 0;
       
 82945   res.zErrMsg = 0;
       
 82946   res.nRow = 0;
       
 82947   res.nColumn = 0;
       
 82948   res.nData = 1;
       
 82949   res.nAlloc = 20;
       
 82950   res.rc = SQLITE_OK;
       
 82951   res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
       
 82952   if( res.azResult==0 ){
       
 82953      db->errCode = SQLITE_NOMEM;
       
 82954      return SQLITE_NOMEM;
       
 82955   }
       
 82956   res.azResult[0] = 0;
       
 82957   rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
       
 82958   assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
       
 82959   res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
       
 82960   if( (rc&0xff)==SQLITE_ABORT ){
       
 82961     sqlite3_free_table(&res.azResult[1]);
       
 82962     if( res.zErrMsg ){
       
 82963       if( pzErrMsg ){
       
 82964         sqlite3_free(*pzErrMsg);
       
 82965         *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
       
 82966       }
       
 82967       sqlite3_free(res.zErrMsg);
       
 82968     }
       
 82969     db->errCode = res.rc;  /* Assume 32-bit assignment is atomic */
       
 82970     return res.rc;
       
 82971   }
       
 82972   sqlite3_free(res.zErrMsg);
       
 82973   if( rc!=SQLITE_OK ){
       
 82974     sqlite3_free_table(&res.azResult[1]);
       
 82975     return rc;
       
 82976   }
       
 82977   if( res.nAlloc>res.nData ){
       
 82978     char **azNew;
       
 82979     azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
       
 82980     if( azNew==0 ){
       
 82981       sqlite3_free_table(&res.azResult[1]);
       
 82982       db->errCode = SQLITE_NOMEM;
       
 82983       return SQLITE_NOMEM;
       
 82984     }
       
 82985     res.azResult = azNew;
       
 82986   }
       
 82987   *pazResult = &res.azResult[1];
       
 82988   if( pnColumn ) *pnColumn = res.nColumn;
       
 82989   if( pnRow ) *pnRow = res.nRow;
       
 82990   return rc;
       
 82991 }
       
 82992 
       
 82993 /*
       
 82994 ** This routine frees the space the sqlite3_get_table() malloced.
       
 82995 */
       
 82996 SQLITE_API void sqlite3_free_table(
       
 82997   char **azResult            /* Result returned from from sqlite3_get_table() */
       
 82998 ){
       
 82999   if( azResult ){
       
 83000     int i, n;
       
 83001     azResult--;
       
 83002     assert( azResult!=0 );
       
 83003     n = SQLITE_PTR_TO_INT(azResult[0]);
       
 83004     for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
       
 83005     sqlite3_free(azResult);
       
 83006   }
       
 83007 }
       
 83008 
       
 83009 #endif /* SQLITE_OMIT_GET_TABLE */
       
 83010 
       
 83011 /************** End of table.c ***********************************************/
       
 83012 /************** Begin file trigger.c *****************************************/
       
 83013 /*
       
 83014 **
       
 83015 ** The author disclaims copyright to this source code.  In place of
       
 83016 ** a legal notice, here is a blessing:
       
 83017 **
       
 83018 **    May you do good and not evil.
       
 83019 **    May you find forgiveness for yourself and forgive others.
       
 83020 **    May you share freely, never taking more than you give.
       
 83021 **
       
 83022 *************************************************************************
       
 83023 **
       
 83024 **
       
 83025 ** $Id: trigger.c,v 1.143 2009/08/10 03:57:58 shane Exp $
       
 83026 */
       
 83027 
       
 83028 #ifndef SQLITE_OMIT_TRIGGER
       
 83029 /*
       
 83030 ** Delete a linked list of TriggerStep structures.
       
 83031 */
       
 83032 SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
       
 83033   while( pTriggerStep ){
       
 83034     TriggerStep * pTmp = pTriggerStep;
       
 83035     pTriggerStep = pTriggerStep->pNext;
       
 83036 
       
 83037     sqlite3ExprDelete(db, pTmp->pWhere);
       
 83038     sqlite3ExprListDelete(db, pTmp->pExprList);
       
 83039     sqlite3SelectDelete(db, pTmp->pSelect);
       
 83040     sqlite3IdListDelete(db, pTmp->pIdList);
       
 83041 
       
 83042     sqlite3DbFree(db, pTmp);
       
 83043   }
       
 83044 }
       
 83045 
       
 83046 /*
       
 83047 ** Given table pTab, return a list of all the triggers attached to 
       
 83048 ** the table. The list is connected by Trigger.pNext pointers.
       
 83049 **
       
 83050 ** All of the triggers on pTab that are in the same database as pTab
       
 83051 ** are already attached to pTab->pTrigger.  But there might be additional
       
 83052 ** triggers on pTab in the TEMP schema.  This routine prepends all
       
 83053 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
       
 83054 ** and returns the combined list.
       
 83055 **
       
 83056 ** To state it another way:  This routine returns a list of all triggers
       
 83057 ** that fire off of pTab.  The list will include any TEMP triggers on
       
 83058 ** pTab as well as the triggers lised in pTab->pTrigger.
       
 83059 */
       
 83060 SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
       
 83061   Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
       
 83062   Trigger *pList = 0;                  /* List of triggers to return */
       
 83063 
       
 83064   if( pParse->disableTriggers ){
       
 83065     return 0;
       
 83066   }
       
 83067 
       
 83068   if( pTmpSchema!=pTab->pSchema ){
       
 83069     HashElem *p;
       
 83070     for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
       
 83071       Trigger *pTrig = (Trigger *)sqliteHashData(p);
       
 83072       if( pTrig->pTabSchema==pTab->pSchema
       
 83073        && 0==sqlite3StrICmp(pTrig->table, pTab->zName) 
       
 83074       ){
       
 83075         pTrig->pNext = (pList ? pList : pTab->pTrigger);
       
 83076         pList = pTrig;
       
 83077       }
       
 83078     }
       
 83079   }
       
 83080 
       
 83081   return (pList ? pList : pTab->pTrigger);
       
 83082 }
       
 83083 
       
 83084 /*
       
 83085 ** This is called by the parser when it sees a CREATE TRIGGER statement
       
 83086 ** up to the point of the BEGIN before the trigger actions.  A Trigger
       
 83087 ** structure is generated based on the information available and stored
       
 83088 ** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
       
 83089 ** sqlite3FinishTrigger() function is called to complete the trigger
       
 83090 ** construction process.
       
 83091 */
       
 83092 SQLITE_PRIVATE void sqlite3BeginTrigger(
       
 83093   Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
       
 83094   Token *pName1,      /* The name of the trigger */
       
 83095   Token *pName2,      /* The name of the trigger */
       
 83096   int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
       
 83097   int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
       
 83098   IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
       
 83099   SrcList *pTableName,/* The name of the table/view the trigger applies to */
       
 83100   Expr *pWhen,        /* WHEN clause */
       
 83101   int isTemp,         /* True if the TEMPORARY keyword is present */
       
 83102   int noErr           /* Suppress errors if the trigger already exists */
       
 83103 ){
       
 83104   Trigger *pTrigger = 0;  /* The new trigger */
       
 83105   Table *pTab;            /* Table that the trigger fires off of */
       
 83106   char *zName = 0;        /* Name of the trigger */
       
 83107   sqlite3 *db = pParse->db;  /* The database connection */
       
 83108   int iDb;                /* The database to store the trigger in */
       
 83109   Token *pName;           /* The unqualified db name */
       
 83110   DbFixer sFix;           /* State vector for the DB fixer */
       
 83111   int iTabDb;             /* Index of the database holding pTab */
       
 83112 
       
 83113   assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
       
 83114   assert( pName2!=0 );
       
 83115   assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
       
 83116   assert( op>0 && op<0xff );
       
 83117   if( isTemp ){
       
 83118     /* If TEMP was specified, then the trigger name may not be qualified. */
       
 83119     if( pName2->n>0 ){
       
 83120       sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
       
 83121       goto trigger_cleanup;
       
 83122     }
       
 83123     iDb = 1;
       
 83124     pName = pName1;
       
 83125   }else{
       
 83126     /* Figure out the db that the the trigger will be created in */
       
 83127     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
       
 83128     if( iDb<0 ){
       
 83129       goto trigger_cleanup;
       
 83130     }
       
 83131   }
       
 83132 
       
 83133   /* If the trigger name was unqualified, and the table is a temp table,
       
 83134   ** then set iDb to 1 to create the trigger in the temporary database.
       
 83135   ** If sqlite3SrcListLookup() returns 0, indicating the table does not
       
 83136   ** exist, the error is caught by the block below.
       
 83137   */
       
 83138   if( !pTableName || db->mallocFailed ){
       
 83139     goto trigger_cleanup;
       
 83140   }
       
 83141   pTab = sqlite3SrcListLookup(pParse, pTableName);
       
 83142   if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
       
 83143     iDb = 1;
       
 83144   }
       
 83145 
       
 83146   /* Ensure the table name matches database name and that the table exists */
       
 83147   if( db->mallocFailed ) goto trigger_cleanup;
       
 83148   assert( pTableName->nSrc==1 );
       
 83149   if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && 
       
 83150       sqlite3FixSrcList(&sFix, pTableName) ){
       
 83151     goto trigger_cleanup;
       
 83152   }
       
 83153   pTab = sqlite3SrcListLookup(pParse, pTableName);
       
 83154   if( !pTab ){
       
 83155     /* The table does not exist. */
       
 83156     if( db->init.iDb==1 ){
       
 83157       /* Ticket #3810.
       
 83158       ** Normally, whenever a table is dropped, all associated triggers are
       
 83159       ** dropped too.  But if a TEMP trigger is created on a non-TEMP table
       
 83160       ** and the table is dropped by a different database connection, the
       
 83161       ** trigger is not visible to the database connection that does the
       
 83162       ** drop so the trigger cannot be dropped.  This results in an
       
 83163       ** "orphaned trigger" - a trigger whose associated table is missing.
       
 83164       */
       
 83165       db->init.orphanTrigger = 1;
       
 83166     }
       
 83167     goto trigger_cleanup;
       
 83168   }
       
 83169   if( IsVirtual(pTab) ){
       
 83170     sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
       
 83171     goto trigger_cleanup;
       
 83172   }
       
 83173 
       
 83174   /* Check that the trigger name is not reserved and that no trigger of the
       
 83175   ** specified name exists */
       
 83176   zName = sqlite3NameFromToken(db, pName);
       
 83177   if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
       
 83178     goto trigger_cleanup;
       
 83179   }
       
 83180   if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
       
 83181                       zName, sqlite3Strlen30(zName)) ){
       
 83182     if( !noErr ){
       
 83183       sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
       
 83184     }
       
 83185     goto trigger_cleanup;
       
 83186   }
       
 83187 
       
 83188   /* Do not create a trigger on a system table */
       
 83189   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
       
 83190     sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
       
 83191     pParse->nErr++;
       
 83192     goto trigger_cleanup;
       
 83193   }
       
 83194 
       
 83195   /* INSTEAD of triggers are only for views and views only support INSTEAD
       
 83196   ** of triggers.
       
 83197   */
       
 83198   if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
       
 83199     sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", 
       
 83200         (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
       
 83201     goto trigger_cleanup;
       
 83202   }
       
 83203   if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
       
 83204     sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
       
 83205         " trigger on table: %S", pTableName, 0);
       
 83206     goto trigger_cleanup;
       
 83207   }
       
 83208   iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       
 83209 
       
 83210 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 83211   {
       
 83212     int code = SQLITE_CREATE_TRIGGER;
       
 83213     const char *zDb = db->aDb[iTabDb].zName;
       
 83214     const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
       
 83215     if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
       
 83216     if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
       
 83217       goto trigger_cleanup;
       
 83218     }
       
 83219     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
       
 83220       goto trigger_cleanup;
       
 83221     }
       
 83222   }
       
 83223 #endif
       
 83224 
       
 83225   /* INSTEAD OF triggers can only appear on views and BEFORE triggers
       
 83226   ** cannot appear on views.  So we might as well translate every
       
 83227   ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
       
 83228   ** elsewhere.
       
 83229   */
       
 83230   if (tr_tm == TK_INSTEAD){
       
 83231     tr_tm = TK_BEFORE;
       
 83232   }
       
 83233 
       
 83234   /* Build the Trigger object */
       
 83235   pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
       
 83236   if( pTrigger==0 ) goto trigger_cleanup;
       
 83237   pTrigger->zName = zName;
       
 83238   zName = 0;
       
 83239   pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
       
 83240   pTrigger->pSchema = db->aDb[iDb].pSchema;
       
 83241   pTrigger->pTabSchema = pTab->pSchema;
       
 83242   pTrigger->op = (u8)op;
       
 83243   pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
       
 83244   pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
       
 83245   pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
       
 83246   assert( pParse->pNewTrigger==0 );
       
 83247   pParse->pNewTrigger = pTrigger;
       
 83248 
       
 83249 trigger_cleanup:
       
 83250   sqlite3DbFree(db, zName);
       
 83251   sqlite3SrcListDelete(db, pTableName);
       
 83252   sqlite3IdListDelete(db, pColumns);
       
 83253   sqlite3ExprDelete(db, pWhen);
       
 83254   if( !pParse->pNewTrigger ){
       
 83255     sqlite3DeleteTrigger(db, pTrigger);
       
 83256   }else{
       
 83257     assert( pParse->pNewTrigger==pTrigger );
       
 83258   }
       
 83259 }
       
 83260 
       
 83261 /*
       
 83262 ** This routine is called after all of the trigger actions have been parsed
       
 83263 ** in order to complete the process of building the trigger.
       
 83264 */
       
 83265 SQLITE_PRIVATE void sqlite3FinishTrigger(
       
 83266   Parse *pParse,          /* Parser context */
       
 83267   TriggerStep *pStepList, /* The triggered program */
       
 83268   Token *pAll             /* Token that describes the complete CREATE TRIGGER */
       
 83269 ){
       
 83270   Trigger *pTrig = pParse->pNewTrigger;    /* Trigger being finished */
       
 83271   char *zName;                             /* Name of trigger */
       
 83272   sqlite3 *db = pParse->db;                /* The database */
       
 83273   DbFixer sFix;
       
 83274   int iDb;                                 /* Database containing the trigger */
       
 83275   Token nameToken;           /* Trigger name for error reporting */
       
 83276 
       
 83277   pTrig = pParse->pNewTrigger;
       
 83278   pParse->pNewTrigger = 0;
       
 83279   if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
       
 83280   zName = pTrig->zName;
       
 83281   iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
       
 83282   pTrig->step_list = pStepList;
       
 83283   while( pStepList ){
       
 83284     pStepList->pTrig = pTrig;
       
 83285     pStepList = pStepList->pNext;
       
 83286   }
       
 83287   nameToken.z = pTrig->zName;
       
 83288   nameToken.n = sqlite3Strlen30(nameToken.z);
       
 83289   if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken) 
       
 83290           && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
       
 83291     goto triggerfinish_cleanup;
       
 83292   }
       
 83293 
       
 83294   /* if we are not initializing, and this trigger is not on a TEMP table, 
       
 83295   ** build the sqlite_master entry
       
 83296   */
       
 83297   if( !db->init.busy ){
       
 83298     Vdbe *v;
       
 83299     char *z;
       
 83300 
       
 83301     /* Make an entry in the sqlite_master table */
       
 83302     v = sqlite3GetVdbe(pParse);
       
 83303     if( v==0 ) goto triggerfinish_cleanup;
       
 83304     sqlite3BeginWriteOperation(pParse, 0, iDb);
       
 83305     z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
       
 83306     sqlite3NestedParse(pParse,
       
 83307        "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
       
 83308        db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
       
 83309        pTrig->table, z);
       
 83310     sqlite3DbFree(db, z);
       
 83311     sqlite3ChangeCookie(pParse, iDb);
       
 83312     sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
       
 83313         db, "type='trigger' AND name='%q'", zName), P4_DYNAMIC
       
 83314     );
       
 83315   }
       
 83316 
       
 83317   if( db->init.busy ){
       
 83318     Trigger *pLink = pTrig;
       
 83319     Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
       
 83320     pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
       
 83321     if( pTrig ){
       
 83322       db->mallocFailed = 1;
       
 83323     }else if( pLink->pSchema==pLink->pTabSchema ){
       
 83324       Table *pTab;
       
 83325       int n = sqlite3Strlen30(pLink->table);
       
 83326       pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
       
 83327       assert( pTab!=0 );
       
 83328       pLink->pNext = pTab->pTrigger;
       
 83329       pTab->pTrigger = pLink;
       
 83330     }
       
 83331   }
       
 83332 
       
 83333 triggerfinish_cleanup:
       
 83334   sqlite3DeleteTrigger(db, pTrig);
       
 83335   assert( !pParse->pNewTrigger );
       
 83336   sqlite3DeleteTriggerStep(db, pStepList);
       
 83337 }
       
 83338 
       
 83339 /*
       
 83340 ** Turn a SELECT statement (that the pSelect parameter points to) into
       
 83341 ** a trigger step.  Return a pointer to a TriggerStep structure.
       
 83342 **
       
 83343 ** The parser calls this routine when it finds a SELECT statement in
       
 83344 ** body of a TRIGGER.  
       
 83345 */
       
 83346 SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
       
 83347   TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
       
 83348   if( pTriggerStep==0 ) {
       
 83349     sqlite3SelectDelete(db, pSelect);
       
 83350     return 0;
       
 83351   }
       
 83352   pTriggerStep->op = TK_SELECT;
       
 83353   pTriggerStep->pSelect = pSelect;
       
 83354   pTriggerStep->orconf = OE_Default;
       
 83355   return pTriggerStep;
       
 83356 }
       
 83357 
       
 83358 /*
       
 83359 ** Allocate space to hold a new trigger step.  The allocated space
       
 83360 ** holds both the TriggerStep object and the TriggerStep.target.z string.
       
 83361 **
       
 83362 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
       
 83363 */
       
 83364 static TriggerStep *triggerStepAllocate(
       
 83365   sqlite3 *db,                /* Database connection */
       
 83366   u8 op,                      /* Trigger opcode */
       
 83367   Token *pName                /* The target name */
       
 83368 ){
       
 83369   TriggerStep *pTriggerStep;
       
 83370 
       
 83371   pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
       
 83372   if( pTriggerStep ){
       
 83373     char *z = (char*)&pTriggerStep[1];
       
 83374     memcpy(z, pName->z, pName->n);
       
 83375     pTriggerStep->target.z = z;
       
 83376     pTriggerStep->target.n = pName->n;
       
 83377     pTriggerStep->op = op;
       
 83378   }
       
 83379   return pTriggerStep;
       
 83380 }
       
 83381 
       
 83382 /*
       
 83383 ** Build a trigger step out of an INSERT statement.  Return a pointer
       
 83384 ** to the new trigger step.
       
 83385 **
       
 83386 ** The parser calls this routine when it sees an INSERT inside the
       
 83387 ** body of a trigger.
       
 83388 */
       
 83389 SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
       
 83390   sqlite3 *db,        /* The database connection */
       
 83391   Token *pTableName,  /* Name of the table into which we insert */
       
 83392   IdList *pColumn,    /* List of columns in pTableName to insert into */
       
 83393   ExprList *pEList,   /* The VALUE clause: a list of values to be inserted */
       
 83394   Select *pSelect,    /* A SELECT statement that supplies values */
       
 83395   u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
       
 83396 ){
       
 83397   TriggerStep *pTriggerStep;
       
 83398 
       
 83399   assert(pEList == 0 || pSelect == 0);
       
 83400   assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
       
 83401 
       
 83402   pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
       
 83403   if( pTriggerStep ){
       
 83404     pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
       
 83405     pTriggerStep->pIdList = pColumn;
       
 83406     pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
       
 83407     pTriggerStep->orconf = orconf;
       
 83408   }else{
       
 83409     sqlite3IdListDelete(db, pColumn);
       
 83410   }
       
 83411   sqlite3ExprListDelete(db, pEList);
       
 83412   sqlite3SelectDelete(db, pSelect);
       
 83413 
       
 83414   return pTriggerStep;
       
 83415 }
       
 83416 
       
 83417 /*
       
 83418 ** Construct a trigger step that implements an UPDATE statement and return
       
 83419 ** a pointer to that trigger step.  The parser calls this routine when it
       
 83420 ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
       
 83421 */
       
 83422 SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
       
 83423   sqlite3 *db,         /* The database connection */
       
 83424   Token *pTableName,   /* Name of the table to be updated */
       
 83425   ExprList *pEList,    /* The SET clause: list of column and new values */
       
 83426   Expr *pWhere,        /* The WHERE clause */
       
 83427   u8 orconf            /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
       
 83428 ){
       
 83429   TriggerStep *pTriggerStep;
       
 83430 
       
 83431   pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
       
 83432   if( pTriggerStep ){
       
 83433     pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
       
 83434     pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
       
 83435     pTriggerStep->orconf = orconf;
       
 83436   }
       
 83437   sqlite3ExprListDelete(db, pEList);
       
 83438   sqlite3ExprDelete(db, pWhere);
       
 83439   return pTriggerStep;
       
 83440 }
       
 83441 
       
 83442 /*
       
 83443 ** Construct a trigger step that implements a DELETE statement and return
       
 83444 ** a pointer to that trigger step.  The parser calls this routine when it
       
 83445 ** sees a DELETE statement inside the body of a CREATE TRIGGER.
       
 83446 */
       
 83447 SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
       
 83448   sqlite3 *db,            /* Database connection */
       
 83449   Token *pTableName,      /* The table from which rows are deleted */
       
 83450   Expr *pWhere            /* The WHERE clause */
       
 83451 ){
       
 83452   TriggerStep *pTriggerStep;
       
 83453 
       
 83454   pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
       
 83455   if( pTriggerStep ){
       
 83456     pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
       
 83457     pTriggerStep->orconf = OE_Default;
       
 83458   }
       
 83459   sqlite3ExprDelete(db, pWhere);
       
 83460   return pTriggerStep;
       
 83461 }
       
 83462 
       
 83463 /* 
       
 83464 ** Recursively delete a Trigger structure
       
 83465 */
       
 83466 SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
       
 83467   if( pTrigger==0 ) return;
       
 83468   sqlite3DeleteTriggerStep(db, pTrigger->step_list);
       
 83469   sqlite3DbFree(db, pTrigger->zName);
       
 83470   sqlite3DbFree(db, pTrigger->table);
       
 83471   sqlite3ExprDelete(db, pTrigger->pWhen);
       
 83472   sqlite3IdListDelete(db, pTrigger->pColumns);
       
 83473   sqlite3DbFree(db, pTrigger);
       
 83474 }
       
 83475 
       
 83476 /*
       
 83477 ** This function is called to drop a trigger from the database schema. 
       
 83478 **
       
 83479 ** This may be called directly from the parser and therefore identifies
       
 83480 ** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
       
 83481 ** same job as this routine except it takes a pointer to the trigger
       
 83482 ** instead of the trigger name.
       
 83483 **/
       
 83484 SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
       
 83485   Trigger *pTrigger = 0;
       
 83486   int i;
       
 83487   const char *zDb;
       
 83488   const char *zName;
       
 83489   int nName;
       
 83490   sqlite3 *db = pParse->db;
       
 83491 
       
 83492   if( db->mallocFailed ) goto drop_trigger_cleanup;
       
 83493   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
       
 83494     goto drop_trigger_cleanup;
       
 83495   }
       
 83496 
       
 83497   assert( pName->nSrc==1 );
       
 83498   zDb = pName->a[0].zDatabase;
       
 83499   zName = pName->a[0].zName;
       
 83500   nName = sqlite3Strlen30(zName);
       
 83501   for(i=OMIT_TEMPDB; i<db->nDb; i++){
       
 83502     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
       
 83503     if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
       
 83504     pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
       
 83505     if( pTrigger ) break;
       
 83506   }
       
 83507   if( !pTrigger ){
       
 83508     if( !noErr ){
       
 83509       sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
       
 83510     }
       
 83511     goto drop_trigger_cleanup;
       
 83512   }
       
 83513   sqlite3DropTriggerPtr(pParse, pTrigger);
       
 83514 
       
 83515 drop_trigger_cleanup:
       
 83516   sqlite3SrcListDelete(db, pName);
       
 83517 }
       
 83518 
       
 83519 /*
       
 83520 ** Return a pointer to the Table structure for the table that a trigger
       
 83521 ** is set on.
       
 83522 */
       
 83523 static Table *tableOfTrigger(Trigger *pTrigger){
       
 83524   int n = sqlite3Strlen30(pTrigger->table);
       
 83525   return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
       
 83526 }
       
 83527 
       
 83528 
       
 83529 /*
       
 83530 ** Drop a trigger given a pointer to that trigger. 
       
 83531 */
       
 83532 SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
       
 83533   Table   *pTable;
       
 83534   Vdbe *v;
       
 83535   sqlite3 *db = pParse->db;
       
 83536   int iDb;
       
 83537 
       
 83538   iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
       
 83539   assert( iDb>=0 && iDb<db->nDb );
       
 83540   pTable = tableOfTrigger(pTrigger);
       
 83541   assert( pTable );
       
 83542   assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
       
 83543 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 83544   {
       
 83545     int code = SQLITE_DROP_TRIGGER;
       
 83546     const char *zDb = db->aDb[iDb].zName;
       
 83547     const char *zTab = SCHEMA_TABLE(iDb);
       
 83548     if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
       
 83549     if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
       
 83550       sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
       
 83551       return;
       
 83552     }
       
 83553   }
       
 83554 #endif
       
 83555 
       
 83556   /* Generate code to destroy the database record of the trigger.
       
 83557   */
       
 83558   assert( pTable!=0 );
       
 83559   if( (v = sqlite3GetVdbe(pParse))!=0 ){
       
 83560     int base;
       
 83561     static const VdbeOpList dropTrigger[] = {
       
 83562       { OP_Rewind,     0, ADDR(9),  0},
       
 83563       { OP_String8,    0, 1,        0}, /* 1 */
       
 83564       { OP_Column,     0, 1,        2},
       
 83565       { OP_Ne,         2, ADDR(8),  1},
       
 83566       { OP_String8,    0, 1,        0}, /* 4: "trigger" */
       
 83567       { OP_Column,     0, 0,        2},
       
 83568       { OP_Ne,         2, ADDR(8),  1},
       
 83569       { OP_Delete,     0, 0,        0},
       
 83570       { OP_Next,       0, ADDR(1),  0}, /* 8 */
       
 83571     };
       
 83572 
       
 83573     sqlite3BeginWriteOperation(pParse, 0, iDb);
       
 83574     sqlite3OpenMasterTable(pParse, iDb);
       
 83575     base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger);
       
 83576     sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, 0);
       
 83577     sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
       
 83578     sqlite3ChangeCookie(pParse, iDb);
       
 83579     sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
       
 83580     sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
       
 83581     if( pParse->nMem<3 ){
       
 83582       pParse->nMem = 3;
       
 83583     }
       
 83584   }
       
 83585 }
       
 83586 
       
 83587 /*
       
 83588 ** Remove a trigger from the hash tables of the sqlite* pointer.
       
 83589 */
       
 83590 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
       
 83591   Hash *pHash = &(db->aDb[iDb].pSchema->trigHash);
       
 83592   Trigger *pTrigger;
       
 83593   pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
       
 83594   if( ALWAYS(pTrigger) ){
       
 83595     if( pTrigger->pSchema==pTrigger->pTabSchema ){
       
 83596       Table *pTab = tableOfTrigger(pTrigger);
       
 83597       Trigger **pp;
       
 83598       for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
       
 83599       *pp = (*pp)->pNext;
       
 83600     }
       
 83601     sqlite3DeleteTrigger(db, pTrigger);
       
 83602     db->flags |= SQLITE_InternChanges;
       
 83603   }
       
 83604 }
       
 83605 
       
 83606 /*
       
 83607 ** pEList is the SET clause of an UPDATE statement.  Each entry
       
 83608 ** in pEList is of the format <id>=<expr>.  If any of the entries
       
 83609 ** in pEList have an <id> which matches an identifier in pIdList,
       
 83610 ** then return TRUE.  If pIdList==NULL, then it is considered a
       
 83611 ** wildcard that matches anything.  Likewise if pEList==NULL then
       
 83612 ** it matches anything so always return true.  Return false only
       
 83613 ** if there is no match.
       
 83614 */
       
 83615 static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
       
 83616   int e;
       
 83617   if( pIdList==0 || NEVER(pEList==0) ) return 1;
       
 83618   for(e=0; e<pEList->nExpr; e++){
       
 83619     if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
       
 83620   }
       
 83621   return 0; 
       
 83622 }
       
 83623 
       
 83624 /*
       
 83625 ** Return a list of all triggers on table pTab if there exists at least
       
 83626 ** one trigger that must be fired when an operation of type 'op' is 
       
 83627 ** performed on the table, and, if that operation is an UPDATE, if at
       
 83628 ** least one of the columns in pChanges is being modified.
       
 83629 */
       
 83630 SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
       
 83631   Parse *pParse,          /* Parse context */
       
 83632   Table *pTab,            /* The table the contains the triggers */
       
 83633   int op,                 /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
       
 83634   ExprList *pChanges,     /* Columns that change in an UPDATE statement */
       
 83635   int *pMask              /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
       
 83636 ){
       
 83637   int mask = 0;
       
 83638   Trigger *pList = sqlite3TriggerList(pParse, pTab);
       
 83639   Trigger *p;
       
 83640   assert( pList==0 || IsVirtual(pTab)==0 );
       
 83641   for(p=pList; p; p=p->pNext){
       
 83642     if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
       
 83643       mask |= p->tr_tm;
       
 83644     }
       
 83645   }
       
 83646   if( pMask ){
       
 83647     *pMask = mask;
       
 83648   }
       
 83649   return (mask ? pList : 0);
       
 83650 }
       
 83651 
       
 83652 /*
       
 83653 ** Convert the pStep->target token into a SrcList and return a pointer
       
 83654 ** to that SrcList.
       
 83655 **
       
 83656 ** This routine adds a specific database name, if needed, to the target when
       
 83657 ** forming the SrcList.  This prevents a trigger in one database from
       
 83658 ** referring to a target in another database.  An exception is when the
       
 83659 ** trigger is in TEMP in which case it can refer to any other database it
       
 83660 ** wants.
       
 83661 */
       
 83662 static SrcList *targetSrcList(
       
 83663   Parse *pParse,       /* The parsing context */
       
 83664   TriggerStep *pStep   /* The trigger containing the target token */
       
 83665 ){
       
 83666   int iDb;             /* Index of the database to use */
       
 83667   SrcList *pSrc;       /* SrcList to be returned */
       
 83668 
       
 83669   pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
       
 83670   if( pSrc ){
       
 83671     assert( pSrc->nSrc>0 );
       
 83672     assert( pSrc->a!=0 );
       
 83673     iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
       
 83674     if( iDb==0 || iDb>=2 ){
       
 83675       sqlite3 *db = pParse->db;
       
 83676       assert( iDb<pParse->db->nDb );
       
 83677       pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
       
 83678     }
       
 83679   }
       
 83680   return pSrc;
       
 83681 }
       
 83682 
       
 83683 /*
       
 83684 ** Generate VDBE code for the statements inside the body of a single 
       
 83685 ** trigger.
       
 83686 */
       
 83687 static int codeTriggerProgram(
       
 83688   Parse *pParse,            /* The parser context */
       
 83689   TriggerStep *pStepList,   /* List of statements inside the trigger body */
       
 83690   int orconf                /* Conflict algorithm. (OE_Abort, etc) */  
       
 83691 ){
       
 83692   TriggerStep *pStep;
       
 83693   Vdbe *v = pParse->pVdbe;
       
 83694   sqlite3 *db = pParse->db;
       
 83695 
       
 83696   assert( pParse->pTriggerTab && pParse->pToplevel );
       
 83697   assert( pStepList );
       
 83698   assert( v!=0 );
       
 83699   for(pStep=pStepList; pStep; pStep=pStep->pNext){
       
 83700     /* Figure out the ON CONFLICT policy that will be used for this step
       
 83701     ** of the trigger program. If the statement that caused this trigger
       
 83702     ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
       
 83703     ** the ON CONFLICT policy that was specified as part of the trigger
       
 83704     ** step statement. Example:
       
 83705     **
       
 83706     **   CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
       
 83707     **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
       
 83708     **   END;
       
 83709     **
       
 83710     **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
       
 83711     **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
       
 83712     */
       
 83713     pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
       
 83714 
       
 83715     switch( pStep->op ){
       
 83716       case TK_UPDATE: {
       
 83717         sqlite3Update(pParse, 
       
 83718           targetSrcList(pParse, pStep),
       
 83719           sqlite3ExprListDup(db, pStep->pExprList, 0), 
       
 83720           sqlite3ExprDup(db, pStep->pWhere, 0), 
       
 83721           pParse->eOrconf
       
 83722         );
       
 83723         break;
       
 83724       }
       
 83725       case TK_INSERT: {
       
 83726         sqlite3Insert(pParse, 
       
 83727           targetSrcList(pParse, pStep),
       
 83728           sqlite3ExprListDup(db, pStep->pExprList, 0), 
       
 83729           sqlite3SelectDup(db, pStep->pSelect, 0), 
       
 83730           sqlite3IdListDup(db, pStep->pIdList), 
       
 83731           pParse->eOrconf
       
 83732         );
       
 83733         break;
       
 83734       }
       
 83735       case TK_DELETE: {
       
 83736         sqlite3DeleteFrom(pParse, 
       
 83737           targetSrcList(pParse, pStep),
       
 83738           sqlite3ExprDup(db, pStep->pWhere, 0)
       
 83739         );
       
 83740         break;
       
 83741       }
       
 83742       default: assert( pStep->op==TK_SELECT ); {
       
 83743         SelectDest sDest;
       
 83744         Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
       
 83745         sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
       
 83746         sqlite3Select(pParse, pSelect, &sDest);
       
 83747         sqlite3SelectDelete(db, pSelect);
       
 83748         break;
       
 83749       }
       
 83750     } 
       
 83751     if( pStep->op!=TK_SELECT ){
       
 83752       sqlite3VdbeAddOp0(v, OP_ResetCount);
       
 83753     }
       
 83754   }
       
 83755 
       
 83756   return 0;
       
 83757 }
       
 83758 
       
 83759 #ifdef SQLITE_DEBUG
       
 83760 /*
       
 83761 ** This function is used to add VdbeComment() annotations to a VDBE
       
 83762 ** program. It is not used in production code, only for debugging.
       
 83763 */
       
 83764 static const char *onErrorText(int onError){
       
 83765   switch( onError ){
       
 83766     case OE_Abort:    return "abort";
       
 83767     case OE_Rollback: return "rollback";
       
 83768     case OE_Fail:     return "fail";
       
 83769     case OE_Replace:  return "replace";
       
 83770     case OE_Ignore:   return "ignore";
       
 83771     case OE_Default:  return "default";
       
 83772   }
       
 83773   return "n/a";
       
 83774 }
       
 83775 #endif
       
 83776 
       
 83777 /*
       
 83778 ** Parse context structure pFrom has just been used to create a sub-vdbe
       
 83779 ** (trigger program). If an error has occurred, transfer error information
       
 83780 ** from pFrom to pTo.
       
 83781 */
       
 83782 static void transferParseError(Parse *pTo, Parse *pFrom){
       
 83783   assert( pFrom->zErrMsg==0 || pFrom->nErr );
       
 83784   assert( pTo->zErrMsg==0 || pTo->nErr );
       
 83785   if( pTo->nErr==0 ){
       
 83786     pTo->zErrMsg = pFrom->zErrMsg;
       
 83787     pTo->nErr = pFrom->nErr;
       
 83788   }else{
       
 83789     sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
       
 83790   }
       
 83791 }
       
 83792 
       
 83793 /*
       
 83794 ** Create and populate a new TriggerPrg object with a sub-program 
       
 83795 ** implementing trigger pTrigger with ON CONFLICT policy orconf.
       
 83796 */
       
 83797 static TriggerPrg *codeRowTrigger(
       
 83798   Parse *pParse,       /* Current parse context */
       
 83799   Trigger *pTrigger,   /* Trigger to code */
       
 83800   Table *pTab,         /* The table pTrigger is attached to */
       
 83801   int orconf           /* ON CONFLICT policy to code trigger program with */
       
 83802 ){
       
 83803   Parse *pTop = sqlite3ParseToplevel(pParse);
       
 83804   sqlite3 *db = pParse->db;   /* Database handle */
       
 83805   TriggerPrg *pPrg;           /* Value to return */
       
 83806   Expr *pWhen = 0;            /* Duplicate of trigger WHEN expression */
       
 83807   Vdbe *v;                    /* Temporary VM */
       
 83808   NameContext sNC;            /* Name context for sub-vdbe */
       
 83809   SubProgram *pProgram = 0;   /* Sub-vdbe for trigger program */
       
 83810   Parse *pSubParse;           /* Parse context for sub-vdbe */
       
 83811   int iEndTrigger = 0;        /* Label to jump to if WHEN is false */
       
 83812 
       
 83813   assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
       
 83814 
       
 83815   /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
       
 83816   ** are freed if an error occurs, link them into the Parse.pTriggerPrg 
       
 83817   ** list of the top-level Parse object sooner rather than later.  */
       
 83818   pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
       
 83819   if( !pPrg ) return 0;
       
 83820   pPrg->pNext = pTop->pTriggerPrg;
       
 83821   pTop->pTriggerPrg = pPrg;
       
 83822   pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
       
 83823   if( !pProgram ) return 0;
       
 83824   pProgram->nRef = 1;
       
 83825   pPrg->pTrigger = pTrigger;
       
 83826   pPrg->orconf = orconf;
       
 83827   pPrg->oldmask = 0xffffffff;
       
 83828 
       
 83829   /* Allocate and populate a new Parse context to use for coding the 
       
 83830   ** trigger sub-program.  */
       
 83831   pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
       
 83832   if( !pSubParse ) return 0;
       
 83833   memset(&sNC, 0, sizeof(sNC));
       
 83834   sNC.pParse = pSubParse;
       
 83835   pSubParse->db = db;
       
 83836   pSubParse->pTriggerTab = pTab;
       
 83837   pSubParse->pToplevel = pTop;
       
 83838   pSubParse->zAuthContext = pTrigger->zName;
       
 83839   pSubParse->eTriggerOp = pTrigger->op;
       
 83840 
       
 83841   v = sqlite3GetVdbe(pSubParse);
       
 83842   if( v ){
       
 83843     VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", 
       
 83844       pTrigger->zName, onErrorText(orconf),
       
 83845       (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
       
 83846         (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
       
 83847         (pTrigger->op==TK_INSERT ? "INSERT" : ""),
       
 83848         (pTrigger->op==TK_DELETE ? "DELETE" : ""),
       
 83849       pTab->zName
       
 83850     ));
       
 83851 #ifndef SQLITE_OMIT_TRACE
       
 83852     sqlite3VdbeChangeP4(v, -1, 
       
 83853       sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
       
 83854     );
       
 83855 #endif
       
 83856 
       
 83857     /* If one was specified, code the WHEN clause. If it evaluates to false
       
 83858     ** (or NULL) the sub-vdbe is immediately halted by jumping to the 
       
 83859     ** OP_Halt inserted at the end of the program.  */
       
 83860     if( pTrigger->pWhen ){
       
 83861       pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
       
 83862       if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) 
       
 83863        && db->mallocFailed==0 
       
 83864       ){
       
 83865         iEndTrigger = sqlite3VdbeMakeLabel(v);
       
 83866         sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
       
 83867       }
       
 83868       sqlite3ExprDelete(db, pWhen);
       
 83869     }
       
 83870 
       
 83871     /* Code the trigger program into the sub-vdbe. */
       
 83872     codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
       
 83873 
       
 83874     /* Insert an OP_Halt at the end of the sub-program. */
       
 83875     if( iEndTrigger ){
       
 83876       sqlite3VdbeResolveLabel(v, iEndTrigger);
       
 83877     }
       
 83878     sqlite3VdbeAddOp0(v, OP_Halt);
       
 83879     VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
       
 83880 
       
 83881     transferParseError(pParse, pSubParse);
       
 83882     if( db->mallocFailed==0 ){
       
 83883       pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
       
 83884     }
       
 83885     pProgram->nMem = pSubParse->nMem;
       
 83886     pProgram->nCsr = pSubParse->nTab;
       
 83887     pProgram->token = (void *)pTrigger;
       
 83888     pPrg->oldmask = pSubParse->oldmask;
       
 83889     sqlite3VdbeDelete(v);
       
 83890   }
       
 83891 
       
 83892   assert( !pSubParse->pAinc       && !pSubParse->pZombieTab );
       
 83893   assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
       
 83894   sqlite3StackFree(db, pSubParse);
       
 83895 
       
 83896   return pPrg;
       
 83897 }
       
 83898     
       
 83899 /*
       
 83900 ** Return a pointer to a TriggerPrg object containing the sub-program for
       
 83901 ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
       
 83902 ** TriggerPrg object exists, a new object is allocated and populated before
       
 83903 ** being returned.
       
 83904 */
       
 83905 static TriggerPrg *getRowTrigger(
       
 83906   Parse *pParse,       /* Current parse context */
       
 83907   Trigger *pTrigger,   /* Trigger to code */
       
 83908   Table *pTab,         /* The table trigger pTrigger is attached to */
       
 83909   int orconf           /* ON CONFLICT algorithm. */
       
 83910 ){
       
 83911   Parse *pRoot = sqlite3ParseToplevel(pParse);
       
 83912   TriggerPrg *pPrg;
       
 83913 
       
 83914   assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
       
 83915 
       
 83916   /* It may be that this trigger has already been coded (or is in the
       
 83917   ** process of being coded). If this is the case, then an entry with
       
 83918   ** a matching TriggerPrg.pTrigger field will be present somewhere
       
 83919   ** in the Parse.pTriggerPrg list. Search for such an entry.  */
       
 83920   for(pPrg=pRoot->pTriggerPrg; 
       
 83921       pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf); 
       
 83922       pPrg=pPrg->pNext
       
 83923   );
       
 83924 
       
 83925   /* If an existing TriggerPrg could not be located, create a new one. */
       
 83926   if( !pPrg ){
       
 83927     pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
       
 83928   }
       
 83929 
       
 83930   return pPrg;
       
 83931 }
       
 83932 
       
 83933 /*
       
 83934 ** Generate code for the trigger program associated with trigger p on 
       
 83935 ** table pTab. The reg, orconf and ignoreJump parameters passed to this
       
 83936 ** function are the same as those described in the header function for
       
 83937 ** sqlite3CodeRowTrigger()
       
 83938 */
       
 83939 SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
       
 83940   Parse *pParse,       /* Parse context */
       
 83941   Trigger *p,          /* Trigger to code */
       
 83942   Table *pTab,         /* The table to code triggers from */
       
 83943   int reg,             /* Reg array containing OLD.* and NEW.* values */
       
 83944   int orconf,          /* ON CONFLICT policy */
       
 83945   int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
       
 83946 ){
       
 83947   Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
       
 83948   TriggerPrg *pPrg;
       
 83949   pPrg = getRowTrigger(pParse, p, pTab, orconf);
       
 83950   assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
       
 83951 
       
 83952   /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program 
       
 83953   ** is a pointer to the sub-vdbe containing the trigger program.  */
       
 83954   if( pPrg ){
       
 83955     sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
       
 83956     pPrg->pProgram->nRef++;
       
 83957     sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
       
 83958     VdbeComment(
       
 83959         (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
       
 83960 
       
 83961     /* Set the P5 operand of the OP_Program instruction to non-zero if
       
 83962     ** recursive invocation of this trigger program is disallowed. Recursive
       
 83963     ** invocation is disallowed if (a) the sub-program is really a trigger,
       
 83964     ** not a foreign key action, and (b) the flag to enable recursive triggers
       
 83965     ** is clear.  */
       
 83966     sqlite3VdbeChangeP5(v, (u8)(p->zName && !(pParse->db->flags&SQLITE_RecTriggers)));
       
 83967   }
       
 83968 }
       
 83969 
       
 83970 /*
       
 83971 ** This is called to code the required FOR EACH ROW triggers for an operation
       
 83972 ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
       
 83973 ** is given by the op paramater. The tr_tm parameter determines whether the
       
 83974 ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
       
 83975 ** parameter pChanges is passed the list of columns being modified.
       
 83976 **
       
 83977 ** If there are no triggers that fire at the specified time for the specified
       
 83978 ** operation on pTab, this function is a no-op.
       
 83979 **
       
 83980 ** The reg argument is the address of the first in an array of registers 
       
 83981 ** that contain the values substituted for the new.* and old.* references
       
 83982 ** in the trigger program. If N is the number of columns in table pTab
       
 83983 ** (a copy of pTab->nCol), then registers are populated as follows:
       
 83984 **
       
 83985 **   Register       Contains
       
 83986 **   ------------------------------------------------------
       
 83987 **   reg+0          OLD.rowid
       
 83988 **   reg+1          OLD.* value of left-most column of pTab
       
 83989 **   ...            ...
       
 83990 **   reg+N          OLD.* value of right-most column of pTab
       
 83991 **   reg+N+1        NEW.rowid
       
 83992 **   reg+N+2        OLD.* value of left-most column of pTab
       
 83993 **   ...            ...
       
 83994 **   reg+N+N+1      NEW.* value of right-most column of pTab
       
 83995 **
       
 83996 ** For ON DELETE triggers, the registers containing the NEW.* values will
       
 83997 ** never be accessed by the trigger program, so they are not allocated or 
       
 83998 ** populated by the caller (there is no data to populate them with anyway). 
       
 83999 ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
       
 84000 ** are never accessed, and so are not allocated by the caller. So, for an
       
 84001 ** ON INSERT trigger, the value passed to this function as parameter reg
       
 84002 ** is not a readable register, although registers (reg+N) through 
       
 84003 ** (reg+N+N+1) are.
       
 84004 **
       
 84005 ** Parameter orconf is the default conflict resolution algorithm for the
       
 84006 ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
       
 84007 ** is the instruction that control should jump to if a trigger program
       
 84008 ** raises an IGNORE exception.
       
 84009 */
       
 84010 SQLITE_PRIVATE void sqlite3CodeRowTrigger(
       
 84011   Parse *pParse,       /* Parse context */
       
 84012   Trigger *pTrigger,   /* List of triggers on table pTab */
       
 84013   int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
       
 84014   ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
       
 84015   int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
       
 84016   Table *pTab,         /* The table to code triggers from */
       
 84017   int reg,             /* The first in an array of registers (see above) */
       
 84018   int orconf,          /* ON CONFLICT policy */
       
 84019   int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
       
 84020 ){
       
 84021   Trigger *p;          /* Used to iterate through pTrigger list */
       
 84022 
       
 84023   assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
       
 84024   assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
       
 84025   assert( (op==TK_UPDATE)==(pChanges!=0) );
       
 84026 
       
 84027   for(p=pTrigger; p; p=p->pNext){
       
 84028 
       
 84029     /* Sanity checking:  The schema for the trigger and for the table are
       
 84030     ** always defined.  The trigger must be in the same schema as the table
       
 84031     ** or else it must be a TEMP trigger. */
       
 84032     assert( p->pSchema!=0 );
       
 84033     assert( p->pTabSchema!=0 );
       
 84034     assert( p->pSchema==p->pTabSchema 
       
 84035          || p->pSchema==pParse->db->aDb[1].pSchema );
       
 84036 
       
 84037     /* Determine whether we should code this trigger */
       
 84038     if( p->op==op 
       
 84039      && p->tr_tm==tr_tm 
       
 84040      && checkColumnOverlap(p->pColumns, pChanges)
       
 84041     ){
       
 84042       sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
       
 84043     }
       
 84044   }
       
 84045 }
       
 84046 
       
 84047 /*
       
 84048 ** Triggers fired by UPDATE or DELETE statements may access values stored
       
 84049 ** in the old.* pseudo-table. This function returns a 32-bit bitmask
       
 84050 ** indicating which columns of the old.* table actually are used by
       
 84051 ** triggers. This information may be used by the caller to avoid having
       
 84052 ** to load the entire old.* record into memory when executing an UPDATE
       
 84053 ** or DELETE command.
       
 84054 **
       
 84055 ** Bit 0 of the returned mask is set if the left-most column of the
       
 84056 ** table may be accessed using an old.<col> reference. Bit 1 is set if
       
 84057 ** the second leftmost column value is required, and so on. If there
       
 84058 ** are more than 32 columns in the table, and at least one of the columns
       
 84059 ** with an index greater than 32 may be accessed, 0xffffffff is returned.
       
 84060 **
       
 84061 ** It is not possible to determine if the old.rowid column is accessed
       
 84062 ** by triggers. The caller must always assume that it is.
       
 84063 **
       
 84064 ** There is no equivalent function for new.* references.
       
 84065 */
       
 84066 SQLITE_PRIVATE u32 sqlite3TriggerOldmask(
       
 84067   Parse *pParse,       /* Parse context */
       
 84068   Trigger *pTrigger,   /* List of triggers on table pTab */
       
 84069   ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
       
 84070   Table *pTab,         /* The table to code triggers from */
       
 84071   int orconf           /* Default ON CONFLICT policy for trigger steps */
       
 84072 ){
       
 84073   const int op = pChanges ? TK_UPDATE : TK_DELETE;
       
 84074   u32 mask = 0;
       
 84075   Trigger *p;
       
 84076 
       
 84077   for(p=pTrigger; p; p=p->pNext){
       
 84078     if( p->op==op && checkColumnOverlap(p->pColumns,pChanges) ){
       
 84079       TriggerPrg *pPrg;
       
 84080       pPrg = getRowTrigger(pParse, p, pTab, orconf);
       
 84081       if( pPrg ){
       
 84082         mask |= pPrg->oldmask;
       
 84083       }
       
 84084     }
       
 84085   }
       
 84086 
       
 84087   return mask;
       
 84088 }
       
 84089 
       
 84090 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
       
 84091 
       
 84092 /************** End of trigger.c *********************************************/
       
 84093 /************** Begin file update.c ******************************************/
       
 84094 /*
       
 84095 ** 2001 September 15
       
 84096 **
       
 84097 ** The author disclaims copyright to this source code.  In place of
       
 84098 ** a legal notice, here is a blessing:
       
 84099 **
       
 84100 **    May you do good and not evil.
       
 84101 **    May you find forgiveness for yourself and forgive others.
       
 84102 **    May you share freely, never taking more than you give.
       
 84103 **
       
 84104 *************************************************************************
       
 84105 ** This file contains C code routines that are called by the parser
       
 84106 ** to handle UPDATE statements.
       
 84107 **
       
 84108 ** $Id: update.c,v 1.207 2009/08/08 18:01:08 drh Exp $
       
 84109 */
       
 84110 
       
 84111 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 84112 /* Forward declaration */
       
 84113 static void updateVirtualTable(
       
 84114   Parse *pParse,       /* The parsing context */
       
 84115   SrcList *pSrc,       /* The virtual table to be modified */
       
 84116   Table *pTab,         /* The virtual table */
       
 84117   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
       
 84118   Expr *pRowidExpr,    /* Expression used to recompute the rowid */
       
 84119   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
       
 84120   Expr *pWhere         /* WHERE clause of the UPDATE statement */
       
 84121 );
       
 84122 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 84123 
       
 84124 /*
       
 84125 ** The most recently coded instruction was an OP_Column to retrieve the
       
 84126 ** i-th column of table pTab. This routine sets the P4 parameter of the 
       
 84127 ** OP_Column to the default value, if any.
       
 84128 **
       
 84129 ** The default value of a column is specified by a DEFAULT clause in the 
       
 84130 ** column definition. This was either supplied by the user when the table
       
 84131 ** was created, or added later to the table definition by an ALTER TABLE
       
 84132 ** command. If the latter, then the row-records in the table btree on disk
       
 84133 ** may not contain a value for the column and the default value, taken
       
 84134 ** from the P4 parameter of the OP_Column instruction, is returned instead.
       
 84135 ** If the former, then all row-records are guaranteed to include a value
       
 84136 ** for the column and the P4 value is not required.
       
 84137 **
       
 84138 ** Column definitions created by an ALTER TABLE command may only have 
       
 84139 ** literal default values specified: a number, null or a string. (If a more
       
 84140 ** complicated default expression value was provided, it is evaluated 
       
 84141 ** when the ALTER TABLE is executed and one of the literal values written
       
 84142 ** into the sqlite_master table.)
       
 84143 **
       
 84144 ** Therefore, the P4 parameter is only required if the default value for
       
 84145 ** the column is a literal number, string or null. The sqlite3ValueFromExpr()
       
 84146 ** function is capable of transforming these types of expressions into
       
 84147 ** sqlite3_value objects.
       
 84148 **
       
 84149 ** If parameter iReg is not negative, code an OP_RealAffinity instruction
       
 84150 ** on register iReg. This is used when an equivalent integer value is 
       
 84151 ** stored in place of an 8-byte floating point value in order to save 
       
 84152 ** space.
       
 84153 */
       
 84154 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
       
 84155   assert( pTab!=0 );
       
 84156   if( !pTab->pSelect ){
       
 84157     sqlite3_value *pValue;
       
 84158     u8 enc = ENC(sqlite3VdbeDb(v));
       
 84159     Column *pCol = &pTab->aCol[i];
       
 84160     VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
       
 84161     assert( i<pTab->nCol );
       
 84162     sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, 
       
 84163                          pCol->affinity, &pValue);
       
 84164     if( pValue ){
       
 84165       sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
       
 84166     }
       
 84167 #ifndef SQLITE_OMIT_FLOATING_POINT
       
 84168     if( iReg>=0 && pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
       
 84169       sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
       
 84170     }
       
 84171 #endif
       
 84172   }
       
 84173 }
       
 84174 
       
 84175 /*
       
 84176 ** Process an UPDATE statement.
       
 84177 **
       
 84178 **   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
       
 84179 **          \_______/ \________/     \______/       \________________/
       
 84180 *            onError   pTabList      pChanges             pWhere
       
 84181 */
       
 84182 SQLITE_PRIVATE void sqlite3Update(
       
 84183   Parse *pParse,         /* The parser context */
       
 84184   SrcList *pTabList,     /* The table in which we should change things */
       
 84185   ExprList *pChanges,    /* Things to be changed */
       
 84186   Expr *pWhere,          /* The WHERE clause.  May be null */
       
 84187   int onError            /* How to handle constraint errors */
       
 84188 ){
       
 84189   int i, j;              /* Loop counters */
       
 84190   Table *pTab;           /* The table to be updated */
       
 84191   int addr = 0;          /* VDBE instruction address of the start of the loop */
       
 84192   WhereInfo *pWInfo;     /* Information about the WHERE clause */
       
 84193   Vdbe *v;               /* The virtual database engine */
       
 84194   Index *pIdx;           /* For looping over indices */
       
 84195   int nIdx;              /* Number of indices that need updating */
       
 84196   int iCur;              /* VDBE Cursor number of pTab */
       
 84197   sqlite3 *db;           /* The database structure */
       
 84198   int *aRegIdx = 0;      /* One register assigned to each index to be updated */
       
 84199   int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
       
 84200                          ** an expression for the i-th column of the table.
       
 84201                          ** aXRef[i]==-1 if the i-th column is not changed. */
       
 84202   int chngRowid;         /* True if the record number is being changed */
       
 84203   Expr *pRowidExpr = 0;  /* Expression defining the new record number */
       
 84204   int openAll = 0;       /* True if all indices need to be opened */
       
 84205   AuthContext sContext;  /* The authorization context */
       
 84206   NameContext sNC;       /* The name-context to resolve expressions in */
       
 84207   int iDb;               /* Database containing the table being updated */
       
 84208   int j1;                /* Addresses of jump instructions */
       
 84209   int okOnePass;         /* True for one-pass algorithm without the FIFO */
       
 84210   int hasFK;             /* True if foreign key processing is required */
       
 84211 
       
 84212 #ifndef SQLITE_OMIT_TRIGGER
       
 84213   int isView;                  /* Trying to update a view */
       
 84214   Trigger *pTrigger;           /* List of triggers on pTab, if required */
       
 84215 #endif
       
 84216 
       
 84217   /* Register Allocations */
       
 84218   int regRowCount = 0;   /* A count of rows changed */
       
 84219   int regOldRowid;       /* The old rowid */
       
 84220   int regNewRowid;       /* The new rowid */
       
 84221   int regNew;
       
 84222   int regOld = 0;
       
 84223   int regRowSet = 0;     /* Rowset of rows to be updated */
       
 84224   int regRec;            /* Register used for new table record to insert */
       
 84225 
       
 84226   memset(&sContext, 0, sizeof(sContext));
       
 84227   db = pParse->db;
       
 84228   if( pParse->nErr || db->mallocFailed ){
       
 84229     goto update_cleanup;
       
 84230   }
       
 84231   assert( pTabList->nSrc==1 );
       
 84232 
       
 84233   /* Locate the table which we want to update. 
       
 84234   */
       
 84235   pTab = sqlite3SrcListLookup(pParse, pTabList);
       
 84236   if( pTab==0 ) goto update_cleanup;
       
 84237   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
       
 84238 
       
 84239   /* Figure out if we have any triggers and if the table being
       
 84240   ** updated is a view.
       
 84241   */
       
 84242 #ifndef SQLITE_OMIT_TRIGGER
       
 84243   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, 0);
       
 84244   isView = pTab->pSelect!=0;
       
 84245 #else
       
 84246 # define pTrigger 0
       
 84247 # define isView 0
       
 84248 #endif
       
 84249 #ifdef SQLITE_OMIT_VIEW
       
 84250 # undef isView
       
 84251 # define isView 0
       
 84252 #endif
       
 84253 
       
 84254   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
       
 84255     goto update_cleanup;
       
 84256   }
       
 84257   if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
       
 84258     goto update_cleanup;
       
 84259   }
       
 84260   aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
       
 84261   if( aXRef==0 ) goto update_cleanup;
       
 84262   for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
       
 84263 
       
 84264   /* Allocate a cursors for the main database table and for all indices.
       
 84265   ** The index cursors might not be used, but if they are used they
       
 84266   ** need to occur right after the database cursor.  So go ahead and
       
 84267   ** allocate enough space, just in case.
       
 84268   */
       
 84269   pTabList->a[0].iCursor = iCur = pParse->nTab++;
       
 84270   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       
 84271     pParse->nTab++;
       
 84272   }
       
 84273 
       
 84274   /* Initialize the name-context */
       
 84275   memset(&sNC, 0, sizeof(sNC));
       
 84276   sNC.pParse = pParse;
       
 84277   sNC.pSrcList = pTabList;
       
 84278 
       
 84279   /* Resolve the column names in all the expressions of the
       
 84280   ** of the UPDATE statement.  Also find the column index
       
 84281   ** for each column to be updated in the pChanges array.  For each
       
 84282   ** column to be updated, make sure we have authorization to change
       
 84283   ** that column.
       
 84284   */
       
 84285   chngRowid = 0;
       
 84286   for(i=0; i<pChanges->nExpr; i++){
       
 84287     if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
       
 84288       goto update_cleanup;
       
 84289     }
       
 84290     for(j=0; j<pTab->nCol; j++){
       
 84291       if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
       
 84292         if( j==pTab->iPKey ){
       
 84293           chngRowid = 1;
       
 84294           pRowidExpr = pChanges->a[i].pExpr;
       
 84295         }
       
 84296         aXRef[j] = i;
       
 84297         break;
       
 84298       }
       
 84299     }
       
 84300     if( j>=pTab->nCol ){
       
 84301       if( sqlite3IsRowid(pChanges->a[i].zName) ){
       
 84302         chngRowid = 1;
       
 84303         pRowidExpr = pChanges->a[i].pExpr;
       
 84304       }else{
       
 84305         sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
       
 84306         goto update_cleanup;
       
 84307       }
       
 84308     }
       
 84309 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 84310     {
       
 84311       int rc;
       
 84312       rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
       
 84313                            pTab->aCol[j].zName, db->aDb[iDb].zName);
       
 84314       if( rc==SQLITE_DENY ){
       
 84315         goto update_cleanup;
       
 84316       }else if( rc==SQLITE_IGNORE ){
       
 84317         aXRef[j] = -1;
       
 84318       }
       
 84319     }
       
 84320 #endif
       
 84321   }
       
 84322 
       
 84323   hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngRowid);
       
 84324 
       
 84325   /* Allocate memory for the array aRegIdx[].  There is one entry in the
       
 84326   ** array for each index associated with table being updated.  Fill in
       
 84327   ** the value with a register number for indices that are to be used
       
 84328   ** and with zero for unused indices.
       
 84329   */
       
 84330   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
       
 84331   if( nIdx>0 ){
       
 84332     aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
       
 84333     if( aRegIdx==0 ) goto update_cleanup;
       
 84334   }
       
 84335   for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
       
 84336     int reg;
       
 84337     if( chngRowid ){
       
 84338       reg = ++pParse->nMem;
       
 84339     }else{
       
 84340       reg = 0;
       
 84341       for(i=0; i<pIdx->nColumn; i++){
       
 84342         if( aXRef[pIdx->aiColumn[i]]>=0 ){
       
 84343           reg = ++pParse->nMem;
       
 84344           break;
       
 84345         }
       
 84346       }
       
 84347     }
       
 84348     aRegIdx[j] = reg;
       
 84349   }
       
 84350 
       
 84351   /* Begin generating code. */
       
 84352   v = sqlite3GetVdbe(pParse);
       
 84353   if( v==0 ) goto update_cleanup;
       
 84354   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
       
 84355   sqlite3BeginWriteOperation(pParse, 1, iDb);
       
 84356 
       
 84357 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 84358   /* Virtual tables must be handled separately */
       
 84359   if( IsVirtual(pTab) ){
       
 84360     updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
       
 84361                        pWhere);
       
 84362     pWhere = 0;
       
 84363     pTabList = 0;
       
 84364     goto update_cleanup;
       
 84365   }
       
 84366 #endif
       
 84367 
       
 84368   /* Allocate required registers. */
       
 84369   regOldRowid = regNewRowid = ++pParse->nMem;
       
 84370   if( pTrigger || hasFK ){
       
 84371     regOld = pParse->nMem + 1;
       
 84372     pParse->nMem += pTab->nCol;
       
 84373   }
       
 84374   if( chngRowid || pTrigger || hasFK ){
       
 84375     regNewRowid = ++pParse->nMem;
       
 84376   }
       
 84377   regNew = pParse->nMem + 1;
       
 84378   pParse->nMem += pTab->nCol;
       
 84379   regRec = ++pParse->nMem;
       
 84380 
       
 84381   /* Start the view context. */
       
 84382   if( isView ){
       
 84383     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
       
 84384   }
       
 84385 
       
 84386   /* If we are trying to update a view, realize that view into
       
 84387   ** a ephemeral table.
       
 84388   */
       
 84389 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
       
 84390   if( isView ){
       
 84391     sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
       
 84392   }
       
 84393 #endif
       
 84394 
       
 84395   /* Resolve the column names in all the expressions in the
       
 84396   ** WHERE clause.
       
 84397   */
       
 84398   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
       
 84399     goto update_cleanup;
       
 84400   }
       
 84401 
       
 84402   /* Begin the database scan
       
 84403   */
       
 84404   sqlite3VdbeAddOp2(v, OP_Null, 0, regOldRowid);
       
 84405   pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0, WHERE_ONEPASS_DESIRED);
       
 84406   if( pWInfo==0 ) goto update_cleanup;
       
 84407   okOnePass = pWInfo->okOnePass;
       
 84408 
       
 84409   /* Remember the rowid of every item to be updated.
       
 84410   */
       
 84411   sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
       
 84412   if( !okOnePass ){
       
 84413     regRowSet = ++pParse->nMem;
       
 84414     sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
       
 84415   }
       
 84416 
       
 84417   /* End the database scan loop.
       
 84418   */
       
 84419   sqlite3WhereEnd(pWInfo);
       
 84420 
       
 84421   /* Initialize the count of updated rows
       
 84422   */
       
 84423   if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
       
 84424     regRowCount = ++pParse->nMem;
       
 84425     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
       
 84426   }
       
 84427 
       
 84428   if( !isView ){
       
 84429     /* 
       
 84430     ** Open every index that needs updating.  Note that if any
       
 84431     ** index could potentially invoke a REPLACE conflict resolution 
       
 84432     ** action, then we need to open all indices because we might need
       
 84433     ** to be deleting some records.
       
 84434     */
       
 84435     if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); 
       
 84436     if( onError==OE_Replace ){
       
 84437       openAll = 1;
       
 84438     }else{
       
 84439       openAll = 0;
       
 84440       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       
 84441         if( pIdx->onError==OE_Replace ){
       
 84442           openAll = 1;
       
 84443           break;
       
 84444         }
       
 84445       }
       
 84446     }
       
 84447     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
       
 84448       if( openAll || aRegIdx[i]>0 ){
       
 84449         KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
       
 84450         sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
       
 84451                        (char*)pKey, P4_KEYINFO_HANDOFF);
       
 84452         assert( pParse->nTab>iCur+i+1 );
       
 84453       }
       
 84454     }
       
 84455   }
       
 84456 
       
 84457   /* Top of the update loop */
       
 84458   if( okOnePass ){
       
 84459     int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid);
       
 84460     addr = sqlite3VdbeAddOp0(v, OP_Goto);
       
 84461     sqlite3VdbeJumpHere(v, a1);
       
 84462   }else{
       
 84463     addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid);
       
 84464   }
       
 84465 
       
 84466   /* Make cursor iCur point to the record that is being updated. If
       
 84467   ** this record does not exist for some reason (deleted by a trigger,
       
 84468   ** for example, then jump to the next iteration of the RowSet loop.  */
       
 84469   sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
       
 84470 
       
 84471   /* If the record number will change, set register regNewRowid to
       
 84472   ** contain the new value. If the record number is not being modified,
       
 84473   ** then regNewRowid is the same register as regOldRowid, which is
       
 84474   ** already populated.  */
       
 84475   assert( chngRowid || pTrigger || hasFK || regOldRowid==regNewRowid );
       
 84476   if( chngRowid ){
       
 84477     sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
       
 84478     sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
       
 84479   }
       
 84480 
       
 84481   /* If there are triggers on this table, populate an array of registers 
       
 84482   ** with the required old.* column data.  */
       
 84483   if( hasFK || pTrigger ){
       
 84484     u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
       
 84485     oldmask |= sqlite3TriggerOldmask(pParse, pTrigger, pChanges, pTab, onError);
       
 84486     for(i=0; i<pTab->nCol; i++){
       
 84487       if( aXRef[i]<0 || oldmask==0xffffffff || (oldmask & (1<<i)) ){
       
 84488         sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regOld+i);
       
 84489         sqlite3ColumnDefault(v, pTab, i, regOld+i);
       
 84490       }else{
       
 84491         sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
       
 84492       }
       
 84493     }
       
 84494     if( chngRowid==0 ){
       
 84495       sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
       
 84496     }
       
 84497   }
       
 84498 
       
 84499   /* Populate the array of registers beginning at regNew with the new
       
 84500   ** row data. This array is used to check constaints, create the new
       
 84501   ** table and index records, and as the values for any new.* references
       
 84502   ** made by triggers.  */
       
 84503   for(i=0; i<pTab->nCol; i++){
       
 84504     if( i==pTab->iPKey ){
       
 84505       sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
       
 84506     }else{
       
 84507       j = aXRef[i];
       
 84508       if( j<0 ){
       
 84509         sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
       
 84510         sqlite3ColumnDefault(v, pTab, i, regNew+i);
       
 84511       }else{
       
 84512         sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
       
 84513       }
       
 84514     }
       
 84515   }
       
 84516 
       
 84517   /* Fire any BEFORE UPDATE triggers. This happens before constraints are
       
 84518   ** verified. One could argue that this is wrong.  */
       
 84519   if( pTrigger ){
       
 84520     sqlite3VdbeAddOp2(v, OP_Affinity, regNew, pTab->nCol);
       
 84521     sqlite3TableAffinityStr(v, pTab);
       
 84522     sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
       
 84523         TRIGGER_BEFORE, pTab, regOldRowid, onError, addr);
       
 84524 
       
 84525     /* The row-trigger may have deleted the row being updated. In this
       
 84526     ** case, jump to the next row. No updates or AFTER triggers are 
       
 84527     ** required. This behaviour - what happens when the row being updated
       
 84528     ** is deleted or renamed by a BEFORE trigger - is left undefined in the
       
 84529     ** documentation.  */
       
 84530     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
       
 84531   }
       
 84532 
       
 84533   if( !isView ){
       
 84534 
       
 84535     /* Do constraint checks. */
       
 84536     sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
       
 84537         aRegIdx, (chngRowid?regOldRowid:0), 1, onError, addr, 0);
       
 84538 
       
 84539     /* Do FK constraint checks. */
       
 84540     if( hasFK ){
       
 84541       sqlite3FkCheck(pParse, pTab, regOldRowid, 0);
       
 84542     }
       
 84543 
       
 84544     /* Delete the index entries associated with the current record.  */
       
 84545     j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid);
       
 84546     sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
       
 84547   
       
 84548     /* If changing the record number, delete the old record.  */
       
 84549     if( hasFK || chngRowid ){
       
 84550       sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
       
 84551     }
       
 84552     sqlite3VdbeJumpHere(v, j1);
       
 84553 
       
 84554     if( hasFK ){
       
 84555       sqlite3FkCheck(pParse, pTab, 0, regNewRowid);
       
 84556     }
       
 84557   
       
 84558     /* Insert the new index entries and the new record. */
       
 84559     sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, aRegIdx, 1, 0, 0);
       
 84560 
       
 84561     /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
       
 84562     ** handle rows (possibly in other tables) that refer via a foreign key
       
 84563     ** to the row just updated. */ 
       
 84564     if( hasFK ){
       
 84565       sqlite3FkActions(pParse, pTab, pChanges, regOldRowid);
       
 84566     }
       
 84567   }
       
 84568 
       
 84569   /* Increment the row counter 
       
 84570   */
       
 84571   if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
       
 84572     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
       
 84573   }
       
 84574 
       
 84575   sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
       
 84576       TRIGGER_AFTER, pTab, regOldRowid, onError, addr);
       
 84577 
       
 84578   /* Repeat the above with the next record to be updated, until
       
 84579   ** all record selected by the WHERE clause have been updated.
       
 84580   */
       
 84581   sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
       
 84582   sqlite3VdbeJumpHere(v, addr);
       
 84583 
       
 84584   /* Close all tables */
       
 84585   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
       
 84586     if( openAll || aRegIdx[i]>0 ){
       
 84587       sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
       
 84588     }
       
 84589   }
       
 84590   sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
       
 84591 
       
 84592   /* Update the sqlite_sequence table by storing the content of the
       
 84593   ** maximum rowid counter values recorded while inserting into
       
 84594   ** autoincrement tables.
       
 84595   */
       
 84596   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
       
 84597     sqlite3AutoincrementEnd(pParse);
       
 84598   }
       
 84599 
       
 84600   /*
       
 84601   ** Return the number of rows that were changed. If this routine is 
       
 84602   ** generating code because of a call to sqlite3NestedParse(), do not
       
 84603   ** invoke the callback function.
       
 84604   */
       
 84605   if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
       
 84606     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
       
 84607     sqlite3VdbeSetNumCols(v, 1);
       
 84608     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
       
 84609   }
       
 84610 
       
 84611 update_cleanup:
       
 84612   sqlite3AuthContextPop(&sContext);
       
 84613   sqlite3DbFree(db, aRegIdx);
       
 84614   sqlite3DbFree(db, aXRef);
       
 84615   sqlite3SrcListDelete(db, pTabList);
       
 84616   sqlite3ExprListDelete(db, pChanges);
       
 84617   sqlite3ExprDelete(db, pWhere);
       
 84618   return;
       
 84619 }
       
 84620 /* Make sure "isView" and other macros defined above are undefined. Otherwise
       
 84621 ** thely may interfere with compilation of other functions in this file
       
 84622 ** (or in another file, if this file becomes part of the amalgamation).  */
       
 84623 #ifdef isView
       
 84624  #undef isView
       
 84625 #endif
       
 84626 #ifdef pTrigger
       
 84627  #undef pTrigger
       
 84628 #endif
       
 84629 
       
 84630 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 84631 /*
       
 84632 ** Generate code for an UPDATE of a virtual table.
       
 84633 **
       
 84634 ** The strategy is that we create an ephemerial table that contains
       
 84635 ** for each row to be changed:
       
 84636 **
       
 84637 **   (A)  The original rowid of that row.
       
 84638 **   (B)  The revised rowid for the row. (note1)
       
 84639 **   (C)  The content of every column in the row.
       
 84640 **
       
 84641 ** Then we loop over this ephemeral table and for each row in
       
 84642 ** the ephermeral table call VUpdate.
       
 84643 **
       
 84644 ** When finished, drop the ephemeral table.
       
 84645 **
       
 84646 ** (note1) Actually, if we know in advance that (A) is always the same
       
 84647 ** as (B) we only store (A), then duplicate (A) when pulling
       
 84648 ** it out of the ephemeral table before calling VUpdate.
       
 84649 */
       
 84650 static void updateVirtualTable(
       
 84651   Parse *pParse,       /* The parsing context */
       
 84652   SrcList *pSrc,       /* The virtual table to be modified */
       
 84653   Table *pTab,         /* The virtual table */
       
 84654   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
       
 84655   Expr *pRowid,        /* Expression used to recompute the rowid */
       
 84656   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
       
 84657   Expr *pWhere         /* WHERE clause of the UPDATE statement */
       
 84658 ){
       
 84659   Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
       
 84660   ExprList *pEList = 0;     /* The result set of the SELECT statement */
       
 84661   Select *pSelect = 0;      /* The SELECT statement */
       
 84662   Expr *pExpr;              /* Temporary expression */
       
 84663   int ephemTab;             /* Table holding the result of the SELECT */
       
 84664   int i;                    /* Loop counter */
       
 84665   int addr;                 /* Address of top of loop */
       
 84666   int iReg;                 /* First register in set passed to OP_VUpdate */
       
 84667   sqlite3 *db = pParse->db; /* Database connection */
       
 84668   const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
       
 84669   SelectDest dest;
       
 84670 
       
 84671   /* Construct the SELECT statement that will find the new values for
       
 84672   ** all updated rows. 
       
 84673   */
       
 84674   pEList = sqlite3ExprListAppend(pParse, 0, 
       
 84675                                  sqlite3CreateIdExpr(pParse, "_rowid_"));
       
 84676   if( pRowid ){
       
 84677     pEList = sqlite3ExprListAppend(pParse, pEList,
       
 84678                                    sqlite3ExprDup(db, pRowid, 0));
       
 84679   }
       
 84680   assert( pTab->iPKey<0 );
       
 84681   for(i=0; i<pTab->nCol; i++){
       
 84682     if( aXRef[i]>=0 ){
       
 84683       pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
       
 84684     }else{
       
 84685       pExpr = sqlite3CreateIdExpr(pParse, pTab->aCol[i].zName);
       
 84686     }
       
 84687     pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
       
 84688   }
       
 84689   pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
       
 84690   
       
 84691   /* Create the ephemeral table into which the update results will
       
 84692   ** be stored.
       
 84693   */
       
 84694   assert( v );
       
 84695   ephemTab = pParse->nTab++;
       
 84696   sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
       
 84697 
       
 84698   /* fill the ephemeral table 
       
 84699   */
       
 84700   sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
       
 84701   sqlite3Select(pParse, pSelect, &dest);
       
 84702 
       
 84703   /* Generate code to scan the ephemeral table and call VUpdate. */
       
 84704   iReg = ++pParse->nMem;
       
 84705   pParse->nMem += pTab->nCol+1;
       
 84706   addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
       
 84707   sqlite3VdbeAddOp3(v, OP_Column,  ephemTab, 0, iReg);
       
 84708   sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
       
 84709   for(i=0; i<pTab->nCol; i++){
       
 84710     sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
       
 84711   }
       
 84712   sqlite3VtabMakeWritable(pParse, pTab);
       
 84713   sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
       
 84714   sqlite3MayAbort(pParse);
       
 84715   sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1);
       
 84716   sqlite3VdbeJumpHere(v, addr);
       
 84717   sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
       
 84718 
       
 84719   /* Cleanup */
       
 84720   sqlite3SelectDelete(db, pSelect);  
       
 84721 }
       
 84722 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 84723 
       
 84724 /************** End of update.c **********************************************/
       
 84725 /************** Begin file vacuum.c ******************************************/
       
 84726 /*
       
 84727 ** 2003 April 6
       
 84728 **
       
 84729 ** The author disclaims copyright to this source code.  In place of
       
 84730 ** a legal notice, here is a blessing:
       
 84731 **
       
 84732 **    May you do good and not evil.
       
 84733 **    May you find forgiveness for yourself and forgive others.
       
 84734 **    May you share freely, never taking more than you give.
       
 84735 **
       
 84736 *************************************************************************
       
 84737 ** This file contains code used to implement the VACUUM command.
       
 84738 **
       
 84739 ** Most of the code in this file may be omitted by defining the
       
 84740 ** SQLITE_OMIT_VACUUM macro.
       
 84741 **
       
 84742 ** $Id: vacuum.c,v 1.91 2009/07/02 07:47:33 danielk1977 Exp $
       
 84743 */
       
 84744 
       
 84745 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
       
 84746 /*
       
 84747 ** Execute zSql on database db. Return an error code.
       
 84748 */
       
 84749 static int execSql(sqlite3 *db, const char *zSql){
       
 84750   sqlite3_stmt *pStmt;
       
 84751   VVA_ONLY( int rc; )
       
 84752   if( !zSql ){
       
 84753     return SQLITE_NOMEM;
       
 84754   }
       
 84755   if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
       
 84756     return sqlite3_errcode(db);
       
 84757   }
       
 84758   VVA_ONLY( rc = ) sqlite3_step(pStmt);
       
 84759   assert( rc!=SQLITE_ROW );
       
 84760   return sqlite3_finalize(pStmt);
       
 84761 }
       
 84762 
       
 84763 /*
       
 84764 ** Execute zSql on database db. The statement returns exactly
       
 84765 ** one column. Execute this as SQL on the same database.
       
 84766 */
       
 84767 static int execExecSql(sqlite3 *db, const char *zSql){
       
 84768   sqlite3_stmt *pStmt;
       
 84769   int rc;
       
 84770 
       
 84771   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
       
 84772   if( rc!=SQLITE_OK ) return rc;
       
 84773 
       
 84774   while( SQLITE_ROW==sqlite3_step(pStmt) ){
       
 84775     rc = execSql(db, (char*)sqlite3_column_text(pStmt, 0));
       
 84776     if( rc!=SQLITE_OK ){
       
 84777       sqlite3_finalize(pStmt);
       
 84778       return rc;
       
 84779     }
       
 84780   }
       
 84781 
       
 84782   return sqlite3_finalize(pStmt);
       
 84783 }
       
 84784 
       
 84785 /*
       
 84786 ** The non-standard VACUUM command is used to clean up the database,
       
 84787 ** collapse free space, etc.  It is modelled after the VACUUM command
       
 84788 ** in PostgreSQL.
       
 84789 **
       
 84790 ** In version 1.0.x of SQLite, the VACUUM command would call
       
 84791 ** gdbm_reorganize() on all the database tables.  But beginning
       
 84792 ** with 2.0.0, SQLite no longer uses GDBM so this command has
       
 84793 ** become a no-op.
       
 84794 */
       
 84795 SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
       
 84796   Vdbe *v = sqlite3GetVdbe(pParse);
       
 84797   if( v ){
       
 84798     sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
       
 84799   }
       
 84800   return;
       
 84801 }
       
 84802 
       
 84803 /*
       
 84804 ** This routine implements the OP_Vacuum opcode of the VDBE.
       
 84805 */
       
 84806 SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
       
 84807   int rc = SQLITE_OK;     /* Return code from service routines */
       
 84808   Btree *pMain;           /* The database being vacuumed */
       
 84809   Btree *pTemp;           /* The temporary database we vacuum into */
       
 84810   char *zSql = 0;         /* SQL statements */
       
 84811   int saved_flags;        /* Saved value of the db->flags */
       
 84812   int saved_nChange;      /* Saved value of db->nChange */
       
 84813   int saved_nTotalChange; /* Saved value of db->nTotalChange */
       
 84814   Db *pDb = 0;            /* Database to detach at end of vacuum */
       
 84815   int isMemDb;            /* True if vacuuming a :memory: database */
       
 84816   int nRes;
       
 84817 
       
 84818   if( !db->autoCommit ){
       
 84819     sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
       
 84820     return SQLITE_ERROR;
       
 84821   }
       
 84822 
       
 84823   /* Save the current value of the database flags so that it can be 
       
 84824   ** restored before returning. Then set the writable-schema flag, and
       
 84825   ** disable CHECK and foreign key constraints.  */
       
 84826   saved_flags = db->flags;
       
 84827   saved_nChange = db->nChange;
       
 84828   saved_nTotalChange = db->nTotalChange;
       
 84829   db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
       
 84830   db->flags &= ~SQLITE_ForeignKeys;
       
 84831 
       
 84832   pMain = db->aDb[0].pBt;
       
 84833   isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
       
 84834 
       
 84835   /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
       
 84836   ** can be set to 'off' for this file, as it is not recovered if a crash
       
 84837   ** occurs anyway. The integrity of the database is maintained by a
       
 84838   ** (possibly synchronous) transaction opened on the main database before
       
 84839   ** sqlite3BtreeCopyFile() is called.
       
 84840   **
       
 84841   ** An optimisation would be to use a non-journaled pager.
       
 84842   ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
       
 84843   ** that actually made the VACUUM run slower.  Very little journalling
       
 84844   ** actually occurs when doing a vacuum since the vacuum_db is initially
       
 84845   ** empty.  Only the journal header is written.  Apparently it takes more
       
 84846   ** time to parse and run the PRAGMA to turn journalling off than it does
       
 84847   ** to write the journal header file.
       
 84848   */
       
 84849   zSql = "ATTACH '' AS vacuum_db;";
       
 84850   rc = execSql(db, zSql);
       
 84851   if( rc!=SQLITE_OK ) goto end_of_vacuum;
       
 84852   pDb = &db->aDb[db->nDb-1];
       
 84853   assert( strcmp(db->aDb[db->nDb-1].zName,"vacuum_db")==0 );
       
 84854   pTemp = db->aDb[db->nDb-1].pBt;
       
 84855 
       
 84856   nRes = sqlite3BtreeGetReserve(pMain);
       
 84857 
       
 84858   /* A VACUUM cannot change the pagesize of an encrypted database. */
       
 84859 #ifdef SQLITE_HAS_CODEC
       
 84860   if( db->nextPagesize ){
       
 84861     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
       
 84862     int nKey;
       
 84863     char *zKey;
       
 84864     sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
       
 84865     if( nKey ) db->nextPagesize = 0;
       
 84866   }
       
 84867 #endif
       
 84868 
       
 84869   if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
       
 84870    || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
       
 84871    || NEVER(db->mallocFailed)
       
 84872   ){
       
 84873     rc = SQLITE_NOMEM;
       
 84874     goto end_of_vacuum;
       
 84875   }
       
 84876   rc = execSql(db, "PRAGMA vacuum_db.synchronous=OFF");
       
 84877   if( rc!=SQLITE_OK ){
       
 84878     goto end_of_vacuum;
       
 84879   }
       
 84880 
       
 84881 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 84882   sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
       
 84883                                            sqlite3BtreeGetAutoVacuum(pMain));
       
 84884 #endif
       
 84885 
       
 84886   /* Begin a transaction */
       
 84887   rc = execSql(db, "BEGIN EXCLUSIVE;");
       
 84888   if( rc!=SQLITE_OK ) goto end_of_vacuum;
       
 84889 
       
 84890   /* Query the schema of the main database. Create a mirror schema
       
 84891   ** in the temporary database.
       
 84892   */
       
 84893   rc = execExecSql(db, 
       
 84894       "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
       
 84895       "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
       
 84896       "   AND rootpage>0"
       
 84897   );
       
 84898   if( rc!=SQLITE_OK ) goto end_of_vacuum;
       
 84899   rc = execExecSql(db, 
       
 84900       "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
       
 84901       "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
       
 84902   if( rc!=SQLITE_OK ) goto end_of_vacuum;
       
 84903   rc = execExecSql(db, 
       
 84904       "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
       
 84905       "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
       
 84906   if( rc!=SQLITE_OK ) goto end_of_vacuum;
       
 84907 
       
 84908   /* Loop through the tables in the main database. For each, do
       
 84909   ** an "INSERT INTO vacuum_db.xxx SELECT * FROM xxx;" to copy
       
 84910   ** the contents to the temporary database.
       
 84911   */
       
 84912   rc = execExecSql(db, 
       
 84913       "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
       
 84914       "|| ' SELECT * FROM ' || quote(name) || ';'"
       
 84915       "FROM sqlite_master "
       
 84916       "WHERE type = 'table' AND name!='sqlite_sequence' "
       
 84917       "  AND rootpage>0"
       
 84918 
       
 84919   );
       
 84920   if( rc!=SQLITE_OK ) goto end_of_vacuum;
       
 84921 
       
 84922   /* Copy over the sequence table
       
 84923   */
       
 84924   rc = execExecSql(db, 
       
 84925       "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
       
 84926       "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
       
 84927   );
       
 84928   if( rc!=SQLITE_OK ) goto end_of_vacuum;
       
 84929   rc = execExecSql(db, 
       
 84930       "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
       
 84931       "|| ' SELECT * FROM ' || quote(name) || ';' "
       
 84932       "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
       
 84933   );
       
 84934   if( rc!=SQLITE_OK ) goto end_of_vacuum;
       
 84935 
       
 84936 
       
 84937   /* Copy the triggers, views, and virtual tables from the main database
       
 84938   ** over to the temporary database.  None of these objects has any
       
 84939   ** associated storage, so all we have to do is copy their entries
       
 84940   ** from the SQLITE_MASTER table.
       
 84941   */
       
 84942   rc = execSql(db,
       
 84943       "INSERT INTO vacuum_db.sqlite_master "
       
 84944       "  SELECT type, name, tbl_name, rootpage, sql"
       
 84945       "    FROM sqlite_master"
       
 84946       "   WHERE type='view' OR type='trigger'"
       
 84947       "      OR (type='table' AND rootpage=0)"
       
 84948   );
       
 84949   if( rc ) goto end_of_vacuum;
       
 84950 
       
 84951   /* At this point, unless the main db was completely empty, there is now a
       
 84952   ** transaction open on the vacuum database, but not on the main database.
       
 84953   ** Open a btree level transaction on the main database. This allows a
       
 84954   ** call to sqlite3BtreeCopyFile(). The main database btree level
       
 84955   ** transaction is then committed, so the SQL level never knows it was
       
 84956   ** opened for writing. This way, the SQL transaction used to create the
       
 84957   ** temporary database never needs to be committed.
       
 84958   */
       
 84959   {
       
 84960     u32 meta;
       
 84961     int i;
       
 84962 
       
 84963     /* This array determines which meta meta values are preserved in the
       
 84964     ** vacuum.  Even entries are the meta value number and odd entries
       
 84965     ** are an increment to apply to the meta value after the vacuum.
       
 84966     ** The increment is used to increase the schema cookie so that other
       
 84967     ** connections to the same database will know to reread the schema.
       
 84968     */
       
 84969     static const unsigned char aCopy[] = {
       
 84970        BTREE_SCHEMA_VERSION,     1,  /* Add one to the old schema cookie */
       
 84971        BTREE_DEFAULT_CACHE_SIZE, 0,  /* Preserve the default page cache size */
       
 84972        BTREE_TEXT_ENCODING,      0,  /* Preserve the text encoding */
       
 84973        BTREE_USER_VERSION,       0,  /* Preserve the user version */
       
 84974     };
       
 84975 
       
 84976     assert( 1==sqlite3BtreeIsInTrans(pTemp) );
       
 84977     assert( 1==sqlite3BtreeIsInTrans(pMain) );
       
 84978 
       
 84979     /* Copy Btree meta values */
       
 84980     for(i=0; i<ArraySize(aCopy); i+=2){
       
 84981       /* GetMeta() and UpdateMeta() cannot fail in this context because
       
 84982       ** we already have page 1 loaded into cache and marked dirty. */
       
 84983       sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
       
 84984       rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
       
 84985       if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
       
 84986     }
       
 84987 
       
 84988     rc = sqlite3BtreeCopyFile(pMain, pTemp);
       
 84989     if( rc!=SQLITE_OK ) goto end_of_vacuum;
       
 84990     rc = sqlite3BtreeCommit(pTemp);
       
 84991     if( rc!=SQLITE_OK ) goto end_of_vacuum;
       
 84992 #ifndef SQLITE_OMIT_AUTOVACUUM
       
 84993     sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
       
 84994 #endif
       
 84995   }
       
 84996 
       
 84997   assert( rc==SQLITE_OK );
       
 84998   rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
       
 84999 
       
 85000 end_of_vacuum:
       
 85001   /* Restore the original value of db->flags */
       
 85002   db->flags = saved_flags;
       
 85003   db->nChange = saved_nChange;
       
 85004   db->nTotalChange = saved_nTotalChange;
       
 85005 
       
 85006   /* Currently there is an SQL level transaction open on the vacuum
       
 85007   ** database. No locks are held on any other files (since the main file
       
 85008   ** was committed at the btree level). So it safe to end the transaction
       
 85009   ** by manually setting the autoCommit flag to true and detaching the
       
 85010   ** vacuum database. The vacuum_db journal file is deleted when the pager
       
 85011   ** is closed by the DETACH.
       
 85012   */
       
 85013   db->autoCommit = 1;
       
 85014 
       
 85015   if( pDb ){
       
 85016     sqlite3BtreeClose(pDb->pBt);
       
 85017     pDb->pBt = 0;
       
 85018     pDb->pSchema = 0;
       
 85019   }
       
 85020 
       
 85021   sqlite3ResetInternalSchema(db, 0);
       
 85022 
       
 85023   return rc;
       
 85024 }
       
 85025 #endif  /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
       
 85026 
       
 85027 /************** End of vacuum.c **********************************************/
       
 85028 /************** Begin file vtab.c ********************************************/
       
 85029 /*
       
 85030 ** 2006 June 10
       
 85031 **
       
 85032 ** The author disclaims copyright to this source code.  In place of
       
 85033 ** a legal notice, here is a blessing:
       
 85034 **
       
 85035 **    May you do good and not evil.
       
 85036 **    May you find forgiveness for yourself and forgive others.
       
 85037 **    May you share freely, never taking more than you give.
       
 85038 **
       
 85039 *************************************************************************
       
 85040 ** This file contains code used to help implement virtual tables.
       
 85041 **
       
 85042 ** $Id: vtab.c,v 1.94 2009/08/08 18:01:08 drh Exp $
       
 85043 */
       
 85044 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 85045 
       
 85046 /*
       
 85047 ** The actual function that does the work of creating a new module.
       
 85048 ** This function implements the sqlite3_create_module() and
       
 85049 ** sqlite3_create_module_v2() interfaces.
       
 85050 */
       
 85051 static int createModule(
       
 85052   sqlite3 *db,                    /* Database in which module is registered */
       
 85053   const char *zName,              /* Name assigned to this module */
       
 85054   const sqlite3_module *pModule,  /* The definition of the module */
       
 85055   void *pAux,                     /* Context pointer for xCreate/xConnect */
       
 85056   void (*xDestroy)(void *)        /* Module destructor function */
       
 85057 ){
       
 85058   int rc, nName;
       
 85059   Module *pMod;
       
 85060 
       
 85061   sqlite3_mutex_enter(db->mutex);
       
 85062   nName = sqlite3Strlen30(zName);
       
 85063   pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
       
 85064   if( pMod ){
       
 85065     Module *pDel;
       
 85066     char *zCopy = (char *)(&pMod[1]);
       
 85067     memcpy(zCopy, zName, nName+1);
       
 85068     pMod->zName = zCopy;
       
 85069     pMod->pModule = pModule;
       
 85070     pMod->pAux = pAux;
       
 85071     pMod->xDestroy = xDestroy;
       
 85072     pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
       
 85073     if( pDel && pDel->xDestroy ){
       
 85074       pDel->xDestroy(pDel->pAux);
       
 85075     }
       
 85076     sqlite3DbFree(db, pDel);
       
 85077     if( pDel==pMod ){
       
 85078       db->mallocFailed = 1;
       
 85079     }
       
 85080     sqlite3ResetInternalSchema(db, 0);
       
 85081   }else if( xDestroy ){
       
 85082     xDestroy(pAux);
       
 85083   }
       
 85084   rc = sqlite3ApiExit(db, SQLITE_OK);
       
 85085   sqlite3_mutex_leave(db->mutex);
       
 85086   return rc;
       
 85087 }
       
 85088 
       
 85089 
       
 85090 /*
       
 85091 ** External API function used to create a new virtual-table module.
       
 85092 */
       
 85093 SQLITE_API int sqlite3_create_module(
       
 85094   sqlite3 *db,                    /* Database in which module is registered */
       
 85095   const char *zName,              /* Name assigned to this module */
       
 85096   const sqlite3_module *pModule,  /* The definition of the module */
       
 85097   void *pAux                      /* Context pointer for xCreate/xConnect */
       
 85098 ){
       
 85099   return createModule(db, zName, pModule, pAux, 0);
       
 85100 }
       
 85101 
       
 85102 /*
       
 85103 ** External API function used to create a new virtual-table module.
       
 85104 */
       
 85105 SQLITE_API int sqlite3_create_module_v2(
       
 85106   sqlite3 *db,                    /* Database in which module is registered */
       
 85107   const char *zName,              /* Name assigned to this module */
       
 85108   const sqlite3_module *pModule,  /* The definition of the module */
       
 85109   void *pAux,                     /* Context pointer for xCreate/xConnect */
       
 85110   void (*xDestroy)(void *)        /* Module destructor function */
       
 85111 ){
       
 85112   return createModule(db, zName, pModule, pAux, xDestroy);
       
 85113 }
       
 85114 
       
 85115 /*
       
 85116 ** Lock the virtual table so that it cannot be disconnected.
       
 85117 ** Locks nest.  Every lock should have a corresponding unlock.
       
 85118 ** If an unlock is omitted, resources leaks will occur.  
       
 85119 **
       
 85120 ** If a disconnect is attempted while a virtual table is locked,
       
 85121 ** the disconnect is deferred until all locks have been removed.
       
 85122 */
       
 85123 SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
       
 85124   pVTab->nRef++;
       
 85125 }
       
 85126 
       
 85127 
       
 85128 /*
       
 85129 ** pTab is a pointer to a Table structure representing a virtual-table.
       
 85130 ** Return a pointer to the VTable object used by connection db to access 
       
 85131 ** this virtual-table, if one has been created, or NULL otherwise.
       
 85132 */
       
 85133 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
       
 85134   VTable *pVtab;
       
 85135   assert( IsVirtual(pTab) );
       
 85136   for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
       
 85137   return pVtab;
       
 85138 }
       
 85139 
       
 85140 /*
       
 85141 ** Decrement the ref-count on a virtual table object. When the ref-count
       
 85142 ** reaches zero, call the xDisconnect() method to delete the object.
       
 85143 */
       
 85144 SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
       
 85145   sqlite3 *db = pVTab->db;
       
 85146 
       
 85147   assert( db );
       
 85148   assert( pVTab->nRef>0 );
       
 85149   assert( sqlite3SafetyCheckOk(db) );
       
 85150 
       
 85151   pVTab->nRef--;
       
 85152   if( pVTab->nRef==0 ){
       
 85153     sqlite3_vtab *p = pVTab->pVtab;
       
 85154     if( p ){
       
 85155 #ifdef SQLITE_DEBUG
       
 85156       if( pVTab->db->magic==SQLITE_MAGIC_BUSY ){
       
 85157         (void)sqlite3SafetyOff(db);
       
 85158         p->pModule->xDisconnect(p);
       
 85159         (void)sqlite3SafetyOn(db);
       
 85160       } else
       
 85161 #endif
       
 85162       {
       
 85163         p->pModule->xDisconnect(p);
       
 85164       }
       
 85165     }
       
 85166     sqlite3DbFree(db, pVTab);
       
 85167   }
       
 85168 }
       
 85169 
       
 85170 /*
       
 85171 ** Table p is a virtual table. This function moves all elements in the
       
 85172 ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
       
 85173 ** database connections to be disconnected at the next opportunity. 
       
 85174 ** Except, if argument db is not NULL, then the entry associated with
       
 85175 ** connection db is left in the p->pVTable list.
       
 85176 */
       
 85177 static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
       
 85178   VTable *pRet = 0;
       
 85179   VTable *pVTable = p->pVTable;
       
 85180   p->pVTable = 0;
       
 85181 
       
 85182   /* Assert that the mutex (if any) associated with the BtShared database 
       
 85183   ** that contains table p is held by the caller. See header comments 
       
 85184   ** above function sqlite3VtabUnlockList() for an explanation of why
       
 85185   ** this makes it safe to access the sqlite3.pDisconnect list of any
       
 85186   ** database connection that may have an entry in the p->pVTable list.  */
       
 85187   assert( db==0 ||
       
 85188     sqlite3BtreeHoldsMutex(db->aDb[sqlite3SchemaToIndex(db, p->pSchema)].pBt) 
       
 85189   );
       
 85190 
       
 85191   while( pVTable ){
       
 85192     sqlite3 *db2 = pVTable->db;
       
 85193     VTable *pNext = pVTable->pNext;
       
 85194     assert( db2 );
       
 85195     if( db2==db ){
       
 85196       pRet = pVTable;
       
 85197       p->pVTable = pRet;
       
 85198       pRet->pNext = 0;
       
 85199     }else{
       
 85200       pVTable->pNext = db2->pDisconnect;
       
 85201       db2->pDisconnect = pVTable;
       
 85202     }
       
 85203     pVTable = pNext;
       
 85204   }
       
 85205 
       
 85206   assert( !db || pRet );
       
 85207   return pRet;
       
 85208 }
       
 85209 
       
 85210 
       
 85211 /*
       
 85212 ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
       
 85213 **
       
 85214 ** This function may only be called when the mutexes associated with all
       
 85215 ** shared b-tree databases opened using connection db are held by the 
       
 85216 ** caller. This is done to protect the sqlite3.pDisconnect list. The
       
 85217 ** sqlite3.pDisconnect list is accessed only as follows:
       
 85218 **
       
 85219 **   1) By this function. In this case, all BtShared mutexes and the mutex
       
 85220 **      associated with the database handle itself must be held.
       
 85221 **
       
 85222 **   2) By function vtabDisconnectAll(), when it adds a VTable entry to
       
 85223 **      the sqlite3.pDisconnect list. In this case either the BtShared mutex
       
 85224 **      associated with the database the virtual table is stored in is held
       
 85225 **      or, if the virtual table is stored in a non-sharable database, then
       
 85226 **      the database handle mutex is held.
       
 85227 **
       
 85228 ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously 
       
 85229 ** by multiple threads. It is thread-safe.
       
 85230 */
       
 85231 SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
       
 85232   VTable *p = db->pDisconnect;
       
 85233   db->pDisconnect = 0;
       
 85234 
       
 85235   assert( sqlite3BtreeHoldsAllMutexes(db) );
       
 85236   assert( sqlite3_mutex_held(db->mutex) );
       
 85237 
       
 85238   if( p ){
       
 85239     sqlite3ExpirePreparedStatements(db);
       
 85240     do {
       
 85241       VTable *pNext = p->pNext;
       
 85242       sqlite3VtabUnlock(p);
       
 85243       p = pNext;
       
 85244     }while( p );
       
 85245   }
       
 85246 }
       
 85247 
       
 85248 /*
       
 85249 ** Clear any and all virtual-table information from the Table record.
       
 85250 ** This routine is called, for example, just before deleting the Table
       
 85251 ** record.
       
 85252 **
       
 85253 ** Since it is a virtual-table, the Table structure contains a pointer
       
 85254 ** to the head of a linked list of VTable structures. Each VTable 
       
 85255 ** structure is associated with a single sqlite3* user of the schema.
       
 85256 ** The reference count of the VTable structure associated with database 
       
 85257 ** connection db is decremented immediately (which may lead to the 
       
 85258 ** structure being xDisconnected and free). Any other VTable structures
       
 85259 ** in the list are moved to the sqlite3.pDisconnect list of the associated 
       
 85260 ** database connection.
       
 85261 */
       
 85262 SQLITE_PRIVATE void sqlite3VtabClear(Table *p){
       
 85263   vtabDisconnectAll(0, p);
       
 85264   if( p->azModuleArg ){
       
 85265     int i;
       
 85266     for(i=0; i<p->nModuleArg; i++){
       
 85267       sqlite3DbFree(p->dbMem, p->azModuleArg[i]);
       
 85268     }
       
 85269     sqlite3DbFree(p->dbMem, p->azModuleArg);
       
 85270   }
       
 85271 }
       
 85272 
       
 85273 /*
       
 85274 ** Add a new module argument to pTable->azModuleArg[].
       
 85275 ** The string is not copied - the pointer is stored.  The
       
 85276 ** string will be freed automatically when the table is
       
 85277 ** deleted.
       
 85278 */
       
 85279 static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
       
 85280   int i = pTable->nModuleArg++;
       
 85281   int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
       
 85282   char **azModuleArg;
       
 85283   azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
       
 85284   if( azModuleArg==0 ){
       
 85285     int j;
       
 85286     for(j=0; j<i; j++){
       
 85287       sqlite3DbFree(db, pTable->azModuleArg[j]);
       
 85288     }
       
 85289     sqlite3DbFree(db, zArg);
       
 85290     sqlite3DbFree(db, pTable->azModuleArg);
       
 85291     pTable->nModuleArg = 0;
       
 85292   }else{
       
 85293     azModuleArg[i] = zArg;
       
 85294     azModuleArg[i+1] = 0;
       
 85295   }
       
 85296   pTable->azModuleArg = azModuleArg;
       
 85297 }
       
 85298 
       
 85299 /*
       
 85300 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
       
 85301 ** statement.  The module name has been parsed, but the optional list
       
 85302 ** of parameters that follow the module name are still pending.
       
 85303 */
       
 85304 SQLITE_PRIVATE void sqlite3VtabBeginParse(
       
 85305   Parse *pParse,        /* Parsing context */
       
 85306   Token *pName1,        /* Name of new table, or database name */
       
 85307   Token *pName2,        /* Name of new table or NULL */
       
 85308   Token *pModuleName    /* Name of the module for the virtual table */
       
 85309 ){
       
 85310   int iDb;              /* The database the table is being created in */
       
 85311   Table *pTable;        /* The new virtual table */
       
 85312   sqlite3 *db;          /* Database connection */
       
 85313 
       
 85314   sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
       
 85315   pTable = pParse->pNewTable;
       
 85316   if( pTable==0 ) return;
       
 85317   assert( 0==pTable->pIndex );
       
 85318 
       
 85319   db = pParse->db;
       
 85320   iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
       
 85321   assert( iDb>=0 );
       
 85322 
       
 85323   pTable->tabFlags |= TF_Virtual;
       
 85324   pTable->nModuleArg = 0;
       
 85325   addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
       
 85326   addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
       
 85327   addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
       
 85328   pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
       
 85329 
       
 85330 #ifndef SQLITE_OMIT_AUTHORIZATION
       
 85331   /* Creating a virtual table invokes the authorization callback twice.
       
 85332   ** The first invocation, to obtain permission to INSERT a row into the
       
 85333   ** sqlite_master table, has already been made by sqlite3StartTable().
       
 85334   ** The second call, to obtain permission to create the table, is made now.
       
 85335   */
       
 85336   if( pTable->azModuleArg ){
       
 85337     sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 
       
 85338             pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
       
 85339   }
       
 85340 #endif
       
 85341 }
       
 85342 
       
 85343 /*
       
 85344 ** This routine takes the module argument that has been accumulating
       
 85345 ** in pParse->zArg[] and appends it to the list of arguments on the
       
 85346 ** virtual table currently under construction in pParse->pTable.
       
 85347 */
       
 85348 static void addArgumentToVtab(Parse *pParse){
       
 85349   if( pParse->sArg.z && ALWAYS(pParse->pNewTable) ){
       
 85350     const char *z = (const char*)pParse->sArg.z;
       
 85351     int n = pParse->sArg.n;
       
 85352     sqlite3 *db = pParse->db;
       
 85353     addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
       
 85354   }
       
 85355 }
       
 85356 
       
 85357 /*
       
 85358 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
       
 85359 ** has been completely parsed.
       
 85360 */
       
 85361 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
       
 85362   Table *pTab = pParse->pNewTable;  /* The table being constructed */
       
 85363   sqlite3 *db = pParse->db;         /* The database connection */
       
 85364 
       
 85365   if( pTab==0 ) return;
       
 85366   addArgumentToVtab(pParse);
       
 85367   pParse->sArg.z = 0;
       
 85368   if( pTab->nModuleArg<1 ) return;
       
 85369   
       
 85370   /* If the CREATE VIRTUAL TABLE statement is being entered for the
       
 85371   ** first time (in other words if the virtual table is actually being
       
 85372   ** created now instead of just being read out of sqlite_master) then
       
 85373   ** do additional initialization work and store the statement text
       
 85374   ** in the sqlite_master table.
       
 85375   */
       
 85376   if( !db->init.busy ){
       
 85377     char *zStmt;
       
 85378     char *zWhere;
       
 85379     int iDb;
       
 85380     Vdbe *v;
       
 85381 
       
 85382     /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
       
 85383     if( pEnd ){
       
 85384       pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
       
 85385     }
       
 85386     zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
       
 85387 
       
 85388     /* A slot for the record has already been allocated in the 
       
 85389     ** SQLITE_MASTER table.  We just need to update that slot with all
       
 85390     ** the information we've collected.  
       
 85391     **
       
 85392     ** The VM register number pParse->regRowid holds the rowid of an
       
 85393     ** entry in the sqlite_master table tht was created for this vtab
       
 85394     ** by sqlite3StartTable().
       
 85395     */
       
 85396     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       
 85397     sqlite3NestedParse(pParse,
       
 85398       "UPDATE %Q.%s "
       
 85399          "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
       
 85400        "WHERE rowid=#%d",
       
 85401       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
       
 85402       pTab->zName,
       
 85403       pTab->zName,
       
 85404       zStmt,
       
 85405       pParse->regRowid
       
 85406     );
       
 85407     sqlite3DbFree(db, zStmt);
       
 85408     v = sqlite3GetVdbe(pParse);
       
 85409     sqlite3ChangeCookie(pParse, iDb);
       
 85410 
       
 85411     sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
       
 85412     zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName);
       
 85413     sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 1, 0, zWhere, P4_DYNAMIC);
       
 85414     sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, 
       
 85415                          pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
       
 85416   }
       
 85417 
       
 85418   /* If we are rereading the sqlite_master table create the in-memory
       
 85419   ** record of the table. The xConnect() method is not called until
       
 85420   ** the first time the virtual table is used in an SQL statement. This
       
 85421   ** allows a schema that contains virtual tables to be loaded before
       
 85422   ** the required virtual table implementations are registered.  */
       
 85423   else {
       
 85424     Table *pOld;
       
 85425     Schema *pSchema = pTab->pSchema;
       
 85426     const char *zName = pTab->zName;
       
 85427     int nName = sqlite3Strlen30(zName);
       
 85428     pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
       
 85429     if( pOld ){
       
 85430       db->mallocFailed = 1;
       
 85431       assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
       
 85432       return;
       
 85433     }
       
 85434     pSchema->db = pParse->db;
       
 85435     pParse->pNewTable = 0;
       
 85436   }
       
 85437 }
       
 85438 
       
 85439 /*
       
 85440 ** The parser calls this routine when it sees the first token
       
 85441 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
       
 85442 */
       
 85443 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
       
 85444   addArgumentToVtab(pParse);
       
 85445   pParse->sArg.z = 0;
       
 85446   pParse->sArg.n = 0;
       
 85447 }
       
 85448 
       
 85449 /*
       
 85450 ** The parser calls this routine for each token after the first token
       
 85451 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
       
 85452 */
       
 85453 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
       
 85454   Token *pArg = &pParse->sArg;
       
 85455   if( pArg->z==0 ){
       
 85456     pArg->z = p->z;
       
 85457     pArg->n = p->n;
       
 85458   }else{
       
 85459     assert(pArg->z < p->z);
       
 85460     pArg->n = (int)(&p->z[p->n] - pArg->z);
       
 85461   }
       
 85462 }
       
 85463 
       
 85464 /*
       
 85465 ** Invoke a virtual table constructor (either xCreate or xConnect). The
       
 85466 ** pointer to the function to invoke is passed as the fourth parameter
       
 85467 ** to this procedure.
       
 85468 */
       
 85469 static int vtabCallConstructor(
       
 85470   sqlite3 *db, 
       
 85471   Table *pTab,
       
 85472   Module *pMod,
       
 85473   int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
       
 85474   char **pzErr
       
 85475 ){
       
 85476   VTable *pVTable;
       
 85477   int rc;
       
 85478   const char *const*azArg = (const char *const*)pTab->azModuleArg;
       
 85479   int nArg = pTab->nModuleArg;
       
 85480   char *zErr = 0;
       
 85481   char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
       
 85482 
       
 85483   if( !zModuleName ){
       
 85484     return SQLITE_NOMEM;
       
 85485   }
       
 85486 
       
 85487   pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
       
 85488   if( !pVTable ){
       
 85489     sqlite3DbFree(db, zModuleName);
       
 85490     return SQLITE_NOMEM;
       
 85491   }
       
 85492   pVTable->db = db;
       
 85493   pVTable->pMod = pMod;
       
 85494 
       
 85495   assert( !db->pVTab );
       
 85496   assert( xConstruct );
       
 85497   db->pVTab = pTab;
       
 85498 
       
 85499   /* Invoke the virtual table constructor */
       
 85500   (void)sqlite3SafetyOff(db);
       
 85501   rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
       
 85502   (void)sqlite3SafetyOn(db);
       
 85503   if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
       
 85504 
       
 85505   if( SQLITE_OK!=rc ){
       
 85506     if( zErr==0 ){
       
 85507       *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
       
 85508     }else {
       
 85509       *pzErr = sqlite3MPrintf(db, "%s", zErr);
       
 85510       sqlite3DbFree(db, zErr);
       
 85511     }
       
 85512     sqlite3DbFree(db, pVTable);
       
 85513   }else if( ALWAYS(pVTable->pVtab) ){
       
 85514     /* Justification of ALWAYS():  A correct vtab constructor must allocate
       
 85515     ** the sqlite3_vtab object if successful.  */
       
 85516     pVTable->pVtab->pModule = pMod->pModule;
       
 85517     pVTable->nRef = 1;
       
 85518     if( db->pVTab ){
       
 85519       const char *zFormat = "vtable constructor did not declare schema: %s";
       
 85520       *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
       
 85521       sqlite3VtabUnlock(pVTable);
       
 85522       rc = SQLITE_ERROR;
       
 85523     }else{
       
 85524       int iCol;
       
 85525       /* If everything went according to plan, link the new VTable structure
       
 85526       ** into the linked list headed by pTab->pVTable. Then loop through the 
       
 85527       ** columns of the table to see if any of them contain the token "hidden".
       
 85528       ** If so, set the Column.isHidden flag and remove the token from
       
 85529       ** the type string.  */
       
 85530       pVTable->pNext = pTab->pVTable;
       
 85531       pTab->pVTable = pVTable;
       
 85532 
       
 85533       for(iCol=0; iCol<pTab->nCol; iCol++){
       
 85534         char *zType = pTab->aCol[iCol].zType;
       
 85535         int nType;
       
 85536         int i = 0;
       
 85537         if( !zType ) continue;
       
 85538         nType = sqlite3Strlen30(zType);
       
 85539         if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
       
 85540           for(i=0; i<nType; i++){
       
 85541             if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
       
 85542              && (zType[i+7]=='\0' || zType[i+7]==' ')
       
 85543             ){
       
 85544               i++;
       
 85545               break;
       
 85546             }
       
 85547           }
       
 85548         }
       
 85549         if( i<nType ){
       
 85550           int j;
       
 85551           int nDel = 6 + (zType[i+6] ? 1 : 0);
       
 85552           for(j=i; (j+nDel)<=nType; j++){
       
 85553             zType[j] = zType[j+nDel];
       
 85554           }
       
 85555           if( zType[i]=='\0' && i>0 ){
       
 85556             assert(zType[i-1]==' ');
       
 85557             zType[i-1] = '\0';
       
 85558           }
       
 85559           pTab->aCol[iCol].isHidden = 1;
       
 85560         }
       
 85561       }
       
 85562     }
       
 85563   }
       
 85564 
       
 85565   sqlite3DbFree(db, zModuleName);
       
 85566   db->pVTab = 0;
       
 85567   return rc;
       
 85568 }
       
 85569 
       
 85570 /*
       
 85571 ** This function is invoked by the parser to call the xConnect() method
       
 85572 ** of the virtual table pTab. If an error occurs, an error code is returned 
       
 85573 ** and an error left in pParse.
       
 85574 **
       
 85575 ** This call is a no-op if table pTab is not a virtual table.
       
 85576 */
       
 85577 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
       
 85578   sqlite3 *db = pParse->db;
       
 85579   const char *zMod;
       
 85580   Module *pMod;
       
 85581   int rc;
       
 85582 
       
 85583   assert( pTab );
       
 85584   if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
       
 85585     return SQLITE_OK;
       
 85586   }
       
 85587 
       
 85588   /* Locate the required virtual table module */
       
 85589   zMod = pTab->azModuleArg[0];
       
 85590   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
       
 85591 
       
 85592   if( !pMod ){
       
 85593     const char *zModule = pTab->azModuleArg[0];
       
 85594     sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
       
 85595     rc = SQLITE_ERROR;
       
 85596   }else{
       
 85597     char *zErr = 0;
       
 85598     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
       
 85599     if( rc!=SQLITE_OK ){
       
 85600       sqlite3ErrorMsg(pParse, "%s", zErr);
       
 85601     }
       
 85602     sqlite3DbFree(db, zErr);
       
 85603   }
       
 85604 
       
 85605   return rc;
       
 85606 }
       
 85607 
       
 85608 /*
       
 85609 ** Add the virtual table pVTab to the array sqlite3.aVTrans[].
       
 85610 */
       
 85611 static int addToVTrans(sqlite3 *db, VTable *pVTab){
       
 85612   const int ARRAY_INCR = 5;
       
 85613 
       
 85614   /* Grow the sqlite3.aVTrans array if required */
       
 85615   if( (db->nVTrans%ARRAY_INCR)==0 ){
       
 85616     VTable **aVTrans;
       
 85617     int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
       
 85618     aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
       
 85619     if( !aVTrans ){
       
 85620       return SQLITE_NOMEM;
       
 85621     }
       
 85622     memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
       
 85623     db->aVTrans = aVTrans;
       
 85624   }
       
 85625 
       
 85626   /* Add pVtab to the end of sqlite3.aVTrans */
       
 85627   db->aVTrans[db->nVTrans++] = pVTab;
       
 85628   sqlite3VtabLock(pVTab);
       
 85629   return SQLITE_OK;
       
 85630 }
       
 85631 
       
 85632 /*
       
 85633 ** This function is invoked by the vdbe to call the xCreate method
       
 85634 ** of the virtual table named zTab in database iDb. 
       
 85635 **
       
 85636 ** If an error occurs, *pzErr is set to point an an English language
       
 85637 ** description of the error and an SQLITE_XXX error code is returned.
       
 85638 ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
       
 85639 */
       
 85640 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
       
 85641   int rc = SQLITE_OK;
       
 85642   Table *pTab;
       
 85643   Module *pMod;
       
 85644   const char *zMod;
       
 85645 
       
 85646   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
       
 85647   assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
       
 85648 
       
 85649   /* Locate the required virtual table module */
       
 85650   zMod = pTab->azModuleArg[0];
       
 85651   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
       
 85652 
       
 85653   /* If the module has been registered and includes a Create method, 
       
 85654   ** invoke it now. If the module has not been registered, return an 
       
 85655   ** error. Otherwise, do nothing.
       
 85656   */
       
 85657   if( !pMod ){
       
 85658     *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
       
 85659     rc = SQLITE_ERROR;
       
 85660   }else{
       
 85661     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
       
 85662   }
       
 85663 
       
 85664   /* Justification of ALWAYS():  The xConstructor method is required to
       
 85665   ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
       
 85666   if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
       
 85667       rc = addToVTrans(db, sqlite3GetVTable(db, pTab));
       
 85668   }
       
 85669 
       
 85670   return rc;
       
 85671 }
       
 85672 
       
 85673 /*
       
 85674 ** This function is used to set the schema of a virtual table.  It is only
       
 85675 ** valid to call this function from within the xCreate() or xConnect() of a
       
 85676 ** virtual table module.
       
 85677 */
       
 85678 SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
       
 85679   Parse *pParse;
       
 85680 
       
 85681   int rc = SQLITE_OK;
       
 85682   Table *pTab;
       
 85683   char *zErr = 0;
       
 85684 
       
 85685   sqlite3_mutex_enter(db->mutex);
       
 85686   pTab = db->pVTab;
       
 85687   if( !pTab ){
       
 85688     sqlite3Error(db, SQLITE_MISUSE, 0);
       
 85689     sqlite3_mutex_leave(db->mutex);
       
 85690     return SQLITE_MISUSE;
       
 85691   }
       
 85692   assert( (pTab->tabFlags & TF_Virtual)!=0 );
       
 85693 
       
 85694   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
       
 85695   if( pParse==0 ){
       
 85696     rc = SQLITE_NOMEM;
       
 85697   }else{
       
 85698     pParse->declareVtab = 1;
       
 85699     pParse->db = db;
       
 85700   
       
 85701     if( 
       
 85702         SQLITE_OK == sqlite3RunParser(pParse, zCreateTable, &zErr) && 
       
 85703         pParse->pNewTable && 
       
 85704         !pParse->pNewTable->pSelect && 
       
 85705         (pParse->pNewTable->tabFlags & TF_Virtual)==0
       
 85706     ){
       
 85707       if( !pTab->aCol ){
       
 85708         pTab->aCol = pParse->pNewTable->aCol;
       
 85709         pTab->nCol = pParse->pNewTable->nCol;
       
 85710         pParse->pNewTable->nCol = 0;
       
 85711         pParse->pNewTable->aCol = 0;
       
 85712       }
       
 85713       db->pVTab = 0;
       
 85714     } else {
       
 85715       sqlite3Error(db, SQLITE_ERROR, zErr);
       
 85716       sqlite3DbFree(db, zErr);
       
 85717       rc = SQLITE_ERROR;
       
 85718     }
       
 85719     pParse->declareVtab = 0;
       
 85720   
       
 85721     if( pParse->pVdbe ){
       
 85722       sqlite3VdbeFinalize(pParse->pVdbe);
       
 85723     }
       
 85724     sqlite3DeleteTable(pParse->pNewTable);
       
 85725     sqlite3StackFree(db, pParse);
       
 85726   }
       
 85727 
       
 85728   assert( (rc&0xff)==rc );
       
 85729   rc = sqlite3ApiExit(db, rc);
       
 85730   sqlite3_mutex_leave(db->mutex);
       
 85731   return rc;
       
 85732 }
       
 85733 
       
 85734 /*
       
 85735 ** This function is invoked by the vdbe to call the xDestroy method
       
 85736 ** of the virtual table named zTab in database iDb. This occurs
       
 85737 ** when a DROP TABLE is mentioned.
       
 85738 **
       
 85739 ** This call is a no-op if zTab is not a virtual table.
       
 85740 */
       
 85741 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
       
 85742   int rc = SQLITE_OK;
       
 85743   Table *pTab;
       
 85744 
       
 85745   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
       
 85746   if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
       
 85747     VTable *p = vtabDisconnectAll(db, pTab);
       
 85748 
       
 85749     rc = sqlite3SafetyOff(db);
       
 85750     assert( rc==SQLITE_OK );
       
 85751     rc = p->pMod->pModule->xDestroy(p->pVtab);
       
 85752     (void)sqlite3SafetyOn(db);
       
 85753 
       
 85754     /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
       
 85755     if( rc==SQLITE_OK ){
       
 85756       assert( pTab->pVTable==p && p->pNext==0 );
       
 85757       p->pVtab = 0;
       
 85758       pTab->pVTable = 0;
       
 85759       sqlite3VtabUnlock(p);
       
 85760     }
       
 85761   }
       
 85762 
       
 85763   return rc;
       
 85764 }
       
 85765 
       
 85766 /*
       
 85767 ** This function invokes either the xRollback or xCommit method
       
 85768 ** of each of the virtual tables in the sqlite3.aVTrans array. The method
       
 85769 ** called is identified by the second argument, "offset", which is
       
 85770 ** the offset of the method to call in the sqlite3_module structure.
       
 85771 **
       
 85772 ** The array is cleared after invoking the callbacks. 
       
 85773 */
       
 85774 static void callFinaliser(sqlite3 *db, int offset){
       
 85775   int i;
       
 85776   if( db->aVTrans ){
       
 85777     for(i=0; i<db->nVTrans; i++){
       
 85778       VTable *pVTab = db->aVTrans[i];
       
 85779       sqlite3_vtab *p = pVTab->pVtab;
       
 85780       if( p ){
       
 85781         int (*x)(sqlite3_vtab *);
       
 85782         x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
       
 85783         if( x ) x(p);
       
 85784       }
       
 85785       sqlite3VtabUnlock(pVTab);
       
 85786     }
       
 85787     sqlite3DbFree(db, db->aVTrans);
       
 85788     db->nVTrans = 0;
       
 85789     db->aVTrans = 0;
       
 85790   }
       
 85791 }
       
 85792 
       
 85793 /*
       
 85794 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
       
 85795 ** array. Return the error code for the first error that occurs, or
       
 85796 ** SQLITE_OK if all xSync operations are successful.
       
 85797 **
       
 85798 ** Set *pzErrmsg to point to a buffer that should be released using 
       
 85799 ** sqlite3DbFree() containing an error message, if one is available.
       
 85800 */
       
 85801 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
       
 85802   int i;
       
 85803   int rc = SQLITE_OK;
       
 85804   int rcsafety;
       
 85805   VTable **aVTrans = db->aVTrans;
       
 85806 
       
 85807   rc = sqlite3SafetyOff(db);
       
 85808   db->aVTrans = 0;
       
 85809   for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
       
 85810     int (*x)(sqlite3_vtab *);
       
 85811     sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
       
 85812     if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
       
 85813       rc = x(pVtab);
       
 85814       sqlite3DbFree(db, *pzErrmsg);
       
 85815       *pzErrmsg = pVtab->zErrMsg;
       
 85816       pVtab->zErrMsg = 0;
       
 85817     }
       
 85818   }
       
 85819   db->aVTrans = aVTrans;
       
 85820   rcsafety = sqlite3SafetyOn(db);
       
 85821 
       
 85822   if( rc==SQLITE_OK ){
       
 85823     rc = rcsafety;
       
 85824   }
       
 85825   return rc;
       
 85826 }
       
 85827 
       
 85828 /*
       
 85829 ** Invoke the xRollback method of all virtual tables in the 
       
 85830 ** sqlite3.aVTrans array. Then clear the array itself.
       
 85831 */
       
 85832 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
       
 85833   callFinaliser(db, offsetof(sqlite3_module,xRollback));
       
 85834   return SQLITE_OK;
       
 85835 }
       
 85836 
       
 85837 /*
       
 85838 ** Invoke the xCommit method of all virtual tables in the 
       
 85839 ** sqlite3.aVTrans array. Then clear the array itself.
       
 85840 */
       
 85841 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
       
 85842   callFinaliser(db, offsetof(sqlite3_module,xCommit));
       
 85843   return SQLITE_OK;
       
 85844 }
       
 85845 
       
 85846 /*
       
 85847 ** If the virtual table pVtab supports the transaction interface
       
 85848 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
       
 85849 ** not currently open, invoke the xBegin method now.
       
 85850 **
       
 85851 ** If the xBegin call is successful, place the sqlite3_vtab pointer
       
 85852 ** in the sqlite3.aVTrans array.
       
 85853 */
       
 85854 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
       
 85855   int rc = SQLITE_OK;
       
 85856   const sqlite3_module *pModule;
       
 85857 
       
 85858   /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
       
 85859   ** than zero, then this function is being called from within a
       
 85860   ** virtual module xSync() callback. It is illegal to write to 
       
 85861   ** virtual module tables in this case, so return SQLITE_LOCKED.
       
 85862   */
       
 85863   if( sqlite3VtabInSync(db) ){
       
 85864     return SQLITE_LOCKED;
       
 85865   }
       
 85866   if( !pVTab ){
       
 85867     return SQLITE_OK;
       
 85868   } 
       
 85869   pModule = pVTab->pVtab->pModule;
       
 85870 
       
 85871   if( pModule->xBegin ){
       
 85872     int i;
       
 85873 
       
 85874 
       
 85875     /* If pVtab is already in the aVTrans array, return early */
       
 85876     for(i=0; i<db->nVTrans; i++){
       
 85877       if( db->aVTrans[i]==pVTab ){
       
 85878         return SQLITE_OK;
       
 85879       }
       
 85880     }
       
 85881 
       
 85882     /* Invoke the xBegin method */
       
 85883     rc = pModule->xBegin(pVTab->pVtab);
       
 85884     if( rc==SQLITE_OK ){
       
 85885       rc = addToVTrans(db, pVTab);
       
 85886     }
       
 85887   }
       
 85888   return rc;
       
 85889 }
       
 85890 
       
 85891 /*
       
 85892 ** The first parameter (pDef) is a function implementation.  The
       
 85893 ** second parameter (pExpr) is the first argument to this function.
       
 85894 ** If pExpr is a column in a virtual table, then let the virtual
       
 85895 ** table implementation have an opportunity to overload the function.
       
 85896 **
       
 85897 ** This routine is used to allow virtual table implementations to
       
 85898 ** overload MATCH, LIKE, GLOB, and REGEXP operators.
       
 85899 **
       
 85900 ** Return either the pDef argument (indicating no change) or a 
       
 85901 ** new FuncDef structure that is marked as ephemeral using the
       
 85902 ** SQLITE_FUNC_EPHEM flag.
       
 85903 */
       
 85904 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
       
 85905   sqlite3 *db,    /* Database connection for reporting malloc problems */
       
 85906   FuncDef *pDef,  /* Function to possibly overload */
       
 85907   int nArg,       /* Number of arguments to the function */
       
 85908   Expr *pExpr     /* First argument to the function */
       
 85909 ){
       
 85910   Table *pTab;
       
 85911   sqlite3_vtab *pVtab;
       
 85912   sqlite3_module *pMod;
       
 85913   void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
       
 85914   void *pArg = 0;
       
 85915   FuncDef *pNew;
       
 85916   int rc = 0;
       
 85917   char *zLowerName;
       
 85918   unsigned char *z;
       
 85919 
       
 85920 
       
 85921   /* Check to see the left operand is a column in a virtual table */
       
 85922   if( NEVER(pExpr==0) ) return pDef;
       
 85923   if( pExpr->op!=TK_COLUMN ) return pDef;
       
 85924   pTab = pExpr->pTab;
       
 85925   if( NEVER(pTab==0) ) return pDef;
       
 85926   if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
       
 85927   pVtab = sqlite3GetVTable(db, pTab)->pVtab;
       
 85928   assert( pVtab!=0 );
       
 85929   assert( pVtab->pModule!=0 );
       
 85930   pMod = (sqlite3_module *)pVtab->pModule;
       
 85931   if( pMod->xFindFunction==0 ) return pDef;
       
 85932  
       
 85933   /* Call the xFindFunction method on the virtual table implementation
       
 85934   ** to see if the implementation wants to overload this function 
       
 85935   */
       
 85936   zLowerName = sqlite3DbStrDup(db, pDef->zName);
       
 85937   if( zLowerName ){
       
 85938     for(z=(unsigned char*)zLowerName; *z; z++){
       
 85939       *z = sqlite3UpperToLower[*z];
       
 85940     }
       
 85941     rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
       
 85942     sqlite3DbFree(db, zLowerName);
       
 85943   }
       
 85944   if( rc==0 ){
       
 85945     return pDef;
       
 85946   }
       
 85947 
       
 85948   /* Create a new ephemeral function definition for the overloaded
       
 85949   ** function */
       
 85950   pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
       
 85951                              + sqlite3Strlen30(pDef->zName) + 1);
       
 85952   if( pNew==0 ){
       
 85953     return pDef;
       
 85954   }
       
 85955   *pNew = *pDef;
       
 85956   pNew->zName = (char *)&pNew[1];
       
 85957   memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
       
 85958   pNew->xFunc = xFunc;
       
 85959   pNew->pUserData = pArg;
       
 85960   pNew->flags |= SQLITE_FUNC_EPHEM;
       
 85961   return pNew;
       
 85962 }
       
 85963 
       
 85964 /*
       
 85965 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
       
 85966 ** array so that an OP_VBegin will get generated for it.  Add pTab to the
       
 85967 ** array if it is missing.  If pTab is already in the array, this routine
       
 85968 ** is a no-op.
       
 85969 */
       
 85970 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
       
 85971   Parse *pToplevel = sqlite3ParseToplevel(pParse);
       
 85972   int i, n;
       
 85973   Table **apVtabLock;
       
 85974 
       
 85975   assert( IsVirtual(pTab) );
       
 85976   for(i=0; i<pToplevel->nVtabLock; i++){
       
 85977     if( pTab==pToplevel->apVtabLock[i] ) return;
       
 85978   }
       
 85979   n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
       
 85980   apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
       
 85981   if( apVtabLock ){
       
 85982     pToplevel->apVtabLock = apVtabLock;
       
 85983     pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
       
 85984   }else{
       
 85985     pToplevel->db->mallocFailed = 1;
       
 85986   }
       
 85987 }
       
 85988 
       
 85989 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 85990 
       
 85991 /************** End of vtab.c ************************************************/
       
 85992 /************** Begin file where.c *******************************************/
       
 85993 /*
       
 85994 ** 2001 September 15
       
 85995 **
       
 85996 ** The author disclaims copyright to this source code.  In place of
       
 85997 ** a legal notice, here is a blessing:
       
 85998 **
       
 85999 **    May you do good and not evil.
       
 86000 **    May you find forgiveness for yourself and forgive others.
       
 86001 **    May you share freely, never taking more than you give.
       
 86002 **
       
 86003 *************************************************************************
       
 86004 ** This module contains C code that generates VDBE code used to process
       
 86005 ** the WHERE clause of SQL statements.  This module is responsible for
       
 86006 ** generating the code that loops through a table looking for applicable
       
 86007 ** rows.  Indices are selected and used to speed the search when doing
       
 86008 ** so is applicable.  Because this module is responsible for selecting
       
 86009 ** indices, you might also think of this module as the "query optimizer".
       
 86010 **
       
 86011 ** $Id: where.c,v 1.411 2009/07/31 06:14:52 danielk1977 Exp $
       
 86012 */
       
 86013 
       
 86014 /*
       
 86015 ** Trace output macros
       
 86016 */
       
 86017 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
       
 86018 SQLITE_PRIVATE int sqlite3WhereTrace = 0;
       
 86019 #endif
       
 86020 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
       
 86021 # define WHERETRACE(X)  if(sqlite3WhereTrace) sqlite3DebugPrintf X
       
 86022 #else
       
 86023 # define WHERETRACE(X)
       
 86024 #endif
       
 86025 
       
 86026 /* Forward reference
       
 86027 */
       
 86028 typedef struct WhereClause WhereClause;
       
 86029 typedef struct WhereMaskSet WhereMaskSet;
       
 86030 typedef struct WhereOrInfo WhereOrInfo;
       
 86031 typedef struct WhereAndInfo WhereAndInfo;
       
 86032 typedef struct WhereCost WhereCost;
       
 86033 
       
 86034 /*
       
 86035 ** The query generator uses an array of instances of this structure to
       
 86036 ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
       
 86037 ** clause subexpression is separated from the others by AND operators,
       
 86038 ** usually, or sometimes subexpressions separated by OR.
       
 86039 **
       
 86040 ** All WhereTerms are collected into a single WhereClause structure.  
       
 86041 ** The following identity holds:
       
 86042 **
       
 86043 **        WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
       
 86044 **
       
 86045 ** When a term is of the form:
       
 86046 **
       
 86047 **              X <op> <expr>
       
 86048 **
       
 86049 ** where X is a column name and <op> is one of certain operators,
       
 86050 ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
       
 86051 ** cursor number and column number for X.  WhereTerm.eOperator records
       
 86052 ** the <op> using a bitmask encoding defined by WO_xxx below.  The
       
 86053 ** use of a bitmask encoding for the operator allows us to search
       
 86054 ** quickly for terms that match any of several different operators.
       
 86055 **
       
 86056 ** A WhereTerm might also be two or more subterms connected by OR:
       
 86057 **
       
 86058 **         (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
       
 86059 **
       
 86060 ** In this second case, wtFlag as the TERM_ORINFO set and eOperator==WO_OR
       
 86061 ** and the WhereTerm.u.pOrInfo field points to auxiliary information that
       
 86062 ** is collected about the
       
 86063 **
       
 86064 ** If a term in the WHERE clause does not match either of the two previous
       
 86065 ** categories, then eOperator==0.  The WhereTerm.pExpr field is still set
       
 86066 ** to the original subexpression content and wtFlags is set up appropriately
       
 86067 ** but no other fields in the WhereTerm object are meaningful.
       
 86068 **
       
 86069 ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
       
 86070 ** but they do so indirectly.  A single WhereMaskSet structure translates
       
 86071 ** cursor number into bits and the translated bit is stored in the prereq
       
 86072 ** fields.  The translation is used in order to maximize the number of
       
 86073 ** bits that will fit in a Bitmask.  The VDBE cursor numbers might be
       
 86074 ** spread out over the non-negative integers.  For example, the cursor
       
 86075 ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45.  The WhereMaskSet
       
 86076 ** translates these sparse cursor numbers into consecutive integers
       
 86077 ** beginning with 0 in order to make the best possible use of the available
       
 86078 ** bits in the Bitmask.  So, in the example above, the cursor numbers
       
 86079 ** would be mapped into integers 0 through 7.
       
 86080 **
       
 86081 ** The number of terms in a join is limited by the number of bits
       
 86082 ** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
       
 86083 ** is only able to process joins with 64 or fewer tables.
       
 86084 */
       
 86085 typedef struct WhereTerm WhereTerm;
       
 86086 struct WhereTerm {
       
 86087   Expr *pExpr;            /* Pointer to the subexpression that is this term */
       
 86088   int iParent;            /* Disable pWC->a[iParent] when this term disabled */
       
 86089   int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
       
 86090   union {
       
 86091     int leftColumn;         /* Column number of X in "X <op> <expr>" */
       
 86092     WhereOrInfo *pOrInfo;   /* Extra information if eOperator==WO_OR */
       
 86093     WhereAndInfo *pAndInfo; /* Extra information if eOperator==WO_AND */
       
 86094   } u;
       
 86095   u16 eOperator;          /* A WO_xx value describing <op> */
       
 86096   u8 wtFlags;             /* TERM_xxx bit flags.  See below */
       
 86097   u8 nChild;              /* Number of children that must disable us */
       
 86098   WhereClause *pWC;       /* The clause this term is part of */
       
 86099   Bitmask prereqRight;    /* Bitmask of tables used by pExpr->pRight */
       
 86100   Bitmask prereqAll;      /* Bitmask of tables referenced by pExpr */
       
 86101 };
       
 86102 
       
 86103 /*
       
 86104 ** Allowed values of WhereTerm.wtFlags
       
 86105 */
       
 86106 #define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
       
 86107 #define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
       
 86108 #define TERM_CODED      0x04   /* This term is already coded */
       
 86109 #define TERM_COPIED     0x08   /* Has a child */
       
 86110 #define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
       
 86111 #define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
       
 86112 #define TERM_OR_OK      0x40   /* Used during OR-clause processing */
       
 86113 
       
 86114 /*
       
 86115 ** An instance of the following structure holds all information about a
       
 86116 ** WHERE clause.  Mostly this is a container for one or more WhereTerms.
       
 86117 */
       
 86118 struct WhereClause {
       
 86119   Parse *pParse;           /* The parser context */
       
 86120   WhereMaskSet *pMaskSet;  /* Mapping of table cursor numbers to bitmasks */
       
 86121   Bitmask vmask;           /* Bitmask identifying virtual table cursors */
       
 86122   u8 op;                   /* Split operator.  TK_AND or TK_OR */
       
 86123   int nTerm;               /* Number of terms */
       
 86124   int nSlot;               /* Number of entries in a[] */
       
 86125   WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
       
 86126 #if defined(SQLITE_SMALL_STACK)
       
 86127   WhereTerm aStatic[1];    /* Initial static space for a[] */
       
 86128 #else
       
 86129   WhereTerm aStatic[8];    /* Initial static space for a[] */
       
 86130 #endif
       
 86131 };
       
 86132 
       
 86133 /*
       
 86134 ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
       
 86135 ** a dynamically allocated instance of the following structure.
       
 86136 */
       
 86137 struct WhereOrInfo {
       
 86138   WhereClause wc;          /* Decomposition into subterms */
       
 86139   Bitmask indexable;       /* Bitmask of all indexable tables in the clause */
       
 86140 };
       
 86141 
       
 86142 /*
       
 86143 ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
       
 86144 ** a dynamically allocated instance of the following structure.
       
 86145 */
       
 86146 struct WhereAndInfo {
       
 86147   WhereClause wc;          /* The subexpression broken out */
       
 86148 };
       
 86149 
       
 86150 /*
       
 86151 ** An instance of the following structure keeps track of a mapping
       
 86152 ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
       
 86153 **
       
 86154 ** The VDBE cursor numbers are small integers contained in 
       
 86155 ** SrcList_item.iCursor and Expr.iTable fields.  For any given WHERE 
       
 86156 ** clause, the cursor numbers might not begin with 0 and they might
       
 86157 ** contain gaps in the numbering sequence.  But we want to make maximum
       
 86158 ** use of the bits in our bitmasks.  This structure provides a mapping
       
 86159 ** from the sparse cursor numbers into consecutive integers beginning
       
 86160 ** with 0.
       
 86161 **
       
 86162 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
       
 86163 ** corresponds VDBE cursor number B.  The A-th bit of a bitmask is 1<<A.
       
 86164 **
       
 86165 ** For example, if the WHERE clause expression used these VDBE
       
 86166 ** cursors:  4, 5, 8, 29, 57, 73.  Then the  WhereMaskSet structure
       
 86167 ** would map those cursor numbers into bits 0 through 5.
       
 86168 **
       
 86169 ** Note that the mapping is not necessarily ordered.  In the example
       
 86170 ** above, the mapping might go like this:  4->3, 5->1, 8->2, 29->0,
       
 86171 ** 57->5, 73->4.  Or one of 719 other combinations might be used. It
       
 86172 ** does not really matter.  What is important is that sparse cursor
       
 86173 ** numbers all get mapped into bit numbers that begin with 0 and contain
       
 86174 ** no gaps.
       
 86175 */
       
 86176 struct WhereMaskSet {
       
 86177   int n;                        /* Number of assigned cursor values */
       
 86178   int ix[BMS];                  /* Cursor assigned to each bit */
       
 86179 };
       
 86180 
       
 86181 /*
       
 86182 ** A WhereCost object records a lookup strategy and the estimated
       
 86183 ** cost of pursuing that strategy.
       
 86184 */
       
 86185 struct WhereCost {
       
 86186   WherePlan plan;    /* The lookup strategy */
       
 86187   double rCost;      /* Overall cost of pursuing this search strategy */
       
 86188   double nRow;       /* Estimated number of output rows */
       
 86189   Bitmask used;      /* Bitmask of cursors used by this plan */
       
 86190 };
       
 86191 
       
 86192 /*
       
 86193 ** Bitmasks for the operators that indices are able to exploit.  An
       
 86194 ** OR-ed combination of these values can be used when searching for
       
 86195 ** terms in the where clause.
       
 86196 */
       
 86197 #define WO_IN     0x001
       
 86198 #define WO_EQ     0x002
       
 86199 #define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
       
 86200 #define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
       
 86201 #define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
       
 86202 #define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
       
 86203 #define WO_MATCH  0x040
       
 86204 #define WO_ISNULL 0x080
       
 86205 #define WO_OR     0x100       /* Two or more OR-connected terms */
       
 86206 #define WO_AND    0x200       /* Two or more AND-connected terms */
       
 86207 
       
 86208 #define WO_ALL    0xfff       /* Mask of all possible WO_* values */
       
 86209 #define WO_SINGLE 0x0ff       /* Mask of all non-compound WO_* values */
       
 86210 
       
 86211 /*
       
 86212 ** Value for wsFlags returned by bestIndex() and stored in
       
 86213 ** WhereLevel.wsFlags.  These flags determine which search
       
 86214 ** strategies are appropriate.
       
 86215 **
       
 86216 ** The least significant 12 bits is reserved as a mask for WO_ values above.
       
 86217 ** The WhereLevel.wsFlags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
       
 86218 ** But if the table is the right table of a left join, WhereLevel.wsFlags
       
 86219 ** is set to WO_IN|WO_EQ.  The WhereLevel.wsFlags field can then be used as
       
 86220 ** the "op" parameter to findTerm when we are resolving equality constraints.
       
 86221 ** ISNULL constraints will then not be used on the right table of a left
       
 86222 ** join.  Tickets #2177 and #2189.
       
 86223 */
       
 86224 #define WHERE_ROWID_EQ     0x00001000  /* rowid=EXPR or rowid IN (...) */
       
 86225 #define WHERE_ROWID_RANGE  0x00002000  /* rowid<EXPR and/or rowid>EXPR */
       
 86226 #define WHERE_COLUMN_EQ    0x00010000  /* x=EXPR or x IN (...) or x IS NULL */
       
 86227 #define WHERE_COLUMN_RANGE 0x00020000  /* x<EXPR and/or x>EXPR */
       
 86228 #define WHERE_COLUMN_IN    0x00040000  /* x IN (...) */
       
 86229 #define WHERE_COLUMN_NULL  0x00080000  /* x IS NULL */
       
 86230 #define WHERE_INDEXED      0x000f0000  /* Anything that uses an index */
       
 86231 #define WHERE_IN_ABLE      0x000f1000  /* Able to support an IN operator */
       
 86232 #define WHERE_TOP_LIMIT    0x00100000  /* x<EXPR or x<=EXPR constraint */
       
 86233 #define WHERE_BTM_LIMIT    0x00200000  /* x>EXPR or x>=EXPR constraint */
       
 86234 #define WHERE_IDX_ONLY     0x00800000  /* Use index only - omit table */
       
 86235 #define WHERE_ORDERBY      0x01000000  /* Output will appear in correct order */
       
 86236 #define WHERE_REVERSE      0x02000000  /* Scan in reverse order */
       
 86237 #define WHERE_UNIQUE       0x04000000  /* Selects no more than one row */
       
 86238 #define WHERE_VIRTUALTABLE 0x08000000  /* Use virtual-table processing */
       
 86239 #define WHERE_MULTI_OR     0x10000000  /* OR using multiple indices */
       
 86240 
       
 86241 /*
       
 86242 ** Initialize a preallocated WhereClause structure.
       
 86243 */
       
 86244 static void whereClauseInit(
       
 86245   WhereClause *pWC,        /* The WhereClause to be initialized */
       
 86246   Parse *pParse,           /* The parsing context */
       
 86247   WhereMaskSet *pMaskSet   /* Mapping from table cursor numbers to bitmasks */
       
 86248 ){
       
 86249   pWC->pParse = pParse;
       
 86250   pWC->pMaskSet = pMaskSet;
       
 86251   pWC->nTerm = 0;
       
 86252   pWC->nSlot = ArraySize(pWC->aStatic);
       
 86253   pWC->a = pWC->aStatic;
       
 86254   pWC->vmask = 0;
       
 86255 }
       
 86256 
       
 86257 /* Forward reference */
       
 86258 static void whereClauseClear(WhereClause*);
       
 86259 
       
 86260 /*
       
 86261 ** Deallocate all memory associated with a WhereOrInfo object.
       
 86262 */
       
 86263 static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
       
 86264   whereClauseClear(&p->wc);
       
 86265   sqlite3DbFree(db, p);
       
 86266 }
       
 86267 
       
 86268 /*
       
 86269 ** Deallocate all memory associated with a WhereAndInfo object.
       
 86270 */
       
 86271 static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
       
 86272   whereClauseClear(&p->wc);
       
 86273   sqlite3DbFree(db, p);
       
 86274 }
       
 86275 
       
 86276 /*
       
 86277 ** Deallocate a WhereClause structure.  The WhereClause structure
       
 86278 ** itself is not freed.  This routine is the inverse of whereClauseInit().
       
 86279 */
       
 86280 static void whereClauseClear(WhereClause *pWC){
       
 86281   int i;
       
 86282   WhereTerm *a;
       
 86283   sqlite3 *db = pWC->pParse->db;
       
 86284   for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
       
 86285     if( a->wtFlags & TERM_DYNAMIC ){
       
 86286       sqlite3ExprDelete(db, a->pExpr);
       
 86287     }
       
 86288     if( a->wtFlags & TERM_ORINFO ){
       
 86289       whereOrInfoDelete(db, a->u.pOrInfo);
       
 86290     }else if( a->wtFlags & TERM_ANDINFO ){
       
 86291       whereAndInfoDelete(db, a->u.pAndInfo);
       
 86292     }
       
 86293   }
       
 86294   if( pWC->a!=pWC->aStatic ){
       
 86295     sqlite3DbFree(db, pWC->a);
       
 86296   }
       
 86297 }
       
 86298 
       
 86299 /*
       
 86300 ** Add a single new WhereTerm entry to the WhereClause object pWC.
       
 86301 ** The new WhereTerm object is constructed from Expr p and with wtFlags.
       
 86302 ** The index in pWC->a[] of the new WhereTerm is returned on success.
       
 86303 ** 0 is returned if the new WhereTerm could not be added due to a memory
       
 86304 ** allocation error.  The memory allocation failure will be recorded in
       
 86305 ** the db->mallocFailed flag so that higher-level functions can detect it.
       
 86306 **
       
 86307 ** This routine will increase the size of the pWC->a[] array as necessary.
       
 86308 **
       
 86309 ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
       
 86310 ** for freeing the expression p is assumed by the WhereClause object pWC.
       
 86311 ** This is true even if this routine fails to allocate a new WhereTerm.
       
 86312 **
       
 86313 ** WARNING:  This routine might reallocate the space used to store
       
 86314 ** WhereTerms.  All pointers to WhereTerms should be invalidated after
       
 86315 ** calling this routine.  Such pointers may be reinitialized by referencing
       
 86316 ** the pWC->a[] array.
       
 86317 */
       
 86318 static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
       
 86319   WhereTerm *pTerm;
       
 86320   int idx;
       
 86321   if( pWC->nTerm>=pWC->nSlot ){
       
 86322     WhereTerm *pOld = pWC->a;
       
 86323     sqlite3 *db = pWC->pParse->db;
       
 86324     pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
       
 86325     if( pWC->a==0 ){
       
 86326       if( wtFlags & TERM_DYNAMIC ){
       
 86327         sqlite3ExprDelete(db, p);
       
 86328       }
       
 86329       pWC->a = pOld;
       
 86330       return 0;
       
 86331     }
       
 86332     memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
       
 86333     if( pOld!=pWC->aStatic ){
       
 86334       sqlite3DbFree(db, pOld);
       
 86335     }
       
 86336     pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
       
 86337   }
       
 86338   pTerm = &pWC->a[idx = pWC->nTerm++];
       
 86339   pTerm->pExpr = p;
       
 86340   pTerm->wtFlags = wtFlags;
       
 86341   pTerm->pWC = pWC;
       
 86342   pTerm->iParent = -1;
       
 86343   return idx;
       
 86344 }
       
 86345 
       
 86346 /*
       
 86347 ** This routine identifies subexpressions in the WHERE clause where
       
 86348 ** each subexpression is separated by the AND operator or some other
       
 86349 ** operator specified in the op parameter.  The WhereClause structure
       
 86350 ** is filled with pointers to subexpressions.  For example:
       
 86351 **
       
 86352 **    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
       
 86353 **           \________/     \_______________/     \________________/
       
 86354 **            slot[0]            slot[1]               slot[2]
       
 86355 **
       
 86356 ** The original WHERE clause in pExpr is unaltered.  All this routine
       
 86357 ** does is make slot[] entries point to substructure within pExpr.
       
 86358 **
       
 86359 ** In the previous sentence and in the diagram, "slot[]" refers to
       
 86360 ** the WhereClause.a[] array.  The slot[] array grows as needed to contain
       
 86361 ** all terms of the WHERE clause.
       
 86362 */
       
 86363 static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){
       
 86364   pWC->op = (u8)op;
       
 86365   if( pExpr==0 ) return;
       
 86366   if( pExpr->op!=op ){
       
 86367     whereClauseInsert(pWC, pExpr, 0);
       
 86368   }else{
       
 86369     whereSplit(pWC, pExpr->pLeft, op);
       
 86370     whereSplit(pWC, pExpr->pRight, op);
       
 86371   }
       
 86372 }
       
 86373 
       
 86374 /*
       
 86375 ** Initialize an expression mask set (a WhereMaskSet object)
       
 86376 */
       
 86377 #define initMaskSet(P)  memset(P, 0, sizeof(*P))
       
 86378 
       
 86379 /*
       
 86380 ** Return the bitmask for the given cursor number.  Return 0 if
       
 86381 ** iCursor is not in the set.
       
 86382 */
       
 86383 static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
       
 86384   int i;
       
 86385   assert( pMaskSet->n<=sizeof(Bitmask)*8 );
       
 86386   for(i=0; i<pMaskSet->n; i++){
       
 86387     if( pMaskSet->ix[i]==iCursor ){
       
 86388       return ((Bitmask)1)<<i;
       
 86389     }
       
 86390   }
       
 86391   return 0;
       
 86392 }
       
 86393 
       
 86394 /*
       
 86395 ** Create a new mask for cursor iCursor.
       
 86396 **
       
 86397 ** There is one cursor per table in the FROM clause.  The number of
       
 86398 ** tables in the FROM clause is limited by a test early in the
       
 86399 ** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
       
 86400 ** array will never overflow.
       
 86401 */
       
 86402 static void createMask(WhereMaskSet *pMaskSet, int iCursor){
       
 86403   assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
       
 86404   pMaskSet->ix[pMaskSet->n++] = iCursor;
       
 86405 }
       
 86406 
       
 86407 /*
       
 86408 ** This routine walks (recursively) an expression tree and generates
       
 86409 ** a bitmask indicating which tables are used in that expression
       
 86410 ** tree.
       
 86411 **
       
 86412 ** In order for this routine to work, the calling function must have
       
 86413 ** previously invoked sqlite3ResolveExprNames() on the expression.  See
       
 86414 ** the header comment on that routine for additional information.
       
 86415 ** The sqlite3ResolveExprNames() routines looks for column names and
       
 86416 ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
       
 86417 ** the VDBE cursor number of the table.  This routine just has to
       
 86418 ** translate the cursor numbers into bitmask values and OR all
       
 86419 ** the bitmasks together.
       
 86420 */
       
 86421 static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
       
 86422 static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
       
 86423 static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
       
 86424   Bitmask mask = 0;
       
 86425   if( p==0 ) return 0;
       
 86426   if( p->op==TK_COLUMN ){
       
 86427     mask = getMask(pMaskSet, p->iTable);
       
 86428     return mask;
       
 86429   }
       
 86430   mask = exprTableUsage(pMaskSet, p->pRight);
       
 86431   mask |= exprTableUsage(pMaskSet, p->pLeft);
       
 86432   if( ExprHasProperty(p, EP_xIsSelect) ){
       
 86433     mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
       
 86434   }else{
       
 86435     mask |= exprListTableUsage(pMaskSet, p->x.pList);
       
 86436   }
       
 86437   return mask;
       
 86438 }
       
 86439 static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
       
 86440   int i;
       
 86441   Bitmask mask = 0;
       
 86442   if( pList ){
       
 86443     for(i=0; i<pList->nExpr; i++){
       
 86444       mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
       
 86445     }
       
 86446   }
       
 86447   return mask;
       
 86448 }
       
 86449 static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
       
 86450   Bitmask mask = 0;
       
 86451   while( pS ){
       
 86452     mask |= exprListTableUsage(pMaskSet, pS->pEList);
       
 86453     mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
       
 86454     mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
       
 86455     mask |= exprTableUsage(pMaskSet, pS->pWhere);
       
 86456     mask |= exprTableUsage(pMaskSet, pS->pHaving);
       
 86457     pS = pS->pPrior;
       
 86458   }
       
 86459   return mask;
       
 86460 }
       
 86461 
       
 86462 /*
       
 86463 ** Return TRUE if the given operator is one of the operators that is
       
 86464 ** allowed for an indexable WHERE clause term.  The allowed operators are
       
 86465 ** "=", "<", ">", "<=", ">=", and "IN".
       
 86466 */
       
 86467 static int allowedOp(int op){
       
 86468   assert( TK_GT>TK_EQ && TK_GT<TK_GE );
       
 86469   assert( TK_LT>TK_EQ && TK_LT<TK_GE );
       
 86470   assert( TK_LE>TK_EQ && TK_LE<TK_GE );
       
 86471   assert( TK_GE==TK_EQ+4 );
       
 86472   return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
       
 86473 }
       
 86474 
       
 86475 /*
       
 86476 ** Swap two objects of type TYPE.
       
 86477 */
       
 86478 #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
       
 86479 
       
 86480 /*
       
 86481 ** Commute a comparison operator.  Expressions of the form "X op Y"
       
 86482 ** are converted into "Y op X".
       
 86483 **
       
 86484 ** If a collation sequence is associated with either the left or right
       
 86485 ** side of the comparison, it remains associated with the same side after
       
 86486 ** the commutation. So "Y collate NOCASE op X" becomes 
       
 86487 ** "X collate NOCASE op Y". This is because any collation sequence on
       
 86488 ** the left hand side of a comparison overrides any collation sequence 
       
 86489 ** attached to the right. For the same reason the EP_ExpCollate flag
       
 86490 ** is not commuted.
       
 86491 */
       
 86492 static void exprCommute(Parse *pParse, Expr *pExpr){
       
 86493   u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
       
 86494   u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
       
 86495   assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
       
 86496   pExpr->pRight->pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
       
 86497   pExpr->pLeft->pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
       
 86498   SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
       
 86499   pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
       
 86500   pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
       
 86501   SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
       
 86502   if( pExpr->op>=TK_GT ){
       
 86503     assert( TK_LT==TK_GT+2 );
       
 86504     assert( TK_GE==TK_LE+2 );
       
 86505     assert( TK_GT>TK_EQ );
       
 86506     assert( TK_GT<TK_LE );
       
 86507     assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
       
 86508     pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
       
 86509   }
       
 86510 }
       
 86511 
       
 86512 /*
       
 86513 ** Translate from TK_xx operator to WO_xx bitmask.
       
 86514 */
       
 86515 static u16 operatorMask(int op){
       
 86516   u16 c;
       
 86517   assert( allowedOp(op) );
       
 86518   if( op==TK_IN ){
       
 86519     c = WO_IN;
       
 86520   }else if( op==TK_ISNULL ){
       
 86521     c = WO_ISNULL;
       
 86522   }else{
       
 86523     assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
       
 86524     c = (u16)(WO_EQ<<(op-TK_EQ));
       
 86525   }
       
 86526   assert( op!=TK_ISNULL || c==WO_ISNULL );
       
 86527   assert( op!=TK_IN || c==WO_IN );
       
 86528   assert( op!=TK_EQ || c==WO_EQ );
       
 86529   assert( op!=TK_LT || c==WO_LT );
       
 86530   assert( op!=TK_LE || c==WO_LE );
       
 86531   assert( op!=TK_GT || c==WO_GT );
       
 86532   assert( op!=TK_GE || c==WO_GE );
       
 86533   return c;
       
 86534 }
       
 86535 
       
 86536 /*
       
 86537 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
       
 86538 ** where X is a reference to the iColumn of table iCur and <op> is one of
       
 86539 ** the WO_xx operator codes specified by the op parameter.
       
 86540 ** Return a pointer to the term.  Return 0 if not found.
       
 86541 */
       
 86542 static WhereTerm *findTerm(
       
 86543   WhereClause *pWC,     /* The WHERE clause to be searched */
       
 86544   int iCur,             /* Cursor number of LHS */
       
 86545   int iColumn,          /* Column number of LHS */
       
 86546   Bitmask notReady,     /* RHS must not overlap with this mask */
       
 86547   u32 op,               /* Mask of WO_xx values describing operator */
       
 86548   Index *pIdx           /* Must be compatible with this index, if not NULL */
       
 86549 ){
       
 86550   WhereTerm *pTerm;
       
 86551   int k;
       
 86552   assert( iCur>=0 );
       
 86553   op &= WO_ALL;
       
 86554   for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
       
 86555     if( pTerm->leftCursor==iCur
       
 86556        && (pTerm->prereqRight & notReady)==0
       
 86557        && pTerm->u.leftColumn==iColumn
       
 86558        && (pTerm->eOperator & op)!=0
       
 86559     ){
       
 86560       if( pIdx && pTerm->eOperator!=WO_ISNULL ){
       
 86561         Expr *pX = pTerm->pExpr;
       
 86562         CollSeq *pColl;
       
 86563         char idxaff;
       
 86564         int j;
       
 86565         Parse *pParse = pWC->pParse;
       
 86566 
       
 86567         idxaff = pIdx->pTable->aCol[iColumn].affinity;
       
 86568         if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
       
 86569 
       
 86570         /* Figure out the collation sequence required from an index for
       
 86571         ** it to be useful for optimising expression pX. Store this
       
 86572         ** value in variable pColl.
       
 86573         */
       
 86574         assert(pX->pLeft);
       
 86575         pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
       
 86576         assert(pColl || pParse->nErr);
       
 86577 
       
 86578         for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
       
 86579           if( NEVER(j>=pIdx->nColumn) ) return 0;
       
 86580         }
       
 86581         if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
       
 86582       }
       
 86583       return pTerm;
       
 86584     }
       
 86585   }
       
 86586   return 0;
       
 86587 }
       
 86588 
       
 86589 /* Forward reference */
       
 86590 static void exprAnalyze(SrcList*, WhereClause*, int);
       
 86591 
       
 86592 /*
       
 86593 ** Call exprAnalyze on all terms in a WHERE clause.  
       
 86594 **
       
 86595 **
       
 86596 */
       
 86597 static void exprAnalyzeAll(
       
 86598   SrcList *pTabList,       /* the FROM clause */
       
 86599   WhereClause *pWC         /* the WHERE clause to be analyzed */
       
 86600 ){
       
 86601   int i;
       
 86602   for(i=pWC->nTerm-1; i>=0; i--){
       
 86603     exprAnalyze(pTabList, pWC, i);
       
 86604   }
       
 86605 }
       
 86606 
       
 86607 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
       
 86608 /*
       
 86609 ** Check to see if the given expression is a LIKE or GLOB operator that
       
 86610 ** can be optimized using inequality constraints.  Return TRUE if it is
       
 86611 ** so and false if not.
       
 86612 **
       
 86613 ** In order for the operator to be optimizible, the RHS must be a string
       
 86614 ** literal that does not begin with a wildcard.  
       
 86615 */
       
 86616 static int isLikeOrGlob(
       
 86617   Parse *pParse,    /* Parsing and code generating context */
       
 86618   Expr *pExpr,      /* Test this expression */
       
 86619   int *pnPattern,   /* Number of non-wildcard prefix characters */
       
 86620   int *pisComplete, /* True if the only wildcard is % in the last character */
       
 86621   int *pnoCase      /* True if uppercase is equivalent to lowercase */
       
 86622 ){
       
 86623   const char *z;             /* String on RHS of LIKE operator */
       
 86624   Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
       
 86625   ExprList *pList;           /* List of operands to the LIKE operator */
       
 86626   int c;                     /* One character in z[] */
       
 86627   int cnt;                   /* Number of non-wildcard prefix characters */
       
 86628   char wc[3];                /* Wildcard characters */
       
 86629   CollSeq *pColl;            /* Collating sequence for LHS */
       
 86630   sqlite3 *db = pParse->db;  /* Database connection */
       
 86631 
       
 86632   if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
       
 86633     return 0;
       
 86634   }
       
 86635 #ifdef SQLITE_EBCDIC
       
 86636   if( *pnoCase ) return 0;
       
 86637 #endif
       
 86638   pList = pExpr->x.pList;
       
 86639   pRight = pList->a[0].pExpr;
       
 86640   if( pRight->op!=TK_STRING ){
       
 86641     return 0;
       
 86642   }
       
 86643   pLeft = pList->a[1].pExpr;
       
 86644   if( pLeft->op!=TK_COLUMN ){
       
 86645     return 0;
       
 86646   }
       
 86647   pColl = sqlite3ExprCollSeq(pParse, pLeft);
       
 86648   assert( pColl!=0 || pLeft->iColumn==-1 );
       
 86649   if( pColl==0 ) return 0;
       
 86650   if( (pColl->type!=SQLITE_COLL_BINARY || *pnoCase) &&
       
 86651       (pColl->type!=SQLITE_COLL_NOCASE || !*pnoCase) ){
       
 86652     return 0;
       
 86653   }
       
 86654   if( sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT ) return 0;
       
 86655   z = pRight->u.zToken;
       
 86656   if( ALWAYS(z) ){
       
 86657     cnt = 0;
       
 86658     while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
       
 86659       cnt++;
       
 86660     }
       
 86661     if( cnt!=0 && c!=0 && 255!=(u8)z[cnt-1] ){
       
 86662       *pisComplete = z[cnt]==wc[0] && z[cnt+1]==0;
       
 86663       *pnPattern = cnt;
       
 86664       return 1;
       
 86665     }
       
 86666   }
       
 86667   return 0;
       
 86668 }
       
 86669 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
       
 86670 
       
 86671 
       
 86672 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 86673 /*
       
 86674 ** Check to see if the given expression is of the form
       
 86675 **
       
 86676 **         column MATCH expr
       
 86677 **
       
 86678 ** If it is then return TRUE.  If not, return FALSE.
       
 86679 */
       
 86680 static int isMatchOfColumn(
       
 86681   Expr *pExpr      /* Test this expression */
       
 86682 ){
       
 86683   ExprList *pList;
       
 86684 
       
 86685   if( pExpr->op!=TK_FUNCTION ){
       
 86686     return 0;
       
 86687   }
       
 86688   if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
       
 86689     return 0;
       
 86690   }
       
 86691   pList = pExpr->x.pList;
       
 86692   if( pList->nExpr!=2 ){
       
 86693     return 0;
       
 86694   }
       
 86695   if( pList->a[1].pExpr->op != TK_COLUMN ){
       
 86696     return 0;
       
 86697   }
       
 86698   return 1;
       
 86699 }
       
 86700 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 86701 
       
 86702 /*
       
 86703 ** If the pBase expression originated in the ON or USING clause of
       
 86704 ** a join, then transfer the appropriate markings over to derived.
       
 86705 */
       
 86706 static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
       
 86707   pDerived->flags |= pBase->flags & EP_FromJoin;
       
 86708   pDerived->iRightJoinTable = pBase->iRightJoinTable;
       
 86709 }
       
 86710 
       
 86711 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
       
 86712 /*
       
 86713 ** Analyze a term that consists of two or more OR-connected
       
 86714 ** subterms.  So in:
       
 86715 **
       
 86716 **     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
       
 86717 **                          ^^^^^^^^^^^^^^^^^^^^
       
 86718 **
       
 86719 ** This routine analyzes terms such as the middle term in the above example.
       
 86720 ** A WhereOrTerm object is computed and attached to the term under
       
 86721 ** analysis, regardless of the outcome of the analysis.  Hence:
       
 86722 **
       
 86723 **     WhereTerm.wtFlags   |=  TERM_ORINFO
       
 86724 **     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
       
 86725 **
       
 86726 ** The term being analyzed must have two or more of OR-connected subterms.
       
 86727 ** A single subterm might be a set of AND-connected sub-subterms.
       
 86728 ** Examples of terms under analysis:
       
 86729 **
       
 86730 **     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
       
 86731 **     (B)     x=expr1 OR expr2=x OR x=expr3
       
 86732 **     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
       
 86733 **     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
       
 86734 **     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
       
 86735 **
       
 86736 ** CASE 1:
       
 86737 **
       
 86738 ** If all subterms are of the form T.C=expr for some single column of C
       
 86739 ** a single table T (as shown in example B above) then create a new virtual
       
 86740 ** term that is an equivalent IN expression.  In other words, if the term
       
 86741 ** being analyzed is:
       
 86742 **
       
 86743 **      x = expr1  OR  expr2 = x  OR  x = expr3
       
 86744 **
       
 86745 ** then create a new virtual term like this:
       
 86746 **
       
 86747 **      x IN (expr1,expr2,expr3)
       
 86748 **
       
 86749 ** CASE 2:
       
 86750 **
       
 86751 ** If all subterms are indexable by a single table T, then set
       
 86752 **
       
 86753 **     WhereTerm.eOperator              =  WO_OR
       
 86754 **     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
       
 86755 **
       
 86756 ** A subterm is "indexable" if it is of the form
       
 86757 ** "T.C <op> <expr>" where C is any column of table T and 
       
 86758 ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
       
 86759 ** A subterm is also indexable if it is an AND of two or more
       
 86760 ** subsubterms at least one of which is indexable.  Indexable AND 
       
 86761 ** subterms have their eOperator set to WO_AND and they have
       
 86762 ** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
       
 86763 **
       
 86764 ** From another point of view, "indexable" means that the subterm could
       
 86765 ** potentially be used with an index if an appropriate index exists.
       
 86766 ** This analysis does not consider whether or not the index exists; that
       
 86767 ** is something the bestIndex() routine will determine.  This analysis
       
 86768 ** only looks at whether subterms appropriate for indexing exist.
       
 86769 **
       
 86770 ** All examples A through E above all satisfy case 2.  But if a term
       
 86771 ** also statisfies case 1 (such as B) we know that the optimizer will
       
 86772 ** always prefer case 1, so in that case we pretend that case 2 is not
       
 86773 ** satisfied.
       
 86774 **
       
 86775 ** It might be the case that multiple tables are indexable.  For example,
       
 86776 ** (E) above is indexable on tables P, Q, and R.
       
 86777 **
       
 86778 ** Terms that satisfy case 2 are candidates for lookup by using
       
 86779 ** separate indices to find rowids for each subterm and composing
       
 86780 ** the union of all rowids using a RowSet object.  This is similar
       
 86781 ** to "bitmap indices" in other database engines.
       
 86782 **
       
 86783 ** OTHERWISE:
       
 86784 **
       
 86785 ** If neither case 1 nor case 2 apply, then leave the eOperator set to
       
 86786 ** zero.  This term is not useful for search.
       
 86787 */
       
 86788 static void exprAnalyzeOrTerm(
       
 86789   SrcList *pSrc,            /* the FROM clause */
       
 86790   WhereClause *pWC,         /* the complete WHERE clause */
       
 86791   int idxTerm               /* Index of the OR-term to be analyzed */
       
 86792 ){
       
 86793   Parse *pParse = pWC->pParse;            /* Parser context */
       
 86794   sqlite3 *db = pParse->db;               /* Database connection */
       
 86795   WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
       
 86796   Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
       
 86797   WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */
       
 86798   int i;                                  /* Loop counters */
       
 86799   WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
       
 86800   WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
       
 86801   WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
       
 86802   Bitmask chngToIN;         /* Tables that might satisfy case 1 */
       
 86803   Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
       
 86804 
       
 86805   /*
       
 86806   ** Break the OR clause into its separate subterms.  The subterms are
       
 86807   ** stored in a WhereClause structure containing within the WhereOrInfo
       
 86808   ** object that is attached to the original OR clause term.
       
 86809   */
       
 86810   assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
       
 86811   assert( pExpr->op==TK_OR );
       
 86812   pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
       
 86813   if( pOrInfo==0 ) return;
       
 86814   pTerm->wtFlags |= TERM_ORINFO;
       
 86815   pOrWc = &pOrInfo->wc;
       
 86816   whereClauseInit(pOrWc, pWC->pParse, pMaskSet);
       
 86817   whereSplit(pOrWc, pExpr, TK_OR);
       
 86818   exprAnalyzeAll(pSrc, pOrWc);
       
 86819   if( db->mallocFailed ) return;
       
 86820   assert( pOrWc->nTerm>=2 );
       
 86821 
       
 86822   /*
       
 86823   ** Compute the set of tables that might satisfy cases 1 or 2.
       
 86824   */
       
 86825   indexable = ~(Bitmask)0;
       
 86826   chngToIN = ~(pWC->vmask);
       
 86827   for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
       
 86828     if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
       
 86829       WhereAndInfo *pAndInfo;
       
 86830       assert( pOrTerm->eOperator==0 );
       
 86831       assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
       
 86832       chngToIN = 0;
       
 86833       pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
       
 86834       if( pAndInfo ){
       
 86835         WhereClause *pAndWC;
       
 86836         WhereTerm *pAndTerm;
       
 86837         int j;
       
 86838         Bitmask b = 0;
       
 86839         pOrTerm->u.pAndInfo = pAndInfo;
       
 86840         pOrTerm->wtFlags |= TERM_ANDINFO;
       
 86841         pOrTerm->eOperator = WO_AND;
       
 86842         pAndWC = &pAndInfo->wc;
       
 86843         whereClauseInit(pAndWC, pWC->pParse, pMaskSet);
       
 86844         whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
       
 86845         exprAnalyzeAll(pSrc, pAndWC);
       
 86846         testcase( db->mallocFailed );
       
 86847         if( !db->mallocFailed ){
       
 86848           for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
       
 86849             assert( pAndTerm->pExpr );
       
 86850             if( allowedOp(pAndTerm->pExpr->op) ){
       
 86851               b |= getMask(pMaskSet, pAndTerm->leftCursor);
       
 86852             }
       
 86853           }
       
 86854         }
       
 86855         indexable &= b;
       
 86856       }
       
 86857     }else if( pOrTerm->wtFlags & TERM_COPIED ){
       
 86858       /* Skip this term for now.  We revisit it when we process the
       
 86859       ** corresponding TERM_VIRTUAL term */
       
 86860     }else{
       
 86861       Bitmask b;
       
 86862       b = getMask(pMaskSet, pOrTerm->leftCursor);
       
 86863       if( pOrTerm->wtFlags & TERM_VIRTUAL ){
       
 86864         WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
       
 86865         b |= getMask(pMaskSet, pOther->leftCursor);
       
 86866       }
       
 86867       indexable &= b;
       
 86868       if( pOrTerm->eOperator!=WO_EQ ){
       
 86869         chngToIN = 0;
       
 86870       }else{
       
 86871         chngToIN &= b;
       
 86872       }
       
 86873     }
       
 86874   }
       
 86875 
       
 86876   /*
       
 86877   ** Record the set of tables that satisfy case 2.  The set might be
       
 86878   ** empty.
       
 86879   */
       
 86880   pOrInfo->indexable = indexable;
       
 86881   pTerm->eOperator = indexable==0 ? 0 : WO_OR;
       
 86882 
       
 86883   /*
       
 86884   ** chngToIN holds a set of tables that *might* satisfy case 1.  But
       
 86885   ** we have to do some additional checking to see if case 1 really
       
 86886   ** is satisfied.
       
 86887   **
       
 86888   ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
       
 86889   ** that there is no possibility of transforming the OR clause into an
       
 86890   ** IN operator because one or more terms in the OR clause contain
       
 86891   ** something other than == on a column in the single table.  The 1-bit
       
 86892   ** case means that every term of the OR clause is of the form
       
 86893   ** "table.column=expr" for some single table.  The one bit that is set
       
 86894   ** will correspond to the common table.  We still need to check to make
       
 86895   ** sure the same column is used on all terms.  The 2-bit case is when
       
 86896   ** the all terms are of the form "table1.column=table2.column".  It
       
 86897   ** might be possible to form an IN operator with either table1.column
       
 86898   ** or table2.column as the LHS if either is common to every term of
       
 86899   ** the OR clause.
       
 86900   **
       
 86901   ** Note that terms of the form "table.column1=table.column2" (the
       
 86902   ** same table on both sizes of the ==) cannot be optimized.
       
 86903   */
       
 86904   if( chngToIN ){
       
 86905     int okToChngToIN = 0;     /* True if the conversion to IN is valid */
       
 86906     int iColumn = -1;         /* Column index on lhs of IN operator */
       
 86907     int iCursor = -1;         /* Table cursor common to all terms */
       
 86908     int j = 0;                /* Loop counter */
       
 86909 
       
 86910     /* Search for a table and column that appears on one side or the
       
 86911     ** other of the == operator in every subterm.  That table and column
       
 86912     ** will be recorded in iCursor and iColumn.  There might not be any
       
 86913     ** such table and column.  Set okToChngToIN if an appropriate table
       
 86914     ** and column is found but leave okToChngToIN false if not found.
       
 86915     */
       
 86916     for(j=0; j<2 && !okToChngToIN; j++){
       
 86917       pOrTerm = pOrWc->a;
       
 86918       for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
       
 86919         assert( pOrTerm->eOperator==WO_EQ );
       
 86920         pOrTerm->wtFlags &= ~TERM_OR_OK;
       
 86921         if( pOrTerm->leftCursor==iCursor ){
       
 86922           /* This is the 2-bit case and we are on the second iteration and
       
 86923           ** current term is from the first iteration.  So skip this term. */
       
 86924           assert( j==1 );
       
 86925           continue;
       
 86926         }
       
 86927         if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){
       
 86928           /* This term must be of the form t1.a==t2.b where t2 is in the
       
 86929           ** chngToIN set but t1 is not.  This term will be either preceeded
       
 86930           ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
       
 86931           ** and use its inversion. */
       
 86932           testcase( pOrTerm->wtFlags & TERM_COPIED );
       
 86933           testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
       
 86934           assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
       
 86935           continue;
       
 86936         }
       
 86937         iColumn = pOrTerm->u.leftColumn;
       
 86938         iCursor = pOrTerm->leftCursor;
       
 86939         break;
       
 86940       }
       
 86941       if( i<0 ){
       
 86942         /* No candidate table+column was found.  This can only occur
       
 86943         ** on the second iteration */
       
 86944         assert( j==1 );
       
 86945         assert( (chngToIN&(chngToIN-1))==0 );
       
 86946         assert( chngToIN==getMask(pMaskSet, iCursor) );
       
 86947         break;
       
 86948       }
       
 86949       testcase( j==1 );
       
 86950 
       
 86951       /* We have found a candidate table and column.  Check to see if that
       
 86952       ** table and column is common to every term in the OR clause */
       
 86953       okToChngToIN = 1;
       
 86954       for(; i>=0 && okToChngToIN; i--, pOrTerm++){
       
 86955         assert( pOrTerm->eOperator==WO_EQ );
       
 86956         if( pOrTerm->leftCursor!=iCursor ){
       
 86957           pOrTerm->wtFlags &= ~TERM_OR_OK;
       
 86958         }else if( pOrTerm->u.leftColumn!=iColumn ){
       
 86959           okToChngToIN = 0;
       
 86960         }else{
       
 86961           int affLeft, affRight;
       
 86962           /* If the right-hand side is also a column, then the affinities
       
 86963           ** of both right and left sides must be such that no type
       
 86964           ** conversions are required on the right.  (Ticket #2249)
       
 86965           */
       
 86966           affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
       
 86967           affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
       
 86968           if( affRight!=0 && affRight!=affLeft ){
       
 86969             okToChngToIN = 0;
       
 86970           }else{
       
 86971             pOrTerm->wtFlags |= TERM_OR_OK;
       
 86972           }
       
 86973         }
       
 86974       }
       
 86975     }
       
 86976 
       
 86977     /* At this point, okToChngToIN is true if original pTerm satisfies
       
 86978     ** case 1.  In that case, construct a new virtual term that is 
       
 86979     ** pTerm converted into an IN operator.
       
 86980     */
       
 86981     if( okToChngToIN ){
       
 86982       Expr *pDup;            /* A transient duplicate expression */
       
 86983       ExprList *pList = 0;   /* The RHS of the IN operator */
       
 86984       Expr *pLeft = 0;       /* The LHS of the IN operator */
       
 86985       Expr *pNew;            /* The complete IN operator */
       
 86986 
       
 86987       for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
       
 86988         if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
       
 86989         assert( pOrTerm->eOperator==WO_EQ );
       
 86990         assert( pOrTerm->leftCursor==iCursor );
       
 86991         assert( pOrTerm->u.leftColumn==iColumn );
       
 86992         pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
       
 86993         pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup);
       
 86994         pLeft = pOrTerm->pExpr->pLeft;
       
 86995       }
       
 86996       assert( pLeft!=0 );
       
 86997       pDup = sqlite3ExprDup(db, pLeft, 0);
       
 86998       pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
       
 86999       if( pNew ){
       
 87000         int idxNew;
       
 87001         transferJoinMarkings(pNew, pExpr);
       
 87002         assert( !ExprHasProperty(pNew, EP_xIsSelect) );
       
 87003         pNew->x.pList = pList;
       
 87004         idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
       
 87005         testcase( idxNew==0 );
       
 87006         exprAnalyze(pSrc, pWC, idxNew);
       
 87007         pTerm = &pWC->a[idxTerm];
       
 87008         pWC->a[idxNew].iParent = idxTerm;
       
 87009         pTerm->nChild = 1;
       
 87010       }else{
       
 87011         sqlite3ExprListDelete(db, pList);
       
 87012       }
       
 87013       pTerm->eOperator = 0;  /* case 1 trumps case 2 */
       
 87014     }
       
 87015   }
       
 87016 }
       
 87017 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
       
 87018 
       
 87019 
       
 87020 /*
       
 87021 ** The input to this routine is an WhereTerm structure with only the
       
 87022 ** "pExpr" field filled in.  The job of this routine is to analyze the
       
 87023 ** subexpression and populate all the other fields of the WhereTerm
       
 87024 ** structure.
       
 87025 **
       
 87026 ** If the expression is of the form "<expr> <op> X" it gets commuted
       
 87027 ** to the standard form of "X <op> <expr>".
       
 87028 **
       
 87029 ** If the expression is of the form "X <op> Y" where both X and Y are
       
 87030 ** columns, then the original expression is unchanged and a new virtual
       
 87031 ** term of the form "Y <op> X" is added to the WHERE clause and
       
 87032 ** analyzed separately.  The original term is marked with TERM_COPIED
       
 87033 ** and the new term is marked with TERM_DYNAMIC (because it's pExpr
       
 87034 ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
       
 87035 ** is a commuted copy of a prior term.)  The original term has nChild=1
       
 87036 ** and the copy has idxParent set to the index of the original term.
       
 87037 */
       
 87038 static void exprAnalyze(
       
 87039   SrcList *pSrc,            /* the FROM clause */
       
 87040   WhereClause *pWC,         /* the WHERE clause */
       
 87041   int idxTerm               /* Index of the term to be analyzed */
       
 87042 ){
       
 87043   WhereTerm *pTerm;                /* The term to be analyzed */
       
 87044   WhereMaskSet *pMaskSet;          /* Set of table index masks */
       
 87045   Expr *pExpr;                     /* The expression to be analyzed */
       
 87046   Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
       
 87047   Bitmask prereqAll;               /* Prerequesites of pExpr */
       
 87048   Bitmask extraRight = 0;
       
 87049   int nPattern;
       
 87050   int isComplete;
       
 87051   int noCase;
       
 87052   int op;                          /* Top-level operator.  pExpr->op */
       
 87053   Parse *pParse = pWC->pParse;     /* Parsing context */
       
 87054   sqlite3 *db = pParse->db;        /* Database connection */
       
 87055 
       
 87056   if( db->mallocFailed ){
       
 87057     return;
       
 87058   }
       
 87059   pTerm = &pWC->a[idxTerm];
       
 87060   pMaskSet = pWC->pMaskSet;
       
 87061   pExpr = pTerm->pExpr;
       
 87062   prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
       
 87063   op = pExpr->op;
       
 87064   if( op==TK_IN ){
       
 87065     assert( pExpr->pRight==0 );
       
 87066     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
       
 87067       pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
       
 87068     }else{
       
 87069       pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
       
 87070     }
       
 87071   }else if( op==TK_ISNULL ){
       
 87072     pTerm->prereqRight = 0;
       
 87073   }else{
       
 87074     pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
       
 87075   }
       
 87076   prereqAll = exprTableUsage(pMaskSet, pExpr);
       
 87077   if( ExprHasProperty(pExpr, EP_FromJoin) ){
       
 87078     Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
       
 87079     prereqAll |= x;
       
 87080     extraRight = x-1;  /* ON clause terms may not be used with an index
       
 87081                        ** on left table of a LEFT JOIN.  Ticket #3015 */
       
 87082   }
       
 87083   pTerm->prereqAll = prereqAll;
       
 87084   pTerm->leftCursor = -1;
       
 87085   pTerm->iParent = -1;
       
 87086   pTerm->eOperator = 0;
       
 87087   if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
       
 87088     Expr *pLeft = pExpr->pLeft;
       
 87089     Expr *pRight = pExpr->pRight;
       
 87090     if( pLeft->op==TK_COLUMN ){
       
 87091       pTerm->leftCursor = pLeft->iTable;
       
 87092       pTerm->u.leftColumn = pLeft->iColumn;
       
 87093       pTerm->eOperator = operatorMask(op);
       
 87094     }
       
 87095     if( pRight && pRight->op==TK_COLUMN ){
       
 87096       WhereTerm *pNew;
       
 87097       Expr *pDup;
       
 87098       if( pTerm->leftCursor>=0 ){
       
 87099         int idxNew;
       
 87100         pDup = sqlite3ExprDup(db, pExpr, 0);
       
 87101         if( db->mallocFailed ){
       
 87102           sqlite3ExprDelete(db, pDup);
       
 87103           return;
       
 87104         }
       
 87105         idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
       
 87106         if( idxNew==0 ) return;
       
 87107         pNew = &pWC->a[idxNew];
       
 87108         pNew->iParent = idxTerm;
       
 87109         pTerm = &pWC->a[idxTerm];
       
 87110         pTerm->nChild = 1;
       
 87111         pTerm->wtFlags |= TERM_COPIED;
       
 87112       }else{
       
 87113         pDup = pExpr;
       
 87114         pNew = pTerm;
       
 87115       }
       
 87116       exprCommute(pParse, pDup);
       
 87117       pLeft = pDup->pLeft;
       
 87118       pNew->leftCursor = pLeft->iTable;
       
 87119       pNew->u.leftColumn = pLeft->iColumn;
       
 87120       pNew->prereqRight = prereqLeft;
       
 87121       pNew->prereqAll = prereqAll;
       
 87122       pNew->eOperator = operatorMask(pDup->op);
       
 87123     }
       
 87124   }
       
 87125 
       
 87126 #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
       
 87127   /* If a term is the BETWEEN operator, create two new virtual terms
       
 87128   ** that define the range that the BETWEEN implements.  For example:
       
 87129   **
       
 87130   **      a BETWEEN b AND c
       
 87131   **
       
 87132   ** is converted into:
       
 87133   **
       
 87134   **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
       
 87135   **
       
 87136   ** The two new terms are added onto the end of the WhereClause object.
       
 87137   ** The new terms are "dynamic" and are children of the original BETWEEN
       
 87138   ** term.  That means that if the BETWEEN term is coded, the children are
       
 87139   ** skipped.  Or, if the children are satisfied by an index, the original
       
 87140   ** BETWEEN term is skipped.
       
 87141   */
       
 87142   else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
       
 87143     ExprList *pList = pExpr->x.pList;
       
 87144     int i;
       
 87145     static const u8 ops[] = {TK_GE, TK_LE};
       
 87146     assert( pList!=0 );
       
 87147     assert( pList->nExpr==2 );
       
 87148     for(i=0; i<2; i++){
       
 87149       Expr *pNewExpr;
       
 87150       int idxNew;
       
 87151       pNewExpr = sqlite3PExpr(pParse, ops[i], 
       
 87152                              sqlite3ExprDup(db, pExpr->pLeft, 0),
       
 87153                              sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
       
 87154       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
       
 87155       testcase( idxNew==0 );
       
 87156       exprAnalyze(pSrc, pWC, idxNew);
       
 87157       pTerm = &pWC->a[idxTerm];
       
 87158       pWC->a[idxNew].iParent = idxTerm;
       
 87159     }
       
 87160     pTerm->nChild = 2;
       
 87161   }
       
 87162 #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
       
 87163 
       
 87164 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
       
 87165   /* Analyze a term that is composed of two or more subterms connected by
       
 87166   ** an OR operator.
       
 87167   */
       
 87168   else if( pExpr->op==TK_OR ){
       
 87169     assert( pWC->op==TK_AND );
       
 87170     exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
       
 87171     pTerm = &pWC->a[idxTerm];
       
 87172   }
       
 87173 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
       
 87174 
       
 87175 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
       
 87176   /* Add constraints to reduce the search space on a LIKE or GLOB
       
 87177   ** operator.
       
 87178   **
       
 87179   ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
       
 87180   **
       
 87181   **          x>='abc' AND x<'abd' AND x LIKE 'abc%'
       
 87182   **
       
 87183   ** The last character of the prefix "abc" is incremented to form the
       
 87184   ** termination condition "abd".
       
 87185   */
       
 87186   if( isLikeOrGlob(pParse, pExpr, &nPattern, &isComplete, &noCase)
       
 87187          && pWC->op==TK_AND ){
       
 87188     Expr *pLeft, *pRight;
       
 87189     Expr *pStr1, *pStr2;
       
 87190     Expr *pNewExpr1, *pNewExpr2;
       
 87191     int idxNew1, idxNew2;
       
 87192 
       
 87193     pLeft = pExpr->x.pList->a[1].pExpr;
       
 87194     pRight = pExpr->x.pList->a[0].pExpr;
       
 87195     pStr1 = sqlite3Expr(db, TK_STRING, pRight->u.zToken);
       
 87196     if( pStr1 ) pStr1->u.zToken[nPattern] = 0;
       
 87197     pStr2 = sqlite3ExprDup(db, pStr1, 0);
       
 87198     if( !db->mallocFailed ){
       
 87199       u8 c, *pC;       /* Last character before the first wildcard */
       
 87200       pC = (u8*)&pStr2->u.zToken[nPattern-1];
       
 87201       c = *pC;
       
 87202       if( noCase ){
       
 87203         /* The point is to increment the last character before the first
       
 87204         ** wildcard.  But if we increment '@', that will push it into the
       
 87205         ** alphabetic range where case conversions will mess up the 
       
 87206         ** inequality.  To avoid this, make sure to also run the full
       
 87207         ** LIKE on all candidate expressions by clearing the isComplete flag
       
 87208         */
       
 87209         if( c=='A'-1 ) isComplete = 0;
       
 87210 
       
 87211         c = sqlite3UpperToLower[c];
       
 87212       }
       
 87213       *pC = c + 1;
       
 87214     }
       
 87215     pNewExpr1 = sqlite3PExpr(pParse, TK_GE, sqlite3ExprDup(db,pLeft,0),pStr1,0);
       
 87216     idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
       
 87217     testcase( idxNew1==0 );
       
 87218     exprAnalyze(pSrc, pWC, idxNew1);
       
 87219     pNewExpr2 = sqlite3PExpr(pParse, TK_LT, sqlite3ExprDup(db,pLeft,0),pStr2,0);
       
 87220     idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
       
 87221     testcase( idxNew2==0 );
       
 87222     exprAnalyze(pSrc, pWC, idxNew2);
       
 87223     pTerm = &pWC->a[idxTerm];
       
 87224     if( isComplete ){
       
 87225       pWC->a[idxNew1].iParent = idxTerm;
       
 87226       pWC->a[idxNew2].iParent = idxTerm;
       
 87227       pTerm->nChild = 2;
       
 87228     }
       
 87229   }
       
 87230 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
       
 87231 
       
 87232 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 87233   /* Add a WO_MATCH auxiliary term to the constraint set if the
       
 87234   ** current expression is of the form:  column MATCH expr.
       
 87235   ** This information is used by the xBestIndex methods of
       
 87236   ** virtual tables.  The native query optimizer does not attempt
       
 87237   ** to do anything with MATCH functions.
       
 87238   */
       
 87239   if( isMatchOfColumn(pExpr) ){
       
 87240     int idxNew;
       
 87241     Expr *pRight, *pLeft;
       
 87242     WhereTerm *pNewTerm;
       
 87243     Bitmask prereqColumn, prereqExpr;
       
 87244 
       
 87245     pRight = pExpr->x.pList->a[0].pExpr;
       
 87246     pLeft = pExpr->x.pList->a[1].pExpr;
       
 87247     prereqExpr = exprTableUsage(pMaskSet, pRight);
       
 87248     prereqColumn = exprTableUsage(pMaskSet, pLeft);
       
 87249     if( (prereqExpr & prereqColumn)==0 ){
       
 87250       Expr *pNewExpr;
       
 87251       pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
       
 87252                               0, sqlite3ExprDup(db, pRight, 0), 0);
       
 87253       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
       
 87254       testcase( idxNew==0 );
       
 87255       pNewTerm = &pWC->a[idxNew];
       
 87256       pNewTerm->prereqRight = prereqExpr;
       
 87257       pNewTerm->leftCursor = pLeft->iTable;
       
 87258       pNewTerm->u.leftColumn = pLeft->iColumn;
       
 87259       pNewTerm->eOperator = WO_MATCH;
       
 87260       pNewTerm->iParent = idxTerm;
       
 87261       pTerm = &pWC->a[idxTerm];
       
 87262       pTerm->nChild = 1;
       
 87263       pTerm->wtFlags |= TERM_COPIED;
       
 87264       pNewTerm->prereqAll = pTerm->prereqAll;
       
 87265     }
       
 87266   }
       
 87267 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 87268 
       
 87269   /* Prevent ON clause terms of a LEFT JOIN from being used to drive
       
 87270   ** an index for tables to the left of the join.
       
 87271   */
       
 87272   pTerm->prereqRight |= extraRight;
       
 87273 }
       
 87274 
       
 87275 /*
       
 87276 ** Return TRUE if any of the expressions in pList->a[iFirst...] contain
       
 87277 ** a reference to any table other than the iBase table.
       
 87278 */
       
 87279 static int referencesOtherTables(
       
 87280   ExprList *pList,          /* Search expressions in ths list */
       
 87281   WhereMaskSet *pMaskSet,   /* Mapping from tables to bitmaps */
       
 87282   int iFirst,               /* Be searching with the iFirst-th expression */
       
 87283   int iBase                 /* Ignore references to this table */
       
 87284 ){
       
 87285   Bitmask allowed = ~getMask(pMaskSet, iBase);
       
 87286   while( iFirst<pList->nExpr ){
       
 87287     if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){
       
 87288       return 1;
       
 87289     }
       
 87290   }
       
 87291   return 0;
       
 87292 }
       
 87293 
       
 87294 
       
 87295 /*
       
 87296 ** This routine decides if pIdx can be used to satisfy the ORDER BY
       
 87297 ** clause.  If it can, it returns 1.  If pIdx cannot satisfy the
       
 87298 ** ORDER BY clause, this routine returns 0.
       
 87299 **
       
 87300 ** pOrderBy is an ORDER BY clause from a SELECT statement.  pTab is the
       
 87301 ** left-most table in the FROM clause of that same SELECT statement and
       
 87302 ** the table has a cursor number of "base".  pIdx is an index on pTab.
       
 87303 **
       
 87304 ** nEqCol is the number of columns of pIdx that are used as equality
       
 87305 ** constraints.  Any of these columns may be missing from the ORDER BY
       
 87306 ** clause and the match can still be a success.
       
 87307 **
       
 87308 ** All terms of the ORDER BY that match against the index must be either
       
 87309 ** ASC or DESC.  (Terms of the ORDER BY clause past the end of a UNIQUE
       
 87310 ** index do not need to satisfy this constraint.)  The *pbRev value is
       
 87311 ** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
       
 87312 ** the ORDER BY clause is all ASC.
       
 87313 */
       
 87314 static int isSortingIndex(
       
 87315   Parse *pParse,          /* Parsing context */
       
 87316   WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */
       
 87317   Index *pIdx,            /* The index we are testing */
       
 87318   int base,               /* Cursor number for the table to be sorted */
       
 87319   ExprList *pOrderBy,     /* The ORDER BY clause */
       
 87320   int nEqCol,             /* Number of index columns with == constraints */
       
 87321   int *pbRev              /* Set to 1 if ORDER BY is DESC */
       
 87322 ){
       
 87323   int i, j;                       /* Loop counters */
       
 87324   int sortOrder = 0;              /* XOR of index and ORDER BY sort direction */
       
 87325   int nTerm;                      /* Number of ORDER BY terms */
       
 87326   struct ExprList_item *pTerm;    /* A term of the ORDER BY clause */
       
 87327   sqlite3 *db = pParse->db;
       
 87328 
       
 87329   assert( pOrderBy!=0 );
       
 87330   nTerm = pOrderBy->nExpr;
       
 87331   assert( nTerm>0 );
       
 87332 
       
 87333   /* Argument pIdx must either point to a 'real' named index structure, 
       
 87334   ** or an index structure allocated on the stack by bestBtreeIndex() to
       
 87335   ** represent the rowid index that is part of every table.  */
       
 87336   assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
       
 87337 
       
 87338   /* Match terms of the ORDER BY clause against columns of
       
 87339   ** the index.
       
 87340   **
       
 87341   ** Note that indices have pIdx->nColumn regular columns plus
       
 87342   ** one additional column containing the rowid.  The rowid column
       
 87343   ** of the index is also allowed to match against the ORDER BY
       
 87344   ** clause.
       
 87345   */
       
 87346   for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<=pIdx->nColumn; i++){
       
 87347     Expr *pExpr;       /* The expression of the ORDER BY pTerm */
       
 87348     CollSeq *pColl;    /* The collating sequence of pExpr */
       
 87349     int termSortOrder; /* Sort order for this term */
       
 87350     int iColumn;       /* The i-th column of the index.  -1 for rowid */
       
 87351     int iSortOrder;    /* 1 for DESC, 0 for ASC on the i-th index term */
       
 87352     const char *zColl; /* Name of the collating sequence for i-th index term */
       
 87353 
       
 87354     pExpr = pTerm->pExpr;
       
 87355     if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
       
 87356       /* Can not use an index sort on anything that is not a column in the
       
 87357       ** left-most table of the FROM clause */
       
 87358       break;
       
 87359     }
       
 87360     pColl = sqlite3ExprCollSeq(pParse, pExpr);
       
 87361     if( !pColl ){
       
 87362       pColl = db->pDfltColl;
       
 87363     }
       
 87364     if( pIdx->zName && i<pIdx->nColumn ){
       
 87365       iColumn = pIdx->aiColumn[i];
       
 87366       if( iColumn==pIdx->pTable->iPKey ){
       
 87367         iColumn = -1;
       
 87368       }
       
 87369       iSortOrder = pIdx->aSortOrder[i];
       
 87370       zColl = pIdx->azColl[i];
       
 87371     }else{
       
 87372       iColumn = -1;
       
 87373       iSortOrder = 0;
       
 87374       zColl = pColl->zName;
       
 87375     }
       
 87376     if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
       
 87377       /* Term j of the ORDER BY clause does not match column i of the index */
       
 87378       if( i<nEqCol ){
       
 87379         /* If an index column that is constrained by == fails to match an
       
 87380         ** ORDER BY term, that is OK.  Just ignore that column of the index
       
 87381         */
       
 87382         continue;
       
 87383       }else if( i==pIdx->nColumn ){
       
 87384         /* Index column i is the rowid.  All other terms match. */
       
 87385         break;
       
 87386       }else{
       
 87387         /* If an index column fails to match and is not constrained by ==
       
 87388         ** then the index cannot satisfy the ORDER BY constraint.
       
 87389         */
       
 87390         return 0;
       
 87391       }
       
 87392     }
       
 87393     assert( pIdx->aSortOrder!=0 || iColumn==-1 );
       
 87394     assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
       
 87395     assert( iSortOrder==0 || iSortOrder==1 );
       
 87396     termSortOrder = iSortOrder ^ pTerm->sortOrder;
       
 87397     if( i>nEqCol ){
       
 87398       if( termSortOrder!=sortOrder ){
       
 87399         /* Indices can only be used if all ORDER BY terms past the
       
 87400         ** equality constraints are all either DESC or ASC. */
       
 87401         return 0;
       
 87402       }
       
 87403     }else{
       
 87404       sortOrder = termSortOrder;
       
 87405     }
       
 87406     j++;
       
 87407     pTerm++;
       
 87408     if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
       
 87409       /* If the indexed column is the primary key and everything matches
       
 87410       ** so far and none of the ORDER BY terms to the right reference other
       
 87411       ** tables in the join, then we are assured that the index can be used 
       
 87412       ** to sort because the primary key is unique and so none of the other
       
 87413       ** columns will make any difference
       
 87414       */
       
 87415       j = nTerm;
       
 87416     }
       
 87417   }
       
 87418 
       
 87419   *pbRev = sortOrder!=0;
       
 87420   if( j>=nTerm ){
       
 87421     /* All terms of the ORDER BY clause are covered by this index so
       
 87422     ** this index can be used for sorting. */
       
 87423     return 1;
       
 87424   }
       
 87425   if( pIdx->onError!=OE_None && i==pIdx->nColumn
       
 87426       && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
       
 87427     /* All terms of this index match some prefix of the ORDER BY clause
       
 87428     ** and the index is UNIQUE and no terms on the tail of the ORDER BY
       
 87429     ** clause reference other tables in a join.  If this is all true then
       
 87430     ** the order by clause is superfluous. */
       
 87431     return 1;
       
 87432   }
       
 87433   return 0;
       
 87434 }
       
 87435 
       
 87436 /*
       
 87437 ** Prepare a crude estimate of the logarithm of the input value.
       
 87438 ** The results need not be exact.  This is only used for estimating
       
 87439 ** the total cost of performing operations with O(logN) or O(NlogN)
       
 87440 ** complexity.  Because N is just a guess, it is no great tragedy if
       
 87441 ** logN is a little off.
       
 87442 */
       
 87443 static double estLog(double N){
       
 87444   double logN = 1;
       
 87445   double x = 10;
       
 87446   while( N>x ){
       
 87447     logN += 1;
       
 87448     x *= 10;
       
 87449   }
       
 87450   return logN;
       
 87451 }
       
 87452 
       
 87453 /*
       
 87454 ** Two routines for printing the content of an sqlite3_index_info
       
 87455 ** structure.  Used for testing and debugging only.  If neither
       
 87456 ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
       
 87457 ** are no-ops.
       
 87458 */
       
 87459 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG)
       
 87460 static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
       
 87461   int i;
       
 87462   if( !sqlite3WhereTrace ) return;
       
 87463   for(i=0; i<p->nConstraint; i++){
       
 87464     sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
       
 87465        i,
       
 87466        p->aConstraint[i].iColumn,
       
 87467        p->aConstraint[i].iTermOffset,
       
 87468        p->aConstraint[i].op,
       
 87469        p->aConstraint[i].usable);
       
 87470   }
       
 87471   for(i=0; i<p->nOrderBy; i++){
       
 87472     sqlite3DebugPrintf("  orderby[%d]: col=%d desc=%d\n",
       
 87473        i,
       
 87474        p->aOrderBy[i].iColumn,
       
 87475        p->aOrderBy[i].desc);
       
 87476   }
       
 87477 }
       
 87478 static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
       
 87479   int i;
       
 87480   if( !sqlite3WhereTrace ) return;
       
 87481   for(i=0; i<p->nConstraint; i++){
       
 87482     sqlite3DebugPrintf("  usage[%d]: argvIdx=%d omit=%d\n",
       
 87483        i,
       
 87484        p->aConstraintUsage[i].argvIndex,
       
 87485        p->aConstraintUsage[i].omit);
       
 87486   }
       
 87487   sqlite3DebugPrintf("  idxNum=%d\n", p->idxNum);
       
 87488   sqlite3DebugPrintf("  idxStr=%s\n", p->idxStr);
       
 87489   sqlite3DebugPrintf("  orderByConsumed=%d\n", p->orderByConsumed);
       
 87490   sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
       
 87491 }
       
 87492 #else
       
 87493 #define TRACE_IDX_INPUTS(A)
       
 87494 #define TRACE_IDX_OUTPUTS(A)
       
 87495 #endif
       
 87496 
       
 87497 /* 
       
 87498 ** Required because bestIndex() is called by bestOrClauseIndex() 
       
 87499 */
       
 87500 static void bestIndex(
       
 87501     Parse*, WhereClause*, struct SrcList_item*, Bitmask, ExprList*, WhereCost*);
       
 87502 
       
 87503 /*
       
 87504 ** This routine attempts to find an scanning strategy that can be used 
       
 87505 ** to optimize an 'OR' expression that is part of a WHERE clause. 
       
 87506 **
       
 87507 ** The table associated with FROM clause term pSrc may be either a
       
 87508 ** regular B-Tree table or a virtual table.
       
 87509 */
       
 87510 static void bestOrClauseIndex(
       
 87511   Parse *pParse,              /* The parsing context */
       
 87512   WhereClause *pWC,           /* The WHERE clause */
       
 87513   struct SrcList_item *pSrc,  /* The FROM clause term to search */
       
 87514   Bitmask notReady,           /* Mask of cursors that are not available */
       
 87515   ExprList *pOrderBy,         /* The ORDER BY clause */
       
 87516   WhereCost *pCost            /* Lowest cost query plan */
       
 87517 ){
       
 87518 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
       
 87519   const int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
       
 87520   const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur);  /* Bitmask for pSrc */
       
 87521   WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm];        /* End of pWC->a[] */
       
 87522   WhereTerm *pTerm;                 /* A single term of the WHERE clause */
       
 87523 
       
 87524   /* Search the WHERE clause terms for a usable WO_OR term. */
       
 87525   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
       
 87526     if( pTerm->eOperator==WO_OR 
       
 87527      && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
       
 87528      && (pTerm->u.pOrInfo->indexable & maskSrc)!=0 
       
 87529     ){
       
 87530       WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
       
 87531       WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
       
 87532       WhereTerm *pOrTerm;
       
 87533       int flags = WHERE_MULTI_OR;
       
 87534       double rTotal = 0;
       
 87535       double nRow = 0;
       
 87536       Bitmask used = 0;
       
 87537 
       
 87538       for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
       
 87539         WhereCost sTermCost;
       
 87540         WHERETRACE(("... Multi-index OR testing for term %d of %d....\n", 
       
 87541           (pOrTerm - pOrWC->a), (pTerm - pWC->a)
       
 87542         ));
       
 87543         if( pOrTerm->eOperator==WO_AND ){
       
 87544           WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc;
       
 87545           bestIndex(pParse, pAndWC, pSrc, notReady, 0, &sTermCost);
       
 87546         }else if( pOrTerm->leftCursor==iCur ){
       
 87547           WhereClause tempWC;
       
 87548           tempWC.pParse = pWC->pParse;
       
 87549           tempWC.pMaskSet = pWC->pMaskSet;
       
 87550           tempWC.op = TK_AND;
       
 87551           tempWC.a = pOrTerm;
       
 87552           tempWC.nTerm = 1;
       
 87553           bestIndex(pParse, &tempWC, pSrc, notReady, 0, &sTermCost);
       
 87554         }else{
       
 87555           continue;
       
 87556         }
       
 87557         rTotal += sTermCost.rCost;
       
 87558         nRow += sTermCost.nRow;
       
 87559         used |= sTermCost.used;
       
 87560         if( rTotal>=pCost->rCost ) break;
       
 87561       }
       
 87562 
       
 87563       /* If there is an ORDER BY clause, increase the scan cost to account 
       
 87564       ** for the cost of the sort. */
       
 87565       if( pOrderBy!=0 ){
       
 87566         rTotal += nRow*estLog(nRow);
       
 87567         WHERETRACE(("... sorting increases OR cost to %.9g\n", rTotal));
       
 87568       }
       
 87569 
       
 87570       /* If the cost of scanning using this OR term for optimization is
       
 87571       ** less than the current cost stored in pCost, replace the contents
       
 87572       ** of pCost. */
       
 87573       WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
       
 87574       if( rTotal<pCost->rCost ){
       
 87575         pCost->rCost = rTotal;
       
 87576         pCost->nRow = nRow;
       
 87577         pCost->used = used;
       
 87578         pCost->plan.wsFlags = flags;
       
 87579         pCost->plan.u.pTerm = pTerm;
       
 87580       }
       
 87581     }
       
 87582   }
       
 87583 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
       
 87584 }
       
 87585 
       
 87586 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 87587 /*
       
 87588 ** Allocate and populate an sqlite3_index_info structure. It is the 
       
 87589 ** responsibility of the caller to eventually release the structure
       
 87590 ** by passing the pointer returned by this function to sqlite3_free().
       
 87591 */
       
 87592 static sqlite3_index_info *allocateIndexInfo(
       
 87593   Parse *pParse, 
       
 87594   WhereClause *pWC,
       
 87595   struct SrcList_item *pSrc,
       
 87596   ExprList *pOrderBy
       
 87597 ){
       
 87598   int i, j;
       
 87599   int nTerm;
       
 87600   struct sqlite3_index_constraint *pIdxCons;
       
 87601   struct sqlite3_index_orderby *pIdxOrderBy;
       
 87602   struct sqlite3_index_constraint_usage *pUsage;
       
 87603   WhereTerm *pTerm;
       
 87604   int nOrderBy;
       
 87605   sqlite3_index_info *pIdxInfo;
       
 87606 
       
 87607   WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName));
       
 87608 
       
 87609   /* Count the number of possible WHERE clause constraints referring
       
 87610   ** to this virtual table */
       
 87611   for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
       
 87612     if( pTerm->leftCursor != pSrc->iCursor ) continue;
       
 87613     assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
       
 87614     testcase( pTerm->eOperator==WO_IN );
       
 87615     testcase( pTerm->eOperator==WO_ISNULL );
       
 87616     if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
       
 87617     nTerm++;
       
 87618   }
       
 87619 
       
 87620   /* If the ORDER BY clause contains only columns in the current 
       
 87621   ** virtual table then allocate space for the aOrderBy part of
       
 87622   ** the sqlite3_index_info structure.
       
 87623   */
       
 87624   nOrderBy = 0;
       
 87625   if( pOrderBy ){
       
 87626     for(i=0; i<pOrderBy->nExpr; i++){
       
 87627       Expr *pExpr = pOrderBy->a[i].pExpr;
       
 87628       if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
       
 87629     }
       
 87630     if( i==pOrderBy->nExpr ){
       
 87631       nOrderBy = pOrderBy->nExpr;
       
 87632     }
       
 87633   }
       
 87634 
       
 87635   /* Allocate the sqlite3_index_info structure
       
 87636   */
       
 87637   pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
       
 87638                            + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
       
 87639                            + sizeof(*pIdxOrderBy)*nOrderBy );
       
 87640   if( pIdxInfo==0 ){
       
 87641     sqlite3ErrorMsg(pParse, "out of memory");
       
 87642     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
       
 87643     return 0;
       
 87644   }
       
 87645 
       
 87646   /* Initialize the structure.  The sqlite3_index_info structure contains
       
 87647   ** many fields that are declared "const" to prevent xBestIndex from
       
 87648   ** changing them.  We have to do some funky casting in order to
       
 87649   ** initialize those fields.
       
 87650   */
       
 87651   pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
       
 87652   pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
       
 87653   pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
       
 87654   *(int*)&pIdxInfo->nConstraint = nTerm;
       
 87655   *(int*)&pIdxInfo->nOrderBy = nOrderBy;
       
 87656   *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
       
 87657   *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
       
 87658   *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
       
 87659                                                                    pUsage;
       
 87660 
       
 87661   for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
       
 87662     if( pTerm->leftCursor != pSrc->iCursor ) continue;
       
 87663     assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
       
 87664     testcase( pTerm->eOperator==WO_IN );
       
 87665     testcase( pTerm->eOperator==WO_ISNULL );
       
 87666     if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
       
 87667     pIdxCons[j].iColumn = pTerm->u.leftColumn;
       
 87668     pIdxCons[j].iTermOffset = i;
       
 87669     pIdxCons[j].op = (u8)pTerm->eOperator;
       
 87670     /* The direct assignment in the previous line is possible only because
       
 87671     ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
       
 87672     ** following asserts verify this fact. */
       
 87673     assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
       
 87674     assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
       
 87675     assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
       
 87676     assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
       
 87677     assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
       
 87678     assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
       
 87679     assert( pTerm->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
       
 87680     j++;
       
 87681   }
       
 87682   for(i=0; i<nOrderBy; i++){
       
 87683     Expr *pExpr = pOrderBy->a[i].pExpr;
       
 87684     pIdxOrderBy[i].iColumn = pExpr->iColumn;
       
 87685     pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
       
 87686   }
       
 87687 
       
 87688   return pIdxInfo;
       
 87689 }
       
 87690 
       
 87691 /*
       
 87692 ** The table object reference passed as the second argument to this function
       
 87693 ** must represent a virtual table. This function invokes the xBestIndex()
       
 87694 ** method of the virtual table with the sqlite3_index_info pointer passed
       
 87695 ** as the argument.
       
 87696 **
       
 87697 ** If an error occurs, pParse is populated with an error message and a
       
 87698 ** non-zero value is returned. Otherwise, 0 is returned and the output
       
 87699 ** part of the sqlite3_index_info structure is left populated.
       
 87700 **
       
 87701 ** Whether or not an error is returned, it is the responsibility of the
       
 87702 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
       
 87703 ** that this is required.
       
 87704 */
       
 87705 static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
       
 87706   sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
       
 87707   int i;
       
 87708   int rc;
       
 87709 
       
 87710   (void)sqlite3SafetyOff(pParse->db);
       
 87711   WHERETRACE(("xBestIndex for %s\n", pTab->zName));
       
 87712   TRACE_IDX_INPUTS(p);
       
 87713   rc = pVtab->pModule->xBestIndex(pVtab, p);
       
 87714   TRACE_IDX_OUTPUTS(p);
       
 87715   (void)sqlite3SafetyOn(pParse->db);
       
 87716 
       
 87717   if( rc!=SQLITE_OK ){
       
 87718     if( rc==SQLITE_NOMEM ){
       
 87719       pParse->db->mallocFailed = 1;
       
 87720     }else if( !pVtab->zErrMsg ){
       
 87721       sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
       
 87722     }else{
       
 87723       sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
       
 87724     }
       
 87725   }
       
 87726   sqlite3DbFree(pParse->db, pVtab->zErrMsg);
       
 87727   pVtab->zErrMsg = 0;
       
 87728 
       
 87729   for(i=0; i<p->nConstraint; i++){
       
 87730     if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
       
 87731       sqlite3ErrorMsg(pParse, 
       
 87732           "table %s: xBestIndex returned an invalid plan", pTab->zName);
       
 87733     }
       
 87734   }
       
 87735 
       
 87736   return pParse->nErr;
       
 87737 }
       
 87738 
       
 87739 
       
 87740 /*
       
 87741 ** Compute the best index for a virtual table.
       
 87742 **
       
 87743 ** The best index is computed by the xBestIndex method of the virtual
       
 87744 ** table module.  This routine is really just a wrapper that sets up
       
 87745 ** the sqlite3_index_info structure that is used to communicate with
       
 87746 ** xBestIndex.
       
 87747 **
       
 87748 ** In a join, this routine might be called multiple times for the
       
 87749 ** same virtual table.  The sqlite3_index_info structure is created
       
 87750 ** and initialized on the first invocation and reused on all subsequent
       
 87751 ** invocations.  The sqlite3_index_info structure is also used when
       
 87752 ** code is generated to access the virtual table.  The whereInfoDelete() 
       
 87753 ** routine takes care of freeing the sqlite3_index_info structure after
       
 87754 ** everybody has finished with it.
       
 87755 */
       
 87756 static void bestVirtualIndex(
       
 87757   Parse *pParse,                  /* The parsing context */
       
 87758   WhereClause *pWC,               /* The WHERE clause */
       
 87759   struct SrcList_item *pSrc,      /* The FROM clause term to search */
       
 87760   Bitmask notReady,               /* Mask of cursors that are not available */
       
 87761   ExprList *pOrderBy,             /* The order by clause */
       
 87762   WhereCost *pCost,               /* Lowest cost query plan */
       
 87763   sqlite3_index_info **ppIdxInfo  /* Index information passed to xBestIndex */
       
 87764 ){
       
 87765   Table *pTab = pSrc->pTab;
       
 87766   sqlite3_index_info *pIdxInfo;
       
 87767   struct sqlite3_index_constraint *pIdxCons;
       
 87768   struct sqlite3_index_constraint_usage *pUsage;
       
 87769   WhereTerm *pTerm;
       
 87770   int i, j;
       
 87771   int nOrderBy;
       
 87772 
       
 87773   /* Make sure wsFlags is initialized to some sane value. Otherwise, if the 
       
 87774   ** malloc in allocateIndexInfo() fails and this function returns leaving
       
 87775   ** wsFlags in an uninitialized state, the caller may behave unpredictably.
       
 87776   */
       
 87777   memset(pCost, 0, sizeof(*pCost));
       
 87778   pCost->plan.wsFlags = WHERE_VIRTUALTABLE;
       
 87779 
       
 87780   /* If the sqlite3_index_info structure has not been previously
       
 87781   ** allocated and initialized, then allocate and initialize it now.
       
 87782   */
       
 87783   pIdxInfo = *ppIdxInfo;
       
 87784   if( pIdxInfo==0 ){
       
 87785     *ppIdxInfo = pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pOrderBy);
       
 87786   }
       
 87787   if( pIdxInfo==0 ){
       
 87788     return;
       
 87789   }
       
 87790 
       
 87791   /* At this point, the sqlite3_index_info structure that pIdxInfo points
       
 87792   ** to will have been initialized, either during the current invocation or
       
 87793   ** during some prior invocation.  Now we just have to customize the
       
 87794   ** details of pIdxInfo for the current invocation and pass it to
       
 87795   ** xBestIndex.
       
 87796   */
       
 87797 
       
 87798   /* The module name must be defined. Also, by this point there must
       
 87799   ** be a pointer to an sqlite3_vtab structure. Otherwise
       
 87800   ** sqlite3ViewGetColumnNames() would have picked up the error. 
       
 87801   */
       
 87802   assert( pTab->azModuleArg && pTab->azModuleArg[0] );
       
 87803   assert( sqlite3GetVTable(pParse->db, pTab) );
       
 87804 
       
 87805   /* Set the aConstraint[].usable fields and initialize all 
       
 87806   ** output variables to zero.
       
 87807   **
       
 87808   ** aConstraint[].usable is true for constraints where the right-hand
       
 87809   ** side contains only references to tables to the left of the current
       
 87810   ** table.  In other words, if the constraint is of the form:
       
 87811   **
       
 87812   **           column = expr
       
 87813   **
       
 87814   ** and we are evaluating a join, then the constraint on column is 
       
 87815   ** only valid if all tables referenced in expr occur to the left
       
 87816   ** of the table containing column.
       
 87817   **
       
 87818   ** The aConstraints[] array contains entries for all constraints
       
 87819   ** on the current table.  That way we only have to compute it once
       
 87820   ** even though we might try to pick the best index multiple times.
       
 87821   ** For each attempt at picking an index, the order of tables in the
       
 87822   ** join might be different so we have to recompute the usable flag
       
 87823   ** each time.
       
 87824   */
       
 87825   pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
       
 87826   pUsage = pIdxInfo->aConstraintUsage;
       
 87827   for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
       
 87828     j = pIdxCons->iTermOffset;
       
 87829     pTerm = &pWC->a[j];
       
 87830     pIdxCons->usable = (pTerm->prereqRight&notReady) ? 0 : 1;
       
 87831   }
       
 87832   memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
       
 87833   if( pIdxInfo->needToFreeIdxStr ){
       
 87834     sqlite3_free(pIdxInfo->idxStr);
       
 87835   }
       
 87836   pIdxInfo->idxStr = 0;
       
 87837   pIdxInfo->idxNum = 0;
       
 87838   pIdxInfo->needToFreeIdxStr = 0;
       
 87839   pIdxInfo->orderByConsumed = 0;
       
 87840   /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
       
 87841   pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
       
 87842   nOrderBy = pIdxInfo->nOrderBy;
       
 87843   if( !pOrderBy ){
       
 87844     pIdxInfo->nOrderBy = 0;
       
 87845   }
       
 87846 
       
 87847   if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
       
 87848     return;
       
 87849   }
       
 87850 
       
 87851   pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
       
 87852   for(i=0; i<pIdxInfo->nConstraint; i++){
       
 87853     if( pUsage[i].argvIndex>0 ){
       
 87854       pCost->used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
       
 87855     }
       
 87856   }
       
 87857 
       
 87858   /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
       
 87859   ** inital value of lowestCost in this loop. If it is, then the
       
 87860   ** (cost<lowestCost) test below will never be true.
       
 87861   ** 
       
 87862   ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT 
       
 87863   ** is defined.
       
 87864   */
       
 87865   if( (SQLITE_BIG_DBL/((double)2))<pIdxInfo->estimatedCost ){
       
 87866     pCost->rCost = (SQLITE_BIG_DBL/((double)2));
       
 87867   }else{
       
 87868     pCost->rCost = pIdxInfo->estimatedCost;
       
 87869   }
       
 87870   pCost->plan.u.pVtabIdx = pIdxInfo;
       
 87871   if( pIdxInfo->orderByConsumed ){
       
 87872     pCost->plan.wsFlags |= WHERE_ORDERBY;
       
 87873   }
       
 87874   pCost->plan.nEq = 0;
       
 87875   pIdxInfo->nOrderBy = nOrderBy;
       
 87876 
       
 87877   /* Try to find a more efficient access pattern by using multiple indexes
       
 87878   ** to optimize an OR expression within the WHERE clause. 
       
 87879   */
       
 87880   bestOrClauseIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost);
       
 87881 }
       
 87882 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 87883 
       
 87884 /*
       
 87885 ** Argument pIdx is a pointer to an index structure that has an array of
       
 87886 ** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
       
 87887 ** stored in Index.aSample. The domain of values stored in said column
       
 87888 ** may be thought of as divided into (SQLITE_INDEX_SAMPLES+1) regions.
       
 87889 ** Region 0 contains all values smaller than the first sample value. Region
       
 87890 ** 1 contains values larger than or equal to the value of the first sample,
       
 87891 ** but smaller than the value of the second. And so on.
       
 87892 **
       
 87893 ** If successful, this function determines which of the regions value 
       
 87894 ** pVal lies in, sets *piRegion to the region index (a value between 0
       
 87895 ** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
       
 87896 ** Or, if an OOM occurs while converting text values between encodings,
       
 87897 ** SQLITE_NOMEM is returned and *piRegion is undefined.
       
 87898 */
       
 87899 #ifdef SQLITE_ENABLE_STAT2
       
 87900 static int whereRangeRegion(
       
 87901   Parse *pParse,              /* Database connection */
       
 87902   Index *pIdx,                /* Index to consider domain of */
       
 87903   sqlite3_value *pVal,        /* Value to consider */
       
 87904   int *piRegion               /* OUT: Region of domain in which value lies */
       
 87905 ){
       
 87906   if( ALWAYS(pVal) ){
       
 87907     IndexSample *aSample = pIdx->aSample;
       
 87908     int i = 0;
       
 87909     int eType = sqlite3_value_type(pVal);
       
 87910 
       
 87911     if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
       
 87912       double r = sqlite3_value_double(pVal);
       
 87913       for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
       
 87914         if( aSample[i].eType==SQLITE_NULL ) continue;
       
 87915         if( aSample[i].eType>=SQLITE_TEXT || aSample[i].u.r>r ) break;
       
 87916       }
       
 87917     }else{ 
       
 87918       sqlite3 *db = pParse->db;
       
 87919       CollSeq *pColl;
       
 87920       const u8 *z;
       
 87921       int n;
       
 87922 
       
 87923       /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
       
 87924       assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
       
 87925 
       
 87926       if( eType==SQLITE_BLOB ){
       
 87927         z = (const u8 *)sqlite3_value_blob(pVal);
       
 87928         pColl = db->pDfltColl;
       
 87929         assert( pColl->enc==SQLITE_UTF8 );
       
 87930       }else{
       
 87931         pColl = sqlite3GetCollSeq(db, SQLITE_UTF8, 0, *pIdx->azColl);
       
 87932         if( pColl==0 ){
       
 87933           sqlite3ErrorMsg(pParse, "no such collation sequence: %s",
       
 87934                           *pIdx->azColl);
       
 87935           return SQLITE_ERROR;
       
 87936         }
       
 87937         z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
       
 87938         if( !z ){
       
 87939           return SQLITE_NOMEM;
       
 87940         }
       
 87941         assert( z && pColl && pColl->xCmp );
       
 87942       }
       
 87943       n = sqlite3ValueBytes(pVal, pColl->enc);
       
 87944 
       
 87945       for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
       
 87946         int r;
       
 87947         int eSampletype = aSample[i].eType;
       
 87948         if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
       
 87949         if( (eSampletype!=eType) ) break;
       
 87950 #ifndef SQLITE_OMIT_UTF16
       
 87951         if( pColl->enc!=SQLITE_UTF8 ){
       
 87952           int nSample;
       
 87953           char *zSample = sqlite3Utf8to16(
       
 87954               db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
       
 87955           );
       
 87956           if( !zSample ){
       
 87957             assert( db->mallocFailed );
       
 87958             return SQLITE_NOMEM;
       
 87959           }
       
 87960           r = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
       
 87961           sqlite3DbFree(db, zSample);
       
 87962         }else
       
 87963 #endif
       
 87964         {
       
 87965           r = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
       
 87966         }
       
 87967         if( r>0 ) break;
       
 87968       }
       
 87969     }
       
 87970 
       
 87971     assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
       
 87972     *piRegion = i;
       
 87973   }
       
 87974   return SQLITE_OK;
       
 87975 }
       
 87976 #endif   /* #ifdef SQLITE_ENABLE_STAT2 */
       
 87977 
       
 87978 /*
       
 87979 ** This function is used to estimate the number of rows that will be visited
       
 87980 ** by scanning an index for a range of values. The range may have an upper
       
 87981 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
       
 87982 ** and lower bounds are represented by pLower and pUpper respectively. For
       
 87983 ** example, assuming that index p is on t1(a):
       
 87984 **
       
 87985 **   ... FROM t1 WHERE a > ? AND a < ? ...
       
 87986 **                    |_____|   |_____|
       
 87987 **                       |         |
       
 87988 **                     pLower    pUpper
       
 87989 **
       
 87990 ** If either of the upper or lower bound is not present, then NULL is passed in
       
 87991 ** place of the corresponding WhereTerm.
       
 87992 **
       
 87993 ** The nEq parameter is passed the index of the index column subject to the
       
 87994 ** range constraint. Or, equivalently, the number of equality constraints
       
 87995 ** optimized by the proposed index scan. For example, assuming index p is
       
 87996 ** on t1(a, b), and the SQL query is:
       
 87997 **
       
 87998 **   ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
       
 87999 **
       
 88000 ** then nEq should be passed the value 1 (as the range restricted column,
       
 88001 ** b, is the second left-most column of the index). Or, if the query is:
       
 88002 **
       
 88003 **   ... FROM t1 WHERE a > ? AND a < ? ...
       
 88004 **
       
 88005 ** then nEq should be passed 0.
       
 88006 **
       
 88007 ** The returned value is an integer between 1 and 100, inclusive. A return
       
 88008 ** value of 1 indicates that the proposed range scan is expected to visit
       
 88009 ** approximately 1/100th (1%) of the rows selected by the nEq equality
       
 88010 ** constraints (if any). A return value of 100 indicates that it is expected
       
 88011 ** that the range scan will visit every row (100%) selected by the equality
       
 88012 ** constraints.
       
 88013 **
       
 88014 ** In the absence of sqlite_stat2 ANALYZE data, each range inequality
       
 88015 ** reduces the search space by 2/3rds.  Hence a single constraint (x>?)
       
 88016 ** results in a return of 33 and a range constraint (x>? AND x<?) results
       
 88017 ** in a return of 11.
       
 88018 */
       
 88019 static int whereRangeScanEst(
       
 88020   Parse *pParse,       /* Parsing & code generating context */
       
 88021   Index *p,            /* The index containing the range-compared column; "x" */
       
 88022   int nEq,             /* index into p->aCol[] of the range-compared column */
       
 88023   WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
       
 88024   WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
       
 88025   int *piEst           /* OUT: Return value */
       
 88026 ){
       
 88027   int rc = SQLITE_OK;
       
 88028 
       
 88029 #ifdef SQLITE_ENABLE_STAT2
       
 88030   sqlite3 *db = pParse->db;
       
 88031   sqlite3_value *pLowerVal = 0;
       
 88032   sqlite3_value *pUpperVal = 0;
       
 88033 
       
 88034   if( nEq==0 && p->aSample ){
       
 88035     int iEst;
       
 88036     int iLower = 0;
       
 88037     int iUpper = SQLITE_INDEX_SAMPLES;
       
 88038     u8 aff = p->pTable->aCol[0].affinity;
       
 88039 
       
 88040     if( pLower ){
       
 88041       Expr *pExpr = pLower->pExpr->pRight;
       
 88042       rc = sqlite3ValueFromExpr(db, pExpr, SQLITE_UTF8, aff, &pLowerVal);
       
 88043     }
       
 88044     if( rc==SQLITE_OK && pUpper ){
       
 88045       Expr *pExpr = pUpper->pExpr->pRight;
       
 88046       rc = sqlite3ValueFromExpr(db, pExpr, SQLITE_UTF8, aff, &pUpperVal);
       
 88047     }
       
 88048 
       
 88049     if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
       
 88050       sqlite3ValueFree(pLowerVal);
       
 88051       sqlite3ValueFree(pUpperVal);
       
 88052       goto range_est_fallback;
       
 88053     }else if( pLowerVal==0 ){
       
 88054       rc = whereRangeRegion(pParse, p, pUpperVal, &iUpper);
       
 88055       if( pLower ) iLower = iUpper/2;
       
 88056     }else if( pUpperVal==0 ){
       
 88057       rc = whereRangeRegion(pParse, p, pLowerVal, &iLower);
       
 88058       if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
       
 88059     }else{
       
 88060       rc = whereRangeRegion(pParse, p, pUpperVal, &iUpper);
       
 88061       if( rc==SQLITE_OK ){
       
 88062         rc = whereRangeRegion(pParse, p, pLowerVal, &iLower);
       
 88063       }
       
 88064     }
       
 88065 
       
 88066     iEst = iUpper - iLower;
       
 88067     testcase( iEst==SQLITE_INDEX_SAMPLES );
       
 88068     assert( iEst<=SQLITE_INDEX_SAMPLES );
       
 88069     if( iEst<1 ){
       
 88070       iEst = 1;
       
 88071     }
       
 88072 
       
 88073     sqlite3ValueFree(pLowerVal);
       
 88074     sqlite3ValueFree(pUpperVal);
       
 88075     *piEst = (iEst * 100)/SQLITE_INDEX_SAMPLES;
       
 88076     return rc;
       
 88077   }
       
 88078 range_est_fallback:
       
 88079 #else
       
 88080   UNUSED_PARAMETER(pParse);
       
 88081   UNUSED_PARAMETER(p);
       
 88082   UNUSED_PARAMETER(nEq);
       
 88083 #endif
       
 88084   assert( pLower || pUpper );
       
 88085   if( pLower && pUpper ){
       
 88086     *piEst = 11;
       
 88087   }else{
       
 88088     *piEst = 33;
       
 88089   }
       
 88090   return rc;
       
 88091 }
       
 88092 
       
 88093 
       
 88094 /*
       
 88095 ** Find the query plan for accessing a particular table.  Write the
       
 88096 ** best query plan and its cost into the WhereCost object supplied as the
       
 88097 ** last parameter.
       
 88098 **
       
 88099 ** The lowest cost plan wins.  The cost is an estimate of the amount of
       
 88100 ** CPU and disk I/O need to process the request using the selected plan.
       
 88101 ** Factors that influence cost include:
       
 88102 **
       
 88103 **    *  The estimated number of rows that will be retrieved.  (The
       
 88104 **       fewer the better.)
       
 88105 **
       
 88106 **    *  Whether or not sorting must occur.
       
 88107 **
       
 88108 **    *  Whether or not there must be separate lookups in the
       
 88109 **       index and in the main table.
       
 88110 **
       
 88111 ** If there was an INDEXED BY clause (pSrc->pIndex) attached to the table in
       
 88112 ** the SQL statement, then this function only considers plans using the 
       
 88113 ** named index. If no such plan is found, then the returned cost is
       
 88114 ** SQLITE_BIG_DBL. If a plan is found that uses the named index, 
       
 88115 ** then the cost is calculated in the usual way.
       
 88116 **
       
 88117 ** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table 
       
 88118 ** in the SELECT statement, then no indexes are considered. However, the 
       
 88119 ** selected plan may still take advantage of the tables built-in rowid
       
 88120 ** index.
       
 88121 */
       
 88122 static void bestBtreeIndex(
       
 88123   Parse *pParse,              /* The parsing context */
       
 88124   WhereClause *pWC,           /* The WHERE clause */
       
 88125   struct SrcList_item *pSrc,  /* The FROM clause term to search */
       
 88126   Bitmask notReady,           /* Mask of cursors that are not available */
       
 88127   ExprList *pOrderBy,         /* The ORDER BY clause */
       
 88128   WhereCost *pCost            /* Lowest cost query plan */
       
 88129 ){
       
 88130   int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
       
 88131   Index *pProbe;              /* An index we are evaluating */
       
 88132   Index *pIdx;                /* Copy of pProbe, or zero for IPK index */
       
 88133   int eqTermMask;             /* Current mask of valid equality operators */
       
 88134   int idxEqTermMask;          /* Index mask of valid equality operators */
       
 88135   Index sPk;                  /* A fake index object for the primary key */
       
 88136   unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
       
 88137   int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
       
 88138   int wsFlagMask;             /* Allowed flags in pCost->plan.wsFlag */
       
 88139 
       
 88140   /* Initialize the cost to a worst-case value */
       
 88141   memset(pCost, 0, sizeof(*pCost));
       
 88142   pCost->rCost = SQLITE_BIG_DBL;
       
 88143 
       
 88144   /* If the pSrc table is the right table of a LEFT JOIN then we may not
       
 88145   ** use an index to satisfy IS NULL constraints on that table.  This is
       
 88146   ** because columns might end up being NULL if the table does not match -
       
 88147   ** a circumstance which the index cannot help us discover.  Ticket #2177.
       
 88148   */
       
 88149   if( pSrc->jointype & JT_LEFT ){
       
 88150     idxEqTermMask = WO_EQ|WO_IN;
       
 88151   }else{
       
 88152     idxEqTermMask = WO_EQ|WO_IN|WO_ISNULL;
       
 88153   }
       
 88154 
       
 88155   if( pSrc->pIndex ){
       
 88156     /* An INDEXED BY clause specifies a particular index to use */
       
 88157     pIdx = pProbe = pSrc->pIndex;
       
 88158     wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
       
 88159     eqTermMask = idxEqTermMask;
       
 88160   }else{
       
 88161     /* There is no INDEXED BY clause.  Create a fake Index object to
       
 88162     ** represent the primary key */
       
 88163     Index *pFirst;                /* Any other index on the table */
       
 88164     memset(&sPk, 0, sizeof(Index));
       
 88165     sPk.nColumn = 1;
       
 88166     sPk.aiColumn = &aiColumnPk;
       
 88167     sPk.aiRowEst = aiRowEstPk;
       
 88168     aiRowEstPk[1] = 1;
       
 88169     sPk.onError = OE_Replace;
       
 88170     sPk.pTable = pSrc->pTab;
       
 88171     pFirst = pSrc->pTab->pIndex;
       
 88172     if( pSrc->notIndexed==0 ){
       
 88173       sPk.pNext = pFirst;
       
 88174     }
       
 88175     /* The aiRowEstPk[0] is an estimate of the total number of rows in the
       
 88176     ** table.  Get this information from the ANALYZE information if it is
       
 88177     ** available.  If not available, assume the table 1 million rows in size.
       
 88178     */
       
 88179     if( pFirst ){
       
 88180       assert( pFirst->aiRowEst!=0 ); /* Allocated together with pFirst */
       
 88181       aiRowEstPk[0] = pFirst->aiRowEst[0];
       
 88182     }else{
       
 88183       aiRowEstPk[0] = 1000000;
       
 88184     }
       
 88185     pProbe = &sPk;
       
 88186     wsFlagMask = ~(
       
 88187         WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE
       
 88188     );
       
 88189     eqTermMask = WO_EQ|WO_IN;
       
 88190     pIdx = 0;
       
 88191   }
       
 88192 
       
 88193   /* Loop over all indices looking for the best one to use
       
 88194   */
       
 88195   for(; pProbe; pIdx=pProbe=pProbe->pNext){
       
 88196     const unsigned int * const aiRowEst = pProbe->aiRowEst;
       
 88197     double cost;                /* Cost of using pProbe */
       
 88198     double nRow;                /* Estimated number of rows in result set */
       
 88199     int rev;                    /* True to scan in reverse order */
       
 88200     int wsFlags = 0;
       
 88201     Bitmask used = 0;
       
 88202 
       
 88203     /* The following variables are populated based on the properties of
       
 88204     ** scan being evaluated. They are then used to determine the expected
       
 88205     ** cost and number of rows returned.
       
 88206     **
       
 88207     **  nEq: 
       
 88208     **    Number of equality terms that can be implemented using the index.
       
 88209     **
       
 88210     **  nInMul:  
       
 88211     **    The "in-multiplier". This is an estimate of how many seek operations 
       
 88212     **    SQLite must perform on the index in question. For example, if the 
       
 88213     **    WHERE clause is:
       
 88214     **
       
 88215     **      WHERE a IN (1, 2, 3) AND b IN (4, 5, 6)
       
 88216     **
       
 88217     **    SQLite must perform 9 lookups on an index on (a, b), so nInMul is 
       
 88218     **    set to 9. Given the same schema and either of the following WHERE 
       
 88219     **    clauses:
       
 88220     **
       
 88221     **      WHERE a =  1
       
 88222     **      WHERE a >= 2
       
 88223     **
       
 88224     **    nInMul is set to 1.
       
 88225     **
       
 88226     **    If there exists a WHERE term of the form "x IN (SELECT ...)", then 
       
 88227     **    the sub-select is assumed to return 25 rows for the purposes of 
       
 88228     **    determining nInMul.
       
 88229     **
       
 88230     **  bInEst:  
       
 88231     **    Set to true if there was at least one "x IN (SELECT ...)" term used 
       
 88232     **    in determining the value of nInMul.
       
 88233     **
       
 88234     **  nBound:
       
 88235     **    An estimate on the amount of the table that must be searched.  A
       
 88236     **    value of 100 means the entire table is searched.  Range constraints
       
 88237     **    might reduce this to a value less than 100 to indicate that only
       
 88238     **    a fraction of the table needs searching.  In the absence of
       
 88239     **    sqlite_stat2 ANALYZE data, a single inequality reduces the search
       
 88240     **    space to 1/3rd its original size.  So an x>? constraint reduces
       
 88241     **    nBound to 33.  Two constraints (x>? AND x<?) reduce nBound to 11.
       
 88242     **
       
 88243     **  bSort:   
       
 88244     **    Boolean. True if there is an ORDER BY clause that will require an 
       
 88245     **    external sort (i.e. scanning the index being evaluated will not 
       
 88246     **    correctly order records).
       
 88247     **
       
 88248     **  bLookup: 
       
 88249     **    Boolean. True if for each index entry visited a lookup on the 
       
 88250     **    corresponding table b-tree is required. This is always false 
       
 88251     **    for the rowid index. For other indexes, it is true unless all the 
       
 88252     **    columns of the table used by the SELECT statement are present in 
       
 88253     **    the index (such an index is sometimes described as a covering index).
       
 88254     **    For example, given the index on (a, b), the second of the following 
       
 88255     **    two queries requires table b-tree lookups, but the first does not.
       
 88256     **
       
 88257     **             SELECT a, b    FROM tbl WHERE a = 1;
       
 88258     **             SELECT a, b, c FROM tbl WHERE a = 1;
       
 88259     */
       
 88260     int nEq;
       
 88261     int bInEst = 0;
       
 88262     int nInMul = 1;
       
 88263     int nBound = 100;
       
 88264     int bSort = 0;
       
 88265     int bLookup = 0;
       
 88266 
       
 88267     /* Determine the values of nEq and nInMul */
       
 88268     for(nEq=0; nEq<pProbe->nColumn; nEq++){
       
 88269       WhereTerm *pTerm;           /* A single term of the WHERE clause */
       
 88270       int j = pProbe->aiColumn[nEq];
       
 88271       pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
       
 88272       if( pTerm==0 ) break;
       
 88273       wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
       
 88274       if( pTerm->eOperator & WO_IN ){
       
 88275         Expr *pExpr = pTerm->pExpr;
       
 88276         wsFlags |= WHERE_COLUMN_IN;
       
 88277         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
       
 88278           nInMul *= 25;
       
 88279           bInEst = 1;
       
 88280         }else if( pExpr->x.pList ){
       
 88281           nInMul *= pExpr->x.pList->nExpr + 1;
       
 88282         }
       
 88283       }else if( pTerm->eOperator & WO_ISNULL ){
       
 88284         wsFlags |= WHERE_COLUMN_NULL;
       
 88285       }
       
 88286       used |= pTerm->prereqRight;
       
 88287     }
       
 88288 
       
 88289     /* Determine the value of nBound. */
       
 88290     if( nEq<pProbe->nColumn ){
       
 88291       int j = pProbe->aiColumn[nEq];
       
 88292       if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
       
 88293         WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
       
 88294         WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
       
 88295         whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &nBound);
       
 88296         if( pTop ){
       
 88297           wsFlags |= WHERE_TOP_LIMIT;
       
 88298           used |= pTop->prereqRight;
       
 88299         }
       
 88300         if( pBtm ){
       
 88301           wsFlags |= WHERE_BTM_LIMIT;
       
 88302           used |= pBtm->prereqRight;
       
 88303         }
       
 88304         wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
       
 88305       }
       
 88306     }else if( pProbe->onError!=OE_None ){
       
 88307       testcase( wsFlags & WHERE_COLUMN_IN );
       
 88308       testcase( wsFlags & WHERE_COLUMN_NULL );
       
 88309       if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
       
 88310         wsFlags |= WHERE_UNIQUE;
       
 88311       }
       
 88312     }
       
 88313 
       
 88314     /* If there is an ORDER BY clause and the index being considered will
       
 88315     ** naturally scan rows in the required order, set the appropriate flags
       
 88316     ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
       
 88317     ** will scan rows in a different order, set the bSort variable.  */
       
 88318     if( pOrderBy ){
       
 88319       if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0
       
 88320         && isSortingIndex(pParse,pWC->pMaskSet,pProbe,iCur,pOrderBy,nEq,&rev)
       
 88321       ){
       
 88322         wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
       
 88323         wsFlags |= (rev ? WHERE_REVERSE : 0);
       
 88324       }else{
       
 88325         bSort = 1;
       
 88326       }
       
 88327     }
       
 88328 
       
 88329     /* If currently calculating the cost of using an index (not the IPK
       
 88330     ** index), determine if all required column data may be obtained without 
       
 88331     ** seeking to entries in the main table (i.e. if the index is a covering
       
 88332     ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
       
 88333     ** wsFlags. Otherwise, set the bLookup variable to true.  */
       
 88334     if( pIdx && wsFlags ){
       
 88335       Bitmask m = pSrc->colUsed;
       
 88336       int j;
       
 88337       for(j=0; j<pIdx->nColumn; j++){
       
 88338         int x = pIdx->aiColumn[j];
       
 88339         if( x<BMS-1 ){
       
 88340           m &= ~(((Bitmask)1)<<x);
       
 88341         }
       
 88342       }
       
 88343       if( m==0 ){
       
 88344         wsFlags |= WHERE_IDX_ONLY;
       
 88345       }else{
       
 88346         bLookup = 1;
       
 88347       }
       
 88348     }
       
 88349 
       
 88350     /**** Begin adding up the cost of using this index (Needs improvements)
       
 88351     **
       
 88352     ** Estimate the number of rows of output.  For an IN operator,
       
 88353     ** do not let the estimate exceed half the rows in the table.
       
 88354     */
       
 88355     nRow = (double)(aiRowEst[nEq] * nInMul);
       
 88356     if( bInEst && nRow*2>aiRowEst[0] ){
       
 88357       nRow = aiRowEst[0]/2;
       
 88358       nInMul = (int)(nRow / aiRowEst[nEq]);
       
 88359     }
       
 88360 
       
 88361     /* Assume constant cost to access a row and logarithmic cost to
       
 88362     ** do a binary search.  Hence, the initial cost is the number of output
       
 88363     ** rows plus log2(table-size) times the number of binary searches.
       
 88364     */
       
 88365     cost = nRow + nInMul*estLog(aiRowEst[0]);
       
 88366 
       
 88367     /* Adjust the number of rows and the cost downward to reflect rows
       
 88368     ** that are excluded by range constraints.
       
 88369     */
       
 88370     nRow = (nRow * (double)nBound) / (double)100;
       
 88371     cost = (cost * (double)nBound) / (double)100;
       
 88372 
       
 88373     /* Add in the estimated cost of sorting the result
       
 88374     */
       
 88375     if( bSort ){
       
 88376       cost += cost*estLog(cost);
       
 88377     }
       
 88378 
       
 88379     /* If all information can be taken directly from the index, we avoid
       
 88380     ** doing table lookups.  This reduces the cost by half.  (Not really -
       
 88381     ** this needs to be fixed.)
       
 88382     */
       
 88383     if( pIdx && bLookup==0 ){
       
 88384       cost /= (double)2;
       
 88385     }
       
 88386     /**** Cost of using this index has now been computed ****/
       
 88387 
       
 88388     WHERETRACE((
       
 88389       "tbl=%s idx=%s nEq=%d nInMul=%d nBound=%d bSort=%d bLookup=%d"
       
 88390       " wsFlags=%d   (nRow=%.2f cost=%.2f)\n",
       
 88391       pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), 
       
 88392       nEq, nInMul, nBound, bSort, bLookup, wsFlags, nRow, cost
       
 88393     ));
       
 88394 
       
 88395     /* If this index is the best we have seen so far, then record this
       
 88396     ** index and its cost in the pCost structure.
       
 88397     */
       
 88398     if( (!pIdx || wsFlags) && cost<pCost->rCost ){
       
 88399       pCost->rCost = cost;
       
 88400       pCost->nRow = nRow;
       
 88401       pCost->used = used;
       
 88402       pCost->plan.wsFlags = (wsFlags&wsFlagMask);
       
 88403       pCost->plan.nEq = nEq;
       
 88404       pCost->plan.u.pIdx = pIdx;
       
 88405     }
       
 88406 
       
 88407     /* If there was an INDEXED BY clause, then only that one index is
       
 88408     ** considered. */
       
 88409     if( pSrc->pIndex ) break;
       
 88410 
       
 88411     /* Reset masks for the next index in the loop */
       
 88412     wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
       
 88413     eqTermMask = idxEqTermMask;
       
 88414   }
       
 88415 
       
 88416   /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
       
 88417   ** is set, then reverse the order that the index will be scanned
       
 88418   ** in. This is used for application testing, to help find cases
       
 88419   ** where application behaviour depends on the (undefined) order that
       
 88420   ** SQLite outputs rows in in the absence of an ORDER BY clause.  */
       
 88421   if( !pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
       
 88422     pCost->plan.wsFlags |= WHERE_REVERSE;
       
 88423   }
       
 88424 
       
 88425   assert( pOrderBy || (pCost->plan.wsFlags&WHERE_ORDERBY)==0 );
       
 88426   assert( pCost->plan.u.pIdx==0 || (pCost->plan.wsFlags&WHERE_ROWID_EQ)==0 );
       
 88427   assert( pSrc->pIndex==0 
       
 88428        || pCost->plan.u.pIdx==0 
       
 88429        || pCost->plan.u.pIdx==pSrc->pIndex 
       
 88430   );
       
 88431 
       
 88432   WHERETRACE(("best index is: %s\n", 
       
 88433     (pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
       
 88434   ));
       
 88435   
       
 88436   bestOrClauseIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost);
       
 88437   pCost->plan.wsFlags |= eqTermMask;
       
 88438 }
       
 88439 
       
 88440 /*
       
 88441 ** Find the query plan for accessing table pSrc->pTab. Write the
       
 88442 ** best query plan and its cost into the WhereCost object supplied 
       
 88443 ** as the last parameter. This function may calculate the cost of
       
 88444 ** both real and virtual table scans.
       
 88445 */
       
 88446 static void bestIndex(
       
 88447   Parse *pParse,              /* The parsing context */
       
 88448   WhereClause *pWC,           /* The WHERE clause */
       
 88449   struct SrcList_item *pSrc,  /* The FROM clause term to search */
       
 88450   Bitmask notReady,           /* Mask of cursors that are not available */
       
 88451   ExprList *pOrderBy,         /* The ORDER BY clause */
       
 88452   WhereCost *pCost            /* Lowest cost query plan */
       
 88453 ){
       
 88454 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 88455   if( IsVirtual(pSrc->pTab) ){
       
 88456     sqlite3_index_info *p = 0;
       
 88457     bestVirtualIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost, &p);
       
 88458     if( p->needToFreeIdxStr ){
       
 88459       sqlite3_free(p->idxStr);
       
 88460     }
       
 88461     sqlite3DbFree(pParse->db, p);
       
 88462   }else
       
 88463 #endif
       
 88464   {
       
 88465     bestBtreeIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost);
       
 88466   }
       
 88467 }
       
 88468 
       
 88469 /*
       
 88470 ** Disable a term in the WHERE clause.  Except, do not disable the term
       
 88471 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
       
 88472 ** or USING clause of that join.
       
 88473 **
       
 88474 ** Consider the term t2.z='ok' in the following queries:
       
 88475 **
       
 88476 **   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
       
 88477 **   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
       
 88478 **   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
       
 88479 **
       
 88480 ** The t2.z='ok' is disabled in the in (2) because it originates
       
 88481 ** in the ON clause.  The term is disabled in (3) because it is not part
       
 88482 ** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
       
 88483 **
       
 88484 ** Disabling a term causes that term to not be tested in the inner loop
       
 88485 ** of the join.  Disabling is an optimization.  When terms are satisfied
       
 88486 ** by indices, we disable them to prevent redundant tests in the inner
       
 88487 ** loop.  We would get the correct results if nothing were ever disabled,
       
 88488 ** but joins might run a little slower.  The trick is to disable as much
       
 88489 ** as we can without disabling too much.  If we disabled in (1), we'd get
       
 88490 ** the wrong answer.  See ticket #813.
       
 88491 */
       
 88492 static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
       
 88493   if( pTerm
       
 88494       && ALWAYS((pTerm->wtFlags & TERM_CODED)==0)
       
 88495       && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
       
 88496   ){
       
 88497     pTerm->wtFlags |= TERM_CODED;
       
 88498     if( pTerm->iParent>=0 ){
       
 88499       WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
       
 88500       if( (--pOther->nChild)==0 ){
       
 88501         disableTerm(pLevel, pOther);
       
 88502       }
       
 88503     }
       
 88504   }
       
 88505 }
       
 88506 
       
 88507 /*
       
 88508 ** Code an OP_Affinity opcode to apply the column affinity string zAff
       
 88509 ** to the n registers starting at base. 
       
 88510 **
       
 88511 ** Buffer zAff was allocated using sqlite3DbMalloc(). It is the 
       
 88512 ** responsibility of this function to arrange for it to be eventually
       
 88513 ** freed using sqlite3DbFree().
       
 88514 */
       
 88515 static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
       
 88516   Vdbe *v = pParse->pVdbe;
       
 88517   assert( v!=0 );
       
 88518   sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
       
 88519   sqlite3VdbeChangeP4(v, -1, zAff, P4_DYNAMIC);
       
 88520   sqlite3ExprCacheAffinityChange(pParse, base, n);
       
 88521 }
       
 88522 
       
 88523 
       
 88524 /*
       
 88525 ** Generate code for a single equality term of the WHERE clause.  An equality
       
 88526 ** term can be either X=expr or X IN (...).   pTerm is the term to be 
       
 88527 ** coded.
       
 88528 **
       
 88529 ** The current value for the constraint is left in register iReg.
       
 88530 **
       
 88531 ** For a constraint of the form X=expr, the expression is evaluated and its
       
 88532 ** result is left on the stack.  For constraints of the form X IN (...)
       
 88533 ** this routine sets up a loop that will iterate over all values of X.
       
 88534 */
       
 88535 static int codeEqualityTerm(
       
 88536   Parse *pParse,      /* The parsing context */
       
 88537   WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
       
 88538   WhereLevel *pLevel, /* When level of the FROM clause we are working on */
       
 88539   int iTarget         /* Attempt to leave results in this register */
       
 88540 ){
       
 88541   Expr *pX = pTerm->pExpr;
       
 88542   Vdbe *v = pParse->pVdbe;
       
 88543   int iReg;                  /* Register holding results */
       
 88544 
       
 88545   assert( iTarget>0 );
       
 88546   if( pX->op==TK_EQ ){
       
 88547     iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
       
 88548   }else if( pX->op==TK_ISNULL ){
       
 88549     iReg = iTarget;
       
 88550     sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
       
 88551 #ifndef SQLITE_OMIT_SUBQUERY
       
 88552   }else{
       
 88553     int eType;
       
 88554     int iTab;
       
 88555     struct InLoop *pIn;
       
 88556 
       
 88557     assert( pX->op==TK_IN );
       
 88558     iReg = iTarget;
       
 88559     eType = sqlite3FindInIndex(pParse, pX, 0);
       
 88560     iTab = pX->iTable;
       
 88561     sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
       
 88562     assert( pLevel->plan.wsFlags & WHERE_IN_ABLE );
       
 88563     if( pLevel->u.in.nIn==0 ){
       
 88564       pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
       
 88565     }
       
 88566     pLevel->u.in.nIn++;
       
 88567     pLevel->u.in.aInLoop =
       
 88568        sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
       
 88569                               sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
       
 88570     pIn = pLevel->u.in.aInLoop;
       
 88571     if( pIn ){
       
 88572       pIn += pLevel->u.in.nIn - 1;
       
 88573       pIn->iCur = iTab;
       
 88574       if( eType==IN_INDEX_ROWID ){
       
 88575         pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
       
 88576       }else{
       
 88577         pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
       
 88578       }
       
 88579       sqlite3VdbeAddOp1(v, OP_IsNull, iReg);
       
 88580     }else{
       
 88581       pLevel->u.in.nIn = 0;
       
 88582     }
       
 88583 #endif
       
 88584   }
       
 88585   disableTerm(pLevel, pTerm);
       
 88586   return iReg;
       
 88587 }
       
 88588 
       
 88589 /*
       
 88590 ** Generate code that will evaluate all == and IN constraints for an
       
 88591 ** index.  The values for all constraints are left on the stack.
       
 88592 **
       
 88593 ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
       
 88594 ** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
       
 88595 ** The index has as many as three equality constraints, but in this
       
 88596 ** example, the third "c" value is an inequality.  So only two 
       
 88597 ** constraints are coded.  This routine will generate code to evaluate
       
 88598 ** a==5 and b IN (1,2,3).  The current values for a and b will be stored
       
 88599 ** in consecutive registers and the index of the first register is returned.
       
 88600 **
       
 88601 ** In the example above nEq==2.  But this subroutine works for any value
       
 88602 ** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
       
 88603 ** The only thing it does is allocate the pLevel->iMem memory cell.
       
 88604 **
       
 88605 ** This routine always allocates at least one memory cell and returns
       
 88606 ** the index of that memory cell. The code that
       
 88607 ** calls this routine will use that memory cell to store the termination
       
 88608 ** key value of the loop.  If one or more IN operators appear, then
       
 88609 ** this routine allocates an additional nEq memory cells for internal
       
 88610 ** use.
       
 88611 **
       
 88612 ** Before returning, *pzAff is set to point to a buffer containing a
       
 88613 ** copy of the column affinity string of the index allocated using
       
 88614 ** sqlite3DbMalloc(). Except, entries in the copy of the string associated
       
 88615 ** with equality constraints that use NONE affinity are set to
       
 88616 ** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
       
 88617 **
       
 88618 **   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
       
 88619 **   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
       
 88620 **
       
 88621 ** In the example above, the index on t1(a) has TEXT affinity. But since
       
 88622 ** the right hand side of the equality constraint (t2.b) has NONE affinity,
       
 88623 ** no conversion should be attempted before using a t2.b value as part of
       
 88624 ** a key to search the index. Hence the first byte in the returned affinity
       
 88625 ** string in this example would be set to SQLITE_AFF_NONE.
       
 88626 */
       
 88627 static int codeAllEqualityTerms(
       
 88628   Parse *pParse,        /* Parsing context */
       
 88629   WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
       
 88630   WhereClause *pWC,     /* The WHERE clause */
       
 88631   Bitmask notReady,     /* Which parts of FROM have not yet been coded */
       
 88632   int nExtraReg,        /* Number of extra registers to allocate */
       
 88633   char **pzAff          /* OUT: Set to point to affinity string */
       
 88634 ){
       
 88635   int nEq = pLevel->plan.nEq;   /* The number of == or IN constraints to code */
       
 88636   Vdbe *v = pParse->pVdbe;      /* The vm under construction */
       
 88637   Index *pIdx;                  /* The index being used for this loop */
       
 88638   int iCur = pLevel->iTabCur;   /* The cursor of the table */
       
 88639   WhereTerm *pTerm;             /* A single constraint term */
       
 88640   int j;                        /* Loop counter */
       
 88641   int regBase;                  /* Base register */
       
 88642   int nReg;                     /* Number of registers to allocate */
       
 88643   char *zAff;                   /* Affinity string to return */
       
 88644 
       
 88645   /* This module is only called on query plans that use an index. */
       
 88646   assert( pLevel->plan.wsFlags & WHERE_INDEXED );
       
 88647   pIdx = pLevel->plan.u.pIdx;
       
 88648 
       
 88649   /* Figure out how many memory cells we will need then allocate them.
       
 88650   */
       
 88651   regBase = pParse->nMem + 1;
       
 88652   nReg = pLevel->plan.nEq + nExtraReg;
       
 88653   pParse->nMem += nReg;
       
 88654 
       
 88655   zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
       
 88656   if( !zAff ){
       
 88657     pParse->db->mallocFailed = 1;
       
 88658   }
       
 88659 
       
 88660   /* Evaluate the equality constraints
       
 88661   */
       
 88662   assert( pIdx->nColumn>=nEq );
       
 88663   for(j=0; j<nEq; j++){
       
 88664     int r1;
       
 88665     int k = pIdx->aiColumn[j];
       
 88666     pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
       
 88667     if( NEVER(pTerm==0) ) break;
       
 88668     assert( (pTerm->wtFlags & TERM_CODED)==0 );
       
 88669     r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
       
 88670     if( r1!=regBase+j ){
       
 88671       if( nReg==1 ){
       
 88672         sqlite3ReleaseTempReg(pParse, regBase);
       
 88673         regBase = r1;
       
 88674       }else{
       
 88675         sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
       
 88676       }
       
 88677     }
       
 88678     testcase( pTerm->eOperator & WO_ISNULL );
       
 88679     testcase( pTerm->eOperator & WO_IN );
       
 88680     if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
       
 88681       sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
       
 88682       if( zAff 
       
 88683        && sqlite3CompareAffinity(pTerm->pExpr->pRight, zAff[j])==SQLITE_AFF_NONE
       
 88684       ){
       
 88685         zAff[j] = SQLITE_AFF_NONE;
       
 88686       }
       
 88687     }
       
 88688   }
       
 88689   *pzAff = zAff;
       
 88690   return regBase;
       
 88691 }
       
 88692 
       
 88693 /*
       
 88694 ** Generate code for the start of the iLevel-th loop in the WHERE clause
       
 88695 ** implementation described by pWInfo.
       
 88696 */
       
 88697 static Bitmask codeOneLoopStart(
       
 88698   WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
       
 88699   int iLevel,          /* Which level of pWInfo->a[] should be coded */
       
 88700   u16 wctrlFlags,      /* One of the WHERE_* flags defined in sqliteInt.h */
       
 88701   Bitmask notReady     /* Which tables are currently available */
       
 88702 ){
       
 88703   int j, k;            /* Loop counters */
       
 88704   int iCur;            /* The VDBE cursor for the table */
       
 88705   int addrNxt;         /* Where to jump to continue with the next IN case */
       
 88706   int omitTable;       /* True if we use the index only */
       
 88707   int bRev;            /* True if we need to scan in reverse order */
       
 88708   WhereLevel *pLevel;  /* The where level to be coded */
       
 88709   WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
       
 88710   WhereTerm *pTerm;               /* A WHERE clause term */
       
 88711   Parse *pParse;                  /* Parsing context */
       
 88712   Vdbe *v;                        /* The prepared stmt under constructions */
       
 88713   struct SrcList_item *pTabItem;  /* FROM clause term being coded */
       
 88714   int addrBrk;                    /* Jump here to break out of the loop */
       
 88715   int addrCont;                   /* Jump here to continue with next cycle */
       
 88716   int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
       
 88717   int iReleaseReg = 0;      /* Temp register to free before returning */
       
 88718 
       
 88719   pParse = pWInfo->pParse;
       
 88720   v = pParse->pVdbe;
       
 88721   pWC = pWInfo->pWC;
       
 88722   pLevel = &pWInfo->a[iLevel];
       
 88723   pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
       
 88724   iCur = pTabItem->iCursor;
       
 88725   bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
       
 88726   omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0 
       
 88727            && (wctrlFlags & WHERE_FORCE_TABLE)==0;
       
 88728 
       
 88729   /* Create labels for the "break" and "continue" instructions
       
 88730   ** for the current loop.  Jump to addrBrk to break out of a loop.
       
 88731   ** Jump to cont to go immediately to the next iteration of the
       
 88732   ** loop.
       
 88733   **
       
 88734   ** When there is an IN operator, we also have a "addrNxt" label that
       
 88735   ** means to continue with the next IN value combination.  When
       
 88736   ** there are no IN operators in the constraints, the "addrNxt" label
       
 88737   ** is the same as "addrBrk".
       
 88738   */
       
 88739   addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
       
 88740   addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
       
 88741 
       
 88742   /* If this is the right table of a LEFT OUTER JOIN, allocate and
       
 88743   ** initialize a memory cell that records if this table matches any
       
 88744   ** row of the left table of the join.
       
 88745   */
       
 88746   if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
       
 88747     pLevel->iLeftJoin = ++pParse->nMem;
       
 88748     sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
       
 88749     VdbeComment((v, "init LEFT JOIN no-match flag"));
       
 88750   }
       
 88751 
       
 88752 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 88753   if(  (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
       
 88754     /* Case 0:  The table is a virtual-table.  Use the VFilter and VNext
       
 88755     **          to access the data.
       
 88756     */
       
 88757     int iReg;   /* P3 Value for OP_VFilter */
       
 88758     sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
       
 88759     int nConstraint = pVtabIdx->nConstraint;
       
 88760     struct sqlite3_index_constraint_usage *aUsage =
       
 88761                                                 pVtabIdx->aConstraintUsage;
       
 88762     const struct sqlite3_index_constraint *aConstraint =
       
 88763                                                 pVtabIdx->aConstraint;
       
 88764 
       
 88765     iReg = sqlite3GetTempRange(pParse, nConstraint+2);
       
 88766     for(j=1; j<=nConstraint; j++){
       
 88767       for(k=0; k<nConstraint; k++){
       
 88768         if( aUsage[k].argvIndex==j ){
       
 88769           int iTerm = aConstraint[k].iTermOffset;
       
 88770           sqlite3ExprCode(pParse, pWC->a[iTerm].pExpr->pRight, iReg+j+1);
       
 88771           break;
       
 88772         }
       
 88773       }
       
 88774       if( k==nConstraint ) break;
       
 88775     }
       
 88776     sqlite3VdbeAddOp2(v, OP_Integer, pVtabIdx->idxNum, iReg);
       
 88777     sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1);
       
 88778     sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrBrk, iReg, pVtabIdx->idxStr,
       
 88779                       pVtabIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC);
       
 88780     pVtabIdx->needToFreeIdxStr = 0;
       
 88781     for(j=0; j<nConstraint; j++){
       
 88782       if( aUsage[j].omit ){
       
 88783         int iTerm = aConstraint[j].iTermOffset;
       
 88784         disableTerm(pLevel, &pWC->a[iTerm]);
       
 88785       }
       
 88786     }
       
 88787     pLevel->op = OP_VNext;
       
 88788     pLevel->p1 = iCur;
       
 88789     pLevel->p2 = sqlite3VdbeCurrentAddr(v);
       
 88790     sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
       
 88791   }else
       
 88792 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
 88793 
       
 88794   if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){
       
 88795     /* Case 1:  We can directly reference a single row using an
       
 88796     **          equality comparison against the ROWID field.  Or
       
 88797     **          we reference multiple rows using a "rowid IN (...)"
       
 88798     **          construct.
       
 88799     */
       
 88800     iReleaseReg = sqlite3GetTempReg(pParse);
       
 88801     pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
       
 88802     assert( pTerm!=0 );
       
 88803     assert( pTerm->pExpr!=0 );
       
 88804     assert( pTerm->leftCursor==iCur );
       
 88805     assert( omitTable==0 );
       
 88806     iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iReleaseReg);
       
 88807     addrNxt = pLevel->addrNxt;
       
 88808     sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
       
 88809     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
       
 88810     sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
       
 88811     VdbeComment((v, "pk"));
       
 88812     pLevel->op = OP_Noop;
       
 88813   }else if( pLevel->plan.wsFlags & WHERE_ROWID_RANGE ){
       
 88814     /* Case 2:  We have an inequality comparison against the ROWID field.
       
 88815     */
       
 88816     int testOp = OP_Noop;
       
 88817     int start;
       
 88818     int memEndValue = 0;
       
 88819     WhereTerm *pStart, *pEnd;
       
 88820 
       
 88821     assert( omitTable==0 );
       
 88822     pStart = findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0);
       
 88823     pEnd = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0);
       
 88824     if( bRev ){
       
 88825       pTerm = pStart;
       
 88826       pStart = pEnd;
       
 88827       pEnd = pTerm;
       
 88828     }
       
 88829     if( pStart ){
       
 88830       Expr *pX;             /* The expression that defines the start bound */
       
 88831       int r1, rTemp;        /* Registers for holding the start boundary */
       
 88832 
       
 88833       /* The following constant maps TK_xx codes into corresponding 
       
 88834       ** seek opcodes.  It depends on a particular ordering of TK_xx
       
 88835       */
       
 88836       const u8 aMoveOp[] = {
       
 88837            /* TK_GT */  OP_SeekGt,
       
 88838            /* TK_LE */  OP_SeekLe,
       
 88839            /* TK_LT */  OP_SeekLt,
       
 88840            /* TK_GE */  OP_SeekGe
       
 88841       };
       
 88842       assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
       
 88843       assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
       
 88844       assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
       
 88845 
       
 88846       pX = pStart->pExpr;
       
 88847       assert( pX!=0 );
       
 88848       assert( pStart->leftCursor==iCur );
       
 88849       r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
       
 88850       sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
       
 88851       VdbeComment((v, "pk"));
       
 88852       sqlite3ExprCacheAffinityChange(pParse, r1, 1);
       
 88853       sqlite3ReleaseTempReg(pParse, rTemp);
       
 88854       disableTerm(pLevel, pStart);
       
 88855     }else{
       
 88856       sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
       
 88857     }
       
 88858     if( pEnd ){
       
 88859       Expr *pX;
       
 88860       pX = pEnd->pExpr;
       
 88861       assert( pX!=0 );
       
 88862       assert( pEnd->leftCursor==iCur );
       
 88863       memEndValue = ++pParse->nMem;
       
 88864       sqlite3ExprCode(pParse, pX->pRight, memEndValue);
       
 88865       if( pX->op==TK_LT || pX->op==TK_GT ){
       
 88866         testOp = bRev ? OP_Le : OP_Ge;
       
 88867       }else{
       
 88868         testOp = bRev ? OP_Lt : OP_Gt;
       
 88869       }
       
 88870       disableTerm(pLevel, pEnd);
       
 88871     }
       
 88872     start = sqlite3VdbeCurrentAddr(v);
       
 88873     pLevel->op = bRev ? OP_Prev : OP_Next;
       
 88874     pLevel->p1 = iCur;
       
 88875     pLevel->p2 = start;
       
 88876     pLevel->p5 = (pStart==0 && pEnd==0) ?1:0;
       
 88877     if( testOp!=OP_Noop ){
       
 88878       iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
       
 88879       sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
       
 88880       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
       
 88881       sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
       
 88882       sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
       
 88883     }
       
 88884   }else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){
       
 88885     /* Case 3: A scan using an index.
       
 88886     **
       
 88887     **         The WHERE clause may contain zero or more equality 
       
 88888     **         terms ("==" or "IN" operators) that refer to the N
       
 88889     **         left-most columns of the index. It may also contain
       
 88890     **         inequality constraints (>, <, >= or <=) on the indexed
       
 88891     **         column that immediately follows the N equalities. Only 
       
 88892     **         the right-most column can be an inequality - the rest must
       
 88893     **         use the "==" and "IN" operators. For example, if the 
       
 88894     **         index is on (x,y,z), then the following clauses are all 
       
 88895     **         optimized:
       
 88896     **
       
 88897     **            x=5
       
 88898     **            x=5 AND y=10
       
 88899     **            x=5 AND y<10
       
 88900     **            x=5 AND y>5 AND y<10
       
 88901     **            x=5 AND y=5 AND z<=10
       
 88902     **
       
 88903     **         The z<10 term of the following cannot be used, only
       
 88904     **         the x=5 term:
       
 88905     **
       
 88906     **            x=5 AND z<10
       
 88907     **
       
 88908     **         N may be zero if there are inequality constraints.
       
 88909     **         If there are no inequality constraints, then N is at
       
 88910     **         least one.
       
 88911     **
       
 88912     **         This case is also used when there are no WHERE clause
       
 88913     **         constraints but an index is selected anyway, in order
       
 88914     **         to force the output order to conform to an ORDER BY.
       
 88915     */  
       
 88916     int aStartOp[] = {
       
 88917       0,
       
 88918       0,
       
 88919       OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
       
 88920       OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
       
 88921       OP_SeekGt,           /* 4: (start_constraints  && !startEq && !bRev) */
       
 88922       OP_SeekLt,           /* 5: (start_constraints  && !startEq &&  bRev) */
       
 88923       OP_SeekGe,           /* 6: (start_constraints  &&  startEq && !bRev) */
       
 88924       OP_SeekLe            /* 7: (start_constraints  &&  startEq &&  bRev) */
       
 88925     };
       
 88926     int aEndOp[] = {
       
 88927       OP_Noop,             /* 0: (!end_constraints) */
       
 88928       OP_IdxGE,            /* 1: (end_constraints && !bRev) */
       
 88929       OP_IdxLT             /* 2: (end_constraints && bRev) */
       
 88930     };
       
 88931     int nEq = pLevel->plan.nEq;
       
 88932     int isMinQuery = 0;          /* If this is an optimized SELECT min(x).. */
       
 88933     int regBase;                 /* Base register holding constraint values */
       
 88934     int r1;                      /* Temp register */
       
 88935     WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
       
 88936     WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
       
 88937     int startEq;                 /* True if range start uses ==, >= or <= */
       
 88938     int endEq;                   /* True if range end uses ==, >= or <= */
       
 88939     int start_constraints;       /* Start of range is constrained */
       
 88940     int nConstraint;             /* Number of constraint terms */
       
 88941     Index *pIdx;         /* The index we will be using */
       
 88942     int iIdxCur;         /* The VDBE cursor for the index */
       
 88943     int nExtraReg = 0;   /* Number of extra registers needed */
       
 88944     int op;              /* Instruction opcode */
       
 88945     char *zAff;
       
 88946 
       
 88947     pIdx = pLevel->plan.u.pIdx;
       
 88948     iIdxCur = pLevel->iIdxCur;
       
 88949     k = pIdx->aiColumn[nEq];     /* Column for inequality constraints */
       
 88950 
       
 88951     /* If this loop satisfies a sort order (pOrderBy) request that 
       
 88952     ** was passed to this function to implement a "SELECT min(x) ..." 
       
 88953     ** query, then the caller will only allow the loop to run for
       
 88954     ** a single iteration. This means that the first row returned
       
 88955     ** should not have a NULL value stored in 'x'. If column 'x' is
       
 88956     ** the first one after the nEq equality constraints in the index,
       
 88957     ** this requires some special handling.
       
 88958     */
       
 88959     if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
       
 88960      && (pLevel->plan.wsFlags&WHERE_ORDERBY)
       
 88961      && (pIdx->nColumn>nEq)
       
 88962     ){
       
 88963       /* assert( pOrderBy->nExpr==1 ); */
       
 88964       /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */
       
 88965       isMinQuery = 1;
       
 88966       nExtraReg = 1;
       
 88967     }
       
 88968 
       
 88969     /* Find any inequality constraint terms for the start and end 
       
 88970     ** of the range. 
       
 88971     */
       
 88972     if( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ){
       
 88973       pRangeEnd = findTerm(pWC, iCur, k, notReady, (WO_LT|WO_LE), pIdx);
       
 88974       nExtraReg = 1;
       
 88975     }
       
 88976     if( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ){
       
 88977       pRangeStart = findTerm(pWC, iCur, k, notReady, (WO_GT|WO_GE), pIdx);
       
 88978       nExtraReg = 1;
       
 88979     }
       
 88980 
       
 88981     /* Generate code to evaluate all constraint terms using == or IN
       
 88982     ** and store the values of those terms in an array of registers
       
 88983     ** starting at regBase.
       
 88984     */
       
 88985     regBase = codeAllEqualityTerms(
       
 88986         pParse, pLevel, pWC, notReady, nExtraReg, &zAff
       
 88987     );
       
 88988     addrNxt = pLevel->addrNxt;
       
 88989 
       
 88990     /* If we are doing a reverse order scan on an ascending index, or
       
 88991     ** a forward order scan on a descending index, interchange the 
       
 88992     ** start and end terms (pRangeStart and pRangeEnd).
       
 88993     */
       
 88994     if( bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC) ){
       
 88995       SWAP(WhereTerm *, pRangeEnd, pRangeStart);
       
 88996     }
       
 88997 
       
 88998     testcase( pRangeStart && pRangeStart->eOperator & WO_LE );
       
 88999     testcase( pRangeStart && pRangeStart->eOperator & WO_GE );
       
 89000     testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE );
       
 89001     testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE );
       
 89002     startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
       
 89003     endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
       
 89004     start_constraints = pRangeStart || nEq>0;
       
 89005 
       
 89006     /* Seek the index cursor to the start of the range. */
       
 89007     nConstraint = nEq;
       
 89008     if( pRangeStart ){
       
 89009       Expr *pRight = pRangeStart->pExpr->pRight;
       
 89010       sqlite3ExprCode(pParse, pRight, regBase+nEq);
       
 89011       sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
       
 89012       if( zAff 
       
 89013        && sqlite3CompareAffinity(pRight, zAff[nConstraint])==SQLITE_AFF_NONE
       
 89014       ){
       
 89015         /* Since the comparison is to be performed with no conversions applied
       
 89016         ** to the operands, set the affinity to apply to pRight to 
       
 89017         ** SQLITE_AFF_NONE.  */
       
 89018         zAff[nConstraint] = SQLITE_AFF_NONE;
       
 89019       }
       
 89020       nConstraint++;
       
 89021     }else if( isMinQuery ){
       
 89022       sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
       
 89023       nConstraint++;
       
 89024       startEq = 0;
       
 89025       start_constraints = 1;
       
 89026     }
       
 89027     codeApplyAffinity(pParse, regBase, nConstraint, zAff);
       
 89028     op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
       
 89029     assert( op!=0 );
       
 89030     testcase( op==OP_Rewind );
       
 89031     testcase( op==OP_Last );
       
 89032     testcase( op==OP_SeekGt );
       
 89033     testcase( op==OP_SeekGe );
       
 89034     testcase( op==OP_SeekLe );
       
 89035     testcase( op==OP_SeekLt );
       
 89036     sqlite3VdbeAddOp4(v, op, iIdxCur, addrNxt, regBase, 
       
 89037                       SQLITE_INT_TO_PTR(nConstraint), P4_INT32);
       
 89038 
       
 89039     /* Load the value for the inequality constraint at the end of the
       
 89040     ** range (if any).
       
 89041     */
       
 89042     nConstraint = nEq;
       
 89043     if( pRangeEnd ){
       
 89044       Expr *pRight = pRangeEnd->pExpr->pRight;
       
 89045       sqlite3ExprCacheRemove(pParse, regBase+nEq);
       
 89046       sqlite3ExprCode(pParse, pRight, regBase+nEq);
       
 89047       sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
       
 89048       zAff = sqlite3DbStrDup(pParse->db, zAff);
       
 89049       if( zAff 
       
 89050        && sqlite3CompareAffinity(pRight, zAff[nConstraint])==SQLITE_AFF_NONE
       
 89051       ){
       
 89052         /* Since the comparison is to be performed with no conversions applied
       
 89053         ** to the operands, set the affinity to apply to pRight to 
       
 89054         ** SQLITE_AFF_NONE.  */
       
 89055         zAff[nConstraint] = SQLITE_AFF_NONE;
       
 89056       }
       
 89057       codeApplyAffinity(pParse, regBase, nEq+1, zAff);
       
 89058       nConstraint++;
       
 89059     }
       
 89060 
       
 89061     /* Top of the loop body */
       
 89062     pLevel->p2 = sqlite3VdbeCurrentAddr(v);
       
 89063 
       
 89064     /* Check if the index cursor is past the end of the range. */
       
 89065     op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)];
       
 89066     testcase( op==OP_Noop );
       
 89067     testcase( op==OP_IdxGE );
       
 89068     testcase( op==OP_IdxLT );
       
 89069     if( op!=OP_Noop ){
       
 89070       sqlite3VdbeAddOp4(v, op, iIdxCur, addrNxt, regBase,
       
 89071                         SQLITE_INT_TO_PTR(nConstraint), P4_INT32);
       
 89072       sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0);
       
 89073     }
       
 89074 
       
 89075     /* If there are inequality constraints, check that the value
       
 89076     ** of the table column that the inequality contrains is not NULL.
       
 89077     ** If it is, jump to the next iteration of the loop.
       
 89078     */
       
 89079     r1 = sqlite3GetTempReg(pParse);
       
 89080     testcase( pLevel->plan.wsFlags & WHERE_BTM_LIMIT );
       
 89081     testcase( pLevel->plan.wsFlags & WHERE_TOP_LIMIT );
       
 89082     if( pLevel->plan.wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT) ){
       
 89083       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
       
 89084       sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
       
 89085     }
       
 89086     sqlite3ReleaseTempReg(pParse, r1);
       
 89087 
       
 89088     /* Seek the table cursor, if required */
       
 89089     disableTerm(pLevel, pRangeStart);
       
 89090     disableTerm(pLevel, pRangeEnd);
       
 89091     if( !omitTable ){
       
 89092       iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
       
 89093       sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
       
 89094       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
       
 89095       sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
       
 89096     }
       
 89097 
       
 89098     /* Record the instruction used to terminate the loop. Disable 
       
 89099     ** WHERE clause terms made redundant by the index range scan.
       
 89100     */
       
 89101     pLevel->op = bRev ? OP_Prev : OP_Next;
       
 89102     pLevel->p1 = iIdxCur;
       
 89103   }else
       
 89104 
       
 89105 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
       
 89106   if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
       
 89107     /* Case 4:  Two or more separately indexed terms connected by OR
       
 89108     **
       
 89109     ** Example:
       
 89110     **
       
 89111     **   CREATE TABLE t1(a,b,c,d);
       
 89112     **   CREATE INDEX i1 ON t1(a);
       
 89113     **   CREATE INDEX i2 ON t1(b);
       
 89114     **   CREATE INDEX i3 ON t1(c);
       
 89115     **
       
 89116     **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
       
 89117     **
       
 89118     ** In the example, there are three indexed terms connected by OR.
       
 89119     ** The top of the loop looks like this:
       
 89120     **
       
 89121     **          Null       1                # Zero the rowset in reg 1
       
 89122     **
       
 89123     ** Then, for each indexed term, the following. The arguments to
       
 89124     ** RowSetTest are such that the rowid of the current row is inserted
       
 89125     ** into the RowSet. If it is already present, control skips the
       
 89126     ** Gosub opcode and jumps straight to the code generated by WhereEnd().
       
 89127     **
       
 89128     **        sqlite3WhereBegin(<term>)
       
 89129     **          RowSetTest                  # Insert rowid into rowset
       
 89130     **          Gosub      2 A
       
 89131     **        sqlite3WhereEnd()
       
 89132     **
       
 89133     ** Following the above, code to terminate the loop. Label A, the target
       
 89134     ** of the Gosub above, jumps to the instruction right after the Goto.
       
 89135     **
       
 89136     **          Null       1                # Zero the rowset in reg 1
       
 89137     **          Goto       B                # The loop is finished.
       
 89138     **
       
 89139     **       A: <loop body>                 # Return data, whatever.
       
 89140     **
       
 89141     **          Return     2                # Jump back to the Gosub
       
 89142     **
       
 89143     **       B: <after the loop>
       
 89144     **
       
 89145     */
       
 89146     WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
       
 89147     WhereTerm *pFinal;     /* Final subterm within the OR-clause. */
       
 89148     SrcList oneTab;        /* Shortened table list */
       
 89149 
       
 89150     int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
       
 89151     int regRowset = 0;                        /* Register for RowSet object */
       
 89152     int regRowid = 0;                         /* Register holding rowid */
       
 89153     int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
       
 89154     int iRetInit;                             /* Address of regReturn init */
       
 89155     int ii;
       
 89156    
       
 89157     pTerm = pLevel->plan.u.pTerm;
       
 89158     assert( pTerm!=0 );
       
 89159     assert( pTerm->eOperator==WO_OR );
       
 89160     assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
       
 89161     pOrWc = &pTerm->u.pOrInfo->wc;
       
 89162     pFinal = &pOrWc->a[pOrWc->nTerm-1];
       
 89163 
       
 89164     /* Set up a SrcList containing just the table being scanned by this loop. */
       
 89165     oneTab.nSrc = 1;
       
 89166     oneTab.nAlloc = 1;
       
 89167     oneTab.a[0] = *pTabItem;
       
 89168 
       
 89169     /* Initialize the rowset register to contain NULL. An SQL NULL is 
       
 89170     ** equivalent to an empty rowset.
       
 89171     **
       
 89172     ** Also initialize regReturn to contain the address of the instruction 
       
 89173     ** immediately following the OP_Return at the bottom of the loop. This
       
 89174     ** is required in a few obscure LEFT JOIN cases where control jumps
       
 89175     ** over the top of the loop into the body of it. In this case the 
       
 89176     ** correct response for the end-of-loop code (the OP_Return) is to 
       
 89177     ** fall through to the next instruction, just as an OP_Next does if
       
 89178     ** called on an uninitialized cursor.
       
 89179     */
       
 89180     if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
       
 89181       regRowset = ++pParse->nMem;
       
 89182       regRowid = ++pParse->nMem;
       
 89183       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
       
 89184     }
       
 89185     iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
       
 89186 
       
 89187     for(ii=0; ii<pOrWc->nTerm; ii++){
       
 89188       WhereTerm *pOrTerm = &pOrWc->a[ii];
       
 89189       if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
       
 89190         WhereInfo *pSubWInfo;          /* Info for single OR-term scan */
       
 89191         /* Loop through table entries that match term pOrTerm. */
       
 89192         pSubWInfo = sqlite3WhereBegin(pParse, &oneTab, pOrTerm->pExpr, 0,
       
 89193                         WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE | WHERE_FORCE_TABLE);
       
 89194         if( pSubWInfo ){
       
 89195           if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
       
 89196             int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
       
 89197             int r;
       
 89198             r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, 
       
 89199                                          regRowid, 0);
       
 89200             sqlite3VdbeAddOp4(v, OP_RowSetTest, regRowset,
       
 89201                               sqlite3VdbeCurrentAddr(v)+2,
       
 89202                               r, SQLITE_INT_TO_PTR(iSet), P4_INT32);
       
 89203           }
       
 89204           sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
       
 89205 
       
 89206           /* Finish the loop through table entries that match term pOrTerm. */
       
 89207           sqlite3WhereEnd(pSubWInfo);
       
 89208         }
       
 89209       }
       
 89210     }
       
 89211     sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
       
 89212     /* sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); */
       
 89213     sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
       
 89214     sqlite3VdbeResolveLabel(v, iLoopBody);
       
 89215 
       
 89216     pLevel->op = OP_Return;
       
 89217     pLevel->p1 = regReturn;
       
 89218     disableTerm(pLevel, pTerm);
       
 89219   }else
       
 89220 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
       
 89221 
       
 89222   {
       
 89223     /* Case 5:  There is no usable index.  We must do a complete
       
 89224     **          scan of the entire table.
       
 89225     */
       
 89226     static const u8 aStep[] = { OP_Next, OP_Prev };
       
 89227     static const u8 aStart[] = { OP_Rewind, OP_Last };
       
 89228     assert( bRev==0 || bRev==1 );
       
 89229     assert( omitTable==0 );
       
 89230     pLevel->op = aStep[bRev];
       
 89231     pLevel->p1 = iCur;
       
 89232     pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
       
 89233     pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
       
 89234   }
       
 89235   notReady &= ~getMask(pWC->pMaskSet, iCur);
       
 89236 
       
 89237   /* Insert code to test every subexpression that can be completely
       
 89238   ** computed using the current set of tables.
       
 89239   */
       
 89240   k = 0;
       
 89241   for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
       
 89242     Expr *pE;
       
 89243     testcase( pTerm->wtFlags & TERM_VIRTUAL );
       
 89244     testcase( pTerm->wtFlags & TERM_CODED );
       
 89245     if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
       
 89246     if( (pTerm->prereqAll & notReady)!=0 ) continue;
       
 89247     pE = pTerm->pExpr;
       
 89248     assert( pE!=0 );
       
 89249     if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
       
 89250       continue;
       
 89251     }
       
 89252     sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
       
 89253     k = 1;
       
 89254     pTerm->wtFlags |= TERM_CODED;
       
 89255   }
       
 89256 
       
 89257   /* For a LEFT OUTER JOIN, generate code that will record the fact that
       
 89258   ** at least one row of the right table has matched the left table.  
       
 89259   */
       
 89260   if( pLevel->iLeftJoin ){
       
 89261     pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
       
 89262     sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
       
 89263     VdbeComment((v, "record LEFT JOIN hit"));
       
 89264     sqlite3ExprCacheClear(pParse);
       
 89265     for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
       
 89266       testcase( pTerm->wtFlags & TERM_VIRTUAL );
       
 89267       testcase( pTerm->wtFlags & TERM_CODED );
       
 89268       if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
       
 89269       if( (pTerm->prereqAll & notReady)!=0 ) continue;
       
 89270       assert( pTerm->pExpr );
       
 89271       sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
       
 89272       pTerm->wtFlags |= TERM_CODED;
       
 89273     }
       
 89274   }
       
 89275   sqlite3ReleaseTempReg(pParse, iReleaseReg);
       
 89276 
       
 89277   return notReady;
       
 89278 }
       
 89279 
       
 89280 #if defined(SQLITE_TEST)
       
 89281 /*
       
 89282 ** The following variable holds a text description of query plan generated
       
 89283 ** by the most recent call to sqlite3WhereBegin().  Each call to WhereBegin
       
 89284 ** overwrites the previous.  This information is used for testing and
       
 89285 ** analysis only.
       
 89286 */
       
 89287 SQLITE_API char sqlite3_query_plan[BMS*2*40];  /* Text of the join */
       
 89288 static int nQPlan = 0;              /* Next free slow in _query_plan[] */
       
 89289 
       
 89290 #endif /* SQLITE_TEST */
       
 89291 
       
 89292 
       
 89293 /*
       
 89294 ** Free a WhereInfo structure
       
 89295 */
       
 89296 static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
       
 89297   if( pWInfo ){
       
 89298     int i;
       
 89299     for(i=0; i<pWInfo->nLevel; i++){
       
 89300       sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
       
 89301       if( pInfo ){
       
 89302         /* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */
       
 89303         if( pInfo->needToFreeIdxStr ){
       
 89304           sqlite3_free(pInfo->idxStr);
       
 89305         }
       
 89306         sqlite3DbFree(db, pInfo);
       
 89307       }
       
 89308     }
       
 89309     whereClauseClear(pWInfo->pWC);
       
 89310     sqlite3DbFree(db, pWInfo);
       
 89311   }
       
 89312 }
       
 89313 
       
 89314 
       
 89315 /*
       
 89316 ** Generate the beginning of the loop used for WHERE clause processing.
       
 89317 ** The return value is a pointer to an opaque structure that contains
       
 89318 ** information needed to terminate the loop.  Later, the calling routine
       
 89319 ** should invoke sqlite3WhereEnd() with the return value of this function
       
 89320 ** in order to complete the WHERE clause processing.
       
 89321 **
       
 89322 ** If an error occurs, this routine returns NULL.
       
 89323 **
       
 89324 ** The basic idea is to do a nested loop, one loop for each table in
       
 89325 ** the FROM clause of a select.  (INSERT and UPDATE statements are the
       
 89326 ** same as a SELECT with only a single table in the FROM clause.)  For
       
 89327 ** example, if the SQL is this:
       
 89328 **
       
 89329 **       SELECT * FROM t1, t2, t3 WHERE ...;
       
 89330 **
       
 89331 ** Then the code generated is conceptually like the following:
       
 89332 **
       
 89333 **      foreach row1 in t1 do       \    Code generated
       
 89334 **        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
       
 89335 **          foreach row3 in t3 do   /
       
 89336 **            ...
       
 89337 **          end                     \    Code generated
       
 89338 **        end                        |-- by sqlite3WhereEnd()
       
 89339 **      end                         /
       
 89340 **
       
 89341 ** Note that the loops might not be nested in the order in which they
       
 89342 ** appear in the FROM clause if a different order is better able to make
       
 89343 ** use of indices.  Note also that when the IN operator appears in
       
 89344 ** the WHERE clause, it might result in additional nested loops for
       
 89345 ** scanning through all values on the right-hand side of the IN.
       
 89346 **
       
 89347 ** There are Btree cursors associated with each table.  t1 uses cursor
       
 89348 ** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
       
 89349 ** And so forth.  This routine generates code to open those VDBE cursors
       
 89350 ** and sqlite3WhereEnd() generates the code to close them.
       
 89351 **
       
 89352 ** The code that sqlite3WhereBegin() generates leaves the cursors named
       
 89353 ** in pTabList pointing at their appropriate entries.  The [...] code
       
 89354 ** can use OP_Column and OP_Rowid opcodes on these cursors to extract
       
 89355 ** data from the various tables of the loop.
       
 89356 **
       
 89357 ** If the WHERE clause is empty, the foreach loops must each scan their
       
 89358 ** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
       
 89359 ** the tables have indices and there are terms in the WHERE clause that
       
 89360 ** refer to those indices, a complete table scan can be avoided and the
       
 89361 ** code will run much faster.  Most of the work of this routine is checking
       
 89362 ** to see if there are indices that can be used to speed up the loop.
       
 89363 **
       
 89364 ** Terms of the WHERE clause are also used to limit which rows actually
       
 89365 ** make it to the "..." in the middle of the loop.  After each "foreach",
       
 89366 ** terms of the WHERE clause that use only terms in that loop and outer
       
 89367 ** loops are evaluated and if false a jump is made around all subsequent
       
 89368 ** inner loops (or around the "..." if the test occurs within the inner-
       
 89369 ** most loop)
       
 89370 **
       
 89371 ** OUTER JOINS
       
 89372 **
       
 89373 ** An outer join of tables t1 and t2 is conceptally coded as follows:
       
 89374 **
       
 89375 **    foreach row1 in t1 do
       
 89376 **      flag = 0
       
 89377 **      foreach row2 in t2 do
       
 89378 **        start:
       
 89379 **          ...
       
 89380 **          flag = 1
       
 89381 **      end
       
 89382 **      if flag==0 then
       
 89383 **        move the row2 cursor to a null row
       
 89384 **        goto start
       
 89385 **      fi
       
 89386 **    end
       
 89387 **
       
 89388 ** ORDER BY CLAUSE PROCESSING
       
 89389 **
       
 89390 ** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
       
 89391 ** if there is one.  If there is no ORDER BY clause or if this routine
       
 89392 ** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
       
 89393 **
       
 89394 ** If an index can be used so that the natural output order of the table
       
 89395 ** scan is correct for the ORDER BY clause, then that index is used and
       
 89396 ** *ppOrderBy is set to NULL.  This is an optimization that prevents an
       
 89397 ** unnecessary sort of the result set if an index appropriate for the
       
 89398 ** ORDER BY clause already exists.
       
 89399 **
       
 89400 ** If the where clause loops cannot be arranged to provide the correct
       
 89401 ** output order, then the *ppOrderBy is unchanged.
       
 89402 */
       
 89403 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
       
 89404   Parse *pParse,        /* The parser context */
       
 89405   SrcList *pTabList,    /* A list of all tables to be scanned */
       
 89406   Expr *pWhere,         /* The WHERE clause */
       
 89407   ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
       
 89408   u16 wctrlFlags        /* One of the WHERE_* flags defined in sqliteInt.h */
       
 89409 ){
       
 89410   int i;                     /* Loop counter */
       
 89411   int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
       
 89412   WhereInfo *pWInfo;         /* Will become the return value of this function */
       
 89413   Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
       
 89414   Bitmask notReady;          /* Cursors that are not yet positioned */
       
 89415   WhereMaskSet *pMaskSet;    /* The expression mask set */
       
 89416   WhereClause *pWC;               /* Decomposition of the WHERE clause */
       
 89417   struct SrcList_item *pTabItem;  /* A single entry from pTabList */
       
 89418   WhereLevel *pLevel;             /* A single level in the pWInfo list */
       
 89419   int iFrom;                      /* First unused FROM clause element */
       
 89420   int andFlags;              /* AND-ed combination of all pWC->a[].wtFlags */
       
 89421   sqlite3 *db;               /* Database connection */
       
 89422 
       
 89423   /* The number of tables in the FROM clause is limited by the number of
       
 89424   ** bits in a Bitmask 
       
 89425   */
       
 89426   if( pTabList->nSrc>BMS ){
       
 89427     sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
       
 89428     return 0;
       
 89429   }
       
 89430 
       
 89431   /* Allocate and initialize the WhereInfo structure that will become the
       
 89432   ** return value. A single allocation is used to store the WhereInfo
       
 89433   ** struct, the contents of WhereInfo.a[], the WhereClause structure
       
 89434   ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
       
 89435   ** field (type Bitmask) it must be aligned on an 8-byte boundary on
       
 89436   ** some architectures. Hence the ROUND8() below.
       
 89437   */
       
 89438   db = pParse->db;
       
 89439   nByteWInfo = ROUND8(sizeof(WhereInfo)+(pTabList->nSrc-1)*sizeof(WhereLevel));
       
 89440   pWInfo = sqlite3DbMallocZero(db, 
       
 89441       nByteWInfo + 
       
 89442       sizeof(WhereClause) +
       
 89443       sizeof(WhereMaskSet)
       
 89444   );
       
 89445   if( db->mallocFailed ){
       
 89446     goto whereBeginError;
       
 89447   }
       
 89448   pWInfo->nLevel = pTabList->nSrc;
       
 89449   pWInfo->pParse = pParse;
       
 89450   pWInfo->pTabList = pTabList;
       
 89451   pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
       
 89452   pWInfo->pWC = pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
       
 89453   pWInfo->wctrlFlags = wctrlFlags;
       
 89454   pMaskSet = (WhereMaskSet*)&pWC[1];
       
 89455 
       
 89456   /* Split the WHERE clause into separate subexpressions where each
       
 89457   ** subexpression is separated by an AND operator.
       
 89458   */
       
 89459   initMaskSet(pMaskSet);
       
 89460   whereClauseInit(pWC, pParse, pMaskSet);
       
 89461   sqlite3ExprCodeConstants(pParse, pWhere);
       
 89462   whereSplit(pWC, pWhere, TK_AND);
       
 89463     
       
 89464   /* Special case: a WHERE clause that is constant.  Evaluate the
       
 89465   ** expression and either jump over all of the code or fall thru.
       
 89466   */
       
 89467   if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
       
 89468     sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
       
 89469     pWhere = 0;
       
 89470   }
       
 89471 
       
 89472   /* Assign a bit from the bitmask to every term in the FROM clause.
       
 89473   **
       
 89474   ** When assigning bitmask values to FROM clause cursors, it must be
       
 89475   ** the case that if X is the bitmask for the N-th FROM clause term then
       
 89476   ** the bitmask for all FROM clause terms to the left of the N-th term
       
 89477   ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
       
 89478   ** its Expr.iRightJoinTable value to find the bitmask of the right table
       
 89479   ** of the join.  Subtracting one from the right table bitmask gives a
       
 89480   ** bitmask for all tables to the left of the join.  Knowing the bitmask
       
 89481   ** for all tables to the left of a left join is important.  Ticket #3015.
       
 89482   **
       
 89483   ** Configure the WhereClause.vmask variable so that bits that correspond
       
 89484   ** to virtual table cursors are set. This is used to selectively disable 
       
 89485   ** the OR-to-IN transformation in exprAnalyzeOrTerm(). It is not helpful 
       
 89486   ** with virtual tables.
       
 89487   */
       
 89488   assert( pWC->vmask==0 && pMaskSet->n==0 );
       
 89489   for(i=0; i<pTabList->nSrc; i++){
       
 89490     createMask(pMaskSet, pTabList->a[i].iCursor);
       
 89491 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 89492     if( ALWAYS(pTabList->a[i].pTab) && IsVirtual(pTabList->a[i].pTab) ){
       
 89493       pWC->vmask |= ((Bitmask)1 << i);
       
 89494     }
       
 89495 #endif
       
 89496   }
       
 89497 #ifndef NDEBUG
       
 89498   {
       
 89499     Bitmask toTheLeft = 0;
       
 89500     for(i=0; i<pTabList->nSrc; i++){
       
 89501       Bitmask m = getMask(pMaskSet, pTabList->a[i].iCursor);
       
 89502       assert( (m-1)==toTheLeft );
       
 89503       toTheLeft |= m;
       
 89504     }
       
 89505   }
       
 89506 #endif
       
 89507 
       
 89508   /* Analyze all of the subexpressions.  Note that exprAnalyze() might
       
 89509   ** add new virtual terms onto the end of the WHERE clause.  We do not
       
 89510   ** want to analyze these virtual terms, so start analyzing at the end
       
 89511   ** and work forward so that the added virtual terms are never processed.
       
 89512   */
       
 89513   exprAnalyzeAll(pTabList, pWC);
       
 89514   if( db->mallocFailed ){
       
 89515     goto whereBeginError;
       
 89516   }
       
 89517 
       
 89518   /* Chose the best index to use for each table in the FROM clause.
       
 89519   **
       
 89520   ** This loop fills in the following fields:
       
 89521   **
       
 89522   **   pWInfo->a[].pIdx      The index to use for this level of the loop.
       
 89523   **   pWInfo->a[].wsFlags   WHERE_xxx flags associated with pIdx
       
 89524   **   pWInfo->a[].nEq       The number of == and IN constraints
       
 89525   **   pWInfo->a[].iFrom     Which term of the FROM clause is being coded
       
 89526   **   pWInfo->a[].iTabCur   The VDBE cursor for the database table
       
 89527   **   pWInfo->a[].iIdxCur   The VDBE cursor for the index
       
 89528   **   pWInfo->a[].pTerm     When wsFlags==WO_OR, the OR-clause term
       
 89529   **
       
 89530   ** This loop also figures out the nesting order of tables in the FROM
       
 89531   ** clause.
       
 89532   */
       
 89533   notReady = ~(Bitmask)0;
       
 89534   pTabItem = pTabList->a;
       
 89535   pLevel = pWInfo->a;
       
 89536   andFlags = ~0;
       
 89537   WHERETRACE(("*** Optimizer Start ***\n"));
       
 89538   for(i=iFrom=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
       
 89539     WhereCost bestPlan;         /* Most efficient plan seen so far */
       
 89540     Index *pIdx;                /* Index for FROM table at pTabItem */
       
 89541     int j;                      /* For looping over FROM tables */
       
 89542     int bestJ = -1;             /* The value of j */
       
 89543     Bitmask m;                  /* Bitmask value for j or bestJ */
       
 89544     int isOptimal;              /* Iterator for optimal/non-optimal search */
       
 89545 
       
 89546     memset(&bestPlan, 0, sizeof(bestPlan));
       
 89547     bestPlan.rCost = SQLITE_BIG_DBL;
       
 89548 
       
 89549     /* Loop through the remaining entries in the FROM clause to find the
       
 89550     ** next nested loop. The FROM clause entries may be iterated through
       
 89551     ** either once or twice. 
       
 89552     **
       
 89553     ** The first iteration, which is always performed, searches for the
       
 89554     ** FROM clause entry that permits the lowest-cost, "optimal" scan. In
       
 89555     ** this context an optimal scan is one that uses the same strategy
       
 89556     ** for the given FROM clause entry as would be selected if the entry
       
 89557     ** were used as the innermost nested loop.  In other words, a table
       
 89558     ** is chosen such that the cost of running that table cannot be reduced
       
 89559     ** by waiting for other tables to run first.
       
 89560     **
       
 89561     ** The second iteration is only performed if no optimal scan strategies
       
 89562     ** were found by the first. This iteration is used to search for the
       
 89563     ** lowest cost scan overall.
       
 89564     **
       
 89565     ** Previous versions of SQLite performed only the second iteration -
       
 89566     ** the next outermost loop was always that with the lowest overall
       
 89567     ** cost. However, this meant that SQLite could select the wrong plan
       
 89568     ** for scripts such as the following:
       
 89569     **   
       
 89570     **   CREATE TABLE t1(a, b); 
       
 89571     **   CREATE TABLE t2(c, d);
       
 89572     **   SELECT * FROM t2, t1 WHERE t2.rowid = t1.a;
       
 89573     **
       
 89574     ** The best strategy is to iterate through table t1 first. However it
       
 89575     ** is not possible to determine this with a simple greedy algorithm.
       
 89576     ** However, since the cost of a linear scan through table t2 is the same 
       
 89577     ** as the cost of a linear scan through table t1, a simple greedy 
       
 89578     ** algorithm may choose to use t2 for the outer loop, which is a much
       
 89579     ** costlier approach.
       
 89580     */
       
 89581     for(isOptimal=1; isOptimal>=0 && bestJ<0; isOptimal--){
       
 89582       Bitmask mask = (isOptimal ? 0 : notReady);
       
 89583       assert( (pTabList->nSrc-iFrom)>1 || isOptimal );
       
 89584       for(j=iFrom, pTabItem=&pTabList->a[j]; j<pTabList->nSrc; j++, pTabItem++){
       
 89585         int doNotReorder;    /* True if this table should not be reordered */
       
 89586         WhereCost sCost;     /* Cost information from best[Virtual]Index() */
       
 89587         ExprList *pOrderBy;  /* ORDER BY clause for index to optimize */
       
 89588   
       
 89589         doNotReorder =  (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
       
 89590         if( j!=iFrom && doNotReorder ) break;
       
 89591         m = getMask(pMaskSet, pTabItem->iCursor);
       
 89592         if( (m & notReady)==0 ){
       
 89593           if( j==iFrom ) iFrom++;
       
 89594           continue;
       
 89595         }
       
 89596         pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
       
 89597   
       
 89598         assert( pTabItem->pTab );
       
 89599 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 89600         if( IsVirtual(pTabItem->pTab) ){
       
 89601           sqlite3_index_info **pp = &pWInfo->a[j].pIdxInfo;
       
 89602           bestVirtualIndex(pParse, pWC, pTabItem, mask, pOrderBy, &sCost, pp);
       
 89603         }else 
       
 89604 #endif
       
 89605         {
       
 89606           bestBtreeIndex(pParse, pWC, pTabItem, mask, pOrderBy, &sCost);
       
 89607         }
       
 89608         assert( isOptimal || (sCost.used&notReady)==0 );
       
 89609 
       
 89610         if( (sCost.used&notReady)==0
       
 89611          && (j==iFrom || sCost.rCost<bestPlan.rCost) 
       
 89612         ){
       
 89613           bestPlan = sCost;
       
 89614           bestJ = j;
       
 89615         }
       
 89616         if( doNotReorder ) break;
       
 89617       }
       
 89618     }
       
 89619     assert( bestJ>=0 );
       
 89620     assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
       
 89621     WHERETRACE(("*** Optimizer selects table %d for loop %d\n", bestJ,
       
 89622            pLevel-pWInfo->a));
       
 89623     if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){
       
 89624       *ppOrderBy = 0;
       
 89625     }
       
 89626     andFlags &= bestPlan.plan.wsFlags;
       
 89627     pLevel->plan = bestPlan.plan;
       
 89628     if( bestPlan.plan.wsFlags & WHERE_INDEXED ){
       
 89629       pLevel->iIdxCur = pParse->nTab++;
       
 89630     }else{
       
 89631       pLevel->iIdxCur = -1;
       
 89632     }
       
 89633     notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
       
 89634     pLevel->iFrom = (u8)bestJ;
       
 89635 
       
 89636     /* Check that if the table scanned by this loop iteration had an
       
 89637     ** INDEXED BY clause attached to it, that the named index is being
       
 89638     ** used for the scan. If not, then query compilation has failed.
       
 89639     ** Return an error.
       
 89640     */
       
 89641     pIdx = pTabList->a[bestJ].pIndex;
       
 89642     if( pIdx ){
       
 89643       if( (bestPlan.plan.wsFlags & WHERE_INDEXED)==0 ){
       
 89644         sqlite3ErrorMsg(pParse, "cannot use index: %s", pIdx->zName);
       
 89645         goto whereBeginError;
       
 89646       }else{
       
 89647         /* If an INDEXED BY clause is used, the bestIndex() function is
       
 89648         ** guaranteed to find the index specified in the INDEXED BY clause
       
 89649         ** if it find an index at all. */
       
 89650         assert( bestPlan.plan.u.pIdx==pIdx );
       
 89651       }
       
 89652     }
       
 89653   }
       
 89654   WHERETRACE(("*** Optimizer Finished ***\n"));
       
 89655   if( pParse->nErr || db->mallocFailed ){
       
 89656     goto whereBeginError;
       
 89657   }
       
 89658 
       
 89659   /* If the total query only selects a single row, then the ORDER BY
       
 89660   ** clause is irrelevant.
       
 89661   */
       
 89662   if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
       
 89663     *ppOrderBy = 0;
       
 89664   }
       
 89665 
       
 89666   /* If the caller is an UPDATE or DELETE statement that is requesting
       
 89667   ** to use a one-pass algorithm, determine if this is appropriate.
       
 89668   ** The one-pass algorithm only works if the WHERE clause constraints
       
 89669   ** the statement to update a single row.
       
 89670   */
       
 89671   assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
       
 89672   if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){
       
 89673     pWInfo->okOnePass = 1;
       
 89674     pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY;
       
 89675   }
       
 89676 
       
 89677   /* Open all tables in the pTabList and any indices selected for
       
 89678   ** searching those tables.
       
 89679   */
       
 89680   sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
       
 89681   for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
       
 89682     Table *pTab;     /* Table to open */
       
 89683     int iDb;         /* Index of database containing table/index */
       
 89684 
       
 89685 #ifndef SQLITE_OMIT_EXPLAIN
       
 89686     if( pParse->explain==2 ){
       
 89687       char *zMsg;
       
 89688       struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
       
 89689       zMsg = sqlite3MPrintf(db, "TABLE %s", pItem->zName);
       
 89690       if( pItem->zAlias ){
       
 89691         zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
       
 89692       }
       
 89693       if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
       
 89694         zMsg = sqlite3MAppendf(db, zMsg, "%s WITH INDEX %s",
       
 89695            zMsg, pLevel->plan.u.pIdx->zName);
       
 89696       }else if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
       
 89697         zMsg = sqlite3MAppendf(db, zMsg, "%s VIA MULTI-INDEX UNION", zMsg);
       
 89698       }else if( pLevel->plan.wsFlags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
       
 89699         zMsg = sqlite3MAppendf(db, zMsg, "%s USING PRIMARY KEY", zMsg);
       
 89700       }
       
 89701 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 89702       else if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
       
 89703         sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
       
 89704         zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg,
       
 89705                     pVtabIdx->idxNum, pVtabIdx->idxStr);
       
 89706       }
       
 89707 #endif
       
 89708       if( pLevel->plan.wsFlags & WHERE_ORDERBY ){
       
 89709         zMsg = sqlite3MAppendf(db, zMsg, "%s ORDER BY", zMsg);
       
 89710       }
       
 89711       sqlite3VdbeAddOp4(v, OP_Explain, i, pLevel->iFrom, 0, zMsg, P4_DYNAMIC);
       
 89712     }
       
 89713 #endif /* SQLITE_OMIT_EXPLAIN */
       
 89714     pTabItem = &pTabList->a[pLevel->iFrom];
       
 89715     pTab = pTabItem->pTab;
       
 89716     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       
 89717     if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ) continue;
       
 89718 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 89719     if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
       
 89720       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
       
 89721       int iCur = pTabItem->iCursor;
       
 89722       sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
       
 89723     }else
       
 89724 #endif
       
 89725     if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
       
 89726          && (wctrlFlags & WHERE_OMIT_OPEN)==0 ){
       
 89727       int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
       
 89728       sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
       
 89729       if( !pWInfo->okOnePass && pTab->nCol<BMS ){
       
 89730         Bitmask b = pTabItem->colUsed;
       
 89731         int n = 0;
       
 89732         for(; b; b=b>>1, n++){}
       
 89733         sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, SQLITE_INT_TO_PTR(n), P4_INT32);
       
 89734         assert( n<=pTab->nCol );
       
 89735       }
       
 89736     }else{
       
 89737       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
       
 89738     }
       
 89739     pLevel->iTabCur = pTabItem->iCursor;
       
 89740     if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
       
 89741       Index *pIx = pLevel->plan.u.pIdx;
       
 89742       KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
       
 89743       int iIdxCur = pLevel->iIdxCur;
       
 89744       assert( pIx->pSchema==pTab->pSchema );
       
 89745       assert( iIdxCur>=0 );
       
 89746       sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIx->tnum, iDb,
       
 89747                         (char*)pKey, P4_KEYINFO_HANDOFF);
       
 89748       VdbeComment((v, "%s", pIx->zName));
       
 89749     }
       
 89750     sqlite3CodeVerifySchema(pParse, iDb);
       
 89751   }
       
 89752   pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
       
 89753 
       
 89754   /* Generate the code to do the search.  Each iteration of the for
       
 89755   ** loop below generates code for a single nested loop of the VM
       
 89756   ** program.
       
 89757   */
       
 89758   notReady = ~(Bitmask)0;
       
 89759   for(i=0; i<pTabList->nSrc; i++){
       
 89760     notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady);
       
 89761     pWInfo->iContinue = pWInfo->a[i].addrCont;
       
 89762   }
       
 89763 
       
 89764 #ifdef SQLITE_TEST  /* For testing and debugging use only */
       
 89765   /* Record in the query plan information about the current table
       
 89766   ** and the index used to access it (if any).  If the table itself
       
 89767   ** is not used, its name is just '{}'.  If no index is used
       
 89768   ** the index is listed as "{}".  If the primary key is used the
       
 89769   ** index name is '*'.
       
 89770   */
       
 89771   for(i=0; i<pTabList->nSrc; i++){
       
 89772     char *z;
       
 89773     int n;
       
 89774     pLevel = &pWInfo->a[i];
       
 89775     pTabItem = &pTabList->a[pLevel->iFrom];
       
 89776     z = pTabItem->zAlias;
       
 89777     if( z==0 ) z = pTabItem->pTab->zName;
       
 89778     n = sqlite3Strlen30(z);
       
 89779     if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
       
 89780       if( pLevel->plan.wsFlags & WHERE_IDX_ONLY ){
       
 89781         memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
       
 89782         nQPlan += 2;
       
 89783       }else{
       
 89784         memcpy(&sqlite3_query_plan[nQPlan], z, n);
       
 89785         nQPlan += n;
       
 89786       }
       
 89787       sqlite3_query_plan[nQPlan++] = ' ';
       
 89788     }
       
 89789     testcase( pLevel->plan.wsFlags & WHERE_ROWID_EQ );
       
 89790     testcase( pLevel->plan.wsFlags & WHERE_ROWID_RANGE );
       
 89791     if( pLevel->plan.wsFlags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
       
 89792       memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
       
 89793       nQPlan += 2;
       
 89794     }else if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
       
 89795       n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName);
       
 89796       if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
       
 89797         memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n);
       
 89798         nQPlan += n;
       
 89799         sqlite3_query_plan[nQPlan++] = ' ';
       
 89800       }
       
 89801     }else{
       
 89802       memcpy(&sqlite3_query_plan[nQPlan], "{} ", 3);
       
 89803       nQPlan += 3;
       
 89804     }
       
 89805   }
       
 89806   while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
       
 89807     sqlite3_query_plan[--nQPlan] = 0;
       
 89808   }
       
 89809   sqlite3_query_plan[nQPlan] = 0;
       
 89810   nQPlan = 0;
       
 89811 #endif /* SQLITE_TEST // Testing and debugging use only */
       
 89812 
       
 89813   /* Record the continuation address in the WhereInfo structure.  Then
       
 89814   ** clean up and return.
       
 89815   */
       
 89816   return pWInfo;
       
 89817 
       
 89818   /* Jump here if malloc fails */
       
 89819 whereBeginError:
       
 89820   whereInfoFree(db, pWInfo);
       
 89821   return 0;
       
 89822 }
       
 89823 
       
 89824 /*
       
 89825 ** Generate the end of the WHERE loop.  See comments on 
       
 89826 ** sqlite3WhereBegin() for additional information.
       
 89827 */
       
 89828 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
       
 89829   Parse *pParse = pWInfo->pParse;
       
 89830   Vdbe *v = pParse->pVdbe;
       
 89831   int i;
       
 89832   WhereLevel *pLevel;
       
 89833   SrcList *pTabList = pWInfo->pTabList;
       
 89834   sqlite3 *db = pParse->db;
       
 89835 
       
 89836   /* Generate loop termination code.
       
 89837   */
       
 89838   sqlite3ExprCacheClear(pParse);
       
 89839   for(i=pTabList->nSrc-1; i>=0; i--){
       
 89840     pLevel = &pWInfo->a[i];
       
 89841     sqlite3VdbeResolveLabel(v, pLevel->addrCont);
       
 89842     if( pLevel->op!=OP_Noop ){
       
 89843       sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
       
 89844       sqlite3VdbeChangeP5(v, pLevel->p5);
       
 89845     }
       
 89846     if( pLevel->plan.wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
       
 89847       struct InLoop *pIn;
       
 89848       int j;
       
 89849       sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
       
 89850       for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
       
 89851         sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
       
 89852         sqlite3VdbeAddOp2(v, OP_Next, pIn->iCur, pIn->addrInTop);
       
 89853         sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
       
 89854       }
       
 89855       sqlite3DbFree(db, pLevel->u.in.aInLoop);
       
 89856     }
       
 89857     sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
       
 89858     if( pLevel->iLeftJoin ){
       
 89859       int addr;
       
 89860       addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
       
 89861       sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
       
 89862       if( pLevel->iIdxCur>=0 ){
       
 89863         sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
       
 89864       }
       
 89865       if( pLevel->op==OP_Return ){
       
 89866         sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
       
 89867       }else{
       
 89868         sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
       
 89869       }
       
 89870       sqlite3VdbeJumpHere(v, addr);
       
 89871     }
       
 89872   }
       
 89873 
       
 89874   /* The "break" point is here, just past the end of the outer loop.
       
 89875   ** Set it.
       
 89876   */
       
 89877   sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
       
 89878 
       
 89879   /* Close all of the cursors that were opened by sqlite3WhereBegin.
       
 89880   */
       
 89881   for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
       
 89882     struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
       
 89883     Table *pTab = pTabItem->pTab;
       
 89884     assert( pTab!=0 );
       
 89885     if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ) continue;
       
 89886     if( (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0 ){
       
 89887       if( !pWInfo->okOnePass && (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 ){
       
 89888         sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
       
 89889       }
       
 89890       if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
       
 89891         sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
       
 89892       }
       
 89893     }
       
 89894 
       
 89895     /* If this scan uses an index, make code substitutions to read data
       
 89896     ** from the index in preference to the table. Sometimes, this means
       
 89897     ** the table need never be read from. This is a performance boost,
       
 89898     ** as the vdbe level waits until the table is read before actually
       
 89899     ** seeking the table cursor to the record corresponding to the current
       
 89900     ** position in the index.
       
 89901     ** 
       
 89902     ** Calls to the code generator in between sqlite3WhereBegin and
       
 89903     ** sqlite3WhereEnd will have created code that references the table
       
 89904     ** directly.  This loop scans all that code looking for opcodes
       
 89905     ** that reference the table and converts them into opcodes that
       
 89906     ** reference the index.
       
 89907     */
       
 89908     if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 && !db->mallocFailed){
       
 89909       int k, j, last;
       
 89910       VdbeOp *pOp;
       
 89911       Index *pIdx = pLevel->plan.u.pIdx;
       
 89912       int useIndexOnly = pLevel->plan.wsFlags & WHERE_IDX_ONLY;
       
 89913 
       
 89914       assert( pIdx!=0 );
       
 89915       pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
       
 89916       last = sqlite3VdbeCurrentAddr(v);
       
 89917       for(k=pWInfo->iTop; k<last; k++, pOp++){
       
 89918         if( pOp->p1!=pLevel->iTabCur ) continue;
       
 89919         if( pOp->opcode==OP_Column ){
       
 89920           for(j=0; j<pIdx->nColumn; j++){
       
 89921             if( pOp->p2==pIdx->aiColumn[j] ){
       
 89922               pOp->p2 = j;
       
 89923               pOp->p1 = pLevel->iIdxCur;
       
 89924               break;
       
 89925             }
       
 89926           }
       
 89927           assert(!useIndexOnly || j<pIdx->nColumn);
       
 89928         }else if( pOp->opcode==OP_Rowid ){
       
 89929           pOp->p1 = pLevel->iIdxCur;
       
 89930           pOp->opcode = OP_IdxRowid;
       
 89931         }else if( pOp->opcode==OP_NullRow && useIndexOnly ){
       
 89932           pOp->opcode = OP_Noop;
       
 89933         }
       
 89934       }
       
 89935     }
       
 89936   }
       
 89937 
       
 89938   /* Final cleanup
       
 89939   */
       
 89940   whereInfoFree(db, pWInfo);
       
 89941   return;
       
 89942 }
       
 89943 
       
 89944 /************** End of where.c ***********************************************/
       
 89945 /************** Begin file parse.c *******************************************/
       
 89946 /* Driver template for the LEMON parser generator.
       
 89947 ** The author disclaims copyright to this source code.
       
 89948 **
       
 89949 ** This version of "lempar.c" is modified, slightly, for use by SQLite.
       
 89950 ** The only modifications are the addition of a couple of NEVER()
       
 89951 ** macros to disable tests that are needed in the case of a general
       
 89952 ** LALR(1) grammar but which are always false in the
       
 89953 ** specific grammar used by SQLite.
       
 89954 */
       
 89955 /* First off, code is included that follows the "include" declaration
       
 89956 ** in the input grammar file. */
       
 89957 
       
 89958 
       
 89959 /*
       
 89960 ** Disable all error recovery processing in the parser push-down
       
 89961 ** automaton.
       
 89962 */
       
 89963 #define YYNOERRORRECOVERY 1
       
 89964 
       
 89965 /*
       
 89966 ** Make yytestcase() the same as testcase()
       
 89967 */
       
 89968 #define yytestcase(X) testcase(X)
       
 89969 
       
 89970 /*
       
 89971 ** An instance of this structure holds information about the
       
 89972 ** LIMIT clause of a SELECT statement.
       
 89973 */
       
 89974 struct LimitVal {
       
 89975   Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
       
 89976   Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
       
 89977 };
       
 89978 
       
 89979 /*
       
 89980 ** An instance of this structure is used to store the LIKE,
       
 89981 ** GLOB, NOT LIKE, and NOT GLOB operators.
       
 89982 */
       
 89983 struct LikeOp {
       
 89984   Token eOperator;  /* "like" or "glob" or "regexp" */
       
 89985   int not;         /* True if the NOT keyword is present */
       
 89986 };
       
 89987 
       
 89988 /*
       
 89989 ** An instance of the following structure describes the event of a
       
 89990 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
       
 89991 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
       
 89992 **
       
 89993 **      UPDATE ON (a,b,c)
       
 89994 **
       
 89995 ** Then the "b" IdList records the list "a,b,c".
       
 89996 */
       
 89997 struct TrigEvent { int a; IdList * b; };
       
 89998 
       
 89999 /*
       
 90000 ** An instance of this structure holds the ATTACH key and the key type.
       
 90001 */
       
 90002 struct AttachKey { int type;  Token key; };
       
 90003 
       
 90004 
       
 90005   /* This is a utility routine used to set the ExprSpan.zStart and
       
 90006   ** ExprSpan.zEnd values of pOut so that the span covers the complete
       
 90007   ** range of text beginning with pStart and going to the end of pEnd.
       
 90008   */
       
 90009   static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
       
 90010     pOut->zStart = pStart->z;
       
 90011     pOut->zEnd = &pEnd->z[pEnd->n];
       
 90012   }
       
 90013 
       
 90014   /* Construct a new Expr object from a single identifier.  Use the
       
 90015   ** new Expr to populate pOut.  Set the span of pOut to be the identifier
       
 90016   ** that created the expression.
       
 90017   */
       
 90018   static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
       
 90019     pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
       
 90020     pOut->zStart = pValue->z;
       
 90021     pOut->zEnd = &pValue->z[pValue->n];
       
 90022   }
       
 90023 
       
 90024   /* This routine constructs a binary expression node out of two ExprSpan
       
 90025   ** objects and uses the result to populate a new ExprSpan object.
       
 90026   */
       
 90027   static void spanBinaryExpr(
       
 90028     ExprSpan *pOut,     /* Write the result here */
       
 90029     Parse *pParse,      /* The parsing context.  Errors accumulate here */
       
 90030     int op,             /* The binary operation */
       
 90031     ExprSpan *pLeft,    /* The left operand */
       
 90032     ExprSpan *pRight    /* The right operand */
       
 90033   ){
       
 90034     pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
       
 90035     pOut->zStart = pLeft->zStart;
       
 90036     pOut->zEnd = pRight->zEnd;
       
 90037   }
       
 90038 
       
 90039   /* Construct an expression node for a unary postfix operator
       
 90040   */
       
 90041   static void spanUnaryPostfix(
       
 90042     ExprSpan *pOut,        /* Write the new expression node here */
       
 90043     Parse *pParse,         /* Parsing context to record errors */
       
 90044     int op,                /* The operator */
       
 90045     ExprSpan *pOperand,    /* The operand */
       
 90046     Token *pPostOp         /* The operand token for setting the span */
       
 90047   ){
       
 90048     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
       
 90049     pOut->zStart = pOperand->zStart;
       
 90050     pOut->zEnd = &pPostOp->z[pPostOp->n];
       
 90051   }                           
       
 90052 
       
 90053   /* Construct an expression node for a unary prefix operator
       
 90054   */
       
 90055   static void spanUnaryPrefix(
       
 90056     ExprSpan *pOut,        /* Write the new expression node here */
       
 90057     Parse *pParse,         /* Parsing context to record errors */
       
 90058     int op,                /* The operator */
       
 90059     ExprSpan *pOperand,    /* The operand */
       
 90060     Token *pPreOp         /* The operand token for setting the span */
       
 90061   ){
       
 90062     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
       
 90063     pOut->zStart = pPreOp->z;
       
 90064     pOut->zEnd = pOperand->zEnd;
       
 90065   }
       
 90066 /* Next is all token values, in a form suitable for use by makeheaders.
       
 90067 ** This section will be null unless lemon is run with the -m switch.
       
 90068 */
       
 90069 /* 
       
 90070 ** These constants (all generated automatically by the parser generator)
       
 90071 ** specify the various kinds of tokens (terminals) that the parser
       
 90072 ** understands. 
       
 90073 **
       
 90074 ** Each symbol here is a terminal symbol in the grammar.
       
 90075 */
       
 90076 /* Make sure the INTERFACE macro is defined.
       
 90077 */
       
 90078 #ifndef INTERFACE
       
 90079 # define INTERFACE 1
       
 90080 #endif
       
 90081 /* The next thing included is series of defines which control
       
 90082 ** various aspects of the generated parser.
       
 90083 **    YYCODETYPE         is the data type used for storing terminal
       
 90084 **                       and nonterminal numbers.  "unsigned char" is
       
 90085 **                       used if there are fewer than 250 terminals
       
 90086 **                       and nonterminals.  "int" is used otherwise.
       
 90087 **    YYNOCODE           is a number of type YYCODETYPE which corresponds
       
 90088 **                       to no legal terminal or nonterminal number.  This
       
 90089 **                       number is used to fill in empty slots of the hash 
       
 90090 **                       table.
       
 90091 **    YYFALLBACK         If defined, this indicates that one or more tokens
       
 90092 **                       have fall-back values which should be used if the
       
 90093 **                       original value of the token will not parse.
       
 90094 **    YYACTIONTYPE       is the data type used for storing terminal
       
 90095 **                       and nonterminal numbers.  "unsigned char" is
       
 90096 **                       used if there are fewer than 250 rules and
       
 90097 **                       states combined.  "int" is used otherwise.
       
 90098 **    sqlite3ParserTOKENTYPE     is the data type used for minor tokens given 
       
 90099 **                       directly to the parser from the tokenizer.
       
 90100 **    YYMINORTYPE        is the data type used for all minor tokens.
       
 90101 **                       This is typically a union of many types, one of
       
 90102 **                       which is sqlite3ParserTOKENTYPE.  The entry in the union
       
 90103 **                       for base tokens is called "yy0".
       
 90104 **    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
       
 90105 **                       zero the stack is dynamically sized using realloc()
       
 90106 **    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
       
 90107 **    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
       
 90108 **    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
       
 90109 **    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
       
 90110 **    YYNSTATE           the combined number of states.
       
 90111 **    YYNRULE            the number of rules in the grammar
       
 90112 **    YYERRORSYMBOL      is the code number of the error symbol.  If not
       
 90113 **                       defined, then do no error processing.
       
 90114 */
       
 90115 #define YYCODETYPE unsigned char
       
 90116 #define YYNOCODE 254
       
 90117 #define YYACTIONTYPE unsigned short int
       
 90118 #define YYWILDCARD 67
       
 90119 #define sqlite3ParserTOKENTYPE Token
       
 90120 typedef union {
       
 90121   int yyinit;
       
 90122   sqlite3ParserTOKENTYPE yy0;
       
 90123   Select* yy3;
       
 90124   ExprList* yy14;
       
 90125   SrcList* yy65;
       
 90126   struct LikeOp yy96;
       
 90127   Expr* yy132;
       
 90128   u8 yy186;
       
 90129   int yy328;
       
 90130   ExprSpan yy346;
       
 90131   struct TrigEvent yy378;
       
 90132   IdList* yy408;
       
 90133   struct {int value; int mask;} yy429;
       
 90134   TriggerStep* yy473;
       
 90135   struct LimitVal yy476;
       
 90136 } YYMINORTYPE;
       
 90137 #ifndef YYSTACKDEPTH
       
 90138 #define YYSTACKDEPTH 100
       
 90139 #endif
       
 90140 #define sqlite3ParserARG_SDECL Parse *pParse;
       
 90141 #define sqlite3ParserARG_PDECL ,Parse *pParse
       
 90142 #define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
       
 90143 #define sqlite3ParserARG_STORE yypParser->pParse = pParse
       
 90144 #define YYNSTATE 629
       
 90145 #define YYNRULE 329
       
 90146 #define YYFALLBACK 1
       
 90147 #define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
       
 90148 #define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
       
 90149 #define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
       
 90150 
       
 90151 /* The yyzerominor constant is used to initialize instances of
       
 90152 ** YYMINORTYPE objects to zero. */
       
 90153 static const YYMINORTYPE yyzerominor = { 0 };
       
 90154 
       
 90155 /* Define the yytestcase() macro to be a no-op if is not already defined
       
 90156 ** otherwise.
       
 90157 **
       
 90158 ** Applications can choose to define yytestcase() in the %include section
       
 90159 ** to a macro that can assist in verifying code coverage.  For production
       
 90160 ** code the yytestcase() macro should be turned off.  But it is useful
       
 90161 ** for testing.
       
 90162 */
       
 90163 #ifndef yytestcase
       
 90164 # define yytestcase(X)
       
 90165 #endif
       
 90166 
       
 90167 
       
 90168 /* Next are the tables used to determine what action to take based on the
       
 90169 ** current state and lookahead token.  These tables are used to implement
       
 90170 ** functions that take a state number and lookahead value and return an
       
 90171 ** action integer.  
       
 90172 **
       
 90173 ** Suppose the action integer is N.  Then the action is determined as
       
 90174 ** follows
       
 90175 **
       
 90176 **   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
       
 90177 **                                      token onto the stack and goto state N.
       
 90178 **
       
 90179 **   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
       
 90180 **
       
 90181 **   N == YYNSTATE+YYNRULE              A syntax error has occurred.
       
 90182 **
       
 90183 **   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
       
 90184 **
       
 90185 **   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
       
 90186 **                                      slots in the yy_action[] table.
       
 90187 **
       
 90188 ** The action table is constructed as a single large table named yy_action[].
       
 90189 ** Given state S and lookahead X, the action is computed as
       
 90190 **
       
 90191 **      yy_action[ yy_shift_ofst[S] + X ]
       
 90192 **
       
 90193 ** If the index value yy_shift_ofst[S]+X is out of range or if the value
       
 90194 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
       
 90195 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
       
 90196 ** and that yy_default[S] should be used instead.  
       
 90197 **
       
 90198 ** The formula above is for computing the action when the lookahead is
       
 90199 ** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
       
 90200 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
       
 90201 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
       
 90202 ** YY_SHIFT_USE_DFLT.
       
 90203 **
       
 90204 ** The following are the tables generated in this section:
       
 90205 **
       
 90206 **  yy_action[]        A single table containing all actions.
       
 90207 **  yy_lookahead[]     A table containing the lookahead for each entry in
       
 90208 **                     yy_action.  Used to detect hash collisions.
       
 90209 **  yy_shift_ofst[]    For each state, the offset into yy_action for
       
 90210 **                     shifting terminals.
       
 90211 **  yy_reduce_ofst[]   For each state, the offset into yy_action for
       
 90212 **                     shifting non-terminals after a reduce.
       
 90213 **  yy_default[]       Default action for each state.
       
 90214 */
       
 90215 static const YYACTIONTYPE yy_action[] = {
       
 90216  /*     0 */   312,  959,  182,  628,    2,  157,  219,  450,   24,   24,
       
 90217  /*    10 */    24,   24,  221,   26,   26,   26,   26,   27,   27,   28,
       
 90218  /*    20 */    28,   28,   29,  221,  424,  425,   30,  492,   33,  141,
       
 90219  /*    30 */   457,  463,   31,   26,   26,   26,   26,   27,   27,   28,
       
 90220  /*    40 */    28,   28,   29,  221,   28,   28,   28,   29,  221,   23,
       
 90221  /*    50 */    22,   32,  465,  466,  464,  464,   25,   25,   24,   24,
       
 90222  /*    60 */    24,   24,  293,   26,   26,   26,   26,   27,   27,   28,
       
 90223  /*    70 */    28,   28,   29,  221,  312,  450,  319,  479,  344,  208,
       
 90224  /*    80 */    47,   26,   26,   26,   26,   27,   27,   28,   28,   28,
       
 90225  /*    90 */    29,  221,  427,  428,  163,  339,  543,  368,  371,  372,
       
 90226  /*   100 */   521,  317,  472,  473,  457,  463,  296,  373,  294,   21,
       
 90227  /*   110 */   336,  367,  419,  416,  424,  425,  523,    1,  544,  446,
       
 90228  /*   120 */    80,  424,  425,   23,   22,   32,  465,  466,  464,  464,
       
 90229  /*   130 */    25,   25,   24,   24,   24,   24,  564,   26,   26,   26,
       
 90230  /*   140 */    26,   27,   27,   28,   28,   28,   29,  221,  312,  233,
       
 90231  /*   150 */   319,  441,  554,  152,  139,  263,  365,  268,  366,  160,
       
 90232  /*   160 */   551,  352,  332,  421,  222,  272,  362,  322,  218,  557,
       
 90233  /*   170 */   116,  339,  248,  574,  477,  223,  216,  573,  457,  463,
       
 90234  /*   180 */   450,   59,  427,  428,  295,  610,  336,  563,  538,  427,
       
 90235  /*   190 */   428,  385,  608,  609,  562,  446,   87,   23,   22,   32,
       
 90236  /*   200 */   465,  466,  464,  464,   25,   25,   24,   24,   24,   24,
       
 90237  /*   210 */   447,   26,   26,   26,   26,   27,   27,   28,   28,   28,
       
 90238  /*   220 */    29,  221,  312,  233,  477,  223,  576,  134,  139,  263,
       
 90239  /*   230 */   365,  268,  366,  160,  406,  354,  226,  498,  481,  272,
       
 90240  /*   240 */   339,   27,   27,   28,   28,   28,   29,  221,  450,  442,
       
 90241  /*   250 */   199,  540,  457,  463,  349,  336,  163,  551,   66,  368,
       
 90242  /*   260 */   371,  372,  450,  415,  446,   80,  522,  581,  401,  373,
       
 90243  /*   270 */   452,   23,   22,   32,  465,  466,  464,  464,   25,   25,
       
 90244  /*   280 */    24,   24,   24,   24,  447,   26,   26,   26,   26,   27,
       
 90245  /*   290 */    27,   28,   28,   28,   29,  221,  312,  339,  556,  607,
       
 90246  /*   300 */   197,  454,  454,  454,  546,  578,  352,  198,  607,  440,
       
 90247  /*   310 */    65,  351,  336,  426,  426,  399,  289,  424,  425,  606,
       
 90248  /*   320 */   605,  446,   73,  426,  214,  219,  457,  463,  606,  410,
       
 90249  /*   330 */   450,  241,  306,  196,  565,  479,  555,  208,  288,   29,
       
 90250  /*   340 */   221,  447,    4,  874,  504,   23,   22,   32,  465,  466,
       
 90251  /*   350 */   464,  464,   25,   25,   24,   24,   24,   24,  447,   26,
       
 90252  /*   360 */    26,   26,   26,   27,   27,   28,   28,   28,   29,  221,
       
 90253  /*   370 */   312,  163,  582,  339,  368,  371,  372,  314,  424,  425,
       
 90254  /*   380 */   604,  222,  397,  227,  373,  427,  428,  339,  336,  409,
       
 90255  /*   390 */   222,  478,  339,   30,  396,   33,  141,  446,   81,   62,
       
 90256  /*   400 */   457,  463,  336,  157,  400,  450,  504,  336,  438,  426,
       
 90257  /*   410 */   500,  446,   87,   41,  380,  613,  446,   80,  581,   23,
       
 90258  /*   420 */    22,   32,  465,  466,  464,  464,   25,   25,   24,   24,
       
 90259  /*   430 */    24,   24,  213,   26,   26,   26,   26,   27,   27,   28,
       
 90260  /*   440 */    28,   28,   29,  221,  312,  513,  427,  428,  517,  254,
       
 90261  /*   450 */   524,  386,  225,  339,  486,  363,  389,  339,  356,  443,
       
 90262  /*   460 */   494,  236,   30,  497,   33,  141,  399,  289,  336,  495,
       
 90263  /*   470 */   487,  501,  336,  450,  457,  463,  219,  446,   95,  445,
       
 90264  /*   480 */    68,  446,   95,  444,  424,  425,  488,   44,  348,  288,
       
 90265  /*   490 */   504,  424,  425,   23,   22,   32,  465,  466,  464,  464,
       
 90266  /*   500 */    25,   25,   24,   24,   24,   24,  391,   26,   26,   26,
       
 90267  /*   510 */    26,   27,   27,   28,   28,   28,   29,  221,  312,  361,
       
 90268  /*   520 */   556,  426,  520,  328,  191,  271,  339,  329,  247,  259,
       
 90269  /*   530 */   339,  566,   65,  249,  336,  426,  424,  425,  445,  516,
       
 90270  /*   540 */   426,  336,  444,  446,    9,  336,  556,  451,  457,  463,
       
 90271  /*   550 */   446,   74,  427,  428,  446,   69,  192,  618,   65,  427,
       
 90272  /*   560 */   428,  426,  323,  277,   16,  202,  189,   23,   22,   32,
       
 90273  /*   570 */   465,  466,  464,  464,   25,   25,   24,   24,   24,   24,
       
 90274  /*   580 */   255,   26,   26,   26,   26,   27,   27,   28,   28,   28,
       
 90275  /*   590 */    29,  221,  312,  339,  486,  426,  537,  235,  515,  447,
       
 90276  /*   600 */   339,  629,  419,  416,  427,  428,  217,  281,  336,  279,
       
 90277  /*   610 */   487,  203,  144,  526,  527,  336,  391,  446,   78,  429,
       
 90278  /*   620 */   430,  431,  457,  463,  446,   99,  488,  341,  528,  468,
       
 90279  /*   630 */   468,  426,  343,  472,  473,  626,  949,  474,  949,  529,
       
 90280  /*   640 */   447,   23,   22,   32,  465,  466,  464,  464,   25,   25,
       
 90281  /*   650 */    24,   24,   24,   24,  339,   26,   26,   26,   26,   27,
       
 90282  /*   660 */    27,   28,   28,   28,   29,  221,  312,  339,  162,  336,
       
 90283  /*   670 */   275,  283,  476,  376,  339,  579,  527,  346,  446,   98,
       
 90284  /*   680 */   622,   30,  336,   33,  141,  339,  426,  339,  508,  336,
       
 90285  /*   690 */   469,  446,  105,  418,    2,  222,  457,  463,  446,  101,
       
 90286  /*   700 */   336,  219,  336,  426,  161,  626,  948,  290,  948,  446,
       
 90287  /*   710 */   108,  446,  109,  398,  284,   23,   22,   32,  465,  466,
       
 90288  /*   720 */   464,  464,   25,   25,   24,   24,   24,   24,  339,   26,
       
 90289  /*   730 */    26,   26,   26,   27,   27,   28,   28,   28,   29,  221,
       
 90290  /*   740 */   312,  339,  271,  336,  339,   58,  535,  482,  143,  339,
       
 90291  /*   750 */   622,  318,  446,  133,  408,  257,  336,  426,  321,  336,
       
 90292  /*   760 */   357,  339,  272,  426,  336,  446,  135,  184,  446,   61,
       
 90293  /*   770 */   457,  463,  219,  446,  106,  426,  336,  493,  341,  234,
       
 90294  /*   780 */   468,  468,  621,  310,  407,  446,  102,  209,  144,   23,
       
 90295  /*   790 */    22,   32,  465,  466,  464,  464,   25,   25,   24,   24,
       
 90296  /*   800 */    24,   24,  339,   26,   26,   26,   26,   27,   27,   28,
       
 90297  /*   810 */    28,   28,   29,  221,  312,  339,  271,  336,  339,  341,
       
 90298  /*   820 */   538,  468,  468,  572,  383,  496,  446,   79,  499,  549,
       
 90299  /*   830 */   336,  426,  508,  336,  508,  341,  339,  468,  468,  446,
       
 90300  /*   840 */   103,  391,  446,   70,  457,  463,  572,  426,   40,  426,
       
 90301  /*   850 */    42,  336,  220,  324,  504,  341,  426,  468,  468,   18,
       
 90302  /*   860 */   446,  100,  266,   23,   22,   32,  465,  466,  464,  464,
       
 90303  /*   870 */    25,   25,   24,   24,   24,   24,  339,   26,   26,   26,
       
 90304  /*   880 */    26,   27,   27,   28,   28,   28,   29,  221,  312,  339,
       
 90305  /*   890 */   283,  336,  339,  261,  548,  384,  339,  327,  142,  550,
       
 90306  /*   900 */   446,  136,  475,  475,  336,  426,  185,  336,  499,  396,
       
 90307  /*   910 */   339,  336,  370,  446,  137,  256,  446,  138,  457,  463,
       
 90308  /*   920 */   446,   71,  499,  360,  426,  336,  161,  311,  623,  215,
       
 90309  /*   930 */   426,  359,  237,  412,  446,   82,  200,   23,   34,   32,
       
 90310  /*   940 */   465,  466,  464,  464,   25,   25,   24,   24,   24,   24,
       
 90311  /*   950 */   339,   26,   26,   26,   26,   27,   27,   28,   28,   28,
       
 90312  /*   960 */    29,  221,  312,  447,  271,  336,  339,  271,  340,  210,
       
 90313  /*   970 */   447,  172,  625,  211,  446,   83,  240,  552,  142,  426,
       
 90314  /*   980 */   321,  336,  426,  426,  339,  414,  331,  181,  458,  459,
       
 90315  /*   990 */   446,   72,  457,  463,  470,  506,   67,  158,  394,  336,
       
 90316  /*  1000 */   587,  325,  499,  447,  326,  311,  624,  447,  446,   84,
       
 90317  /*  1010 */   461,  462,   22,   32,  465,  466,  464,  464,   25,   25,
       
 90318  /*  1020 */    24,   24,   24,   24,  339,   26,   26,   26,   26,   27,
       
 90319  /*  1030 */    27,   28,   28,   28,   29,  221,  312,  460,  339,  336,
       
 90320  /*  1040 */   339,  283,  423,  393,  532,  533,  204,  205,  446,   85,
       
 90321  /*  1050 */   625,  392,  547,  336,  162,  336,  426,  426,  339,  435,
       
 90322  /*  1060 */   436,  339,  446,  104,  446,   86,  457,  463,  264,  291,
       
 90323  /*  1070 */   274,   49,  162,  336,  426,  426,  336,  297,  265,  542,
       
 90324  /*  1080 */   541,  405,  446,   88,  594,  446,   89,   32,  465,  466,
       
 90325  /*  1090 */   464,  464,   25,   25,   24,   24,   24,   24,  600,   26,
       
 90326  /*  1100 */    26,   26,   26,   27,   27,   28,   28,   28,   29,  221,
       
 90327  /*  1110 */    36,  345,  339,    3,  214,    8,  422,  335,  425,  437,
       
 90328  /*  1120 */   375,  148,  162,   36,  345,  339,    3,  336,  342,  432,
       
 90329  /*  1130 */   335,  425,  149,  577,  426,  162,  446,   90,  151,  339,
       
 90330  /*  1140 */   336,  342,  434,  339,  283,  433,  333,  347,  447,  446,
       
 90331  /*  1150 */    75,  588,    6,  158,  336,  448,  140,  481,  336,  426,
       
 90332  /*  1160 */   347,  453,  334,  446,   76,   49,  350,  446,   91,    7,
       
 90333  /*  1170 */   481,  426,  397,  283,  355,  250,  426,   39,   38,  251,
       
 90334  /*  1180 */   339,  426,   48,  353,   37,  337,  338,  596,  426,  452,
       
 90335  /*  1190 */    39,   38,  514,  252,  390,  336,   20,   37,  337,  338,
       
 90336  /*  1200 */   253,   43,  452,  206,  446,   92,  219,  449,  242,  243,
       
 90337  /*  1210 */   244,  150,  246,  283,  491,  593,  597,  490,  224,  258,
       
 90338  /*  1220 */   454,  454,  454,  455,  456,   10,  503,  183,  426,  178,
       
 90339  /*  1230 */   156,  301,  426,  454,  454,  454,  455,  456,   10,  339,
       
 90340  /*  1240 */   302,  426,   36,  345,   50,    3,  339,  505,  260,  335,
       
 90341  /*  1250 */   425,  262,  339,  176,  336,  581,  598,  358,  364,  175,
       
 90342  /*  1260 */   342,  336,  177,  446,   93,   46,  345,  336,    3,  339,
       
 90343  /*  1270 */   446,   94,  335,  425,  525,  339,  446,   77,  320,  347,
       
 90344  /*  1280 */   511,  339,  507,  342,  336,  589,  601,   56,   56,  481,
       
 90345  /*  1290 */   336,  512,  283,  446,   17,  531,  336,  426,  530,  446,
       
 90346  /*  1300 */    96,  534,  347,  404,  298,  446,   97,  426,  313,   39,
       
 90347  /*  1310 */    38,  267,  481,  219,  535,  536,   37,  337,  338,  283,
       
 90348  /*  1320 */   620,  452,  309,  283,  111,   19,  288,  509,  269,  424,
       
 90349  /*  1330 */   425,  539,   39,   38,  426,  238,  270,  411,  426,   37,
       
 90350  /*  1340 */   337,  338,  426,  426,  452,  558,  426,  307,  231,  276,
       
 90351  /*  1350 */   278,  426,  454,  454,  454,  455,  456,   10,  553,  280,
       
 90352  /*  1360 */   426,  559,  239,  230,  426,  426,  299,  282,  287,  481,
       
 90353  /*  1370 */   560,  388,  584,  232,  426,  454,  454,  454,  455,  456,
       
 90354  /*  1380 */    10,  561,  426,  426,  585,  395,  426,  426,  292,  194,
       
 90355  /*  1390 */   195,  592,  603,  300,  303,  308,  377,  522,  381,  426,
       
 90356  /*  1400 */   426,  452,  567,  426,  304,  617,  426,  426,  426,  426,
       
 90357  /*  1410 */   379,   53,  147,  165,  166,  167,  580,  212,  569,  426,
       
 90358  /*  1420 */   426,  285,  168,  570,  387,  120,  123,  187,  590,  402,
       
 90359  /*  1430 */   403,  125,  454,  454,  454,  330,  599,  614,  186,  126,
       
 90360  /*  1440 */   127,  128,  615,  616,   57,   60,  619,  107,  229,   64,
       
 90361  /*  1450 */   115,  420,  245,  130,  439,  180,  315,  207,  670,  316,
       
 90362  /*  1460 */   671,  467,  672,  153,  154,   35,  483,  471,  480,  188,
       
 90363  /*  1470 */   201,  155,  484,    5,  485,  489,   12,  502,   45,   11,
       
 90364  /*  1480 */   110,  145,  518,  519,  510,  228,   51,  112,  369,  273,
       
 90365  /*  1490 */   113,  159,  545,   52,  374,  114,  164,  265,  378,  190,
       
 90366  /*  1500 */   146,  568,  117,  158,  286,  382,  169,  119,   15,  583,
       
 90367  /*  1510 */   170,  171,  121,  586,  122,   54,   55,   13,  124,  591,
       
 90368  /*  1520 */   173,  174,  118,  575,  129,  595,  571,  131,   14,  132,
       
 90369  /*  1530 */   611,   63,  612,  193,  602,  179,  305,  413,  417,  960,
       
 90370  /*  1540 */   627,
       
 90371 };
       
 90372 static const YYCODETYPE yy_lookahead[] = {
       
 90373  /*     0 */    19,  142,  143,  144,  145,   24,  115,   26,   77,   78,
       
 90374  /*    10 */    79,   80,   92,   82,   83,   84,   85,   86,   87,   88,
       
 90375  /*    20 */    89,   90,   91,   92,   26,   27,  222,  223,  224,  225,
       
 90376  /*    30 */    49,   50,   81,   82,   83,   84,   85,   86,   87,   88,
       
 90377  /*    40 */    89,   90,   91,   92,   88,   89,   90,   91,   92,   68,
       
 90378  /*    50 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
       
 90379  /*    60 */    79,   80,   16,   82,   83,   84,   85,   86,   87,   88,
       
 90380  /*    70 */    89,   90,   91,   92,   19,   94,   19,  166,  167,  168,
       
 90381  /*    80 */    25,   82,   83,   84,   85,   86,   87,   88,   89,   90,
       
 90382  /*    90 */    91,   92,   94,   95,   96,  150,   36,   99,  100,  101,
       
 90383  /*   100 */   174,  169,  170,  171,   49,   50,   60,  109,   62,   54,
       
 90384  /*   110 */   165,   51,    1,    2,   26,   27,  174,   22,   58,  174,
       
 90385  /*   120 */   175,   26,   27,   68,   69,   70,   71,   72,   73,   74,
       
 90386  /*   130 */    75,   76,   77,   78,   79,   80,  186,   82,   83,   84,
       
 90387  /*   140 */    85,   86,   87,   88,   89,   90,   91,   92,   19,   92,
       
 90388  /*   150 */    19,  172,  173,   96,   97,   98,   99,  100,  101,  102,
       
 90389  /*   160 */   181,  216,  146,  147,  232,  108,  221,  107,  152,  186,
       
 90390  /*   170 */   154,  150,  195,   30,   86,   87,  160,   34,   49,   50,
       
 90391  /*   180 */    26,   52,   94,   95,  138,   97,  165,  181,  182,   94,
       
 90392  /*   190 */    95,   48,  104,  105,  188,  174,  175,   68,   69,   70,
       
 90393  /*   200 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
       
 90394  /*   210 */   194,   82,   83,   84,   85,   86,   87,   88,   89,   90,
       
 90395  /*   220 */    91,   92,   19,   92,   86,   87,   21,   24,   97,   98,
       
 90396  /*   230 */    99,  100,  101,  102,  218,  214,  215,  208,   66,  108,
       
 90397  /*   240 */   150,   86,   87,   88,   89,   90,   91,   92,   94,  173,
       
 90398  /*   250 */   160,  183,   49,   50,  191,  165,   96,  181,   22,   99,
       
 90399  /*   260 */   100,  101,   26,  247,  174,  175,   94,   57,   63,  109,
       
 90400  /*   270 */    98,   68,   69,   70,   71,   72,   73,   74,   75,   76,
       
 90401  /*   280 */    77,   78,   79,   80,  194,   82,   83,   84,   85,   86,
       
 90402  /*   290 */    87,   88,   89,   90,   91,   92,   19,  150,  150,  150,
       
 90403  /*   300 */    25,  129,  130,  131,  183,  100,  216,  160,  150,  161,
       
 90404  /*   310 */   162,  221,  165,  165,  165,  105,  106,   26,   27,  170,
       
 90405  /*   320 */   171,  174,  175,  165,  160,  115,   49,   50,  170,  171,
       
 90406  /*   330 */    94,  148,  163,  185,  186,  166,  167,  168,  128,   91,
       
 90407  /*   340 */    92,  194,  196,  138,  166,   68,   69,   70,   71,   72,
       
 90408  /*   350 */    73,   74,   75,   76,   77,   78,   79,   80,  194,   82,
       
 90409  /*   360 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
       
 90410  /*   370 */    19,   96,   11,  150,   99,  100,  101,  155,   26,   27,
       
 90411  /*   380 */   231,  232,  218,  205,  109,   94,   95,  150,  165,  231,
       
 90412  /*   390 */   232,  166,  150,  222,  150,  224,  225,  174,  175,  235,
       
 90413  /*   400 */    49,   50,  165,   24,  240,   26,  166,  165,  153,  165,
       
 90414  /*   410 */   119,  174,  175,  136,  237,  244,  174,  175,   57,   68,
       
 90415  /*   420 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
       
 90416  /*   430 */    79,   80,  236,   82,   83,   84,   85,   86,   87,   88,
       
 90417  /*   440 */    89,   90,   91,   92,   19,  205,   94,   95,   23,  226,
       
 90418  /*   450 */   165,  229,  215,  150,   12,   88,  234,  150,  216,  174,
       
 90419  /*   460 */    32,  217,  222,   25,  224,  225,  105,  106,  165,   41,
       
 90420  /*   470 */    28,  119,  165,   94,   49,   50,  115,  174,  175,  112,
       
 90421  /*   480 */    22,  174,  175,  116,   26,   27,   44,  136,   46,  128,
       
 90422  /*   490 */   166,   26,   27,   68,   69,   70,   71,   72,   73,   74,
       
 90423  /*   500 */    75,   76,   77,   78,   79,   80,  150,   82,   83,   84,
       
 90424  /*   510 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  150,
       
 90425  /*   520 */   150,  165,   23,  220,  196,  150,  150,  220,  158,  205,
       
 90426  /*   530 */   150,  161,  162,  198,  165,  165,   26,   27,  112,   23,
       
 90427  /*   540 */   165,  165,  116,  174,  175,  165,  150,  166,   49,   50,
       
 90428  /*   550 */   174,  175,   94,   95,  174,  175,  118,  161,  162,   94,
       
 90429  /*   560 */    95,  165,  187,   16,   22,  160,   24,   68,   69,   70,
       
 90430  /*   570 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
       
 90431  /*   580 */   150,   82,   83,   84,   85,   86,   87,   88,   89,   90,
       
 90432  /*   590 */    91,   92,   19,  150,   12,  165,   23,  241,   88,  194,
       
 90433  /*   600 */   150,    0,    1,    2,   94,   95,  160,   60,  165,   62,
       
 90434  /*   610 */    28,  206,  207,  190,  191,  165,  150,  174,  175,    7,
       
 90435  /*   620 */     8,    9,   49,   50,  174,  175,   44,  111,   46,  113,
       
 90436  /*   630 */   114,  165,  169,  170,  171,   22,   23,  233,   25,   57,
       
 90437  /*   640 */   194,   68,   69,   70,   71,   72,   73,   74,   75,   76,
       
 90438  /*   650 */    77,   78,   79,   80,  150,   82,   83,   84,   85,   86,
       
 90439  /*   660 */    87,   88,   89,   90,   91,   92,   19,  150,   25,  165,
       
 90440  /*   670 */    23,  150,  233,   19,  150,  190,  191,  228,  174,  175,
       
 90441  /*   680 */    67,  222,  165,  224,  225,  150,  165,  150,  150,  165,
       
 90442  /*   690 */    23,  174,  175,  144,  145,  232,   49,   50,  174,  175,
       
 90443  /*   700 */   165,  115,  165,  165,   50,   22,   23,  241,   25,  174,
       
 90444  /*   710 */   175,  174,  175,  127,  193,   68,   69,   70,   71,   72,
       
 90445  /*   720 */    73,   74,   75,   76,   77,   78,   79,   80,  150,   82,
       
 90446  /*   730 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
       
 90447  /*   740 */    19,  150,  150,  165,  150,   24,  103,   23,  150,  150,
       
 90448  /*   750 */    67,  213,  174,  175,   97,  209,  165,  165,  104,  165,
       
 90449  /*   760 */   150,  150,  108,  165,  165,  174,  175,   23,  174,  175,
       
 90450  /*   770 */    49,   50,  115,  174,  175,  165,  165,  177,  111,  187,
       
 90451  /*   780 */   113,  114,  250,  251,  127,  174,  175,  206,  207,   68,
       
 90452  /*   790 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
       
 90453  /*   800 */    79,   80,  150,   82,   83,   84,   85,   86,   87,   88,
       
 90454  /*   810 */    89,   90,   91,   92,   19,  150,  150,  165,  150,  111,
       
 90455  /*   820 */   182,  113,  114,  105,  106,  177,  174,  175,   25,  166,
       
 90456  /*   830 */   165,  165,  150,  165,  150,  111,  150,  113,  114,  174,
       
 90457  /*   840 */   175,  150,  174,  175,   49,   50,  128,  165,  135,  165,
       
 90458  /*   850 */   137,  165,  197,  187,  166,  111,  165,  113,  114,  204,
       
 90459  /*   860 */   174,  175,  177,   68,   69,   70,   71,   72,   73,   74,
       
 90460  /*   870 */    75,   76,   77,   78,   79,   80,  150,   82,   83,   84,
       
 90461  /*   880 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  150,
       
 90462  /*   890 */   150,  165,  150,  205,  177,  213,  150,  213,   95,  177,
       
 90463  /*   900 */   174,  175,  129,  130,  165,  165,   23,  165,   25,  150,
       
 90464  /*   910 */   150,  165,  178,  174,  175,  150,  174,  175,   49,   50,
       
 90465  /*   920 */   174,  175,  119,   19,  165,  165,   50,   22,   23,  160,
       
 90466  /*   930 */   165,   27,  241,  193,  174,  175,  160,   68,   69,   70,
       
 90467  /*   940 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
       
 90468  /*   950 */   150,   82,   83,   84,   85,   86,   87,   88,   89,   90,
       
 90469  /*   960 */    91,   92,   19,  194,  150,  165,  150,  150,  150,  160,
       
 90470  /*   970 */   194,   25,   67,  160,  174,  175,  217,  166,   95,  165,
       
 90471  /*   980 */   104,  165,  165,  165,  150,  245,  248,  249,   49,   50,
       
 90472  /*   990 */   174,  175,   49,   50,   23,   23,   25,   25,  242,  165,
       
 90473  /*  1000 */   199,  187,  119,  194,  187,   22,   23,  194,  174,  175,
       
 90474  /*  1010 */    71,   72,   69,   70,   71,   72,   73,   74,   75,   76,
       
 90475  /*  1020 */    77,   78,   79,   80,  150,   82,   83,   84,   85,   86,
       
 90476  /*  1030 */    87,   88,   89,   90,   91,   92,   19,   98,  150,  165,
       
 90477  /*  1040 */   150,  150,  150,   19,    7,    8,  105,  106,  174,  175,
       
 90478  /*  1050 */    67,   27,   23,  165,   25,  165,  165,  165,  150,  150,
       
 90479  /*  1060 */   150,  150,  174,  175,  174,  175,   49,   50,   98,  242,
       
 90480  /*  1070 */    23,  125,   25,  165,  165,  165,  165,  209,  108,   97,
       
 90481  /*  1080 */    98,  209,  174,  175,  193,  174,  175,   70,   71,   72,
       
 90482  /*  1090 */    73,   74,   75,   76,   77,   78,   79,   80,  199,   82,
       
 90483  /*  1100 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
       
 90484  /*  1110 */    19,   20,  150,   22,  160,   22,  149,   26,   27,  150,
       
 90485  /*  1120 */    23,    6,   25,   19,   20,  150,   22,  165,   37,  149,
       
 90486  /*  1130 */    26,   27,  151,   23,  165,   25,  174,  175,  151,  150,
       
 90487  /*  1140 */   165,   37,   13,  150,  150,  149,  149,   56,  194,  174,
       
 90488  /*  1150 */   175,   23,   25,   25,  165,  194,  150,   66,  165,  165,
       
 90489  /*  1160 */    56,  150,  159,  174,  175,  125,  150,  174,  175,   76,
       
 90490  /*  1170 */    66,  165,  218,  150,  122,  199,  165,   86,   87,  200,
       
 90491  /*  1180 */   150,  165,  123,  121,   93,   94,   95,  193,  165,   98,
       
 90492  /*  1190 */    86,   87,   88,  201,  240,  165,  124,   93,   94,   95,
       
 90493  /*  1200 */   202,  135,   98,    5,  174,  175,  115,  203,   10,   11,
       
 90494  /*  1210 */    12,   13,   14,  150,  157,   17,  193,  150,  227,  210,
       
 90495  /*  1220 */   129,  130,  131,  132,  133,  134,  150,  157,  165,   31,
       
 90496  /*  1230 */   117,   33,  165,  129,  130,  131,  132,  133,  134,  150,
       
 90497  /*  1240 */    42,  165,   19,   20,  104,   22,  150,  211,  210,   26,
       
 90498  /*  1250 */    27,  210,  150,   55,  165,   57,  193,  120,  104,   61,
       
 90499  /*  1260 */    37,  165,   64,  174,  175,   19,   20,  165,   22,  150,
       
 90500  /*  1270 */   174,  175,   26,   27,  176,  150,  174,  175,   47,   56,
       
 90501  /*  1280 */   211,  150,  150,   37,  165,   23,   23,   25,   25,   66,
       
 90502  /*  1290 */   165,  211,  150,  174,  175,  184,  165,  165,  176,  174,
       
 90503  /*  1300 */   175,  178,   56,  105,  106,  174,  175,  165,  110,   86,
       
 90504  /*  1310 */    87,  176,   66,  115,  103,  176,   93,   94,   95,  150,
       
 90505  /*  1320 */    23,   98,   25,  150,   22,   22,  128,  150,  150,   26,
       
 90506  /*  1330 */    27,  150,   86,   87,  165,  193,  150,  139,  165,   93,
       
 90507  /*  1340 */    94,   95,  165,  165,   98,  150,  165,  179,   92,  150,
       
 90508  /*  1350 */   150,  165,  129,  130,  131,  132,  133,  134,  184,  150,
       
 90509  /*  1360 */   165,  176,  193,  230,  165,  165,  193,  150,  150,   66,
       
 90510  /*  1370 */   176,  150,  150,  230,  165,  129,  130,  131,  132,  133,
       
 90511  /*  1380 */   134,  176,  165,  165,  150,  150,  165,  165,  150,   86,
       
 90512  /*  1390 */    87,  150,  150,  150,  150,  179,   18,   94,   45,  165,
       
 90513  /*  1400 */   165,   98,  157,  165,  150,  150,  165,  165,  165,  165,
       
 90514  /*  1410 */   157,  135,   68,  156,  156,  156,  189,  157,  157,  165,
       
 90515  /*  1420 */   165,  238,  156,  239,  157,  189,   22,  219,  199,  157,
       
 90516  /*  1430 */    18,  192,  129,  130,  131,  157,  199,   40,  219,  192,
       
 90517  /*  1440 */   192,  192,  157,  157,  243,  243,   38,  164,  180,  246,
       
 90518  /*  1450 */   180,    1,   15,  189,   23,  249,  252,   22,  117,  252,
       
 90519  /*  1460 */   117,  112,  117,  117,  117,   22,   11,   23,   23,   22,
       
 90520  /*  1470 */    22,   25,   23,   35,   23,   23,   35,  119,   25,   25,
       
 90521  /*  1480 */    22,  117,   23,   23,   27,   52,   22,   22,   52,   23,
       
 90522  /*  1490 */    22,   35,   29,   22,   52,   22,  102,  108,   19,   24,
       
 90523  /*  1500 */    39,   20,  104,   25,  138,   43,  104,   22,    5,    1,
       
 90524  /*  1510 */   117,   35,  107,   27,  126,   76,   76,   22,  118,    1,
       
 90525  /*  1520 */    16,  120,   53,   53,  118,   20,   59,  107,   22,  126,
       
 90526  /*  1530 */    23,   16,   23,   22,  127,   15,  140,   65,    3,  253,
       
 90527  /*  1540 */     4,
       
 90528 };
       
 90529 #define YY_SHIFT_USE_DFLT (-110)
       
 90530 #define YY_SHIFT_MAX 417
       
 90531 static const short yy_shift_ofst[] = {
       
 90532  /*     0 */   111, 1091, 1198, 1091, 1223, 1223,   -2,   88,   88,  -19,
       
 90533  /*    10 */  1223, 1223, 1223, 1223, 1223,  210,  465,  129, 1104, 1223,
       
 90534  /*    20 */  1223, 1223, 1223, 1223, 1223, 1223, 1223, 1223, 1223, 1223,
       
 90535  /*    30 */  1223, 1223, 1246, 1223, 1223, 1223, 1223, 1223, 1223, 1223,
       
 90536  /*    40 */  1223, 1223, 1223, 1223, 1223, 1223, 1223, 1223, 1223, 1223,
       
 90537  /*    50 */  1223, 1223, 1223, 1223, 1223, 1223, 1223, 1223, 1223, 1223,
       
 90538  /*    60 */  1223,  -49,  361,  465,  465,  154,  138,  138, -109,   55,
       
 90539  /*    70 */   203,  277,  351,  425,  499,  573,  647,  721,  795,  869,
       
 90540  /*    80 */   795,  795,  795,  795,  795,  795,  795,  795,  795,  795,
       
 90541  /*    90 */   795,  795,  795,  795,  795,  795,  795,  795,  943, 1017,
       
 90542  /*   100 */  1017,  -69,  -69,  -69,  -69,   -1,   -1,   57,  155,  -44,
       
 90543  /*   110 */   465,  465,  465,  465,  465,  654,  205,  465,  465,  465,
       
 90544  /*   120 */   465,  465,  465,  465,  465,  465,  465,  465,  465,  465,
       
 90545  /*   130 */   465,  465,  465,  248,  154,  -80, -110, -110, -110, 1303,
       
 90546  /*   140 */   131,   95,  291,  352,  458,  510,  582,  582,  465,  465,
       
 90547  /*   150 */   465,  465,  465,  465,  465,  465,  465,  465,  465,  465,
       
 90548  /*   160 */   465,  465,  465,  465,  465,  465,  465,  465,  465,  465,
       
 90549  /*   170 */   465,  465,  465,  465,  465,  465,  465,  465,  465,  465,
       
 90550  /*   180 */   613,  683,  601,  379,  379,  379,  657,  586, -109, -109,
       
 90551  /*   190 */  -109, -110, -110, -110,  172,  172,  275,  160,  516,  667,
       
 90552  /*   200 */   724,  442,  744,  883,   60,   60,  612,  367,  236,  803,
       
 90553  /*   210 */   708,  708,  143,  718,  708,  708,  708,  708,  542,  426,
       
 90554  /*   220 */   438,  154,  773,  773,  713,  428,  428,  904,  428,  876,
       
 90555  /*   230 */   428,  154,  428,  154,  643, 1024,  946, 1024,  904,  904,
       
 90556  /*   240 */   946, 1115, 1115, 1115, 1115, 1129, 1129, 1127, -109, 1040,
       
 90557  /*   250 */  1052, 1059, 1062, 1072, 1066, 1113, 1113, 1140, 1137, 1140,
       
 90558  /*   260 */  1137, 1140, 1137, 1154, 1154, 1231, 1154, 1211, 1154, 1302,
       
 90559  /*   270 */  1256, 1256, 1231, 1154, 1154, 1154, 1302, 1378, 1113, 1378,
       
 90560  /*   280 */  1113, 1378, 1113, 1113, 1353, 1276, 1378, 1113, 1344, 1344,
       
 90561  /*   290 */  1404, 1040, 1113, 1412, 1412, 1412, 1412, 1040, 1344, 1404,
       
 90562  /*   300 */  1113, 1397, 1397, 1113, 1113, 1408, -110, -110, -110, -110,
       
 90563  /*   310 */  -110, -110,  939,   46,  547,  905,  983,  971,  972,  970,
       
 90564  /*   320 */  1037,  941,  982, 1029, 1047, 1097, 1110, 1128, 1262, 1263,
       
 90565  /*   330 */  1093, 1297, 1450, 1437, 1431, 1435, 1341, 1343, 1345, 1346,
       
 90566  /*   340 */  1347, 1349, 1443, 1444, 1445, 1447, 1455, 1448, 1449, 1446,
       
 90567  /*   350 */  1451, 1452, 1453, 1438, 1454, 1441, 1453, 1358, 1458, 1456,
       
 90568  /*   360 */  1457, 1364, 1459, 1460, 1461, 1433, 1464, 1463, 1436, 1465,
       
 90569  /*   370 */  1466, 1468, 1471, 1442, 1473, 1394, 1389, 1479, 1481, 1475,
       
 90570  /*   380 */  1398, 1462, 1467, 1469, 1478, 1470, 1366, 1402, 1485, 1503,
       
 90571  /*   390 */  1508, 1393, 1476, 1486, 1405, 1439, 1440, 1388, 1495, 1400,
       
 90572  /*   400 */  1518, 1504, 1401, 1505, 1406, 1420, 1403, 1506, 1407, 1507,
       
 90573  /*   410 */  1509, 1515, 1472, 1520, 1396, 1511, 1535, 1536,
       
 90574 };
       
 90575 #define YY_REDUCE_USE_DFLT (-197)
       
 90576 #define YY_REDUCE_MAX 311
       
 90577 static const short yy_reduce_ofst[] = {
       
 90578  /*     0 */  -141,   90,   16,  147,  -55,   21,  148,  149,  158,  240,
       
 90579  /*    10 */   223,  237,  242,  303,  307,  164,  370,  171,  369,  376,
       
 90580  /*    20 */   380,  443,  450,  504,  517,  524,  535,  537,  578,  591,
       
 90581  /*    30 */   594,  599,  611,  652,  665,  668,  686,  726,  739,  742,
       
 90582  /*    40 */   746,  760,  800,  816,  834,  874,  888,  890,  908,  911,
       
 90583  /*    50 */   962,  975,  989,  993, 1030, 1089, 1096, 1102, 1119, 1125,
       
 90584  /*    60 */  1131, -196,  954,  740,  396,  169,  -68,  463,  405,  459,
       
 90585  /*    70 */   459,  459,  459,  459,  459,  459,  459,  459,  459,  459,
       
 90586  /*    80 */   459,  459,  459,  459,  459,  459,  459,  459,  459,  459,
       
 90587  /*    90 */   459,  459,  459,  459,  459,  459,  459,  459,  459,  459,
       
 90588  /*   100 */   459,  459,  459,  459,  459,  459,  459,  -21,  459,  459,
       
 90589  /*   110 */   538,  375,  592,  666,  814,    6,  222,  521,  682,  817,
       
 90590  /*   120 */   356,  244,  466,  684,  691,  891,  994, 1023, 1063, 1142,
       
 90591  /*   130 */  1169,  759, 1173,  459,  -89,  459,  459,  459,  459,  285,
       
 90592  /*   140 */    76,  430,  598,  610,  765,  818,  423,  485,  892,  909,
       
 90593  /*   150 */   910,  969, 1006,  818, 1011, 1016, 1067, 1076, 1132, 1177,
       
 90594  /*   160 */  1178, 1181, 1186, 1195, 1199, 1200, 1209, 1217, 1218, 1221,
       
 90595  /*   170 */  1222, 1234, 1235, 1238, 1241, 1242, 1243, 1244, 1254, 1255,
       
 90596  /*   180 */   532,  532,  549,  178,  324,  688,  446,  769,  776,  809,
       
 90597  /*   190 */   813,  655,  581,  738,  -74,  -58,  -50,  -17,  -23,  -23,
       
 90598  /*   200 */   -23,   63,  -23,   29,   68,  121,  183,  146,  225,   29,
       
 90599  /*   210 */   -23,  -23,  196,  177,  -23,  -23,  -23,  -23,  255,  328,
       
 90600  /*   220 */   335,  381,  404,  439,  449,  600,  648,  546,  685,  638,
       
 90601  /*   230 */   717,  663,  722,  811,  734,  756,  801,  827,  868,  872,
       
 90602  /*   240 */   899,  967,  980,  996,  997,  981,  987, 1003,  961,  976,
       
 90603  /*   250 */   979,  992,  998, 1004,  991, 1057, 1070, 1009, 1036, 1038,
       
 90604  /*   260 */  1069, 1041, 1080, 1098, 1122, 1111, 1135, 1123, 1139, 1168,
       
 90605  /*   270 */  1133, 1143, 1174, 1185, 1194, 1205, 1216, 1257, 1245, 1258,
       
 90606  /*   280 */  1253, 1259, 1260, 1261, 1183, 1184, 1266, 1267, 1227, 1236,
       
 90607  /*   290 */  1208, 1229, 1272, 1239, 1247, 1248, 1249, 1237, 1264, 1219,
       
 90608  /*   300 */  1278, 1201, 1202, 1285, 1286, 1203, 1283, 1268, 1270, 1206,
       
 90609  /*   310 */  1204, 1207,
       
 90610 };
       
 90611 static const YYACTIONTYPE yy_default[] = {
       
 90612  /*     0 */   634,  869,  958,  958,  869,  958,  958,  898,  898,  757,
       
 90613  /*    10 */   867,  958,  958,  958,  958,  958,  958,  932,  958,  958,
       
 90614  /*    20 */   958,  958,  958,  958,  958,  958,  958,  958,  958,  958,
       
 90615  /*    30 */   958,  958,  958,  958,  958,  958,  958,  958,  958,  958,
       
 90616  /*    40 */   958,  958,  958,  958,  958,  958,  958,  958,  958,  958,
       
 90617  /*    50 */   958,  958,  958,  958,  958,  958,  958,  958,  958,  958,
       
 90618  /*    60 */   958,  841,  958,  958,  958,  673,  898,  898,  761,  792,
       
 90619  /*    70 */   958,  958,  958,  958,  958,  958,  958,  958,  793,  958,
       
 90620  /*    80 */   871,  866,  862,  864,  863,  870,  794,  783,  790,  797,
       
 90621  /*    90 */   772,  911,  799,  800,  806,  807,  933,  931,  829,  828,
       
 90622  /*   100 */   847,  831,  845,  853,  846,  830,  840,  665,  832,  833,
       
 90623  /*   110 */   958,  958,  958,  958,  958,  726,  660,  958,  958,  958,
       
 90624  /*   120 */   958,  958,  958,  958,  958,  958,  958,  958,  958,  958,
       
 90625  /*   130 */   958,  958,  958,  834,  958,  835,  848,  849,  850,  958,
       
 90626  /*   140 */   958,  958,  958,  958,  958,  958,  958,  958,  640,  958,
       
 90627  /*   150 */   958,  958,  958,  958,  958,  958,  958,  958,  958,  958,
       
 90628  /*   160 */   958,  958,  958,  958,  958,  958,  958,  958,  958,  958,
       
 90629  /*   170 */   958,  958,  958,  958,  958,  882,  958,  936,  938,  958,
       
 90630  /*   180 */   958,  958,  634,  757,  757,  757,  958,  958,  958,  958,
       
 90631  /*   190 */   958,  751,  761,  950,  958,  958,  717,  958,  958,  958,
       
 90632  /*   200 */   958,  958,  958,  958,  958,  958,  642,  749,  675,  759,
       
 90633  /*   210 */   662,  738,  904,  958,  923,  921,  740,  802,  958,  749,
       
 90634  /*   220 */   758,  958,  958,  958,  865,  786,  786,  774,  786,  696,
       
 90635  /*   230 */   786,  958,  786,  958,  699,  916,  796,  916,  774,  774,
       
 90636  /*   240 */   796,  639,  639,  639,  639,  650,  650,  716,  958,  796,
       
 90637  /*   250 */   787,  789,  779,  791,  958,  765,  765,  773,  778,  773,
       
 90638  /*   260 */   778,  773,  778,  728,  728,  713,  728,  699,  728,  875,
       
 90639  /*   270 */   879,  879,  713,  728,  728,  728,  875,  657,  765,  657,
       
 90640  /*   280 */   765,  657,  765,  765,  908,  910,  657,  765,  730,  730,
       
 90641  /*   290 */   808,  796,  765,  737,  737,  737,  737,  796,  730,  808,
       
 90642  /*   300 */   765,  935,  935,  765,  765,  943,  683,  701,  701,  950,
       
 90643  /*   310 */   955,  955,  958,  958,  958,  958,  958,  958,  958,  958,
       
 90644  /*   320 */   958,  958,  958,  958,  958,  958,  958,  958,  958,  958,
       
 90645  /*   330 */   884,  958,  958,  648,  958,  667,  815,  820,  816,  958,
       
 90646  /*   340 */   817,  743,  958,  958,  958,  958,  958,  958,  958,  958,
       
 90647  /*   350 */   958,  958,  868,  958,  780,  958,  788,  958,  958,  958,
       
 90648  /*   360 */   958,  958,  958,  958,  958,  958,  958,  958,  958,  958,
       
 90649  /*   370 */   958,  958,  958,  958,  958,  958,  958,  958,  958,  958,
       
 90650  /*   380 */   958,  958,  958,  906,  907,  958,  958,  958,  958,  958,
       
 90651  /*   390 */   958,  914,  958,  958,  958,  958,  958,  958,  958,  958,
       
 90652  /*   400 */   958,  958,  958,  958,  958,  958,  958,  958,  958,  958,
       
 90653  /*   410 */   958,  958,  942,  958,  958,  945,  635,  958,  630,  632,
       
 90654  /*   420 */   633,  637,  638,  641,  667,  668,  670,  671,  672,  643,
       
 90655  /*   430 */   644,  645,  646,  647,  649,  653,  651,  652,  654,  661,
       
 90656  /*   440 */   663,  682,  684,  686,  747,  748,  812,  741,  742,  746,
       
 90657  /*   450 */   669,  823,  814,  818,  819,  821,  822,  836,  837,  839,
       
 90658  /*   460 */   844,  852,  855,  838,  843,  851,  854,  744,  745,  858,
       
 90659  /*   470 */   676,  677,  680,  681,  894,  896,  895,  897,  679,  678,
       
 90660  /*   480 */   824,  827,  860,  861,  924,  925,  926,  927,  928,  856,
       
 90661  /*   490 */   766,  859,  842,  781,  784,  785,  782,  750,  760,  768,
       
 90662  /*   500 */   769,  770,  771,  755,  756,  762,  777,  810,  811,  775,
       
 90663  /*   510 */   776,  763,  764,  752,  753,  754,  857,  813,  825,  826,
       
 90664  /*   520 */   687,  688,  820,  689,  690,  691,  729,  732,  733,  734,
       
 90665  /*   530 */   692,  711,  714,  715,  693,  700,  694,  695,  702,  703,
       
 90666  /*   540 */   704,  706,  707,  708,  709,  710,  705,  876,  877,  880,
       
 90667  /*   550 */   878,  697,  698,  712,  685,  674,  666,  718,  721,  722,
       
 90668  /*   560 */   723,  724,  725,  727,  719,  720,  664,  655,  658,  767,
       
 90669  /*   570 */   900,  909,  905,  901,  902,  903,  659,  872,  873,  731,
       
 90670  /*   580 */   804,  805,  899,  912,  915,  917,  918,  919,  809,  920,
       
 90671  /*   590 */   922,  913,  947,  656,  735,  736,  739,  881,  929,  795,
       
 90672  /*   600 */   798,  801,  803,  883,  885,  887,  889,  890,  891,  892,
       
 90673  /*   610 */   893,  886,  888,  930,  934,  937,  939,  940,  941,  944,
       
 90674  /*   620 */   946,  951,  952,  953,  956,  957,  954,  636,  631,
       
 90675 };
       
 90676 #define YY_SZ_ACTTAB (int)(sizeof(yy_action)/sizeof(yy_action[0]))
       
 90677 
       
 90678 /* The next table maps tokens into fallback tokens.  If a construct
       
 90679 ** like the following:
       
 90680 ** 
       
 90681 **      %fallback ID X Y Z.
       
 90682 **
       
 90683 ** appears in the grammar, then ID becomes a fallback token for X, Y,
       
 90684 ** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
       
 90685 ** but it does not parse, the type of the token is changed to ID and
       
 90686 ** the parse is retried before an error is thrown.
       
 90687 */
       
 90688 #ifdef YYFALLBACK
       
 90689 static const YYCODETYPE yyFallback[] = {
       
 90690     0,  /*          $ => nothing */
       
 90691     0,  /*       SEMI => nothing */
       
 90692    26,  /*    EXPLAIN => ID */
       
 90693    26,  /*      QUERY => ID */
       
 90694    26,  /*       PLAN => ID */
       
 90695    26,  /*      BEGIN => ID */
       
 90696     0,  /* TRANSACTION => nothing */
       
 90697    26,  /*   DEFERRED => ID */
       
 90698    26,  /*  IMMEDIATE => ID */
       
 90699    26,  /*  EXCLUSIVE => ID */
       
 90700     0,  /*     COMMIT => nothing */
       
 90701    26,  /*        END => ID */
       
 90702    26,  /*   ROLLBACK => ID */
       
 90703    26,  /*  SAVEPOINT => ID */
       
 90704    26,  /*    RELEASE => ID */
       
 90705     0,  /*         TO => nothing */
       
 90706     0,  /*      TABLE => nothing */
       
 90707     0,  /*     CREATE => nothing */
       
 90708    26,  /*         IF => ID */
       
 90709     0,  /*        NOT => nothing */
       
 90710     0,  /*     EXISTS => nothing */
       
 90711    26,  /*       TEMP => ID */
       
 90712     0,  /*         LP => nothing */
       
 90713     0,  /*         RP => nothing */
       
 90714     0,  /*         AS => nothing */
       
 90715     0,  /*      COMMA => nothing */
       
 90716     0,  /*         ID => nothing */
       
 90717     0,  /*    INDEXED => nothing */
       
 90718    26,  /*      ABORT => ID */
       
 90719    26,  /*     ACTION => ID */
       
 90720    26,  /*      AFTER => ID */
       
 90721    26,  /*    ANALYZE => ID */
       
 90722    26,  /*        ASC => ID */
       
 90723    26,  /*     ATTACH => ID */
       
 90724    26,  /*     BEFORE => ID */
       
 90725    26,  /*         BY => ID */
       
 90726    26,  /*    CASCADE => ID */
       
 90727    26,  /*       CAST => ID */
       
 90728    26,  /*   COLUMNKW => ID */
       
 90729    26,  /*   CONFLICT => ID */
       
 90730    26,  /*   DATABASE => ID */
       
 90731    26,  /*       DESC => ID */
       
 90732    26,  /*     DETACH => ID */
       
 90733    26,  /*       EACH => ID */
       
 90734    26,  /*       FAIL => ID */
       
 90735    26,  /*        FOR => ID */
       
 90736    26,  /*     IGNORE => ID */
       
 90737    26,  /*  INITIALLY => ID */
       
 90738    26,  /*    INSTEAD => ID */
       
 90739    26,  /*    LIKE_KW => ID */
       
 90740    26,  /*      MATCH => ID */
       
 90741    26,  /*         NO => ID */
       
 90742    26,  /*        KEY => ID */
       
 90743    26,  /*         OF => ID */
       
 90744    26,  /*     OFFSET => ID */
       
 90745    26,  /*     PRAGMA => ID */
       
 90746    26,  /*      RAISE => ID */
       
 90747    26,  /*    REPLACE => ID */
       
 90748    26,  /*   RESTRICT => ID */
       
 90749    26,  /*        ROW => ID */
       
 90750    26,  /*    TRIGGER => ID */
       
 90751    26,  /*     VACUUM => ID */
       
 90752    26,  /*       VIEW => ID */
       
 90753    26,  /*    VIRTUAL => ID */
       
 90754    26,  /*    REINDEX => ID */
       
 90755    26,  /*     RENAME => ID */
       
 90756    26,  /*   CTIME_KW => ID */
       
 90757 };
       
 90758 #endif /* YYFALLBACK */
       
 90759 
       
 90760 /* The following structure represents a single element of the
       
 90761 ** parser's stack.  Information stored includes:
       
 90762 **
       
 90763 **   +  The state number for the parser at this level of the stack.
       
 90764 **
       
 90765 **   +  The value of the token stored at this level of the stack.
       
 90766 **      (In other words, the "major" token.)
       
 90767 **
       
 90768 **   +  The semantic value stored at this level of the stack.  This is
       
 90769 **      the information used by the action routines in the grammar.
       
 90770 **      It is sometimes called the "minor" token.
       
 90771 */
       
 90772 struct yyStackEntry {
       
 90773   YYACTIONTYPE stateno;  /* The state-number */
       
 90774   YYCODETYPE major;      /* The major token value.  This is the code
       
 90775                          ** number for the token at this stack level */
       
 90776   YYMINORTYPE minor;     /* The user-supplied minor token value.  This
       
 90777                          ** is the value of the token  */
       
 90778 };
       
 90779 typedef struct yyStackEntry yyStackEntry;
       
 90780 
       
 90781 /* The state of the parser is completely contained in an instance of
       
 90782 ** the following structure */
       
 90783 struct yyParser {
       
 90784   int yyidx;                    /* Index of top element in stack */
       
 90785 #ifdef YYTRACKMAXSTACKDEPTH
       
 90786   int yyidxMax;                 /* Maximum value of yyidx */
       
 90787 #endif
       
 90788   int yyerrcnt;                 /* Shifts left before out of the error */
       
 90789   sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
       
 90790 #if YYSTACKDEPTH<=0
       
 90791   int yystksz;                  /* Current side of the stack */
       
 90792   yyStackEntry *yystack;        /* The parser's stack */
       
 90793 #else
       
 90794   yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
       
 90795 #endif
       
 90796 };
       
 90797 typedef struct yyParser yyParser;
       
 90798 
       
 90799 #ifndef NDEBUG
       
 90800 static FILE *yyTraceFILE = 0;
       
 90801 static char *yyTracePrompt = 0;
       
 90802 #endif /* NDEBUG */
       
 90803 
       
 90804 #ifndef NDEBUG
       
 90805 /* 
       
 90806 ** Turn parser tracing on by giving a stream to which to write the trace
       
 90807 ** and a prompt to preface each trace message.  Tracing is turned off
       
 90808 ** by making either argument NULL 
       
 90809 **
       
 90810 ** Inputs:
       
 90811 ** <ul>
       
 90812 ** <li> A FILE* to which trace output should be written.
       
 90813 **      If NULL, then tracing is turned off.
       
 90814 ** <li> A prefix string written at the beginning of every
       
 90815 **      line of trace output.  If NULL, then tracing is
       
 90816 **      turned off.
       
 90817 ** </ul>
       
 90818 **
       
 90819 ** Outputs:
       
 90820 ** None.
       
 90821 */
       
 90822 SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
       
 90823   yyTraceFILE = TraceFILE;
       
 90824   yyTracePrompt = zTracePrompt;
       
 90825   if( yyTraceFILE==0 ) yyTracePrompt = 0;
       
 90826   else if( yyTracePrompt==0 ) yyTraceFILE = 0;
       
 90827 }
       
 90828 #endif /* NDEBUG */
       
 90829 
       
 90830 #ifndef NDEBUG
       
 90831 /* For tracing shifts, the names of all terminals and nonterminals
       
 90832 ** are required.  The following table supplies these names */
       
 90833 static const char *const yyTokenName[] = { 
       
 90834   "$",             "SEMI",          "EXPLAIN",       "QUERY",       
       
 90835   "PLAN",          "BEGIN",         "TRANSACTION",   "DEFERRED",    
       
 90836   "IMMEDIATE",     "EXCLUSIVE",     "COMMIT",        "END",         
       
 90837   "ROLLBACK",      "SAVEPOINT",     "RELEASE",       "TO",          
       
 90838   "TABLE",         "CREATE",        "IF",            "NOT",         
       
 90839   "EXISTS",        "TEMP",          "LP",            "RP",          
       
 90840   "AS",            "COMMA",         "ID",            "INDEXED",     
       
 90841   "ABORT",         "ACTION",        "AFTER",         "ANALYZE",     
       
 90842   "ASC",           "ATTACH",        "BEFORE",        "BY",          
       
 90843   "CASCADE",       "CAST",          "COLUMNKW",      "CONFLICT",    
       
 90844   "DATABASE",      "DESC",          "DETACH",        "EACH",        
       
 90845   "FAIL",          "FOR",           "IGNORE",        "INITIALLY",   
       
 90846   "INSTEAD",       "LIKE_KW",       "MATCH",         "NO",          
       
 90847   "KEY",           "OF",            "OFFSET",        "PRAGMA",      
       
 90848   "RAISE",         "REPLACE",       "RESTRICT",      "ROW",         
       
 90849   "TRIGGER",       "VACUUM",        "VIEW",          "VIRTUAL",     
       
 90850   "REINDEX",       "RENAME",        "CTIME_KW",      "ANY",         
       
 90851   "OR",            "AND",           "IS",            "BETWEEN",     
       
 90852   "IN",            "ISNULL",        "NOTNULL",       "NE",          
       
 90853   "EQ",            "GT",            "LE",            "LT",          
       
 90854   "GE",            "ESCAPE",        "BITAND",        "BITOR",       
       
 90855   "LSHIFT",        "RSHIFT",        "PLUS",          "MINUS",       
       
 90856   "STAR",          "SLASH",         "REM",           "CONCAT",      
       
 90857   "COLLATE",       "BITNOT",        "STRING",        "JOIN_KW",     
       
 90858   "CONSTRAINT",    "DEFAULT",       "NULL",          "PRIMARY",     
       
 90859   "UNIQUE",        "CHECK",         "REFERENCES",    "AUTOINCR",    
       
 90860   "ON",            "DELETE",        "UPDATE",        "SET",         
       
 90861   "DEFERRABLE",    "FOREIGN",       "DROP",          "UNION",       
       
 90862   "ALL",           "EXCEPT",        "INTERSECT",     "SELECT",      
       
 90863   "DISTINCT",      "DOT",           "FROM",          "JOIN",        
       
 90864   "USING",         "ORDER",         "GROUP",         "HAVING",      
       
 90865   "LIMIT",         "WHERE",         "INTO",          "VALUES",      
       
 90866   "INSERT",        "INTEGER",       "FLOAT",         "BLOB",        
       
 90867   "REGISTER",      "VARIABLE",      "CASE",          "WHEN",        
       
 90868   "THEN",          "ELSE",          "INDEX",         "ALTER",       
       
 90869   "ADD",           "error",         "input",         "cmdlist",     
       
 90870   "ecmd",          "explain",       "cmdx",          "cmd",         
       
 90871   "transtype",     "trans_opt",     "nm",            "savepoint_opt",
       
 90872   "create_table",  "create_table_args",  "createkw",      "temp",        
       
 90873   "ifnotexists",   "dbnm",          "columnlist",    "conslist_opt",
       
 90874   "select",        "column",        "columnid",      "type",        
       
 90875   "carglist",      "id",            "ids",           "typetoken",   
       
 90876   "typename",      "signed",        "plus_num",      "minus_num",   
       
 90877   "carg",          "ccons",         "term",          "expr",        
       
 90878   "onconf",        "sortorder",     "autoinc",       "idxlist_opt", 
       
 90879   "refargs",       "defer_subclause",  "refarg",        "refact",      
       
 90880   "init_deferred_pred_opt",  "conslist",      "tcons",         "idxlist",     
       
 90881   "defer_subclause_opt",  "orconf",        "resolvetype",   "raisetype",   
       
 90882   "ifexists",      "fullname",      "oneselect",     "multiselect_op",
       
 90883   "distinct",      "selcollist",    "from",          "where_opt",   
       
 90884   "groupby_opt",   "having_opt",    "orderby_opt",   "limit_opt",   
       
 90885   "sclp",          "as",            "seltablist",    "stl_prefix",  
       
 90886   "joinop",        "indexed_opt",   "on_opt",        "using_opt",   
       
 90887   "joinop2",       "inscollist",    "sortlist",      "sortitem",    
       
 90888   "nexprlist",     "setlist",       "insert_cmd",    "inscollist_opt",
       
 90889   "itemlist",      "exprlist",      "likeop",        "escape",      
       
 90890   "between_op",    "in_op",         "case_operand",  "case_exprlist",
       
 90891   "case_else",     "uniqueflag",    "collate",       "nmnum",       
       
 90892   "plus_opt",      "number",        "trigger_decl",  "trigger_cmd_list",
       
 90893   "trigger_time",  "trigger_event",  "foreach_clause",  "when_clause", 
       
 90894   "trigger_cmd",   "trnm",          "tridxby",       "database_kw_opt",
       
 90895   "key_opt",       "add_column_fullname",  "kwcolumn_opt",  "create_vtab", 
       
 90896   "vtabarglist",   "vtabarg",       "vtabargtoken",  "lp",          
       
 90897   "anylist",     
       
 90898 };
       
 90899 #endif /* NDEBUG */
       
 90900 
       
 90901 #ifndef NDEBUG
       
 90902 /* For tracing reduce actions, the names of all rules are required.
       
 90903 */
       
 90904 static const char *const yyRuleName[] = {
       
 90905  /*   0 */ "input ::= cmdlist",
       
 90906  /*   1 */ "cmdlist ::= cmdlist ecmd",
       
 90907  /*   2 */ "cmdlist ::= ecmd",
       
 90908  /*   3 */ "ecmd ::= SEMI",
       
 90909  /*   4 */ "ecmd ::= explain cmdx SEMI",
       
 90910  /*   5 */ "explain ::=",
       
 90911  /*   6 */ "explain ::= EXPLAIN",
       
 90912  /*   7 */ "explain ::= EXPLAIN QUERY PLAN",
       
 90913  /*   8 */ "cmdx ::= cmd",
       
 90914  /*   9 */ "cmd ::= BEGIN transtype trans_opt",
       
 90915  /*  10 */ "trans_opt ::=",
       
 90916  /*  11 */ "trans_opt ::= TRANSACTION",
       
 90917  /*  12 */ "trans_opt ::= TRANSACTION nm",
       
 90918  /*  13 */ "transtype ::=",
       
 90919  /*  14 */ "transtype ::= DEFERRED",
       
 90920  /*  15 */ "transtype ::= IMMEDIATE",
       
 90921  /*  16 */ "transtype ::= EXCLUSIVE",
       
 90922  /*  17 */ "cmd ::= COMMIT trans_opt",
       
 90923  /*  18 */ "cmd ::= END trans_opt",
       
 90924  /*  19 */ "cmd ::= ROLLBACK trans_opt",
       
 90925  /*  20 */ "savepoint_opt ::= SAVEPOINT",
       
 90926  /*  21 */ "savepoint_opt ::=",
       
 90927  /*  22 */ "cmd ::= SAVEPOINT nm",
       
 90928  /*  23 */ "cmd ::= RELEASE savepoint_opt nm",
       
 90929  /*  24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
       
 90930  /*  25 */ "cmd ::= create_table create_table_args",
       
 90931  /*  26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
       
 90932  /*  27 */ "createkw ::= CREATE",
       
 90933  /*  28 */ "ifnotexists ::=",
       
 90934  /*  29 */ "ifnotexists ::= IF NOT EXISTS",
       
 90935  /*  30 */ "temp ::= TEMP",
       
 90936  /*  31 */ "temp ::=",
       
 90937  /*  32 */ "create_table_args ::= LP columnlist conslist_opt RP",
       
 90938  /*  33 */ "create_table_args ::= AS select",
       
 90939  /*  34 */ "columnlist ::= columnlist COMMA column",
       
 90940  /*  35 */ "columnlist ::= column",
       
 90941  /*  36 */ "column ::= columnid type carglist",
       
 90942  /*  37 */ "columnid ::= nm",
       
 90943  /*  38 */ "id ::= ID",
       
 90944  /*  39 */ "id ::= INDEXED",
       
 90945  /*  40 */ "ids ::= ID|STRING",
       
 90946  /*  41 */ "nm ::= id",
       
 90947  /*  42 */ "nm ::= STRING",
       
 90948  /*  43 */ "nm ::= JOIN_KW",
       
 90949  /*  44 */ "type ::=",
       
 90950  /*  45 */ "type ::= typetoken",
       
 90951  /*  46 */ "typetoken ::= typename",
       
 90952  /*  47 */ "typetoken ::= typename LP signed RP",
       
 90953  /*  48 */ "typetoken ::= typename LP signed COMMA signed RP",
       
 90954  /*  49 */ "typename ::= ids",
       
 90955  /*  50 */ "typename ::= typename ids",
       
 90956  /*  51 */ "signed ::= plus_num",
       
 90957  /*  52 */ "signed ::= minus_num",
       
 90958  /*  53 */ "carglist ::= carglist carg",
       
 90959  /*  54 */ "carglist ::=",
       
 90960  /*  55 */ "carg ::= CONSTRAINT nm ccons",
       
 90961  /*  56 */ "carg ::= ccons",
       
 90962  /*  57 */ "ccons ::= DEFAULT term",
       
 90963  /*  58 */ "ccons ::= DEFAULT LP expr RP",
       
 90964  /*  59 */ "ccons ::= DEFAULT PLUS term",
       
 90965  /*  60 */ "ccons ::= DEFAULT MINUS term",
       
 90966  /*  61 */ "ccons ::= DEFAULT id",
       
 90967  /*  62 */ "ccons ::= NULL onconf",
       
 90968  /*  63 */ "ccons ::= NOT NULL onconf",
       
 90969  /*  64 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
       
 90970  /*  65 */ "ccons ::= UNIQUE onconf",
       
 90971  /*  66 */ "ccons ::= CHECK LP expr RP",
       
 90972  /*  67 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
       
 90973  /*  68 */ "ccons ::= defer_subclause",
       
 90974  /*  69 */ "ccons ::= COLLATE ids",
       
 90975  /*  70 */ "autoinc ::=",
       
 90976  /*  71 */ "autoinc ::= AUTOINCR",
       
 90977  /*  72 */ "refargs ::=",
       
 90978  /*  73 */ "refargs ::= refargs refarg",
       
 90979  /*  74 */ "refarg ::= MATCH nm",
       
 90980  /*  75 */ "refarg ::= ON DELETE refact",
       
 90981  /*  76 */ "refarg ::= ON UPDATE refact",
       
 90982  /*  77 */ "refact ::= SET NULL",
       
 90983  /*  78 */ "refact ::= SET DEFAULT",
       
 90984  /*  79 */ "refact ::= CASCADE",
       
 90985  /*  80 */ "refact ::= RESTRICT",
       
 90986  /*  81 */ "refact ::= NO ACTION",
       
 90987  /*  82 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
       
 90988  /*  83 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
       
 90989  /*  84 */ "init_deferred_pred_opt ::=",
       
 90990  /*  85 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
       
 90991  /*  86 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
       
 90992  /*  87 */ "conslist_opt ::=",
       
 90993  /*  88 */ "conslist_opt ::= COMMA conslist",
       
 90994  /*  89 */ "conslist ::= conslist COMMA tcons",
       
 90995  /*  90 */ "conslist ::= conslist tcons",
       
 90996  /*  91 */ "conslist ::= tcons",
       
 90997  /*  92 */ "tcons ::= CONSTRAINT nm",
       
 90998  /*  93 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
       
 90999  /*  94 */ "tcons ::= UNIQUE LP idxlist RP onconf",
       
 91000  /*  95 */ "tcons ::= CHECK LP expr RP onconf",
       
 91001  /*  96 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
       
 91002  /*  97 */ "defer_subclause_opt ::=",
       
 91003  /*  98 */ "defer_subclause_opt ::= defer_subclause",
       
 91004  /*  99 */ "onconf ::=",
       
 91005  /* 100 */ "onconf ::= ON CONFLICT resolvetype",
       
 91006  /* 101 */ "orconf ::=",
       
 91007  /* 102 */ "orconf ::= OR resolvetype",
       
 91008  /* 103 */ "resolvetype ::= raisetype",
       
 91009  /* 104 */ "resolvetype ::= IGNORE",
       
 91010  /* 105 */ "resolvetype ::= REPLACE",
       
 91011  /* 106 */ "cmd ::= DROP TABLE ifexists fullname",
       
 91012  /* 107 */ "ifexists ::= IF EXISTS",
       
 91013  /* 108 */ "ifexists ::=",
       
 91014  /* 109 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select",
       
 91015  /* 110 */ "cmd ::= DROP VIEW ifexists fullname",
       
 91016  /* 111 */ "cmd ::= select",
       
 91017  /* 112 */ "select ::= oneselect",
       
 91018  /* 113 */ "select ::= select multiselect_op oneselect",
       
 91019  /* 114 */ "multiselect_op ::= UNION",
       
 91020  /* 115 */ "multiselect_op ::= UNION ALL",
       
 91021  /* 116 */ "multiselect_op ::= EXCEPT|INTERSECT",
       
 91022  /* 117 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
       
 91023  /* 118 */ "distinct ::= DISTINCT",
       
 91024  /* 119 */ "distinct ::= ALL",
       
 91025  /* 120 */ "distinct ::=",
       
 91026  /* 121 */ "sclp ::= selcollist COMMA",
       
 91027  /* 122 */ "sclp ::=",
       
 91028  /* 123 */ "selcollist ::= sclp expr as",
       
 91029  /* 124 */ "selcollist ::= sclp STAR",
       
 91030  /* 125 */ "selcollist ::= sclp nm DOT STAR",
       
 91031  /* 126 */ "as ::= AS nm",
       
 91032  /* 127 */ "as ::= ids",
       
 91033  /* 128 */ "as ::=",
       
 91034  /* 129 */ "from ::=",
       
 91035  /* 130 */ "from ::= FROM seltablist",
       
 91036  /* 131 */ "stl_prefix ::= seltablist joinop",
       
 91037  /* 132 */ "stl_prefix ::=",
       
 91038  /* 133 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
       
 91039  /* 134 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
       
 91040  /* 135 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
       
 91041  /* 136 */ "dbnm ::=",
       
 91042  /* 137 */ "dbnm ::= DOT nm",
       
 91043  /* 138 */ "fullname ::= nm dbnm",
       
 91044  /* 139 */ "joinop ::= COMMA|JOIN",
       
 91045  /* 140 */ "joinop ::= JOIN_KW JOIN",
       
 91046  /* 141 */ "joinop ::= JOIN_KW nm JOIN",
       
 91047  /* 142 */ "joinop ::= JOIN_KW nm nm JOIN",
       
 91048  /* 143 */ "on_opt ::= ON expr",
       
 91049  /* 144 */ "on_opt ::=",
       
 91050  /* 145 */ "indexed_opt ::=",
       
 91051  /* 146 */ "indexed_opt ::= INDEXED BY nm",
       
 91052  /* 147 */ "indexed_opt ::= NOT INDEXED",
       
 91053  /* 148 */ "using_opt ::= USING LP inscollist RP",
       
 91054  /* 149 */ "using_opt ::=",
       
 91055  /* 150 */ "orderby_opt ::=",
       
 91056  /* 151 */ "orderby_opt ::= ORDER BY sortlist",
       
 91057  /* 152 */ "sortlist ::= sortlist COMMA sortitem sortorder",
       
 91058  /* 153 */ "sortlist ::= sortitem sortorder",
       
 91059  /* 154 */ "sortitem ::= expr",
       
 91060  /* 155 */ "sortorder ::= ASC",
       
 91061  /* 156 */ "sortorder ::= DESC",
       
 91062  /* 157 */ "sortorder ::=",
       
 91063  /* 158 */ "groupby_opt ::=",
       
 91064  /* 159 */ "groupby_opt ::= GROUP BY nexprlist",
       
 91065  /* 160 */ "having_opt ::=",
       
 91066  /* 161 */ "having_opt ::= HAVING expr",
       
 91067  /* 162 */ "limit_opt ::=",
       
 91068  /* 163 */ "limit_opt ::= LIMIT expr",
       
 91069  /* 164 */ "limit_opt ::= LIMIT expr OFFSET expr",
       
 91070  /* 165 */ "limit_opt ::= LIMIT expr COMMA expr",
       
 91071  /* 166 */ "cmd ::= DELETE FROM fullname indexed_opt where_opt",
       
 91072  /* 167 */ "where_opt ::=",
       
 91073  /* 168 */ "where_opt ::= WHERE expr",
       
 91074  /* 169 */ "cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt",
       
 91075  /* 170 */ "setlist ::= setlist COMMA nm EQ expr",
       
 91076  /* 171 */ "setlist ::= nm EQ expr",
       
 91077  /* 172 */ "cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP",
       
 91078  /* 173 */ "cmd ::= insert_cmd INTO fullname inscollist_opt select",
       
 91079  /* 174 */ "cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES",
       
 91080  /* 175 */ "insert_cmd ::= INSERT orconf",
       
 91081  /* 176 */ "insert_cmd ::= REPLACE",
       
 91082  /* 177 */ "itemlist ::= itemlist COMMA expr",
       
 91083  /* 178 */ "itemlist ::= expr",
       
 91084  /* 179 */ "inscollist_opt ::=",
       
 91085  /* 180 */ "inscollist_opt ::= LP inscollist RP",
       
 91086  /* 181 */ "inscollist ::= inscollist COMMA nm",
       
 91087  /* 182 */ "inscollist ::= nm",
       
 91088  /* 183 */ "expr ::= term",
       
 91089  /* 184 */ "expr ::= LP expr RP",
       
 91090  /* 185 */ "term ::= NULL",
       
 91091  /* 186 */ "expr ::= id",
       
 91092  /* 187 */ "expr ::= JOIN_KW",
       
 91093  /* 188 */ "expr ::= nm DOT nm",
       
 91094  /* 189 */ "expr ::= nm DOT nm DOT nm",
       
 91095  /* 190 */ "term ::= INTEGER|FLOAT|BLOB",
       
 91096  /* 191 */ "term ::= STRING",
       
 91097  /* 192 */ "expr ::= REGISTER",
       
 91098  /* 193 */ "expr ::= VARIABLE",
       
 91099  /* 194 */ "expr ::= expr COLLATE ids",
       
 91100  /* 195 */ "expr ::= CAST LP expr AS typetoken RP",
       
 91101  /* 196 */ "expr ::= ID LP distinct exprlist RP",
       
 91102  /* 197 */ "expr ::= ID LP STAR RP",
       
 91103  /* 198 */ "term ::= CTIME_KW",
       
 91104  /* 199 */ "expr ::= expr AND expr",
       
 91105  /* 200 */ "expr ::= expr OR expr",
       
 91106  /* 201 */ "expr ::= expr LT|GT|GE|LE expr",
       
 91107  /* 202 */ "expr ::= expr EQ|NE expr",
       
 91108  /* 203 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
       
 91109  /* 204 */ "expr ::= expr PLUS|MINUS expr",
       
 91110  /* 205 */ "expr ::= expr STAR|SLASH|REM expr",
       
 91111  /* 206 */ "expr ::= expr CONCAT expr",
       
 91112  /* 207 */ "likeop ::= LIKE_KW",
       
 91113  /* 208 */ "likeop ::= NOT LIKE_KW",
       
 91114  /* 209 */ "likeop ::= MATCH",
       
 91115  /* 210 */ "likeop ::= NOT MATCH",
       
 91116  /* 211 */ "escape ::= ESCAPE expr",
       
 91117  /* 212 */ "escape ::=",
       
 91118  /* 213 */ "expr ::= expr likeop expr escape",
       
 91119  /* 214 */ "expr ::= expr ISNULL|NOTNULL",
       
 91120  /* 215 */ "expr ::= expr NOT NULL",
       
 91121  /* 216 */ "expr ::= expr IS expr",
       
 91122  /* 217 */ "expr ::= expr IS NOT expr",
       
 91123  /* 218 */ "expr ::= NOT expr",
       
 91124  /* 219 */ "expr ::= BITNOT expr",
       
 91125  /* 220 */ "expr ::= MINUS expr",
       
 91126  /* 221 */ "expr ::= PLUS expr",
       
 91127  /* 222 */ "between_op ::= BETWEEN",
       
 91128  /* 223 */ "between_op ::= NOT BETWEEN",
       
 91129  /* 224 */ "expr ::= expr between_op expr AND expr",
       
 91130  /* 225 */ "in_op ::= IN",
       
 91131  /* 226 */ "in_op ::= NOT IN",
       
 91132  /* 227 */ "expr ::= expr in_op LP exprlist RP",
       
 91133  /* 228 */ "expr ::= LP select RP",
       
 91134  /* 229 */ "expr ::= expr in_op LP select RP",
       
 91135  /* 230 */ "expr ::= expr in_op nm dbnm",
       
 91136  /* 231 */ "expr ::= EXISTS LP select RP",
       
 91137  /* 232 */ "expr ::= CASE case_operand case_exprlist case_else END",
       
 91138  /* 233 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
       
 91139  /* 234 */ "case_exprlist ::= WHEN expr THEN expr",
       
 91140  /* 235 */ "case_else ::= ELSE expr",
       
 91141  /* 236 */ "case_else ::=",
       
 91142  /* 237 */ "case_operand ::= expr",
       
 91143  /* 238 */ "case_operand ::=",
       
 91144  /* 239 */ "exprlist ::= nexprlist",
       
 91145  /* 240 */ "exprlist ::=",
       
 91146  /* 241 */ "nexprlist ::= nexprlist COMMA expr",
       
 91147  /* 242 */ "nexprlist ::= expr",
       
 91148  /* 243 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP",
       
 91149  /* 244 */ "uniqueflag ::= UNIQUE",
       
 91150  /* 245 */ "uniqueflag ::=",
       
 91151  /* 246 */ "idxlist_opt ::=",
       
 91152  /* 247 */ "idxlist_opt ::= LP idxlist RP",
       
 91153  /* 248 */ "idxlist ::= idxlist COMMA nm collate sortorder",
       
 91154  /* 249 */ "idxlist ::= nm collate sortorder",
       
 91155  /* 250 */ "collate ::=",
       
 91156  /* 251 */ "collate ::= COLLATE ids",
       
 91157  /* 252 */ "cmd ::= DROP INDEX ifexists fullname",
       
 91158  /* 253 */ "cmd ::= VACUUM",
       
 91159  /* 254 */ "cmd ::= VACUUM nm",
       
 91160  /* 255 */ "cmd ::= PRAGMA nm dbnm",
       
 91161  /* 256 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
       
 91162  /* 257 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
       
 91163  /* 258 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
       
 91164  /* 259 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
       
 91165  /* 260 */ "nmnum ::= plus_num",
       
 91166  /* 261 */ "nmnum ::= nm",
       
 91167  /* 262 */ "nmnum ::= ON",
       
 91168  /* 263 */ "nmnum ::= DELETE",
       
 91169  /* 264 */ "nmnum ::= DEFAULT",
       
 91170  /* 265 */ "plus_num ::= plus_opt number",
       
 91171  /* 266 */ "minus_num ::= MINUS number",
       
 91172  /* 267 */ "number ::= INTEGER|FLOAT",
       
 91173  /* 268 */ "plus_opt ::= PLUS",
       
 91174  /* 269 */ "plus_opt ::=",
       
 91175  /* 270 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
       
 91176  /* 271 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
       
 91177  /* 272 */ "trigger_time ::= BEFORE",
       
 91178  /* 273 */ "trigger_time ::= AFTER",
       
 91179  /* 274 */ "trigger_time ::= INSTEAD OF",
       
 91180  /* 275 */ "trigger_time ::=",
       
 91181  /* 276 */ "trigger_event ::= DELETE|INSERT",
       
 91182  /* 277 */ "trigger_event ::= UPDATE",
       
 91183  /* 278 */ "trigger_event ::= UPDATE OF inscollist",
       
 91184  /* 279 */ "foreach_clause ::=",
       
 91185  /* 280 */ "foreach_clause ::= FOR EACH ROW",
       
 91186  /* 281 */ "when_clause ::=",
       
 91187  /* 282 */ "when_clause ::= WHEN expr",
       
 91188  /* 283 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
       
 91189  /* 284 */ "trigger_cmd_list ::= trigger_cmd SEMI",
       
 91190  /* 285 */ "trnm ::= nm",
       
 91191  /* 286 */ "trnm ::= nm DOT nm",
       
 91192  /* 287 */ "tridxby ::=",
       
 91193  /* 288 */ "tridxby ::= INDEXED BY nm",
       
 91194  /* 289 */ "tridxby ::= NOT INDEXED",
       
 91195  /* 290 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
       
 91196  /* 291 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt VALUES LP itemlist RP",
       
 91197  /* 292 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select",
       
 91198  /* 293 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
       
 91199  /* 294 */ "trigger_cmd ::= select",
       
 91200  /* 295 */ "expr ::= RAISE LP IGNORE RP",
       
 91201  /* 296 */ "expr ::= RAISE LP raisetype COMMA nm RP",
       
 91202  /* 297 */ "raisetype ::= ROLLBACK",
       
 91203  /* 298 */ "raisetype ::= ABORT",
       
 91204  /* 299 */ "raisetype ::= FAIL",
       
 91205  /* 300 */ "cmd ::= DROP TRIGGER ifexists fullname",
       
 91206  /* 301 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
       
 91207  /* 302 */ "cmd ::= DETACH database_kw_opt expr",
       
 91208  /* 303 */ "key_opt ::=",
       
 91209  /* 304 */ "key_opt ::= KEY expr",
       
 91210  /* 305 */ "database_kw_opt ::= DATABASE",
       
 91211  /* 306 */ "database_kw_opt ::=",
       
 91212  /* 307 */ "cmd ::= REINDEX",
       
 91213  /* 308 */ "cmd ::= REINDEX nm dbnm",
       
 91214  /* 309 */ "cmd ::= ANALYZE",
       
 91215  /* 310 */ "cmd ::= ANALYZE nm dbnm",
       
 91216  /* 311 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
       
 91217  /* 312 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
       
 91218  /* 313 */ "add_column_fullname ::= fullname",
       
 91219  /* 314 */ "kwcolumn_opt ::=",
       
 91220  /* 315 */ "kwcolumn_opt ::= COLUMNKW",
       
 91221  /* 316 */ "cmd ::= create_vtab",
       
 91222  /* 317 */ "cmd ::= create_vtab LP vtabarglist RP",
       
 91223  /* 318 */ "create_vtab ::= createkw VIRTUAL TABLE nm dbnm USING nm",
       
 91224  /* 319 */ "vtabarglist ::= vtabarg",
       
 91225  /* 320 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
       
 91226  /* 321 */ "vtabarg ::=",
       
 91227  /* 322 */ "vtabarg ::= vtabarg vtabargtoken",
       
 91228  /* 323 */ "vtabargtoken ::= ANY",
       
 91229  /* 324 */ "vtabargtoken ::= lp anylist RP",
       
 91230  /* 325 */ "lp ::= LP",
       
 91231  /* 326 */ "anylist ::=",
       
 91232  /* 327 */ "anylist ::= anylist LP anylist RP",
       
 91233  /* 328 */ "anylist ::= anylist ANY",
       
 91234 };
       
 91235 #endif /* NDEBUG */
       
 91236 
       
 91237 
       
 91238 #if YYSTACKDEPTH<=0
       
 91239 /*
       
 91240 ** Try to increase the size of the parser stack.
       
 91241 */
       
 91242 static void yyGrowStack(yyParser *p){
       
 91243   int newSize;
       
 91244   yyStackEntry *pNew;
       
 91245 
       
 91246   newSize = p->yystksz*2 + 100;
       
 91247   pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
       
 91248   if( pNew ){
       
 91249     p->yystack = pNew;
       
 91250     p->yystksz = newSize;
       
 91251 #ifndef NDEBUG
       
 91252     if( yyTraceFILE ){
       
 91253       fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
       
 91254               yyTracePrompt, p->yystksz);
       
 91255     }
       
 91256 #endif
       
 91257   }
       
 91258 }
       
 91259 #endif
       
 91260 
       
 91261 /* 
       
 91262 ** This function allocates a new parser.
       
 91263 ** The only argument is a pointer to a function which works like
       
 91264 ** malloc.
       
 91265 **
       
 91266 ** Inputs:
       
 91267 ** A pointer to the function used to allocate memory.
       
 91268 **
       
 91269 ** Outputs:
       
 91270 ** A pointer to a parser.  This pointer is used in subsequent calls
       
 91271 ** to sqlite3Parser and sqlite3ParserFree.
       
 91272 */
       
 91273 SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(size_t)){
       
 91274   yyParser *pParser;
       
 91275   pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
       
 91276   if( pParser ){
       
 91277     pParser->yyidx = -1;
       
 91278 #ifdef YYTRACKMAXSTACKDEPTH
       
 91279     pParser->yyidxMax = 0;
       
 91280 #endif
       
 91281 #if YYSTACKDEPTH<=0
       
 91282     pParser->yystack = NULL;
       
 91283     pParser->yystksz = 0;
       
 91284     yyGrowStack(pParser);
       
 91285 #endif
       
 91286   }
       
 91287   return pParser;
       
 91288 }
       
 91289 
       
 91290 /* The following function deletes the value associated with a
       
 91291 ** symbol.  The symbol can be either a terminal or nonterminal.
       
 91292 ** "yymajor" is the symbol code, and "yypminor" is a pointer to
       
 91293 ** the value.
       
 91294 */
       
 91295 static void yy_destructor(
       
 91296   yyParser *yypParser,    /* The parser */
       
 91297   YYCODETYPE yymajor,     /* Type code for object to destroy */
       
 91298   YYMINORTYPE *yypminor   /* The object to be destroyed */
       
 91299 ){
       
 91300   sqlite3ParserARG_FETCH;
       
 91301   switch( yymajor ){
       
 91302     /* Here is inserted the actions which take place when a
       
 91303     ** terminal or non-terminal is destroyed.  This can happen
       
 91304     ** when the symbol is popped from the stack during a
       
 91305     ** reduce or during error processing or when a parser is 
       
 91306     ** being destroyed before it is finished parsing.
       
 91307     **
       
 91308     ** Note: during a reduce, the only symbols destroyed are those
       
 91309     ** which appear on the RHS of the rule, but which are not used
       
 91310     ** inside the C code.
       
 91311     */
       
 91312     case 160: /* select */
       
 91313     case 194: /* oneselect */
       
 91314 {
       
 91315 sqlite3SelectDelete(pParse->db, (yypminor->yy3));
       
 91316 }
       
 91317       break;
       
 91318     case 174: /* term */
       
 91319     case 175: /* expr */
       
 91320     case 223: /* escape */
       
 91321 {
       
 91322 sqlite3ExprDelete(pParse->db, (yypminor->yy346).pExpr);
       
 91323 }
       
 91324       break;
       
 91325     case 179: /* idxlist_opt */
       
 91326     case 187: /* idxlist */
       
 91327     case 197: /* selcollist */
       
 91328     case 200: /* groupby_opt */
       
 91329     case 202: /* orderby_opt */
       
 91330     case 204: /* sclp */
       
 91331     case 214: /* sortlist */
       
 91332     case 216: /* nexprlist */
       
 91333     case 217: /* setlist */
       
 91334     case 220: /* itemlist */
       
 91335     case 221: /* exprlist */
       
 91336     case 227: /* case_exprlist */
       
 91337 {
       
 91338 sqlite3ExprListDelete(pParse->db, (yypminor->yy14));
       
 91339 }
       
 91340       break;
       
 91341     case 193: /* fullname */
       
 91342     case 198: /* from */
       
 91343     case 206: /* seltablist */
       
 91344     case 207: /* stl_prefix */
       
 91345 {
       
 91346 sqlite3SrcListDelete(pParse->db, (yypminor->yy65));
       
 91347 }
       
 91348       break;
       
 91349     case 199: /* where_opt */
       
 91350     case 201: /* having_opt */
       
 91351     case 210: /* on_opt */
       
 91352     case 215: /* sortitem */
       
 91353     case 226: /* case_operand */
       
 91354     case 228: /* case_else */
       
 91355     case 239: /* when_clause */
       
 91356     case 244: /* key_opt */
       
 91357 {
       
 91358 sqlite3ExprDelete(pParse->db, (yypminor->yy132));
       
 91359 }
       
 91360       break;
       
 91361     case 211: /* using_opt */
       
 91362     case 213: /* inscollist */
       
 91363     case 219: /* inscollist_opt */
       
 91364 {
       
 91365 sqlite3IdListDelete(pParse->db, (yypminor->yy408));
       
 91366 }
       
 91367       break;
       
 91368     case 235: /* trigger_cmd_list */
       
 91369     case 240: /* trigger_cmd */
       
 91370 {
       
 91371 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy473));
       
 91372 }
       
 91373       break;
       
 91374     case 237: /* trigger_event */
       
 91375 {
       
 91376 sqlite3IdListDelete(pParse->db, (yypminor->yy378).b);
       
 91377 }
       
 91378       break;
       
 91379     default:  break;   /* If no destructor action specified: do nothing */
       
 91380   }
       
 91381 }
       
 91382 
       
 91383 /*
       
 91384 ** Pop the parser's stack once.
       
 91385 **
       
 91386 ** If there is a destructor routine associated with the token which
       
 91387 ** is popped from the stack, then call it.
       
 91388 **
       
 91389 ** Return the major token number for the symbol popped.
       
 91390 */
       
 91391 static int yy_pop_parser_stack(yyParser *pParser){
       
 91392   YYCODETYPE yymajor;
       
 91393   yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
       
 91394 
       
 91395   /* There is no mechanism by which the parser stack can be popped below
       
 91396   ** empty in SQLite.  */
       
 91397   if( NEVER(pParser->yyidx<0) ) return 0;
       
 91398 #ifndef NDEBUG
       
 91399   if( yyTraceFILE && pParser->yyidx>=0 ){
       
 91400     fprintf(yyTraceFILE,"%sPopping %s\n",
       
 91401       yyTracePrompt,
       
 91402       yyTokenName[yytos->major]);
       
 91403   }
       
 91404 #endif
       
 91405   yymajor = yytos->major;
       
 91406   yy_destructor(pParser, yymajor, &yytos->minor);
       
 91407   pParser->yyidx--;
       
 91408   return yymajor;
       
 91409 }
       
 91410 
       
 91411 /* 
       
 91412 ** Deallocate and destroy a parser.  Destructors are all called for
       
 91413 ** all stack elements before shutting the parser down.
       
 91414 **
       
 91415 ** Inputs:
       
 91416 ** <ul>
       
 91417 ** <li>  A pointer to the parser.  This should be a pointer
       
 91418 **       obtained from sqlite3ParserAlloc.
       
 91419 ** <li>  A pointer to a function used to reclaim memory obtained
       
 91420 **       from malloc.
       
 91421 ** </ul>
       
 91422 */
       
 91423 SQLITE_PRIVATE void sqlite3ParserFree(
       
 91424   void *p,                    /* The parser to be deleted */
       
 91425   void (*freeProc)(void*)     /* Function used to reclaim memory */
       
 91426 ){
       
 91427   yyParser *pParser = (yyParser*)p;
       
 91428   /* In SQLite, we never try to destroy a parser that was not successfully
       
 91429   ** created in the first place. */
       
 91430   if( NEVER(pParser==0) ) return;
       
 91431   while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
       
 91432 #if YYSTACKDEPTH<=0
       
 91433   free(pParser->yystack);
       
 91434 #endif
       
 91435   (*freeProc)((void*)pParser);
       
 91436 }
       
 91437 
       
 91438 /*
       
 91439 ** Return the peak depth of the stack for a parser.
       
 91440 */
       
 91441 #ifdef YYTRACKMAXSTACKDEPTH
       
 91442 SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
       
 91443   yyParser *pParser = (yyParser*)p;
       
 91444   return pParser->yyidxMax;
       
 91445 }
       
 91446 #endif
       
 91447 
       
 91448 /*
       
 91449 ** Find the appropriate action for a parser given the terminal
       
 91450 ** look-ahead token iLookAhead.
       
 91451 **
       
 91452 ** If the look-ahead token is YYNOCODE, then check to see if the action is
       
 91453 ** independent of the look-ahead.  If it is, return the action, otherwise
       
 91454 ** return YY_NO_ACTION.
       
 91455 */
       
 91456 static int yy_find_shift_action(
       
 91457   yyParser *pParser,        /* The parser */
       
 91458   YYCODETYPE iLookAhead     /* The look-ahead token */
       
 91459 ){
       
 91460   int i;
       
 91461   int stateno = pParser->yystack[pParser->yyidx].stateno;
       
 91462  
       
 91463   if( stateno>YY_SHIFT_MAX || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
       
 91464     return yy_default[stateno];
       
 91465   }
       
 91466   assert( iLookAhead!=YYNOCODE );
       
 91467   i += iLookAhead;
       
 91468   if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
       
 91469     /* The user of ";" instead of "\000" as a statement terminator in SQLite
       
 91470     ** means that we always have a look-ahead token. */
       
 91471     if( iLookAhead>0 ){
       
 91472 #ifdef YYFALLBACK
       
 91473       YYCODETYPE iFallback;            /* Fallback token */
       
 91474       if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
       
 91475              && (iFallback = yyFallback[iLookAhead])!=0 ){
       
 91476 #ifndef NDEBUG
       
 91477         if( yyTraceFILE ){
       
 91478           fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
       
 91479              yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
       
 91480         }
       
 91481 #endif
       
 91482         return yy_find_shift_action(pParser, iFallback);
       
 91483       }
       
 91484 #endif
       
 91485 #ifdef YYWILDCARD
       
 91486       {
       
 91487         int j = i - iLookAhead + YYWILDCARD;
       
 91488         if( j>=0 && j<YY_SZ_ACTTAB && yy_lookahead[j]==YYWILDCARD ){
       
 91489 #ifndef NDEBUG
       
 91490           if( yyTraceFILE ){
       
 91491             fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
       
 91492                yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
       
 91493           }
       
 91494 #endif /* NDEBUG */
       
 91495           return yy_action[j];
       
 91496         }
       
 91497       }
       
 91498 #endif /* YYWILDCARD */
       
 91499     }
       
 91500     return yy_default[stateno];
       
 91501   }else{
       
 91502     return yy_action[i];
       
 91503   }
       
 91504 }
       
 91505 
       
 91506 /*
       
 91507 ** Find the appropriate action for a parser given the non-terminal
       
 91508 ** look-ahead token iLookAhead.
       
 91509 **
       
 91510 ** If the look-ahead token is YYNOCODE, then check to see if the action is
       
 91511 ** independent of the look-ahead.  If it is, return the action, otherwise
       
 91512 ** return YY_NO_ACTION.
       
 91513 */
       
 91514 static int yy_find_reduce_action(
       
 91515   int stateno,              /* Current state number */
       
 91516   YYCODETYPE iLookAhead     /* The look-ahead token */
       
 91517 ){
       
 91518   int i;
       
 91519 #ifdef YYERRORSYMBOL
       
 91520   if( stateno>YY_REDUCE_MAX ){
       
 91521     return yy_default[stateno];
       
 91522   }
       
 91523 #else
       
 91524   assert( stateno<=YY_REDUCE_MAX );
       
 91525 #endif
       
 91526   i = yy_reduce_ofst[stateno];
       
 91527   assert( i!=YY_REDUCE_USE_DFLT );
       
 91528   assert( iLookAhead!=YYNOCODE );
       
 91529   i += iLookAhead;
       
 91530 #ifdef YYERRORSYMBOL
       
 91531   if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
       
 91532     return yy_default[stateno];
       
 91533   }
       
 91534 #else
       
 91535   assert( i>=0 && i<YY_SZ_ACTTAB );
       
 91536   assert( yy_lookahead[i]==iLookAhead );
       
 91537 #endif
       
 91538   return yy_action[i];
       
 91539 }
       
 91540 
       
 91541 /*
       
 91542 ** The following routine is called if the stack overflows.
       
 91543 */
       
 91544 static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
       
 91545    sqlite3ParserARG_FETCH;
       
 91546    yypParser->yyidx--;
       
 91547 #ifndef NDEBUG
       
 91548    if( yyTraceFILE ){
       
 91549      fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
       
 91550    }
       
 91551 #endif
       
 91552    while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
       
 91553    /* Here code is inserted which will execute if the parser
       
 91554    ** stack every overflows */
       
 91555 
       
 91556   UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
       
 91557   sqlite3ErrorMsg(pParse, "parser stack overflow");
       
 91558   pParse->parseError = 1;
       
 91559    sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
       
 91560 }
       
 91561 
       
 91562 /*
       
 91563 ** Perform a shift action.
       
 91564 */
       
 91565 static void yy_shift(
       
 91566   yyParser *yypParser,          /* The parser to be shifted */
       
 91567   int yyNewState,               /* The new state to shift in */
       
 91568   int yyMajor,                  /* The major token to shift in */
       
 91569   YYMINORTYPE *yypMinor         /* Pointer to the minor token to shift in */
       
 91570 ){
       
 91571   yyStackEntry *yytos;
       
 91572   yypParser->yyidx++;
       
 91573 #ifdef YYTRACKMAXSTACKDEPTH
       
 91574   if( yypParser->yyidx>yypParser->yyidxMax ){
       
 91575     yypParser->yyidxMax = yypParser->yyidx;
       
 91576   }
       
 91577 #endif
       
 91578 #if YYSTACKDEPTH>0 
       
 91579   if( yypParser->yyidx>=YYSTACKDEPTH ){
       
 91580     yyStackOverflow(yypParser, yypMinor);
       
 91581     return;
       
 91582   }
       
 91583 #else
       
 91584   if( yypParser->yyidx>=yypParser->yystksz ){
       
 91585     yyGrowStack(yypParser);
       
 91586     if( yypParser->yyidx>=yypParser->yystksz ){
       
 91587       yyStackOverflow(yypParser, yypMinor);
       
 91588       return;
       
 91589     }
       
 91590   }
       
 91591 #endif
       
 91592   yytos = &yypParser->yystack[yypParser->yyidx];
       
 91593   yytos->stateno = (YYACTIONTYPE)yyNewState;
       
 91594   yytos->major = (YYCODETYPE)yyMajor;
       
 91595   yytos->minor = *yypMinor;
       
 91596 #ifndef NDEBUG
       
 91597   if( yyTraceFILE && yypParser->yyidx>0 ){
       
 91598     int i;
       
 91599     fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
       
 91600     fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
       
 91601     for(i=1; i<=yypParser->yyidx; i++)
       
 91602       fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
       
 91603     fprintf(yyTraceFILE,"\n");
       
 91604   }
       
 91605 #endif
       
 91606 }
       
 91607 
       
 91608 /* The following table contains information about every rule that
       
 91609 ** is used during the reduce.
       
 91610 */
       
 91611 static const struct {
       
 91612   YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
       
 91613   unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
       
 91614 } yyRuleInfo[] = {
       
 91615   { 142, 1 },
       
 91616   { 143, 2 },
       
 91617   { 143, 1 },
       
 91618   { 144, 1 },
       
 91619   { 144, 3 },
       
 91620   { 145, 0 },
       
 91621   { 145, 1 },
       
 91622   { 145, 3 },
       
 91623   { 146, 1 },
       
 91624   { 147, 3 },
       
 91625   { 149, 0 },
       
 91626   { 149, 1 },
       
 91627   { 149, 2 },
       
 91628   { 148, 0 },
       
 91629   { 148, 1 },
       
 91630   { 148, 1 },
       
 91631   { 148, 1 },
       
 91632   { 147, 2 },
       
 91633   { 147, 2 },
       
 91634   { 147, 2 },
       
 91635   { 151, 1 },
       
 91636   { 151, 0 },
       
 91637   { 147, 2 },
       
 91638   { 147, 3 },
       
 91639   { 147, 5 },
       
 91640   { 147, 2 },
       
 91641   { 152, 6 },
       
 91642   { 154, 1 },
       
 91643   { 156, 0 },
       
 91644   { 156, 3 },
       
 91645   { 155, 1 },
       
 91646   { 155, 0 },
       
 91647   { 153, 4 },
       
 91648   { 153, 2 },
       
 91649   { 158, 3 },
       
 91650   { 158, 1 },
       
 91651   { 161, 3 },
       
 91652   { 162, 1 },
       
 91653   { 165, 1 },
       
 91654   { 165, 1 },
       
 91655   { 166, 1 },
       
 91656   { 150, 1 },
       
 91657   { 150, 1 },
       
 91658   { 150, 1 },
       
 91659   { 163, 0 },
       
 91660   { 163, 1 },
       
 91661   { 167, 1 },
       
 91662   { 167, 4 },
       
 91663   { 167, 6 },
       
 91664   { 168, 1 },
       
 91665   { 168, 2 },
       
 91666   { 169, 1 },
       
 91667   { 169, 1 },
       
 91668   { 164, 2 },
       
 91669   { 164, 0 },
       
 91670   { 172, 3 },
       
 91671   { 172, 1 },
       
 91672   { 173, 2 },
       
 91673   { 173, 4 },
       
 91674   { 173, 3 },
       
 91675   { 173, 3 },
       
 91676   { 173, 2 },
       
 91677   { 173, 2 },
       
 91678   { 173, 3 },
       
 91679   { 173, 5 },
       
 91680   { 173, 2 },
       
 91681   { 173, 4 },
       
 91682   { 173, 4 },
       
 91683   { 173, 1 },
       
 91684   { 173, 2 },
       
 91685   { 178, 0 },
       
 91686   { 178, 1 },
       
 91687   { 180, 0 },
       
 91688   { 180, 2 },
       
 91689   { 182, 2 },
       
 91690   { 182, 3 },
       
 91691   { 182, 3 },
       
 91692   { 183, 2 },
       
 91693   { 183, 2 },
       
 91694   { 183, 1 },
       
 91695   { 183, 1 },
       
 91696   { 183, 2 },
       
 91697   { 181, 3 },
       
 91698   { 181, 2 },
       
 91699   { 184, 0 },
       
 91700   { 184, 2 },
       
 91701   { 184, 2 },
       
 91702   { 159, 0 },
       
 91703   { 159, 2 },
       
 91704   { 185, 3 },
       
 91705   { 185, 2 },
       
 91706   { 185, 1 },
       
 91707   { 186, 2 },
       
 91708   { 186, 7 },
       
 91709   { 186, 5 },
       
 91710   { 186, 5 },
       
 91711   { 186, 10 },
       
 91712   { 188, 0 },
       
 91713   { 188, 1 },
       
 91714   { 176, 0 },
       
 91715   { 176, 3 },
       
 91716   { 189, 0 },
       
 91717   { 189, 2 },
       
 91718   { 190, 1 },
       
 91719   { 190, 1 },
       
 91720   { 190, 1 },
       
 91721   { 147, 4 },
       
 91722   { 192, 2 },
       
 91723   { 192, 0 },
       
 91724   { 147, 8 },
       
 91725   { 147, 4 },
       
 91726   { 147, 1 },
       
 91727   { 160, 1 },
       
 91728   { 160, 3 },
       
 91729   { 195, 1 },
       
 91730   { 195, 2 },
       
 91731   { 195, 1 },
       
 91732   { 194, 9 },
       
 91733   { 196, 1 },
       
 91734   { 196, 1 },
       
 91735   { 196, 0 },
       
 91736   { 204, 2 },
       
 91737   { 204, 0 },
       
 91738   { 197, 3 },
       
 91739   { 197, 2 },
       
 91740   { 197, 4 },
       
 91741   { 205, 2 },
       
 91742   { 205, 1 },
       
 91743   { 205, 0 },
       
 91744   { 198, 0 },
       
 91745   { 198, 2 },
       
 91746   { 207, 2 },
       
 91747   { 207, 0 },
       
 91748   { 206, 7 },
       
 91749   { 206, 7 },
       
 91750   { 206, 7 },
       
 91751   { 157, 0 },
       
 91752   { 157, 2 },
       
 91753   { 193, 2 },
       
 91754   { 208, 1 },
       
 91755   { 208, 2 },
       
 91756   { 208, 3 },
       
 91757   { 208, 4 },
       
 91758   { 210, 2 },
       
 91759   { 210, 0 },
       
 91760   { 209, 0 },
       
 91761   { 209, 3 },
       
 91762   { 209, 2 },
       
 91763   { 211, 4 },
       
 91764   { 211, 0 },
       
 91765   { 202, 0 },
       
 91766   { 202, 3 },
       
 91767   { 214, 4 },
       
 91768   { 214, 2 },
       
 91769   { 215, 1 },
       
 91770   { 177, 1 },
       
 91771   { 177, 1 },
       
 91772   { 177, 0 },
       
 91773   { 200, 0 },
       
 91774   { 200, 3 },
       
 91775   { 201, 0 },
       
 91776   { 201, 2 },
       
 91777   { 203, 0 },
       
 91778   { 203, 2 },
       
 91779   { 203, 4 },
       
 91780   { 203, 4 },
       
 91781   { 147, 5 },
       
 91782   { 199, 0 },
       
 91783   { 199, 2 },
       
 91784   { 147, 7 },
       
 91785   { 217, 5 },
       
 91786   { 217, 3 },
       
 91787   { 147, 8 },
       
 91788   { 147, 5 },
       
 91789   { 147, 6 },
       
 91790   { 218, 2 },
       
 91791   { 218, 1 },
       
 91792   { 220, 3 },
       
 91793   { 220, 1 },
       
 91794   { 219, 0 },
       
 91795   { 219, 3 },
       
 91796   { 213, 3 },
       
 91797   { 213, 1 },
       
 91798   { 175, 1 },
       
 91799   { 175, 3 },
       
 91800   { 174, 1 },
       
 91801   { 175, 1 },
       
 91802   { 175, 1 },
       
 91803   { 175, 3 },
       
 91804   { 175, 5 },
       
 91805   { 174, 1 },
       
 91806   { 174, 1 },
       
 91807   { 175, 1 },
       
 91808   { 175, 1 },
       
 91809   { 175, 3 },
       
 91810   { 175, 6 },
       
 91811   { 175, 5 },
       
 91812   { 175, 4 },
       
 91813   { 174, 1 },
       
 91814   { 175, 3 },
       
 91815   { 175, 3 },
       
 91816   { 175, 3 },
       
 91817   { 175, 3 },
       
 91818   { 175, 3 },
       
 91819   { 175, 3 },
       
 91820   { 175, 3 },
       
 91821   { 175, 3 },
       
 91822   { 222, 1 },
       
 91823   { 222, 2 },
       
 91824   { 222, 1 },
       
 91825   { 222, 2 },
       
 91826   { 223, 2 },
       
 91827   { 223, 0 },
       
 91828   { 175, 4 },
       
 91829   { 175, 2 },
       
 91830   { 175, 3 },
       
 91831   { 175, 3 },
       
 91832   { 175, 4 },
       
 91833   { 175, 2 },
       
 91834   { 175, 2 },
       
 91835   { 175, 2 },
       
 91836   { 175, 2 },
       
 91837   { 224, 1 },
       
 91838   { 224, 2 },
       
 91839   { 175, 5 },
       
 91840   { 225, 1 },
       
 91841   { 225, 2 },
       
 91842   { 175, 5 },
       
 91843   { 175, 3 },
       
 91844   { 175, 5 },
       
 91845   { 175, 4 },
       
 91846   { 175, 4 },
       
 91847   { 175, 5 },
       
 91848   { 227, 5 },
       
 91849   { 227, 4 },
       
 91850   { 228, 2 },
       
 91851   { 228, 0 },
       
 91852   { 226, 1 },
       
 91853   { 226, 0 },
       
 91854   { 221, 1 },
       
 91855   { 221, 0 },
       
 91856   { 216, 3 },
       
 91857   { 216, 1 },
       
 91858   { 147, 11 },
       
 91859   { 229, 1 },
       
 91860   { 229, 0 },
       
 91861   { 179, 0 },
       
 91862   { 179, 3 },
       
 91863   { 187, 5 },
       
 91864   { 187, 3 },
       
 91865   { 230, 0 },
       
 91866   { 230, 2 },
       
 91867   { 147, 4 },
       
 91868   { 147, 1 },
       
 91869   { 147, 2 },
       
 91870   { 147, 3 },
       
 91871   { 147, 5 },
       
 91872   { 147, 6 },
       
 91873   { 147, 5 },
       
 91874   { 147, 6 },
       
 91875   { 231, 1 },
       
 91876   { 231, 1 },
       
 91877   { 231, 1 },
       
 91878   { 231, 1 },
       
 91879   { 231, 1 },
       
 91880   { 170, 2 },
       
 91881   { 171, 2 },
       
 91882   { 233, 1 },
       
 91883   { 232, 1 },
       
 91884   { 232, 0 },
       
 91885   { 147, 5 },
       
 91886   { 234, 11 },
       
 91887   { 236, 1 },
       
 91888   { 236, 1 },
       
 91889   { 236, 2 },
       
 91890   { 236, 0 },
       
 91891   { 237, 1 },
       
 91892   { 237, 1 },
       
 91893   { 237, 3 },
       
 91894   { 238, 0 },
       
 91895   { 238, 3 },
       
 91896   { 239, 0 },
       
 91897   { 239, 2 },
       
 91898   { 235, 3 },
       
 91899   { 235, 2 },
       
 91900   { 241, 1 },
       
 91901   { 241, 3 },
       
 91902   { 242, 0 },
       
 91903   { 242, 3 },
       
 91904   { 242, 2 },
       
 91905   { 240, 7 },
       
 91906   { 240, 8 },
       
 91907   { 240, 5 },
       
 91908   { 240, 5 },
       
 91909   { 240, 1 },
       
 91910   { 175, 4 },
       
 91911   { 175, 6 },
       
 91912   { 191, 1 },
       
 91913   { 191, 1 },
       
 91914   { 191, 1 },
       
 91915   { 147, 4 },
       
 91916   { 147, 6 },
       
 91917   { 147, 3 },
       
 91918   { 244, 0 },
       
 91919   { 244, 2 },
       
 91920   { 243, 1 },
       
 91921   { 243, 0 },
       
 91922   { 147, 1 },
       
 91923   { 147, 3 },
       
 91924   { 147, 1 },
       
 91925   { 147, 3 },
       
 91926   { 147, 6 },
       
 91927   { 147, 6 },
       
 91928   { 245, 1 },
       
 91929   { 246, 0 },
       
 91930   { 246, 1 },
       
 91931   { 147, 1 },
       
 91932   { 147, 4 },
       
 91933   { 247, 7 },
       
 91934   { 248, 1 },
       
 91935   { 248, 3 },
       
 91936   { 249, 0 },
       
 91937   { 249, 2 },
       
 91938   { 250, 1 },
       
 91939   { 250, 3 },
       
 91940   { 251, 1 },
       
 91941   { 252, 0 },
       
 91942   { 252, 4 },
       
 91943   { 252, 2 },
       
 91944 };
       
 91945 
       
 91946 static void yy_accept(yyParser*);  /* Forward Declaration */
       
 91947 
       
 91948 /*
       
 91949 ** Perform a reduce action and the shift that must immediately
       
 91950 ** follow the reduce.
       
 91951 */
       
 91952 static void yy_reduce(
       
 91953   yyParser *yypParser,         /* The parser */
       
 91954   int yyruleno                 /* Number of the rule by which to reduce */
       
 91955 ){
       
 91956   int yygoto;                     /* The next state */
       
 91957   int yyact;                      /* The next action */
       
 91958   YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
       
 91959   yyStackEntry *yymsp;            /* The top of the parser's stack */
       
 91960   int yysize;                     /* Amount to pop the stack */
       
 91961   sqlite3ParserARG_FETCH;
       
 91962   yymsp = &yypParser->yystack[yypParser->yyidx];
       
 91963 #ifndef NDEBUG
       
 91964   if( yyTraceFILE && yyruleno>=0 
       
 91965         && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
       
 91966     fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
       
 91967       yyRuleName[yyruleno]);
       
 91968   }
       
 91969 #endif /* NDEBUG */
       
 91970 
       
 91971   /* Silence complaints from purify about yygotominor being uninitialized
       
 91972   ** in some cases when it is copied into the stack after the following
       
 91973   ** switch.  yygotominor is uninitialized when a rule reduces that does
       
 91974   ** not set the value of its left-hand side nonterminal.  Leaving the
       
 91975   ** value of the nonterminal uninitialized is utterly harmless as long
       
 91976   ** as the value is never used.  So really the only thing this code
       
 91977   ** accomplishes is to quieten purify.  
       
 91978   **
       
 91979   ** 2007-01-16:  The wireshark project (www.wireshark.org) reports that
       
 91980   ** without this code, their parser segfaults.  I'm not sure what there
       
 91981   ** parser is doing to make this happen.  This is the second bug report
       
 91982   ** from wireshark this week.  Clearly they are stressing Lemon in ways
       
 91983   ** that it has not been previously stressed...  (SQLite ticket #2172)
       
 91984   */
       
 91985   /*memset(&yygotominor, 0, sizeof(yygotominor));*/
       
 91986   yygotominor = yyzerominor;
       
 91987 
       
 91988 
       
 91989   switch( yyruleno ){
       
 91990   /* Beginning here are the reduction cases.  A typical example
       
 91991   ** follows:
       
 91992   **   case 0:
       
 91993   **  #line <lineno> <grammarfile>
       
 91994   **     { ... }           // User supplied code
       
 91995   **  #line <lineno> <thisfile>
       
 91996   **     break;
       
 91997   */
       
 91998       case 5: /* explain ::= */
       
 91999 { sqlite3BeginParse(pParse, 0); }
       
 92000         break;
       
 92001       case 6: /* explain ::= EXPLAIN */
       
 92002 { sqlite3BeginParse(pParse, 1); }
       
 92003         break;
       
 92004       case 7: /* explain ::= EXPLAIN QUERY PLAN */
       
 92005 { sqlite3BeginParse(pParse, 2); }
       
 92006         break;
       
 92007       case 8: /* cmdx ::= cmd */
       
 92008 { sqlite3FinishCoding(pParse); }
       
 92009         break;
       
 92010       case 9: /* cmd ::= BEGIN transtype trans_opt */
       
 92011 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy328);}
       
 92012         break;
       
 92013       case 13: /* transtype ::= */
       
 92014 {yygotominor.yy328 = TK_DEFERRED;}
       
 92015         break;
       
 92016       case 14: /* transtype ::= DEFERRED */
       
 92017       case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
       
 92018       case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
       
 92019       case 114: /* multiselect_op ::= UNION */ yytestcase(yyruleno==114);
       
 92020       case 116: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==116);
       
 92021 {yygotominor.yy328 = yymsp[0].major;}
       
 92022         break;
       
 92023       case 17: /* cmd ::= COMMIT trans_opt */
       
 92024       case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
       
 92025 {sqlite3CommitTransaction(pParse);}
       
 92026         break;
       
 92027       case 19: /* cmd ::= ROLLBACK trans_opt */
       
 92028 {sqlite3RollbackTransaction(pParse);}
       
 92029         break;
       
 92030       case 22: /* cmd ::= SAVEPOINT nm */
       
 92031 {
       
 92032   sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
       
 92033 }
       
 92034         break;
       
 92035       case 23: /* cmd ::= RELEASE savepoint_opt nm */
       
 92036 {
       
 92037   sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
       
 92038 }
       
 92039         break;
       
 92040       case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
       
 92041 {
       
 92042   sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
       
 92043 }
       
 92044         break;
       
 92045       case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
       
 92046 {
       
 92047    sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy328,0,0,yymsp[-2].minor.yy328);
       
 92048 }
       
 92049         break;
       
 92050       case 27: /* createkw ::= CREATE */
       
 92051 {
       
 92052   pParse->db->lookaside.bEnabled = 0;
       
 92053   yygotominor.yy0 = yymsp[0].minor.yy0;
       
 92054 }
       
 92055         break;
       
 92056       case 28: /* ifnotexists ::= */
       
 92057       case 31: /* temp ::= */ yytestcase(yyruleno==31);
       
 92058       case 70: /* autoinc ::= */ yytestcase(yyruleno==70);
       
 92059       case 82: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==82);
       
 92060       case 84: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==84);
       
 92061       case 86: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==86);
       
 92062       case 97: /* defer_subclause_opt ::= */ yytestcase(yyruleno==97);
       
 92063       case 108: /* ifexists ::= */ yytestcase(yyruleno==108);
       
 92064       case 119: /* distinct ::= ALL */ yytestcase(yyruleno==119);
       
 92065       case 120: /* distinct ::= */ yytestcase(yyruleno==120);
       
 92066       case 222: /* between_op ::= BETWEEN */ yytestcase(yyruleno==222);
       
 92067       case 225: /* in_op ::= IN */ yytestcase(yyruleno==225);
       
 92068 {yygotominor.yy328 = 0;}
       
 92069         break;
       
 92070       case 29: /* ifnotexists ::= IF NOT EXISTS */
       
 92071       case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
       
 92072       case 71: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==71);
       
 92073       case 85: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==85);
       
 92074       case 107: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==107);
       
 92075       case 118: /* distinct ::= DISTINCT */ yytestcase(yyruleno==118);
       
 92076       case 223: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==223);
       
 92077       case 226: /* in_op ::= NOT IN */ yytestcase(yyruleno==226);
       
 92078 {yygotominor.yy328 = 1;}
       
 92079         break;
       
 92080       case 32: /* create_table_args ::= LP columnlist conslist_opt RP */
       
 92081 {
       
 92082   sqlite3EndTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0);
       
 92083 }
       
 92084         break;
       
 92085       case 33: /* create_table_args ::= AS select */
       
 92086 {
       
 92087   sqlite3EndTable(pParse,0,0,yymsp[0].minor.yy3);
       
 92088   sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
       
 92089 }
       
 92090         break;
       
 92091       case 36: /* column ::= columnid type carglist */
       
 92092 {
       
 92093   yygotominor.yy0.z = yymsp[-2].minor.yy0.z;
       
 92094   yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
       
 92095 }
       
 92096         break;
       
 92097       case 37: /* columnid ::= nm */
       
 92098 {
       
 92099   sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
       
 92100   yygotominor.yy0 = yymsp[0].minor.yy0;
       
 92101 }
       
 92102         break;
       
 92103       case 38: /* id ::= ID */
       
 92104       case 39: /* id ::= INDEXED */ yytestcase(yyruleno==39);
       
 92105       case 40: /* ids ::= ID|STRING */ yytestcase(yyruleno==40);
       
 92106       case 41: /* nm ::= id */ yytestcase(yyruleno==41);
       
 92107       case 42: /* nm ::= STRING */ yytestcase(yyruleno==42);
       
 92108       case 43: /* nm ::= JOIN_KW */ yytestcase(yyruleno==43);
       
 92109       case 46: /* typetoken ::= typename */ yytestcase(yyruleno==46);
       
 92110       case 49: /* typename ::= ids */ yytestcase(yyruleno==49);
       
 92111       case 126: /* as ::= AS nm */ yytestcase(yyruleno==126);
       
 92112       case 127: /* as ::= ids */ yytestcase(yyruleno==127);
       
 92113       case 137: /* dbnm ::= DOT nm */ yytestcase(yyruleno==137);
       
 92114       case 146: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==146);
       
 92115       case 251: /* collate ::= COLLATE ids */ yytestcase(yyruleno==251);
       
 92116       case 260: /* nmnum ::= plus_num */ yytestcase(yyruleno==260);
       
 92117       case 261: /* nmnum ::= nm */ yytestcase(yyruleno==261);
       
 92118       case 262: /* nmnum ::= ON */ yytestcase(yyruleno==262);
       
 92119       case 263: /* nmnum ::= DELETE */ yytestcase(yyruleno==263);
       
 92120       case 264: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==264);
       
 92121       case 265: /* plus_num ::= plus_opt number */ yytestcase(yyruleno==265);
       
 92122       case 266: /* minus_num ::= MINUS number */ yytestcase(yyruleno==266);
       
 92123       case 267: /* number ::= INTEGER|FLOAT */ yytestcase(yyruleno==267);
       
 92124       case 285: /* trnm ::= nm */ yytestcase(yyruleno==285);
       
 92125 {yygotominor.yy0 = yymsp[0].minor.yy0;}
       
 92126         break;
       
 92127       case 45: /* type ::= typetoken */
       
 92128 {sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
       
 92129         break;
       
 92130       case 47: /* typetoken ::= typename LP signed RP */
       
 92131 {
       
 92132   yygotominor.yy0.z = yymsp[-3].minor.yy0.z;
       
 92133   yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
       
 92134 }
       
 92135         break;
       
 92136       case 48: /* typetoken ::= typename LP signed COMMA signed RP */
       
 92137 {
       
 92138   yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
       
 92139   yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
       
 92140 }
       
 92141         break;
       
 92142       case 50: /* typename ::= typename ids */
       
 92143 {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);}
       
 92144         break;
       
 92145       case 57: /* ccons ::= DEFAULT term */
       
 92146       case 59: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==59);
       
 92147 {sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy346);}
       
 92148         break;
       
 92149       case 58: /* ccons ::= DEFAULT LP expr RP */
       
 92150 {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy346);}
       
 92151         break;
       
 92152       case 60: /* ccons ::= DEFAULT MINUS term */
       
 92153 {
       
 92154   ExprSpan v;
       
 92155   v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy346.pExpr, 0, 0);
       
 92156   v.zStart = yymsp[-1].minor.yy0.z;
       
 92157   v.zEnd = yymsp[0].minor.yy346.zEnd;
       
 92158   sqlite3AddDefaultValue(pParse,&v);
       
 92159 }
       
 92160         break;
       
 92161       case 61: /* ccons ::= DEFAULT id */
       
 92162 {
       
 92163   ExprSpan v;
       
 92164   spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
       
 92165   sqlite3AddDefaultValue(pParse,&v);
       
 92166 }
       
 92167         break;
       
 92168       case 63: /* ccons ::= NOT NULL onconf */
       
 92169 {sqlite3AddNotNull(pParse, yymsp[0].minor.yy328);}
       
 92170         break;
       
 92171       case 64: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
       
 92172 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy328,yymsp[0].minor.yy328,yymsp[-2].minor.yy328);}
       
 92173         break;
       
 92174       case 65: /* ccons ::= UNIQUE onconf */
       
 92175 {sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy328,0,0,0,0);}
       
 92176         break;
       
 92177       case 66: /* ccons ::= CHECK LP expr RP */
       
 92178 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy346.pExpr);}
       
 92179         break;
       
 92180       case 67: /* ccons ::= REFERENCES nm idxlist_opt refargs */
       
 92181 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy14,yymsp[0].minor.yy328);}
       
 92182         break;
       
 92183       case 68: /* ccons ::= defer_subclause */
       
 92184 {sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy328);}
       
 92185         break;
       
 92186       case 69: /* ccons ::= COLLATE ids */
       
 92187 {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
       
 92188         break;
       
 92189       case 72: /* refargs ::= */
       
 92190 { yygotominor.yy328 = OE_None * 0x000101; }
       
 92191         break;
       
 92192       case 73: /* refargs ::= refargs refarg */
       
 92193 { yygotominor.yy328 = (yymsp[-1].minor.yy328 & ~yymsp[0].minor.yy429.mask) | yymsp[0].minor.yy429.value; }
       
 92194         break;
       
 92195       case 74: /* refarg ::= MATCH nm */
       
 92196 { yygotominor.yy429.value = 0;     yygotominor.yy429.mask = 0x000000; }
       
 92197         break;
       
 92198       case 75: /* refarg ::= ON DELETE refact */
       
 92199 { yygotominor.yy429.value = yymsp[0].minor.yy328;     yygotominor.yy429.mask = 0x0000ff; }
       
 92200         break;
       
 92201       case 76: /* refarg ::= ON UPDATE refact */
       
 92202 { yygotominor.yy429.value = yymsp[0].minor.yy328<<8;  yygotominor.yy429.mask = 0x00ff00; }
       
 92203         break;
       
 92204       case 77: /* refact ::= SET NULL */
       
 92205 { yygotominor.yy328 = OE_SetNull; }
       
 92206         break;
       
 92207       case 78: /* refact ::= SET DEFAULT */
       
 92208 { yygotominor.yy328 = OE_SetDflt; }
       
 92209         break;
       
 92210       case 79: /* refact ::= CASCADE */
       
 92211 { yygotominor.yy328 = OE_Cascade; }
       
 92212         break;
       
 92213       case 80: /* refact ::= RESTRICT */
       
 92214 { yygotominor.yy328 = OE_Restrict; }
       
 92215         break;
       
 92216       case 81: /* refact ::= NO ACTION */
       
 92217 { yygotominor.yy328 = OE_None; }
       
 92218         break;
       
 92219       case 83: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
       
 92220       case 98: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==98);
       
 92221       case 100: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==100);
       
 92222       case 103: /* resolvetype ::= raisetype */ yytestcase(yyruleno==103);
       
 92223 {yygotominor.yy328 = yymsp[0].minor.yy328;}
       
 92224         break;
       
 92225       case 87: /* conslist_opt ::= */
       
 92226 {yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
       
 92227         break;
       
 92228       case 88: /* conslist_opt ::= COMMA conslist */
       
 92229 {yygotominor.yy0 = yymsp[-1].minor.yy0;}
       
 92230         break;
       
 92231       case 93: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
       
 92232 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy14,yymsp[0].minor.yy328,yymsp[-2].minor.yy328,0);}
       
 92233         break;
       
 92234       case 94: /* tcons ::= UNIQUE LP idxlist RP onconf */
       
 92235 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy14,yymsp[0].minor.yy328,0,0,0,0);}
       
 92236         break;
       
 92237       case 95: /* tcons ::= CHECK LP expr RP onconf */
       
 92238 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy346.pExpr);}
       
 92239         break;
       
 92240       case 96: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */
       
 92241 {
       
 92242     sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy14, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy14, yymsp[-1].minor.yy328);
       
 92243     sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy328);
       
 92244 }
       
 92245         break;
       
 92246       case 99: /* onconf ::= */
       
 92247 {yygotominor.yy328 = OE_Default;}
       
 92248         break;
       
 92249       case 101: /* orconf ::= */
       
 92250 {yygotominor.yy186 = OE_Default;}
       
 92251         break;
       
 92252       case 102: /* orconf ::= OR resolvetype */
       
 92253 {yygotominor.yy186 = (u8)yymsp[0].minor.yy328;}
       
 92254         break;
       
 92255       case 104: /* resolvetype ::= IGNORE */
       
 92256 {yygotominor.yy328 = OE_Ignore;}
       
 92257         break;
       
 92258       case 105: /* resolvetype ::= REPLACE */
       
 92259 {yygotominor.yy328 = OE_Replace;}
       
 92260         break;
       
 92261       case 106: /* cmd ::= DROP TABLE ifexists fullname */
       
 92262 {
       
 92263   sqlite3DropTable(pParse, yymsp[0].minor.yy65, 0, yymsp[-1].minor.yy328);
       
 92264 }
       
 92265         break;
       
 92266       case 109: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select */
       
 92267 {
       
 92268   sqlite3CreateView(pParse, &yymsp[-7].minor.yy0, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, yymsp[0].minor.yy3, yymsp[-6].minor.yy328, yymsp[-4].minor.yy328);
       
 92269 }
       
 92270         break;
       
 92271       case 110: /* cmd ::= DROP VIEW ifexists fullname */
       
 92272 {
       
 92273   sqlite3DropTable(pParse, yymsp[0].minor.yy65, 1, yymsp[-1].minor.yy328);
       
 92274 }
       
 92275         break;
       
 92276       case 111: /* cmd ::= select */
       
 92277 {
       
 92278   SelectDest dest = {SRT_Output, 0, 0, 0, 0};
       
 92279   sqlite3Select(pParse, yymsp[0].minor.yy3, &dest);
       
 92280   sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
       
 92281 }
       
 92282         break;
       
 92283       case 112: /* select ::= oneselect */
       
 92284 {yygotominor.yy3 = yymsp[0].minor.yy3;}
       
 92285         break;
       
 92286       case 113: /* select ::= select multiselect_op oneselect */
       
 92287 {
       
 92288   if( yymsp[0].minor.yy3 ){
       
 92289     yymsp[0].minor.yy3->op = (u8)yymsp[-1].minor.yy328;
       
 92290     yymsp[0].minor.yy3->pPrior = yymsp[-2].minor.yy3;
       
 92291   }else{
       
 92292     sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy3);
       
 92293   }
       
 92294   yygotominor.yy3 = yymsp[0].minor.yy3;
       
 92295 }
       
 92296         break;
       
 92297       case 115: /* multiselect_op ::= UNION ALL */
       
 92298 {yygotominor.yy328 = TK_ALL;}
       
 92299         break;
       
 92300       case 117: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
       
 92301 {
       
 92302   yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy14,yymsp[-5].minor.yy65,yymsp[-4].minor.yy132,yymsp[-3].minor.yy14,yymsp[-2].minor.yy132,yymsp[-1].minor.yy14,yymsp[-7].minor.yy328,yymsp[0].minor.yy476.pLimit,yymsp[0].minor.yy476.pOffset);
       
 92303 }
       
 92304         break;
       
 92305       case 121: /* sclp ::= selcollist COMMA */
       
 92306       case 247: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==247);
       
 92307 {yygotominor.yy14 = yymsp[-1].minor.yy14;}
       
 92308         break;
       
 92309       case 122: /* sclp ::= */
       
 92310       case 150: /* orderby_opt ::= */ yytestcase(yyruleno==150);
       
 92311       case 158: /* groupby_opt ::= */ yytestcase(yyruleno==158);
       
 92312       case 240: /* exprlist ::= */ yytestcase(yyruleno==240);
       
 92313       case 246: /* idxlist_opt ::= */ yytestcase(yyruleno==246);
       
 92314 {yygotominor.yy14 = 0;}
       
 92315         break;
       
 92316       case 123: /* selcollist ::= sclp expr as */
       
 92317 {
       
 92318    yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy14, yymsp[-1].minor.yy346.pExpr);
       
 92319    if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[0].minor.yy0, 1);
       
 92320    sqlite3ExprListSetSpan(pParse,yygotominor.yy14,&yymsp[-1].minor.yy346);
       
 92321 }
       
 92322         break;
       
 92323       case 124: /* selcollist ::= sclp STAR */
       
 92324 {
       
 92325   Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
       
 92326   yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy14, p);
       
 92327 }
       
 92328         break;
       
 92329       case 125: /* selcollist ::= sclp nm DOT STAR */
       
 92330 {
       
 92331   Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &yymsp[0].minor.yy0);
       
 92332   Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
       
 92333   Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
       
 92334   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy14, pDot);
       
 92335 }
       
 92336         break;
       
 92337       case 128: /* as ::= */
       
 92338 {yygotominor.yy0.n = 0;}
       
 92339         break;
       
 92340       case 129: /* from ::= */
       
 92341 {yygotominor.yy65 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy65));}
       
 92342         break;
       
 92343       case 130: /* from ::= FROM seltablist */
       
 92344 {
       
 92345   yygotominor.yy65 = yymsp[0].minor.yy65;
       
 92346   sqlite3SrcListShiftJoinType(yygotominor.yy65);
       
 92347 }
       
 92348         break;
       
 92349       case 131: /* stl_prefix ::= seltablist joinop */
       
 92350 {
       
 92351    yygotominor.yy65 = yymsp[-1].minor.yy65;
       
 92352    if( ALWAYS(yygotominor.yy65 && yygotominor.yy65->nSrc>0) ) yygotominor.yy65->a[yygotominor.yy65->nSrc-1].jointype = (u8)yymsp[0].minor.yy328;
       
 92353 }
       
 92354         break;
       
 92355       case 132: /* stl_prefix ::= */
       
 92356 {yygotominor.yy65 = 0;}
       
 92357         break;
       
 92358       case 133: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
       
 92359 {
       
 92360   yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
       
 92361   sqlite3SrcListIndexedBy(pParse, yygotominor.yy65, &yymsp[-2].minor.yy0);
       
 92362 }
       
 92363         break;
       
 92364       case 134: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
       
 92365 {
       
 92366     yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy3,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
       
 92367   }
       
 92368         break;
       
 92369       case 135: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
       
 92370 {
       
 92371     if( yymsp[-6].minor.yy65==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy132==0 && yymsp[0].minor.yy408==0 ){
       
 92372       yygotominor.yy65 = yymsp[-4].minor.yy65;
       
 92373     }else{
       
 92374       Select *pSubquery;
       
 92375       sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy65);
       
 92376       pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy65,0,0,0,0,0,0,0);
       
 92377       yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
       
 92378     }
       
 92379   }
       
 92380         break;
       
 92381       case 136: /* dbnm ::= */
       
 92382       case 145: /* indexed_opt ::= */ yytestcase(yyruleno==145);
       
 92383 {yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
       
 92384         break;
       
 92385       case 138: /* fullname ::= nm dbnm */
       
 92386 {yygotominor.yy65 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
       
 92387         break;
       
 92388       case 139: /* joinop ::= COMMA|JOIN */
       
 92389 { yygotominor.yy328 = JT_INNER; }
       
 92390         break;
       
 92391       case 140: /* joinop ::= JOIN_KW JOIN */
       
 92392 { yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
       
 92393         break;
       
 92394       case 141: /* joinop ::= JOIN_KW nm JOIN */
       
 92395 { yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); }
       
 92396         break;
       
 92397       case 142: /* joinop ::= JOIN_KW nm nm JOIN */
       
 92398 { yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
       
 92399         break;
       
 92400       case 143: /* on_opt ::= ON expr */
       
 92401       case 154: /* sortitem ::= expr */ yytestcase(yyruleno==154);
       
 92402       case 161: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==161);
       
 92403       case 168: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==168);
       
 92404       case 235: /* case_else ::= ELSE expr */ yytestcase(yyruleno==235);
       
 92405       case 237: /* case_operand ::= expr */ yytestcase(yyruleno==237);
       
 92406 {yygotominor.yy132 = yymsp[0].minor.yy346.pExpr;}
       
 92407         break;
       
 92408       case 144: /* on_opt ::= */
       
 92409       case 160: /* having_opt ::= */ yytestcase(yyruleno==160);
       
 92410       case 167: /* where_opt ::= */ yytestcase(yyruleno==167);
       
 92411       case 236: /* case_else ::= */ yytestcase(yyruleno==236);
       
 92412       case 238: /* case_operand ::= */ yytestcase(yyruleno==238);
       
 92413 {yygotominor.yy132 = 0;}
       
 92414         break;
       
 92415       case 147: /* indexed_opt ::= NOT INDEXED */
       
 92416 {yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
       
 92417         break;
       
 92418       case 148: /* using_opt ::= USING LP inscollist RP */
       
 92419       case 180: /* inscollist_opt ::= LP inscollist RP */ yytestcase(yyruleno==180);
       
 92420 {yygotominor.yy408 = yymsp[-1].minor.yy408;}
       
 92421         break;
       
 92422       case 149: /* using_opt ::= */
       
 92423       case 179: /* inscollist_opt ::= */ yytestcase(yyruleno==179);
       
 92424 {yygotominor.yy408 = 0;}
       
 92425         break;
       
 92426       case 151: /* orderby_opt ::= ORDER BY sortlist */
       
 92427       case 159: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==159);
       
 92428       case 239: /* exprlist ::= nexprlist */ yytestcase(yyruleno==239);
       
 92429 {yygotominor.yy14 = yymsp[0].minor.yy14;}
       
 92430         break;
       
 92431       case 152: /* sortlist ::= sortlist COMMA sortitem sortorder */
       
 92432 {
       
 92433   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy14,yymsp[-1].minor.yy132);
       
 92434   if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
       
 92435 }
       
 92436         break;
       
 92437       case 153: /* sortlist ::= sortitem sortorder */
       
 92438 {
       
 92439   yygotominor.yy14 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy132);
       
 92440   if( yygotominor.yy14 && ALWAYS(yygotominor.yy14->a) ) yygotominor.yy14->a[0].sortOrder = (u8)yymsp[0].minor.yy328;
       
 92441 }
       
 92442         break;
       
 92443       case 155: /* sortorder ::= ASC */
       
 92444       case 157: /* sortorder ::= */ yytestcase(yyruleno==157);
       
 92445 {yygotominor.yy328 = SQLITE_SO_ASC;}
       
 92446         break;
       
 92447       case 156: /* sortorder ::= DESC */
       
 92448 {yygotominor.yy328 = SQLITE_SO_DESC;}
       
 92449         break;
       
 92450       case 162: /* limit_opt ::= */
       
 92451 {yygotominor.yy476.pLimit = 0; yygotominor.yy476.pOffset = 0;}
       
 92452         break;
       
 92453       case 163: /* limit_opt ::= LIMIT expr */
       
 92454 {yygotominor.yy476.pLimit = yymsp[0].minor.yy346.pExpr; yygotominor.yy476.pOffset = 0;}
       
 92455         break;
       
 92456       case 164: /* limit_opt ::= LIMIT expr OFFSET expr */
       
 92457 {yygotominor.yy476.pLimit = yymsp[-2].minor.yy346.pExpr; yygotominor.yy476.pOffset = yymsp[0].minor.yy346.pExpr;}
       
 92458         break;
       
 92459       case 165: /* limit_opt ::= LIMIT expr COMMA expr */
       
 92460 {yygotominor.yy476.pOffset = yymsp[-2].minor.yy346.pExpr; yygotominor.yy476.pLimit = yymsp[0].minor.yy346.pExpr;}
       
 92461         break;
       
 92462       case 166: /* cmd ::= DELETE FROM fullname indexed_opt where_opt */
       
 92463 {
       
 92464   sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy65, &yymsp[-1].minor.yy0);
       
 92465   sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy65,yymsp[0].minor.yy132);
       
 92466 }
       
 92467         break;
       
 92468       case 169: /* cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt */
       
 92469 {
       
 92470   sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy65, &yymsp[-3].minor.yy0);
       
 92471   sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy14,"set list"); 
       
 92472   sqlite3Update(pParse,yymsp[-4].minor.yy65,yymsp[-1].minor.yy14,yymsp[0].minor.yy132,yymsp[-5].minor.yy186);
       
 92473 }
       
 92474         break;
       
 92475       case 170: /* setlist ::= setlist COMMA nm EQ expr */
       
 92476 {
       
 92477   yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy14, yymsp[0].minor.yy346.pExpr);
       
 92478   sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
       
 92479 }
       
 92480         break;
       
 92481       case 171: /* setlist ::= nm EQ expr */
       
 92482 {
       
 92483   yygotominor.yy14 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy346.pExpr);
       
 92484   sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
       
 92485 }
       
 92486         break;
       
 92487       case 172: /* cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP */
       
 92488 {sqlite3Insert(pParse, yymsp[-5].minor.yy65, yymsp[-1].minor.yy14, 0, yymsp[-4].minor.yy408, yymsp[-7].minor.yy186);}
       
 92489         break;
       
 92490       case 173: /* cmd ::= insert_cmd INTO fullname inscollist_opt select */
       
 92491 {sqlite3Insert(pParse, yymsp[-2].minor.yy65, 0, yymsp[0].minor.yy3, yymsp[-1].minor.yy408, yymsp[-4].minor.yy186);}
       
 92492         break;
       
 92493       case 174: /* cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
       
 92494 {sqlite3Insert(pParse, yymsp[-3].minor.yy65, 0, 0, yymsp[-2].minor.yy408, yymsp[-5].minor.yy186);}
       
 92495         break;
       
 92496       case 175: /* insert_cmd ::= INSERT orconf */
       
 92497 {yygotominor.yy186 = yymsp[0].minor.yy186;}
       
 92498         break;
       
 92499       case 176: /* insert_cmd ::= REPLACE */
       
 92500 {yygotominor.yy186 = OE_Replace;}
       
 92501         break;
       
 92502       case 177: /* itemlist ::= itemlist COMMA expr */
       
 92503       case 241: /* nexprlist ::= nexprlist COMMA expr */ yytestcase(yyruleno==241);
       
 92504 {yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy14,yymsp[0].minor.yy346.pExpr);}
       
 92505         break;
       
 92506       case 178: /* itemlist ::= expr */
       
 92507       case 242: /* nexprlist ::= expr */ yytestcase(yyruleno==242);
       
 92508 {yygotominor.yy14 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy346.pExpr);}
       
 92509         break;
       
 92510       case 181: /* inscollist ::= inscollist COMMA nm */
       
 92511 {yygotominor.yy408 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy408,&yymsp[0].minor.yy0);}
       
 92512         break;
       
 92513       case 182: /* inscollist ::= nm */
       
 92514 {yygotominor.yy408 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
       
 92515         break;
       
 92516       case 183: /* expr ::= term */
       
 92517       case 211: /* escape ::= ESCAPE expr */ yytestcase(yyruleno==211);
       
 92518 {yygotominor.yy346 = yymsp[0].minor.yy346;}
       
 92519         break;
       
 92520       case 184: /* expr ::= LP expr RP */
       
 92521 {yygotominor.yy346.pExpr = yymsp[-1].minor.yy346.pExpr; spanSet(&yygotominor.yy346,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
       
 92522         break;
       
 92523       case 185: /* term ::= NULL */
       
 92524       case 190: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==190);
       
 92525       case 191: /* term ::= STRING */ yytestcase(yyruleno==191);
       
 92526 {spanExpr(&yygotominor.yy346, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
       
 92527         break;
       
 92528       case 186: /* expr ::= id */
       
 92529       case 187: /* expr ::= JOIN_KW */ yytestcase(yyruleno==187);
       
 92530 {spanExpr(&yygotominor.yy346, pParse, TK_ID, &yymsp[0].minor.yy0);}
       
 92531         break;
       
 92532       case 188: /* expr ::= nm DOT nm */
       
 92533 {
       
 92534   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
       
 92535   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
       
 92536   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
       
 92537   spanSet(&yygotominor.yy346,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
       
 92538 }
       
 92539         break;
       
 92540       case 189: /* expr ::= nm DOT nm DOT nm */
       
 92541 {
       
 92542   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0);
       
 92543   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
       
 92544   Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
       
 92545   Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
       
 92546   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
       
 92547   spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
       
 92548 }
       
 92549         break;
       
 92550       case 192: /* expr ::= REGISTER */
       
 92551 {
       
 92552   /* When doing a nested parse, one can include terms in an expression
       
 92553   ** that look like this:   #1 #2 ...  These terms refer to registers
       
 92554   ** in the virtual machine.  #N is the N-th register. */
       
 92555   if( pParse->nested==0 ){
       
 92556     sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0);
       
 92557     yygotominor.yy346.pExpr = 0;
       
 92558   }else{
       
 92559     yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0].minor.yy0);
       
 92560     if( yygotominor.yy346.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &yygotominor.yy346.pExpr->iTable);
       
 92561   }
       
 92562   spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
       
 92563 }
       
 92564         break;
       
 92565       case 193: /* expr ::= VARIABLE */
       
 92566 {
       
 92567   spanExpr(&yygotominor.yy346, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
       
 92568   sqlite3ExprAssignVarNumber(pParse, yygotominor.yy346.pExpr);
       
 92569   spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
       
 92570 }
       
 92571         break;
       
 92572       case 194: /* expr ::= expr COLLATE ids */
       
 92573 {
       
 92574   yygotominor.yy346.pExpr = sqlite3ExprSetColl(pParse, yymsp[-2].minor.yy346.pExpr, &yymsp[0].minor.yy0);
       
 92575   yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart;
       
 92576   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
       
 92577 }
       
 92578         break;
       
 92579       case 195: /* expr ::= CAST LP expr AS typetoken RP */
       
 92580 {
       
 92581   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy346.pExpr, 0, &yymsp[-1].minor.yy0);
       
 92582   spanSet(&yygotominor.yy346,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
       
 92583 }
       
 92584         break;
       
 92585       case 196: /* expr ::= ID LP distinct exprlist RP */
       
 92586 {
       
 92587   if( yymsp[-1].minor.yy14 && yymsp[-1].minor.yy14->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
       
 92588     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
       
 92589   }
       
 92590   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy14, &yymsp[-4].minor.yy0);
       
 92591   spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
       
 92592   if( yymsp[-2].minor.yy328 && yygotominor.yy346.pExpr ){
       
 92593     yygotominor.yy346.pExpr->flags |= EP_Distinct;
       
 92594   }
       
 92595 }
       
 92596         break;
       
 92597       case 197: /* expr ::= ID LP STAR RP */
       
 92598 {
       
 92599   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
       
 92600   spanSet(&yygotominor.yy346,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
       
 92601 }
       
 92602         break;
       
 92603       case 198: /* term ::= CTIME_KW */
       
 92604 {
       
 92605   /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
       
 92606   ** treated as functions that return constants */
       
 92607   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, 0,&yymsp[0].minor.yy0);
       
 92608   if( yygotominor.yy346.pExpr ){
       
 92609     yygotominor.yy346.pExpr->op = TK_CONST_FUNC;  
       
 92610   }
       
 92611   spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
       
 92612 }
       
 92613         break;
       
 92614       case 199: /* expr ::= expr AND expr */
       
 92615       case 200: /* expr ::= expr OR expr */ yytestcase(yyruleno==200);
       
 92616       case 201: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==201);
       
 92617       case 202: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==202);
       
 92618       case 203: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==203);
       
 92619       case 204: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==204);
       
 92620       case 205: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==205);
       
 92621       case 206: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==206);
       
 92622 {spanBinaryExpr(&yygotominor.yy346,pParse,yymsp[-1].major,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy346);}
       
 92623         break;
       
 92624       case 207: /* likeop ::= LIKE_KW */
       
 92625       case 209: /* likeop ::= MATCH */ yytestcase(yyruleno==209);
       
 92626 {yygotominor.yy96.eOperator = yymsp[0].minor.yy0; yygotominor.yy96.not = 0;}
       
 92627         break;
       
 92628       case 208: /* likeop ::= NOT LIKE_KW */
       
 92629       case 210: /* likeop ::= NOT MATCH */ yytestcase(yyruleno==210);
       
 92630 {yygotominor.yy96.eOperator = yymsp[0].minor.yy0; yygotominor.yy96.not = 1;}
       
 92631         break;
       
 92632       case 212: /* escape ::= */
       
 92633 {memset(&yygotominor.yy346,0,sizeof(yygotominor.yy346));}
       
 92634         break;
       
 92635       case 213: /* expr ::= expr likeop expr escape */
       
 92636 {
       
 92637   ExprList *pList;
       
 92638   pList = sqlite3ExprListAppend(pParse,0, yymsp[-1].minor.yy346.pExpr);
       
 92639   pList = sqlite3ExprListAppend(pParse,pList, yymsp[-3].minor.yy346.pExpr);
       
 92640   if( yymsp[0].minor.yy346.pExpr ){
       
 92641     pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy346.pExpr);
       
 92642   }
       
 92643   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-2].minor.yy96.eOperator);
       
 92644   if( yymsp[-2].minor.yy96.not ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
       
 92645   yygotominor.yy346.zStart = yymsp[-3].minor.yy346.zStart;
       
 92646   yygotominor.yy346.zEnd = yymsp[-1].minor.yy346.zEnd;
       
 92647   if( yygotominor.yy346.pExpr ) yygotominor.yy346.pExpr->flags |= EP_InfixFunc;
       
 92648 }
       
 92649         break;
       
 92650       case 214: /* expr ::= expr ISNULL|NOTNULL */
       
 92651 {spanUnaryPostfix(&yygotominor.yy346,pParse,yymsp[0].major,&yymsp[-1].minor.yy346,&yymsp[0].minor.yy0);}
       
 92652         break;
       
 92653       case 215: /* expr ::= expr NOT NULL */
       
 92654 {spanUnaryPostfix(&yygotominor.yy346,pParse,TK_NOTNULL,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy0);}
       
 92655         break;
       
 92656       case 216: /* expr ::= expr IS expr */
       
 92657 {
       
 92658   spanBinaryExpr(&yygotominor.yy346,pParse,TK_IS,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy346);
       
 92659   if( pParse->db->mallocFailed==0  && yymsp[0].minor.yy346.pExpr->op==TK_NULL ){
       
 92660     yygotominor.yy346.pExpr->op = TK_ISNULL;
       
 92661   }
       
 92662 }
       
 92663         break;
       
 92664       case 217: /* expr ::= expr IS NOT expr */
       
 92665 {
       
 92666   spanBinaryExpr(&yygotominor.yy346,pParse,TK_ISNOT,&yymsp[-3].minor.yy346,&yymsp[0].minor.yy346);
       
 92667   if( pParse->db->mallocFailed==0  && yymsp[0].minor.yy346.pExpr->op==TK_NULL ){
       
 92668     yygotominor.yy346.pExpr->op = TK_NOTNULL;
       
 92669   }
       
 92670 }
       
 92671         break;
       
 92672       case 218: /* expr ::= NOT expr */
       
 92673       case 219: /* expr ::= BITNOT expr */ yytestcase(yyruleno==219);
       
 92674 {spanUnaryPrefix(&yygotominor.yy346,pParse,yymsp[-1].major,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
       
 92675         break;
       
 92676       case 220: /* expr ::= MINUS expr */
       
 92677 {spanUnaryPrefix(&yygotominor.yy346,pParse,TK_UMINUS,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
       
 92678         break;
       
 92679       case 221: /* expr ::= PLUS expr */
       
 92680 {spanUnaryPrefix(&yygotominor.yy346,pParse,TK_UPLUS,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
       
 92681         break;
       
 92682       case 224: /* expr ::= expr between_op expr AND expr */
       
 92683 {
       
 92684   ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
       
 92685   pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy346.pExpr);
       
 92686   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy346.pExpr, 0, 0);
       
 92687   if( yygotominor.yy346.pExpr ){
       
 92688     yygotominor.yy346.pExpr->x.pList = pList;
       
 92689   }else{
       
 92690     sqlite3ExprListDelete(pParse->db, pList);
       
 92691   } 
       
 92692   if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
       
 92693   yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
       
 92694   yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
       
 92695 }
       
 92696         break;
       
 92697       case 227: /* expr ::= expr in_op LP exprlist RP */
       
 92698 {
       
 92699     yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy346.pExpr, 0, 0);
       
 92700     if( yygotominor.yy346.pExpr ){
       
 92701       yygotominor.yy346.pExpr->x.pList = yymsp[-1].minor.yy14;
       
 92702       sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
       
 92703     }else{
       
 92704       sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy14);
       
 92705     }
       
 92706     if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
       
 92707     yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
       
 92708     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
       
 92709   }
       
 92710         break;
       
 92711       case 228: /* expr ::= LP select RP */
       
 92712 {
       
 92713     yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
       
 92714     if( yygotominor.yy346.pExpr ){
       
 92715       yygotominor.yy346.pExpr->x.pSelect = yymsp[-1].minor.yy3;
       
 92716       ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect);
       
 92717       sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
       
 92718     }else{
       
 92719       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
       
 92720     }
       
 92721     yygotominor.yy346.zStart = yymsp[-2].minor.yy0.z;
       
 92722     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
       
 92723   }
       
 92724         break;
       
 92725       case 229: /* expr ::= expr in_op LP select RP */
       
 92726 {
       
 92727     yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy346.pExpr, 0, 0);
       
 92728     if( yygotominor.yy346.pExpr ){
       
 92729       yygotominor.yy346.pExpr->x.pSelect = yymsp[-1].minor.yy3;
       
 92730       ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect);
       
 92731       sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
       
 92732     }else{
       
 92733       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
       
 92734     }
       
 92735     if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
       
 92736     yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
       
 92737     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
       
 92738   }
       
 92739         break;
       
 92740       case 230: /* expr ::= expr in_op nm dbnm */
       
 92741 {
       
 92742     SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);
       
 92743     yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy346.pExpr, 0, 0);
       
 92744     if( yygotominor.yy346.pExpr ){
       
 92745       yygotominor.yy346.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
       
 92746       ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect);
       
 92747       sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
       
 92748     }else{
       
 92749       sqlite3SrcListDelete(pParse->db, pSrc);
       
 92750     }
       
 92751     if( yymsp[-2].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
       
 92752     yygotominor.yy346.zStart = yymsp[-3].minor.yy346.zStart;
       
 92753     yygotominor.yy346.zEnd = yymsp[0].minor.yy0.z ? &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] : &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n];
       
 92754   }
       
 92755         break;
       
 92756       case 231: /* expr ::= EXISTS LP select RP */
       
 92757 {
       
 92758     Expr *p = yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
       
 92759     if( p ){
       
 92760       p->x.pSelect = yymsp[-1].minor.yy3;
       
 92761       ExprSetProperty(p, EP_xIsSelect);
       
 92762       sqlite3ExprSetHeight(pParse, p);
       
 92763     }else{
       
 92764       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
       
 92765     }
       
 92766     yygotominor.yy346.zStart = yymsp[-3].minor.yy0.z;
       
 92767     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
       
 92768   }
       
 92769         break;
       
 92770       case 232: /* expr ::= CASE case_operand case_exprlist case_else END */
       
 92771 {
       
 92772   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy132, yymsp[-1].minor.yy132, 0);
       
 92773   if( yygotominor.yy346.pExpr ){
       
 92774     yygotominor.yy346.pExpr->x.pList = yymsp[-2].minor.yy14;
       
 92775     sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
       
 92776   }else{
       
 92777     sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy14);
       
 92778   }
       
 92779   yygotominor.yy346.zStart = yymsp[-4].minor.yy0.z;
       
 92780   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
       
 92781 }
       
 92782         break;
       
 92783       case 233: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
       
 92784 {
       
 92785   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, yymsp[-2].minor.yy346.pExpr);
       
 92786   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yygotominor.yy14, yymsp[0].minor.yy346.pExpr);
       
 92787 }
       
 92788         break;
       
 92789       case 234: /* case_exprlist ::= WHEN expr THEN expr */
       
 92790 {
       
 92791   yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
       
 92792   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yygotominor.yy14, yymsp[0].minor.yy346.pExpr);
       
 92793 }
       
 92794         break;
       
 92795       case 243: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP */
       
 92796 {
       
 92797   sqlite3CreateIndex(pParse, &yymsp[-6].minor.yy0, &yymsp[-5].minor.yy0, 
       
 92798                      sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy0,0), yymsp[-1].minor.yy14, yymsp[-9].minor.yy328,
       
 92799                       &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy328);
       
 92800 }
       
 92801         break;
       
 92802       case 244: /* uniqueflag ::= UNIQUE */
       
 92803       case 298: /* raisetype ::= ABORT */ yytestcase(yyruleno==298);
       
 92804 {yygotominor.yy328 = OE_Abort;}
       
 92805         break;
       
 92806       case 245: /* uniqueflag ::= */
       
 92807 {yygotominor.yy328 = OE_None;}
       
 92808         break;
       
 92809       case 248: /* idxlist ::= idxlist COMMA nm collate sortorder */
       
 92810 {
       
 92811   Expr *p = 0;
       
 92812   if( yymsp[-1].minor.yy0.n>0 ){
       
 92813     p = sqlite3Expr(pParse->db, TK_COLUMN, 0);
       
 92814     sqlite3ExprSetColl(pParse, p, &yymsp[-1].minor.yy0);
       
 92815   }
       
 92816   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, p);
       
 92817   sqlite3ExprListSetName(pParse,yygotominor.yy14,&yymsp[-2].minor.yy0,1);
       
 92818   sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
       
 92819   if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
       
 92820 }
       
 92821         break;
       
 92822       case 249: /* idxlist ::= nm collate sortorder */
       
 92823 {
       
 92824   Expr *p = 0;
       
 92825   if( yymsp[-1].minor.yy0.n>0 ){
       
 92826     p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
       
 92827     sqlite3ExprSetColl(pParse, p, &yymsp[-1].minor.yy0);
       
 92828   }
       
 92829   yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, p);
       
 92830   sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
       
 92831   sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
       
 92832   if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
       
 92833 }
       
 92834         break;
       
 92835       case 250: /* collate ::= */
       
 92836 {yygotominor.yy0.z = 0; yygotominor.yy0.n = 0;}
       
 92837         break;
       
 92838       case 252: /* cmd ::= DROP INDEX ifexists fullname */
       
 92839 {sqlite3DropIndex(pParse, yymsp[0].minor.yy65, yymsp[-1].minor.yy328);}
       
 92840         break;
       
 92841       case 253: /* cmd ::= VACUUM */
       
 92842       case 254: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==254);
       
 92843 {sqlite3Vacuum(pParse);}
       
 92844         break;
       
 92845       case 255: /* cmd ::= PRAGMA nm dbnm */
       
 92846 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
       
 92847         break;
       
 92848       case 256: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
       
 92849 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
       
 92850         break;
       
 92851       case 257: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
       
 92852 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
       
 92853         break;
       
 92854       case 258: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
       
 92855 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
       
 92856         break;
       
 92857       case 259: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
       
 92858 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
       
 92859         break;
       
 92860       case 270: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
       
 92861 {
       
 92862   Token all;
       
 92863   all.z = yymsp[-3].minor.yy0.z;
       
 92864   all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
       
 92865   sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy473, &all);
       
 92866 }
       
 92867         break;
       
 92868       case 271: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
       
 92869 {
       
 92870   sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy328, yymsp[-4].minor.yy378.a, yymsp[-4].minor.yy378.b, yymsp[-2].minor.yy65, yymsp[0].minor.yy132, yymsp[-10].minor.yy328, yymsp[-8].minor.yy328);
       
 92871   yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
       
 92872 }
       
 92873         break;
       
 92874       case 272: /* trigger_time ::= BEFORE */
       
 92875       case 275: /* trigger_time ::= */ yytestcase(yyruleno==275);
       
 92876 { yygotominor.yy328 = TK_BEFORE; }
       
 92877         break;
       
 92878       case 273: /* trigger_time ::= AFTER */
       
 92879 { yygotominor.yy328 = TK_AFTER;  }
       
 92880         break;
       
 92881       case 274: /* trigger_time ::= INSTEAD OF */
       
 92882 { yygotominor.yy328 = TK_INSTEAD;}
       
 92883         break;
       
 92884       case 276: /* trigger_event ::= DELETE|INSERT */
       
 92885       case 277: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==277);
       
 92886 {yygotominor.yy378.a = yymsp[0].major; yygotominor.yy378.b = 0;}
       
 92887         break;
       
 92888       case 278: /* trigger_event ::= UPDATE OF inscollist */
       
 92889 {yygotominor.yy378.a = TK_UPDATE; yygotominor.yy378.b = yymsp[0].minor.yy408;}
       
 92890         break;
       
 92891       case 281: /* when_clause ::= */
       
 92892       case 303: /* key_opt ::= */ yytestcase(yyruleno==303);
       
 92893 { yygotominor.yy132 = 0; }
       
 92894         break;
       
 92895       case 282: /* when_clause ::= WHEN expr */
       
 92896       case 304: /* key_opt ::= KEY expr */ yytestcase(yyruleno==304);
       
 92897 { yygotominor.yy132 = yymsp[0].minor.yy346.pExpr; }
       
 92898         break;
       
 92899       case 283: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
       
 92900 {
       
 92901   assert( yymsp[-2].minor.yy473!=0 );
       
 92902   yymsp[-2].minor.yy473->pLast->pNext = yymsp[-1].minor.yy473;
       
 92903   yymsp[-2].minor.yy473->pLast = yymsp[-1].minor.yy473;
       
 92904   yygotominor.yy473 = yymsp[-2].minor.yy473;
       
 92905 }
       
 92906         break;
       
 92907       case 284: /* trigger_cmd_list ::= trigger_cmd SEMI */
       
 92908 { 
       
 92909   assert( yymsp[-1].minor.yy473!=0 );
       
 92910   yymsp[-1].minor.yy473->pLast = yymsp[-1].minor.yy473;
       
 92911   yygotominor.yy473 = yymsp[-1].minor.yy473;
       
 92912 }
       
 92913         break;
       
 92914       case 286: /* trnm ::= nm DOT nm */
       
 92915 {
       
 92916   yygotominor.yy0 = yymsp[0].minor.yy0;
       
 92917   sqlite3ErrorMsg(pParse, 
       
 92918         "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
       
 92919         "statements within triggers");
       
 92920 }
       
 92921         break;
       
 92922       case 288: /* tridxby ::= INDEXED BY nm */
       
 92923 {
       
 92924   sqlite3ErrorMsg(pParse,
       
 92925         "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
       
 92926         "within triggers");
       
 92927 }
       
 92928         break;
       
 92929       case 289: /* tridxby ::= NOT INDEXED */
       
 92930 {
       
 92931   sqlite3ErrorMsg(pParse,
       
 92932         "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
       
 92933         "within triggers");
       
 92934 }
       
 92935         break;
       
 92936       case 290: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
       
 92937 { yygotominor.yy473 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy14, yymsp[0].minor.yy132, yymsp[-5].minor.yy186); }
       
 92938         break;
       
 92939       case 291: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt VALUES LP itemlist RP */
       
 92940 {yygotominor.yy473 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy408, yymsp[-1].minor.yy14, 0, yymsp[-7].minor.yy186);}
       
 92941         break;
       
 92942       case 292: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select */
       
 92943 {yygotominor.yy473 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy408, 0, yymsp[0].minor.yy3, yymsp[-4].minor.yy186);}
       
 92944         break;
       
 92945       case 293: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
       
 92946 {yygotominor.yy473 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy132);}
       
 92947         break;
       
 92948       case 294: /* trigger_cmd ::= select */
       
 92949 {yygotominor.yy473 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy3); }
       
 92950         break;
       
 92951       case 295: /* expr ::= RAISE LP IGNORE RP */
       
 92952 {
       
 92953   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); 
       
 92954   if( yygotominor.yy346.pExpr ){
       
 92955     yygotominor.yy346.pExpr->affinity = OE_Ignore;
       
 92956   }
       
 92957   yygotominor.yy346.zStart = yymsp[-3].minor.yy0.z;
       
 92958   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
       
 92959 }
       
 92960         break;
       
 92961       case 296: /* expr ::= RAISE LP raisetype COMMA nm RP */
       
 92962 {
       
 92963   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0); 
       
 92964   if( yygotominor.yy346.pExpr ) {
       
 92965     yygotominor.yy346.pExpr->affinity = (char)yymsp[-3].minor.yy328;
       
 92966   }
       
 92967   yygotominor.yy346.zStart = yymsp[-5].minor.yy0.z;
       
 92968   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
       
 92969 }
       
 92970         break;
       
 92971       case 297: /* raisetype ::= ROLLBACK */
       
 92972 {yygotominor.yy328 = OE_Rollback;}
       
 92973         break;
       
 92974       case 299: /* raisetype ::= FAIL */
       
 92975 {yygotominor.yy328 = OE_Fail;}
       
 92976         break;
       
 92977       case 300: /* cmd ::= DROP TRIGGER ifexists fullname */
       
 92978 {
       
 92979   sqlite3DropTrigger(pParse,yymsp[0].minor.yy65,yymsp[-1].minor.yy328);
       
 92980 }
       
 92981         break;
       
 92982       case 301: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
       
 92983 {
       
 92984   sqlite3Attach(pParse, yymsp[-3].minor.yy346.pExpr, yymsp[-1].minor.yy346.pExpr, yymsp[0].minor.yy132);
       
 92985 }
       
 92986         break;
       
 92987       case 302: /* cmd ::= DETACH database_kw_opt expr */
       
 92988 {
       
 92989   sqlite3Detach(pParse, yymsp[0].minor.yy346.pExpr);
       
 92990 }
       
 92991         break;
       
 92992       case 307: /* cmd ::= REINDEX */
       
 92993 {sqlite3Reindex(pParse, 0, 0);}
       
 92994         break;
       
 92995       case 308: /* cmd ::= REINDEX nm dbnm */
       
 92996 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
       
 92997         break;
       
 92998       case 309: /* cmd ::= ANALYZE */
       
 92999 {sqlite3Analyze(pParse, 0, 0);}
       
 93000         break;
       
 93001       case 310: /* cmd ::= ANALYZE nm dbnm */
       
 93002 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
       
 93003         break;
       
 93004       case 311: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
       
 93005 {
       
 93006   sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy65,&yymsp[0].minor.yy0);
       
 93007 }
       
 93008         break;
       
 93009       case 312: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */
       
 93010 {
       
 93011   sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
       
 93012 }
       
 93013         break;
       
 93014       case 313: /* add_column_fullname ::= fullname */
       
 93015 {
       
 93016   pParse->db->lookaside.bEnabled = 0;
       
 93017   sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy65);
       
 93018 }
       
 93019         break;
       
 93020       case 316: /* cmd ::= create_vtab */
       
 93021 {sqlite3VtabFinishParse(pParse,0);}
       
 93022         break;
       
 93023       case 317: /* cmd ::= create_vtab LP vtabarglist RP */
       
 93024 {sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
       
 93025         break;
       
 93026       case 318: /* create_vtab ::= createkw VIRTUAL TABLE nm dbnm USING nm */
       
 93027 {
       
 93028     sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
       
 93029 }
       
 93030         break;
       
 93031       case 321: /* vtabarg ::= */
       
 93032 {sqlite3VtabArgInit(pParse);}
       
 93033         break;
       
 93034       case 323: /* vtabargtoken ::= ANY */
       
 93035       case 324: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==324);
       
 93036       case 325: /* lp ::= LP */ yytestcase(yyruleno==325);
       
 93037 {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
       
 93038         break;
       
 93039       default:
       
 93040       /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
       
 93041       /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
       
 93042       /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
       
 93043       /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
       
 93044       /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
       
 93045       /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
       
 93046       /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
       
 93047       /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
       
 93048       /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
       
 93049       /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
       
 93050       /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
       
 93051       /* (34) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==34);
       
 93052       /* (35) columnlist ::= column */ yytestcase(yyruleno==35);
       
 93053       /* (44) type ::= */ yytestcase(yyruleno==44);
       
 93054       /* (51) signed ::= plus_num */ yytestcase(yyruleno==51);
       
 93055       /* (52) signed ::= minus_num */ yytestcase(yyruleno==52);
       
 93056       /* (53) carglist ::= carglist carg */ yytestcase(yyruleno==53);
       
 93057       /* (54) carglist ::= */ yytestcase(yyruleno==54);
       
 93058       /* (55) carg ::= CONSTRAINT nm ccons */ yytestcase(yyruleno==55);
       
 93059       /* (56) carg ::= ccons */ yytestcase(yyruleno==56);
       
 93060       /* (62) ccons ::= NULL onconf */ yytestcase(yyruleno==62);
       
 93061       /* (89) conslist ::= conslist COMMA tcons */ yytestcase(yyruleno==89);
       
 93062       /* (90) conslist ::= conslist tcons */ yytestcase(yyruleno==90);
       
 93063       /* (91) conslist ::= tcons */ yytestcase(yyruleno==91);
       
 93064       /* (92) tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==92);
       
 93065       /* (268) plus_opt ::= PLUS */ yytestcase(yyruleno==268);
       
 93066       /* (269) plus_opt ::= */ yytestcase(yyruleno==269);
       
 93067       /* (279) foreach_clause ::= */ yytestcase(yyruleno==279);
       
 93068       /* (280) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==280);
       
 93069       /* (287) tridxby ::= */ yytestcase(yyruleno==287);
       
 93070       /* (305) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==305);
       
 93071       /* (306) database_kw_opt ::= */ yytestcase(yyruleno==306);
       
 93072       /* (314) kwcolumn_opt ::= */ yytestcase(yyruleno==314);
       
 93073       /* (315) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==315);
       
 93074       /* (319) vtabarglist ::= vtabarg */ yytestcase(yyruleno==319);
       
 93075       /* (320) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==320);
       
 93076       /* (322) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==322);
       
 93077       /* (326) anylist ::= */ yytestcase(yyruleno==326);
       
 93078       /* (327) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==327);
       
 93079       /* (328) anylist ::= anylist ANY */ yytestcase(yyruleno==328);
       
 93080         break;
       
 93081   };
       
 93082   yygoto = yyRuleInfo[yyruleno].lhs;
       
 93083   yysize = yyRuleInfo[yyruleno].nrhs;
       
 93084   yypParser->yyidx -= yysize;
       
 93085   yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
       
 93086   if( yyact < YYNSTATE ){
       
 93087 #ifdef NDEBUG
       
 93088     /* If we are not debugging and the reduce action popped at least
       
 93089     ** one element off the stack, then we can push the new element back
       
 93090     ** onto the stack here, and skip the stack overflow test in yy_shift().
       
 93091     ** That gives a significant speed improvement. */
       
 93092     if( yysize ){
       
 93093       yypParser->yyidx++;
       
 93094       yymsp -= yysize-1;
       
 93095       yymsp->stateno = (YYACTIONTYPE)yyact;
       
 93096       yymsp->major = (YYCODETYPE)yygoto;
       
 93097       yymsp->minor = yygotominor;
       
 93098     }else
       
 93099 #endif
       
 93100     {
       
 93101       yy_shift(yypParser,yyact,yygoto,&yygotominor);
       
 93102     }
       
 93103   }else{
       
 93104     assert( yyact == YYNSTATE + YYNRULE + 1 );
       
 93105     yy_accept(yypParser);
       
 93106   }
       
 93107 }
       
 93108 
       
 93109 /*
       
 93110 ** The following code executes when the parse fails
       
 93111 */
       
 93112 #ifndef YYNOERRORRECOVERY
       
 93113 static void yy_parse_failed(
       
 93114   yyParser *yypParser           /* The parser */
       
 93115 ){
       
 93116   sqlite3ParserARG_FETCH;
       
 93117 #ifndef NDEBUG
       
 93118   if( yyTraceFILE ){
       
 93119     fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
       
 93120   }
       
 93121 #endif
       
 93122   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
       
 93123   /* Here code is inserted which will be executed whenever the
       
 93124   ** parser fails */
       
 93125   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
       
 93126 }
       
 93127 #endif /* YYNOERRORRECOVERY */
       
 93128 
       
 93129 /*
       
 93130 ** The following code executes when a syntax error first occurs.
       
 93131 */
       
 93132 static void yy_syntax_error(
       
 93133   yyParser *yypParser,           /* The parser */
       
 93134   int yymajor,                   /* The major type of the error token */
       
 93135   YYMINORTYPE yyminor            /* The minor type of the error token */
       
 93136 ){
       
 93137   sqlite3ParserARG_FETCH;
       
 93138 #define TOKEN (yyminor.yy0)
       
 93139 
       
 93140   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
       
 93141   assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
       
 93142   sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
       
 93143   pParse->parseError = 1;
       
 93144   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
       
 93145 }
       
 93146 
       
 93147 /*
       
 93148 ** The following is executed when the parser accepts
       
 93149 */
       
 93150 static void yy_accept(
       
 93151   yyParser *yypParser           /* The parser */
       
 93152 ){
       
 93153   sqlite3ParserARG_FETCH;
       
 93154 #ifndef NDEBUG
       
 93155   if( yyTraceFILE ){
       
 93156     fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
       
 93157   }
       
 93158 #endif
       
 93159   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
       
 93160   /* Here code is inserted which will be executed whenever the
       
 93161   ** parser accepts */
       
 93162   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
       
 93163 }
       
 93164 
       
 93165 /* The main parser program.
       
 93166 ** The first argument is a pointer to a structure obtained from
       
 93167 ** "sqlite3ParserAlloc" which describes the current state of the parser.
       
 93168 ** The second argument is the major token number.  The third is
       
 93169 ** the minor token.  The fourth optional argument is whatever the
       
 93170 ** user wants (and specified in the grammar) and is available for
       
 93171 ** use by the action routines.
       
 93172 **
       
 93173 ** Inputs:
       
 93174 ** <ul>
       
 93175 ** <li> A pointer to the parser (an opaque structure.)
       
 93176 ** <li> The major token number.
       
 93177 ** <li> The minor token number.
       
 93178 ** <li> An option argument of a grammar-specified type.
       
 93179 ** </ul>
       
 93180 **
       
 93181 ** Outputs:
       
 93182 ** None.
       
 93183 */
       
 93184 SQLITE_PRIVATE void sqlite3Parser(
       
 93185   void *yyp,                   /* The parser */
       
 93186   int yymajor,                 /* The major token code number */
       
 93187   sqlite3ParserTOKENTYPE yyminor       /* The value for the token */
       
 93188   sqlite3ParserARG_PDECL               /* Optional %extra_argument parameter */
       
 93189 ){
       
 93190   YYMINORTYPE yyminorunion;
       
 93191   int yyact;            /* The parser action. */
       
 93192   int yyendofinput;     /* True if we are at the end of input */
       
 93193 #ifdef YYERRORSYMBOL
       
 93194   int yyerrorhit = 0;   /* True if yymajor has invoked an error */
       
 93195 #endif
       
 93196   yyParser *yypParser;  /* The parser */
       
 93197 
       
 93198   /* (re)initialize the parser, if necessary */
       
 93199   yypParser = (yyParser*)yyp;
       
 93200   if( yypParser->yyidx<0 ){
       
 93201 #if YYSTACKDEPTH<=0
       
 93202     if( yypParser->yystksz <=0 ){
       
 93203       /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
       
 93204       yyminorunion = yyzerominor;
       
 93205       yyStackOverflow(yypParser, &yyminorunion);
       
 93206       return;
       
 93207     }
       
 93208 #endif
       
 93209     yypParser->yyidx = 0;
       
 93210     yypParser->yyerrcnt = -1;
       
 93211     yypParser->yystack[0].stateno = 0;
       
 93212     yypParser->yystack[0].major = 0;
       
 93213   }
       
 93214   yyminorunion.yy0 = yyminor;
       
 93215   yyendofinput = (yymajor==0);
       
 93216   sqlite3ParserARG_STORE;
       
 93217 
       
 93218 #ifndef NDEBUG
       
 93219   if( yyTraceFILE ){
       
 93220     fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
       
 93221   }
       
 93222 #endif
       
 93223 
       
 93224   do{
       
 93225     yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
       
 93226     if( yyact<YYNSTATE ){
       
 93227       assert( !yyendofinput );  /* Impossible to shift the $ token */
       
 93228       yy_shift(yypParser,yyact,yymajor,&yyminorunion);
       
 93229       yypParser->yyerrcnt--;
       
 93230       yymajor = YYNOCODE;
       
 93231     }else if( yyact < YYNSTATE + YYNRULE ){
       
 93232       yy_reduce(yypParser,yyact-YYNSTATE);
       
 93233     }else{
       
 93234       assert( yyact == YY_ERROR_ACTION );
       
 93235 #ifdef YYERRORSYMBOL
       
 93236       int yymx;
       
 93237 #endif
       
 93238 #ifndef NDEBUG
       
 93239       if( yyTraceFILE ){
       
 93240         fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
       
 93241       }
       
 93242 #endif
       
 93243 #ifdef YYERRORSYMBOL
       
 93244       /* A syntax error has occurred.
       
 93245       ** The response to an error depends upon whether or not the
       
 93246       ** grammar defines an error token "ERROR".  
       
 93247       **
       
 93248       ** This is what we do if the grammar does define ERROR:
       
 93249       **
       
 93250       **  * Call the %syntax_error function.
       
 93251       **
       
 93252       **  * Begin popping the stack until we enter a state where
       
 93253       **    it is legal to shift the error symbol, then shift
       
 93254       **    the error symbol.
       
 93255       **
       
 93256       **  * Set the error count to three.
       
 93257       **
       
 93258       **  * Begin accepting and shifting new tokens.  No new error
       
 93259       **    processing will occur until three tokens have been
       
 93260       **    shifted successfully.
       
 93261       **
       
 93262       */
       
 93263       if( yypParser->yyerrcnt<0 ){
       
 93264         yy_syntax_error(yypParser,yymajor,yyminorunion);
       
 93265       }
       
 93266       yymx = yypParser->yystack[yypParser->yyidx].major;
       
 93267       if( yymx==YYERRORSYMBOL || yyerrorhit ){
       
 93268 #ifndef NDEBUG
       
 93269         if( yyTraceFILE ){
       
 93270           fprintf(yyTraceFILE,"%sDiscard input token %s\n",
       
 93271              yyTracePrompt,yyTokenName[yymajor]);
       
 93272         }
       
 93273 #endif
       
 93274         yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
       
 93275         yymajor = YYNOCODE;
       
 93276       }else{
       
 93277          while(
       
 93278           yypParser->yyidx >= 0 &&
       
 93279           yymx != YYERRORSYMBOL &&
       
 93280           (yyact = yy_find_reduce_action(
       
 93281                         yypParser->yystack[yypParser->yyidx].stateno,
       
 93282                         YYERRORSYMBOL)) >= YYNSTATE
       
 93283         ){
       
 93284           yy_pop_parser_stack(yypParser);
       
 93285         }
       
 93286         if( yypParser->yyidx < 0 || yymajor==0 ){
       
 93287           yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
       
 93288           yy_parse_failed(yypParser);
       
 93289           yymajor = YYNOCODE;
       
 93290         }else if( yymx!=YYERRORSYMBOL ){
       
 93291           YYMINORTYPE u2;
       
 93292           u2.YYERRSYMDT = 0;
       
 93293           yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
       
 93294         }
       
 93295       }
       
 93296       yypParser->yyerrcnt = 3;
       
 93297       yyerrorhit = 1;
       
 93298 #elif defined(YYNOERRORRECOVERY)
       
 93299       /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
       
 93300       ** do any kind of error recovery.  Instead, simply invoke the syntax
       
 93301       ** error routine and continue going as if nothing had happened.
       
 93302       **
       
 93303       ** Applications can set this macro (for example inside %include) if
       
 93304       ** they intend to abandon the parse upon the first syntax error seen.
       
 93305       */
       
 93306       yy_syntax_error(yypParser,yymajor,yyminorunion);
       
 93307       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
       
 93308       yymajor = YYNOCODE;
       
 93309       
       
 93310 #else  /* YYERRORSYMBOL is not defined */
       
 93311       /* This is what we do if the grammar does not define ERROR:
       
 93312       **
       
 93313       **  * Report an error message, and throw away the input token.
       
 93314       **
       
 93315       **  * If the input token is $, then fail the parse.
       
 93316       **
       
 93317       ** As before, subsequent error messages are suppressed until
       
 93318       ** three input tokens have been successfully shifted.
       
 93319       */
       
 93320       if( yypParser->yyerrcnt<=0 ){
       
 93321         yy_syntax_error(yypParser,yymajor,yyminorunion);
       
 93322       }
       
 93323       yypParser->yyerrcnt = 3;
       
 93324       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
       
 93325       if( yyendofinput ){
       
 93326         yy_parse_failed(yypParser);
       
 93327       }
       
 93328       yymajor = YYNOCODE;
       
 93329 #endif
       
 93330     }
       
 93331   }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
       
 93332   return;
       
 93333 }
       
 93334 
       
 93335 /************** End of parse.c ***********************************************/
       
 93336 /************** Begin file tokenize.c ****************************************/
       
 93337 /*
       
 93338 ** 2001 September 15
       
 93339 **
       
 93340 ** The author disclaims copyright to this source code.  In place of
       
 93341 ** a legal notice, here is a blessing:
       
 93342 **
       
 93343 **    May you do good and not evil.
       
 93344 **    May you find forgiveness for yourself and forgive others.
       
 93345 **    May you share freely, never taking more than you give.
       
 93346 **
       
 93347 *************************************************************************
       
 93348 ** An tokenizer for SQL
       
 93349 **
       
 93350 ** This file contains C code that splits an SQL input string up into
       
 93351 ** individual tokens and sends those tokens one-by-one over to the
       
 93352 ** parser for analysis.
       
 93353 **
       
 93354 ** $Id: tokenize.c,v 1.163 2009/07/03 22:54:37 drh Exp $
       
 93355 */
       
 93356 
       
 93357 /*
       
 93358 ** The charMap() macro maps alphabetic characters into their
       
 93359 ** lower-case ASCII equivalent.  On ASCII machines, this is just
       
 93360 ** an upper-to-lower case map.  On EBCDIC machines we also need
       
 93361 ** to adjust the encoding.  Only alphabetic characters and underscores
       
 93362 ** need to be translated.
       
 93363 */
       
 93364 #ifdef SQLITE_ASCII
       
 93365 # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
       
 93366 #endif
       
 93367 #ifdef SQLITE_EBCDIC
       
 93368 # define charMap(X) ebcdicToAscii[(unsigned char)X]
       
 93369 const unsigned char ebcdicToAscii[] = {
       
 93370 /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
       
 93371    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
       
 93372    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
       
 93373    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
       
 93374    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
       
 93375    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
       
 93376    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
       
 93377    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
       
 93378    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
       
 93379    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
       
 93380    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
       
 93381    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
       
 93382    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
       
 93383    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
       
 93384    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
       
 93385    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
       
 93386    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
       
 93387 };
       
 93388 #endif
       
 93389 
       
 93390 /*
       
 93391 ** The sqlite3KeywordCode function looks up an identifier to determine if
       
 93392 ** it is a keyword.  If it is a keyword, the token code of that keyword is 
       
 93393 ** returned.  If the input is not a keyword, TK_ID is returned.
       
 93394 **
       
 93395 ** The implementation of this routine was generated by a program,
       
 93396 ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
       
 93397 ** The output of the mkkeywordhash.c program is written into a file
       
 93398 ** named keywordhash.h and then included into this source file by
       
 93399 ** the #include below.
       
 93400 */
       
 93401 /************** Include keywordhash.h in the middle of tokenize.c ************/
       
 93402 /************** Begin file keywordhash.h *************************************/
       
 93403 /***** This file contains automatically generated code ******
       
 93404 **
       
 93405 ** The code in this file has been automatically generated by
       
 93406 **
       
 93407 **     $Header: /home/drh/sqlite/trans/cvs/sqlite/sqlite/tool/mkkeywordhash.c,v 1.38 2009/06/09 14:27:41 drh Exp $
       
 93408 **
       
 93409 ** The code in this file implements a function that determines whether
       
 93410 ** or not a given identifier is really an SQL keyword.  The same thing
       
 93411 ** might be implemented more directly using a hand-written hash table.
       
 93412 ** But by using this automatically generated code, the size of the code
       
 93413 ** is substantially reduced.  This is important for embedded applications
       
 93414 ** on platforms with limited memory.
       
 93415 */
       
 93416 /* Hash score: 175 */
       
 93417 static int keywordCode(const char *z, int n){
       
 93418   /* zText[] encodes 811 bytes of keywords in 541 bytes */
       
 93419   /*   REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT       */
       
 93420   /*   ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE         */
       
 93421   /*   XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY         */
       
 93422   /*   UNIQUERYATTACHAVINGROUPDATEBEGINNERELEASEBETWEENOTNULLIKE          */
       
 93423   /*   CASCADELETECASECOLLATECREATECURRENT_DATEDETACHIMMEDIATEJOIN        */
       
 93424   /*   SERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHENWHERENAME         */
       
 93425   /*   AFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMITCONFLICTCROSS     */
       
 93426   /*   CURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAILFROMFULLGLOBYIF      */
       
 93427   /*   ISNULLORDERESTRICTOUTERIGHTROLLBACKROWUNIONUSINGVACUUMVIEW         */
       
 93428   /*   INITIALLY                                                          */
       
 93429   static const char zText[540] = {
       
 93430     'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
       
 93431     'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
       
 93432     'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
       
 93433     'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
       
 93434     'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
       
 93435     'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
       
 93436     'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
       
 93437     'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
       
 93438     'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
       
 93439     'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
       
 93440     'U','E','R','Y','A','T','T','A','C','H','A','V','I','N','G','R','O','U',
       
 93441     'P','D','A','T','E','B','E','G','I','N','N','E','R','E','L','E','A','S',
       
 93442     'E','B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C',
       
 93443     'A','S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L',
       
 93444     'A','T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D',
       
 93445     'A','T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E',
       
 93446     'J','O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A',
       
 93447     'L','Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U',
       
 93448     'E','S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W',
       
 93449     'H','E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C',
       
 93450     'E','A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R',
       
 93451     'E','M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M',
       
 93452     'M','I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U',
       
 93453     'R','R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M',
       
 93454     'A','R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T',
       
 93455     'D','R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L',
       
 93456     'O','B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S',
       
 93457     'T','R','I','C','T','O','U','T','E','R','I','G','H','T','R','O','L','L',
       
 93458     'B','A','C','K','R','O','W','U','N','I','O','N','U','S','I','N','G','V',
       
 93459     'A','C','U','U','M','V','I','E','W','I','N','I','T','I','A','L','L','Y',
       
 93460   };
       
 93461   static const unsigned char aHash[127] = {
       
 93462       72, 101, 114,  70,   0,  44,   0,   0,  78,   0,  73,   0,   0,
       
 93463       42,  12,  74,  15,   0, 113,  81,  50, 108,   0,  19,   0,   0,
       
 93464      118,   0, 116, 111,   0,  22,  89,   0,   9,   0,   0,  66,  67,
       
 93465        0,  65,   6,   0,  48,  86,  98,   0, 115,  97,   0,   0,  45,
       
 93466        0,  99,  24,   0,  17,   0, 119,  49,  23,   0,   5, 106,  25,
       
 93467       92,   0,   0, 121, 102,  56, 120,  53,  28,  51,   0,  87,   0,
       
 93468       96,  26,   0,  95,   0,   0,   0,  91,  88,  93,  84, 105,  14,
       
 93469       39, 104,   0,  77,   0,  18,  85, 107,  32,   0, 117,  76, 109,
       
 93470       59,  46,  80,   0,   0,  90,  40,   0, 112,   0,  36,   0,   0,
       
 93471       29,   0,  82,  58,  60,   0,  20,  57,   0,  52,
       
 93472   };
       
 93473   static const unsigned char aNext[121] = {
       
 93474        0,   0,   0,   0,   4,   0,   0,   0,   0,   0,   0,   0,   0,
       
 93475        0,   2,   0,   0,   0,   0,   0,   0,  13,   0,   0,   0,   0,
       
 93476        0,   7,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
       
 93477        0,   0,   0,   0,   0,  33,  21,   0,   0,   0,  43,   3,  47,
       
 93478        0,   0,   0,   0,  30,  54,   0,   0,  38,   0,   0,   0,   1,
       
 93479       62,   0,   0,  63,   0,  41,   0,   0,   0,   0,   0,   0,   0,
       
 93480       61,   0,   0,   0,   0,  31,  55,  16,  34,  10,   0,   0,   0,
       
 93481        0,   0,   0,   0,  11,  68,  75,   0,   8,   0, 100,  94,   0,
       
 93482      103,   0,  83,   0,  71,   0,   0, 110,  27,  37,  69,  79,   0,
       
 93483       35,  64,   0,   0,
       
 93484   };
       
 93485   static const unsigned char aLen[121] = {
       
 93486        7,   7,   5,   4,   6,   4,   5,   3,   6,   7,   3,   6,   6,
       
 93487        7,   7,   3,   8,   2,   6,   5,   4,   4,   3,  10,   4,   6,
       
 93488       11,   6,   2,   7,   5,   5,   9,   6,   9,   9,   7,  10,  10,
       
 93489        4,   6,   2,   3,   4,   9,   2,   6,   5,   6,   6,   5,   6,
       
 93490        5,   5,   7,   7,   7,   2,   3,   4,   4,   7,   3,   6,   4,
       
 93491        7,   6,  12,   6,   9,   4,   6,   5,   4,   7,   6,   5,   6,
       
 93492        7,   5,   4,   5,   6,   5,   7,   3,   7,  13,   2,   2,   4,
       
 93493        6,   6,   8,   5,  17,  12,   7,   8,   8,   2,   4,   4,   4,
       
 93494        4,   4,   2,   2,   6,   5,   8,   5,   5,   8,   3,   5,   5,
       
 93495        6,   4,   9,   3,
       
 93496   };
       
 93497   static const unsigned short int aOffset[121] = {
       
 93498        0,   2,   2,   8,   9,  14,  16,  20,  23,  25,  25,  29,  33,
       
 93499       36,  41,  46,  48,  53,  54,  59,  62,  65,  67,  69,  78,  81,
       
 93500       86,  91,  95,  96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
       
 93501      159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 189, 194, 197,
       
 93502      203, 206, 210, 217, 223, 223, 223, 226, 229, 233, 234, 238, 244,
       
 93503      248, 255, 261, 273, 279, 288, 290, 296, 301, 303, 310, 315, 320,
       
 93504      326, 332, 337, 341, 344, 350, 354, 361, 363, 370, 372, 374, 383,
       
 93505      387, 393, 399, 407, 412, 412, 428, 435, 442, 443, 450, 454, 458,
       
 93506      462, 466, 469, 471, 473, 479, 483, 491, 495, 500, 508, 511, 516,
       
 93507      521, 527, 531, 536,
       
 93508   };
       
 93509   static const unsigned char aCode[121] = {
       
 93510     TK_REINDEX,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,     
       
 93511     TK_EACH,       TK_CHECK,      TK_KEY,        TK_BEFORE,     TK_FOREIGN,    
       
 93512     TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    TK_EXPLAIN,    TK_INSTEAD,    
       
 93513     TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     TK_TABLE,      
       
 93514     TK_JOIN_KW,    TK_THEN,       TK_END,        TK_DEFERRABLE, TK_ELSE,       
       
 93515     TK_EXCEPT,     TK_TRANSACTION,TK_ACTION,     TK_ON,         TK_JOIN_KW,    
       
 93516     TK_ALTER,      TK_RAISE,      TK_EXCLUSIVE,  TK_EXISTS,     TK_SAVEPOINT,  
       
 93517     TK_INTERSECT,  TK_TRIGGER,    TK_REFERENCES, TK_CONSTRAINT, TK_INTO,       
       
 93518     TK_OFFSET,     TK_OF,         TK_SET,        TK_TEMP,       TK_TEMP,       
       
 93519     TK_OR,         TK_UNIQUE,     TK_QUERY,      TK_ATTACH,     TK_HAVING,     
       
 93520     TK_GROUP,      TK_UPDATE,     TK_BEGIN,      TK_JOIN_KW,    TK_RELEASE,    
       
 93521     TK_BETWEEN,    TK_NOTNULL,    TK_NO,         TK_NOT,        TK_NULL,       
       
 93522     TK_LIKE_KW,    TK_CASCADE,    TK_ASC,        TK_DELETE,     TK_CASE,       
       
 93523     TK_COLLATE,    TK_CREATE,     TK_CTIME_KW,   TK_DETACH,     TK_IMMEDIATE,  
       
 93524     TK_JOIN,       TK_INSERT,     TK_MATCH,      TK_PLAN,       TK_ANALYZE,    
       
 93525     TK_PRAGMA,     TK_ABORT,      TK_VALUES,     TK_VIRTUAL,    TK_LIMIT,      
       
 93526     TK_WHEN,       TK_WHERE,      TK_RENAME,     TK_AFTER,      TK_REPLACE,    
       
 93527     TK_AND,        TK_DEFAULT,    TK_AUTOINCR,   TK_TO,         TK_IN,         
       
 93528     TK_CAST,       TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_JOIN_KW,    
       
 93529     TK_CTIME_KW,   TK_CTIME_KW,   TK_PRIMARY,    TK_DEFERRED,   TK_DISTINCT,   
       
 93530     TK_IS,         TK_DROP,       TK_FAIL,       TK_FROM,       TK_JOIN_KW,    
       
 93531     TK_LIKE_KW,    TK_BY,         TK_IF,         TK_ISNULL,     TK_ORDER,      
       
 93532     TK_RESTRICT,   TK_JOIN_KW,    TK_JOIN_KW,    TK_ROLLBACK,   TK_ROW,        
       
 93533     TK_UNION,      TK_USING,      TK_VACUUM,     TK_VIEW,       TK_INITIALLY,  
       
 93534     TK_ALL,        
       
 93535   };
       
 93536   int h, i;
       
 93537   if( n<2 ) return TK_ID;
       
 93538   h = ((charMap(z[0])*4) ^
       
 93539       (charMap(z[n-1])*3) ^
       
 93540       n) % 127;
       
 93541   for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
       
 93542     if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
       
 93543       testcase( i==0 ); /* REINDEX */
       
 93544       testcase( i==1 ); /* INDEXED */
       
 93545       testcase( i==2 ); /* INDEX */
       
 93546       testcase( i==3 ); /* DESC */
       
 93547       testcase( i==4 ); /* ESCAPE */
       
 93548       testcase( i==5 ); /* EACH */
       
 93549       testcase( i==6 ); /* CHECK */
       
 93550       testcase( i==7 ); /* KEY */
       
 93551       testcase( i==8 ); /* BEFORE */
       
 93552       testcase( i==9 ); /* FOREIGN */
       
 93553       testcase( i==10 ); /* FOR */
       
 93554       testcase( i==11 ); /* IGNORE */
       
 93555       testcase( i==12 ); /* REGEXP */
       
 93556       testcase( i==13 ); /* EXPLAIN */
       
 93557       testcase( i==14 ); /* INSTEAD */
       
 93558       testcase( i==15 ); /* ADD */
       
 93559       testcase( i==16 ); /* DATABASE */
       
 93560       testcase( i==17 ); /* AS */
       
 93561       testcase( i==18 ); /* SELECT */
       
 93562       testcase( i==19 ); /* TABLE */
       
 93563       testcase( i==20 ); /* LEFT */
       
 93564       testcase( i==21 ); /* THEN */
       
 93565       testcase( i==22 ); /* END */
       
 93566       testcase( i==23 ); /* DEFERRABLE */
       
 93567       testcase( i==24 ); /* ELSE */
       
 93568       testcase( i==25 ); /* EXCEPT */
       
 93569       testcase( i==26 ); /* TRANSACTION */
       
 93570       testcase( i==27 ); /* ACTION */
       
 93571       testcase( i==28 ); /* ON */
       
 93572       testcase( i==29 ); /* NATURAL */
       
 93573       testcase( i==30 ); /* ALTER */
       
 93574       testcase( i==31 ); /* RAISE */
       
 93575       testcase( i==32 ); /* EXCLUSIVE */
       
 93576       testcase( i==33 ); /* EXISTS */
       
 93577       testcase( i==34 ); /* SAVEPOINT */
       
 93578       testcase( i==35 ); /* INTERSECT */
       
 93579       testcase( i==36 ); /* TRIGGER */
       
 93580       testcase( i==37 ); /* REFERENCES */
       
 93581       testcase( i==38 ); /* CONSTRAINT */
       
 93582       testcase( i==39 ); /* INTO */
       
 93583       testcase( i==40 ); /* OFFSET */
       
 93584       testcase( i==41 ); /* OF */
       
 93585       testcase( i==42 ); /* SET */
       
 93586       testcase( i==43 ); /* TEMP */
       
 93587       testcase( i==44 ); /* TEMPORARY */
       
 93588       testcase( i==45 ); /* OR */
       
 93589       testcase( i==46 ); /* UNIQUE */
       
 93590       testcase( i==47 ); /* QUERY */
       
 93591       testcase( i==48 ); /* ATTACH */
       
 93592       testcase( i==49 ); /* HAVING */
       
 93593       testcase( i==50 ); /* GROUP */
       
 93594       testcase( i==51 ); /* UPDATE */
       
 93595       testcase( i==52 ); /* BEGIN */
       
 93596       testcase( i==53 ); /* INNER */
       
 93597       testcase( i==54 ); /* RELEASE */
       
 93598       testcase( i==55 ); /* BETWEEN */
       
 93599       testcase( i==56 ); /* NOTNULL */
       
 93600       testcase( i==57 ); /* NO */
       
 93601       testcase( i==58 ); /* NOT */
       
 93602       testcase( i==59 ); /* NULL */
       
 93603       testcase( i==60 ); /* LIKE */
       
 93604       testcase( i==61 ); /* CASCADE */
       
 93605       testcase( i==62 ); /* ASC */
       
 93606       testcase( i==63 ); /* DELETE */
       
 93607       testcase( i==64 ); /* CASE */
       
 93608       testcase( i==65 ); /* COLLATE */
       
 93609       testcase( i==66 ); /* CREATE */
       
 93610       testcase( i==67 ); /* CURRENT_DATE */
       
 93611       testcase( i==68 ); /* DETACH */
       
 93612       testcase( i==69 ); /* IMMEDIATE */
       
 93613       testcase( i==70 ); /* JOIN */
       
 93614       testcase( i==71 ); /* INSERT */
       
 93615       testcase( i==72 ); /* MATCH */
       
 93616       testcase( i==73 ); /* PLAN */
       
 93617       testcase( i==74 ); /* ANALYZE */
       
 93618       testcase( i==75 ); /* PRAGMA */
       
 93619       testcase( i==76 ); /* ABORT */
       
 93620       testcase( i==77 ); /* VALUES */
       
 93621       testcase( i==78 ); /* VIRTUAL */
       
 93622       testcase( i==79 ); /* LIMIT */
       
 93623       testcase( i==80 ); /* WHEN */
       
 93624       testcase( i==81 ); /* WHERE */
       
 93625       testcase( i==82 ); /* RENAME */
       
 93626       testcase( i==83 ); /* AFTER */
       
 93627       testcase( i==84 ); /* REPLACE */
       
 93628       testcase( i==85 ); /* AND */
       
 93629       testcase( i==86 ); /* DEFAULT */
       
 93630       testcase( i==87 ); /* AUTOINCREMENT */
       
 93631       testcase( i==88 ); /* TO */
       
 93632       testcase( i==89 ); /* IN */
       
 93633       testcase( i==90 ); /* CAST */
       
 93634       testcase( i==91 ); /* COLUMN */
       
 93635       testcase( i==92 ); /* COMMIT */
       
 93636       testcase( i==93 ); /* CONFLICT */
       
 93637       testcase( i==94 ); /* CROSS */
       
 93638       testcase( i==95 ); /* CURRENT_TIMESTAMP */
       
 93639       testcase( i==96 ); /* CURRENT_TIME */
       
 93640       testcase( i==97 ); /* PRIMARY */
       
 93641       testcase( i==98 ); /* DEFERRED */
       
 93642       testcase( i==99 ); /* DISTINCT */
       
 93643       testcase( i==100 ); /* IS */
       
 93644       testcase( i==101 ); /* DROP */
       
 93645       testcase( i==102 ); /* FAIL */
       
 93646       testcase( i==103 ); /* FROM */
       
 93647       testcase( i==104 ); /* FULL */
       
 93648       testcase( i==105 ); /* GLOB */
       
 93649       testcase( i==106 ); /* BY */
       
 93650       testcase( i==107 ); /* IF */
       
 93651       testcase( i==108 ); /* ISNULL */
       
 93652       testcase( i==109 ); /* ORDER */
       
 93653       testcase( i==110 ); /* RESTRICT */
       
 93654       testcase( i==111 ); /* OUTER */
       
 93655       testcase( i==112 ); /* RIGHT */
       
 93656       testcase( i==113 ); /* ROLLBACK */
       
 93657       testcase( i==114 ); /* ROW */
       
 93658       testcase( i==115 ); /* UNION */
       
 93659       testcase( i==116 ); /* USING */
       
 93660       testcase( i==117 ); /* VACUUM */
       
 93661       testcase( i==118 ); /* VIEW */
       
 93662       testcase( i==119 ); /* INITIALLY */
       
 93663       testcase( i==120 ); /* ALL */
       
 93664       return aCode[i];
       
 93665     }
       
 93666   }
       
 93667   return TK_ID;
       
 93668 }
       
 93669 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
       
 93670   return keywordCode((char*)z, n);
       
 93671 }
       
 93672 
       
 93673 /************** End of keywordhash.h *****************************************/
       
 93674 /************** Continuing where we left off in tokenize.c *******************/
       
 93675 
       
 93676 
       
 93677 /*
       
 93678 ** If X is a character that can be used in an identifier then
       
 93679 ** IdChar(X) will be true.  Otherwise it is false.
       
 93680 **
       
 93681 ** For ASCII, any character with the high-order bit set is
       
 93682 ** allowed in an identifier.  For 7-bit characters, 
       
 93683 ** sqlite3IsIdChar[X] must be 1.
       
 93684 **
       
 93685 ** For EBCDIC, the rules are more complex but have the same
       
 93686 ** end result.
       
 93687 **
       
 93688 ** Ticket #1066.  the SQL standard does not allow '$' in the
       
 93689 ** middle of identfiers.  But many SQL implementations do. 
       
 93690 ** SQLite will allow '$' in identifiers for compatibility.
       
 93691 ** But the feature is undocumented.
       
 93692 */
       
 93693 #ifdef SQLITE_ASCII
       
 93694 SQLITE_PRIVATE const char sqlite3IsAsciiIdChar[] = {
       
 93695 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
       
 93696     0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
       
 93697     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
       
 93698     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
       
 93699     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
       
 93700     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
       
 93701     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
       
 93702 };
       
 93703 #define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
       
 93704 #endif
       
 93705 #ifdef SQLITE_EBCDIC
       
 93706 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
       
 93707 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
       
 93708     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
       
 93709     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
       
 93710     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
       
 93711     0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
       
 93712     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
       
 93713     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
       
 93714     1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
       
 93715     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
       
 93716     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
       
 93717     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
       
 93718     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
       
 93719     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
       
 93720 };
       
 93721 #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
       
 93722 #endif
       
 93723 
       
 93724 
       
 93725 /*
       
 93726 ** Return the length of the token that begins at z[0]. 
       
 93727 ** Store the token type in *tokenType before returning.
       
 93728 */
       
 93729 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
       
 93730   int i, c;
       
 93731   switch( *z ){
       
 93732     case ' ': case '\t': case '\n': case '\f': case '\r': {
       
 93733       testcase( z[0]==' ' );
       
 93734       testcase( z[0]=='\t' );
       
 93735       testcase( z[0]=='\n' );
       
 93736       testcase( z[0]=='\f' );
       
 93737       testcase( z[0]=='\r' );
       
 93738       for(i=1; sqlite3Isspace(z[i]); i++){}
       
 93739       *tokenType = TK_SPACE;
       
 93740       return i;
       
 93741     }
       
 93742     case '-': {
       
 93743       if( z[1]=='-' ){
       
 93744         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
       
 93745         *tokenType = TK_SPACE;
       
 93746         return i;
       
 93747       }
       
 93748       *tokenType = TK_MINUS;
       
 93749       return 1;
       
 93750     }
       
 93751     case '(': {
       
 93752       *tokenType = TK_LP;
       
 93753       return 1;
       
 93754     }
       
 93755     case ')': {
       
 93756       *tokenType = TK_RP;
       
 93757       return 1;
       
 93758     }
       
 93759     case ';': {
       
 93760       *tokenType = TK_SEMI;
       
 93761       return 1;
       
 93762     }
       
 93763     case '+': {
       
 93764       *tokenType = TK_PLUS;
       
 93765       return 1;
       
 93766     }
       
 93767     case '*': {
       
 93768       *tokenType = TK_STAR;
       
 93769       return 1;
       
 93770     }
       
 93771     case '/': {
       
 93772       if( z[1]!='*' || z[2]==0 ){
       
 93773         *tokenType = TK_SLASH;
       
 93774         return 1;
       
 93775       }
       
 93776       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
       
 93777       if( c ) i++;
       
 93778       *tokenType = TK_SPACE;
       
 93779       return i;
       
 93780     }
       
 93781     case '%': {
       
 93782       *tokenType = TK_REM;
       
 93783       return 1;
       
 93784     }
       
 93785     case '=': {
       
 93786       *tokenType = TK_EQ;
       
 93787       return 1 + (z[1]=='=');
       
 93788     }
       
 93789     case '<': {
       
 93790       if( (c=z[1])=='=' ){
       
 93791         *tokenType = TK_LE;
       
 93792         return 2;
       
 93793       }else if( c=='>' ){
       
 93794         *tokenType = TK_NE;
       
 93795         return 2;
       
 93796       }else if( c=='<' ){
       
 93797         *tokenType = TK_LSHIFT;
       
 93798         return 2;
       
 93799       }else{
       
 93800         *tokenType = TK_LT;
       
 93801         return 1;
       
 93802       }
       
 93803     }
       
 93804     case '>': {
       
 93805       if( (c=z[1])=='=' ){
       
 93806         *tokenType = TK_GE;
       
 93807         return 2;
       
 93808       }else if( c=='>' ){
       
 93809         *tokenType = TK_RSHIFT;
       
 93810         return 2;
       
 93811       }else{
       
 93812         *tokenType = TK_GT;
       
 93813         return 1;
       
 93814       }
       
 93815     }
       
 93816     case '!': {
       
 93817       if( z[1]!='=' ){
       
 93818         *tokenType = TK_ILLEGAL;
       
 93819         return 2;
       
 93820       }else{
       
 93821         *tokenType = TK_NE;
       
 93822         return 2;
       
 93823       }
       
 93824     }
       
 93825     case '|': {
       
 93826       if( z[1]!='|' ){
       
 93827         *tokenType = TK_BITOR;
       
 93828         return 1;
       
 93829       }else{
       
 93830         *tokenType = TK_CONCAT;
       
 93831         return 2;
       
 93832       }
       
 93833     }
       
 93834     case ',': {
       
 93835       *tokenType = TK_COMMA;
       
 93836       return 1;
       
 93837     }
       
 93838     case '&': {
       
 93839       *tokenType = TK_BITAND;
       
 93840       return 1;
       
 93841     }
       
 93842     case '~': {
       
 93843       *tokenType = TK_BITNOT;
       
 93844       return 1;
       
 93845     }
       
 93846     case '`':
       
 93847     case '\'':
       
 93848     case '"': {
       
 93849       int delim = z[0];
       
 93850       testcase( delim=='`' );
       
 93851       testcase( delim=='\'' );
       
 93852       testcase( delim=='"' );
       
 93853       for(i=1; (c=z[i])!=0; i++){
       
 93854         if( c==delim ){
       
 93855           if( z[i+1]==delim ){
       
 93856             i++;
       
 93857           }else{
       
 93858             break;
       
 93859           }
       
 93860         }
       
 93861       }
       
 93862       if( c=='\'' ){
       
 93863         *tokenType = TK_STRING;
       
 93864         return i+1;
       
 93865       }else if( c!=0 ){
       
 93866         *tokenType = TK_ID;
       
 93867         return i+1;
       
 93868       }else{
       
 93869         *tokenType = TK_ILLEGAL;
       
 93870         return i;
       
 93871       }
       
 93872     }
       
 93873     case '.': {
       
 93874 #ifndef SQLITE_OMIT_FLOATING_POINT
       
 93875       if( !sqlite3Isdigit(z[1]) )
       
 93876 #endif
       
 93877       {
       
 93878         *tokenType = TK_DOT;
       
 93879         return 1;
       
 93880       }
       
 93881       /* If the next character is a digit, this is a floating point
       
 93882       ** number that begins with ".".  Fall thru into the next case */
       
 93883     }
       
 93884     case '0': case '1': case '2': case '3': case '4':
       
 93885     case '5': case '6': case '7': case '8': case '9': {
       
 93886       testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
       
 93887       testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
       
 93888       testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
       
 93889       testcase( z[0]=='9' );
       
 93890       *tokenType = TK_INTEGER;
       
 93891       for(i=0; sqlite3Isdigit(z[i]); i++){}
       
 93892 #ifndef SQLITE_OMIT_FLOATING_POINT
       
 93893       if( z[i]=='.' ){
       
 93894         i++;
       
 93895         while( sqlite3Isdigit(z[i]) ){ i++; }
       
 93896         *tokenType = TK_FLOAT;
       
 93897       }
       
 93898       if( (z[i]=='e' || z[i]=='E') &&
       
 93899            ( sqlite3Isdigit(z[i+1]) 
       
 93900             || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
       
 93901            )
       
 93902       ){
       
 93903         i += 2;
       
 93904         while( sqlite3Isdigit(z[i]) ){ i++; }
       
 93905         *tokenType = TK_FLOAT;
       
 93906       }
       
 93907 #endif
       
 93908       while( IdChar(z[i]) ){
       
 93909         *tokenType = TK_ILLEGAL;
       
 93910         i++;
       
 93911       }
       
 93912       return i;
       
 93913     }
       
 93914     case '[': {
       
 93915       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
       
 93916       *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
       
 93917       return i;
       
 93918     }
       
 93919     case '?': {
       
 93920       *tokenType = TK_VARIABLE;
       
 93921       for(i=1; sqlite3Isdigit(z[i]); i++){}
       
 93922       return i;
       
 93923     }
       
 93924     case '#': {
       
 93925       for(i=1; sqlite3Isdigit(z[i]); i++){}
       
 93926       if( i>1 ){
       
 93927         /* Parameters of the form #NNN (where NNN is a number) are used
       
 93928         ** internally by sqlite3NestedParse.  */
       
 93929         *tokenType = TK_REGISTER;
       
 93930         return i;
       
 93931       }
       
 93932       /* Fall through into the next case if the '#' is not followed by
       
 93933       ** a digit. Try to match #AAAA where AAAA is a parameter name. */
       
 93934     }
       
 93935 #ifndef SQLITE_OMIT_TCL_VARIABLE
       
 93936     case '$':
       
 93937 #endif
       
 93938     case '@':  /* For compatibility with MS SQL Server */
       
 93939     case ':': {
       
 93940       int n = 0;
       
 93941       testcase( z[0]=='$' );  testcase( z[0]=='@' );  testcase( z[0]==':' );
       
 93942       *tokenType = TK_VARIABLE;
       
 93943       for(i=1; (c=z[i])!=0; i++){
       
 93944         if( IdChar(c) ){
       
 93945           n++;
       
 93946 #ifndef SQLITE_OMIT_TCL_VARIABLE
       
 93947         }else if( c=='(' && n>0 ){
       
 93948           do{
       
 93949             i++;
       
 93950           }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
       
 93951           if( c==')' ){
       
 93952             i++;
       
 93953           }else{
       
 93954             *tokenType = TK_ILLEGAL;
       
 93955           }
       
 93956           break;
       
 93957         }else if( c==':' && z[i+1]==':' ){
       
 93958           i++;
       
 93959 #endif
       
 93960         }else{
       
 93961           break;
       
 93962         }
       
 93963       }
       
 93964       if( n==0 ) *tokenType = TK_ILLEGAL;
       
 93965       return i;
       
 93966     }
       
 93967 #ifndef SQLITE_OMIT_BLOB_LITERAL
       
 93968     case 'x': case 'X': {
       
 93969       testcase( z[0]=='x' ); testcase( z[0]=='X' );
       
 93970       if( z[1]=='\'' ){
       
 93971         *tokenType = TK_BLOB;
       
 93972         for(i=2; (c=z[i])!=0 && c!='\''; i++){
       
 93973           if( !sqlite3Isxdigit(c) ){
       
 93974             *tokenType = TK_ILLEGAL;
       
 93975           }
       
 93976         }
       
 93977         if( i%2 || !c ) *tokenType = TK_ILLEGAL;
       
 93978         if( c ) i++;
       
 93979         return i;
       
 93980       }
       
 93981       /* Otherwise fall through to the next case */
       
 93982     }
       
 93983 #endif
       
 93984     default: {
       
 93985       if( !IdChar(*z) ){
       
 93986         break;
       
 93987       }
       
 93988       for(i=1; IdChar(z[i]); i++){}
       
 93989       *tokenType = keywordCode((char*)z, i);
       
 93990       return i;
       
 93991     }
       
 93992   }
       
 93993   *tokenType = TK_ILLEGAL;
       
 93994   return 1;
       
 93995 }
       
 93996 
       
 93997 /*
       
 93998 ** Run the parser on the given SQL string.  The parser structure is
       
 93999 ** passed in.  An SQLITE_ status code is returned.  If an error occurs
       
 94000 ** then an and attempt is made to write an error message into 
       
 94001 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
       
 94002 ** error message.
       
 94003 */
       
 94004 SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
       
 94005   int nErr = 0;                   /* Number of errors encountered */
       
 94006   int i;                          /* Loop counter */
       
 94007   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
       
 94008   int tokenType;                  /* type of the next token */
       
 94009   int lastTokenParsed = -1;       /* type of the previous token */
       
 94010   u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
       
 94011   sqlite3 *db = pParse->db;       /* The database connection */
       
 94012   int mxSqlLen;                   /* Max length of an SQL string */
       
 94013 
       
 94014 
       
 94015   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
       
 94016   if( db->activeVdbeCnt==0 ){
       
 94017     db->u1.isInterrupted = 0;
       
 94018   }
       
 94019   pParse->rc = SQLITE_OK;
       
 94020   pParse->zTail = zSql;
       
 94021   i = 0;
       
 94022   assert( pzErrMsg!=0 );
       
 94023   pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
       
 94024   if( pEngine==0 ){
       
 94025     db->mallocFailed = 1;
       
 94026     return SQLITE_NOMEM;
       
 94027   }
       
 94028   assert( pParse->pNewTable==0 );
       
 94029   assert( pParse->pNewTrigger==0 );
       
 94030   assert( pParse->nVar==0 );
       
 94031   assert( pParse->nVarExpr==0 );
       
 94032   assert( pParse->nVarExprAlloc==0 );
       
 94033   assert( pParse->apVarExpr==0 );
       
 94034   enableLookaside = db->lookaside.bEnabled;
       
 94035   if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
       
 94036   while( !db->mallocFailed && zSql[i]!=0 ){
       
 94037     assert( i>=0 );
       
 94038     pParse->sLastToken.z = &zSql[i];
       
 94039     pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
       
 94040     i += pParse->sLastToken.n;
       
 94041     if( i>mxSqlLen ){
       
 94042       pParse->rc = SQLITE_TOOBIG;
       
 94043       break;
       
 94044     }
       
 94045     switch( tokenType ){
       
 94046       case TK_SPACE: {
       
 94047         if( db->u1.isInterrupted ){
       
 94048           sqlite3ErrorMsg(pParse, "interrupt");
       
 94049           pParse->rc = SQLITE_INTERRUPT;
       
 94050           goto abort_parse;
       
 94051         }
       
 94052         break;
       
 94053       }
       
 94054       case TK_ILLEGAL: {
       
 94055         sqlite3DbFree(db, *pzErrMsg);
       
 94056         *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
       
 94057                         &pParse->sLastToken);
       
 94058         nErr++;
       
 94059         goto abort_parse;
       
 94060       }
       
 94061       case TK_SEMI: {
       
 94062         pParse->zTail = &zSql[i];
       
 94063         /* Fall thru into the default case */
       
 94064       }
       
 94065       default: {
       
 94066         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
       
 94067         lastTokenParsed = tokenType;
       
 94068         if( pParse->rc!=SQLITE_OK ){
       
 94069           goto abort_parse;
       
 94070         }
       
 94071         break;
       
 94072       }
       
 94073     }
       
 94074   }
       
 94075 abort_parse:
       
 94076   if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
       
 94077     if( lastTokenParsed!=TK_SEMI ){
       
 94078       sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
       
 94079       pParse->zTail = &zSql[i];
       
 94080     }
       
 94081     sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
       
 94082   }
       
 94083 #ifdef YYTRACKMAXSTACKDEPTH
       
 94084   sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
       
 94085       sqlite3ParserStackPeak(pEngine)
       
 94086   );
       
 94087 #endif /* YYDEBUG */
       
 94088   sqlite3ParserFree(pEngine, sqlite3_free);
       
 94089   db->lookaside.bEnabled = enableLookaside;
       
 94090   if( db->mallocFailed ){
       
 94091     pParse->rc = SQLITE_NOMEM;
       
 94092   }
       
 94093   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
       
 94094     sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
       
 94095   }
       
 94096   assert( pzErrMsg!=0 );
       
 94097   if( pParse->zErrMsg ){
       
 94098     *pzErrMsg = pParse->zErrMsg;
       
 94099     pParse->zErrMsg = 0;
       
 94100     nErr++;
       
 94101   }
       
 94102   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
       
 94103     sqlite3VdbeDelete(pParse->pVdbe);
       
 94104     pParse->pVdbe = 0;
       
 94105   }
       
 94106 #ifndef SQLITE_OMIT_SHARED_CACHE
       
 94107   if( pParse->nested==0 ){
       
 94108     sqlite3DbFree(db, pParse->aTableLock);
       
 94109     pParse->aTableLock = 0;
       
 94110     pParse->nTableLock = 0;
       
 94111   }
       
 94112 #endif
       
 94113 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 94114   sqlite3DbFree(db, pParse->apVtabLock);
       
 94115 #endif
       
 94116 
       
 94117   if( !IN_DECLARE_VTAB ){
       
 94118     /* If the pParse->declareVtab flag is set, do not delete any table 
       
 94119     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
       
 94120     ** will take responsibility for freeing the Table structure.
       
 94121     */
       
 94122     sqlite3DeleteTable(pParse->pNewTable);
       
 94123   }
       
 94124 
       
 94125   sqlite3DeleteTrigger(db, pParse->pNewTrigger);
       
 94126   sqlite3DbFree(db, pParse->apVarExpr);
       
 94127   sqlite3DbFree(db, pParse->aAlias);
       
 94128   while( pParse->pAinc ){
       
 94129     AutoincInfo *p = pParse->pAinc;
       
 94130     pParse->pAinc = p->pNext;
       
 94131     sqlite3DbFree(db, p);
       
 94132   }
       
 94133   while( pParse->pZombieTab ){
       
 94134     Table *p = pParse->pZombieTab;
       
 94135     pParse->pZombieTab = p->pNextZombie;
       
 94136     sqlite3DeleteTable(p);
       
 94137   }
       
 94138   if( nErr>0 && pParse->rc==SQLITE_OK ){
       
 94139     pParse->rc = SQLITE_ERROR;
       
 94140   }
       
 94141   return nErr;
       
 94142 }
       
 94143 
       
 94144 /************** End of tokenize.c ********************************************/
       
 94145 /************** Begin file complete.c ****************************************/
       
 94146 /*
       
 94147 ** 2001 September 15
       
 94148 **
       
 94149 ** The author disclaims copyright to this source code.  In place of
       
 94150 ** a legal notice, here is a blessing:
       
 94151 **
       
 94152 **    May you do good and not evil.
       
 94153 **    May you find forgiveness for yourself and forgive others.
       
 94154 **    May you share freely, never taking more than you give.
       
 94155 **
       
 94156 *************************************************************************
       
 94157 ** An tokenizer for SQL
       
 94158 **
       
 94159 ** This file contains C code that implements the sqlite3_complete() API.
       
 94160 ** This code used to be part of the tokenizer.c source file.  But by
       
 94161 ** separating it out, the code will be automatically omitted from
       
 94162 ** static links that do not use it.
       
 94163 **
       
 94164 ** $Id: complete.c,v 1.8 2009/04/28 04:46:42 drh Exp $
       
 94165 */
       
 94166 #ifndef SQLITE_OMIT_COMPLETE
       
 94167 
       
 94168 /*
       
 94169 ** This is defined in tokenize.c.  We just have to import the definition.
       
 94170 */
       
 94171 #ifndef SQLITE_AMALGAMATION
       
 94172 #ifdef SQLITE_ASCII
       
 94173 SQLITE_PRIVATE const char sqlite3IsAsciiIdChar[];
       
 94174 #define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
       
 94175 #endif
       
 94176 #ifdef SQLITE_EBCDIC
       
 94177 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
       
 94178 #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
       
 94179 #endif
       
 94180 #endif /* SQLITE_AMALGAMATION */
       
 94181 
       
 94182 
       
 94183 /*
       
 94184 ** Token types used by the sqlite3_complete() routine.  See the header
       
 94185 ** comments on that procedure for additional information.
       
 94186 */
       
 94187 #define tkSEMI    0
       
 94188 #define tkWS      1
       
 94189 #define tkOTHER   2
       
 94190 #define tkEXPLAIN 3
       
 94191 #define tkCREATE  4
       
 94192 #define tkTEMP    5
       
 94193 #define tkTRIGGER 6
       
 94194 #define tkEND     7
       
 94195 
       
 94196 /*
       
 94197 ** Return TRUE if the given SQL string ends in a semicolon.
       
 94198 **
       
 94199 ** Special handling is require for CREATE TRIGGER statements.
       
 94200 ** Whenever the CREATE TRIGGER keywords are seen, the statement
       
 94201 ** must end with ";END;".
       
 94202 **
       
 94203 ** This implementation uses a state machine with 7 states:
       
 94204 **
       
 94205 **   (0) START     At the beginning or end of an SQL statement.  This routine
       
 94206 **                 returns 1 if it ends in the START state and 0 if it ends
       
 94207 **                 in any other state.
       
 94208 **
       
 94209 **   (1) NORMAL    We are in the middle of statement which ends with a single
       
 94210 **                 semicolon.
       
 94211 **
       
 94212 **   (2) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of 
       
 94213 **                 a statement.
       
 94214 **
       
 94215 **   (3) CREATE    The keyword CREATE has been seen at the beginning of a
       
 94216 **                 statement, possibly preceeded by EXPLAIN and/or followed by
       
 94217 **                 TEMP or TEMPORARY
       
 94218 **
       
 94219 **   (4) TRIGGER   We are in the middle of a trigger definition that must be
       
 94220 **                 ended by a semicolon, the keyword END, and another semicolon.
       
 94221 **
       
 94222 **   (5) SEMI      We've seen the first semicolon in the ";END;" that occurs at
       
 94223 **                 the end of a trigger definition.
       
 94224 **
       
 94225 **   (6) END       We've seen the ";END" of the ";END;" that occurs at the end
       
 94226 **                 of a trigger difinition.
       
 94227 **
       
 94228 ** Transitions between states above are determined by tokens extracted
       
 94229 ** from the input.  The following tokens are significant:
       
 94230 **
       
 94231 **   (0) tkSEMI      A semicolon.
       
 94232 **   (1) tkWS        Whitespace
       
 94233 **   (2) tkOTHER     Any other SQL token.
       
 94234 **   (3) tkEXPLAIN   The "explain" keyword.
       
 94235 **   (4) tkCREATE    The "create" keyword.
       
 94236 **   (5) tkTEMP      The "temp" or "temporary" keyword.
       
 94237 **   (6) tkTRIGGER   The "trigger" keyword.
       
 94238 **   (7) tkEND       The "end" keyword.
       
 94239 **
       
 94240 ** Whitespace never causes a state transition and is always ignored.
       
 94241 **
       
 94242 ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
       
 94243 ** to recognize the end of a trigger can be omitted.  All we have to do
       
 94244 ** is look for a semicolon that is not part of an string or comment.
       
 94245 */
       
 94246 SQLITE_API int sqlite3_complete(const char *zSql){
       
 94247   u8 state = 0;   /* Current state, using numbers defined in header comment */
       
 94248   u8 token;       /* Value of the next token */
       
 94249 
       
 94250 #ifndef SQLITE_OMIT_TRIGGER
       
 94251   /* A complex statement machine used to detect the end of a CREATE TRIGGER
       
 94252   ** statement.  This is the normal case.
       
 94253   */
       
 94254   static const u8 trans[7][8] = {
       
 94255                      /* Token:                                                */
       
 94256      /* State:       **  SEMI  WS  OTHER EXPLAIN  CREATE  TEMP  TRIGGER  END  */
       
 94257      /* 0   START: */ {    0,  0,     1,      2,      3,    1,       1,   1,  },
       
 94258      /* 1  NORMAL: */ {    0,  1,     1,      1,      1,    1,       1,   1,  },
       
 94259      /* 2 EXPLAIN: */ {    0,  2,     2,      1,      3,    1,       1,   1,  },
       
 94260      /* 3  CREATE: */ {    0,  3,     1,      1,      1,    3,       4,   1,  },
       
 94261      /* 4 TRIGGER: */ {    5,  4,     4,      4,      4,    4,       4,   4,  },
       
 94262      /* 5    SEMI: */ {    5,  5,     4,      4,      4,    4,       4,   6,  },
       
 94263      /* 6     END: */ {    0,  6,     4,      4,      4,    4,       4,   4,  },
       
 94264   };
       
 94265 #else
       
 94266   /* If triggers are not suppored by this compile then the statement machine
       
 94267   ** used to detect the end of a statement is much simplier
       
 94268   */
       
 94269   static const u8 trans[2][3] = {
       
 94270                      /* Token:           */
       
 94271      /* State:       **  SEMI  WS  OTHER */
       
 94272      /* 0   START: */ {    0,  0,     1, },
       
 94273      /* 1  NORMAL: */ {    0,  1,     1, },
       
 94274   };
       
 94275 #endif /* SQLITE_OMIT_TRIGGER */
       
 94276 
       
 94277   while( *zSql ){
       
 94278     switch( *zSql ){
       
 94279       case ';': {  /* A semicolon */
       
 94280         token = tkSEMI;
       
 94281         break;
       
 94282       }
       
 94283       case ' ':
       
 94284       case '\r':
       
 94285       case '\t':
       
 94286       case '\n':
       
 94287       case '\f': {  /* White space is ignored */
       
 94288         token = tkWS;
       
 94289         break;
       
 94290       }
       
 94291       case '/': {   /* C-style comments */
       
 94292         if( zSql[1]!='*' ){
       
 94293           token = tkOTHER;
       
 94294           break;
       
 94295         }
       
 94296         zSql += 2;
       
 94297         while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
       
 94298         if( zSql[0]==0 ) return 0;
       
 94299         zSql++;
       
 94300         token = tkWS;
       
 94301         break;
       
 94302       }
       
 94303       case '-': {   /* SQL-style comments from "--" to end of line */
       
 94304         if( zSql[1]!='-' ){
       
 94305           token = tkOTHER;
       
 94306           break;
       
 94307         }
       
 94308         while( *zSql && *zSql!='\n' ){ zSql++; }
       
 94309         if( *zSql==0 ) return state==0;
       
 94310         token = tkWS;
       
 94311         break;
       
 94312       }
       
 94313       case '[': {   /* Microsoft-style identifiers in [...] */
       
 94314         zSql++;
       
 94315         while( *zSql && *zSql!=']' ){ zSql++; }
       
 94316         if( *zSql==0 ) return 0;
       
 94317         token = tkOTHER;
       
 94318         break;
       
 94319       }
       
 94320       case '`':     /* Grave-accent quoted symbols used by MySQL */
       
 94321       case '"':     /* single- and double-quoted strings */
       
 94322       case '\'': {
       
 94323         int c = *zSql;
       
 94324         zSql++;
       
 94325         while( *zSql && *zSql!=c ){ zSql++; }
       
 94326         if( *zSql==0 ) return 0;
       
 94327         token = tkOTHER;
       
 94328         break;
       
 94329       }
       
 94330       default: {
       
 94331         int c;
       
 94332         if( IdChar((u8)*zSql) ){
       
 94333           /* Keywords and unquoted identifiers */
       
 94334           int nId;
       
 94335           for(nId=1; IdChar(zSql[nId]); nId++){}
       
 94336 #ifdef SQLITE_OMIT_TRIGGER
       
 94337           token = tkOTHER;
       
 94338 #else
       
 94339           switch( *zSql ){
       
 94340             case 'c': case 'C': {
       
 94341               if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
       
 94342                 token = tkCREATE;
       
 94343               }else{
       
 94344                 token = tkOTHER;
       
 94345               }
       
 94346               break;
       
 94347             }
       
 94348             case 't': case 'T': {
       
 94349               if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
       
 94350                 token = tkTRIGGER;
       
 94351               }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
       
 94352                 token = tkTEMP;
       
 94353               }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
       
 94354                 token = tkTEMP;
       
 94355               }else{
       
 94356                 token = tkOTHER;
       
 94357               }
       
 94358               break;
       
 94359             }
       
 94360             case 'e':  case 'E': {
       
 94361               if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
       
 94362                 token = tkEND;
       
 94363               }else
       
 94364 #ifndef SQLITE_OMIT_EXPLAIN
       
 94365               if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
       
 94366                 token = tkEXPLAIN;
       
 94367               }else
       
 94368 #endif
       
 94369               {
       
 94370                 token = tkOTHER;
       
 94371               }
       
 94372               break;
       
 94373             }
       
 94374             default: {
       
 94375               token = tkOTHER;
       
 94376               break;
       
 94377             }
       
 94378           }
       
 94379 #endif /* SQLITE_OMIT_TRIGGER */
       
 94380           zSql += nId-1;
       
 94381         }else{
       
 94382           /* Operators and special symbols */
       
 94383           token = tkOTHER;
       
 94384         }
       
 94385         break;
       
 94386       }
       
 94387     }
       
 94388     state = trans[state][token];
       
 94389     zSql++;
       
 94390   }
       
 94391   return state==0;
       
 94392 }
       
 94393 
       
 94394 #ifndef SQLITE_OMIT_UTF16
       
 94395 /*
       
 94396 ** This routine is the same as the sqlite3_complete() routine described
       
 94397 ** above, except that the parameter is required to be UTF-16 encoded, not
       
 94398 ** UTF-8.
       
 94399 */
       
 94400 SQLITE_API int sqlite3_complete16(const void *zSql){
       
 94401   sqlite3_value *pVal;
       
 94402   char const *zSql8;
       
 94403   int rc = SQLITE_NOMEM;
       
 94404 
       
 94405 #ifndef SQLITE_OMIT_AUTOINIT
       
 94406   rc = sqlite3_initialize();
       
 94407   if( rc ) return rc;
       
 94408 #endif
       
 94409   pVal = sqlite3ValueNew(0);
       
 94410   sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
       
 94411   zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
       
 94412   if( zSql8 ){
       
 94413     rc = sqlite3_complete(zSql8);
       
 94414   }else{
       
 94415     rc = SQLITE_NOMEM;
       
 94416   }
       
 94417   sqlite3ValueFree(pVal);
       
 94418   return sqlite3ApiExit(0, rc);
       
 94419 }
       
 94420 #endif /* SQLITE_OMIT_UTF16 */
       
 94421 #endif /* SQLITE_OMIT_COMPLETE */
       
 94422 
       
 94423 /************** End of complete.c ********************************************/
       
 94424 /************** Begin file main.c ********************************************/
       
 94425 /*
       
 94426 ** 2001 September 15
       
 94427 **
       
 94428 ** The author disclaims copyright to this source code.  In place of
       
 94429 ** a legal notice, here is a blessing:
       
 94430 **
       
 94431 **    May you do good and not evil.
       
 94432 **    May you find forgiveness for yourself and forgive others.
       
 94433 **    May you share freely, never taking more than you give.
       
 94434 **
       
 94435 *************************************************************************
       
 94436 ** Main file for the SQLite library.  The routines in this file
       
 94437 ** implement the programmer interface to the library.  Routines in
       
 94438 ** other files are for internal use by SQLite and should not be
       
 94439 ** accessed by users of the library.
       
 94440 */
       
 94441 
       
 94442 #ifdef SQLITE_ENABLE_FTS3
       
 94443 /************** Include fts3.h in the middle of main.c ***********************/
       
 94444 /************** Begin file fts3.h ********************************************/
       
 94445 /*
       
 94446 ** 2006 Oct 10
       
 94447 **
       
 94448 ** The author disclaims copyright to this source code.  In place of
       
 94449 ** a legal notice, here is a blessing:
       
 94450 **
       
 94451 **    May you do good and not evil.
       
 94452 **    May you find forgiveness for yourself and forgive others.
       
 94453 **    May you share freely, never taking more than you give.
       
 94454 **
       
 94455 ******************************************************************************
       
 94456 **
       
 94457 ** This header file is used by programs that want to link against the
       
 94458 ** FTS3 library.  All it does is declare the sqlite3Fts3Init() interface.
       
 94459 */
       
 94460 
       
 94461 #if 0
       
 94462 extern "C" {
       
 94463 #endif  /* __cplusplus */
       
 94464 
       
 94465 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
       
 94466 
       
 94467 #if 0
       
 94468 }  /* extern "C" */
       
 94469 #endif  /* __cplusplus */
       
 94470 
       
 94471 /************** End of fts3.h ************************************************/
       
 94472 /************** Continuing where we left off in main.c ***********************/
       
 94473 #endif
       
 94474 #ifdef SQLITE_ENABLE_RTREE
       
 94475 /************** Include rtree.h in the middle of main.c **********************/
       
 94476 /************** Begin file rtree.h *******************************************/
       
 94477 /*
       
 94478 ** 2008 May 26
       
 94479 **
       
 94480 ** The author disclaims copyright to this source code.  In place of
       
 94481 ** a legal notice, here is a blessing:
       
 94482 **
       
 94483 **    May you do good and not evil.
       
 94484 **    May you find forgiveness for yourself and forgive others.
       
 94485 **    May you share freely, never taking more than you give.
       
 94486 **
       
 94487 ******************************************************************************
       
 94488 **
       
 94489 ** This header file is used by programs that want to link against the
       
 94490 ** RTREE library.  All it does is declare the sqlite3RtreeInit() interface.
       
 94491 */
       
 94492 
       
 94493 #if 0
       
 94494 extern "C" {
       
 94495 #endif  /* __cplusplus */
       
 94496 
       
 94497 SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
       
 94498 
       
 94499 #if 0
       
 94500 }  /* extern "C" */
       
 94501 #endif  /* __cplusplus */
       
 94502 
       
 94503 /************** End of rtree.h ***********************************************/
       
 94504 /************** Continuing where we left off in main.c ***********************/
       
 94505 #endif
       
 94506 #ifdef SQLITE_ENABLE_ICU
       
 94507 /************** Include sqliteicu.h in the middle of main.c ******************/
       
 94508 /************** Begin file sqliteicu.h ***************************************/
       
 94509 /*
       
 94510 ** 2008 May 26
       
 94511 **
       
 94512 ** The author disclaims copyright to this source code.  In place of
       
 94513 ** a legal notice, here is a blessing:
       
 94514 **
       
 94515 **    May you do good and not evil.
       
 94516 **    May you find forgiveness for yourself and forgive others.
       
 94517 **    May you share freely, never taking more than you give.
       
 94518 **
       
 94519 ******************************************************************************
       
 94520 **
       
 94521 ** This header file is used by programs that want to link against the
       
 94522 ** ICU extension.  All it does is declare the sqlite3IcuInit() interface.
       
 94523 */
       
 94524 
       
 94525 #if 0
       
 94526 extern "C" {
       
 94527 #endif  /* __cplusplus */
       
 94528 
       
 94529 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
       
 94530 
       
 94531 #if 0
       
 94532 }  /* extern "C" */
       
 94533 #endif  /* __cplusplus */
       
 94534 
       
 94535 
       
 94536 /************** End of sqliteicu.h *******************************************/
       
 94537 /************** Continuing where we left off in main.c ***********************/
       
 94538 #endif
       
 94539 
       
 94540 /*
       
 94541 ** The version of the library
       
 94542 */
       
 94543 #ifndef SQLITE_AMALGAMATION
       
 94544 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
       
 94545 #endif
       
 94546 SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
       
 94547 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
       
 94548 SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
       
 94549 SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
       
 94550 
       
 94551 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
       
 94552 /*
       
 94553 ** If the following function pointer is not NULL and if
       
 94554 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
       
 94555 ** I/O active are written using this function.  These messages
       
 94556 ** are intended for debugging activity only.
       
 94557 */
       
 94558 SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*, ...) = 0;
       
 94559 #endif
       
 94560 
       
 94561 /*
       
 94562 ** If the following global variable points to a string which is the
       
 94563 ** name of a directory, then that directory will be used to store
       
 94564 ** temporary files.
       
 94565 **
       
 94566 ** See also the "PRAGMA temp_store_directory" SQL command.
       
 94567 */
       
 94568 SQLITE_API char *sqlite3_temp_directory = 0;
       
 94569 
       
 94570 /*
       
 94571 ** Initialize SQLite.  
       
 94572 **
       
 94573 ** This routine must be called to initialize the memory allocation,
       
 94574 ** VFS, and mutex subsystems prior to doing any serious work with
       
 94575 ** SQLite.  But as long as you do not compile with SQLITE_OMIT_AUTOINIT
       
 94576 ** this routine will be called automatically by key routines such as
       
 94577 ** sqlite3_open().  
       
 94578 **
       
 94579 ** This routine is a no-op except on its very first call for the process,
       
 94580 ** or for the first call after a call to sqlite3_shutdown.
       
 94581 **
       
 94582 ** The first thread to call this routine runs the initialization to
       
 94583 ** completion.  If subsequent threads call this routine before the first
       
 94584 ** thread has finished the initialization process, then the subsequent
       
 94585 ** threads must block until the first thread finishes with the initialization.
       
 94586 **
       
 94587 ** The first thread might call this routine recursively.  Recursive
       
 94588 ** calls to this routine should not block, of course.  Otherwise the
       
 94589 ** initialization process would never complete.
       
 94590 **
       
 94591 ** Let X be the first thread to enter this routine.  Let Y be some other
       
 94592 ** thread.  Then while the initial invocation of this routine by X is
       
 94593 ** incomplete, it is required that:
       
 94594 **
       
 94595 **    *  Calls to this routine from Y must block until the outer-most
       
 94596 **       call by X completes.
       
 94597 **
       
 94598 **    *  Recursive calls to this routine from thread X return immediately
       
 94599 **       without blocking.
       
 94600 */
       
 94601 SQLITE_API int sqlite3_initialize(void){
       
 94602   sqlite3_mutex *pMaster;                      /* The main static mutex */
       
 94603   int rc;                                      /* Result code */
       
 94604 
       
 94605 #ifdef SQLITE_OMIT_WSD
       
 94606   rc = sqlite3_wsd_init(4096, 24);
       
 94607   if( rc!=SQLITE_OK ){
       
 94608     return rc;
       
 94609   }
       
 94610 #endif
       
 94611 
       
 94612   /* If SQLite is already completely initialized, then this call
       
 94613   ** to sqlite3_initialize() should be a no-op.  But the initialization
       
 94614   ** must be complete.  So isInit must not be set until the very end
       
 94615   ** of this routine.
       
 94616   */
       
 94617   if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
       
 94618 
       
 94619   /* Make sure the mutex subsystem is initialized.  If unable to 
       
 94620   ** initialize the mutex subsystem, return early with the error.
       
 94621   ** If the system is so sick that we are unable to allocate a mutex,
       
 94622   ** there is not much SQLite is going to be able to do.
       
 94623   **
       
 94624   ** The mutex subsystem must take care of serializing its own
       
 94625   ** initialization.
       
 94626   */
       
 94627   rc = sqlite3MutexInit();
       
 94628   if( rc ) return rc;
       
 94629 
       
 94630   /* Initialize the malloc() system and the recursive pInitMutex mutex.
       
 94631   ** This operation is protected by the STATIC_MASTER mutex.  Note that
       
 94632   ** MutexAlloc() is called for a static mutex prior to initializing the
       
 94633   ** malloc subsystem - this implies that the allocation of a static
       
 94634   ** mutex must not require support from the malloc subsystem.
       
 94635   */
       
 94636   pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
       
 94637   sqlite3_mutex_enter(pMaster);
       
 94638   sqlite3GlobalConfig.isMutexInit = 1;
       
 94639   if( !sqlite3GlobalConfig.isMallocInit ){
       
 94640     rc = sqlite3MallocInit();
       
 94641   }
       
 94642   if( rc==SQLITE_OK ){
       
 94643     sqlite3GlobalConfig.isMallocInit = 1;
       
 94644     if( !sqlite3GlobalConfig.pInitMutex ){
       
 94645       sqlite3GlobalConfig.pInitMutex =
       
 94646            sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
       
 94647       if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
       
 94648         rc = SQLITE_NOMEM;
       
 94649       }
       
 94650     }
       
 94651   }
       
 94652   if( rc==SQLITE_OK ){
       
 94653     sqlite3GlobalConfig.nRefInitMutex++;
       
 94654   }
       
 94655   sqlite3_mutex_leave(pMaster);
       
 94656 
       
 94657   /* If rc is not SQLITE_OK at this point, then either the malloc
       
 94658   ** subsystem could not be initialized or the system failed to allocate
       
 94659   ** the pInitMutex mutex. Return an error in either case.  */
       
 94660   if( rc!=SQLITE_OK ){
       
 94661     return rc;
       
 94662   }
       
 94663 
       
 94664   /* Do the rest of the initialization under the recursive mutex so
       
 94665   ** that we will be able to handle recursive calls into
       
 94666   ** sqlite3_initialize().  The recursive calls normally come through
       
 94667   ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
       
 94668   ** recursive calls might also be possible.
       
 94669   */
       
 94670   sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
       
 94671   if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
       
 94672     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
       
 94673     sqlite3GlobalConfig.inProgress = 1;
       
 94674     memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
       
 94675     sqlite3RegisterGlobalFunctions();
       
 94676     if( sqlite3GlobalConfig.isPCacheInit==0 ){
       
 94677       rc = sqlite3PcacheInitialize();
       
 94678     }
       
 94679     if( rc==SQLITE_OK ){
       
 94680       sqlite3GlobalConfig.isPCacheInit = 1;
       
 94681       rc = sqlite3OsInit();
       
 94682     }
       
 94683     if( rc==SQLITE_OK ){
       
 94684       sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 
       
 94685           sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
       
 94686       sqlite3GlobalConfig.isInit = 1;
       
 94687     }
       
 94688     sqlite3GlobalConfig.inProgress = 0;
       
 94689   }
       
 94690   sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
       
 94691 
       
 94692   /* Go back under the static mutex and clean up the recursive
       
 94693   ** mutex to prevent a resource leak.
       
 94694   */
       
 94695   sqlite3_mutex_enter(pMaster);
       
 94696   sqlite3GlobalConfig.nRefInitMutex--;
       
 94697   if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
       
 94698     assert( sqlite3GlobalConfig.nRefInitMutex==0 );
       
 94699     sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
       
 94700     sqlite3GlobalConfig.pInitMutex = 0;
       
 94701   }
       
 94702   sqlite3_mutex_leave(pMaster);
       
 94703 
       
 94704   /* The following is just a sanity check to make sure SQLite has
       
 94705   ** been compiled correctly.  It is important to run this code, but
       
 94706   ** we don't want to run it too often and soak up CPU cycles for no
       
 94707   ** reason.  So we run it once during initialization.
       
 94708   */
       
 94709 #ifndef NDEBUG
       
 94710 #ifndef SQLITE_OMIT_FLOATING_POINT
       
 94711   /* This section of code's only "output" is via assert() statements. */
       
 94712   if ( rc==SQLITE_OK ){
       
 94713     u64 x = (((u64)1)<<63)-1;
       
 94714     double y;
       
 94715     assert(sizeof(x)==8);
       
 94716     assert(sizeof(x)==sizeof(y));
       
 94717     memcpy(&y, &x, 8);
       
 94718     assert( sqlite3IsNaN(y) );
       
 94719   }
       
 94720 #endif
       
 94721 #endif
       
 94722 
       
 94723   return rc;
       
 94724 }
       
 94725 
       
 94726 /*
       
 94727 ** Undo the effects of sqlite3_initialize().  Must not be called while
       
 94728 ** there are outstanding database connections or memory allocations or
       
 94729 ** while any part of SQLite is otherwise in use in any thread.  This
       
 94730 ** routine is not threadsafe.  But it is safe to invoke this routine
       
 94731 ** on when SQLite is already shut down.  If SQLite is already shut down
       
 94732 ** when this routine is invoked, then this routine is a harmless no-op.
       
 94733 */
       
 94734 SQLITE_API int sqlite3_shutdown(void){
       
 94735   if( sqlite3GlobalConfig.isInit ){
       
 94736     sqlite3_os_end();
       
 94737     sqlite3_reset_auto_extension();
       
 94738     sqlite3GlobalConfig.isInit = 0;
       
 94739   }
       
 94740   if( sqlite3GlobalConfig.isPCacheInit ){
       
 94741     sqlite3PcacheShutdown();
       
 94742     sqlite3GlobalConfig.isPCacheInit = 0;
       
 94743   }
       
 94744   if( sqlite3GlobalConfig.isMallocInit ){
       
 94745     sqlite3MallocEnd();
       
 94746     sqlite3GlobalConfig.isMallocInit = 0;
       
 94747   }
       
 94748   if( sqlite3GlobalConfig.isMutexInit ){
       
 94749     sqlite3MutexEnd();
       
 94750     sqlite3GlobalConfig.isMutexInit = 0;
       
 94751   }
       
 94752 
       
 94753   return SQLITE_OK;
       
 94754 }
       
 94755 
       
 94756 /*
       
 94757 ** This API allows applications to modify the global configuration of
       
 94758 ** the SQLite library at run-time.
       
 94759 **
       
 94760 ** This routine should only be called when there are no outstanding
       
 94761 ** database connections or memory allocations.  This routine is not
       
 94762 ** threadsafe.  Failure to heed these warnings can lead to unpredictable
       
 94763 ** behavior.
       
 94764 */
       
 94765 SQLITE_API int sqlite3_config(int op, ...){
       
 94766   va_list ap;
       
 94767   int rc = SQLITE_OK;
       
 94768 
       
 94769   /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
       
 94770   ** the SQLite library is in use. */
       
 94771   if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE;
       
 94772 
       
 94773   va_start(ap, op);
       
 94774   switch( op ){
       
 94775 
       
 94776     /* Mutex configuration options are only available in a threadsafe
       
 94777     ** compile. 
       
 94778     */
       
 94779 #if SQLITE_THREADSAFE
       
 94780     case SQLITE_CONFIG_SINGLETHREAD: {
       
 94781       /* Disable all mutexing */
       
 94782       sqlite3GlobalConfig.bCoreMutex = 0;
       
 94783       sqlite3GlobalConfig.bFullMutex = 0;
       
 94784       break;
       
 94785     }
       
 94786     case SQLITE_CONFIG_MULTITHREAD: {
       
 94787       /* Disable mutexing of database connections */
       
 94788       /* Enable mutexing of core data structures */
       
 94789       sqlite3GlobalConfig.bCoreMutex = 1;
       
 94790       sqlite3GlobalConfig.bFullMutex = 0;
       
 94791       break;
       
 94792     }
       
 94793     case SQLITE_CONFIG_SERIALIZED: {
       
 94794       /* Enable all mutexing */
       
 94795       sqlite3GlobalConfig.bCoreMutex = 1;
       
 94796       sqlite3GlobalConfig.bFullMutex = 1;
       
 94797       break;
       
 94798     }
       
 94799     case SQLITE_CONFIG_MUTEX: {
       
 94800       /* Specify an alternative mutex implementation */
       
 94801       sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
       
 94802       break;
       
 94803     }
       
 94804     case SQLITE_CONFIG_GETMUTEX: {
       
 94805       /* Retrieve the current mutex implementation */
       
 94806       *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
       
 94807       break;
       
 94808     }
       
 94809 #endif
       
 94810 
       
 94811 
       
 94812     case SQLITE_CONFIG_MALLOC: {
       
 94813       /* Specify an alternative malloc implementation */
       
 94814       sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
       
 94815       break;
       
 94816     }
       
 94817     case SQLITE_CONFIG_GETMALLOC: {
       
 94818       /* Retrieve the current malloc() implementation */
       
 94819       if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
       
 94820       *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
       
 94821       break;
       
 94822     }
       
 94823     case SQLITE_CONFIG_MEMSTATUS: {
       
 94824       /* Enable or disable the malloc status collection */
       
 94825       sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
       
 94826       break;
       
 94827     }
       
 94828     case SQLITE_CONFIG_SCRATCH: {
       
 94829       /* Designate a buffer for scratch memory space */
       
 94830       sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
       
 94831       sqlite3GlobalConfig.szScratch = va_arg(ap, int);
       
 94832       sqlite3GlobalConfig.nScratch = va_arg(ap, int);
       
 94833       break;
       
 94834     }
       
 94835     case SQLITE_CONFIG_PAGECACHE: {
       
 94836       /* Designate a buffer for page cache memory space */
       
 94837       sqlite3GlobalConfig.pPage = va_arg(ap, void*);
       
 94838       sqlite3GlobalConfig.szPage = va_arg(ap, int);
       
 94839       sqlite3GlobalConfig.nPage = va_arg(ap, int);
       
 94840       break;
       
 94841     }
       
 94842 
       
 94843     case SQLITE_CONFIG_PCACHE: {
       
 94844       /* Specify an alternative page cache implementation */
       
 94845       sqlite3GlobalConfig.pcache = *va_arg(ap, sqlite3_pcache_methods*);
       
 94846       break;
       
 94847     }
       
 94848 
       
 94849     case SQLITE_CONFIG_GETPCACHE: {
       
 94850       if( sqlite3GlobalConfig.pcache.xInit==0 ){
       
 94851         sqlite3PCacheSetDefault();
       
 94852       }
       
 94853       *va_arg(ap, sqlite3_pcache_methods*) = sqlite3GlobalConfig.pcache;
       
 94854       break;
       
 94855     }
       
 94856 
       
 94857 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
       
 94858     case SQLITE_CONFIG_HEAP: {
       
 94859       /* Designate a buffer for heap memory space */
       
 94860       sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
       
 94861       sqlite3GlobalConfig.nHeap = va_arg(ap, int);
       
 94862       sqlite3GlobalConfig.mnReq = va_arg(ap, int);
       
 94863 
       
 94864       if( sqlite3GlobalConfig.pHeap==0 ){
       
 94865         /* If the heap pointer is NULL, then restore the malloc implementation
       
 94866         ** back to NULL pointers too.  This will cause the malloc to go
       
 94867         ** back to its default implementation when sqlite3_initialize() is
       
 94868         ** run.
       
 94869         */
       
 94870         memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
       
 94871       }else{
       
 94872         /* The heap pointer is not NULL, then install one of the
       
 94873         ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
       
 94874         ** ENABLE_MEMSYS5 is defined, return an error.
       
 94875         */
       
 94876 #ifdef SQLITE_ENABLE_MEMSYS3
       
 94877         sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
       
 94878 #endif
       
 94879 #ifdef SQLITE_ENABLE_MEMSYS5
       
 94880         sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
       
 94881 #endif
       
 94882       }
       
 94883       break;
       
 94884     }
       
 94885 #endif
       
 94886 
       
 94887     case SQLITE_CONFIG_LOOKASIDE: {
       
 94888       sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
       
 94889       sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
       
 94890       break;
       
 94891     }
       
 94892 
       
 94893     default: {
       
 94894       rc = SQLITE_ERROR;
       
 94895       break;
       
 94896     }
       
 94897   }
       
 94898   va_end(ap);
       
 94899   return rc;
       
 94900 }
       
 94901 
       
 94902 /*
       
 94903 ** Set up the lookaside buffers for a database connection.
       
 94904 ** Return SQLITE_OK on success.  
       
 94905 ** If lookaside is already active, return SQLITE_BUSY.
       
 94906 **
       
 94907 ** The sz parameter is the number of bytes in each lookaside slot.
       
 94908 ** The cnt parameter is the number of slots.  If pStart is NULL the
       
 94909 ** space for the lookaside memory is obtained from sqlite3_malloc().
       
 94910 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
       
 94911 ** the lookaside memory.
       
 94912 */
       
 94913 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
       
 94914   void *pStart;
       
 94915   if( db->lookaside.nOut ){
       
 94916     return SQLITE_BUSY;
       
 94917   }
       
 94918   /* Free any existing lookaside buffer for this handle before
       
 94919   ** allocating a new one so we don't have to have space for 
       
 94920   ** both at the same time.
       
 94921   */
       
 94922   if( db->lookaside.bMalloced ){
       
 94923     sqlite3_free(db->lookaside.pStart);
       
 94924   }
       
 94925   /* The size of a lookaside slot needs to be larger than a pointer
       
 94926   ** to be useful.
       
 94927   */
       
 94928   if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
       
 94929   if( cnt<0 ) cnt = 0;
       
 94930   if( sz==0 || cnt==0 ){
       
 94931     sz = 0;
       
 94932     pStart = 0;
       
 94933   }else if( pBuf==0 ){
       
 94934     sz = ROUND8(sz);
       
 94935     sqlite3BeginBenignMalloc();
       
 94936     pStart = sqlite3Malloc( sz*cnt );
       
 94937     sqlite3EndBenignMalloc();
       
 94938   }else{
       
 94939     sz = ROUNDDOWN8(sz);
       
 94940     pStart = pBuf;
       
 94941   }
       
 94942   db->lookaside.pStart = pStart;
       
 94943   db->lookaside.pFree = 0;
       
 94944   db->lookaside.sz = (u16)sz;
       
 94945   if( pStart ){
       
 94946     int i;
       
 94947     LookasideSlot *p;
       
 94948     assert( sz > (int)sizeof(LookasideSlot*) );
       
 94949     p = (LookasideSlot*)pStart;
       
 94950     for(i=cnt-1; i>=0; i--){
       
 94951       p->pNext = db->lookaside.pFree;
       
 94952       db->lookaside.pFree = p;
       
 94953       p = (LookasideSlot*)&((u8*)p)[sz];
       
 94954     }
       
 94955     db->lookaside.pEnd = p;
       
 94956     db->lookaside.bEnabled = 1;
       
 94957     db->lookaside.bMalloced = pBuf==0 ?1:0;
       
 94958   }else{
       
 94959     db->lookaside.pEnd = 0;
       
 94960     db->lookaside.bEnabled = 0;
       
 94961     db->lookaside.bMalloced = 0;
       
 94962   }
       
 94963   return SQLITE_OK;
       
 94964 }
       
 94965 
       
 94966 /*
       
 94967 ** Return the mutex associated with a database connection.
       
 94968 */
       
 94969 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
       
 94970   return db->mutex;
       
 94971 }
       
 94972 
       
 94973 /*
       
 94974 ** Configuration settings for an individual database connection
       
 94975 */
       
 94976 SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
       
 94977   va_list ap;
       
 94978   int rc;
       
 94979   va_start(ap, op);
       
 94980   switch( op ){
       
 94981     case SQLITE_DBCONFIG_LOOKASIDE: {
       
 94982       void *pBuf = va_arg(ap, void*);
       
 94983       int sz = va_arg(ap, int);
       
 94984       int cnt = va_arg(ap, int);
       
 94985       rc = setupLookaside(db, pBuf, sz, cnt);
       
 94986       break;
       
 94987     }
       
 94988     default: {
       
 94989       rc = SQLITE_ERROR;
       
 94990       break;
       
 94991     }
       
 94992   }
       
 94993   va_end(ap);
       
 94994   return rc;
       
 94995 }
       
 94996 
       
 94997 
       
 94998 /*
       
 94999 ** Return true if the buffer z[0..n-1] contains all spaces.
       
 95000 */
       
 95001 static int allSpaces(const char *z, int n){
       
 95002   while( n>0 && z[n-1]==' ' ){ n--; }
       
 95003   return n==0;
       
 95004 }
       
 95005 
       
 95006 /*
       
 95007 ** This is the default collating function named "BINARY" which is always
       
 95008 ** available.
       
 95009 **
       
 95010 ** If the padFlag argument is not NULL then space padding at the end
       
 95011 ** of strings is ignored.  This implements the RTRIM collation.
       
 95012 */
       
 95013 static int binCollFunc(
       
 95014   void *padFlag,
       
 95015   int nKey1, const void *pKey1,
       
 95016   int nKey2, const void *pKey2
       
 95017 ){
       
 95018   int rc, n;
       
 95019   n = nKey1<nKey2 ? nKey1 : nKey2;
       
 95020   rc = memcmp(pKey1, pKey2, n);
       
 95021   if( rc==0 ){
       
 95022     if( padFlag
       
 95023      && allSpaces(((char*)pKey1)+n, nKey1-n)
       
 95024      && allSpaces(((char*)pKey2)+n, nKey2-n)
       
 95025     ){
       
 95026       /* Leave rc unchanged at 0 */
       
 95027     }else{
       
 95028       rc = nKey1 - nKey2;
       
 95029     }
       
 95030   }
       
 95031   return rc;
       
 95032 }
       
 95033 
       
 95034 /*
       
 95035 ** Another built-in collating sequence: NOCASE. 
       
 95036 **
       
 95037 ** This collating sequence is intended to be used for "case independant
       
 95038 ** comparison". SQLite's knowledge of upper and lower case equivalents
       
 95039 ** extends only to the 26 characters used in the English language.
       
 95040 **
       
 95041 ** At the moment there is only a UTF-8 implementation.
       
 95042 */
       
 95043 static int nocaseCollatingFunc(
       
 95044   void *NotUsed,
       
 95045   int nKey1, const void *pKey1,
       
 95046   int nKey2, const void *pKey2
       
 95047 ){
       
 95048   int r = sqlite3StrNICmp(
       
 95049       (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
       
 95050   UNUSED_PARAMETER(NotUsed);
       
 95051   if( 0==r ){
       
 95052     r = nKey1-nKey2;
       
 95053   }
       
 95054   return r;
       
 95055 }
       
 95056 
       
 95057 /*
       
 95058 ** Return the ROWID of the most recent insert
       
 95059 */
       
 95060 SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
       
 95061   return db->lastRowid;
       
 95062 }
       
 95063 
       
 95064 /*
       
 95065 ** Return the number of changes in the most recent call to sqlite3_exec().
       
 95066 */
       
 95067 SQLITE_API int sqlite3_changes(sqlite3 *db){
       
 95068   return db->nChange;
       
 95069 }
       
 95070 
       
 95071 /*
       
 95072 ** Return the number of changes since the database handle was opened.
       
 95073 */
       
 95074 SQLITE_API int sqlite3_total_changes(sqlite3 *db){
       
 95075   return db->nTotalChange;
       
 95076 }
       
 95077 
       
 95078 /*
       
 95079 ** Close all open savepoints. This function only manipulates fields of the
       
 95080 ** database handle object, it does not close any savepoints that may be open
       
 95081 ** at the b-tree/pager level.
       
 95082 */
       
 95083 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
       
 95084   while( db->pSavepoint ){
       
 95085     Savepoint *pTmp = db->pSavepoint;
       
 95086     db->pSavepoint = pTmp->pNext;
       
 95087     sqlite3DbFree(db, pTmp);
       
 95088   }
       
 95089   db->nSavepoint = 0;
       
 95090   db->nStatement = 0;
       
 95091   db->isTransactionSavepoint = 0;
       
 95092 }
       
 95093 
       
 95094 /*
       
 95095 ** Close an existing SQLite database
       
 95096 */
       
 95097 SQLITE_API int sqlite3_close(sqlite3 *db){
       
 95098   HashElem *i;
       
 95099   int j;
       
 95100 
       
 95101   if( !db ){
       
 95102     return SQLITE_OK;
       
 95103   }
       
 95104   if( !sqlite3SafetyCheckSickOrOk(db) ){
       
 95105     return SQLITE_MISUSE;
       
 95106   }
       
 95107   sqlite3_mutex_enter(db->mutex);
       
 95108 
       
 95109   sqlite3ResetInternalSchema(db, 0);
       
 95110 
       
 95111   /* If a transaction is open, the ResetInternalSchema() call above
       
 95112   ** will not have called the xDisconnect() method on any virtual
       
 95113   ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
       
 95114   ** call will do so. We need to do this before the check for active
       
 95115   ** SQL statements below, as the v-table implementation may be storing
       
 95116   ** some prepared statements internally.
       
 95117   */
       
 95118   sqlite3VtabRollback(db);
       
 95119 
       
 95120   /* If there are any outstanding VMs, return SQLITE_BUSY. */
       
 95121   if( db->pVdbe ){
       
 95122     sqlite3Error(db, SQLITE_BUSY, 
       
 95123         "unable to close due to unfinalised statements");
       
 95124     sqlite3_mutex_leave(db->mutex);
       
 95125     return SQLITE_BUSY;
       
 95126   }
       
 95127   assert( sqlite3SafetyCheckSickOrOk(db) );
       
 95128 
       
 95129   for(j=0; j<db->nDb; j++){
       
 95130     Btree *pBt = db->aDb[j].pBt;
       
 95131     if( pBt && sqlite3BtreeIsInBackup(pBt) ){
       
 95132       sqlite3Error(db, SQLITE_BUSY, 
       
 95133           "unable to close due to unfinished backup operation");
       
 95134       sqlite3_mutex_leave(db->mutex);
       
 95135       return SQLITE_BUSY;
       
 95136     }
       
 95137   }
       
 95138 
       
 95139   /* Free any outstanding Savepoint structures. */
       
 95140   sqlite3CloseSavepoints(db);
       
 95141 
       
 95142   for(j=0; j<db->nDb; j++){
       
 95143     struct Db *pDb = &db->aDb[j];
       
 95144     if( pDb->pBt ){
       
 95145       sqlite3BtreeClose(pDb->pBt);
       
 95146       pDb->pBt = 0;
       
 95147       if( j!=1 ){
       
 95148         pDb->pSchema = 0;
       
 95149       }
       
 95150     }
       
 95151   }
       
 95152   sqlite3ResetInternalSchema(db, 0);
       
 95153 
       
 95154   /* Tell the code in notify.c that the connection no longer holds any
       
 95155   ** locks and does not require any further unlock-notify callbacks.
       
 95156   */
       
 95157   sqlite3ConnectionClosed(db);
       
 95158 
       
 95159   assert( db->nDb<=2 );
       
 95160   assert( db->aDb==db->aDbStatic );
       
 95161   for(j=0; j<ArraySize(db->aFunc.a); j++){
       
 95162     FuncDef *pNext, *pHash, *p;
       
 95163     for(p=db->aFunc.a[j]; p; p=pHash){
       
 95164       pHash = p->pHash;
       
 95165       while( p ){
       
 95166         pNext = p->pNext;
       
 95167         sqlite3DbFree(db, p);
       
 95168         p = pNext;
       
 95169       }
       
 95170     }
       
 95171   }
       
 95172   for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
       
 95173     CollSeq *pColl = (CollSeq *)sqliteHashData(i);
       
 95174     /* Invoke any destructors registered for collation sequence user data. */
       
 95175     for(j=0; j<3; j++){
       
 95176       if( pColl[j].xDel ){
       
 95177         pColl[j].xDel(pColl[j].pUser);
       
 95178       }
       
 95179     }
       
 95180     sqlite3DbFree(db, pColl);
       
 95181   }
       
 95182   sqlite3HashClear(&db->aCollSeq);
       
 95183 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 95184   for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
       
 95185     Module *pMod = (Module *)sqliteHashData(i);
       
 95186     if( pMod->xDestroy ){
       
 95187       pMod->xDestroy(pMod->pAux);
       
 95188     }
       
 95189     sqlite3DbFree(db, pMod);
       
 95190   }
       
 95191   sqlite3HashClear(&db->aModule);
       
 95192 #endif
       
 95193 
       
 95194   sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
       
 95195   if( db->pErr ){
       
 95196     sqlite3ValueFree(db->pErr);
       
 95197   }
       
 95198   sqlite3CloseExtensions(db);
       
 95199 
       
 95200   db->magic = SQLITE_MAGIC_ERROR;
       
 95201 
       
 95202   /* The temp-database schema is allocated differently from the other schema
       
 95203   ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
       
 95204   ** So it needs to be freed here. Todo: Why not roll the temp schema into
       
 95205   ** the same sqliteMalloc() as the one that allocates the database 
       
 95206   ** structure?
       
 95207   */
       
 95208   sqlite3DbFree(db, db->aDb[1].pSchema);
       
 95209   sqlite3_mutex_leave(db->mutex);
       
 95210   db->magic = SQLITE_MAGIC_CLOSED;
       
 95211   sqlite3_mutex_free(db->mutex);
       
 95212   assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
       
 95213   if( db->lookaside.bMalloced ){
       
 95214     sqlite3_free(db->lookaside.pStart);
       
 95215   }
       
 95216   sqlite3_free(db);
       
 95217   return SQLITE_OK;
       
 95218 }
       
 95219 
       
 95220 /*
       
 95221 ** Rollback all database files.
       
 95222 */
       
 95223 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db){
       
 95224   int i;
       
 95225   int inTrans = 0;
       
 95226   assert( sqlite3_mutex_held(db->mutex) );
       
 95227   sqlite3BeginBenignMalloc();
       
 95228   for(i=0; i<db->nDb; i++){
       
 95229     if( db->aDb[i].pBt ){
       
 95230       if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
       
 95231         inTrans = 1;
       
 95232       }
       
 95233       sqlite3BtreeRollback(db->aDb[i].pBt);
       
 95234       db->aDb[i].inTrans = 0;
       
 95235     }
       
 95236   }
       
 95237   sqlite3VtabRollback(db);
       
 95238   sqlite3EndBenignMalloc();
       
 95239 
       
 95240   if( db->flags&SQLITE_InternChanges ){
       
 95241     sqlite3ExpirePreparedStatements(db);
       
 95242     sqlite3ResetInternalSchema(db, 0);
       
 95243   }
       
 95244 
       
 95245   /* Any deferred constraint violations have now been resolved. */
       
 95246   db->nDeferredCons = 0;
       
 95247 
       
 95248   /* If one has been configured, invoke the rollback-hook callback */
       
 95249   if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
       
 95250     db->xRollbackCallback(db->pRollbackArg);
       
 95251   }
       
 95252 }
       
 95253 
       
 95254 /*
       
 95255 ** Return a static string that describes the kind of error specified in the
       
 95256 ** argument.
       
 95257 */
       
 95258 SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
       
 95259   static const char* const aMsg[] = {
       
 95260     /* SQLITE_OK          */ "not an error",
       
 95261     /* SQLITE_ERROR       */ "SQL logic error or missing database",
       
 95262     /* SQLITE_INTERNAL    */ 0,
       
 95263     /* SQLITE_PERM        */ "access permission denied",
       
 95264     /* SQLITE_ABORT       */ "callback requested query abort",
       
 95265     /* SQLITE_BUSY        */ "database is locked",
       
 95266     /* SQLITE_LOCKED      */ "database table is locked",
       
 95267     /* SQLITE_NOMEM       */ "out of memory",
       
 95268     /* SQLITE_READONLY    */ "attempt to write a readonly database",
       
 95269     /* SQLITE_INTERRUPT   */ "interrupted",
       
 95270     /* SQLITE_IOERR       */ "disk I/O error",
       
 95271     /* SQLITE_CORRUPT     */ "database disk image is malformed",
       
 95272     /* SQLITE_NOTFOUND    */ 0,
       
 95273     /* SQLITE_FULL        */ "database or disk is full",
       
 95274     /* SQLITE_CANTOPEN    */ "unable to open database file",
       
 95275     /* SQLITE_PROTOCOL    */ 0,
       
 95276     /* SQLITE_EMPTY       */ "table contains no data",
       
 95277     /* SQLITE_SCHEMA      */ "database schema has changed",
       
 95278     /* SQLITE_TOOBIG      */ "string or blob too big",
       
 95279     /* SQLITE_CONSTRAINT  */ "constraint failed",
       
 95280     /* SQLITE_MISMATCH    */ "datatype mismatch",
       
 95281     /* SQLITE_MISUSE      */ "library routine called out of sequence",
       
 95282     /* SQLITE_NOLFS       */ "large file support is disabled",
       
 95283     /* SQLITE_AUTH        */ "authorization denied",
       
 95284     /* SQLITE_FORMAT      */ "auxiliary database format error",
       
 95285     /* SQLITE_RANGE       */ "bind or column index out of range",
       
 95286     /* SQLITE_NOTADB      */ "file is encrypted or is not a database",
       
 95287   };
       
 95288   rc &= 0xff;
       
 95289   if( ALWAYS(rc>=0) && rc<(int)(sizeof(aMsg)/sizeof(aMsg[0])) && aMsg[rc]!=0 ){
       
 95290     return aMsg[rc];
       
 95291   }else{
       
 95292     return "unknown error";
       
 95293   }
       
 95294 }
       
 95295 
       
 95296 /*
       
 95297 ** This routine implements a busy callback that sleeps and tries
       
 95298 ** again until a timeout value is reached.  The timeout value is
       
 95299 ** an integer number of milliseconds passed in as the first
       
 95300 ** argument.
       
 95301 */
       
 95302 static int sqliteDefaultBusyCallback(
       
 95303  void *ptr,               /* Database connection */
       
 95304  int count                /* Number of times table has been busy */
       
 95305 ){
       
 95306 #if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
       
 95307   static const u8 delays[] =
       
 95308      { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
       
 95309   static const u8 totals[] =
       
 95310      { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
       
 95311 # define NDELAY (sizeof(delays)/sizeof(delays[0]))
       
 95312   sqlite3 *db = (sqlite3 *)ptr;
       
 95313   int timeout = db->busyTimeout;
       
 95314   int delay, prior;
       
 95315 
       
 95316   assert( count>=0 );
       
 95317   if( count < NDELAY ){
       
 95318     delay = delays[count];
       
 95319     prior = totals[count];
       
 95320   }else{
       
 95321     delay = delays[NDELAY-1];
       
 95322     prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
       
 95323   }
       
 95324   if( prior + delay > timeout ){
       
 95325     delay = timeout - prior;
       
 95326     if( delay<=0 ) return 0;
       
 95327   }
       
 95328   sqlite3OsSleep(db->pVfs, delay*1000);
       
 95329   return 1;
       
 95330 #else
       
 95331   sqlite3 *db = (sqlite3 *)ptr;
       
 95332   int timeout = ((sqlite3 *)ptr)->busyTimeout;
       
 95333   if( (count+1)*1000 > timeout ){
       
 95334     return 0;
       
 95335   }
       
 95336   sqlite3OsSleep(db->pVfs, 1000000);
       
 95337   return 1;
       
 95338 #endif
       
 95339 }
       
 95340 
       
 95341 /*
       
 95342 ** Invoke the given busy handler.
       
 95343 **
       
 95344 ** This routine is called when an operation failed with a lock.
       
 95345 ** If this routine returns non-zero, the lock is retried.  If it
       
 95346 ** returns 0, the operation aborts with an SQLITE_BUSY error.
       
 95347 */
       
 95348 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
       
 95349   int rc;
       
 95350   if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
       
 95351   rc = p->xFunc(p->pArg, p->nBusy);
       
 95352   if( rc==0 ){
       
 95353     p->nBusy = -1;
       
 95354   }else{
       
 95355     p->nBusy++;
       
 95356   }
       
 95357   return rc; 
       
 95358 }
       
 95359 
       
 95360 /*
       
 95361 ** This routine sets the busy callback for an Sqlite database to the
       
 95362 ** given callback function with the given argument.
       
 95363 */
       
 95364 SQLITE_API int sqlite3_busy_handler(
       
 95365   sqlite3 *db,
       
 95366   int (*xBusy)(void*,int),
       
 95367   void *pArg
       
 95368 ){
       
 95369   sqlite3_mutex_enter(db->mutex);
       
 95370   db->busyHandler.xFunc = xBusy;
       
 95371   db->busyHandler.pArg = pArg;
       
 95372   db->busyHandler.nBusy = 0;
       
 95373   sqlite3_mutex_leave(db->mutex);
       
 95374   return SQLITE_OK;
       
 95375 }
       
 95376 
       
 95377 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
       
 95378 /*
       
 95379 ** This routine sets the progress callback for an Sqlite database to the
       
 95380 ** given callback function with the given argument. The progress callback will
       
 95381 ** be invoked every nOps opcodes.
       
 95382 */
       
 95383 SQLITE_API void sqlite3_progress_handler(
       
 95384   sqlite3 *db, 
       
 95385   int nOps,
       
 95386   int (*xProgress)(void*), 
       
 95387   void *pArg
       
 95388 ){
       
 95389   sqlite3_mutex_enter(db->mutex);
       
 95390   if( nOps>0 ){
       
 95391     db->xProgress = xProgress;
       
 95392     db->nProgressOps = nOps;
       
 95393     db->pProgressArg = pArg;
       
 95394   }else{
       
 95395     db->xProgress = 0;
       
 95396     db->nProgressOps = 0;
       
 95397     db->pProgressArg = 0;
       
 95398   }
       
 95399   sqlite3_mutex_leave(db->mutex);
       
 95400 }
       
 95401 #endif
       
 95402 
       
 95403 
       
 95404 /*
       
 95405 ** This routine installs a default busy handler that waits for the
       
 95406 ** specified number of milliseconds before returning 0.
       
 95407 */
       
 95408 SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
       
 95409   if( ms>0 ){
       
 95410     db->busyTimeout = ms;
       
 95411     sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
       
 95412   }else{
       
 95413     sqlite3_busy_handler(db, 0, 0);
       
 95414   }
       
 95415   return SQLITE_OK;
       
 95416 }
       
 95417 
       
 95418 /*
       
 95419 ** Cause any pending operation to stop at its earliest opportunity.
       
 95420 */
       
 95421 SQLITE_API void sqlite3_interrupt(sqlite3 *db){
       
 95422   db->u1.isInterrupted = 1;
       
 95423 }
       
 95424 
       
 95425 
       
 95426 /*
       
 95427 ** This function is exactly the same as sqlite3_create_function(), except
       
 95428 ** that it is designed to be called by internal code. The difference is
       
 95429 ** that if a malloc() fails in sqlite3_create_function(), an error code
       
 95430 ** is returned and the mallocFailed flag cleared. 
       
 95431 */
       
 95432 SQLITE_PRIVATE int sqlite3CreateFunc(
       
 95433   sqlite3 *db,
       
 95434   const char *zFunctionName,
       
 95435   int nArg,
       
 95436   int enc,
       
 95437   void *pUserData,
       
 95438   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
       
 95439   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
       
 95440   void (*xFinal)(sqlite3_context*)
       
 95441 ){
       
 95442   FuncDef *p;
       
 95443   int nName;
       
 95444 
       
 95445   assert( sqlite3_mutex_held(db->mutex) );
       
 95446   if( zFunctionName==0 ||
       
 95447       (xFunc && (xFinal || xStep)) || 
       
 95448       (!xFunc && (xFinal && !xStep)) ||
       
 95449       (!xFunc && (!xFinal && xStep)) ||
       
 95450       (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
       
 95451       (255<(nName = sqlite3Strlen30( zFunctionName))) ){
       
 95452     return SQLITE_MISUSE;
       
 95453   }
       
 95454   
       
 95455 #ifndef SQLITE_OMIT_UTF16
       
 95456   /* If SQLITE_UTF16 is specified as the encoding type, transform this
       
 95457   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
       
 95458   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
       
 95459   **
       
 95460   ** If SQLITE_ANY is specified, add three versions of the function
       
 95461   ** to the hash table.
       
 95462   */
       
 95463   if( enc==SQLITE_UTF16 ){
       
 95464     enc = SQLITE_UTF16NATIVE;
       
 95465   }else if( enc==SQLITE_ANY ){
       
 95466     int rc;
       
 95467     rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
       
 95468          pUserData, xFunc, xStep, xFinal);
       
 95469     if( rc==SQLITE_OK ){
       
 95470       rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
       
 95471           pUserData, xFunc, xStep, xFinal);
       
 95472     }
       
 95473     if( rc!=SQLITE_OK ){
       
 95474       return rc;
       
 95475     }
       
 95476     enc = SQLITE_UTF16BE;
       
 95477   }
       
 95478 #else
       
 95479   enc = SQLITE_UTF8;
       
 95480 #endif
       
 95481   
       
 95482   /* Check if an existing function is being overridden or deleted. If so,
       
 95483   ** and there are active VMs, then return SQLITE_BUSY. If a function
       
 95484   ** is being overridden/deleted but there are no active VMs, allow the
       
 95485   ** operation to continue but invalidate all precompiled statements.
       
 95486   */
       
 95487   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
       
 95488   if( p && p->iPrefEnc==enc && p->nArg==nArg ){
       
 95489     if( db->activeVdbeCnt ){
       
 95490       sqlite3Error(db, SQLITE_BUSY, 
       
 95491         "unable to delete/modify user-function due to active statements");
       
 95492       assert( !db->mallocFailed );
       
 95493       return SQLITE_BUSY;
       
 95494     }else{
       
 95495       sqlite3ExpirePreparedStatements(db);
       
 95496     }
       
 95497   }
       
 95498 
       
 95499   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
       
 95500   assert(p || db->mallocFailed);
       
 95501   if( !p ){
       
 95502     return SQLITE_NOMEM;
       
 95503   }
       
 95504   p->flags = 0;
       
 95505   p->xFunc = xFunc;
       
 95506   p->xStep = xStep;
       
 95507   p->xFinalize = xFinal;
       
 95508   p->pUserData = pUserData;
       
 95509   p->nArg = (u16)nArg;
       
 95510   return SQLITE_OK;
       
 95511 }
       
 95512 
       
 95513 /*
       
 95514 ** Create new user functions.
       
 95515 */
       
 95516 SQLITE_API int sqlite3_create_function(
       
 95517   sqlite3 *db,
       
 95518   const char *zFunctionName,
       
 95519   int nArg,
       
 95520   int enc,
       
 95521   void *p,
       
 95522   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
       
 95523   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
       
 95524   void (*xFinal)(sqlite3_context*)
       
 95525 ){
       
 95526   int rc;
       
 95527   sqlite3_mutex_enter(db->mutex);
       
 95528   rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal);
       
 95529   rc = sqlite3ApiExit(db, rc);
       
 95530   sqlite3_mutex_leave(db->mutex);
       
 95531   return rc;
       
 95532 }
       
 95533 
       
 95534 #ifndef SQLITE_OMIT_UTF16
       
 95535 SQLITE_API int sqlite3_create_function16(
       
 95536   sqlite3 *db,
       
 95537   const void *zFunctionName,
       
 95538   int nArg,
       
 95539   int eTextRep,
       
 95540   void *p,
       
 95541   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
       
 95542   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
       
 95543   void (*xFinal)(sqlite3_context*)
       
 95544 ){
       
 95545   int rc;
       
 95546   char *zFunc8;
       
 95547   sqlite3_mutex_enter(db->mutex);
       
 95548   assert( !db->mallocFailed );
       
 95549   zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1);
       
 95550   rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal);
       
 95551   sqlite3DbFree(db, zFunc8);
       
 95552   rc = sqlite3ApiExit(db, rc);
       
 95553   sqlite3_mutex_leave(db->mutex);
       
 95554   return rc;
       
 95555 }
       
 95556 #endif
       
 95557 
       
 95558 
       
 95559 /*
       
 95560 ** Declare that a function has been overloaded by a virtual table.
       
 95561 **
       
 95562 ** If the function already exists as a regular global function, then
       
 95563 ** this routine is a no-op.  If the function does not exist, then create
       
 95564 ** a new one that always throws a run-time error.  
       
 95565 **
       
 95566 ** When virtual tables intend to provide an overloaded function, they
       
 95567 ** should call this routine to make sure the global function exists.
       
 95568 ** A global function must exist in order for name resolution to work
       
 95569 ** properly.
       
 95570 */
       
 95571 SQLITE_API int sqlite3_overload_function(
       
 95572   sqlite3 *db,
       
 95573   const char *zName,
       
 95574   int nArg
       
 95575 ){
       
 95576   int nName = sqlite3Strlen30(zName);
       
 95577   int rc;
       
 95578   sqlite3_mutex_enter(db->mutex);
       
 95579   if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
       
 95580     sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
       
 95581                       0, sqlite3InvalidFunction, 0, 0);
       
 95582   }
       
 95583   rc = sqlite3ApiExit(db, SQLITE_OK);
       
 95584   sqlite3_mutex_leave(db->mutex);
       
 95585   return rc;
       
 95586 }
       
 95587 
       
 95588 #ifndef SQLITE_OMIT_TRACE
       
 95589 /*
       
 95590 ** Register a trace function.  The pArg from the previously registered trace
       
 95591 ** is returned.  
       
 95592 **
       
 95593 ** A NULL trace function means that no tracing is executes.  A non-NULL
       
 95594 ** trace is a pointer to a function that is invoked at the start of each
       
 95595 ** SQL statement.
       
 95596 */
       
 95597 SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
       
 95598   void *pOld;
       
 95599   sqlite3_mutex_enter(db->mutex);
       
 95600   pOld = db->pTraceArg;
       
 95601   db->xTrace = xTrace;
       
 95602   db->pTraceArg = pArg;
       
 95603   sqlite3_mutex_leave(db->mutex);
       
 95604   return pOld;
       
 95605 }
       
 95606 /*
       
 95607 ** Register a profile function.  The pArg from the previously registered 
       
 95608 ** profile function is returned.  
       
 95609 **
       
 95610 ** A NULL profile function means that no profiling is executes.  A non-NULL
       
 95611 ** profile is a pointer to a function that is invoked at the conclusion of
       
 95612 ** each SQL statement that is run.
       
 95613 */
       
 95614 SQLITE_API void *sqlite3_profile(
       
 95615   sqlite3 *db,
       
 95616   void (*xProfile)(void*,const char*,sqlite_uint64),
       
 95617   void *pArg
       
 95618 ){
       
 95619   void *pOld;
       
 95620   sqlite3_mutex_enter(db->mutex);
       
 95621   pOld = db->pProfileArg;
       
 95622   db->xProfile = xProfile;
       
 95623   db->pProfileArg = pArg;
       
 95624   sqlite3_mutex_leave(db->mutex);
       
 95625   return pOld;
       
 95626 }
       
 95627 #endif /* SQLITE_OMIT_TRACE */
       
 95628 
       
 95629 /*** EXPERIMENTAL ***
       
 95630 **
       
 95631 ** Register a function to be invoked when a transaction comments.
       
 95632 ** If the invoked function returns non-zero, then the commit becomes a
       
 95633 ** rollback.
       
 95634 */
       
 95635 SQLITE_API void *sqlite3_commit_hook(
       
 95636   sqlite3 *db,              /* Attach the hook to this database */
       
 95637   int (*xCallback)(void*),  /* Function to invoke on each commit */
       
 95638   void *pArg                /* Argument to the function */
       
 95639 ){
       
 95640   void *pOld;
       
 95641   sqlite3_mutex_enter(db->mutex);
       
 95642   pOld = db->pCommitArg;
       
 95643   db->xCommitCallback = xCallback;
       
 95644   db->pCommitArg = pArg;
       
 95645   sqlite3_mutex_leave(db->mutex);
       
 95646   return pOld;
       
 95647 }
       
 95648 
       
 95649 /*
       
 95650 ** Register a callback to be invoked each time a row is updated,
       
 95651 ** inserted or deleted using this database connection.
       
 95652 */
       
 95653 SQLITE_API void *sqlite3_update_hook(
       
 95654   sqlite3 *db,              /* Attach the hook to this database */
       
 95655   void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
       
 95656   void *pArg                /* Argument to the function */
       
 95657 ){
       
 95658   void *pRet;
       
 95659   sqlite3_mutex_enter(db->mutex);
       
 95660   pRet = db->pUpdateArg;
       
 95661   db->xUpdateCallback = xCallback;
       
 95662   db->pUpdateArg = pArg;
       
 95663   sqlite3_mutex_leave(db->mutex);
       
 95664   return pRet;
       
 95665 }
       
 95666 
       
 95667 /*
       
 95668 ** Register a callback to be invoked each time a transaction is rolled
       
 95669 ** back by this database connection.
       
 95670 */
       
 95671 SQLITE_API void *sqlite3_rollback_hook(
       
 95672   sqlite3 *db,              /* Attach the hook to this database */
       
 95673   void (*xCallback)(void*), /* Callback function */
       
 95674   void *pArg                /* Argument to the function */
       
 95675 ){
       
 95676   void *pRet;
       
 95677   sqlite3_mutex_enter(db->mutex);
       
 95678   pRet = db->pRollbackArg;
       
 95679   db->xRollbackCallback = xCallback;
       
 95680   db->pRollbackArg = pArg;
       
 95681   sqlite3_mutex_leave(db->mutex);
       
 95682   return pRet;
       
 95683 }
       
 95684 
       
 95685 /*
       
 95686 ** This function returns true if main-memory should be used instead of
       
 95687 ** a temporary file for transient pager files and statement journals.
       
 95688 ** The value returned depends on the value of db->temp_store (runtime
       
 95689 ** parameter) and the compile time value of SQLITE_TEMP_STORE. The
       
 95690 ** following table describes the relationship between these two values
       
 95691 ** and this functions return value.
       
 95692 **
       
 95693 **   SQLITE_TEMP_STORE     db->temp_store     Location of temporary database
       
 95694 **   -----------------     --------------     ------------------------------
       
 95695 **   0                     any                file      (return 0)
       
 95696 **   1                     1                  file      (return 0)
       
 95697 **   1                     2                  memory    (return 1)
       
 95698 **   1                     0                  file      (return 0)
       
 95699 **   2                     1                  file      (return 0)
       
 95700 **   2                     2                  memory    (return 1)
       
 95701 **   2                     0                  memory    (return 1)
       
 95702 **   3                     any                memory    (return 1)
       
 95703 */
       
 95704 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
       
 95705 #if SQLITE_TEMP_STORE==1
       
 95706   return ( db->temp_store==2 );
       
 95707 #endif
       
 95708 #if SQLITE_TEMP_STORE==2
       
 95709   return ( db->temp_store!=1 );
       
 95710 #endif
       
 95711 #if SQLITE_TEMP_STORE==3
       
 95712   return 1;
       
 95713 #endif
       
 95714 #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
       
 95715   return 0;
       
 95716 #endif
       
 95717 }
       
 95718 
       
 95719 /*
       
 95720 ** This routine is called to create a connection to a database BTree
       
 95721 ** driver.  If zFilename is the name of a file, then that file is
       
 95722 ** opened and used.  If zFilename is the magic name ":memory:" then
       
 95723 ** the database is stored in memory (and is thus forgotten as soon as
       
 95724 ** the connection is closed.)  If zFilename is NULL then the database
       
 95725 ** is a "virtual" database for transient use only and is deleted as
       
 95726 ** soon as the connection is closed.
       
 95727 **
       
 95728 ** A virtual database can be either a disk file (that is automatically
       
 95729 ** deleted when the file is closed) or it an be held entirely in memory.
       
 95730 ** The sqlite3TempInMemory() function is used to determine which.
       
 95731 */
       
 95732 SQLITE_PRIVATE int sqlite3BtreeFactory(
       
 95733   const sqlite3 *db,        /* Main database when opening aux otherwise 0 */
       
 95734   const char *zFilename,    /* Name of the file containing the BTree database */
       
 95735   int omitJournal,          /* if TRUE then do not journal this file */
       
 95736   int nCache,               /* How many pages in the page cache */
       
 95737   int vfsFlags,             /* Flags passed through to vfsOpen */
       
 95738   Btree **ppBtree           /* Pointer to new Btree object written here */
       
 95739 ){
       
 95740   int btFlags = 0;
       
 95741   int rc;
       
 95742   
       
 95743   assert( sqlite3_mutex_held(db->mutex) );
       
 95744   assert( ppBtree != 0);
       
 95745   if( omitJournal ){
       
 95746     btFlags |= BTREE_OMIT_JOURNAL;
       
 95747   }
       
 95748   if( db->flags & SQLITE_NoReadlock ){
       
 95749     btFlags |= BTREE_NO_READLOCK;
       
 95750   }
       
 95751 #ifndef SQLITE_OMIT_MEMORYDB
       
 95752   if( zFilename==0 && sqlite3TempInMemory(db) ){
       
 95753     zFilename = ":memory:";
       
 95754   }
       
 95755 #endif
       
 95756 
       
 95757   if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (zFilename==0 || *zFilename==0) ){
       
 95758     vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
       
 95759   }
       
 95760   rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btFlags, vfsFlags);
       
 95761 
       
 95762   /* If the B-Tree was successfully opened, set the pager-cache size to the
       
 95763   ** default value. Except, if the call to BtreeOpen() returned a handle
       
 95764   ** open on an existing shared pager-cache, do not change the pager-cache 
       
 95765   ** size.
       
 95766   */
       
 95767   if( rc==SQLITE_OK && 0==sqlite3BtreeSchema(*ppBtree, 0, 0) ){
       
 95768     sqlite3BtreeSetCacheSize(*ppBtree, nCache);
       
 95769   }
       
 95770   return rc;
       
 95771 }
       
 95772 
       
 95773 /*
       
 95774 ** Return UTF-8 encoded English language explanation of the most recent
       
 95775 ** error.
       
 95776 */
       
 95777 SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
       
 95778   const char *z;
       
 95779   if( !db ){
       
 95780     return sqlite3ErrStr(SQLITE_NOMEM);
       
 95781   }
       
 95782   if( !sqlite3SafetyCheckSickOrOk(db) ){
       
 95783     return sqlite3ErrStr(SQLITE_MISUSE);
       
 95784   }
       
 95785   sqlite3_mutex_enter(db->mutex);
       
 95786   if( db->mallocFailed ){
       
 95787     z = sqlite3ErrStr(SQLITE_NOMEM);
       
 95788   }else{
       
 95789     z = (char*)sqlite3_value_text(db->pErr);
       
 95790     assert( !db->mallocFailed );
       
 95791     if( z==0 ){
       
 95792       z = sqlite3ErrStr(db->errCode);
       
 95793     }
       
 95794   }
       
 95795   sqlite3_mutex_leave(db->mutex);
       
 95796   return z;
       
 95797 }
       
 95798 
       
 95799 #ifndef SQLITE_OMIT_UTF16
       
 95800 /*
       
 95801 ** Return UTF-16 encoded English language explanation of the most recent
       
 95802 ** error.
       
 95803 */
       
 95804 SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
       
 95805   static const u16 outOfMem[] = {
       
 95806     'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
       
 95807   };
       
 95808   static const u16 misuse[] = {
       
 95809     'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ', 
       
 95810     'r', 'o', 'u', 't', 'i', 'n', 'e', ' ', 
       
 95811     'c', 'a', 'l', 'l', 'e', 'd', ' ', 
       
 95812     'o', 'u', 't', ' ', 
       
 95813     'o', 'f', ' ', 
       
 95814     's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
       
 95815   };
       
 95816 
       
 95817   const void *z;
       
 95818   if( !db ){
       
 95819     return (void *)outOfMem;
       
 95820   }
       
 95821   if( !sqlite3SafetyCheckSickOrOk(db) ){
       
 95822     return (void *)misuse;
       
 95823   }
       
 95824   sqlite3_mutex_enter(db->mutex);
       
 95825   if( db->mallocFailed ){
       
 95826     z = (void *)outOfMem;
       
 95827   }else{
       
 95828     z = sqlite3_value_text16(db->pErr);
       
 95829     if( z==0 ){
       
 95830       sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
       
 95831            SQLITE_UTF8, SQLITE_STATIC);
       
 95832       z = sqlite3_value_text16(db->pErr);
       
 95833     }
       
 95834     /* A malloc() may have failed within the call to sqlite3_value_text16()
       
 95835     ** above. If this is the case, then the db->mallocFailed flag needs to
       
 95836     ** be cleared before returning. Do this directly, instead of via
       
 95837     ** sqlite3ApiExit(), to avoid setting the database handle error message.
       
 95838     */
       
 95839     db->mallocFailed = 0;
       
 95840   }
       
 95841   sqlite3_mutex_leave(db->mutex);
       
 95842   return z;
       
 95843 }
       
 95844 #endif /* SQLITE_OMIT_UTF16 */
       
 95845 
       
 95846 /*
       
 95847 ** Return the most recent error code generated by an SQLite routine. If NULL is
       
 95848 ** passed to this function, we assume a malloc() failed during sqlite3_open().
       
 95849 */
       
 95850 SQLITE_API int sqlite3_errcode(sqlite3 *db){
       
 95851   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
       
 95852     return SQLITE_MISUSE;
       
 95853   }
       
 95854   if( !db || db->mallocFailed ){
       
 95855     return SQLITE_NOMEM;
       
 95856   }
       
 95857   return db->errCode & db->errMask;
       
 95858 }
       
 95859 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
       
 95860   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
       
 95861     return SQLITE_MISUSE;
       
 95862   }
       
 95863   if( !db || db->mallocFailed ){
       
 95864     return SQLITE_NOMEM;
       
 95865   }
       
 95866   return db->errCode;
       
 95867 }
       
 95868 
       
 95869 /*
       
 95870 ** Create a new collating function for database "db".  The name is zName
       
 95871 ** and the encoding is enc.
       
 95872 */
       
 95873 static int createCollation(
       
 95874   sqlite3* db,
       
 95875   const char *zName, 
       
 95876   u8 enc,
       
 95877   u8 collType,
       
 95878   void* pCtx,
       
 95879   int(*xCompare)(void*,int,const void*,int,const void*),
       
 95880   void(*xDel)(void*)
       
 95881 ){
       
 95882   CollSeq *pColl;
       
 95883   int enc2;
       
 95884   int nName = sqlite3Strlen30(zName);
       
 95885   
       
 95886   assert( sqlite3_mutex_held(db->mutex) );
       
 95887 
       
 95888   /* If SQLITE_UTF16 is specified as the encoding type, transform this
       
 95889   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
       
 95890   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
       
 95891   */
       
 95892   enc2 = enc;
       
 95893   testcase( enc2==SQLITE_UTF16 );
       
 95894   testcase( enc2==SQLITE_UTF16_ALIGNED );
       
 95895   if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
       
 95896     enc2 = SQLITE_UTF16NATIVE;
       
 95897   }
       
 95898   if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
       
 95899     return SQLITE_MISUSE;
       
 95900   }
       
 95901 
       
 95902   /* Check if this call is removing or replacing an existing collation 
       
 95903   ** sequence. If so, and there are active VMs, return busy. If there
       
 95904   ** are no active VMs, invalidate any pre-compiled statements.
       
 95905   */
       
 95906   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
       
 95907   if( pColl && pColl->xCmp ){
       
 95908     if( db->activeVdbeCnt ){
       
 95909       sqlite3Error(db, SQLITE_BUSY, 
       
 95910         "unable to delete/modify collation sequence due to active statements");
       
 95911       return SQLITE_BUSY;
       
 95912     }
       
 95913     sqlite3ExpirePreparedStatements(db);
       
 95914 
       
 95915     /* If collation sequence pColl was created directly by a call to
       
 95916     ** sqlite3_create_collation, and not generated by synthCollSeq(),
       
 95917     ** then any copies made by synthCollSeq() need to be invalidated.
       
 95918     ** Also, collation destructor - CollSeq.xDel() - function may need
       
 95919     ** to be called.
       
 95920     */ 
       
 95921     if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
       
 95922       CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
       
 95923       int j;
       
 95924       for(j=0; j<3; j++){
       
 95925         CollSeq *p = &aColl[j];
       
 95926         if( p->enc==pColl->enc ){
       
 95927           if( p->xDel ){
       
 95928             p->xDel(p->pUser);
       
 95929           }
       
 95930           p->xCmp = 0;
       
 95931         }
       
 95932       }
       
 95933     }
       
 95934   }
       
 95935 
       
 95936   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
       
 95937   if( pColl ){
       
 95938     pColl->xCmp = xCompare;
       
 95939     pColl->pUser = pCtx;
       
 95940     pColl->xDel = xDel;
       
 95941     pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
       
 95942     pColl->type = collType;
       
 95943   }
       
 95944   sqlite3Error(db, SQLITE_OK, 0);
       
 95945   return SQLITE_OK;
       
 95946 }
       
 95947 
       
 95948 
       
 95949 /*
       
 95950 ** This array defines hard upper bounds on limit values.  The
       
 95951 ** initializer must be kept in sync with the SQLITE_LIMIT_*
       
 95952 ** #defines in sqlite3.h.
       
 95953 */
       
 95954 static const int aHardLimit[] = {
       
 95955   SQLITE_MAX_LENGTH,
       
 95956   SQLITE_MAX_SQL_LENGTH,
       
 95957   SQLITE_MAX_COLUMN,
       
 95958   SQLITE_MAX_EXPR_DEPTH,
       
 95959   SQLITE_MAX_COMPOUND_SELECT,
       
 95960   SQLITE_MAX_VDBE_OP,
       
 95961   SQLITE_MAX_FUNCTION_ARG,
       
 95962   SQLITE_MAX_ATTACHED,
       
 95963   SQLITE_MAX_LIKE_PATTERN_LENGTH,
       
 95964   SQLITE_MAX_VARIABLE_NUMBER,
       
 95965   SQLITE_MAX_TRIGGER_DEPTH,
       
 95966 };
       
 95967 
       
 95968 /*
       
 95969 ** Make sure the hard limits are set to reasonable values
       
 95970 */
       
 95971 #if SQLITE_MAX_LENGTH<100
       
 95972 # error SQLITE_MAX_LENGTH must be at least 100
       
 95973 #endif
       
 95974 #if SQLITE_MAX_SQL_LENGTH<100
       
 95975 # error SQLITE_MAX_SQL_LENGTH must be at least 100
       
 95976 #endif
       
 95977 #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
       
 95978 # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
       
 95979 #endif
       
 95980 #if SQLITE_MAX_COMPOUND_SELECT<2
       
 95981 # error SQLITE_MAX_COMPOUND_SELECT must be at least 2
       
 95982 #endif
       
 95983 #if SQLITE_MAX_VDBE_OP<40
       
 95984 # error SQLITE_MAX_VDBE_OP must be at least 40
       
 95985 #endif
       
 95986 #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
       
 95987 # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
       
 95988 #endif
       
 95989 #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>30
       
 95990 # error SQLITE_MAX_ATTACHED must be between 0 and 30
       
 95991 #endif
       
 95992 #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
       
 95993 # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
       
 95994 #endif
       
 95995 #if SQLITE_MAX_VARIABLE_NUMBER<1
       
 95996 # error SQLITE_MAX_VARIABLE_NUMBER must be at least 1
       
 95997 #endif
       
 95998 #if SQLITE_MAX_COLUMN>32767
       
 95999 # error SQLITE_MAX_COLUMN must not exceed 32767
       
 96000 #endif
       
 96001 #if SQLITE_MAX_TRIGGER_DEPTH<1
       
 96002 # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
       
 96003 #endif
       
 96004 
       
 96005 
       
 96006 /*
       
 96007 ** Change the value of a limit.  Report the old value.
       
 96008 ** If an invalid limit index is supplied, report -1.
       
 96009 ** Make no changes but still report the old value if the
       
 96010 ** new limit is negative.
       
 96011 **
       
 96012 ** A new lower limit does not shrink existing constructs.
       
 96013 ** It merely prevents new constructs that exceed the limit
       
 96014 ** from forming.
       
 96015 */
       
 96016 SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
       
 96017   int oldLimit;
       
 96018   if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
       
 96019     return -1;
       
 96020   }
       
 96021   oldLimit = db->aLimit[limitId];
       
 96022   if( newLimit>=0 ){
       
 96023     if( newLimit>aHardLimit[limitId] ){
       
 96024       newLimit = aHardLimit[limitId];
       
 96025     }
       
 96026     db->aLimit[limitId] = newLimit;
       
 96027   }
       
 96028   return oldLimit;
       
 96029 }
       
 96030 
       
 96031 /*
       
 96032 ** This routine does the work of opening a database on behalf of
       
 96033 ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"  
       
 96034 ** is UTF-8 encoded.
       
 96035 */
       
 96036 static int openDatabase(
       
 96037   const char *zFilename, /* Database filename UTF-8 encoded */
       
 96038   sqlite3 **ppDb,        /* OUT: Returned database handle */
       
 96039   unsigned flags,        /* Operational flags */
       
 96040   const char *zVfs       /* Name of the VFS to use */
       
 96041 ){
       
 96042   sqlite3 *db;
       
 96043   int rc;
       
 96044   int isThreadsafe;
       
 96045 
       
 96046   *ppDb = 0;
       
 96047 #ifndef SQLITE_OMIT_AUTOINIT
       
 96048   rc = sqlite3_initialize();
       
 96049   if( rc ) return rc;
       
 96050 #endif
       
 96051 
       
 96052   if( sqlite3GlobalConfig.bCoreMutex==0 ){
       
 96053     isThreadsafe = 0;
       
 96054   }else if( flags & SQLITE_OPEN_NOMUTEX ){
       
 96055     isThreadsafe = 0;
       
 96056   }else if( flags & SQLITE_OPEN_FULLMUTEX ){
       
 96057     isThreadsafe = 1;
       
 96058   }else{
       
 96059     isThreadsafe = sqlite3GlobalConfig.bFullMutex;
       
 96060   }
       
 96061   if( flags & SQLITE_OPEN_PRIVATECACHE ){
       
 96062     flags &= ~SQLITE_OPEN_SHAREDCACHE;
       
 96063   }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
       
 96064     flags |= SQLITE_OPEN_SHAREDCACHE;
       
 96065   }
       
 96066 
       
 96067   /* Remove harmful bits from the flags parameter
       
 96068   **
       
 96069   ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
       
 96070   ** dealt with in the previous code block.  Besides these, the only
       
 96071   ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
       
 96072   ** SQLITE_OPEN_READWRITE, and SQLITE_OPEN_CREATE.  Silently mask
       
 96073   ** off all other flags.
       
 96074   */
       
 96075   flags &=  ~( SQLITE_OPEN_DELETEONCLOSE |
       
 96076                SQLITE_OPEN_EXCLUSIVE |
       
 96077                SQLITE_OPEN_MAIN_DB |
       
 96078                SQLITE_OPEN_TEMP_DB | 
       
 96079                SQLITE_OPEN_TRANSIENT_DB | 
       
 96080                SQLITE_OPEN_MAIN_JOURNAL | 
       
 96081                SQLITE_OPEN_TEMP_JOURNAL | 
       
 96082                SQLITE_OPEN_SUBJOURNAL | 
       
 96083                SQLITE_OPEN_MASTER_JOURNAL |
       
 96084                SQLITE_OPEN_NOMUTEX |
       
 96085                SQLITE_OPEN_FULLMUTEX
       
 96086              );
       
 96087 
       
 96088   /* Allocate the sqlite data structure */
       
 96089   db = sqlite3MallocZero( sizeof(sqlite3) );
       
 96090   if( db==0 ) goto opendb_out;
       
 96091   if( isThreadsafe ){
       
 96092     db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
       
 96093     if( db->mutex==0 ){
       
 96094       sqlite3_free(db);
       
 96095       db = 0;
       
 96096       goto opendb_out;
       
 96097     }
       
 96098   }
       
 96099   sqlite3_mutex_enter(db->mutex);
       
 96100   db->errMask = 0xff;
       
 96101   db->nDb = 2;
       
 96102   db->magic = SQLITE_MAGIC_BUSY;
       
 96103   db->aDb = db->aDbStatic;
       
 96104 
       
 96105   assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
       
 96106   memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
       
 96107   db->autoCommit = 1;
       
 96108   db->nextAutovac = -1;
       
 96109   db->nextPagesize = 0;
       
 96110   db->flags |= SQLITE_ShortColNames
       
 96111 #if SQLITE_DEFAULT_FILE_FORMAT<4
       
 96112                  | SQLITE_LegacyFileFmt
       
 96113 #endif
       
 96114 #ifdef SQLITE_ENABLE_LOAD_EXTENSION
       
 96115                  | SQLITE_LoadExtension
       
 96116 #endif
       
 96117 #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
       
 96118                  | SQLITE_RecTriggers
       
 96119 #endif
       
 96120       ;
       
 96121   sqlite3HashInit(&db->aCollSeq);
       
 96122 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
 96123   sqlite3HashInit(&db->aModule);
       
 96124 #endif
       
 96125 
       
 96126   db->pVfs = sqlite3_vfs_find(zVfs);
       
 96127   if( !db->pVfs ){
       
 96128     rc = SQLITE_ERROR;
       
 96129     sqlite3Error(db, rc, "no such vfs: %s", zVfs);
       
 96130     goto opendb_out;
       
 96131   }
       
 96132 
       
 96133   /* Add the default collation sequence BINARY. BINARY works for both UTF-8
       
 96134   ** and UTF-16, so add a version for each to avoid any unnecessary
       
 96135   ** conversions. The only error that can occur here is a malloc() failure.
       
 96136   */
       
 96137   createCollation(db, "BINARY", SQLITE_UTF8, SQLITE_COLL_BINARY, 0,
       
 96138                   binCollFunc, 0);
       
 96139   createCollation(db, "BINARY", SQLITE_UTF16BE, SQLITE_COLL_BINARY, 0,
       
 96140                   binCollFunc, 0);
       
 96141   createCollation(db, "BINARY", SQLITE_UTF16LE, SQLITE_COLL_BINARY, 0,
       
 96142                   binCollFunc, 0);
       
 96143   createCollation(db, "RTRIM", SQLITE_UTF8, SQLITE_COLL_USER, (void*)1,
       
 96144                   binCollFunc, 0);
       
 96145   if( db->mallocFailed ){
       
 96146     goto opendb_out;
       
 96147   }
       
 96148   db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
       
 96149   assert( db->pDfltColl!=0 );
       
 96150 
       
 96151   /* Also add a UTF-8 case-insensitive collation sequence. */
       
 96152   createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0,
       
 96153                   nocaseCollatingFunc, 0);
       
 96154 
       
 96155   /* Open the backend database driver */
       
 96156   db->openFlags = flags;
       
 96157   rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE, 
       
 96158                            flags | SQLITE_OPEN_MAIN_DB,
       
 96159                            &db->aDb[0].pBt);
       
 96160   if( rc!=SQLITE_OK ){
       
 96161     if( rc==SQLITE_IOERR_NOMEM ){
       
 96162       rc = SQLITE_NOMEM;
       
 96163     }
       
 96164     sqlite3Error(db, rc, 0);
       
 96165     goto opendb_out;
       
 96166   }
       
 96167   db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
       
 96168   db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
       
 96169 
       
 96170 
       
 96171   /* The default safety_level for the main database is 'full'; for the temp
       
 96172   ** database it is 'NONE'. This matches the pager layer defaults.  
       
 96173   */
       
 96174   db->aDb[0].zName = "main";
       
 96175   db->aDb[0].safety_level = 3;
       
 96176   db->aDb[1].zName = "temp";
       
 96177   db->aDb[1].safety_level = 1;
       
 96178 
       
 96179   db->magic = SQLITE_MAGIC_OPEN;
       
 96180   if( db->mallocFailed ){
       
 96181     goto opendb_out;
       
 96182   }
       
 96183 
       
 96184   /* Register all built-in functions, but do not attempt to read the
       
 96185   ** database schema yet. This is delayed until the first time the database
       
 96186   ** is accessed.
       
 96187   */
       
 96188   sqlite3Error(db, SQLITE_OK, 0);
       
 96189   sqlite3RegisterBuiltinFunctions(db);
       
 96190 
       
 96191   /* Load automatic extensions - extensions that have been registered
       
 96192   ** using the sqlite3_automatic_extension() API.
       
 96193   */
       
 96194   sqlite3AutoLoadExtensions(db);
       
 96195   rc = sqlite3_errcode(db);
       
 96196   if( rc!=SQLITE_OK ){
       
 96197     goto opendb_out;
       
 96198   }
       
 96199 
       
 96200 #ifdef SQLITE_ENABLE_FTS1
       
 96201   if( !db->mallocFailed ){
       
 96202     extern int sqlite3Fts1Init(sqlite3*);
       
 96203     rc = sqlite3Fts1Init(db);
       
 96204   }
       
 96205 #endif
       
 96206 
       
 96207 #ifdef SQLITE_ENABLE_FTS2
       
 96208   if( !db->mallocFailed && rc==SQLITE_OK ){
       
 96209     extern int sqlite3Fts2Init(sqlite3*);
       
 96210     rc = sqlite3Fts2Init(db);
       
 96211   }
       
 96212 #endif
       
 96213 
       
 96214 #ifdef SQLITE_ENABLE_FTS3
       
 96215   if( !db->mallocFailed && rc==SQLITE_OK ){
       
 96216     rc = sqlite3Fts3Init(db);
       
 96217   }
       
 96218 #endif
       
 96219 
       
 96220 #ifdef SQLITE_ENABLE_ICU
       
 96221   if( !db->mallocFailed && rc==SQLITE_OK ){
       
 96222     rc = sqlite3IcuInit(db);
       
 96223   }
       
 96224 #endif
       
 96225 
       
 96226 #ifdef SQLITE_ENABLE_RTREE
       
 96227   if( !db->mallocFailed && rc==SQLITE_OK){
       
 96228     rc = sqlite3RtreeInit(db);
       
 96229   }
       
 96230 #endif
       
 96231 
       
 96232   sqlite3Error(db, rc, 0);
       
 96233 
       
 96234   /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
       
 96235   ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
       
 96236   ** mode.  Doing nothing at all also makes NORMAL the default.
       
 96237   */
       
 96238 #ifdef SQLITE_DEFAULT_LOCKING_MODE
       
 96239   db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
       
 96240   sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
       
 96241                           SQLITE_DEFAULT_LOCKING_MODE);
       
 96242 #endif
       
 96243 
       
 96244   /* Enable the lookaside-malloc subsystem */
       
 96245   setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
       
 96246                         sqlite3GlobalConfig.nLookaside);
       
 96247 
       
 96248 opendb_out:
       
 96249   if( db ){
       
 96250     assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
       
 96251     sqlite3_mutex_leave(db->mutex);
       
 96252   }
       
 96253   rc = sqlite3_errcode(db);
       
 96254   if( rc==SQLITE_NOMEM ){
       
 96255     sqlite3_close(db);
       
 96256     db = 0;
       
 96257   }else if( rc!=SQLITE_OK ){
       
 96258     db->magic = SQLITE_MAGIC_SICK;
       
 96259   }
       
 96260   *ppDb = db;
       
 96261   return sqlite3ApiExit(0, rc);
       
 96262 }
       
 96263 
       
 96264 /*
       
 96265 ** Open a new database handle.
       
 96266 */
       
 96267 SQLITE_API int sqlite3_open(
       
 96268   const char *zFilename, 
       
 96269   sqlite3 **ppDb 
       
 96270 ){
       
 96271   return openDatabase(zFilename, ppDb,
       
 96272                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
       
 96273 }
       
 96274 SQLITE_API int sqlite3_open_v2(
       
 96275   const char *filename,   /* Database filename (UTF-8) */
       
 96276   sqlite3 **ppDb,         /* OUT: SQLite db handle */
       
 96277   int flags,              /* Flags */
       
 96278   const char *zVfs        /* Name of VFS module to use */
       
 96279 ){
       
 96280   return openDatabase(filename, ppDb, flags, zVfs);
       
 96281 }
       
 96282 
       
 96283 #ifndef SQLITE_OMIT_UTF16
       
 96284 /*
       
 96285 ** Open a new database handle.
       
 96286 */
       
 96287 SQLITE_API int sqlite3_open16(
       
 96288   const void *zFilename, 
       
 96289   sqlite3 **ppDb
       
 96290 ){
       
 96291   char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
       
 96292   sqlite3_value *pVal;
       
 96293   int rc;
       
 96294 
       
 96295   assert( zFilename );
       
 96296   assert( ppDb );
       
 96297   *ppDb = 0;
       
 96298 #ifndef SQLITE_OMIT_AUTOINIT
       
 96299   rc = sqlite3_initialize();
       
 96300   if( rc ) return rc;
       
 96301 #endif
       
 96302   pVal = sqlite3ValueNew(0);
       
 96303   sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
       
 96304   zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
       
 96305   if( zFilename8 ){
       
 96306     rc = openDatabase(zFilename8, ppDb,
       
 96307                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
       
 96308     assert( *ppDb || rc==SQLITE_NOMEM );
       
 96309     if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
       
 96310       ENC(*ppDb) = SQLITE_UTF16NATIVE;
       
 96311     }
       
 96312   }else{
       
 96313     rc = SQLITE_NOMEM;
       
 96314   }
       
 96315   sqlite3ValueFree(pVal);
       
 96316 
       
 96317   return sqlite3ApiExit(0, rc);
       
 96318 }
       
 96319 #endif /* SQLITE_OMIT_UTF16 */
       
 96320 
       
 96321 /*
       
 96322 ** Register a new collation sequence with the database handle db.
       
 96323 */
       
 96324 SQLITE_API int sqlite3_create_collation(
       
 96325   sqlite3* db, 
       
 96326   const char *zName, 
       
 96327   int enc, 
       
 96328   void* pCtx,
       
 96329   int(*xCompare)(void*,int,const void*,int,const void*)
       
 96330 ){
       
 96331   int rc;
       
 96332   sqlite3_mutex_enter(db->mutex);
       
 96333   assert( !db->mallocFailed );
       
 96334   rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
       
 96335   rc = sqlite3ApiExit(db, rc);
       
 96336   sqlite3_mutex_leave(db->mutex);
       
 96337   return rc;
       
 96338 }
       
 96339 
       
 96340 /*
       
 96341 ** Register a new collation sequence with the database handle db.
       
 96342 */
       
 96343 SQLITE_API int sqlite3_create_collation_v2(
       
 96344   sqlite3* db, 
       
 96345   const char *zName, 
       
 96346   int enc, 
       
 96347   void* pCtx,
       
 96348   int(*xCompare)(void*,int,const void*,int,const void*),
       
 96349   void(*xDel)(void*)
       
 96350 ){
       
 96351   int rc;
       
 96352   sqlite3_mutex_enter(db->mutex);
       
 96353   assert( !db->mallocFailed );
       
 96354   rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, xDel);
       
 96355   rc = sqlite3ApiExit(db, rc);
       
 96356   sqlite3_mutex_leave(db->mutex);
       
 96357   return rc;
       
 96358 }
       
 96359 
       
 96360 #ifndef SQLITE_OMIT_UTF16
       
 96361 /*
       
 96362 ** Register a new collation sequence with the database handle db.
       
 96363 */
       
 96364 SQLITE_API int sqlite3_create_collation16(
       
 96365   sqlite3* db, 
       
 96366   const void *zName,
       
 96367   int enc, 
       
 96368   void* pCtx,
       
 96369   int(*xCompare)(void*,int,const void*,int,const void*)
       
 96370 ){
       
 96371   int rc = SQLITE_OK;
       
 96372   char *zName8;
       
 96373   sqlite3_mutex_enter(db->mutex);
       
 96374   assert( !db->mallocFailed );
       
 96375   zName8 = sqlite3Utf16to8(db, zName, -1);
       
 96376   if( zName8 ){
       
 96377     rc = createCollation(db, zName8, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
       
 96378     sqlite3DbFree(db, zName8);
       
 96379   }
       
 96380   rc = sqlite3ApiExit(db, rc);
       
 96381   sqlite3_mutex_leave(db->mutex);
       
 96382   return rc;
       
 96383 }
       
 96384 #endif /* SQLITE_OMIT_UTF16 */
       
 96385 
       
 96386 /*
       
 96387 ** Register a collation sequence factory callback with the database handle
       
 96388 ** db. Replace any previously installed collation sequence factory.
       
 96389 */
       
 96390 SQLITE_API int sqlite3_collation_needed(
       
 96391   sqlite3 *db, 
       
 96392   void *pCollNeededArg, 
       
 96393   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
       
 96394 ){
       
 96395   sqlite3_mutex_enter(db->mutex);
       
 96396   db->xCollNeeded = xCollNeeded;
       
 96397   db->xCollNeeded16 = 0;
       
 96398   db->pCollNeededArg = pCollNeededArg;
       
 96399   sqlite3_mutex_leave(db->mutex);
       
 96400   return SQLITE_OK;
       
 96401 }
       
 96402 
       
 96403 #ifndef SQLITE_OMIT_UTF16
       
 96404 /*
       
 96405 ** Register a collation sequence factory callback with the database handle
       
 96406 ** db. Replace any previously installed collation sequence factory.
       
 96407 */
       
 96408 SQLITE_API int sqlite3_collation_needed16(
       
 96409   sqlite3 *db, 
       
 96410   void *pCollNeededArg, 
       
 96411   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
       
 96412 ){
       
 96413   sqlite3_mutex_enter(db->mutex);
       
 96414   db->xCollNeeded = 0;
       
 96415   db->xCollNeeded16 = xCollNeeded16;
       
 96416   db->pCollNeededArg = pCollNeededArg;
       
 96417   sqlite3_mutex_leave(db->mutex);
       
 96418   return SQLITE_OK;
       
 96419 }
       
 96420 #endif /* SQLITE_OMIT_UTF16 */
       
 96421 
       
 96422 #ifndef SQLITE_OMIT_GLOBALRECOVER
       
 96423 #ifndef SQLITE_OMIT_DEPRECATED
       
 96424 /*
       
 96425 ** This function is now an anachronism. It used to be used to recover from a
       
 96426 ** malloc() failure, but SQLite now does this automatically.
       
 96427 */
       
 96428 SQLITE_API int sqlite3_global_recover(void){
       
 96429   return SQLITE_OK;
       
 96430 }
       
 96431 #endif
       
 96432 #endif
       
 96433 
       
 96434 /*
       
 96435 ** Test to see whether or not the database connection is in autocommit
       
 96436 ** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
       
 96437 ** by default.  Autocommit is disabled by a BEGIN statement and reenabled
       
 96438 ** by the next COMMIT or ROLLBACK.
       
 96439 **
       
 96440 ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
       
 96441 */
       
 96442 SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
       
 96443   return db->autoCommit;
       
 96444 }
       
 96445 
       
 96446 #ifdef SQLITE_DEBUG
       
 96447 /*
       
 96448 ** The following routine is subtituted for constant SQLITE_CORRUPT in
       
 96449 ** debugging builds.  This provides a way to set a breakpoint for when
       
 96450 ** corruption is first detected.
       
 96451 */
       
 96452 SQLITE_PRIVATE int sqlite3Corrupt(void){
       
 96453   return SQLITE_CORRUPT;
       
 96454 }
       
 96455 #endif
       
 96456 
       
 96457 #ifndef SQLITE_OMIT_DEPRECATED
       
 96458 /*
       
 96459 ** This is a convenience routine that makes sure that all thread-specific
       
 96460 ** data for this thread has been deallocated.
       
 96461 **
       
 96462 ** SQLite no longer uses thread-specific data so this routine is now a
       
 96463 ** no-op.  It is retained for historical compatibility.
       
 96464 */
       
 96465 SQLITE_API void sqlite3_thread_cleanup(void){
       
 96466 }
       
 96467 #endif
       
 96468 
       
 96469 /*
       
 96470 ** Return meta information about a specific column of a database table.
       
 96471 ** See comment in sqlite3.h (sqlite.h.in) for details.
       
 96472 */
       
 96473 #ifdef SQLITE_ENABLE_COLUMN_METADATA
       
 96474 SQLITE_API int sqlite3_table_column_metadata(
       
 96475   sqlite3 *db,                /* Connection handle */
       
 96476   const char *zDbName,        /* Database name or NULL */
       
 96477   const char *zTableName,     /* Table name */
       
 96478   const char *zColumnName,    /* Column name */
       
 96479   char const **pzDataType,    /* OUTPUT: Declared data type */
       
 96480   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
       
 96481   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
       
 96482   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
       
 96483   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
       
 96484 ){
       
 96485   int rc;
       
 96486   char *zErrMsg = 0;
       
 96487   Table *pTab = 0;
       
 96488   Column *pCol = 0;
       
 96489   int iCol;
       
 96490 
       
 96491   char const *zDataType = 0;
       
 96492   char const *zCollSeq = 0;
       
 96493   int notnull = 0;
       
 96494   int primarykey = 0;
       
 96495   int autoinc = 0;
       
 96496 
       
 96497   /* Ensure the database schema has been loaded */
       
 96498   sqlite3_mutex_enter(db->mutex);
       
 96499   (void)sqlite3SafetyOn(db);
       
 96500   sqlite3BtreeEnterAll(db);
       
 96501   rc = sqlite3Init(db, &zErrMsg);
       
 96502   if( SQLITE_OK!=rc ){
       
 96503     goto error_out;
       
 96504   }
       
 96505 
       
 96506   /* Locate the table in question */
       
 96507   pTab = sqlite3FindTable(db, zTableName, zDbName);
       
 96508   if( !pTab || pTab->pSelect ){
       
 96509     pTab = 0;
       
 96510     goto error_out;
       
 96511   }
       
 96512 
       
 96513   /* Find the column for which info is requested */
       
 96514   if( sqlite3IsRowid(zColumnName) ){
       
 96515     iCol = pTab->iPKey;
       
 96516     if( iCol>=0 ){
       
 96517       pCol = &pTab->aCol[iCol];
       
 96518     }
       
 96519   }else{
       
 96520     for(iCol=0; iCol<pTab->nCol; iCol++){
       
 96521       pCol = &pTab->aCol[iCol];
       
 96522       if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
       
 96523         break;
       
 96524       }
       
 96525     }
       
 96526     if( iCol==pTab->nCol ){
       
 96527       pTab = 0;
       
 96528       goto error_out;
       
 96529     }
       
 96530   }
       
 96531 
       
 96532   /* The following block stores the meta information that will be returned
       
 96533   ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
       
 96534   ** and autoinc. At this point there are two possibilities:
       
 96535   ** 
       
 96536   **     1. The specified column name was rowid", "oid" or "_rowid_" 
       
 96537   **        and there is no explicitly declared IPK column. 
       
 96538   **
       
 96539   **     2. The table is not a view and the column name identified an 
       
 96540   **        explicitly declared column. Copy meta information from *pCol.
       
 96541   */ 
       
 96542   if( pCol ){
       
 96543     zDataType = pCol->zType;
       
 96544     zCollSeq = pCol->zColl;
       
 96545     notnull = pCol->notNull!=0;
       
 96546     primarykey  = pCol->isPrimKey!=0;
       
 96547     autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
       
 96548   }else{
       
 96549     zDataType = "INTEGER";
       
 96550     primarykey = 1;
       
 96551   }
       
 96552   if( !zCollSeq ){
       
 96553     zCollSeq = "BINARY";
       
 96554   }
       
 96555 
       
 96556 error_out:
       
 96557   sqlite3BtreeLeaveAll(db);
       
 96558   (void)sqlite3SafetyOff(db);
       
 96559 
       
 96560   /* Whether the function call succeeded or failed, set the output parameters
       
 96561   ** to whatever their local counterparts contain. If an error did occur,
       
 96562   ** this has the effect of zeroing all output parameters.
       
 96563   */
       
 96564   if( pzDataType ) *pzDataType = zDataType;
       
 96565   if( pzCollSeq ) *pzCollSeq = zCollSeq;
       
 96566   if( pNotNull ) *pNotNull = notnull;
       
 96567   if( pPrimaryKey ) *pPrimaryKey = primarykey;
       
 96568   if( pAutoinc ) *pAutoinc = autoinc;
       
 96569 
       
 96570   if( SQLITE_OK==rc && !pTab ){
       
 96571     sqlite3DbFree(db, zErrMsg);
       
 96572     zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
       
 96573         zColumnName);
       
 96574     rc = SQLITE_ERROR;
       
 96575   }
       
 96576   sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
       
 96577   sqlite3DbFree(db, zErrMsg);
       
 96578   rc = sqlite3ApiExit(db, rc);
       
 96579   sqlite3_mutex_leave(db->mutex);
       
 96580   return rc;
       
 96581 }
       
 96582 #endif
       
 96583 
       
 96584 /*
       
 96585 ** Sleep for a little while.  Return the amount of time slept.
       
 96586 */
       
 96587 SQLITE_API int sqlite3_sleep(int ms){
       
 96588   sqlite3_vfs *pVfs;
       
 96589   int rc;
       
 96590   pVfs = sqlite3_vfs_find(0);
       
 96591   if( pVfs==0 ) return 0;
       
 96592 
       
 96593   /* This function works in milliseconds, but the underlying OsSleep() 
       
 96594   ** API uses microseconds. Hence the 1000's.
       
 96595   */
       
 96596   rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
       
 96597   return rc;
       
 96598 }
       
 96599 
       
 96600 /*
       
 96601 ** Enable or disable the extended result codes.
       
 96602 */
       
 96603 SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
       
 96604   sqlite3_mutex_enter(db->mutex);
       
 96605   db->errMask = onoff ? 0xffffffff : 0xff;
       
 96606   sqlite3_mutex_leave(db->mutex);
       
 96607   return SQLITE_OK;
       
 96608 }
       
 96609 
       
 96610 /*
       
 96611 ** Invoke the xFileControl method on a particular database.
       
 96612 */
       
 96613 SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
       
 96614   int rc = SQLITE_ERROR;
       
 96615   int iDb;
       
 96616   sqlite3_mutex_enter(db->mutex);
       
 96617   if( zDbName==0 ){
       
 96618     iDb = 0;
       
 96619   }else{
       
 96620     for(iDb=0; iDb<db->nDb; iDb++){
       
 96621       if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break;
       
 96622     }
       
 96623   }
       
 96624   if( iDb<db->nDb ){
       
 96625     Btree *pBtree = db->aDb[iDb].pBt;
       
 96626     if( pBtree ){
       
 96627       Pager *pPager;
       
 96628       sqlite3_file *fd;
       
 96629       sqlite3BtreeEnter(pBtree);
       
 96630       pPager = sqlite3BtreePager(pBtree);
       
 96631       assert( pPager!=0 );
       
 96632       fd = sqlite3PagerFile(pPager);
       
 96633       assert( fd!=0 );
       
 96634       if( fd->pMethods ){
       
 96635         rc = sqlite3OsFileControl(fd, op, pArg);
       
 96636       }
       
 96637       sqlite3BtreeLeave(pBtree);
       
 96638     }
       
 96639   }
       
 96640   sqlite3_mutex_leave(db->mutex);
       
 96641   return rc;   
       
 96642 }
       
 96643 
       
 96644 /*
       
 96645 ** Interface to the testing logic.
       
 96646 */
       
 96647 SQLITE_API int sqlite3_test_control(int op, ...){
       
 96648   int rc = 0;
       
 96649 #ifndef SQLITE_OMIT_BUILTIN_TEST
       
 96650   va_list ap;
       
 96651   va_start(ap, op);
       
 96652   switch( op ){
       
 96653 
       
 96654     /*
       
 96655     ** Save the current state of the PRNG.
       
 96656     */
       
 96657     case SQLITE_TESTCTRL_PRNG_SAVE: {
       
 96658       sqlite3PrngSaveState();
       
 96659       break;
       
 96660     }
       
 96661 
       
 96662     /*
       
 96663     ** Restore the state of the PRNG to the last state saved using
       
 96664     ** PRNG_SAVE.  If PRNG_SAVE has never before been called, then
       
 96665     ** this verb acts like PRNG_RESET.
       
 96666     */
       
 96667     case SQLITE_TESTCTRL_PRNG_RESTORE: {
       
 96668       sqlite3PrngRestoreState();
       
 96669       break;
       
 96670     }
       
 96671 
       
 96672     /*
       
 96673     ** Reset the PRNG back to its uninitialized state.  The next call
       
 96674     ** to sqlite3_randomness() will reseed the PRNG using a single call
       
 96675     ** to the xRandomness method of the default VFS.
       
 96676     */
       
 96677     case SQLITE_TESTCTRL_PRNG_RESET: {
       
 96678       sqlite3PrngResetState();
       
 96679       break;
       
 96680     }
       
 96681 
       
 96682     /*
       
 96683     **  sqlite3_test_control(BITVEC_TEST, size, program)
       
 96684     **
       
 96685     ** Run a test against a Bitvec object of size.  The program argument
       
 96686     ** is an array of integers that defines the test.  Return -1 on a
       
 96687     ** memory allocation error, 0 on success, or non-zero for an error.
       
 96688     ** See the sqlite3BitvecBuiltinTest() for additional information.
       
 96689     */
       
 96690     case SQLITE_TESTCTRL_BITVEC_TEST: {
       
 96691       int sz = va_arg(ap, int);
       
 96692       int *aProg = va_arg(ap, int*);
       
 96693       rc = sqlite3BitvecBuiltinTest(sz, aProg);
       
 96694       break;
       
 96695     }
       
 96696 
       
 96697     /*
       
 96698     **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
       
 96699     **
       
 96700     ** Register hooks to call to indicate which malloc() failures 
       
 96701     ** are benign.
       
 96702     */
       
 96703     case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
       
 96704       typedef void (*void_function)(void);
       
 96705       void_function xBenignBegin;
       
 96706       void_function xBenignEnd;
       
 96707       xBenignBegin = va_arg(ap, void_function);
       
 96708       xBenignEnd = va_arg(ap, void_function);
       
 96709       sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
       
 96710       break;
       
 96711     }
       
 96712 
       
 96713     /*
       
 96714     **  sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
       
 96715     **
       
 96716     ** Set the PENDING byte to the value in the argument, if X>0.
       
 96717     ** Make no changes if X==0.  Return the value of the pending byte
       
 96718     ** as it existing before this routine was called.
       
 96719     **
       
 96720     ** IMPORTANT:  Changing the PENDING byte from 0x40000000 results in
       
 96721     ** an incompatible database file format.  Changing the PENDING byte
       
 96722     ** while any database connection is open results in undefined and
       
 96723     ** dileterious behavior.
       
 96724     */
       
 96725     case SQLITE_TESTCTRL_PENDING_BYTE: {
       
 96726       unsigned int newVal = va_arg(ap, unsigned int);
       
 96727       rc = sqlite3PendingByte;
       
 96728       if( newVal ) sqlite3PendingByte = newVal;
       
 96729       break;
       
 96730     }
       
 96731 
       
 96732     /*
       
 96733     **  sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
       
 96734     **
       
 96735     ** This action provides a run-time test to see whether or not
       
 96736     ** assert() was enabled at compile-time.  If X is true and assert()
       
 96737     ** is enabled, then the return value is true.  If X is true and
       
 96738     ** assert() is disabled, then the return value is zero.  If X is
       
 96739     ** false and assert() is enabled, then the assertion fires and the
       
 96740     ** process aborts.  If X is false and assert() is disabled, then the
       
 96741     ** return value is zero.
       
 96742     */
       
 96743     case SQLITE_TESTCTRL_ASSERT: {
       
 96744       volatile int x = 0;
       
 96745       assert( (x = va_arg(ap,int))!=0 );
       
 96746       rc = x;
       
 96747       break;
       
 96748     }
       
 96749 
       
 96750 
       
 96751     /*
       
 96752     **  sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
       
 96753     **
       
 96754     ** This action provides a run-time test to see how the ALWAYS and
       
 96755     ** NEVER macros were defined at compile-time.
       
 96756     **
       
 96757     ** The return value is ALWAYS(X).  
       
 96758     **
       
 96759     ** The recommended test is X==2.  If the return value is 2, that means
       
 96760     ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
       
 96761     ** default setting.  If the return value is 1, then ALWAYS() is either
       
 96762     ** hard-coded to true or else it asserts if its argument is false.
       
 96763     ** The first behavior (hard-coded to true) is the case if
       
 96764     ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
       
 96765     ** behavior (assert if the argument to ALWAYS() is false) is the case if
       
 96766     ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
       
 96767     **
       
 96768     ** The run-time test procedure might look something like this:
       
 96769     **
       
 96770     **    if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
       
 96771     **      // ALWAYS() and NEVER() are no-op pass-through macros
       
 96772     **    }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
       
 96773     **      // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
       
 96774     **    }else{
       
 96775     **      // ALWAYS(x) is a constant 1.  NEVER(x) is a constant 0.
       
 96776     **    }
       
 96777     */
       
 96778     case SQLITE_TESTCTRL_ALWAYS: {
       
 96779       int x = va_arg(ap,int);
       
 96780       rc = ALWAYS(x);
       
 96781       break;
       
 96782     }
       
 96783 
       
 96784     /*   sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
       
 96785     **
       
 96786     ** Set the nReserve size to N for the main database on the database
       
 96787     ** connection db.
       
 96788     */
       
 96789     case SQLITE_TESTCTRL_RESERVE: {
       
 96790       sqlite3 *db = va_arg(ap, sqlite3*);
       
 96791       int x = va_arg(ap,int);
       
 96792       sqlite3_mutex_enter(db->mutex);
       
 96793       sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
       
 96794       sqlite3_mutex_leave(db->mutex);
       
 96795       break;
       
 96796     }
       
 96797 
       
 96798   }
       
 96799   va_end(ap);
       
 96800 #endif /* SQLITE_OMIT_BUILTIN_TEST */
       
 96801   return rc;
       
 96802 }
       
 96803 
       
 96804 /************** End of main.c ************************************************/
       
 96805 /************** Begin file notify.c ******************************************/
       
 96806 /*
       
 96807 ** 2009 March 3
       
 96808 **
       
 96809 ** The author disclaims copyright to this source code.  In place of
       
 96810 ** a legal notice, here is a blessing:
       
 96811 **
       
 96812 **    May you do good and not evil.
       
 96813 **    May you find forgiveness for yourself and forgive others.
       
 96814 **    May you share freely, never taking more than you give.
       
 96815 **
       
 96816 *************************************************************************
       
 96817 **
       
 96818 ** This file contains the implementation of the sqlite3_unlock_notify()
       
 96819 ** API method and its associated functionality.
       
 96820 **
       
 96821 ** $Id: notify.c,v 1.4 2009/04/07 22:06:57 drh Exp $
       
 96822 */
       
 96823 
       
 96824 /* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
       
 96825 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
       
 96826 
       
 96827 /*
       
 96828 ** Public interfaces:
       
 96829 **
       
 96830 **   sqlite3ConnectionBlocked()
       
 96831 **   sqlite3ConnectionUnlocked()
       
 96832 **   sqlite3ConnectionClosed()
       
 96833 **   sqlite3_unlock_notify()
       
 96834 */
       
 96835 
       
 96836 #define assertMutexHeld() \
       
 96837   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
       
 96838 
       
 96839 /*
       
 96840 ** Head of a linked list of all sqlite3 objects created by this process
       
 96841 ** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
       
 96842 ** is not NULL. This variable may only accessed while the STATIC_MASTER
       
 96843 ** mutex is held.
       
 96844 */
       
 96845 static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
       
 96846 
       
 96847 #ifndef NDEBUG
       
 96848 /*
       
 96849 ** This function is a complex assert() that verifies the following 
       
 96850 ** properties of the blocked connections list:
       
 96851 **
       
 96852 **   1) Each entry in the list has a non-NULL value for either 
       
 96853 **      pUnlockConnection or pBlockingConnection, or both.
       
 96854 **
       
 96855 **   2) All entries in the list that share a common value for 
       
 96856 **      xUnlockNotify are grouped together.
       
 96857 **
       
 96858 **   3) If the argument db is not NULL, then none of the entries in the
       
 96859 **      blocked connections list have pUnlockConnection or pBlockingConnection
       
 96860 **      set to db. This is used when closing connection db.
       
 96861 */
       
 96862 static void checkListProperties(sqlite3 *db){
       
 96863   sqlite3 *p;
       
 96864   for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
       
 96865     int seen = 0;
       
 96866     sqlite3 *p2;
       
 96867 
       
 96868     /* Verify property (1) */
       
 96869     assert( p->pUnlockConnection || p->pBlockingConnection );
       
 96870 
       
 96871     /* Verify property (2) */
       
 96872     for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
       
 96873       if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
       
 96874       assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
       
 96875       assert( db==0 || p->pUnlockConnection!=db );
       
 96876       assert( db==0 || p->pBlockingConnection!=db );
       
 96877     }
       
 96878   }
       
 96879 }
       
 96880 #else
       
 96881 # define checkListProperties(x)
       
 96882 #endif
       
 96883 
       
 96884 /*
       
 96885 ** Remove connection db from the blocked connections list. If connection
       
 96886 ** db is not currently a part of the list, this function is a no-op.
       
 96887 */
       
 96888 static void removeFromBlockedList(sqlite3 *db){
       
 96889   sqlite3 **pp;
       
 96890   assertMutexHeld();
       
 96891   for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
       
 96892     if( *pp==db ){
       
 96893       *pp = (*pp)->pNextBlocked;
       
 96894       break;
       
 96895     }
       
 96896   }
       
 96897 }
       
 96898 
       
 96899 /*
       
 96900 ** Add connection db to the blocked connections list. It is assumed
       
 96901 ** that it is not already a part of the list.
       
 96902 */
       
 96903 static void addToBlockedList(sqlite3 *db){
       
 96904   sqlite3 **pp;
       
 96905   assertMutexHeld();
       
 96906   for(
       
 96907     pp=&sqlite3BlockedList; 
       
 96908     *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify; 
       
 96909     pp=&(*pp)->pNextBlocked
       
 96910   );
       
 96911   db->pNextBlocked = *pp;
       
 96912   *pp = db;
       
 96913 }
       
 96914 
       
 96915 /*
       
 96916 ** Obtain the STATIC_MASTER mutex.
       
 96917 */
       
 96918 static void enterMutex(void){
       
 96919   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
       
 96920   checkListProperties(0);
       
 96921 }
       
 96922 
       
 96923 /*
       
 96924 ** Release the STATIC_MASTER mutex.
       
 96925 */
       
 96926 static void leaveMutex(void){
       
 96927   assertMutexHeld();
       
 96928   checkListProperties(0);
       
 96929   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
       
 96930 }
       
 96931 
       
 96932 /*
       
 96933 ** Register an unlock-notify callback.
       
 96934 **
       
 96935 ** This is called after connection "db" has attempted some operation
       
 96936 ** but has received an SQLITE_LOCKED error because another connection
       
 96937 ** (call it pOther) in the same process was busy using the same shared
       
 96938 ** cache.  pOther is found by looking at db->pBlockingConnection.
       
 96939 **
       
 96940 ** If there is no blocking connection, the callback is invoked immediately,
       
 96941 ** before this routine returns.
       
 96942 **
       
 96943 ** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
       
 96944 ** a deadlock.
       
 96945 **
       
 96946 ** Otherwise, make arrangements to invoke xNotify when pOther drops
       
 96947 ** its locks.
       
 96948 **
       
 96949 ** Each call to this routine overrides any prior callbacks registered
       
 96950 ** on the same "db".  If xNotify==0 then any prior callbacks are immediately
       
 96951 ** cancelled.
       
 96952 */
       
 96953 SQLITE_API int sqlite3_unlock_notify(
       
 96954   sqlite3 *db,
       
 96955   void (*xNotify)(void **, int),
       
 96956   void *pArg
       
 96957 ){
       
 96958   int rc = SQLITE_OK;
       
 96959 
       
 96960   sqlite3_mutex_enter(db->mutex);
       
 96961   enterMutex();
       
 96962 
       
 96963   if( xNotify==0 ){
       
 96964     removeFromBlockedList(db);
       
 96965     db->pUnlockConnection = 0;
       
 96966     db->xUnlockNotify = 0;
       
 96967     db->pUnlockArg = 0;
       
 96968   }else if( 0==db->pBlockingConnection ){
       
 96969     /* The blocking transaction has been concluded. Or there never was a 
       
 96970     ** blocking transaction. In either case, invoke the notify callback
       
 96971     ** immediately. 
       
 96972     */
       
 96973     xNotify(&pArg, 1);
       
 96974   }else{
       
 96975     sqlite3 *p;
       
 96976 
       
 96977     for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
       
 96978     if( p ){
       
 96979       rc = SQLITE_LOCKED;              /* Deadlock detected. */
       
 96980     }else{
       
 96981       db->pUnlockConnection = db->pBlockingConnection;
       
 96982       db->xUnlockNotify = xNotify;
       
 96983       db->pUnlockArg = pArg;
       
 96984       removeFromBlockedList(db);
       
 96985       addToBlockedList(db);
       
 96986     }
       
 96987   }
       
 96988 
       
 96989   leaveMutex();
       
 96990   assert( !db->mallocFailed );
       
 96991   sqlite3Error(db, rc, (rc?"database is deadlocked":0));
       
 96992   sqlite3_mutex_leave(db->mutex);
       
 96993   return rc;
       
 96994 }
       
 96995 
       
 96996 /*
       
 96997 ** This function is called while stepping or preparing a statement 
       
 96998 ** associated with connection db. The operation will return SQLITE_LOCKED
       
 96999 ** to the user because it requires a lock that will not be available
       
 97000 ** until connection pBlocker concludes its current transaction.
       
 97001 */
       
 97002 SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
       
 97003   enterMutex();
       
 97004   if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
       
 97005     addToBlockedList(db);
       
 97006   }
       
 97007   db->pBlockingConnection = pBlocker;
       
 97008   leaveMutex();
       
 97009 }
       
 97010 
       
 97011 /*
       
 97012 ** This function is called when
       
 97013 ** the transaction opened by database db has just finished. Locks held 
       
 97014 ** by database connection db have been released.
       
 97015 **
       
 97016 ** This function loops through each entry in the blocked connections
       
 97017 ** list and does the following:
       
 97018 **
       
 97019 **   1) If the sqlite3.pBlockingConnection member of a list entry is
       
 97020 **      set to db, then set pBlockingConnection=0.
       
 97021 **
       
 97022 **   2) If the sqlite3.pUnlockConnection member of a list entry is
       
 97023 **      set to db, then invoke the configured unlock-notify callback and
       
 97024 **      set pUnlockConnection=0.
       
 97025 **
       
 97026 **   3) If the two steps above mean that pBlockingConnection==0 and
       
 97027 **      pUnlockConnection==0, remove the entry from the blocked connections
       
 97028 **      list.
       
 97029 */
       
 97030 SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
       
 97031   void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
       
 97032   int nArg = 0;                            /* Number of entries in aArg[] */
       
 97033   sqlite3 **pp;                            /* Iterator variable */
       
 97034   void **aArg;               /* Arguments to the unlock callback */
       
 97035   void **aDyn = 0;           /* Dynamically allocated space for aArg[] */
       
 97036   void *aStatic[16];         /* Starter space for aArg[].  No malloc required */
       
 97037 
       
 97038   aArg = aStatic;
       
 97039   enterMutex();         /* Enter STATIC_MASTER mutex */
       
 97040 
       
 97041   /* This loop runs once for each entry in the blocked-connections list. */
       
 97042   for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
       
 97043     sqlite3 *p = *pp;
       
 97044 
       
 97045     /* Step 1. */
       
 97046     if( p->pBlockingConnection==db ){
       
 97047       p->pBlockingConnection = 0;
       
 97048     }
       
 97049 
       
 97050     /* Step 2. */
       
 97051     if( p->pUnlockConnection==db ){
       
 97052       assert( p->xUnlockNotify );
       
 97053       if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
       
 97054         xUnlockNotify(aArg, nArg);
       
 97055         nArg = 0;
       
 97056       }
       
 97057 
       
 97058       sqlite3BeginBenignMalloc();
       
 97059       assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
       
 97060       assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
       
 97061       if( (!aDyn && nArg==(int)ArraySize(aStatic))
       
 97062        || (aDyn && nArg==(int)(sqlite3DbMallocSize(db, aDyn)/sizeof(void*)))
       
 97063       ){
       
 97064         /* The aArg[] array needs to grow. */
       
 97065         void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
       
 97066         if( pNew ){
       
 97067           memcpy(pNew, aArg, nArg*sizeof(void *));
       
 97068           sqlite3_free(aDyn);
       
 97069           aDyn = aArg = pNew;
       
 97070         }else{
       
 97071           /* This occurs when the array of context pointers that need to
       
 97072           ** be passed to the unlock-notify callback is larger than the
       
 97073           ** aStatic[] array allocated on the stack and the attempt to 
       
 97074           ** allocate a larger array from the heap has failed.
       
 97075           **
       
 97076           ** This is a difficult situation to handle. Returning an error
       
 97077           ** code to the caller is insufficient, as even if an error code
       
 97078           ** is returned the transaction on connection db will still be
       
 97079           ** closed and the unlock-notify callbacks on blocked connections
       
 97080           ** will go unissued. This might cause the application to wait
       
 97081           ** indefinitely for an unlock-notify callback that will never 
       
 97082           ** arrive.
       
 97083           **
       
 97084           ** Instead, invoke the unlock-notify callback with the context
       
 97085           ** array already accumulated. We can then clear the array and
       
 97086           ** begin accumulating any further context pointers without 
       
 97087           ** requiring any dynamic allocation. This is sub-optimal because
       
 97088           ** it means that instead of one callback with a large array of
       
 97089           ** context pointers the application will receive two or more
       
 97090           ** callbacks with smaller arrays of context pointers, which will
       
 97091           ** reduce the applications ability to prioritize multiple 
       
 97092           ** connections. But it is the best that can be done under the
       
 97093           ** circumstances.
       
 97094           */
       
 97095           xUnlockNotify(aArg, nArg);
       
 97096           nArg = 0;
       
 97097         }
       
 97098       }
       
 97099       sqlite3EndBenignMalloc();
       
 97100 
       
 97101       aArg[nArg++] = p->pUnlockArg;
       
 97102       xUnlockNotify = p->xUnlockNotify;
       
 97103       p->pUnlockConnection = 0;
       
 97104       p->xUnlockNotify = 0;
       
 97105       p->pUnlockArg = 0;
       
 97106     }
       
 97107 
       
 97108     /* Step 3. */
       
 97109     if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
       
 97110       /* Remove connection p from the blocked connections list. */
       
 97111       *pp = p->pNextBlocked;
       
 97112       p->pNextBlocked = 0;
       
 97113     }else{
       
 97114       pp = &p->pNextBlocked;
       
 97115     }
       
 97116   }
       
 97117 
       
 97118   if( nArg!=0 ){
       
 97119     xUnlockNotify(aArg, nArg);
       
 97120   }
       
 97121   sqlite3_free(aDyn);
       
 97122   leaveMutex();         /* Leave STATIC_MASTER mutex */
       
 97123 }
       
 97124 
       
 97125 /*
       
 97126 ** This is called when the database connection passed as an argument is 
       
 97127 ** being closed. The connection is removed from the blocked list.
       
 97128 */
       
 97129 SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
       
 97130   sqlite3ConnectionUnlocked(db);
       
 97131   enterMutex();
       
 97132   removeFromBlockedList(db);
       
 97133   checkListProperties(db);
       
 97134   leaveMutex();
       
 97135 }
       
 97136 #endif
       
 97137 
       
 97138 /************** End of notify.c **********************************************/
       
 97139 /************** Begin file fts3.c ********************************************/
       
 97140 /*
       
 97141 ** 2006 Oct 10
       
 97142 **
       
 97143 ** The author disclaims copyright to this source code.  In place of
       
 97144 ** a legal notice, here is a blessing:
       
 97145 **
       
 97146 **    May you do good and not evil.
       
 97147 **    May you find forgiveness for yourself and forgive others.
       
 97148 **    May you share freely, never taking more than you give.
       
 97149 **
       
 97150 ******************************************************************************
       
 97151 **
       
 97152 ** This is an SQLite module implementing full-text search.
       
 97153 */
       
 97154 
       
 97155 /*
       
 97156 ** The code in this file is only compiled if:
       
 97157 **
       
 97158 **     * The FTS3 module is being built as an extension
       
 97159 **       (in which case SQLITE_CORE is not defined), or
       
 97160 **
       
 97161 **     * The FTS3 module is being built into the core of
       
 97162 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
       
 97163 */
       
 97164 
       
 97165 /* TODO(shess) Consider exporting this comment to an HTML file or the
       
 97166 ** wiki.
       
 97167 */
       
 97168 /* The full-text index is stored in a series of b+tree (-like)
       
 97169 ** structures called segments which map terms to doclists.  The
       
 97170 ** structures are like b+trees in layout, but are constructed from the
       
 97171 ** bottom up in optimal fashion and are not updatable.  Since trees
       
 97172 ** are built from the bottom up, things will be described from the
       
 97173 ** bottom up.
       
 97174 **
       
 97175 **
       
 97176 **** Varints ****
       
 97177 ** The basic unit of encoding is a variable-length integer called a
       
 97178 ** varint.  We encode variable-length integers in little-endian order
       
 97179 ** using seven bits * per byte as follows:
       
 97180 **
       
 97181 ** KEY:
       
 97182 **         A = 0xxxxxxx    7 bits of data and one flag bit
       
 97183 **         B = 1xxxxxxx    7 bits of data and one flag bit
       
 97184 **
       
 97185 **  7 bits - A
       
 97186 ** 14 bits - BA
       
 97187 ** 21 bits - BBA
       
 97188 ** and so on.
       
 97189 **
       
 97190 ** This is identical to how sqlite encodes varints (see util.c).
       
 97191 **
       
 97192 **
       
 97193 **** Document lists ****
       
 97194 ** A doclist (document list) holds a docid-sorted list of hits for a
       
 97195 ** given term.  Doclists hold docids, and can optionally associate
       
 97196 ** token positions and offsets with docids.
       
 97197 **
       
 97198 ** A DL_POSITIONS_OFFSETS doclist is stored like this:
       
 97199 **
       
 97200 ** array {
       
 97201 **   varint docid;
       
 97202 **   array {                (position list for column 0)
       
 97203 **     varint position;     (delta from previous position plus POS_BASE)
       
 97204 **     varint startOffset;  (delta from previous startOffset)
       
 97205 **     varint endOffset;    (delta from startOffset)
       
 97206 **   }
       
 97207 **   array {
       
 97208 **     varint POS_COLUMN;   (marks start of position list for new column)
       
 97209 **     varint column;       (index of new column)
       
 97210 **     array {
       
 97211 **       varint position;   (delta from previous position plus POS_BASE)
       
 97212 **       varint startOffset;(delta from previous startOffset)
       
 97213 **       varint endOffset;  (delta from startOffset)
       
 97214 **     }
       
 97215 **   }
       
 97216 **   varint POS_END;        (marks end of positions for this document.
       
 97217 ** }
       
 97218 **
       
 97219 ** Here, array { X } means zero or more occurrences of X, adjacent in
       
 97220 ** memory.  A "position" is an index of a token in the token stream
       
 97221 ** generated by the tokenizer, while an "offset" is a byte offset,
       
 97222 ** both based at 0.  Note that POS_END and POS_COLUMN occur in the
       
 97223 ** same logical place as the position element, and act as sentinals
       
 97224 ** ending a position list array.
       
 97225 **
       
 97226 ** A DL_POSITIONS doclist omits the startOffset and endOffset
       
 97227 ** information.  A DL_DOCIDS doclist omits both the position and
       
 97228 ** offset information, becoming an array of varint-encoded docids.
       
 97229 **
       
 97230 ** On-disk data is stored as type DL_DEFAULT, so we don't serialize
       
 97231 ** the type.  Due to how deletion is implemented in the segmentation
       
 97232 ** system, on-disk doclists MUST store at least positions.
       
 97233 **
       
 97234 **
       
 97235 **** Segment leaf nodes ****
       
 97236 ** Segment leaf nodes store terms and doclists, ordered by term.  Leaf
       
 97237 ** nodes are written using LeafWriter, and read using LeafReader (to
       
 97238 ** iterate through a single leaf node's data) and LeavesReader (to
       
 97239 ** iterate through a segment's entire leaf layer).  Leaf nodes have
       
 97240 ** the format:
       
 97241 **
       
 97242 ** varint iHeight;             (height from leaf level, always 0)
       
 97243 ** varint nTerm;               (length of first term)
       
 97244 ** char pTerm[nTerm];          (content of first term)
       
 97245 ** varint nDoclist;            (length of term's associated doclist)
       
 97246 ** char pDoclist[nDoclist];    (content of doclist)
       
 97247 ** array {
       
 97248 **                             (further terms are delta-encoded)
       
 97249 **   varint nPrefix;           (length of prefix shared with previous term)
       
 97250 **   varint nSuffix;           (length of unshared suffix)
       
 97251 **   char pTermSuffix[nSuffix];(unshared suffix of next term)
       
 97252 **   varint nDoclist;          (length of term's associated doclist)
       
 97253 **   char pDoclist[nDoclist];  (content of doclist)
       
 97254 ** }
       
 97255 **
       
 97256 ** Here, array { X } means zero or more occurrences of X, adjacent in
       
 97257 ** memory.
       
 97258 **
       
 97259 ** Leaf nodes are broken into blocks which are stored contiguously in
       
 97260 ** the %_segments table in sorted order.  This means that when the end
       
 97261 ** of a node is reached, the next term is in the node with the next
       
 97262 ** greater node id.
       
 97263 **
       
 97264 ** New data is spilled to a new leaf node when the current node
       
 97265 ** exceeds LEAF_MAX bytes (default 2048).  New data which itself is
       
 97266 ** larger than STANDALONE_MIN (default 1024) is placed in a standalone
       
 97267 ** node (a leaf node with a single term and doclist).  The goal of
       
 97268 ** these settings is to pack together groups of small doclists while
       
 97269 ** making it efficient to directly access large doclists.  The
       
 97270 ** assumption is that large doclists represent terms which are more
       
 97271 ** likely to be query targets.
       
 97272 **
       
 97273 ** TODO(shess) It may be useful for blocking decisions to be more
       
 97274 ** dynamic.  For instance, it may make more sense to have a 2.5k leaf
       
 97275 ** node rather than splitting into 2k and .5k nodes.  My intuition is
       
 97276 ** that this might extend through 2x or 4x the pagesize.
       
 97277 **
       
 97278 **
       
 97279 **** Segment interior nodes ****
       
 97280 ** Segment interior nodes store blockids for subtree nodes and terms
       
 97281 ** to describe what data is stored by the each subtree.  Interior
       
 97282 ** nodes are written using InteriorWriter, and read using
       
 97283 ** InteriorReader.  InteriorWriters are created as needed when
       
 97284 ** SegmentWriter creates new leaf nodes, or when an interior node
       
 97285 ** itself grows too big and must be split.  The format of interior
       
 97286 ** nodes:
       
 97287 **
       
 97288 ** varint iHeight;           (height from leaf level, always >0)
       
 97289 ** varint iBlockid;          (block id of node's leftmost subtree)
       
 97290 ** optional {
       
 97291 **   varint nTerm;           (length of first term)
       
 97292 **   char pTerm[nTerm];      (content of first term)
       
 97293 **   array {
       
 97294 **                                (further terms are delta-encoded)
       
 97295 **     varint nPrefix;            (length of shared prefix with previous term)
       
 97296 **     varint nSuffix;            (length of unshared suffix)
       
 97297 **     char pTermSuffix[nSuffix]; (unshared suffix of next term)
       
 97298 **   }
       
 97299 ** }
       
 97300 **
       
 97301 ** Here, optional { X } means an optional element, while array { X }
       
 97302 ** means zero or more occurrences of X, adjacent in memory.
       
 97303 **
       
 97304 ** An interior node encodes n terms separating n+1 subtrees.  The
       
 97305 ** subtree blocks are contiguous, so only the first subtree's blockid
       
 97306 ** is encoded.  The subtree at iBlockid will contain all terms less
       
 97307 ** than the first term encoded (or all terms if no term is encoded).
       
 97308 ** Otherwise, for terms greater than or equal to pTerm[i] but less
       
 97309 ** than pTerm[i+1], the subtree for that term will be rooted at
       
 97310 ** iBlockid+i.  Interior nodes only store enough term data to
       
 97311 ** distinguish adjacent children (if the rightmost term of the left
       
 97312 ** child is "something", and the leftmost term of the right child is
       
 97313 ** "wicked", only "w" is stored).
       
 97314 **
       
 97315 ** New data is spilled to a new interior node at the same height when
       
 97316 ** the current node exceeds INTERIOR_MAX bytes (default 2048).
       
 97317 ** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
       
 97318 ** interior nodes and making the tree too skinny.  The interior nodes
       
 97319 ** at a given height are naturally tracked by interior nodes at
       
 97320 ** height+1, and so on.
       
 97321 **
       
 97322 **
       
 97323 **** Segment directory ****
       
 97324 ** The segment directory in table %_segdir stores meta-information for
       
 97325 ** merging and deleting segments, and also the root node of the
       
 97326 ** segment's tree.
       
 97327 **
       
 97328 ** The root node is the top node of the segment's tree after encoding
       
 97329 ** the entire segment, restricted to ROOT_MAX bytes (default 1024).
       
 97330 ** This could be either a leaf node or an interior node.  If the top
       
 97331 ** node requires more than ROOT_MAX bytes, it is flushed to %_segments
       
 97332 ** and a new root interior node is generated (which should always fit
       
 97333 ** within ROOT_MAX because it only needs space for 2 varints, the
       
 97334 ** height and the blockid of the previous root).
       
 97335 **
       
 97336 ** The meta-information in the segment directory is:
       
 97337 **   level               - segment level (see below)
       
 97338 **   idx                 - index within level
       
 97339 **                       - (level,idx uniquely identify a segment)
       
 97340 **   start_block         - first leaf node
       
 97341 **   leaves_end_block    - last leaf node
       
 97342 **   end_block           - last block (including interior nodes)
       
 97343 **   root                - contents of root node
       
 97344 **
       
 97345 ** If the root node is a leaf node, then start_block,
       
 97346 ** leaves_end_block, and end_block are all 0.
       
 97347 **
       
 97348 **
       
 97349 **** Segment merging ****
       
 97350 ** To amortize update costs, segments are grouped into levels and
       
 97351 ** merged in batches.  Each increase in level represents exponentially
       
 97352 ** more documents.
       
 97353 **
       
 97354 ** New documents (actually, document updates) are tokenized and
       
 97355 ** written individually (using LeafWriter) to a level 0 segment, with
       
 97356 ** incrementing idx.  When idx reaches MERGE_COUNT (default 16), all
       
 97357 ** level 0 segments are merged into a single level 1 segment.  Level 1
       
 97358 ** is populated like level 0, and eventually MERGE_COUNT level 1
       
 97359 ** segments are merged to a single level 2 segment (representing
       
 97360 ** MERGE_COUNT^2 updates), and so on.
       
 97361 **
       
 97362 ** A segment merge traverses all segments at a given level in
       
 97363 ** parallel, performing a straightforward sorted merge.  Since segment
       
 97364 ** leaf nodes are written in to the %_segments table in order, this
       
 97365 ** merge traverses the underlying sqlite disk structures efficiently.
       
 97366 ** After the merge, all segment blocks from the merged level are
       
 97367 ** deleted.
       
 97368 **
       
 97369 ** MERGE_COUNT controls how often we merge segments.  16 seems to be
       
 97370 ** somewhat of a sweet spot for insertion performance.  32 and 64 show
       
 97371 ** very similar performance numbers to 16 on insertion, though they're
       
 97372 ** a tiny bit slower (perhaps due to more overhead in merge-time
       
 97373 ** sorting).  8 is about 20% slower than 16, 4 about 50% slower than
       
 97374 ** 16, 2 about 66% slower than 16.
       
 97375 **
       
 97376 ** At query time, high MERGE_COUNT increases the number of segments
       
 97377 ** which need to be scanned and merged.  For instance, with 100k docs
       
 97378 ** inserted:
       
 97379 **
       
 97380 **    MERGE_COUNT   segments
       
 97381 **       16           25
       
 97382 **        8           12
       
 97383 **        4           10
       
 97384 **        2            6
       
 97385 **
       
 97386 ** This appears to have only a moderate impact on queries for very
       
 97387 ** frequent terms (which are somewhat dominated by segment merge
       
 97388 ** costs), and infrequent and non-existent terms still seem to be fast
       
 97389 ** even with many segments.
       
 97390 **
       
 97391 ** TODO(shess) That said, it would be nice to have a better query-side
       
 97392 ** argument for MERGE_COUNT of 16.  Also, it is possible/likely that
       
 97393 ** optimizations to things like doclist merging will swing the sweet
       
 97394 ** spot around.
       
 97395 **
       
 97396 **
       
 97397 **
       
 97398 **** Handling of deletions and updates ****
       
 97399 ** Since we're using a segmented structure, with no docid-oriented
       
 97400 ** index into the term index, we clearly cannot simply update the term
       
 97401 ** index when a document is deleted or updated.  For deletions, we
       
 97402 ** write an empty doclist (varint(docid) varint(POS_END)), for updates
       
 97403 ** we simply write the new doclist.  Segment merges overwrite older
       
 97404 ** data for a particular docid with newer data, so deletes or updates
       
 97405 ** will eventually overtake the earlier data and knock it out.  The
       
 97406 ** query logic likewise merges doclists so that newer data knocks out
       
 97407 ** older data.
       
 97408 **
       
 97409 ** TODO(shess) Provide a VACUUM type operation to clear out all
       
 97410 ** deletions and duplications.  This would basically be a forced merge
       
 97411 ** into a single segment.
       
 97412 */
       
 97413 
       
 97414 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
       
 97415 
       
 97416 #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
       
 97417 # define SQLITE_CORE 1
       
 97418 #endif
       
 97419 
       
 97420 
       
 97421 /************** Include fts3_expr.h in the middle of fts3.c ******************/
       
 97422 /************** Begin file fts3_expr.h ***************************************/
       
 97423 /*
       
 97424 ** 2008 Nov 28
       
 97425 **
       
 97426 ** The author disclaims copyright to this source code.  In place of
       
 97427 ** a legal notice, here is a blessing:
       
 97428 **
       
 97429 **    May you do good and not evil.
       
 97430 **    May you find forgiveness for yourself and forgive others.
       
 97431 **    May you share freely, never taking more than you give.
       
 97432 **
       
 97433 ******************************************************************************
       
 97434 **
       
 97435 */
       
 97436 
       
 97437 /************** Include fts3_tokenizer.h in the middle of fts3_expr.h ********/
       
 97438 /************** Begin file fts3_tokenizer.h **********************************/
       
 97439 /*
       
 97440 ** 2006 July 10
       
 97441 **
       
 97442 ** The author disclaims copyright to this source code.
       
 97443 **
       
 97444 *************************************************************************
       
 97445 ** Defines the interface to tokenizers used by fulltext-search.  There
       
 97446 ** are three basic components:
       
 97447 **
       
 97448 ** sqlite3_tokenizer_module is a singleton defining the tokenizer
       
 97449 ** interface functions.  This is essentially the class structure for
       
 97450 ** tokenizers.
       
 97451 **
       
 97452 ** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
       
 97453 ** including customization information defined at creation time.
       
 97454 **
       
 97455 ** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
       
 97456 ** tokens from a particular input.
       
 97457 */
       
 97458 #ifndef _FTS3_TOKENIZER_H_
       
 97459 #define _FTS3_TOKENIZER_H_
       
 97460 
       
 97461 /* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
       
 97462 ** If tokenizers are to be allowed to call sqlite3_*() functions, then
       
 97463 ** we will need a way to register the API consistently.
       
 97464 */
       
 97465 
       
 97466 /*
       
 97467 ** Structures used by the tokenizer interface. When a new tokenizer
       
 97468 ** implementation is registered, the caller provides a pointer to
       
 97469 ** an sqlite3_tokenizer_module containing pointers to the callback
       
 97470 ** functions that make up an implementation.
       
 97471 **
       
 97472 ** When an fts3 table is created, it passes any arguments passed to
       
 97473 ** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
       
 97474 ** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
       
 97475 ** implementation. The xCreate() function in turn returns an 
       
 97476 ** sqlite3_tokenizer structure representing the specific tokenizer to
       
 97477 ** be used for the fts3 table (customized by the tokenizer clause arguments).
       
 97478 **
       
 97479 ** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
       
 97480 ** method is called. It returns an sqlite3_tokenizer_cursor object
       
 97481 ** that may be used to tokenize a specific input buffer based on
       
 97482 ** the tokenization rules supplied by a specific sqlite3_tokenizer
       
 97483 ** object.
       
 97484 */
       
 97485 typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
       
 97486 typedef struct sqlite3_tokenizer sqlite3_tokenizer;
       
 97487 typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
       
 97488 
       
 97489 struct sqlite3_tokenizer_module {
       
 97490 
       
 97491   /*
       
 97492   ** Structure version. Should always be set to 0.
       
 97493   */
       
 97494   int iVersion;
       
 97495 
       
 97496   /*
       
 97497   ** Create a new tokenizer. The values in the argv[] array are the
       
 97498   ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
       
 97499   ** TABLE statement that created the fts3 table. For example, if
       
 97500   ** the following SQL is executed:
       
 97501   **
       
 97502   **   CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
       
 97503   **
       
 97504   ** then argc is set to 2, and the argv[] array contains pointers
       
 97505   ** to the strings "arg1" and "arg2".
       
 97506   **
       
 97507   ** This method should return either SQLITE_OK (0), or an SQLite error 
       
 97508   ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
       
 97509   ** to point at the newly created tokenizer structure. The generic
       
 97510   ** sqlite3_tokenizer.pModule variable should not be initialised by
       
 97511   ** this callback. The caller will do so.
       
 97512   */
       
 97513   int (*xCreate)(
       
 97514     int argc,                           /* Size of argv array */
       
 97515     const char *const*argv,             /* Tokenizer argument strings */
       
 97516     sqlite3_tokenizer **ppTokenizer     /* OUT: Created tokenizer */
       
 97517   );
       
 97518 
       
 97519   /*
       
 97520   ** Destroy an existing tokenizer. The fts3 module calls this method
       
 97521   ** exactly once for each successful call to xCreate().
       
 97522   */
       
 97523   int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
       
 97524 
       
 97525   /*
       
 97526   ** Create a tokenizer cursor to tokenize an input buffer. The caller
       
 97527   ** is responsible for ensuring that the input buffer remains valid
       
 97528   ** until the cursor is closed (using the xClose() method). 
       
 97529   */
       
 97530   int (*xOpen)(
       
 97531     sqlite3_tokenizer *pTokenizer,       /* Tokenizer object */
       
 97532     const char *pInput, int nBytes,      /* Input buffer */
       
 97533     sqlite3_tokenizer_cursor **ppCursor  /* OUT: Created tokenizer cursor */
       
 97534   );
       
 97535 
       
 97536   /*
       
 97537   ** Destroy an existing tokenizer cursor. The fts3 module calls this 
       
 97538   ** method exactly once for each successful call to xOpen().
       
 97539   */
       
 97540   int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
       
 97541 
       
 97542   /*
       
 97543   ** Retrieve the next token from the tokenizer cursor pCursor. This
       
 97544   ** method should either return SQLITE_OK and set the values of the
       
 97545   ** "OUT" variables identified below, or SQLITE_DONE to indicate that
       
 97546   ** the end of the buffer has been reached, or an SQLite error code.
       
 97547   **
       
 97548   ** *ppToken should be set to point at a buffer containing the 
       
 97549   ** normalized version of the token (i.e. after any case-folding and/or
       
 97550   ** stemming has been performed). *pnBytes should be set to the length
       
 97551   ** of this buffer in bytes. The input text that generated the token is
       
 97552   ** identified by the byte offsets returned in *piStartOffset and
       
 97553   ** *piEndOffset. *piStartOffset should be set to the index of the first
       
 97554   ** byte of the token in the input buffer. *piEndOffset should be set
       
 97555   ** to the index of the first byte just past the end of the token in
       
 97556   ** the input buffer.
       
 97557   **
       
 97558   ** The buffer *ppToken is set to point at is managed by the tokenizer
       
 97559   ** implementation. It is only required to be valid until the next call
       
 97560   ** to xNext() or xClose(). 
       
 97561   */
       
 97562   /* TODO(shess) current implementation requires pInput to be
       
 97563   ** nul-terminated.  This should either be fixed, or pInput/nBytes
       
 97564   ** should be converted to zInput.
       
 97565   */
       
 97566   int (*xNext)(
       
 97567     sqlite3_tokenizer_cursor *pCursor,   /* Tokenizer cursor */
       
 97568     const char **ppToken, int *pnBytes,  /* OUT: Normalized text for token */
       
 97569     int *piStartOffset,  /* OUT: Byte offset of token in input buffer */
       
 97570     int *piEndOffset,    /* OUT: Byte offset of end of token in input buffer */
       
 97571     int *piPosition      /* OUT: Number of tokens returned before this one */
       
 97572   );
       
 97573 };
       
 97574 
       
 97575 struct sqlite3_tokenizer {
       
 97576   const sqlite3_tokenizer_module *pModule;  /* The module for this tokenizer */
       
 97577   /* Tokenizer implementations will typically add additional fields */
       
 97578 };
       
 97579 
       
 97580 struct sqlite3_tokenizer_cursor {
       
 97581   sqlite3_tokenizer *pTokenizer;       /* Tokenizer for this cursor. */
       
 97582   /* Tokenizer implementations will typically add additional fields */
       
 97583 };
       
 97584 
       
 97585 #endif /* _FTS3_TOKENIZER_H_ */
       
 97586 
       
 97587 /************** End of fts3_tokenizer.h **************************************/
       
 97588 /************** Continuing where we left off in fts3_expr.h ******************/
       
 97589 
       
 97590 /*
       
 97591 ** The following describes the syntax supported by the fts3 MATCH
       
 97592 ** operator in a similar format to that used by the lemon parser
       
 97593 ** generator. This module does not use actually lemon, it uses a
       
 97594 ** custom parser.
       
 97595 **
       
 97596 **   query ::= andexpr (OR andexpr)*.
       
 97597 **
       
 97598 **   andexpr ::= notexpr (AND? notexpr)*.
       
 97599 **
       
 97600 **   notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
       
 97601 **   notexpr ::= LP query RP.
       
 97602 **
       
 97603 **   nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
       
 97604 **
       
 97605 **   distance_opt ::= .
       
 97606 **   distance_opt ::= / INTEGER.
       
 97607 **
       
 97608 **   phrase ::= TOKEN.
       
 97609 **   phrase ::= COLUMN:TOKEN.
       
 97610 **   phrase ::= "TOKEN TOKEN TOKEN...".
       
 97611 */
       
 97612 
       
 97613 typedef struct Fts3Expr Fts3Expr;
       
 97614 typedef struct Fts3Phrase Fts3Phrase;
       
 97615 
       
 97616 /*
       
 97617 ** A "phrase" is a sequence of one or more tokens that must match in
       
 97618 ** sequence.  A single token is the base case and the most common case.
       
 97619 ** For a sequence of tokens contained in "...", nToken will be the number
       
 97620 ** of tokens in the string.
       
 97621 */
       
 97622 struct Fts3Phrase {
       
 97623   int nToken;          /* Number of tokens in the phrase */
       
 97624   int iColumn;         /* Index of column this phrase must match */
       
 97625   int isNot;           /* Phrase prefixed by unary not (-) operator */
       
 97626   struct PhraseToken {
       
 97627     char *z;              /* Text of the token */
       
 97628     int n;                /* Number of bytes in buffer pointed to by z */
       
 97629     int isPrefix;         /* True if token ends in with a "*" character */
       
 97630   } aToken[1];         /* One entry for each token in the phrase */
       
 97631 };
       
 97632 
       
 97633 /*
       
 97634 ** A tree of these objects forms the RHS of a MATCH operator.
       
 97635 */
       
 97636 struct Fts3Expr {
       
 97637   int eType;                 /* One of the FTSQUERY_XXX values defined below */
       
 97638   int nNear;                 /* Valid if eType==FTSQUERY_NEAR */
       
 97639   Fts3Expr *pParent;         /* pParent->pLeft==this or pParent->pRight==this */
       
 97640   Fts3Expr *pLeft;           /* Left operand */
       
 97641   Fts3Expr *pRight;          /* Right operand */
       
 97642   Fts3Phrase *pPhrase;       /* Valid if eType==FTSQUERY_PHRASE */
       
 97643 };
       
 97644 
       
 97645 SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, char **, int, int, 
       
 97646                          const char *, int, Fts3Expr **);
       
 97647 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
       
 97648 
       
 97649 /*
       
 97650 ** Candidate values for Fts3Query.eType. Note that the order of the first
       
 97651 ** four values is in order of precedence when parsing expressions. For 
       
 97652 ** example, the following:
       
 97653 **
       
 97654 **   "a OR b AND c NOT d NEAR e"
       
 97655 **
       
 97656 ** is equivalent to:
       
 97657 **
       
 97658 **   "a OR (b AND (c NOT (d NEAR e)))"
       
 97659 */
       
 97660 #define FTSQUERY_NEAR   1
       
 97661 #define FTSQUERY_NOT    2
       
 97662 #define FTSQUERY_AND    3
       
 97663 #define FTSQUERY_OR     4
       
 97664 #define FTSQUERY_PHRASE 5
       
 97665 
       
 97666 #ifdef SQLITE_TEST
       
 97667 SQLITE_PRIVATE void sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
       
 97668 #endif
       
 97669 
       
 97670 /************** End of fts3_expr.h *******************************************/
       
 97671 /************** Continuing where we left off in fts3.c ***********************/
       
 97672 /************** Include fts3_hash.h in the middle of fts3.c ******************/
       
 97673 /************** Begin file fts3_hash.h ***************************************/
       
 97674 /*
       
 97675 ** 2001 September 22
       
 97676 **
       
 97677 ** The author disclaims copyright to this source code.  In place of
       
 97678 ** a legal notice, here is a blessing:
       
 97679 **
       
 97680 **    May you do good and not evil.
       
 97681 **    May you find forgiveness for yourself and forgive others.
       
 97682 **    May you share freely, never taking more than you give.
       
 97683 **
       
 97684 *************************************************************************
       
 97685 ** This is the header file for the generic hash-table implemenation
       
 97686 ** used in SQLite.  We've modified it slightly to serve as a standalone
       
 97687 ** hash table implementation for the full-text indexing module.
       
 97688 **
       
 97689 */
       
 97690 #ifndef _FTS3_HASH_H_
       
 97691 #define _FTS3_HASH_H_
       
 97692 
       
 97693 /* Forward declarations of structures. */
       
 97694 typedef struct fts3Hash fts3Hash;
       
 97695 typedef struct fts3HashElem fts3HashElem;
       
 97696 
       
 97697 /* A complete hash table is an instance of the following structure.
       
 97698 ** The internals of this structure are intended to be opaque -- client
       
 97699 ** code should not attempt to access or modify the fields of this structure
       
 97700 ** directly.  Change this structure only by using the routines below.
       
 97701 ** However, many of the "procedures" and "functions" for modifying and
       
 97702 ** accessing this structure are really macros, so we can't really make
       
 97703 ** this structure opaque.
       
 97704 */
       
 97705 struct fts3Hash {
       
 97706   char keyClass;          /* HASH_INT, _POINTER, _STRING, _BINARY */
       
 97707   char copyKey;           /* True if copy of key made on insert */
       
 97708   int count;              /* Number of entries in this table */
       
 97709   fts3HashElem *first;    /* The first element of the array */
       
 97710   int htsize;             /* Number of buckets in the hash table */
       
 97711   struct _fts3ht {        /* the hash table */
       
 97712     int count;               /* Number of entries with this hash */
       
 97713     fts3HashElem *chain;     /* Pointer to first entry with this hash */
       
 97714   } *ht;
       
 97715 };
       
 97716 
       
 97717 /* Each element in the hash table is an instance of the following 
       
 97718 ** structure.  All elements are stored on a single doubly-linked list.
       
 97719 **
       
 97720 ** Again, this structure is intended to be opaque, but it can't really
       
 97721 ** be opaque because it is used by macros.
       
 97722 */
       
 97723 struct fts3HashElem {
       
 97724   fts3HashElem *next, *prev; /* Next and previous elements in the table */
       
 97725   void *data;                /* Data associated with this element */
       
 97726   void *pKey; int nKey;      /* Key associated with this element */
       
 97727 };
       
 97728 
       
 97729 /*
       
 97730 ** There are 2 different modes of operation for a hash table:
       
 97731 **
       
 97732 **   FTS3_HASH_STRING        pKey points to a string that is nKey bytes long
       
 97733 **                           (including the null-terminator, if any).  Case
       
 97734 **                           is respected in comparisons.
       
 97735 **
       
 97736 **   FTS3_HASH_BINARY        pKey points to binary data nKey bytes long. 
       
 97737 **                           memcmp() is used to compare keys.
       
 97738 **
       
 97739 ** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.  
       
 97740 */
       
 97741 #define FTS3_HASH_STRING    1
       
 97742 #define FTS3_HASH_BINARY    2
       
 97743 
       
 97744 /*
       
 97745 ** Access routines.  To delete, insert a NULL pointer.
       
 97746 */
       
 97747 SQLITE_PRIVATE void sqlite3Fts3HashInit(fts3Hash*, int keytype, int copyKey);
       
 97748 SQLITE_PRIVATE void *sqlite3Fts3HashInsert(fts3Hash*, const void *pKey, int nKey, void *pData);
       
 97749 SQLITE_PRIVATE void *sqlite3Fts3HashFind(const fts3Hash*, const void *pKey, int nKey);
       
 97750 SQLITE_PRIVATE void sqlite3Fts3HashClear(fts3Hash*);
       
 97751 
       
 97752 /*
       
 97753 ** Shorthand for the functions above
       
 97754 */
       
 97755 #define fts3HashInit   sqlite3Fts3HashInit
       
 97756 #define fts3HashInsert sqlite3Fts3HashInsert
       
 97757 #define fts3HashFind   sqlite3Fts3HashFind
       
 97758 #define fts3HashClear  sqlite3Fts3HashClear
       
 97759 
       
 97760 /*
       
 97761 ** Macros for looping over all elements of a hash table.  The idiom is
       
 97762 ** like this:
       
 97763 **
       
 97764 **   fts3Hash h;
       
 97765 **   fts3HashElem *p;
       
 97766 **   ...
       
 97767 **   for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
       
 97768 **     SomeStructure *pData = fts3HashData(p);
       
 97769 **     // do something with pData
       
 97770 **   }
       
 97771 */
       
 97772 #define fts3HashFirst(H)  ((H)->first)
       
 97773 #define fts3HashNext(E)   ((E)->next)
       
 97774 #define fts3HashData(E)   ((E)->data)
       
 97775 #define fts3HashKey(E)    ((E)->pKey)
       
 97776 #define fts3HashKeysize(E) ((E)->nKey)
       
 97777 
       
 97778 /*
       
 97779 ** Number of entries in a hash table
       
 97780 */
       
 97781 #define fts3HashCount(H)  ((H)->count)
       
 97782 
       
 97783 #endif /* _FTS3_HASH_H_ */
       
 97784 
       
 97785 /************** End of fts3_hash.h *******************************************/
       
 97786 /************** Continuing where we left off in fts3.c ***********************/
       
 97787 #ifndef SQLITE_CORE 
       
 97788   SQLITE_EXTENSION_INIT1
       
 97789 #endif
       
 97790 
       
 97791 
       
 97792 /* TODO(shess) MAN, this thing needs some refactoring.  At minimum, it
       
 97793 ** would be nice to order the file better, perhaps something along the
       
 97794 ** lines of:
       
 97795 **
       
 97796 **  - utility functions
       
 97797 **  - table setup functions
       
 97798 **  - table update functions
       
 97799 **  - table query functions
       
 97800 **
       
 97801 ** Put the query functions last because they're likely to reference
       
 97802 ** typedefs or functions from the table update section.
       
 97803 */
       
 97804 
       
 97805 #if 0
       
 97806 # define FTSTRACE(A)  printf A; fflush(stdout)
       
 97807 #else
       
 97808 # define FTSTRACE(A)
       
 97809 #endif
       
 97810 
       
 97811 /* It is not safe to call isspace(), tolower(), or isalnum() on
       
 97812 ** hi-bit-set characters.  This is the same solution used in the
       
 97813 ** tokenizer.
       
 97814 */
       
 97815 /* TODO(shess) The snippet-generation code should be using the
       
 97816 ** tokenizer-generated tokens rather than doing its own local
       
 97817 ** tokenization.
       
 97818 */
       
 97819 /* TODO(shess) Is __isascii() a portable version of (c&0x80)==0? */
       
 97820 static int safe_isspace(char c){
       
 97821   return (c&0x80)==0 ? isspace(c) : 0;
       
 97822 }
       
 97823 static int safe_tolower(char c){
       
 97824   return (c&0x80)==0 ? tolower(c) : c;
       
 97825 }
       
 97826 static int safe_isalnum(char c){
       
 97827   return (c&0x80)==0 ? isalnum(c) : 0;
       
 97828 }
       
 97829 
       
 97830 typedef enum DocListType {
       
 97831   DL_DOCIDS,              /* docids only */
       
 97832   DL_POSITIONS,           /* docids + positions */
       
 97833   DL_POSITIONS_OFFSETS    /* docids + positions + offsets */
       
 97834 } DocListType;
       
 97835 
       
 97836 /*
       
 97837 ** By default, only positions and not offsets are stored in the doclists.
       
 97838 ** To change this so that offsets are stored too, compile with
       
 97839 **
       
 97840 **          -DDL_DEFAULT=DL_POSITIONS_OFFSETS
       
 97841 **
       
 97842 ** If DL_DEFAULT is set to DL_DOCIDS, your table can only be inserted
       
 97843 ** into (no deletes or updates).
       
 97844 */
       
 97845 #ifndef DL_DEFAULT
       
 97846 # define DL_DEFAULT DL_POSITIONS
       
 97847 #endif
       
 97848 
       
 97849 enum {
       
 97850   POS_END = 0,        /* end of this position list */
       
 97851   POS_COLUMN,         /* followed by new column number */
       
 97852   POS_BASE
       
 97853 };
       
 97854 
       
 97855 /* MERGE_COUNT controls how often we merge segments (see comment at
       
 97856 ** top of file).
       
 97857 */
       
 97858 #define MERGE_COUNT 16
       
 97859 
       
 97860 /* utility functions */
       
 97861 
       
 97862 /* CLEAR() and SCRAMBLE() abstract memset() on a pointer to a single
       
 97863 ** record to prevent errors of the form:
       
 97864 **
       
 97865 ** my_function(SomeType *b){
       
 97866 **   memset(b, '\0', sizeof(b));  // sizeof(b)!=sizeof(*b)
       
 97867 ** }
       
 97868 */
       
 97869 /* TODO(shess) Obvious candidates for a header file. */
       
 97870 #define CLEAR(b) memset(b, '\0', sizeof(*(b)))
       
 97871 
       
 97872 #ifndef NDEBUG
       
 97873 #  define SCRAMBLE(b) memset(b, 0x55, sizeof(*(b)))
       
 97874 #else
       
 97875 #  define SCRAMBLE(b)
       
 97876 #endif
       
 97877 
       
 97878 /* We may need up to VARINT_MAX bytes to store an encoded 64-bit integer. */
       
 97879 #define VARINT_MAX 10
       
 97880 
       
 97881 /* Write a 64-bit variable-length integer to memory starting at p[0].
       
 97882  * The length of data written will be between 1 and VARINT_MAX bytes.
       
 97883  * The number of bytes written is returned. */
       
 97884 static int fts3PutVarint(char *p, sqlite_int64 v){
       
 97885   unsigned char *q = (unsigned char *) p;
       
 97886   sqlite_uint64 vu = v;
       
 97887   do{
       
 97888     *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
       
 97889     vu >>= 7;
       
 97890   }while( vu!=0 );
       
 97891   q[-1] &= 0x7f;  /* turn off high bit in final byte */
       
 97892   assert( q - (unsigned char *)p <= VARINT_MAX );
       
 97893   return (int) (q - (unsigned char *)p);
       
 97894 }
       
 97895 
       
 97896 /* Read a 64-bit variable-length integer from memory starting at p[0].
       
 97897  * Return the number of bytes read, or 0 on error.
       
 97898  * The value is stored in *v. */
       
 97899 static int fts3GetVarint(const char *p, sqlite_int64 *v){
       
 97900   const unsigned char *q = (const unsigned char *) p;
       
 97901   sqlite_uint64 x = 0, y = 1;
       
 97902   while( (*q & 0x80) == 0x80 ){
       
 97903     x += y * (*q++ & 0x7f);
       
 97904     y <<= 7;
       
 97905     if( q - (unsigned char *)p >= VARINT_MAX ){  /* bad data */
       
 97906       assert( 0 );
       
 97907       return 0;
       
 97908     }
       
 97909   }
       
 97910   x += y * (*q++);
       
 97911   *v = (sqlite_int64) x;
       
 97912   return (int) (q - (unsigned char *)p);
       
 97913 }
       
 97914 
       
 97915 static int fts3GetVarint32(const char *p, int *pi){
       
 97916  sqlite_int64 i;
       
 97917  int ret = fts3GetVarint(p, &i);
       
 97918  *pi = (int) i;
       
 97919  assert( *pi==i );
       
 97920  return ret;
       
 97921 }
       
 97922 
       
 97923 /*******************************************************************/
       
 97924 /* DataBuffer is used to collect data into a buffer in piecemeal
       
 97925 ** fashion.  It implements the usual distinction between amount of
       
 97926 ** data currently stored (nData) and buffer capacity (nCapacity).
       
 97927 **
       
 97928 ** dataBufferInit - create a buffer with given initial capacity.
       
 97929 ** dataBufferReset - forget buffer's data, retaining capacity.
       
 97930 ** dataBufferDestroy - free buffer's data.
       
 97931 ** dataBufferSwap - swap contents of two buffers.
       
 97932 ** dataBufferExpand - expand capacity without adding data.
       
 97933 ** dataBufferAppend - append data.
       
 97934 ** dataBufferAppend2 - append two pieces of data at once.
       
 97935 ** dataBufferReplace - replace buffer's data.
       
 97936 */
       
 97937 typedef struct DataBuffer {
       
 97938   char *pData;          /* Pointer to malloc'ed buffer. */
       
 97939   int nCapacity;        /* Size of pData buffer. */
       
 97940   int nData;            /* End of data loaded into pData. */
       
 97941 } DataBuffer;
       
 97942 
       
 97943 static void dataBufferInit(DataBuffer *pBuffer, int nCapacity){
       
 97944   assert( nCapacity>=0 );
       
 97945   pBuffer->nData = 0;
       
 97946   pBuffer->nCapacity = nCapacity;
       
 97947   pBuffer->pData = nCapacity==0 ? NULL : sqlite3_malloc(nCapacity);
       
 97948 }
       
 97949 static void dataBufferReset(DataBuffer *pBuffer){
       
 97950   pBuffer->nData = 0;
       
 97951 }
       
 97952 static void dataBufferDestroy(DataBuffer *pBuffer){
       
 97953   if( pBuffer->pData!=NULL ) sqlite3_free(pBuffer->pData);
       
 97954   SCRAMBLE(pBuffer);
       
 97955 }
       
 97956 static void dataBufferSwap(DataBuffer *pBuffer1, DataBuffer *pBuffer2){
       
 97957   DataBuffer tmp = *pBuffer1;
       
 97958   *pBuffer1 = *pBuffer2;
       
 97959   *pBuffer2 = tmp;
       
 97960 }
       
 97961 static void dataBufferExpand(DataBuffer *pBuffer, int nAddCapacity){
       
 97962   assert( nAddCapacity>0 );
       
 97963   /* TODO(shess) Consider expanding more aggressively.  Note that the
       
 97964   ** underlying malloc implementation may take care of such things for
       
 97965   ** us already.
       
 97966   */
       
 97967   if( pBuffer->nData+nAddCapacity>pBuffer->nCapacity ){
       
 97968     pBuffer->nCapacity = pBuffer->nData+nAddCapacity;
       
 97969     pBuffer->pData = sqlite3_realloc(pBuffer->pData, pBuffer->nCapacity);
       
 97970   }
       
 97971 }
       
 97972 static void dataBufferAppend(DataBuffer *pBuffer,
       
 97973                              const char *pSource, int nSource){
       
 97974   assert( nSource>0 && pSource!=NULL );
       
 97975   dataBufferExpand(pBuffer, nSource);
       
 97976   memcpy(pBuffer->pData+pBuffer->nData, pSource, nSource);
       
 97977   pBuffer->nData += nSource;
       
 97978 }
       
 97979 static void dataBufferAppend2(DataBuffer *pBuffer,
       
 97980                               const char *pSource1, int nSource1,
       
 97981                               const char *pSource2, int nSource2){
       
 97982   assert( nSource1>0 && pSource1!=NULL );
       
 97983   assert( nSource2>0 && pSource2!=NULL );
       
 97984   dataBufferExpand(pBuffer, nSource1+nSource2);
       
 97985   memcpy(pBuffer->pData+pBuffer->nData, pSource1, nSource1);
       
 97986   memcpy(pBuffer->pData+pBuffer->nData+nSource1, pSource2, nSource2);
       
 97987   pBuffer->nData += nSource1+nSource2;
       
 97988 }
       
 97989 static void dataBufferReplace(DataBuffer *pBuffer,
       
 97990                               const char *pSource, int nSource){
       
 97991   dataBufferReset(pBuffer);
       
 97992   dataBufferAppend(pBuffer, pSource, nSource);
       
 97993 }
       
 97994 
       
 97995 /* StringBuffer is a null-terminated version of DataBuffer. */
       
 97996 typedef struct StringBuffer {
       
 97997   DataBuffer b;            /* Includes null terminator. */
       
 97998 } StringBuffer;
       
 97999 
       
 98000 static void initStringBuffer(StringBuffer *sb){
       
 98001   dataBufferInit(&sb->b, 100);
       
 98002   dataBufferReplace(&sb->b, "", 1);
       
 98003 }
       
 98004 static int stringBufferLength(StringBuffer *sb){
       
 98005   return sb->b.nData-1;
       
 98006 }
       
 98007 static char *stringBufferData(StringBuffer *sb){
       
 98008   return sb->b.pData;
       
 98009 }
       
 98010 static void stringBufferDestroy(StringBuffer *sb){
       
 98011   dataBufferDestroy(&sb->b);
       
 98012 }
       
 98013 
       
 98014 static void nappend(StringBuffer *sb, const char *zFrom, int nFrom){
       
 98015   assert( sb->b.nData>0 );
       
 98016   if( nFrom>0 ){
       
 98017     sb->b.nData--;
       
 98018     dataBufferAppend2(&sb->b, zFrom, nFrom, "", 1);
       
 98019   }
       
 98020 }
       
 98021 static void append(StringBuffer *sb, const char *zFrom){
       
 98022   nappend(sb, zFrom, strlen(zFrom));
       
 98023 }
       
 98024 
       
 98025 /* Append a list of strings separated by commas. */
       
 98026 static void appendList(StringBuffer *sb, int nString, char **azString){
       
 98027   int i;
       
 98028   for(i=0; i<nString; ++i){
       
 98029     if( i>0 ) append(sb, ", ");
       
 98030     append(sb, azString[i]);
       
 98031   }
       
 98032 }
       
 98033 
       
 98034 static int endsInWhiteSpace(StringBuffer *p){
       
 98035   return stringBufferLength(p)>0 &&
       
 98036     safe_isspace(stringBufferData(p)[stringBufferLength(p)-1]);
       
 98037 }
       
 98038 
       
 98039 /* If the StringBuffer ends in something other than white space, add a
       
 98040 ** single space character to the end.
       
 98041 */
       
 98042 static void appendWhiteSpace(StringBuffer *p){
       
 98043   if( stringBufferLength(p)==0 ) return;
       
 98044   if( !endsInWhiteSpace(p) ) append(p, " ");
       
 98045 }
       
 98046 
       
 98047 /* Remove white space from the end of the StringBuffer */
       
 98048 static void trimWhiteSpace(StringBuffer *p){
       
 98049   while( endsInWhiteSpace(p) ){
       
 98050     p->b.pData[--p->b.nData-1] = '\0';
       
 98051   }
       
 98052 }
       
 98053 
       
 98054 /*******************************************************************/
       
 98055 /* DLReader is used to read document elements from a doclist.  The
       
 98056 ** current docid is cached, so dlrDocid() is fast.  DLReader does not
       
 98057 ** own the doclist buffer.
       
 98058 **
       
 98059 ** dlrAtEnd - true if there's no more data to read.
       
 98060 ** dlrDocid - docid of current document.
       
 98061 ** dlrDocData - doclist data for current document (including docid).
       
 98062 ** dlrDocDataBytes - length of same.
       
 98063 ** dlrAllDataBytes - length of all remaining data.
       
 98064 ** dlrPosData - position data for current document.
       
 98065 ** dlrPosDataLen - length of pos data for current document (incl POS_END).
       
 98066 ** dlrStep - step to current document.
       
 98067 ** dlrInit - initial for doclist of given type against given data.
       
 98068 ** dlrDestroy - clean up.
       
 98069 **
       
 98070 ** Expected usage is something like:
       
 98071 **
       
 98072 **   DLReader reader;
       
 98073 **   dlrInit(&reader, pData, nData);
       
 98074 **   while( !dlrAtEnd(&reader) ){
       
 98075 **     // calls to dlrDocid() and kin.
       
 98076 **     dlrStep(&reader);
       
 98077 **   }
       
 98078 **   dlrDestroy(&reader);
       
 98079 */
       
 98080 typedef struct DLReader {
       
 98081   DocListType iType;
       
 98082   const char *pData;
       
 98083   int nData;
       
 98084 
       
 98085   sqlite_int64 iDocid;
       
 98086   int nElement;
       
 98087 } DLReader;
       
 98088 
       
 98089 static int dlrAtEnd(DLReader *pReader){
       
 98090   assert( pReader->nData>=0 );
       
 98091   return pReader->nData==0;
       
 98092 }
       
 98093 static sqlite_int64 dlrDocid(DLReader *pReader){
       
 98094   assert( !dlrAtEnd(pReader) );
       
 98095   return pReader->iDocid;
       
 98096 }
       
 98097 static const char *dlrDocData(DLReader *pReader){
       
 98098   assert( !dlrAtEnd(pReader) );
       
 98099   return pReader->pData;
       
 98100 }
       
 98101 static int dlrDocDataBytes(DLReader *pReader){
       
 98102   assert( !dlrAtEnd(pReader) );
       
 98103   return pReader->nElement;
       
 98104 }
       
 98105 static int dlrAllDataBytes(DLReader *pReader){
       
 98106   assert( !dlrAtEnd(pReader) );
       
 98107   return pReader->nData;
       
 98108 }
       
 98109 /* TODO(shess) Consider adding a field to track iDocid varint length
       
 98110 ** to make these two functions faster.  This might matter (a tiny bit)
       
 98111 ** for queries.
       
 98112 */
       
 98113 static const char *dlrPosData(DLReader *pReader){
       
 98114   sqlite_int64 iDummy;
       
 98115   int n = fts3GetVarint(pReader->pData, &iDummy);
       
 98116   assert( !dlrAtEnd(pReader) );
       
 98117   return pReader->pData+n;
       
 98118 }
       
 98119 static int dlrPosDataLen(DLReader *pReader){
       
 98120   sqlite_int64 iDummy;
       
 98121   int n = fts3GetVarint(pReader->pData, &iDummy);
       
 98122   assert( !dlrAtEnd(pReader) );
       
 98123   return pReader->nElement-n;
       
 98124 }
       
 98125 static void dlrStep(DLReader *pReader){
       
 98126   assert( !dlrAtEnd(pReader) );
       
 98127 
       
 98128   /* Skip past current doclist element. */
       
 98129   assert( pReader->nElement<=pReader->nData );
       
 98130   pReader->pData += pReader->nElement;
       
 98131   pReader->nData -= pReader->nElement;
       
 98132 
       
 98133   /* If there is more data, read the next doclist element. */
       
 98134   if( pReader->nData!=0 ){
       
 98135     sqlite_int64 iDocidDelta;
       
 98136     int iDummy, n = fts3GetVarint(pReader->pData, &iDocidDelta);
       
 98137     pReader->iDocid += iDocidDelta;
       
 98138     if( pReader->iType>=DL_POSITIONS ){
       
 98139       assert( n<pReader->nData );
       
 98140       while( 1 ){
       
 98141         n += fts3GetVarint32(pReader->pData+n, &iDummy);
       
 98142         assert( n<=pReader->nData );
       
 98143         if( iDummy==POS_END ) break;
       
 98144         if( iDummy==POS_COLUMN ){
       
 98145           n += fts3GetVarint32(pReader->pData+n, &iDummy);
       
 98146           assert( n<pReader->nData );
       
 98147         }else if( pReader->iType==DL_POSITIONS_OFFSETS ){
       
 98148           n += fts3GetVarint32(pReader->pData+n, &iDummy);
       
 98149           n += fts3GetVarint32(pReader->pData+n, &iDummy);
       
 98150           assert( n<pReader->nData );
       
 98151         }
       
 98152       }
       
 98153     }
       
 98154     pReader->nElement = n;
       
 98155     assert( pReader->nElement<=pReader->nData );
       
 98156   }
       
 98157 }
       
 98158 static void dlrInit(DLReader *pReader, DocListType iType,
       
 98159                     const char *pData, int nData){
       
 98160   assert( pData!=NULL && nData!=0 );
       
 98161   pReader->iType = iType;
       
 98162   pReader->pData = pData;
       
 98163   pReader->nData = nData;
       
 98164   pReader->nElement = 0;
       
 98165   pReader->iDocid = 0;
       
 98166 
       
 98167   /* Load the first element's data.  There must be a first element. */
       
 98168   dlrStep(pReader);
       
 98169 }
       
 98170 static void dlrDestroy(DLReader *pReader){
       
 98171   SCRAMBLE(pReader);
       
 98172 }
       
 98173 
       
 98174 #ifndef NDEBUG
       
 98175 /* Verify that the doclist can be validly decoded.  Also returns the
       
 98176 ** last docid found because it is convenient in other assertions for
       
 98177 ** DLWriter.
       
 98178 */
       
 98179 static void docListValidate(DocListType iType, const char *pData, int nData,
       
 98180                             sqlite_int64 *pLastDocid){
       
 98181   sqlite_int64 iPrevDocid = 0;
       
 98182   assert( nData>0 );
       
 98183   assert( pData!=0 );
       
 98184   assert( pData+nData>pData );
       
 98185   while( nData!=0 ){
       
 98186     sqlite_int64 iDocidDelta;
       
 98187     int n = fts3GetVarint(pData, &iDocidDelta);
       
 98188     iPrevDocid += iDocidDelta;
       
 98189     if( iType>DL_DOCIDS ){
       
 98190       int iDummy;
       
 98191       while( 1 ){
       
 98192         n += fts3GetVarint32(pData+n, &iDummy);
       
 98193         if( iDummy==POS_END ) break;
       
 98194         if( iDummy==POS_COLUMN ){
       
 98195           n += fts3GetVarint32(pData+n, &iDummy);
       
 98196         }else if( iType>DL_POSITIONS ){
       
 98197           n += fts3GetVarint32(pData+n, &iDummy);
       
 98198           n += fts3GetVarint32(pData+n, &iDummy);
       
 98199         }
       
 98200         assert( n<=nData );
       
 98201       }
       
 98202     }
       
 98203     assert( n<=nData );
       
 98204     pData += n;
       
 98205     nData -= n;
       
 98206   }
       
 98207   if( pLastDocid ) *pLastDocid = iPrevDocid;
       
 98208 }
       
 98209 #define ASSERT_VALID_DOCLIST(i, p, n, o) docListValidate(i, p, n, o)
       
 98210 #else
       
 98211 #define ASSERT_VALID_DOCLIST(i, p, n, o) assert( 1 )
       
 98212 #endif
       
 98213 
       
 98214 /*******************************************************************/
       
 98215 /* DLWriter is used to write doclist data to a DataBuffer.  DLWriter
       
 98216 ** always appends to the buffer and does not own it.
       
 98217 **
       
 98218 ** dlwInit - initialize to write a given type doclistto a buffer.
       
 98219 ** dlwDestroy - clear the writer's memory.  Does not free buffer.
       
 98220 ** dlwAppend - append raw doclist data to buffer.
       
 98221 ** dlwCopy - copy next doclist from reader to writer.
       
 98222 ** dlwAdd - construct doclist element and append to buffer.
       
 98223 **    Only apply dlwAdd() to DL_DOCIDS doclists (else use PLWriter).
       
 98224 */
       
 98225 typedef struct DLWriter {
       
 98226   DocListType iType;
       
 98227   DataBuffer *b;
       
 98228   sqlite_int64 iPrevDocid;
       
 98229 #ifndef NDEBUG
       
 98230   int has_iPrevDocid;
       
 98231 #endif
       
 98232 } DLWriter;
       
 98233 
       
 98234 static void dlwInit(DLWriter *pWriter, DocListType iType, DataBuffer *b){
       
 98235   pWriter->b = b;
       
 98236   pWriter->iType = iType;
       
 98237   pWriter->iPrevDocid = 0;
       
 98238 #ifndef NDEBUG
       
 98239   pWriter->has_iPrevDocid = 0;
       
 98240 #endif
       
 98241 }
       
 98242 static void dlwDestroy(DLWriter *pWriter){
       
 98243   SCRAMBLE(pWriter);
       
 98244 }
       
 98245 /* iFirstDocid is the first docid in the doclist in pData.  It is
       
 98246 ** needed because pData may point within a larger doclist, in which
       
 98247 ** case the first item would be delta-encoded.
       
 98248 **
       
 98249 ** iLastDocid is the final docid in the doclist in pData.  It is
       
 98250 ** needed to create the new iPrevDocid for future delta-encoding.  The
       
 98251 ** code could decode the passed doclist to recreate iLastDocid, but
       
 98252 ** the only current user (docListMerge) already has decoded this
       
 98253 ** information.
       
 98254 */
       
 98255 /* TODO(shess) This has become just a helper for docListMerge.
       
 98256 ** Consider a refactor to make this cleaner.
       
 98257 */
       
 98258 static void dlwAppend(DLWriter *pWriter,
       
 98259                       const char *pData, int nData,
       
 98260                       sqlite_int64 iFirstDocid, sqlite_int64 iLastDocid){
       
 98261   sqlite_int64 iDocid = 0;
       
 98262   char c[VARINT_MAX];
       
 98263   int nFirstOld, nFirstNew;     /* Old and new varint len of first docid. */
       
 98264 #ifndef NDEBUG
       
 98265   sqlite_int64 iLastDocidDelta;
       
 98266 #endif
       
 98267 
       
 98268   /* Recode the initial docid as delta from iPrevDocid. */
       
 98269   nFirstOld = fts3GetVarint(pData, &iDocid);
       
 98270   assert( nFirstOld<nData || (nFirstOld==nData && pWriter->iType==DL_DOCIDS) );
       
 98271   nFirstNew = fts3PutVarint(c, iFirstDocid-pWriter->iPrevDocid);
       
 98272 
       
 98273   /* Verify that the incoming doclist is valid AND that it ends with
       
 98274   ** the expected docid.  This is essential because we'll trust this
       
 98275   ** docid in future delta-encoding.
       
 98276   */
       
 98277   ASSERT_VALID_DOCLIST(pWriter->iType, pData, nData, &iLastDocidDelta);
       
 98278   assert( iLastDocid==iFirstDocid-iDocid+iLastDocidDelta );
       
 98279 
       
 98280   /* Append recoded initial docid and everything else.  Rest of docids
       
 98281   ** should have been delta-encoded from previous initial docid.
       
 98282   */
       
 98283   if( nFirstOld<nData ){
       
 98284     dataBufferAppend2(pWriter->b, c, nFirstNew,
       
 98285                       pData+nFirstOld, nData-nFirstOld);
       
 98286   }else{
       
 98287     dataBufferAppend(pWriter->b, c, nFirstNew);
       
 98288   }
       
 98289   pWriter->iPrevDocid = iLastDocid;
       
 98290 }
       
 98291 static void dlwCopy(DLWriter *pWriter, DLReader *pReader){
       
 98292   dlwAppend(pWriter, dlrDocData(pReader), dlrDocDataBytes(pReader),
       
 98293             dlrDocid(pReader), dlrDocid(pReader));
       
 98294 }
       
 98295 static void dlwAdd(DLWriter *pWriter, sqlite_int64 iDocid){
       
 98296   char c[VARINT_MAX];
       
 98297   int n = fts3PutVarint(c, iDocid-pWriter->iPrevDocid);
       
 98298 
       
 98299   /* Docids must ascend. */
       
 98300   assert( !pWriter->has_iPrevDocid || iDocid>pWriter->iPrevDocid );
       
 98301   assert( pWriter->iType==DL_DOCIDS );
       
 98302 
       
 98303   dataBufferAppend(pWriter->b, c, n);
       
 98304   pWriter->iPrevDocid = iDocid;
       
 98305 #ifndef NDEBUG
       
 98306   pWriter->has_iPrevDocid = 1;
       
 98307 #endif
       
 98308 }
       
 98309 
       
 98310 /*******************************************************************/
       
 98311 /* PLReader is used to read data from a document's position list.  As
       
 98312 ** the caller steps through the list, data is cached so that varints
       
 98313 ** only need to be decoded once.
       
 98314 **
       
 98315 ** plrInit, plrDestroy - create/destroy a reader.
       
 98316 ** plrColumn, plrPosition, plrStartOffset, plrEndOffset - accessors
       
 98317 ** plrAtEnd - at end of stream, only call plrDestroy once true.
       
 98318 ** plrStep - step to the next element.
       
 98319 */
       
 98320 typedef struct PLReader {
       
 98321   /* These refer to the next position's data.  nData will reach 0 when
       
 98322   ** reading the last position, so plrStep() signals EOF by setting
       
 98323   ** pData to NULL.
       
 98324   */
       
 98325   const char *pData;
       
 98326   int nData;
       
 98327 
       
 98328   DocListType iType;
       
 98329   int iColumn;         /* the last column read */
       
 98330   int iPosition;       /* the last position read */
       
 98331   int iStartOffset;    /* the last start offset read */
       
 98332   int iEndOffset;      /* the last end offset read */
       
 98333 } PLReader;
       
 98334 
       
 98335 static int plrAtEnd(PLReader *pReader){
       
 98336   return pReader->pData==NULL;
       
 98337 }
       
 98338 static int plrColumn(PLReader *pReader){
       
 98339   assert( !plrAtEnd(pReader) );
       
 98340   return pReader->iColumn;
       
 98341 }
       
 98342 static int plrPosition(PLReader *pReader){
       
 98343   assert( !plrAtEnd(pReader) );
       
 98344   return pReader->iPosition;
       
 98345 }
       
 98346 static int plrStartOffset(PLReader *pReader){
       
 98347   assert( !plrAtEnd(pReader) );
       
 98348   return pReader->iStartOffset;
       
 98349 }
       
 98350 static int plrEndOffset(PLReader *pReader){
       
 98351   assert( !plrAtEnd(pReader) );
       
 98352   return pReader->iEndOffset;
       
 98353 }
       
 98354 static void plrStep(PLReader *pReader){
       
 98355   int i, n;
       
 98356 
       
 98357   assert( !plrAtEnd(pReader) );
       
 98358 
       
 98359   if( pReader->nData==0 ){
       
 98360     pReader->pData = NULL;
       
 98361     return;
       
 98362   }
       
 98363 
       
 98364   n = fts3GetVarint32(pReader->pData, &i);
       
 98365   if( i==POS_COLUMN ){
       
 98366     n += fts3GetVarint32(pReader->pData+n, &pReader->iColumn);
       
 98367     pReader->iPosition = 0;
       
 98368     pReader->iStartOffset = 0;
       
 98369     n += fts3GetVarint32(pReader->pData+n, &i);
       
 98370   }
       
 98371   /* Should never see adjacent column changes. */
       
 98372   assert( i!=POS_COLUMN );
       
 98373 
       
 98374   if( i==POS_END ){
       
 98375     pReader->nData = 0;
       
 98376     pReader->pData = NULL;
       
 98377     return;
       
 98378   }
       
 98379 
       
 98380   pReader->iPosition += i-POS_BASE;
       
 98381   if( pReader->iType==DL_POSITIONS_OFFSETS ){
       
 98382     n += fts3GetVarint32(pReader->pData+n, &i);
       
 98383     pReader->iStartOffset += i;
       
 98384     n += fts3GetVarint32(pReader->pData+n, &i);
       
 98385     pReader->iEndOffset = pReader->iStartOffset+i;
       
 98386   }
       
 98387   assert( n<=pReader->nData );
       
 98388   pReader->pData += n;
       
 98389   pReader->nData -= n;
       
 98390 }
       
 98391 
       
 98392 static void plrInit(PLReader *pReader, DLReader *pDLReader){
       
 98393   pReader->pData = dlrPosData(pDLReader);
       
 98394   pReader->nData = dlrPosDataLen(pDLReader);
       
 98395   pReader->iType = pDLReader->iType;
       
 98396   pReader->iColumn = 0;
       
 98397   pReader->iPosition = 0;
       
 98398   pReader->iStartOffset = 0;
       
 98399   pReader->iEndOffset = 0;
       
 98400   plrStep(pReader);
       
 98401 }
       
 98402 static void plrDestroy(PLReader *pReader){
       
 98403   SCRAMBLE(pReader);
       
 98404 }
       
 98405 
       
 98406 /*******************************************************************/
       
 98407 /* PLWriter is used in constructing a document's position list.  As a
       
 98408 ** convenience, if iType is DL_DOCIDS, PLWriter becomes a no-op.
       
 98409 ** PLWriter writes to the associated DLWriter's buffer.
       
 98410 **
       
 98411 ** plwInit - init for writing a document's poslist.
       
 98412 ** plwDestroy - clear a writer.
       
 98413 ** plwAdd - append position and offset information.
       
 98414 ** plwCopy - copy next position's data from reader to writer.
       
 98415 ** plwTerminate - add any necessary doclist terminator.
       
 98416 **
       
 98417 ** Calling plwAdd() after plwTerminate() may result in a corrupt
       
 98418 ** doclist.
       
 98419 */
       
 98420 /* TODO(shess) Until we've written the second item, we can cache the
       
 98421 ** first item's information.  Then we'd have three states:
       
 98422 **
       
 98423 ** - initialized with docid, no positions.
       
 98424 ** - docid and one position.
       
 98425 ** - docid and multiple positions.
       
 98426 **
       
 98427 ** Only the last state needs to actually write to dlw->b, which would
       
 98428 ** be an improvement in the DLCollector case.
       
 98429 */
       
 98430 typedef struct PLWriter {
       
 98431   DLWriter *dlw;
       
 98432 
       
 98433   int iColumn;    /* the last column written */
       
 98434   int iPos;       /* the last position written */
       
 98435   int iOffset;    /* the last start offset written */
       
 98436 } PLWriter;
       
 98437 
       
 98438 /* TODO(shess) In the case where the parent is reading these values
       
 98439 ** from a PLReader, we could optimize to a copy if that PLReader has
       
 98440 ** the same type as pWriter.
       
 98441 */
       
 98442 static void plwAdd(PLWriter *pWriter, int iColumn, int iPos,
       
 98443                    int iStartOffset, int iEndOffset){
       
 98444   /* Worst-case space for POS_COLUMN, iColumn, iPosDelta,
       
 98445   ** iStartOffsetDelta, and iEndOffsetDelta.
       
 98446   */
       
 98447   char c[5*VARINT_MAX];
       
 98448   int n = 0;
       
 98449 
       
 98450   /* Ban plwAdd() after plwTerminate(). */
       
 98451   assert( pWriter->iPos!=-1 );
       
 98452 
       
 98453   if( pWriter->dlw->iType==DL_DOCIDS ) return;
       
 98454 
       
 98455   if( iColumn!=pWriter->iColumn ){
       
 98456     n += fts3PutVarint(c+n, POS_COLUMN);
       
 98457     n += fts3PutVarint(c+n, iColumn);
       
 98458     pWriter->iColumn = iColumn;
       
 98459     pWriter->iPos = 0;
       
 98460     pWriter->iOffset = 0;
       
 98461   }
       
 98462   assert( iPos>=pWriter->iPos );
       
 98463   n += fts3PutVarint(c+n, POS_BASE+(iPos-pWriter->iPos));
       
 98464   pWriter->iPos = iPos;
       
 98465   if( pWriter->dlw->iType==DL_POSITIONS_OFFSETS ){
       
 98466     assert( iStartOffset>=pWriter->iOffset );
       
 98467     n += fts3PutVarint(c+n, iStartOffset-pWriter->iOffset);
       
 98468     pWriter->iOffset = iStartOffset;
       
 98469     assert( iEndOffset>=iStartOffset );
       
 98470     n += fts3PutVarint(c+n, iEndOffset-iStartOffset);
       
 98471   }
       
 98472   dataBufferAppend(pWriter->dlw->b, c, n);
       
 98473 }
       
 98474 static void plwCopy(PLWriter *pWriter, PLReader *pReader){
       
 98475   plwAdd(pWriter, plrColumn(pReader), plrPosition(pReader),
       
 98476          plrStartOffset(pReader), plrEndOffset(pReader));
       
 98477 }
       
 98478 static void plwInit(PLWriter *pWriter, DLWriter *dlw, sqlite_int64 iDocid){
       
 98479   char c[VARINT_MAX];
       
 98480   int n;
       
 98481 
       
 98482   pWriter->dlw = dlw;
       
 98483 
       
 98484   /* Docids must ascend. */
       
 98485   assert( !pWriter->dlw->has_iPrevDocid || iDocid>pWriter->dlw->iPrevDocid );
       
 98486   n = fts3PutVarint(c, iDocid-pWriter->dlw->iPrevDocid);
       
 98487   dataBufferAppend(pWriter->dlw->b, c, n);
       
 98488   pWriter->dlw->iPrevDocid = iDocid;
       
 98489 #ifndef NDEBUG
       
 98490   pWriter->dlw->has_iPrevDocid = 1;
       
 98491 #endif
       
 98492 
       
 98493   pWriter->iColumn = 0;
       
 98494   pWriter->iPos = 0;
       
 98495   pWriter->iOffset = 0;
       
 98496 }
       
 98497 /* TODO(shess) Should plwDestroy() also terminate the doclist?  But
       
 98498 ** then plwDestroy() would no longer be just a destructor, it would
       
 98499 ** also be doing work, which isn't consistent with the overall idiom.
       
 98500 ** Another option would be for plwAdd() to always append any necessary
       
 98501 ** terminator, so that the output is always correct.  But that would
       
 98502 ** add incremental work to the common case with the only benefit being
       
 98503 ** API elegance.  Punt for now.
       
 98504 */
       
 98505 static void plwTerminate(PLWriter *pWriter){
       
 98506   if( pWriter->dlw->iType>DL_DOCIDS ){
       
 98507     char c[VARINT_MAX];
       
 98508     int n = fts3PutVarint(c, POS_END);
       
 98509     dataBufferAppend(pWriter->dlw->b, c, n);
       
 98510   }
       
 98511 #ifndef NDEBUG
       
 98512   /* Mark as terminated for assert in plwAdd(). */
       
 98513   pWriter->iPos = -1;
       
 98514 #endif
       
 98515 }
       
 98516 static void plwDestroy(PLWriter *pWriter){
       
 98517   SCRAMBLE(pWriter);
       
 98518 }
       
 98519 
       
 98520 /*******************************************************************/
       
 98521 /* DLCollector wraps PLWriter and DLWriter to provide a
       
 98522 ** dynamically-allocated doclist area to use during tokenization.
       
 98523 **
       
 98524 ** dlcNew - malloc up and initialize a collector.
       
 98525 ** dlcDelete - destroy a collector and all contained items.
       
 98526 ** dlcAddPos - append position and offset information.
       
 98527 ** dlcAddDoclist - add the collected doclist to the given buffer.
       
 98528 ** dlcNext - terminate the current document and open another.
       
 98529 */
       
 98530 typedef struct DLCollector {
       
 98531   DataBuffer b;
       
 98532   DLWriter dlw;
       
 98533   PLWriter plw;
       
 98534 } DLCollector;
       
 98535 
       
 98536 /* TODO(shess) This could also be done by calling plwTerminate() and
       
 98537 ** dataBufferAppend().  I tried that, expecting nominal performance
       
 98538 ** differences, but it seemed to pretty reliably be worth 1% to code
       
 98539 ** it this way.  I suspect it is the incremental malloc overhead (some
       
 98540 ** percentage of the plwTerminate() calls will cause a realloc), so
       
 98541 ** this might be worth revisiting if the DataBuffer implementation
       
 98542 ** changes.
       
 98543 */
       
 98544 static void dlcAddDoclist(DLCollector *pCollector, DataBuffer *b){
       
 98545   if( pCollector->dlw.iType>DL_DOCIDS ){
       
 98546     char c[VARINT_MAX];
       
 98547     int n = fts3PutVarint(c, POS_END);
       
 98548     dataBufferAppend2(b, pCollector->b.pData, pCollector->b.nData, c, n);
       
 98549   }else{
       
 98550     dataBufferAppend(b, pCollector->b.pData, pCollector->b.nData);
       
 98551   }
       
 98552 }
       
 98553 static void dlcNext(DLCollector *pCollector, sqlite_int64 iDocid){
       
 98554   plwTerminate(&pCollector->plw);
       
 98555   plwDestroy(&pCollector->plw);
       
 98556   plwInit(&pCollector->plw, &pCollector->dlw, iDocid);
       
 98557 }
       
 98558 static void dlcAddPos(DLCollector *pCollector, int iColumn, int iPos,
       
 98559                       int iStartOffset, int iEndOffset){
       
 98560   plwAdd(&pCollector->plw, iColumn, iPos, iStartOffset, iEndOffset);
       
 98561 }
       
 98562 
       
 98563 static DLCollector *dlcNew(sqlite_int64 iDocid, DocListType iType){
       
 98564   DLCollector *pCollector = sqlite3_malloc(sizeof(DLCollector));
       
 98565   dataBufferInit(&pCollector->b, 0);
       
 98566   dlwInit(&pCollector->dlw, iType, &pCollector->b);
       
 98567   plwInit(&pCollector->plw, &pCollector->dlw, iDocid);
       
 98568   return pCollector;
       
 98569 }
       
 98570 static void dlcDelete(DLCollector *pCollector){
       
 98571   plwDestroy(&pCollector->plw);
       
 98572   dlwDestroy(&pCollector->dlw);
       
 98573   dataBufferDestroy(&pCollector->b);
       
 98574   SCRAMBLE(pCollector);
       
 98575   sqlite3_free(pCollector);
       
 98576 }
       
 98577 
       
 98578 
       
 98579 /* Copy the doclist data of iType in pData/nData into *out, trimming
       
 98580 ** unnecessary data as we go.  Only columns matching iColumn are
       
 98581 ** copied, all columns copied if iColumn is -1.  Elements with no
       
 98582 ** matching columns are dropped.  The output is an iOutType doclist.
       
 98583 */
       
 98584 /* NOTE(shess) This code is only valid after all doclists are merged.
       
 98585 ** If this is run before merges, then doclist items which represent
       
 98586 ** deletion will be trimmed, and will thus not effect a deletion
       
 98587 ** during the merge.
       
 98588 */
       
 98589 static void docListTrim(DocListType iType, const char *pData, int nData,
       
 98590                         int iColumn, DocListType iOutType, DataBuffer *out){
       
 98591   DLReader dlReader;
       
 98592   DLWriter dlWriter;
       
 98593 
       
 98594   assert( iOutType<=iType );
       
 98595 
       
 98596   dlrInit(&dlReader, iType, pData, nData);
       
 98597   dlwInit(&dlWriter, iOutType, out);
       
 98598 
       
 98599   while( !dlrAtEnd(&dlReader) ){
       
 98600     PLReader plReader;
       
 98601     PLWriter plWriter;
       
 98602     int match = 0;
       
 98603 
       
 98604     plrInit(&plReader, &dlReader);
       
 98605 
       
 98606     while( !plrAtEnd(&plReader) ){
       
 98607       if( iColumn==-1 || plrColumn(&plReader)==iColumn ){
       
 98608         if( !match ){
       
 98609           plwInit(&plWriter, &dlWriter, dlrDocid(&dlReader));
       
 98610           match = 1;
       
 98611         }
       
 98612         plwAdd(&plWriter, plrColumn(&plReader), plrPosition(&plReader),
       
 98613                plrStartOffset(&plReader), plrEndOffset(&plReader));
       
 98614       }
       
 98615       plrStep(&plReader);
       
 98616     }
       
 98617     if( match ){
       
 98618       plwTerminate(&plWriter);
       
 98619       plwDestroy(&plWriter);
       
 98620     }
       
 98621 
       
 98622     plrDestroy(&plReader);
       
 98623     dlrStep(&dlReader);
       
 98624   }
       
 98625   dlwDestroy(&dlWriter);
       
 98626   dlrDestroy(&dlReader);
       
 98627 }
       
 98628 
       
 98629 /* Used by docListMerge() to keep doclists in the ascending order by
       
 98630 ** docid, then ascending order by age (so the newest comes first).
       
 98631 */
       
 98632 typedef struct OrderedDLReader {
       
 98633   DLReader *pReader;
       
 98634 
       
 98635   /* TODO(shess) If we assume that docListMerge pReaders is ordered by
       
 98636   ** age (which we do), then we could use pReader comparisons to break
       
 98637   ** ties.
       
 98638   */
       
 98639   int idx;
       
 98640 } OrderedDLReader;
       
 98641 
       
 98642 /* Order eof to end, then by docid asc, idx desc. */
       
 98643 static int orderedDLReaderCmp(OrderedDLReader *r1, OrderedDLReader *r2){
       
 98644   if( dlrAtEnd(r1->pReader) ){
       
 98645     if( dlrAtEnd(r2->pReader) ) return 0;  /* Both atEnd(). */
       
 98646     return 1;                              /* Only r1 atEnd(). */
       
 98647   }
       
 98648   if( dlrAtEnd(r2->pReader) ) return -1;   /* Only r2 atEnd(). */
       
 98649 
       
 98650   if( dlrDocid(r1->pReader)<dlrDocid(r2->pReader) ) return -1;
       
 98651   if( dlrDocid(r1->pReader)>dlrDocid(r2->pReader) ) return 1;
       
 98652 
       
 98653   /* Descending on idx. */
       
 98654   return r2->idx-r1->idx;
       
 98655 }
       
 98656 
       
 98657 /* Bubble p[0] to appropriate place in p[1..n-1].  Assumes that
       
 98658 ** p[1..n-1] is already sorted.
       
 98659 */
       
 98660 /* TODO(shess) Is this frequent enough to warrant a binary search?
       
 98661 ** Before implementing that, instrument the code to check.  In most
       
 98662 ** current usage, I expect that p[0] will be less than p[1] a very
       
 98663 ** high proportion of the time.
       
 98664 */
       
 98665 static void orderedDLReaderReorder(OrderedDLReader *p, int n){
       
 98666   while( n>1 && orderedDLReaderCmp(p, p+1)>0 ){
       
 98667     OrderedDLReader tmp = p[0];
       
 98668     p[0] = p[1];
       
 98669     p[1] = tmp;
       
 98670     n--;
       
 98671     p++;
       
 98672   }
       
 98673 }
       
 98674 
       
 98675 /* Given an array of doclist readers, merge their doclist elements
       
 98676 ** into out in sorted order (by docid), dropping elements from older
       
 98677 ** readers when there is a duplicate docid.  pReaders is assumed to be
       
 98678 ** ordered by age, oldest first.
       
 98679 */
       
 98680 /* TODO(shess) nReaders must be <= MERGE_COUNT.  This should probably
       
 98681 ** be fixed.
       
 98682 */
       
 98683 static void docListMerge(DataBuffer *out,
       
 98684                          DLReader *pReaders, int nReaders){
       
 98685   OrderedDLReader readers[MERGE_COUNT];
       
 98686   DLWriter writer;
       
 98687   int i, n;
       
 98688   const char *pStart = 0;
       
 98689   int nStart = 0;
       
 98690   sqlite_int64 iFirstDocid = 0, iLastDocid = 0;
       
 98691 
       
 98692   assert( nReaders>0 );
       
 98693   if( nReaders==1 ){
       
 98694     dataBufferAppend(out, dlrDocData(pReaders), dlrAllDataBytes(pReaders));
       
 98695     return;
       
 98696   }
       
 98697 
       
 98698   assert( nReaders<=MERGE_COUNT );
       
 98699   n = 0;
       
 98700   for(i=0; i<nReaders; i++){
       
 98701     assert( pReaders[i].iType==pReaders[0].iType );
       
 98702     readers[i].pReader = pReaders+i;
       
 98703     readers[i].idx = i;
       
 98704     n += dlrAllDataBytes(&pReaders[i]);
       
 98705   }
       
 98706   /* Conservatively size output to sum of inputs.  Output should end
       
 98707   ** up strictly smaller than input.
       
 98708   */
       
 98709   dataBufferExpand(out, n);
       
 98710 
       
 98711   /* Get the readers into sorted order. */
       
 98712   while( i-->0 ){
       
 98713     orderedDLReaderReorder(readers+i, nReaders-i);
       
 98714   }
       
 98715 
       
 98716   dlwInit(&writer, pReaders[0].iType, out);
       
 98717   while( !dlrAtEnd(readers[0].pReader) ){
       
 98718     sqlite_int64 iDocid = dlrDocid(readers[0].pReader);
       
 98719 
       
 98720     /* If this is a continuation of the current buffer to copy, extend
       
 98721     ** that buffer.  memcpy() seems to be more efficient if it has a
       
 98722     ** lots of data to copy.
       
 98723     */
       
 98724     if( dlrDocData(readers[0].pReader)==pStart+nStart ){
       
 98725       nStart += dlrDocDataBytes(readers[0].pReader);
       
 98726     }else{
       
 98727       if( pStart!=0 ){
       
 98728         dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid);
       
 98729       }
       
 98730       pStart = dlrDocData(readers[0].pReader);
       
 98731       nStart = dlrDocDataBytes(readers[0].pReader);
       
 98732       iFirstDocid = iDocid;
       
 98733     }
       
 98734     iLastDocid = iDocid;
       
 98735     dlrStep(readers[0].pReader);
       
 98736 
       
 98737     /* Drop all of the older elements with the same docid. */
       
 98738     for(i=1; i<nReaders &&
       
 98739              !dlrAtEnd(readers[i].pReader) &&
       
 98740              dlrDocid(readers[i].pReader)==iDocid; i++){
       
 98741       dlrStep(readers[i].pReader);
       
 98742     }
       
 98743 
       
 98744     /* Get the readers back into order. */
       
 98745     while( i-->0 ){
       
 98746       orderedDLReaderReorder(readers+i, nReaders-i);
       
 98747     }
       
 98748   }
       
 98749 
       
 98750   /* Copy over any remaining elements. */
       
 98751   if( nStart>0 ) dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid);
       
 98752   dlwDestroy(&writer);
       
 98753 }
       
 98754 
       
 98755 /* Helper function for posListUnion().  Compares the current position
       
 98756 ** between left and right, returning as standard C idiom of <0 if
       
 98757 ** left<right, >0 if left>right, and 0 if left==right.  "End" always
       
 98758 ** compares greater.
       
 98759 */
       
 98760 static int posListCmp(PLReader *pLeft, PLReader *pRight){
       
 98761   assert( pLeft->iType==pRight->iType );
       
 98762   if( pLeft->iType==DL_DOCIDS ) return 0;
       
 98763 
       
 98764   if( plrAtEnd(pLeft) ) return plrAtEnd(pRight) ? 0 : 1;
       
 98765   if( plrAtEnd(pRight) ) return -1;
       
 98766 
       
 98767   if( plrColumn(pLeft)<plrColumn(pRight) ) return -1;
       
 98768   if( plrColumn(pLeft)>plrColumn(pRight) ) return 1;
       
 98769 
       
 98770   if( plrPosition(pLeft)<plrPosition(pRight) ) return -1;
       
 98771   if( plrPosition(pLeft)>plrPosition(pRight) ) return 1;
       
 98772   if( pLeft->iType==DL_POSITIONS ) return 0;
       
 98773 
       
 98774   if( plrStartOffset(pLeft)<plrStartOffset(pRight) ) return -1;
       
 98775   if( plrStartOffset(pLeft)>plrStartOffset(pRight) ) return 1;
       
 98776 
       
 98777   if( plrEndOffset(pLeft)<plrEndOffset(pRight) ) return -1;
       
 98778   if( plrEndOffset(pLeft)>plrEndOffset(pRight) ) return 1;
       
 98779 
       
 98780   return 0;
       
 98781 }
       
 98782 
       
 98783 /* Write the union of position lists in pLeft and pRight to pOut.
       
 98784 ** "Union" in this case meaning "All unique position tuples".  Should
       
 98785 ** work with any doclist type, though both inputs and the output
       
 98786 ** should be the same type.
       
 98787 */
       
 98788 static void posListUnion(DLReader *pLeft, DLReader *pRight, DLWriter *pOut){
       
 98789   PLReader left, right;
       
 98790   PLWriter writer;
       
 98791 
       
 98792   assert( dlrDocid(pLeft)==dlrDocid(pRight) );
       
 98793   assert( pLeft->iType==pRight->iType );
       
 98794   assert( pLeft->iType==pOut->iType );
       
 98795 
       
 98796   plrInit(&left, pLeft);
       
 98797   plrInit(&right, pRight);
       
 98798   plwInit(&writer, pOut, dlrDocid(pLeft));
       
 98799 
       
 98800   while( !plrAtEnd(&left) || !plrAtEnd(&right) ){
       
 98801     int c = posListCmp(&left, &right);
       
 98802     if( c<0 ){
       
 98803       plwCopy(&writer, &left);
       
 98804       plrStep(&left);
       
 98805     }else if( c>0 ){
       
 98806       plwCopy(&writer, &right);
       
 98807       plrStep(&right);
       
 98808     }else{
       
 98809       plwCopy(&writer, &left);
       
 98810       plrStep(&left);
       
 98811       plrStep(&right);
       
 98812     }
       
 98813   }
       
 98814 
       
 98815   plwTerminate(&writer);
       
 98816   plwDestroy(&writer);
       
 98817   plrDestroy(&left);
       
 98818   plrDestroy(&right);
       
 98819 }
       
 98820 
       
 98821 /* Write the union of doclists in pLeft and pRight to pOut.  For
       
 98822 ** docids in common between the inputs, the union of the position
       
 98823 ** lists is written.  Inputs and outputs are always type DL_DEFAULT.
       
 98824 */
       
 98825 static void docListUnion(
       
 98826   const char *pLeft, int nLeft,
       
 98827   const char *pRight, int nRight,
       
 98828   DataBuffer *pOut      /* Write the combined doclist here */
       
 98829 ){
       
 98830   DLReader left, right;
       
 98831   DLWriter writer;
       
 98832 
       
 98833   if( nLeft==0 ){
       
 98834     if( nRight!=0) dataBufferAppend(pOut, pRight, nRight);
       
 98835     return;
       
 98836   }
       
 98837   if( nRight==0 ){
       
 98838     dataBufferAppend(pOut, pLeft, nLeft);
       
 98839     return;
       
 98840   }
       
 98841 
       
 98842   dlrInit(&left, DL_DEFAULT, pLeft, nLeft);
       
 98843   dlrInit(&right, DL_DEFAULT, pRight, nRight);
       
 98844   dlwInit(&writer, DL_DEFAULT, pOut);
       
 98845 
       
 98846   while( !dlrAtEnd(&left) || !dlrAtEnd(&right) ){
       
 98847     if( dlrAtEnd(&right) ){
       
 98848       dlwCopy(&writer, &left);
       
 98849       dlrStep(&left);
       
 98850     }else if( dlrAtEnd(&left) ){
       
 98851       dlwCopy(&writer, &right);
       
 98852       dlrStep(&right);
       
 98853     }else if( dlrDocid(&left)<dlrDocid(&right) ){
       
 98854       dlwCopy(&writer, &left);
       
 98855       dlrStep(&left);
       
 98856     }else if( dlrDocid(&left)>dlrDocid(&right) ){
       
 98857       dlwCopy(&writer, &right);
       
 98858       dlrStep(&right);
       
 98859     }else{
       
 98860       posListUnion(&left, &right, &writer);
       
 98861       dlrStep(&left);
       
 98862       dlrStep(&right);
       
 98863     }
       
 98864   }
       
 98865 
       
 98866   dlrDestroy(&left);
       
 98867   dlrDestroy(&right);
       
 98868   dlwDestroy(&writer);
       
 98869 }
       
 98870 
       
 98871 /* 
       
 98872 ** This function is used as part of the implementation of phrase and
       
 98873 ** NEAR matching.
       
 98874 **
       
 98875 ** pLeft and pRight are DLReaders positioned to the same docid in
       
 98876 ** lists of type DL_POSITION. This function writes an entry to the
       
 98877 ** DLWriter pOut for each position in pRight that is less than
       
 98878 ** (nNear+1) greater (but not equal to or smaller) than a position 
       
 98879 ** in pLeft. For example, if nNear is 0, and the positions contained
       
 98880 ** by pLeft and pRight are:
       
 98881 **
       
 98882 **    pLeft:  5 10 15 20
       
 98883 **    pRight: 6  9 17 21
       
 98884 **
       
 98885 ** then the docid is added to pOut. If pOut is of type DL_POSITIONS,
       
 98886 ** then a positionids "6" and "21" are also added to pOut.
       
 98887 **
       
 98888 ** If boolean argument isSaveLeft is true, then positionids are copied
       
 98889 ** from pLeft instead of pRight. In the example above, the positions "5"
       
 98890 ** and "20" would be added instead of "6" and "21".
       
 98891 */
       
 98892 static void posListPhraseMerge(
       
 98893   DLReader *pLeft, 
       
 98894   DLReader *pRight,
       
 98895   int nNear,
       
 98896   int isSaveLeft,
       
 98897   DLWriter *pOut
       
 98898 ){
       
 98899   PLReader left, right;
       
 98900   PLWriter writer;
       
 98901   int match = 0;
       
 98902 
       
 98903   assert( dlrDocid(pLeft)==dlrDocid(pRight) );
       
 98904   assert( pOut->iType!=DL_POSITIONS_OFFSETS );
       
 98905 
       
 98906   plrInit(&left, pLeft);
       
 98907   plrInit(&right, pRight);
       
 98908 
       
 98909   while( !plrAtEnd(&left) && !plrAtEnd(&right) ){
       
 98910     if( plrColumn(&left)<plrColumn(&right) ){
       
 98911       plrStep(&left);
       
 98912     }else if( plrColumn(&left)>plrColumn(&right) ){
       
 98913       plrStep(&right);
       
 98914     }else if( plrPosition(&left)>=plrPosition(&right) ){
       
 98915       plrStep(&right);
       
 98916     }else{
       
 98917       if( (plrPosition(&right)-plrPosition(&left))<=(nNear+1) ){
       
 98918         if( !match ){
       
 98919           plwInit(&writer, pOut, dlrDocid(pLeft));
       
 98920           match = 1;
       
 98921         }
       
 98922         if( !isSaveLeft ){
       
 98923           plwAdd(&writer, plrColumn(&right), plrPosition(&right), 0, 0);
       
 98924         }else{
       
 98925           plwAdd(&writer, plrColumn(&left), plrPosition(&left), 0, 0);
       
 98926         }
       
 98927         plrStep(&right);
       
 98928       }else{
       
 98929         plrStep(&left);
       
 98930       }
       
 98931     }
       
 98932   }
       
 98933 
       
 98934   if( match ){
       
 98935     plwTerminate(&writer);
       
 98936     plwDestroy(&writer);
       
 98937   }
       
 98938 
       
 98939   plrDestroy(&left);
       
 98940   plrDestroy(&right);
       
 98941 }
       
 98942 
       
 98943 /*
       
 98944 ** Compare the values pointed to by the PLReaders passed as arguments. 
       
 98945 ** Return -1 if the value pointed to by pLeft is considered less than
       
 98946 ** the value pointed to by pRight, +1 if it is considered greater
       
 98947 ** than it, or 0 if it is equal. i.e.
       
 98948 **
       
 98949 **     (*pLeft - *pRight)
       
 98950 **
       
 98951 ** A PLReader that is in the EOF condition is considered greater than
       
 98952 ** any other. If neither argument is in EOF state, the return value of
       
 98953 ** plrColumn() is used. If the plrColumn() values are equal, the
       
 98954 ** comparison is on the basis of plrPosition().
       
 98955 */
       
 98956 static int plrCompare(PLReader *pLeft, PLReader *pRight){
       
 98957   assert(!plrAtEnd(pLeft) || !plrAtEnd(pRight));
       
 98958 
       
 98959   if( plrAtEnd(pRight) || plrAtEnd(pLeft) ){
       
 98960     return (plrAtEnd(pRight) ? -1 : 1);
       
 98961   }
       
 98962   if( plrColumn(pLeft)!=plrColumn(pRight) ){
       
 98963     return ((plrColumn(pLeft)<plrColumn(pRight)) ? -1 : 1);
       
 98964   }
       
 98965   if( plrPosition(pLeft)!=plrPosition(pRight) ){
       
 98966     return ((plrPosition(pLeft)<plrPosition(pRight)) ? -1 : 1);
       
 98967   }
       
 98968   return 0;
       
 98969 }
       
 98970 
       
 98971 /* We have two doclists with positions:  pLeft and pRight. Depending
       
 98972 ** on the value of the nNear parameter, perform either a phrase
       
 98973 ** intersection (if nNear==0) or a NEAR intersection (if nNear>0)
       
 98974 ** and write the results into pOut.
       
 98975 **
       
 98976 ** A phrase intersection means that two documents only match
       
 98977 ** if pLeft.iPos+1==pRight.iPos.
       
 98978 **
       
 98979 ** A NEAR intersection means that two documents only match if 
       
 98980 ** (abs(pLeft.iPos-pRight.iPos)<nNear).
       
 98981 **
       
 98982 ** If a NEAR intersection is requested, then the nPhrase argument should
       
 98983 ** be passed the number of tokens in the two operands to the NEAR operator
       
 98984 ** combined. For example:
       
 98985 **
       
 98986 **       Query syntax               nPhrase
       
 98987 **      ------------------------------------
       
 98988 **       "A B C" NEAR "D E"         5
       
 98989 **       A NEAR B                   2
       
 98990 **
       
 98991 ** iType controls the type of data written to pOut.  If iType is
       
 98992 ** DL_POSITIONS, the positions are those from pRight.
       
 98993 */
       
 98994 static void docListPhraseMerge(
       
 98995   const char *pLeft, int nLeft,
       
 98996   const char *pRight, int nRight,
       
 98997   int nNear,            /* 0 for a phrase merge, non-zero for a NEAR merge */
       
 98998   int nPhrase,          /* Number of tokens in left+right operands to NEAR */
       
 98999   DocListType iType,    /* Type of doclist to write to pOut */
       
 99000   DataBuffer *pOut      /* Write the combined doclist here */
       
 99001 ){
       
 99002   DLReader left, right;
       
 99003   DLWriter writer;
       
 99004 
       
 99005   if( nLeft==0 || nRight==0 ) return;
       
 99006 
       
 99007   assert( iType!=DL_POSITIONS_OFFSETS );
       
 99008 
       
 99009   dlrInit(&left, DL_POSITIONS, pLeft, nLeft);
       
 99010   dlrInit(&right, DL_POSITIONS, pRight, nRight);
       
 99011   dlwInit(&writer, iType, pOut);
       
 99012 
       
 99013   while( !dlrAtEnd(&left) && !dlrAtEnd(&right) ){
       
 99014     if( dlrDocid(&left)<dlrDocid(&right) ){
       
 99015       dlrStep(&left);
       
 99016     }else if( dlrDocid(&right)<dlrDocid(&left) ){
       
 99017       dlrStep(&right);
       
 99018     }else{
       
 99019       if( nNear==0 ){
       
 99020         posListPhraseMerge(&left, &right, 0, 0, &writer);
       
 99021       }else{
       
 99022         /* This case occurs when two terms (simple terms or phrases) are
       
 99023          * connected by a NEAR operator, span (nNear+1). i.e.
       
 99024          *
       
 99025          *     '"terrible company" NEAR widget'
       
 99026          */
       
 99027         DataBuffer one = {0, 0, 0};
       
 99028         DataBuffer two = {0, 0, 0};
       
 99029 
       
 99030         DLWriter dlwriter2;
       
 99031         DLReader dr1 = {0, 0, 0, 0, 0}; 
       
 99032         DLReader dr2 = {0, 0, 0, 0, 0};
       
 99033 
       
 99034         dlwInit(&dlwriter2, iType, &one);
       
 99035         posListPhraseMerge(&right, &left, nNear-3+nPhrase, 1, &dlwriter2);
       
 99036         dlwInit(&dlwriter2, iType, &two);
       
 99037         posListPhraseMerge(&left, &right, nNear-1, 0, &dlwriter2);
       
 99038 
       
 99039         if( one.nData) dlrInit(&dr1, iType, one.pData, one.nData);
       
 99040         if( two.nData) dlrInit(&dr2, iType, two.pData, two.nData);
       
 99041 
       
 99042         if( !dlrAtEnd(&dr1) || !dlrAtEnd(&dr2) ){
       
 99043           PLReader pr1 = {0};
       
 99044           PLReader pr2 = {0};
       
 99045 
       
 99046           PLWriter plwriter;
       
 99047           plwInit(&plwriter, &writer, dlrDocid(dlrAtEnd(&dr1)?&dr2:&dr1));
       
 99048 
       
 99049           if( one.nData ) plrInit(&pr1, &dr1);
       
 99050           if( two.nData ) plrInit(&pr2, &dr2);
       
 99051           while( !plrAtEnd(&pr1) || !plrAtEnd(&pr2) ){
       
 99052             int iCompare = plrCompare(&pr1, &pr2);
       
 99053             switch( iCompare ){
       
 99054               case -1:
       
 99055                 plwCopy(&plwriter, &pr1);
       
 99056                 plrStep(&pr1);
       
 99057                 break;
       
 99058               case 1:
       
 99059                 plwCopy(&plwriter, &pr2);
       
 99060                 plrStep(&pr2);
       
 99061                 break;
       
 99062               case 0:
       
 99063                 plwCopy(&plwriter, &pr1);
       
 99064                 plrStep(&pr1);
       
 99065                 plrStep(&pr2);
       
 99066                 break;
       
 99067             }
       
 99068           }
       
 99069           plwTerminate(&plwriter);
       
 99070         }
       
 99071         dataBufferDestroy(&one);
       
 99072         dataBufferDestroy(&two);
       
 99073       }
       
 99074       dlrStep(&left);
       
 99075       dlrStep(&right);
       
 99076     }
       
 99077   }
       
 99078 
       
 99079   dlrDestroy(&left);
       
 99080   dlrDestroy(&right);
       
 99081   dlwDestroy(&writer);
       
 99082 }
       
 99083 
       
 99084 /* We have two DL_DOCIDS doclists:  pLeft and pRight.
       
 99085 ** Write the intersection of these two doclists into pOut as a
       
 99086 ** DL_DOCIDS doclist.
       
 99087 */
       
 99088 static void docListAndMerge(
       
 99089   const char *pLeft, int nLeft,
       
 99090   const char *pRight, int nRight,
       
 99091   DataBuffer *pOut      /* Write the combined doclist here */
       
 99092 ){
       
 99093   DLReader left, right;
       
 99094   DLWriter writer;
       
 99095 
       
 99096   if( nLeft==0 || nRight==0 ) return;
       
 99097 
       
 99098   dlrInit(&left, DL_DOCIDS, pLeft, nLeft);
       
 99099   dlrInit(&right, DL_DOCIDS, pRight, nRight);
       
 99100   dlwInit(&writer, DL_DOCIDS, pOut);
       
 99101 
       
 99102   while( !dlrAtEnd(&left) && !dlrAtEnd(&right) ){
       
 99103     if( dlrDocid(&left)<dlrDocid(&right) ){
       
 99104       dlrStep(&left);
       
 99105     }else if( dlrDocid(&right)<dlrDocid(&left) ){
       
 99106       dlrStep(&right);
       
 99107     }else{
       
 99108       dlwAdd(&writer, dlrDocid(&left));
       
 99109       dlrStep(&left);
       
 99110       dlrStep(&right);
       
 99111     }
       
 99112   }
       
 99113 
       
 99114   dlrDestroy(&left);
       
 99115   dlrDestroy(&right);
       
 99116   dlwDestroy(&writer);
       
 99117 }
       
 99118 
       
 99119 /* We have two DL_DOCIDS doclists:  pLeft and pRight.
       
 99120 ** Write the union of these two doclists into pOut as a
       
 99121 ** DL_DOCIDS doclist.
       
 99122 */
       
 99123 static void docListOrMerge(
       
 99124   const char *pLeft, int nLeft,
       
 99125   const char *pRight, int nRight,
       
 99126   DataBuffer *pOut      /* Write the combined doclist here */
       
 99127 ){
       
 99128   DLReader left, right;
       
 99129   DLWriter writer;
       
 99130 
       
 99131   if( nLeft==0 ){
       
 99132     if( nRight!=0 ) dataBufferAppend(pOut, pRight, nRight);
       
 99133     return;
       
 99134   }
       
 99135   if( nRight==0 ){
       
 99136     dataBufferAppend(pOut, pLeft, nLeft);
       
 99137     return;
       
 99138   }
       
 99139 
       
 99140   dlrInit(&left, DL_DOCIDS, pLeft, nLeft);
       
 99141   dlrInit(&right, DL_DOCIDS, pRight, nRight);
       
 99142   dlwInit(&writer, DL_DOCIDS, pOut);
       
 99143 
       
 99144   while( !dlrAtEnd(&left) || !dlrAtEnd(&right) ){
       
 99145     if( dlrAtEnd(&right) ){
       
 99146       dlwAdd(&writer, dlrDocid(&left));
       
 99147       dlrStep(&left);
       
 99148     }else if( dlrAtEnd(&left) ){
       
 99149       dlwAdd(&writer, dlrDocid(&right));
       
 99150       dlrStep(&right);
       
 99151     }else if( dlrDocid(&left)<dlrDocid(&right) ){
       
 99152       dlwAdd(&writer, dlrDocid(&left));
       
 99153       dlrStep(&left);
       
 99154     }else if( dlrDocid(&right)<dlrDocid(&left) ){
       
 99155       dlwAdd(&writer, dlrDocid(&right));
       
 99156       dlrStep(&right);
       
 99157     }else{
       
 99158       dlwAdd(&writer, dlrDocid(&left));
       
 99159       dlrStep(&left);
       
 99160       dlrStep(&right);
       
 99161     }
       
 99162   }
       
 99163 
       
 99164   dlrDestroy(&left);
       
 99165   dlrDestroy(&right);
       
 99166   dlwDestroy(&writer);
       
 99167 }
       
 99168 
       
 99169 /* We have two DL_DOCIDS doclists:  pLeft and pRight.
       
 99170 ** Write into pOut as DL_DOCIDS doclist containing all documents that
       
 99171 ** occur in pLeft but not in pRight.
       
 99172 */
       
 99173 static void docListExceptMerge(
       
 99174   const char *pLeft, int nLeft,
       
 99175   const char *pRight, int nRight,
       
 99176   DataBuffer *pOut      /* Write the combined doclist here */
       
 99177 ){
       
 99178   DLReader left, right;
       
 99179   DLWriter writer;
       
 99180 
       
 99181   if( nLeft==0 ) return;
       
 99182   if( nRight==0 ){
       
 99183     dataBufferAppend(pOut, pLeft, nLeft);
       
 99184     return;
       
 99185   }
       
 99186 
       
 99187   dlrInit(&left, DL_DOCIDS, pLeft, nLeft);
       
 99188   dlrInit(&right, DL_DOCIDS, pRight, nRight);
       
 99189   dlwInit(&writer, DL_DOCIDS, pOut);
       
 99190 
       
 99191   while( !dlrAtEnd(&left) ){
       
 99192     while( !dlrAtEnd(&right) && dlrDocid(&right)<dlrDocid(&left) ){
       
 99193       dlrStep(&right);
       
 99194     }
       
 99195     if( dlrAtEnd(&right) || dlrDocid(&left)<dlrDocid(&right) ){
       
 99196       dlwAdd(&writer, dlrDocid(&left));
       
 99197     }
       
 99198     dlrStep(&left);
       
 99199   }
       
 99200 
       
 99201   dlrDestroy(&left);
       
 99202   dlrDestroy(&right);
       
 99203   dlwDestroy(&writer);
       
 99204 }
       
 99205 
       
 99206 static char *string_dup_n(const char *s, int n){
       
 99207   char *str = sqlite3_malloc(n + 1);
       
 99208   memcpy(str, s, n);
       
 99209   str[n] = '\0';
       
 99210   return str;
       
 99211 }
       
 99212 
       
 99213 /* Duplicate a string; the caller must free() the returned string.
       
 99214  * (We don't use strdup() since it is not part of the standard C library and
       
 99215  * may not be available everywhere.) */
       
 99216 static char *string_dup(const char *s){
       
 99217   return string_dup_n(s, strlen(s));
       
 99218 }
       
 99219 
       
 99220 /* Format a string, replacing each occurrence of the % character with
       
 99221  * zDb.zName.  This may be more convenient than sqlite_mprintf()
       
 99222  * when one string is used repeatedly in a format string.
       
 99223  * The caller must free() the returned string. */
       
 99224 static char *string_format(const char *zFormat,
       
 99225                            const char *zDb, const char *zName){
       
 99226   const char *p;
       
 99227   size_t len = 0;
       
 99228   size_t nDb = strlen(zDb);
       
 99229   size_t nName = strlen(zName);
       
 99230   size_t nFullTableName = nDb+1+nName;
       
 99231   char *result;
       
 99232   char *r;
       
 99233 
       
 99234   /* first compute length needed */
       
 99235   for(p = zFormat ; *p ; ++p){
       
 99236     len += (*p=='%' ? nFullTableName : 1);
       
 99237   }
       
 99238   len += 1;  /* for null terminator */
       
 99239 
       
 99240   r = result = sqlite3_malloc(len);
       
 99241   for(p = zFormat; *p; ++p){
       
 99242     if( *p=='%' ){
       
 99243       memcpy(r, zDb, nDb);
       
 99244       r += nDb;
       
 99245       *r++ = '.';
       
 99246       memcpy(r, zName, nName);
       
 99247       r += nName;
       
 99248     } else {
       
 99249       *r++ = *p;
       
 99250     }
       
 99251   }
       
 99252   *r++ = '\0';
       
 99253   assert( r == result + len );
       
 99254   return result;
       
 99255 }
       
 99256 
       
 99257 static int sql_exec(sqlite3 *db, const char *zDb, const char *zName,
       
 99258                     const char *zFormat){
       
 99259   char *zCommand = string_format(zFormat, zDb, zName);
       
 99260   int rc;
       
 99261   FTSTRACE(("FTS3 sql: %s\n", zCommand));
       
 99262   rc = sqlite3_exec(db, zCommand, NULL, 0, NULL);
       
 99263   sqlite3_free(zCommand);
       
 99264   return rc;
       
 99265 }
       
 99266 
       
 99267 static int sql_prepare(sqlite3 *db, const char *zDb, const char *zName,
       
 99268                        sqlite3_stmt **ppStmt, const char *zFormat){
       
 99269   char *zCommand = string_format(zFormat, zDb, zName);
       
 99270   int rc;
       
 99271   FTSTRACE(("FTS3 prepare: %s\n", zCommand));
       
 99272   rc = sqlite3_prepare_v2(db, zCommand, -1, ppStmt, NULL);
       
 99273   sqlite3_free(zCommand);
       
 99274   return rc;
       
 99275 }
       
 99276 
       
 99277 /* end utility functions */
       
 99278 
       
 99279 /* Forward reference */
       
 99280 typedef struct fulltext_vtab fulltext_vtab;
       
 99281 
       
 99282 /*
       
 99283 ** An instance of the following structure keeps track of generated
       
 99284 ** matching-word offset information and snippets.
       
 99285 */
       
 99286 typedef struct Snippet {
       
 99287   int nMatch;     /* Total number of matches */
       
 99288   int nAlloc;     /* Space allocated for aMatch[] */
       
 99289   struct snippetMatch { /* One entry for each matching term */
       
 99290     char snStatus;       /* Status flag for use while constructing snippets */
       
 99291     short int iCol;      /* The column that contains the match */
       
 99292     short int iTerm;     /* The index in Query.pTerms[] of the matching term */
       
 99293     int iToken;          /* The index of the matching document token */
       
 99294     short int nByte;     /* Number of bytes in the term */
       
 99295     int iStart;          /* The offset to the first character of the term */
       
 99296   } *aMatch;      /* Points to space obtained from malloc */
       
 99297   char *zOffset;  /* Text rendering of aMatch[] */
       
 99298   int nOffset;    /* strlen(zOffset) */
       
 99299   char *zSnippet; /* Snippet text */
       
 99300   int nSnippet;   /* strlen(zSnippet) */
       
 99301 } Snippet;
       
 99302 
       
 99303 
       
 99304 typedef enum QueryType {
       
 99305   QUERY_GENERIC,   /* table scan */
       
 99306   QUERY_DOCID,     /* lookup by docid */
       
 99307   QUERY_FULLTEXT   /* QUERY_FULLTEXT + [i] is a full-text search for column i*/
       
 99308 } QueryType;
       
 99309 
       
 99310 typedef enum fulltext_statement {
       
 99311   CONTENT_INSERT_STMT,
       
 99312   CONTENT_SELECT_STMT,
       
 99313   CONTENT_UPDATE_STMT,
       
 99314   CONTENT_DELETE_STMT,
       
 99315   CONTENT_EXISTS_STMT,
       
 99316 
       
 99317   BLOCK_INSERT_STMT,
       
 99318   BLOCK_SELECT_STMT,
       
 99319   BLOCK_DELETE_STMT,
       
 99320   BLOCK_DELETE_ALL_STMT,
       
 99321 
       
 99322   SEGDIR_MAX_INDEX_STMT,
       
 99323   SEGDIR_SET_STMT,
       
 99324   SEGDIR_SELECT_LEVEL_STMT,
       
 99325   SEGDIR_SPAN_STMT,
       
 99326   SEGDIR_DELETE_STMT,
       
 99327   SEGDIR_SELECT_SEGMENT_STMT,
       
 99328   SEGDIR_SELECT_ALL_STMT,
       
 99329   SEGDIR_DELETE_ALL_STMT,
       
 99330   SEGDIR_COUNT_STMT,
       
 99331 
       
 99332   MAX_STMT                     /* Always at end! */
       
 99333 } fulltext_statement;
       
 99334 
       
 99335 /* These must exactly match the enum above. */
       
 99336 /* TODO(shess): Is there some risk that a statement will be used in two
       
 99337 ** cursors at once, e.g.  if a query joins a virtual table to itself?
       
 99338 ** If so perhaps we should move some of these to the cursor object.
       
 99339 */
       
 99340 static const char *const fulltext_zStatement[MAX_STMT] = {
       
 99341   /* CONTENT_INSERT */ NULL,  /* generated in contentInsertStatement() */
       
 99342   /* CONTENT_SELECT */ NULL,  /* generated in contentSelectStatement() */
       
 99343   /* CONTENT_UPDATE */ NULL,  /* generated in contentUpdateStatement() */
       
 99344   /* CONTENT_DELETE */ "delete from %_content where docid = ?",
       
 99345   /* CONTENT_EXISTS */ "select docid from %_content limit 1",
       
 99346 
       
 99347   /* BLOCK_INSERT */
       
 99348   "insert into %_segments (blockid, block) values (null, ?)",
       
 99349   /* BLOCK_SELECT */ "select block from %_segments where blockid = ?",
       
 99350   /* BLOCK_DELETE */ "delete from %_segments where blockid between ? and ?",
       
 99351   /* BLOCK_DELETE_ALL */ "delete from %_segments",
       
 99352 
       
 99353   /* SEGDIR_MAX_INDEX */ "select max(idx) from %_segdir where level = ?",
       
 99354   /* SEGDIR_SET */ "insert into %_segdir values (?, ?, ?, ?, ?, ?)",
       
 99355   /* SEGDIR_SELECT_LEVEL */
       
 99356   "select start_block, leaves_end_block, root from %_segdir "
       
 99357   " where level = ? order by idx",
       
 99358   /* SEGDIR_SPAN */
       
 99359   "select min(start_block), max(end_block) from %_segdir "
       
 99360   " where level = ? and start_block <> 0",
       
 99361   /* SEGDIR_DELETE */ "delete from %_segdir where level = ?",
       
 99362 
       
 99363   /* NOTE(shess): The first three results of the following two
       
 99364   ** statements must match.
       
 99365   */
       
 99366   /* SEGDIR_SELECT_SEGMENT */
       
 99367   "select start_block, leaves_end_block, root from %_segdir "
       
 99368   " where level = ? and idx = ?",
       
 99369   /* SEGDIR_SELECT_ALL */
       
 99370   "select start_block, leaves_end_block, root from %_segdir "
       
 99371   " order by level desc, idx asc",
       
 99372   /* SEGDIR_DELETE_ALL */ "delete from %_segdir",
       
 99373   /* SEGDIR_COUNT */ "select count(*), ifnull(max(level),0) from %_segdir",
       
 99374 };
       
 99375 
       
 99376 /*
       
 99377 ** A connection to a fulltext index is an instance of the following
       
 99378 ** structure.  The xCreate and xConnect methods create an instance
       
 99379 ** of this structure and xDestroy and xDisconnect free that instance.
       
 99380 ** All other methods receive a pointer to the structure as one of their
       
 99381 ** arguments.
       
 99382 */
       
 99383 struct fulltext_vtab {
       
 99384   sqlite3_vtab base;               /* Base class used by SQLite core */
       
 99385   sqlite3 *db;                     /* The database connection */
       
 99386   const char *zDb;                 /* logical database name */
       
 99387   const char *zName;               /* virtual table name */
       
 99388   int nColumn;                     /* number of columns in virtual table */
       
 99389   char **azColumn;                 /* column names.  malloced */
       
 99390   char **azContentColumn;          /* column names in content table; malloced */
       
 99391   sqlite3_tokenizer *pTokenizer;   /* tokenizer for inserts and queries */
       
 99392 
       
 99393   /* Precompiled statements which we keep as long as the table is
       
 99394   ** open.
       
 99395   */
       
 99396   sqlite3_stmt *pFulltextStatements[MAX_STMT];
       
 99397 
       
 99398   /* Precompiled statements used for segment merges.  We run a
       
 99399   ** separate select across the leaf level of each tree being merged.
       
 99400   */
       
 99401   sqlite3_stmt *pLeafSelectStmts[MERGE_COUNT];
       
 99402   /* The statement used to prepare pLeafSelectStmts. */
       
 99403 #define LEAF_SELECT \
       
 99404   "select block from %_segments where blockid between ? and ? order by blockid"
       
 99405 
       
 99406   /* These buffer pending index updates during transactions.
       
 99407   ** nPendingData estimates the memory size of the pending data.  It
       
 99408   ** doesn't include the hash-bucket overhead, nor any malloc
       
 99409   ** overhead.  When nPendingData exceeds kPendingThreshold, the
       
 99410   ** buffer is flushed even before the transaction closes.
       
 99411   ** pendingTerms stores the data, and is only valid when nPendingData
       
 99412   ** is >=0 (nPendingData<0 means pendingTerms has not been
       
 99413   ** initialized).  iPrevDocid is the last docid written, used to make
       
 99414   ** certain we're inserting in sorted order.
       
 99415   */
       
 99416   int nPendingData;
       
 99417 #define kPendingThreshold (1*1024*1024)
       
 99418   sqlite_int64 iPrevDocid;
       
 99419   fts3Hash pendingTerms;
       
 99420 };
       
 99421 
       
 99422 /*
       
 99423 ** When the core wants to do a query, it create a cursor using a
       
 99424 ** call to xOpen.  This structure is an instance of a cursor.  It
       
 99425 ** is destroyed by xClose.
       
 99426 */
       
 99427 typedef struct fulltext_cursor {
       
 99428   sqlite3_vtab_cursor base;        /* Base class used by SQLite core */
       
 99429   QueryType iCursorType;           /* Copy of sqlite3_index_info.idxNum */
       
 99430   sqlite3_stmt *pStmt;             /* Prepared statement in use by the cursor */
       
 99431   int eof;                         /* True if at End Of Results */
       
 99432   Fts3Expr *pExpr;                 /* Parsed MATCH query string */
       
 99433   Snippet snippet;                 /* Cached snippet for the current row */
       
 99434   int iColumn;                     /* Column being searched */
       
 99435   DataBuffer result;               /* Doclist results from fulltextQuery */
       
 99436   DLReader reader;                 /* Result reader if result not empty */
       
 99437 } fulltext_cursor;
       
 99438 
       
 99439 static fulltext_vtab *cursor_vtab(fulltext_cursor *c){
       
 99440   return (fulltext_vtab *) c->base.pVtab;
       
 99441 }
       
 99442 
       
 99443 static const sqlite3_module fts3Module;   /* forward declaration */
       
 99444 
       
 99445 /* Return a dynamically generated statement of the form
       
 99446  *   insert into %_content (docid, ...) values (?, ...)
       
 99447  */
       
 99448 static const char *contentInsertStatement(fulltext_vtab *v){
       
 99449   StringBuffer sb;
       
 99450   int i;
       
 99451 
       
 99452   initStringBuffer(&sb);
       
 99453   append(&sb, "insert into %_content (docid, ");
       
 99454   appendList(&sb, v->nColumn, v->azContentColumn);
       
 99455   append(&sb, ") values (?");
       
 99456   for(i=0; i<v->nColumn; ++i)
       
 99457     append(&sb, ", ?");
       
 99458   append(&sb, ")");
       
 99459   return stringBufferData(&sb);
       
 99460 }
       
 99461 
       
 99462 /* Return a dynamically generated statement of the form
       
 99463  *   select <content columns> from %_content where docid = ?
       
 99464  */
       
 99465 static const char *contentSelectStatement(fulltext_vtab *v){
       
 99466   StringBuffer sb;
       
 99467   initStringBuffer(&sb);
       
 99468   append(&sb, "SELECT ");
       
 99469   appendList(&sb, v->nColumn, v->azContentColumn);
       
 99470   append(&sb, " FROM %_content WHERE docid = ?");
       
 99471   return stringBufferData(&sb);
       
 99472 }
       
 99473 
       
 99474 /* Return a dynamically generated statement of the form
       
 99475  *   update %_content set [col_0] = ?, [col_1] = ?, ...
       
 99476  *                    where docid = ?
       
 99477  */
       
 99478 static const char *contentUpdateStatement(fulltext_vtab *v){
       
 99479   StringBuffer sb;
       
 99480   int i;
       
 99481 
       
 99482   initStringBuffer(&sb);
       
 99483   append(&sb, "update %_content set ");
       
 99484   for(i=0; i<v->nColumn; ++i) {
       
 99485     if( i>0 ){
       
 99486       append(&sb, ", ");
       
 99487     }
       
 99488     append(&sb, v->azContentColumn[i]);
       
 99489     append(&sb, " = ?");
       
 99490   }
       
 99491   append(&sb, " where docid = ?");
       
 99492   return stringBufferData(&sb);
       
 99493 }
       
 99494 
       
 99495 /* Puts a freshly-prepared statement determined by iStmt in *ppStmt.
       
 99496 ** If the indicated statement has never been prepared, it is prepared
       
 99497 ** and cached, otherwise the cached version is reset.
       
 99498 */
       
 99499 static int sql_get_statement(fulltext_vtab *v, fulltext_statement iStmt,
       
 99500                              sqlite3_stmt **ppStmt){
       
 99501   assert( iStmt<MAX_STMT );
       
 99502   if( v->pFulltextStatements[iStmt]==NULL ){
       
 99503     const char *zStmt;
       
 99504     int rc;
       
 99505     switch( iStmt ){
       
 99506       case CONTENT_INSERT_STMT:
       
 99507         zStmt = contentInsertStatement(v); break;
       
 99508       case CONTENT_SELECT_STMT:
       
 99509         zStmt = contentSelectStatement(v); break;
       
 99510       case CONTENT_UPDATE_STMT:
       
 99511         zStmt = contentUpdateStatement(v); break;
       
 99512       default:
       
 99513         zStmt = fulltext_zStatement[iStmt];
       
 99514     }
       
 99515     rc = sql_prepare(v->db, v->zDb, v->zName, &v->pFulltextStatements[iStmt],
       
 99516                          zStmt);
       
 99517     if( zStmt != fulltext_zStatement[iStmt]) sqlite3_free((void *) zStmt);
       
 99518     if( rc!=SQLITE_OK ) return rc;
       
 99519   } else {
       
 99520     int rc = sqlite3_reset(v->pFulltextStatements[iStmt]);
       
 99521     if( rc!=SQLITE_OK ) return rc;
       
 99522   }
       
 99523 
       
 99524   *ppStmt = v->pFulltextStatements[iStmt];
       
 99525   return SQLITE_OK;
       
 99526 }
       
 99527 
       
 99528 /* Like sqlite3_step(), but convert SQLITE_DONE to SQLITE_OK and
       
 99529 ** SQLITE_ROW to SQLITE_ERROR.  Useful for statements like UPDATE,
       
 99530 ** where we expect no results.
       
 99531 */
       
 99532 static int sql_single_step(sqlite3_stmt *s){
       
 99533   int rc = sqlite3_step(s);
       
 99534   return (rc==SQLITE_DONE) ? SQLITE_OK : rc;
       
 99535 }
       
 99536 
       
 99537 /* Like sql_get_statement(), but for special replicated LEAF_SELECT
       
 99538 ** statements.  idx -1 is a special case for an uncached version of
       
 99539 ** the statement (used in the optimize implementation).
       
 99540 */
       
 99541 /* TODO(shess) Write version for generic statements and then share
       
 99542 ** that between the cached-statement functions.
       
 99543 */
       
 99544 static int sql_get_leaf_statement(fulltext_vtab *v, int idx,
       
 99545                                   sqlite3_stmt **ppStmt){
       
 99546   assert( idx>=-1 && idx<MERGE_COUNT );
       
 99547   if( idx==-1 ){
       
 99548     return sql_prepare(v->db, v->zDb, v->zName, ppStmt, LEAF_SELECT);
       
 99549   }else if( v->pLeafSelectStmts[idx]==NULL ){
       
 99550     int rc = sql_prepare(v->db, v->zDb, v->zName, &v->pLeafSelectStmts[idx],
       
 99551                          LEAF_SELECT);
       
 99552     if( rc!=SQLITE_OK ) return rc;
       
 99553   }else{
       
 99554     int rc = sqlite3_reset(v->pLeafSelectStmts[idx]);
       
 99555     if( rc!=SQLITE_OK ) return rc;
       
 99556   }
       
 99557 
       
 99558   *ppStmt = v->pLeafSelectStmts[idx];
       
 99559   return SQLITE_OK;
       
 99560 }
       
 99561 
       
 99562 /* insert into %_content (docid, ...) values ([docid], [pValues])
       
 99563 ** If the docid contains SQL NULL, then a unique docid will be
       
 99564 ** generated.
       
 99565 */
       
 99566 static int content_insert(fulltext_vtab *v, sqlite3_value *docid,
       
 99567                           sqlite3_value **pValues){
       
 99568   sqlite3_stmt *s;
       
 99569   int i;
       
 99570   int rc = sql_get_statement(v, CONTENT_INSERT_STMT, &s);
       
 99571   if( rc!=SQLITE_OK ) return rc;
       
 99572 
       
 99573   rc = sqlite3_bind_value(s, 1, docid);
       
 99574   if( rc!=SQLITE_OK ) return rc;
       
 99575 
       
 99576   for(i=0; i<v->nColumn; ++i){
       
 99577     rc = sqlite3_bind_value(s, 2+i, pValues[i]);
       
 99578     if( rc!=SQLITE_OK ) return rc;
       
 99579   }
       
 99580 
       
 99581   return sql_single_step(s);
       
 99582 }
       
 99583 
       
 99584 /* update %_content set col0 = pValues[0], col1 = pValues[1], ...
       
 99585  *                  where docid = [iDocid] */
       
 99586 static int content_update(fulltext_vtab *v, sqlite3_value **pValues,
       
 99587                           sqlite_int64 iDocid){
       
 99588   sqlite3_stmt *s;
       
 99589   int i;
       
 99590   int rc = sql_get_statement(v, CONTENT_UPDATE_STMT, &s);
       
 99591   if( rc!=SQLITE_OK ) return rc;
       
 99592 
       
 99593   for(i=0; i<v->nColumn; ++i){
       
 99594     rc = sqlite3_bind_value(s, 1+i, pValues[i]);
       
 99595     if( rc!=SQLITE_OK ) return rc;
       
 99596   }
       
 99597 
       
 99598   rc = sqlite3_bind_int64(s, 1+v->nColumn, iDocid);
       
 99599   if( rc!=SQLITE_OK ) return rc;
       
 99600 
       
 99601   return sql_single_step(s);
       
 99602 }
       
 99603 
       
 99604 static void freeStringArray(int nString, const char **pString){
       
 99605   int i;
       
 99606 
       
 99607   for (i=0 ; i < nString ; ++i) {
       
 99608     if( pString[i]!=NULL ) sqlite3_free((void *) pString[i]);
       
 99609   }
       
 99610   sqlite3_free((void *) pString);
       
 99611 }
       
 99612 
       
 99613 /* select * from %_content where docid = [iDocid]
       
 99614  * The caller must delete the returned array and all strings in it.
       
 99615  * null fields will be NULL in the returned array.
       
 99616  *
       
 99617  * TODO: Perhaps we should return pointer/length strings here for consistency
       
 99618  * with other code which uses pointer/length. */
       
 99619 static int content_select(fulltext_vtab *v, sqlite_int64 iDocid,
       
 99620                           const char ***pValues){
       
 99621   sqlite3_stmt *s;
       
 99622   const char **values;
       
 99623   int i;
       
 99624   int rc;
       
 99625 
       
 99626   *pValues = NULL;
       
 99627 
       
 99628   rc = sql_get_statement(v, CONTENT_SELECT_STMT, &s);
       
 99629   if( rc!=SQLITE_OK ) return rc;
       
 99630 
       
 99631   rc = sqlite3_bind_int64(s, 1, iDocid);
       
 99632   if( rc!=SQLITE_OK ) return rc;
       
 99633 
       
 99634   rc = sqlite3_step(s);
       
 99635   if( rc!=SQLITE_ROW ) return rc;
       
 99636 
       
 99637   values = (const char **) sqlite3_malloc(v->nColumn * sizeof(const char *));
       
 99638   for(i=0; i<v->nColumn; ++i){
       
 99639     if( sqlite3_column_type(s, i)==SQLITE_NULL ){
       
 99640       values[i] = NULL;
       
 99641     }else{
       
 99642       values[i] = string_dup((char*)sqlite3_column_text(s, i));
       
 99643     }
       
 99644   }
       
 99645 
       
 99646   /* We expect only one row.  We must execute another sqlite3_step()
       
 99647    * to complete the iteration; otherwise the table will remain locked. */
       
 99648   rc = sqlite3_step(s);
       
 99649   if( rc==SQLITE_DONE ){
       
 99650     *pValues = values;
       
 99651     return SQLITE_OK;
       
 99652   }
       
 99653 
       
 99654   freeStringArray(v->nColumn, values);
       
 99655   return rc;
       
 99656 }
       
 99657 
       
 99658 /* delete from %_content where docid = [iDocid ] */
       
 99659 static int content_delete(fulltext_vtab *v, sqlite_int64 iDocid){
       
 99660   sqlite3_stmt *s;
       
 99661   int rc = sql_get_statement(v, CONTENT_DELETE_STMT, &s);
       
 99662   if( rc!=SQLITE_OK ) return rc;
       
 99663 
       
 99664   rc = sqlite3_bind_int64(s, 1, iDocid);
       
 99665   if( rc!=SQLITE_OK ) return rc;
       
 99666 
       
 99667   return sql_single_step(s);
       
 99668 }
       
 99669 
       
 99670 /* Returns SQLITE_ROW if any rows exist in %_content, SQLITE_DONE if
       
 99671 ** no rows exist, and any error in case of failure.
       
 99672 */
       
 99673 static int content_exists(fulltext_vtab *v){
       
 99674   sqlite3_stmt *s;
       
 99675   int rc = sql_get_statement(v, CONTENT_EXISTS_STMT, &s);
       
 99676   if( rc!=SQLITE_OK ) return rc;
       
 99677 
       
 99678   rc = sqlite3_step(s);
       
 99679   if( rc!=SQLITE_ROW ) return rc;
       
 99680 
       
 99681   /* We expect only one row.  We must execute another sqlite3_step()
       
 99682    * to complete the iteration; otherwise the table will remain locked. */
       
 99683   rc = sqlite3_step(s);
       
 99684   if( rc==SQLITE_DONE ) return SQLITE_ROW;
       
 99685   if( rc==SQLITE_ROW ) return SQLITE_ERROR;
       
 99686   return rc;
       
 99687 }
       
 99688 
       
 99689 /* insert into %_segments values ([pData])
       
 99690 **   returns assigned blockid in *piBlockid
       
 99691 */
       
 99692 static int block_insert(fulltext_vtab *v, const char *pData, int nData,
       
 99693                         sqlite_int64 *piBlockid){
       
 99694   sqlite3_stmt *s;
       
 99695   int rc = sql_get_statement(v, BLOCK_INSERT_STMT, &s);
       
 99696   if( rc!=SQLITE_OK ) return rc;
       
 99697 
       
 99698   rc = sqlite3_bind_blob(s, 1, pData, nData, SQLITE_STATIC);
       
 99699   if( rc!=SQLITE_OK ) return rc;
       
 99700 
       
 99701   rc = sqlite3_step(s);
       
 99702   if( rc==SQLITE_ROW ) return SQLITE_ERROR;
       
 99703   if( rc!=SQLITE_DONE ) return rc;
       
 99704 
       
 99705   /* blockid column is an alias for rowid. */
       
 99706   *piBlockid = sqlite3_last_insert_rowid(v->db);
       
 99707   return SQLITE_OK;
       
 99708 }
       
 99709 
       
 99710 /* delete from %_segments
       
 99711 **   where blockid between [iStartBlockid] and [iEndBlockid]
       
 99712 **
       
 99713 ** Deletes the range of blocks, inclusive, used to delete the blocks
       
 99714 ** which form a segment.
       
 99715 */
       
 99716 static int block_delete(fulltext_vtab *v,
       
 99717                         sqlite_int64 iStartBlockid, sqlite_int64 iEndBlockid){
       
 99718   sqlite3_stmt *s;
       
 99719   int rc = sql_get_statement(v, BLOCK_DELETE_STMT, &s);
       
 99720   if( rc!=SQLITE_OK ) return rc;
       
 99721 
       
 99722   rc = sqlite3_bind_int64(s, 1, iStartBlockid);
       
 99723   if( rc!=SQLITE_OK ) return rc;
       
 99724 
       
 99725   rc = sqlite3_bind_int64(s, 2, iEndBlockid);
       
 99726   if( rc!=SQLITE_OK ) return rc;
       
 99727 
       
 99728   return sql_single_step(s);
       
 99729 }
       
 99730 
       
 99731 /* Returns SQLITE_ROW with *pidx set to the maximum segment idx found
       
 99732 ** at iLevel.  Returns SQLITE_DONE if there are no segments at
       
 99733 ** iLevel.  Otherwise returns an error.
       
 99734 */
       
 99735 static int segdir_max_index(fulltext_vtab *v, int iLevel, int *pidx){
       
 99736   sqlite3_stmt *s;
       
 99737   int rc = sql_get_statement(v, SEGDIR_MAX_INDEX_STMT, &s);
       
 99738   if( rc!=SQLITE_OK ) return rc;
       
 99739 
       
 99740   rc = sqlite3_bind_int(s, 1, iLevel);
       
 99741   if( rc!=SQLITE_OK ) return rc;
       
 99742 
       
 99743   rc = sqlite3_step(s);
       
 99744   /* Should always get at least one row due to how max() works. */
       
 99745   if( rc==SQLITE_DONE ) return SQLITE_DONE;
       
 99746   if( rc!=SQLITE_ROW ) return rc;
       
 99747 
       
 99748   /* NULL means that there were no inputs to max(). */
       
 99749   if( SQLITE_NULL==sqlite3_column_type(s, 0) ){
       
 99750     rc = sqlite3_step(s);
       
 99751     if( rc==SQLITE_ROW ) return SQLITE_ERROR;
       
 99752     return rc;
       
 99753   }
       
 99754 
       
 99755   *pidx = sqlite3_column_int(s, 0);
       
 99756 
       
 99757   /* We expect only one row.  We must execute another sqlite3_step()
       
 99758    * to complete the iteration; otherwise the table will remain locked. */
       
 99759   rc = sqlite3_step(s);
       
 99760   if( rc==SQLITE_ROW ) return SQLITE_ERROR;
       
 99761   if( rc!=SQLITE_DONE ) return rc;
       
 99762   return SQLITE_ROW;
       
 99763 }
       
 99764 
       
 99765 /* insert into %_segdir values (
       
 99766 **   [iLevel], [idx],
       
 99767 **   [iStartBlockid], [iLeavesEndBlockid], [iEndBlockid],
       
 99768 **   [pRootData]
       
 99769 ** )
       
 99770 */
       
 99771 static int segdir_set(fulltext_vtab *v, int iLevel, int idx,
       
 99772                       sqlite_int64 iStartBlockid,
       
 99773                       sqlite_int64 iLeavesEndBlockid,
       
 99774                       sqlite_int64 iEndBlockid,
       
 99775                       const char *pRootData, int nRootData){
       
 99776   sqlite3_stmt *s;
       
 99777   int rc = sql_get_statement(v, SEGDIR_SET_STMT, &s);
       
 99778   if( rc!=SQLITE_OK ) return rc;
       
 99779 
       
 99780   rc = sqlite3_bind_int(s, 1, iLevel);
       
 99781   if( rc!=SQLITE_OK ) return rc;
       
 99782 
       
 99783   rc = sqlite3_bind_int(s, 2, idx);
       
 99784   if( rc!=SQLITE_OK ) return rc;
       
 99785 
       
 99786   rc = sqlite3_bind_int64(s, 3, iStartBlockid);
       
 99787   if( rc!=SQLITE_OK ) return rc;
       
 99788 
       
 99789   rc = sqlite3_bind_int64(s, 4, iLeavesEndBlockid);
       
 99790   if( rc!=SQLITE_OK ) return rc;
       
 99791 
       
 99792   rc = sqlite3_bind_int64(s, 5, iEndBlockid);
       
 99793   if( rc!=SQLITE_OK ) return rc;
       
 99794 
       
 99795   rc = sqlite3_bind_blob(s, 6, pRootData, nRootData, SQLITE_STATIC);
       
 99796   if( rc!=SQLITE_OK ) return rc;
       
 99797 
       
 99798   return sql_single_step(s);
       
 99799 }
       
 99800 
       
 99801 /* Queries %_segdir for the block span of the segments in level
       
 99802 ** iLevel.  Returns SQLITE_DONE if there are no blocks for iLevel,
       
 99803 ** SQLITE_ROW if there are blocks, else an error.
       
 99804 */
       
 99805 static int segdir_span(fulltext_vtab *v, int iLevel,
       
 99806                        sqlite_int64 *piStartBlockid,
       
 99807                        sqlite_int64 *piEndBlockid){
       
 99808   sqlite3_stmt *s;
       
 99809   int rc = sql_get_statement(v, SEGDIR_SPAN_STMT, &s);
       
 99810   if( rc!=SQLITE_OK ) return rc;
       
 99811 
       
 99812   rc = sqlite3_bind_int(s, 1, iLevel);
       
 99813   if( rc!=SQLITE_OK ) return rc;
       
 99814 
       
 99815   rc = sqlite3_step(s);
       
 99816   if( rc==SQLITE_DONE ) return SQLITE_DONE;  /* Should never happen */
       
 99817   if( rc!=SQLITE_ROW ) return rc;
       
 99818 
       
 99819   /* This happens if all segments at this level are entirely inline. */
       
 99820   if( SQLITE_NULL==sqlite3_column_type(s, 0) ){
       
 99821     /* We expect only one row.  We must execute another sqlite3_step()
       
 99822      * to complete the iteration; otherwise the table will remain locked. */
       
 99823     int rc2 = sqlite3_step(s);
       
 99824     if( rc2==SQLITE_ROW ) return SQLITE_ERROR;
       
 99825     return rc2;
       
 99826   }
       
 99827 
       
 99828   *piStartBlockid = sqlite3_column_int64(s, 0);
       
 99829   *piEndBlockid = sqlite3_column_int64(s, 1);
       
 99830 
       
 99831   /* We expect only one row.  We must execute another sqlite3_step()
       
 99832    * to complete the iteration; otherwise the table will remain locked. */
       
 99833   rc = sqlite3_step(s);
       
 99834   if( rc==SQLITE_ROW ) return SQLITE_ERROR;
       
 99835   if( rc!=SQLITE_DONE ) return rc;
       
 99836   return SQLITE_ROW;
       
 99837 }
       
 99838 
       
 99839 /* Delete the segment blocks and segment directory records for all
       
 99840 ** segments at iLevel.
       
 99841 */
       
 99842 static int segdir_delete(fulltext_vtab *v, int iLevel){
       
 99843   sqlite3_stmt *s;
       
 99844   sqlite_int64 iStartBlockid, iEndBlockid;
       
 99845   int rc = segdir_span(v, iLevel, &iStartBlockid, &iEndBlockid);
       
 99846   if( rc!=SQLITE_ROW && rc!=SQLITE_DONE ) return rc;
       
 99847 
       
 99848   if( rc==SQLITE_ROW ){
       
 99849     rc = block_delete(v, iStartBlockid, iEndBlockid);
       
 99850     if( rc!=SQLITE_OK ) return rc;
       
 99851   }
       
 99852 
       
 99853   /* Delete the segment directory itself. */
       
 99854   rc = sql_get_statement(v, SEGDIR_DELETE_STMT, &s);
       
 99855   if( rc!=SQLITE_OK ) return rc;
       
 99856 
       
 99857   rc = sqlite3_bind_int64(s, 1, iLevel);
       
 99858   if( rc!=SQLITE_OK ) return rc;
       
 99859 
       
 99860   return sql_single_step(s);
       
 99861 }
       
 99862 
       
 99863 /* Delete entire fts index, SQLITE_OK on success, relevant error on
       
 99864 ** failure.
       
 99865 */
       
 99866 static int segdir_delete_all(fulltext_vtab *v){
       
 99867   sqlite3_stmt *s;
       
 99868   int rc = sql_get_statement(v, SEGDIR_DELETE_ALL_STMT, &s);
       
 99869   if( rc!=SQLITE_OK ) return rc;
       
 99870 
       
 99871   rc = sql_single_step(s);
       
 99872   if( rc!=SQLITE_OK ) return rc;
       
 99873 
       
 99874   rc = sql_get_statement(v, BLOCK_DELETE_ALL_STMT, &s);
       
 99875   if( rc!=SQLITE_OK ) return rc;
       
 99876 
       
 99877   return sql_single_step(s);
       
 99878 }
       
 99879 
       
 99880 /* Returns SQLITE_OK with *pnSegments set to the number of entries in
       
 99881 ** %_segdir and *piMaxLevel set to the highest level which has a
       
 99882 ** segment.  Otherwise returns the SQLite error which caused failure.
       
 99883 */
       
 99884 static int segdir_count(fulltext_vtab *v, int *pnSegments, int *piMaxLevel){
       
 99885   sqlite3_stmt *s;
       
 99886   int rc = sql_get_statement(v, SEGDIR_COUNT_STMT, &s);
       
 99887   if( rc!=SQLITE_OK ) return rc;
       
 99888 
       
 99889   rc = sqlite3_step(s);
       
 99890   /* TODO(shess): This case should not be possible?  Should stronger
       
 99891   ** measures be taken if it happens?
       
 99892   */
       
 99893   if( rc==SQLITE_DONE ){
       
 99894     *pnSegments = 0;
       
 99895     *piMaxLevel = 0;
       
 99896     return SQLITE_OK;
       
 99897   }
       
 99898   if( rc!=SQLITE_ROW ) return rc;
       
 99899 
       
 99900   *pnSegments = sqlite3_column_int(s, 0);
       
 99901   *piMaxLevel = sqlite3_column_int(s, 1);
       
 99902 
       
 99903   /* We expect only one row.  We must execute another sqlite3_step()
       
 99904    * to complete the iteration; otherwise the table will remain locked. */
       
 99905   rc = sqlite3_step(s);
       
 99906   if( rc==SQLITE_DONE ) return SQLITE_OK;
       
 99907   if( rc==SQLITE_ROW ) return SQLITE_ERROR;
       
 99908   return rc;
       
 99909 }
       
 99910 
       
 99911 /* TODO(shess) clearPendingTerms() is far down the file because
       
 99912 ** writeZeroSegment() is far down the file because LeafWriter is far
       
 99913 ** down the file.  Consider refactoring the code to move the non-vtab
       
 99914 ** code above the vtab code so that we don't need this forward
       
 99915 ** reference.
       
 99916 */
       
 99917 static int clearPendingTerms(fulltext_vtab *v);
       
 99918 
       
 99919 /*
       
 99920 ** Free the memory used to contain a fulltext_vtab structure.
       
 99921 */
       
 99922 static void fulltext_vtab_destroy(fulltext_vtab *v){
       
 99923   int iStmt, i;
       
 99924 
       
 99925   FTSTRACE(("FTS3 Destroy %p\n", v));
       
 99926   for( iStmt=0; iStmt<MAX_STMT; iStmt++ ){
       
 99927     if( v->pFulltextStatements[iStmt]!=NULL ){
       
 99928       sqlite3_finalize(v->pFulltextStatements[iStmt]);
       
 99929       v->pFulltextStatements[iStmt] = NULL;
       
 99930     }
       
 99931   }
       
 99932 
       
 99933   for( i=0; i<MERGE_COUNT; i++ ){
       
 99934     if( v->pLeafSelectStmts[i]!=NULL ){
       
 99935       sqlite3_finalize(v->pLeafSelectStmts[i]);
       
 99936       v->pLeafSelectStmts[i] = NULL;
       
 99937     }
       
 99938   }
       
 99939 
       
 99940   if( v->pTokenizer!=NULL ){
       
 99941     v->pTokenizer->pModule->xDestroy(v->pTokenizer);
       
 99942     v->pTokenizer = NULL;
       
 99943   }
       
 99944 
       
 99945   clearPendingTerms(v);
       
 99946 
       
 99947   sqlite3_free(v->azColumn);
       
 99948   for(i = 0; i < v->nColumn; ++i) {
       
 99949     sqlite3_free(v->azContentColumn[i]);
       
 99950   }
       
 99951   sqlite3_free(v->azContentColumn);
       
 99952   sqlite3_free(v);
       
 99953 }
       
 99954 
       
 99955 /*
       
 99956 ** Token types for parsing the arguments to xConnect or xCreate.
       
 99957 */
       
 99958 #define TOKEN_EOF         0    /* End of file */
       
 99959 #define TOKEN_SPACE       1    /* Any kind of whitespace */
       
 99960 #define TOKEN_ID          2    /* An identifier */
       
 99961 #define TOKEN_STRING      3    /* A string literal */
       
 99962 #define TOKEN_PUNCT       4    /* A single punctuation character */
       
 99963 
       
 99964 /*
       
 99965 ** If X is a character that can be used in an identifier then
       
 99966 ** ftsIdChar(X) will be true.  Otherwise it is false.
       
 99967 **
       
 99968 ** For ASCII, any character with the high-order bit set is
       
 99969 ** allowed in an identifier.  For 7-bit characters, 
       
 99970 ** isFtsIdChar[X] must be 1.
       
 99971 **
       
 99972 ** Ticket #1066.  the SQL standard does not allow '$' in the
       
 99973 ** middle of identfiers.  But many SQL implementations do. 
       
 99974 ** SQLite will allow '$' in identifiers for compatibility.
       
 99975 ** But the feature is undocumented.
       
 99976 */
       
 99977 static const char isFtsIdChar[] = {
       
 99978 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
       
 99979     0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
       
 99980     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
       
 99981     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
       
 99982     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
       
 99983     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
       
 99984     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
       
 99985 };
       
 99986 #define ftsIdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && isFtsIdChar[c-0x20]))
       
 99987 
       
 99988 
       
 99989 /*
       
 99990 ** Return the length of the token that begins at z[0]. 
       
 99991 ** Store the token type in *tokenType before returning.
       
 99992 */
       
 99993 static int ftsGetToken(const char *z, int *tokenType){
       
 99994   int i, c;
       
 99995   switch( *z ){
       
 99996     case 0: {
       
 99997       *tokenType = TOKEN_EOF;
       
 99998       return 0;
       
 99999     }
       
100000     case ' ': case '\t': case '\n': case '\f': case '\r': {
       
100001       for(i=1; safe_isspace(z[i]); i++){}
       
100002       *tokenType = TOKEN_SPACE;
       
100003       return i;
       
100004     }
       
100005     case '`':
       
100006     case '\'':
       
100007     case '"': {
       
100008       int delim = z[0];
       
100009       for(i=1; (c=z[i])!=0; i++){
       
100010         if( c==delim ){
       
100011           if( z[i+1]==delim ){
       
100012             i++;
       
100013           }else{
       
100014             break;
       
100015           }
       
100016         }
       
100017       }
       
100018       *tokenType = TOKEN_STRING;
       
100019       return i + (c!=0);
       
100020     }
       
100021     case '[': {
       
100022       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
       
100023       *tokenType = TOKEN_ID;
       
100024       return i;
       
100025     }
       
100026     default: {
       
100027       if( !ftsIdChar(*z) ){
       
100028         break;
       
100029       }
       
100030       for(i=1; ftsIdChar(z[i]); i++){}
       
100031       *tokenType = TOKEN_ID;
       
100032       return i;
       
100033     }
       
100034   }
       
100035   *tokenType = TOKEN_PUNCT;
       
100036   return 1;
       
100037 }
       
100038 
       
100039 /*
       
100040 ** A token extracted from a string is an instance of the following
       
100041 ** structure.
       
100042 */
       
100043 typedef struct FtsToken {
       
100044   const char *z;       /* Pointer to token text.  Not '\000' terminated */
       
100045   short int n;         /* Length of the token text in bytes. */
       
100046 } FtsToken;
       
100047 
       
100048 /*
       
100049 ** Given a input string (which is really one of the argv[] parameters
       
100050 ** passed into xConnect or xCreate) split the string up into tokens.
       
100051 ** Return an array of pointers to '\000' terminated strings, one string
       
100052 ** for each non-whitespace token.
       
100053 **
       
100054 ** The returned array is terminated by a single NULL pointer.
       
100055 **
       
100056 ** Space to hold the returned array is obtained from a single
       
100057 ** malloc and should be freed by passing the return value to free().
       
100058 ** The individual strings within the token list are all a part of
       
100059 ** the single memory allocation and will all be freed at once.
       
100060 */
       
100061 static char **tokenizeString(const char *z, int *pnToken){
       
100062   int nToken = 0;
       
100063   FtsToken *aToken = sqlite3_malloc( strlen(z) * sizeof(aToken[0]) );
       
100064   int n = 1;
       
100065   int e, i;
       
100066   int totalSize = 0;
       
100067   char **azToken;
       
100068   char *zCopy;
       
100069   while( n>0 ){
       
100070     n = ftsGetToken(z, &e);
       
100071     if( e!=TOKEN_SPACE ){
       
100072       aToken[nToken].z = z;
       
100073       aToken[nToken].n = n;
       
100074       nToken++;
       
100075       totalSize += n+1;
       
100076     }
       
100077     z += n;
       
100078   }
       
100079   azToken = (char**)sqlite3_malloc( nToken*sizeof(char*) + totalSize );
       
100080   zCopy = (char*)&azToken[nToken];
       
100081   nToken--;
       
100082   for(i=0; i<nToken; i++){
       
100083     azToken[i] = zCopy;
       
100084     n = aToken[i].n;
       
100085     memcpy(zCopy, aToken[i].z, n);
       
100086     zCopy[n] = 0;
       
100087     zCopy += n+1;
       
100088   }
       
100089   azToken[nToken] = 0;
       
100090   sqlite3_free(aToken);
       
100091   *pnToken = nToken;
       
100092   return azToken;
       
100093 }
       
100094 
       
100095 /*
       
100096 ** Convert an SQL-style quoted string into a normal string by removing
       
100097 ** the quote characters.  The conversion is done in-place.  If the
       
100098 ** input does not begin with a quote character, then this routine
       
100099 ** is a no-op.
       
100100 **
       
100101 ** Examples:
       
100102 **
       
100103 **     "abc"   becomes   abc
       
100104 **     'xyz'   becomes   xyz
       
100105 **     [pqr]   becomes   pqr
       
100106 **     `mno`   becomes   mno
       
100107 */
       
100108 static void dequoteString(char *z){
       
100109   int quote;
       
100110   int i, j;
       
100111   if( z==0 ) return;
       
100112   quote = z[0];
       
100113   switch( quote ){
       
100114     case '\'':  break;
       
100115     case '"':   break;
       
100116     case '`':   break;                /* For MySQL compatibility */
       
100117     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
       
100118     default:    return;
       
100119   }
       
100120   for(i=1, j=0; z[i]; i++){
       
100121     if( z[i]==quote ){
       
100122       if( z[i+1]==quote ){
       
100123         z[j++] = quote;
       
100124         i++;
       
100125       }else{
       
100126         z[j++] = 0;
       
100127         break;
       
100128       }
       
100129     }else{
       
100130       z[j++] = z[i];
       
100131     }
       
100132   }
       
100133 }
       
100134 
       
100135 /*
       
100136 ** The input azIn is a NULL-terminated list of tokens.  Remove the first
       
100137 ** token and all punctuation tokens.  Remove the quotes from
       
100138 ** around string literal tokens.
       
100139 **
       
100140 ** Example:
       
100141 **
       
100142 **     input:      tokenize chinese ( 'simplifed' , 'mixed' )
       
100143 **     output:     chinese simplifed mixed
       
100144 **
       
100145 ** Another example:
       
100146 **
       
100147 **     input:      delimiters ( '[' , ']' , '...' )
       
100148 **     output:     [ ] ...
       
100149 */
       
100150 static void tokenListToIdList(char **azIn){
       
100151   int i, j;
       
100152   if( azIn ){
       
100153     for(i=0, j=-1; azIn[i]; i++){
       
100154       if( safe_isalnum(azIn[i][0]) || azIn[i][1] ){
       
100155         dequoteString(azIn[i]);
       
100156         if( j>=0 ){
       
100157           azIn[j] = azIn[i];
       
100158         }
       
100159         j++;
       
100160       }
       
100161     }
       
100162     azIn[j] = 0;
       
100163   }
       
100164 }
       
100165 
       
100166 
       
100167 /*
       
100168 ** Find the first alphanumeric token in the string zIn.  Null-terminate
       
100169 ** this token.  Remove any quotation marks.  And return a pointer to
       
100170 ** the result.
       
100171 */
       
100172 static char *firstToken(char *zIn, char **pzTail){
       
100173   int n, ttype;
       
100174   while(1){
       
100175     n = ftsGetToken(zIn, &ttype);
       
100176     if( ttype==TOKEN_SPACE ){
       
100177       zIn += n;
       
100178     }else if( ttype==TOKEN_EOF ){
       
100179       *pzTail = zIn;
       
100180       return 0;
       
100181     }else{
       
100182       zIn[n] = 0;
       
100183       *pzTail = &zIn[1];
       
100184       dequoteString(zIn);
       
100185       return zIn;
       
100186     }
       
100187   }
       
100188   /*NOTREACHED*/
       
100189 }
       
100190 
       
100191 /* Return true if...
       
100192 **
       
100193 **   *  s begins with the string t, ignoring case
       
100194 **   *  s is longer than t
       
100195 **   *  The first character of s beyond t is not a alphanumeric
       
100196 ** 
       
100197 ** Ignore leading space in *s.
       
100198 **
       
100199 ** To put it another way, return true if the first token of
       
100200 ** s[] is t[].
       
100201 */
       
100202 static int startsWith(const char *s, const char *t){
       
100203   while( safe_isspace(*s) ){ s++; }
       
100204   while( *t ){
       
100205     if( safe_tolower(*s++)!=safe_tolower(*t++) ) return 0;
       
100206   }
       
100207   return *s!='_' && !safe_isalnum(*s);
       
100208 }
       
100209 
       
100210 /*
       
100211 ** An instance of this structure defines the "spec" of a
       
100212 ** full text index.  This structure is populated by parseSpec
       
100213 ** and use by fulltextConnect and fulltextCreate.
       
100214 */
       
100215 typedef struct TableSpec {
       
100216   const char *zDb;         /* Logical database name */
       
100217   const char *zName;       /* Name of the full-text index */
       
100218   int nColumn;             /* Number of columns to be indexed */
       
100219   char **azColumn;         /* Original names of columns to be indexed */
       
100220   char **azContentColumn;  /* Column names for %_content */
       
100221   char **azTokenizer;      /* Name of tokenizer and its arguments */
       
100222 } TableSpec;
       
100223 
       
100224 /*
       
100225 ** Reclaim all of the memory used by a TableSpec
       
100226 */
       
100227 static void clearTableSpec(TableSpec *p) {
       
100228   sqlite3_free(p->azColumn);
       
100229   sqlite3_free(p->azContentColumn);
       
100230   sqlite3_free(p->azTokenizer);
       
100231 }
       
100232 
       
100233 /* Parse a CREATE VIRTUAL TABLE statement, which looks like this:
       
100234  *
       
100235  * CREATE VIRTUAL TABLE email
       
100236  *        USING fts3(subject, body, tokenize mytokenizer(myarg))
       
100237  *
       
100238  * We return parsed information in a TableSpec structure.
       
100239  * 
       
100240  */
       
100241 static int parseSpec(TableSpec *pSpec, int argc, const char *const*argv,
       
100242                      char**pzErr){
       
100243   int i, n;
       
100244   char *z, *zDummy;
       
100245   char **azArg;
       
100246   const char *zTokenizer = 0;    /* argv[] entry describing the tokenizer */
       
100247 
       
100248   assert( argc>=3 );
       
100249   /* Current interface:
       
100250   ** argv[0] - module name
       
100251   ** argv[1] - database name
       
100252   ** argv[2] - table name
       
100253   ** argv[3..] - columns, optionally followed by tokenizer specification
       
100254   **             and snippet delimiters specification.
       
100255   */
       
100256 
       
100257   /* Make a copy of the complete argv[][] array in a single allocation.
       
100258   ** The argv[][] array is read-only and transient.  We can write to the
       
100259   ** copy in order to modify things and the copy is persistent.
       
100260   */
       
100261   CLEAR(pSpec);
       
100262   for(i=n=0; i<argc; i++){
       
100263     n += strlen(argv[i]) + 1;
       
100264   }
       
100265   azArg = sqlite3_malloc( sizeof(char*)*argc + n );
       
100266   if( azArg==0 ){
       
100267     return SQLITE_NOMEM;
       
100268   }
       
100269   z = (char*)&azArg[argc];
       
100270   for(i=0; i<argc; i++){
       
100271     azArg[i] = z;
       
100272     strcpy(z, argv[i]);
       
100273     z += strlen(z)+1;
       
100274   }
       
100275 
       
100276   /* Identify the column names and the tokenizer and delimiter arguments
       
100277   ** in the argv[][] array.
       
100278   */
       
100279   pSpec->zDb = azArg[1];
       
100280   pSpec->zName = azArg[2];
       
100281   pSpec->nColumn = 0;
       
100282   pSpec->azColumn = azArg;
       
100283   zTokenizer = "tokenize simple";
       
100284   for(i=3; i<argc; ++i){
       
100285     if( startsWith(azArg[i],"tokenize") ){
       
100286       zTokenizer = azArg[i];
       
100287     }else{
       
100288       z = azArg[pSpec->nColumn] = firstToken(azArg[i], &zDummy);
       
100289       pSpec->nColumn++;
       
100290     }
       
100291   }
       
100292   if( pSpec->nColumn==0 ){
       
100293     azArg[0] = "content";
       
100294     pSpec->nColumn = 1;
       
100295   }
       
100296 
       
100297   /*
       
100298   ** Construct the list of content column names.
       
100299   **
       
100300   ** Each content column name will be of the form cNNAAAA
       
100301   ** where NN is the column number and AAAA is the sanitized
       
100302   ** column name.  "sanitized" means that special characters are
       
100303   ** converted to "_".  The cNN prefix guarantees that all column
       
100304   ** names are unique.
       
100305   **
       
100306   ** The AAAA suffix is not strictly necessary.  It is included
       
100307   ** for the convenience of people who might examine the generated
       
100308   ** %_content table and wonder what the columns are used for.
       
100309   */
       
100310   pSpec->azContentColumn = sqlite3_malloc( pSpec->nColumn * sizeof(char *) );
       
100311   if( pSpec->azContentColumn==0 ){
       
100312     clearTableSpec(pSpec);
       
100313     return SQLITE_NOMEM;
       
100314   }
       
100315   for(i=0; i<pSpec->nColumn; i++){
       
100316     char *p;
       
100317     pSpec->azContentColumn[i] = sqlite3_mprintf("c%d%s", i, azArg[i]);
       
100318     for (p = pSpec->azContentColumn[i]; *p ; ++p) {
       
100319       if( !safe_isalnum(*p) ) *p = '_';
       
100320     }
       
100321   }
       
100322 
       
100323   /*
       
100324   ** Parse the tokenizer specification string.
       
100325   */
       
100326   pSpec->azTokenizer = tokenizeString(zTokenizer, &n);
       
100327   tokenListToIdList(pSpec->azTokenizer);
       
100328 
       
100329   return SQLITE_OK;
       
100330 }
       
100331 
       
100332 /*
       
100333 ** Generate a CREATE TABLE statement that describes the schema of
       
100334 ** the virtual table.  Return a pointer to this schema string.
       
100335 **
       
100336 ** Space is obtained from sqlite3_mprintf() and should be freed
       
100337 ** using sqlite3_free().
       
100338 */
       
100339 static char *fulltextSchema(
       
100340   int nColumn,                  /* Number of columns */
       
100341   const char *const* azColumn,  /* List of columns */
       
100342   const char *zTableName        /* Name of the table */
       
100343 ){
       
100344   int i;
       
100345   char *zSchema, *zNext;
       
100346   const char *zSep = "(";
       
100347   zSchema = sqlite3_mprintf("CREATE TABLE x");
       
100348   for(i=0; i<nColumn; i++){
       
100349     zNext = sqlite3_mprintf("%s%s%Q", zSchema, zSep, azColumn[i]);
       
100350     sqlite3_free(zSchema);
       
100351     zSchema = zNext;
       
100352     zSep = ",";
       
100353   }
       
100354   zNext = sqlite3_mprintf("%s,%Q HIDDEN", zSchema, zTableName);
       
100355   sqlite3_free(zSchema);
       
100356   zSchema = zNext;
       
100357   zNext = sqlite3_mprintf("%s,docid HIDDEN)", zSchema);
       
100358   sqlite3_free(zSchema);
       
100359   return zNext;
       
100360 }
       
100361 
       
100362 /*
       
100363 ** Build a new sqlite3_vtab structure that will describe the
       
100364 ** fulltext index defined by spec.
       
100365 */
       
100366 static int constructVtab(
       
100367   sqlite3 *db,              /* The SQLite database connection */
       
100368   fts3Hash *pHash,          /* Hash table containing tokenizers */
       
100369   TableSpec *spec,          /* Parsed spec information from parseSpec() */
       
100370   sqlite3_vtab **ppVTab,    /* Write the resulting vtab structure here */
       
100371   char **pzErr              /* Write any error message here */
       
100372 ){
       
100373   int rc;
       
100374   int n;
       
100375   fulltext_vtab *v = 0;
       
100376   const sqlite3_tokenizer_module *m = NULL;
       
100377   char *schema;
       
100378 
       
100379   char const *zTok;         /* Name of tokenizer to use for this fts table */
       
100380   int nTok;                 /* Length of zTok, including nul terminator */
       
100381 
       
100382   v = (fulltext_vtab *) sqlite3_malloc(sizeof(fulltext_vtab));
       
100383   if( v==0 ) return SQLITE_NOMEM;
       
100384   CLEAR(v);
       
100385   /* sqlite will initialize v->base */
       
100386   v->db = db;
       
100387   v->zDb = spec->zDb;       /* Freed when azColumn is freed */
       
100388   v->zName = spec->zName;   /* Freed when azColumn is freed */
       
100389   v->nColumn = spec->nColumn;
       
100390   v->azContentColumn = spec->azContentColumn;
       
100391   spec->azContentColumn = 0;
       
100392   v->azColumn = spec->azColumn;
       
100393   spec->azColumn = 0;
       
100394 
       
100395   if( spec->azTokenizer==0 ){
       
100396     return SQLITE_NOMEM;
       
100397   }
       
100398 
       
100399   zTok = spec->azTokenizer[0]; 
       
100400   if( !zTok ){
       
100401     zTok = "simple";
       
100402   }
       
100403   nTok = strlen(zTok)+1;
       
100404 
       
100405   m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zTok, nTok);
       
100406   if( !m ){
       
100407     *pzErr = sqlite3_mprintf("unknown tokenizer: %s", spec->azTokenizer[0]);
       
100408     rc = SQLITE_ERROR;
       
100409     goto err;
       
100410   }
       
100411 
       
100412   for(n=0; spec->azTokenizer[n]; n++){}
       
100413   if( n ){
       
100414     rc = m->xCreate(n-1, (const char*const*)&spec->azTokenizer[1],
       
100415                     &v->pTokenizer);
       
100416   }else{
       
100417     rc = m->xCreate(0, 0, &v->pTokenizer);
       
100418   }
       
100419   if( rc!=SQLITE_OK ) goto err;
       
100420   v->pTokenizer->pModule = m;
       
100421 
       
100422   /* TODO: verify the existence of backing tables foo_content, foo_term */
       
100423 
       
100424   schema = fulltextSchema(v->nColumn, (const char*const*)v->azColumn,
       
100425                           spec->zName);
       
100426   rc = sqlite3_declare_vtab(db, schema);
       
100427   sqlite3_free(schema);
       
100428   if( rc!=SQLITE_OK ) goto err;
       
100429 
       
100430   memset(v->pFulltextStatements, 0, sizeof(v->pFulltextStatements));
       
100431 
       
100432   /* Indicate that the buffer is not live. */
       
100433   v->nPendingData = -1;
       
100434 
       
100435   *ppVTab = &v->base;
       
100436   FTSTRACE(("FTS3 Connect %p\n", v));
       
100437 
       
100438   return rc;
       
100439 
       
100440 err:
       
100441   fulltext_vtab_destroy(v);
       
100442   return rc;
       
100443 }
       
100444 
       
100445 static int fulltextConnect(
       
100446   sqlite3 *db,
       
100447   void *pAux,
       
100448   int argc, const char *const*argv,
       
100449   sqlite3_vtab **ppVTab,
       
100450   char **pzErr
       
100451 ){
       
100452   TableSpec spec;
       
100453   int rc = parseSpec(&spec, argc, argv, pzErr);
       
100454   if( rc!=SQLITE_OK ) return rc;
       
100455 
       
100456   rc = constructVtab(db, (fts3Hash *)pAux, &spec, ppVTab, pzErr);
       
100457   clearTableSpec(&spec);
       
100458   return rc;
       
100459 }
       
100460 
       
100461 /* The %_content table holds the text of each document, with
       
100462 ** the docid column exposed as the SQLite rowid for the table.
       
100463 */
       
100464 /* TODO(shess) This comment needs elaboration to match the updated
       
100465 ** code.  Work it into the top-of-file comment at that time.
       
100466 */
       
100467 static int fulltextCreate(sqlite3 *db, void *pAux,
       
100468                           int argc, const char * const *argv,
       
100469                           sqlite3_vtab **ppVTab, char **pzErr){
       
100470   int rc;
       
100471   TableSpec spec;
       
100472   StringBuffer schema;
       
100473   FTSTRACE(("FTS3 Create\n"));
       
100474 
       
100475   rc = parseSpec(&spec, argc, argv, pzErr);
       
100476   if( rc!=SQLITE_OK ) return rc;
       
100477 
       
100478   initStringBuffer(&schema);
       
100479   append(&schema, "CREATE TABLE %_content(");
       
100480   append(&schema, "  docid INTEGER PRIMARY KEY,");
       
100481   appendList(&schema, spec.nColumn, spec.azContentColumn);
       
100482   append(&schema, ")");
       
100483   rc = sql_exec(db, spec.zDb, spec.zName, stringBufferData(&schema));
       
100484   stringBufferDestroy(&schema);
       
100485   if( rc!=SQLITE_OK ) goto out;
       
100486 
       
100487   rc = sql_exec(db, spec.zDb, spec.zName,
       
100488                 "create table %_segments("
       
100489                 "  blockid INTEGER PRIMARY KEY,"
       
100490                 "  block blob"
       
100491                 ");"
       
100492                 );
       
100493   if( rc!=SQLITE_OK ) goto out;
       
100494 
       
100495   rc = sql_exec(db, spec.zDb, spec.zName,
       
100496                 "create table %_segdir("
       
100497                 "  level integer,"
       
100498                 "  idx integer,"
       
100499                 "  start_block integer,"
       
100500                 "  leaves_end_block integer,"
       
100501                 "  end_block integer,"
       
100502                 "  root blob,"
       
100503                 "  primary key(level, idx)"
       
100504                 ");");
       
100505   if( rc!=SQLITE_OK ) goto out;
       
100506 
       
100507   rc = constructVtab(db, (fts3Hash *)pAux, &spec, ppVTab, pzErr);
       
100508 
       
100509 out:
       
100510   clearTableSpec(&spec);
       
100511   return rc;
       
100512 }
       
100513 
       
100514 /* Decide how to handle an SQL query. */
       
100515 static int fulltextBestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
       
100516   fulltext_vtab *v = (fulltext_vtab *)pVTab;
       
100517   int i;
       
100518   FTSTRACE(("FTS3 BestIndex\n"));
       
100519 
       
100520   for(i=0; i<pInfo->nConstraint; ++i){
       
100521     const struct sqlite3_index_constraint *pConstraint;
       
100522     pConstraint = &pInfo->aConstraint[i];
       
100523     if( pConstraint->usable ) {
       
100524       if( (pConstraint->iColumn==-1 || pConstraint->iColumn==v->nColumn+1) &&
       
100525           pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
       
100526         pInfo->idxNum = QUERY_DOCID;      /* lookup by docid */
       
100527         FTSTRACE(("FTS3 QUERY_DOCID\n"));
       
100528       } else if( pConstraint->iColumn>=0 && pConstraint->iColumn<=v->nColumn &&
       
100529                  pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
       
100530         /* full-text search */
       
100531         pInfo->idxNum = QUERY_FULLTEXT + pConstraint->iColumn;
       
100532         FTSTRACE(("FTS3 QUERY_FULLTEXT %d\n", pConstraint->iColumn));
       
100533       } else continue;
       
100534 
       
100535       pInfo->aConstraintUsage[i].argvIndex = 1;
       
100536       pInfo->aConstraintUsage[i].omit = 1;
       
100537 
       
100538       /* An arbitrary value for now.
       
100539        * TODO: Perhaps docid matches should be considered cheaper than
       
100540        * full-text searches. */
       
100541       pInfo->estimatedCost = 1.0;   
       
100542 
       
100543       return SQLITE_OK;
       
100544     }
       
100545   }
       
100546   pInfo->idxNum = QUERY_GENERIC;
       
100547   return SQLITE_OK;
       
100548 }
       
100549 
       
100550 static int fulltextDisconnect(sqlite3_vtab *pVTab){
       
100551   FTSTRACE(("FTS3 Disconnect %p\n", pVTab));
       
100552   fulltext_vtab_destroy((fulltext_vtab *)pVTab);
       
100553   return SQLITE_OK;
       
100554 }
       
100555 
       
100556 static int fulltextDestroy(sqlite3_vtab *pVTab){
       
100557   fulltext_vtab *v = (fulltext_vtab *)pVTab;
       
100558   int rc;
       
100559 
       
100560   FTSTRACE(("FTS3 Destroy %p\n", pVTab));
       
100561   rc = sql_exec(v->db, v->zDb, v->zName,
       
100562                 "drop table if exists %_content;"
       
100563                 "drop table if exists %_segments;"
       
100564                 "drop table if exists %_segdir;"
       
100565                 );
       
100566   if( rc!=SQLITE_OK ) return rc;
       
100567 
       
100568   fulltext_vtab_destroy((fulltext_vtab *)pVTab);
       
100569   return SQLITE_OK;
       
100570 }
       
100571 
       
100572 static int fulltextOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
       
100573   fulltext_cursor *c;
       
100574 
       
100575   c = (fulltext_cursor *) sqlite3_malloc(sizeof(fulltext_cursor));
       
100576   if( c ){
       
100577     memset(c, 0, sizeof(fulltext_cursor));
       
100578     /* sqlite will initialize c->base */
       
100579     *ppCursor = &c->base;
       
100580     FTSTRACE(("FTS3 Open %p: %p\n", pVTab, c));
       
100581     return SQLITE_OK;
       
100582   }else{
       
100583     return SQLITE_NOMEM;
       
100584   }
       
100585 }
       
100586 
       
100587 /* Free all of the dynamically allocated memory held by the
       
100588 ** Snippet
       
100589 */
       
100590 static void snippetClear(Snippet *p){
       
100591   sqlite3_free(p->aMatch);
       
100592   sqlite3_free(p->zOffset);
       
100593   sqlite3_free(p->zSnippet);
       
100594   CLEAR(p);
       
100595 }
       
100596 
       
100597 /*
       
100598 ** Append a single entry to the p->aMatch[] log.
       
100599 */
       
100600 static void snippetAppendMatch(
       
100601   Snippet *p,               /* Append the entry to this snippet */
       
100602   int iCol, int iTerm,      /* The column and query term */
       
100603   int iToken,               /* Matching token in document */
       
100604   int iStart, int nByte     /* Offset and size of the match */
       
100605 ){
       
100606   int i;
       
100607   struct snippetMatch *pMatch;
       
100608   if( p->nMatch+1>=p->nAlloc ){
       
100609     p->nAlloc = p->nAlloc*2 + 10;
       
100610     p->aMatch = sqlite3_realloc(p->aMatch, p->nAlloc*sizeof(p->aMatch[0]) );
       
100611     if( p->aMatch==0 ){
       
100612       p->nMatch = 0;
       
100613       p->nAlloc = 0;
       
100614       return;
       
100615     }
       
100616   }
       
100617   i = p->nMatch++;
       
100618   pMatch = &p->aMatch[i];
       
100619   pMatch->iCol = iCol;
       
100620   pMatch->iTerm = iTerm;
       
100621   pMatch->iToken = iToken;
       
100622   pMatch->iStart = iStart;
       
100623   pMatch->nByte = nByte;
       
100624 }
       
100625 
       
100626 /*
       
100627 ** Sizing information for the circular buffer used in snippetOffsetsOfColumn()
       
100628 */
       
100629 #define FTS3_ROTOR_SZ   (32)
       
100630 #define FTS3_ROTOR_MASK (FTS3_ROTOR_SZ-1)
       
100631 
       
100632 /*
       
100633 ** Function to iterate through the tokens of a compiled expression.
       
100634 **
       
100635 ** Except, skip all tokens on the right-hand side of a NOT operator.
       
100636 ** This function is used to find tokens as part of snippet and offset
       
100637 ** generation and we do nt want snippets and offsets to report matches
       
100638 ** for tokens on the RHS of a NOT.
       
100639 */
       
100640 static int fts3NextExprToken(Fts3Expr **ppExpr, int *piToken){
       
100641   Fts3Expr *p = *ppExpr;
       
100642   int iToken = *piToken;
       
100643   if( iToken<0 ){
       
100644     /* In this case the expression p is the root of an expression tree.
       
100645     ** Move to the first token in the expression tree.
       
100646     */
       
100647     while( p->pLeft ){
       
100648       p = p->pLeft;
       
100649     }
       
100650     iToken = 0;
       
100651   }else{
       
100652     assert(p && p->eType==FTSQUERY_PHRASE );
       
100653     if( iToken<(p->pPhrase->nToken-1) ){
       
100654       iToken++;
       
100655     }else{
       
100656       iToken = 0;
       
100657       while( p->pParent && p->pParent->pLeft!=p ){
       
100658         assert( p->pParent->pRight==p );
       
100659         p = p->pParent;
       
100660       }
       
100661       p = p->pParent;
       
100662       if( p ){
       
100663         assert( p->pRight!=0 );
       
100664         p = p->pRight;
       
100665         while( p->pLeft ){
       
100666           p = p->pLeft;
       
100667         }
       
100668       }
       
100669     }
       
100670   }
       
100671 
       
100672   *ppExpr = p;
       
100673   *piToken = iToken;
       
100674   return p?1:0;
       
100675 }
       
100676 
       
100677 /*
       
100678 ** Return TRUE if the expression node pExpr is located beneath the
       
100679 ** RHS of a NOT operator.
       
100680 */
       
100681 static int fts3ExprBeneathNot(Fts3Expr *p){
       
100682   Fts3Expr *pParent;
       
100683   while( p ){
       
100684     pParent = p->pParent;
       
100685     if( pParent && pParent->eType==FTSQUERY_NOT && pParent->pRight==p ){
       
100686       return 1;
       
100687     }
       
100688     p = pParent;
       
100689   }
       
100690   return 0;
       
100691 }
       
100692 
       
100693 /*
       
100694 ** Add entries to pSnippet->aMatch[] for every match that occurs against
       
100695 ** document zDoc[0..nDoc-1] which is stored in column iColumn.
       
100696 */
       
100697 static void snippetOffsetsOfColumn(
       
100698   fulltext_cursor *pCur,         /* The fulltest search cursor */
       
100699   Snippet *pSnippet,             /* The Snippet object to be filled in */
       
100700   int iColumn,                   /* Index of fulltext table column */
       
100701   const char *zDoc,              /* Text of the fulltext table column */
       
100702   int nDoc                       /* Length of zDoc in bytes */
       
100703 ){
       
100704   const sqlite3_tokenizer_module *pTModule;  /* The tokenizer module */
       
100705   sqlite3_tokenizer *pTokenizer;             /* The specific tokenizer */
       
100706   sqlite3_tokenizer_cursor *pTCursor;        /* Tokenizer cursor */
       
100707   fulltext_vtab *pVtab;                /* The full text index */
       
100708   int nColumn;                         /* Number of columns in the index */
       
100709   int i, j;                            /* Loop counters */
       
100710   int rc;                              /* Return code */
       
100711   unsigned int match, prevMatch;       /* Phrase search bitmasks */
       
100712   const char *zToken;                  /* Next token from the tokenizer */
       
100713   int nToken;                          /* Size of zToken */
       
100714   int iBegin, iEnd, iPos;              /* Offsets of beginning and end */
       
100715 
       
100716   /* The following variables keep a circular buffer of the last
       
100717   ** few tokens */
       
100718   unsigned int iRotor = 0;             /* Index of current token */
       
100719   int iRotorBegin[FTS3_ROTOR_SZ];      /* Beginning offset of token */
       
100720   int iRotorLen[FTS3_ROTOR_SZ];        /* Length of token */
       
100721 
       
100722   pVtab = cursor_vtab(pCur);
       
100723   nColumn = pVtab->nColumn;
       
100724   pTokenizer = pVtab->pTokenizer;
       
100725   pTModule = pTokenizer->pModule;
       
100726   rc = pTModule->xOpen(pTokenizer, zDoc, nDoc, &pTCursor);
       
100727   if( rc ) return;
       
100728   pTCursor->pTokenizer = pTokenizer;
       
100729 
       
100730   prevMatch = 0;
       
100731   while( !pTModule->xNext(pTCursor, &zToken, &nToken, &iBegin, &iEnd, &iPos) ){
       
100732     Fts3Expr *pIter = pCur->pExpr;
       
100733     int iIter = -1;
       
100734     iRotorBegin[iRotor&FTS3_ROTOR_MASK] = iBegin;
       
100735     iRotorLen[iRotor&FTS3_ROTOR_MASK] = iEnd-iBegin;
       
100736     match = 0;
       
100737     for(i=0; i<(FTS3_ROTOR_SZ-1) && fts3NextExprToken(&pIter, &iIter); i++){
       
100738       int nPhrase;                    /* Number of tokens in current phrase */
       
100739       struct PhraseToken *pToken;     /* Current token */
       
100740       int iCol;                       /* Column index */
       
100741 
       
100742       if( fts3ExprBeneathNot(pIter) ) continue;
       
100743       nPhrase = pIter->pPhrase->nToken;
       
100744       pToken = &pIter->pPhrase->aToken[iIter];
       
100745       iCol = pIter->pPhrase->iColumn;
       
100746       if( iCol>=0 && iCol<nColumn && iCol!=iColumn ) continue;
       
100747       if( pToken->n>nToken ) continue;
       
100748       if( !pToken->isPrefix && pToken->n<nToken ) continue;
       
100749       assert( pToken->n<=nToken );
       
100750       if( memcmp(pToken->z, zToken, pToken->n) ) continue;
       
100751       if( iIter>0 && (prevMatch & (1<<i))==0 ) continue;
       
100752       match |= 1<<i;
       
100753       if( i==(FTS3_ROTOR_SZ-2) || nPhrase==iIter+1 ){
       
100754         for(j=nPhrase-1; j>=0; j--){
       
100755           int k = (iRotor-j) & FTS3_ROTOR_MASK;
       
100756           snippetAppendMatch(pSnippet, iColumn, i-j, iPos-j,
       
100757                 iRotorBegin[k], iRotorLen[k]);
       
100758         }
       
100759       }
       
100760     }
       
100761     prevMatch = match<<1;
       
100762     iRotor++;
       
100763   }
       
100764   pTModule->xClose(pTCursor);  
       
100765 }
       
100766 
       
100767 /*
       
100768 ** Remove entries from the pSnippet structure to account for the NEAR
       
100769 ** operator. When this is called, pSnippet contains the list of token 
       
100770 ** offsets produced by treating all NEAR operators as AND operators.
       
100771 ** This function removes any entries that should not be present after
       
100772 ** accounting for the NEAR restriction. For example, if the queried
       
100773 ** document is:
       
100774 **
       
100775 **     "A B C D E A"
       
100776 **
       
100777 ** and the query is:
       
100778 ** 
       
100779 **     A NEAR/0 E
       
100780 **
       
100781 ** then when this function is called the Snippet contains token offsets
       
100782 ** 0, 4 and 5. This function removes the "0" entry (because the first A
       
100783 ** is not near enough to an E).
       
100784 **
       
100785 ** When this function is called, the value pointed to by parameter piLeft is
       
100786 ** the integer id of the left-most token in the expression tree headed by
       
100787 ** pExpr. This function increments *piLeft by the total number of tokens
       
100788 ** in the expression tree headed by pExpr.
       
100789 **
       
100790 ** Return 1 if any trimming occurs.  Return 0 if no trimming is required.
       
100791 */
       
100792 static int trimSnippetOffsets(
       
100793   Fts3Expr *pExpr,      /* The search expression */
       
100794   Snippet *pSnippet,    /* The set of snippet offsets to be trimmed */
       
100795   int *piLeft           /* Index of left-most token in pExpr */
       
100796 ){
       
100797   if( pExpr ){
       
100798     if( trimSnippetOffsets(pExpr->pLeft, pSnippet, piLeft) ){
       
100799       return 1;
       
100800     }
       
100801 
       
100802     switch( pExpr->eType ){
       
100803       case FTSQUERY_PHRASE:
       
100804         *piLeft += pExpr->pPhrase->nToken;
       
100805         break;
       
100806       case FTSQUERY_NEAR: {
       
100807         /* The right-hand-side of a NEAR operator is always a phrase. The
       
100808         ** left-hand-side is either a phrase or an expression tree that is 
       
100809         ** itself headed by a NEAR operator. The following initializations
       
100810         ** set local variable iLeft to the token number of the left-most
       
100811         ** token in the right-hand phrase, and iRight to the right most
       
100812         ** token in the same phrase. For example, if we had:
       
100813         **
       
100814         **     <col> MATCH '"abc def" NEAR/2 "ghi jkl"'
       
100815         **
       
100816         ** then iLeft will be set to 2 (token number of ghi) and nToken will
       
100817         ** be set to 4.
       
100818         */
       
100819         Fts3Expr *pLeft = pExpr->pLeft;
       
100820         Fts3Expr *pRight = pExpr->pRight;
       
100821         int iLeft = *piLeft;
       
100822         int nNear = pExpr->nNear;
       
100823         int nToken = pRight->pPhrase->nToken;
       
100824         int jj, ii;
       
100825         if( pLeft->eType==FTSQUERY_NEAR ){
       
100826           pLeft = pLeft->pRight;
       
100827         }
       
100828         assert( pRight->eType==FTSQUERY_PHRASE );
       
100829         assert( pLeft->eType==FTSQUERY_PHRASE );
       
100830         nToken += pLeft->pPhrase->nToken;
       
100831 
       
100832         for(ii=0; ii<pSnippet->nMatch; ii++){
       
100833           struct snippetMatch *p = &pSnippet->aMatch[ii];
       
100834           if( p->iTerm==iLeft ){
       
100835             int isOk = 0;
       
100836             /* Snippet ii is an occurence of query term iLeft in the document.
       
100837             ** It occurs at position (p->iToken) of the document. We now
       
100838             ** search for an instance of token (iLeft-1) somewhere in the 
       
100839             ** range (p->iToken - nNear)...(p->iToken + nNear + nToken) within 
       
100840             ** the set of snippetMatch structures. If one is found, proceed. 
       
100841             ** If one cannot be found, then remove snippets ii..(ii+N-1) 
       
100842             ** from the matching snippets, where N is the number of tokens 
       
100843             ** in phrase pRight->pPhrase.
       
100844             */
       
100845             for(jj=0; isOk==0 && jj<pSnippet->nMatch; jj++){
       
100846               struct snippetMatch *p2 = &pSnippet->aMatch[jj];
       
100847               if( p2->iTerm==(iLeft-1) ){
       
100848                 if( p2->iToken>=(p->iToken-nNear-1) 
       
100849                  && p2->iToken<(p->iToken+nNear+nToken) 
       
100850                 ){
       
100851                   isOk = 1;
       
100852                 }
       
100853               }
       
100854             }
       
100855             if( !isOk ){
       
100856               int kk;
       
100857               for(kk=0; kk<pRight->pPhrase->nToken; kk++){
       
100858                 pSnippet->aMatch[kk+ii].iTerm = -2;
       
100859               }
       
100860               return 1;
       
100861             }
       
100862           }
       
100863           if( p->iTerm==(iLeft-1) ){
       
100864             int isOk = 0;
       
100865             for(jj=0; isOk==0 && jj<pSnippet->nMatch; jj++){
       
100866               struct snippetMatch *p2 = &pSnippet->aMatch[jj];
       
100867               if( p2->iTerm==iLeft ){
       
100868                 if( p2->iToken<=(p->iToken+nNear+1) 
       
100869                  && p2->iToken>(p->iToken-nNear-nToken) 
       
100870                 ){
       
100871                   isOk = 1;
       
100872                 }
       
100873               }
       
100874             }
       
100875             if( !isOk ){
       
100876               int kk;
       
100877               for(kk=0; kk<pLeft->pPhrase->nToken; kk++){
       
100878                 pSnippet->aMatch[ii-kk].iTerm = -2;
       
100879               }
       
100880               return 1;
       
100881             }
       
100882           }
       
100883         }
       
100884         break;
       
100885       }
       
100886     }
       
100887 
       
100888     if( trimSnippetOffsets(pExpr->pRight, pSnippet, piLeft) ){
       
100889       return 1;
       
100890     }
       
100891   }
       
100892   return 0;
       
100893 }
       
100894 
       
100895 /*
       
100896 ** Compute all offsets for the current row of the query.  
       
100897 ** If the offsets have already been computed, this routine is a no-op.
       
100898 */
       
100899 static void snippetAllOffsets(fulltext_cursor *p){
       
100900   int nColumn;
       
100901   int iColumn, i;
       
100902   int iFirst, iLast;
       
100903   int iTerm = 0;
       
100904   fulltext_vtab *pFts = cursor_vtab(p);
       
100905 
       
100906   if( p->snippet.nMatch || p->pExpr==0 ){
       
100907     return;
       
100908   }
       
100909   nColumn = pFts->nColumn;
       
100910   iColumn = (p->iCursorType - QUERY_FULLTEXT);
       
100911   if( iColumn<0 || iColumn>=nColumn ){
       
100912     /* Look for matches over all columns of the full-text index */
       
100913     iFirst = 0;
       
100914     iLast = nColumn-1;
       
100915   }else{
       
100916     /* Look for matches in the iColumn-th column of the index only */
       
100917     iFirst = iColumn;
       
100918     iLast = iColumn;
       
100919   }
       
100920   for(i=iFirst; i<=iLast; i++){
       
100921     const char *zDoc;
       
100922     int nDoc;
       
100923     zDoc = (const char*)sqlite3_column_text(p->pStmt, i+1);
       
100924     nDoc = sqlite3_column_bytes(p->pStmt, i+1);
       
100925     snippetOffsetsOfColumn(p, &p->snippet, i, zDoc, nDoc);
       
100926   }
       
100927 
       
100928   while( trimSnippetOffsets(p->pExpr, &p->snippet, &iTerm) ){
       
100929     iTerm = 0;
       
100930   }
       
100931 }
       
100932 
       
100933 /*
       
100934 ** Convert the information in the aMatch[] array of the snippet
       
100935 ** into the string zOffset[0..nOffset-1]. This string is used as
       
100936 ** the return of the SQL offsets() function.
       
100937 */
       
100938 static void snippetOffsetText(Snippet *p){
       
100939   int i;
       
100940   int cnt = 0;
       
100941   StringBuffer sb;
       
100942   char zBuf[200];
       
100943   if( p->zOffset ) return;
       
100944   initStringBuffer(&sb);
       
100945   for(i=0; i<p->nMatch; i++){
       
100946     struct snippetMatch *pMatch = &p->aMatch[i];
       
100947     if( pMatch->iTerm>=0 ){
       
100948       /* If snippetMatch.iTerm is less than 0, then the match was 
       
100949       ** discarded as part of processing the NEAR operator (see the 
       
100950       ** trimSnippetOffsetsForNear() function for details). Ignore 
       
100951       ** it in this case
       
100952       */
       
100953       zBuf[0] = ' ';
       
100954       sqlite3_snprintf(sizeof(zBuf)-1, &zBuf[cnt>0], "%d %d %d %d",
       
100955           pMatch->iCol, pMatch->iTerm, pMatch->iStart, pMatch->nByte);
       
100956       append(&sb, zBuf);
       
100957       cnt++;
       
100958     }
       
100959   }
       
100960   p->zOffset = stringBufferData(&sb);
       
100961   p->nOffset = stringBufferLength(&sb);
       
100962 }
       
100963 
       
100964 /*
       
100965 ** zDoc[0..nDoc-1] is phrase of text.  aMatch[0..nMatch-1] are a set
       
100966 ** of matching words some of which might be in zDoc.  zDoc is column
       
100967 ** number iCol.
       
100968 **
       
100969 ** iBreak is suggested spot in zDoc where we could begin or end an
       
100970 ** excerpt.  Return a value similar to iBreak but possibly adjusted
       
100971 ** to be a little left or right so that the break point is better.
       
100972 */
       
100973 static int wordBoundary(
       
100974   int iBreak,                   /* The suggested break point */
       
100975   const char *zDoc,             /* Document text */
       
100976   int nDoc,                     /* Number of bytes in zDoc[] */
       
100977   struct snippetMatch *aMatch,  /* Matching words */
       
100978   int nMatch,                   /* Number of entries in aMatch[] */
       
100979   int iCol                      /* The column number for zDoc[] */
       
100980 ){
       
100981   int i;
       
100982   if( iBreak<=10 ){
       
100983     return 0;
       
100984   }
       
100985   if( iBreak>=nDoc-10 ){
       
100986     return nDoc;
       
100987   }
       
100988   for(i=0; i<nMatch && aMatch[i].iCol<iCol; i++){}
       
100989   while( i<nMatch && aMatch[i].iStart+aMatch[i].nByte<iBreak ){ i++; }
       
100990   if( i<nMatch ){
       
100991     if( aMatch[i].iStart<iBreak+10 ){
       
100992       return aMatch[i].iStart;
       
100993     }
       
100994     if( i>0 && aMatch[i-1].iStart+aMatch[i-1].nByte>=iBreak ){
       
100995       return aMatch[i-1].iStart;
       
100996     }
       
100997   }
       
100998   for(i=1; i<=10; i++){
       
100999     if( safe_isspace(zDoc[iBreak-i]) ){
       
101000       return iBreak - i + 1;
       
101001     }
       
101002     if( safe_isspace(zDoc[iBreak+i]) ){
       
101003       return iBreak + i + 1;
       
101004     }
       
101005   }
       
101006   return iBreak;
       
101007 }
       
101008 
       
101009 
       
101010 
       
101011 /*
       
101012 ** Allowed values for Snippet.aMatch[].snStatus
       
101013 */
       
101014 #define SNIPPET_IGNORE  0   /* It is ok to omit this match from the snippet */
       
101015 #define SNIPPET_DESIRED 1   /* We want to include this match in the snippet */
       
101016 
       
101017 /*
       
101018 ** Generate the text of a snippet.
       
101019 */
       
101020 static void snippetText(
       
101021   fulltext_cursor *pCursor,   /* The cursor we need the snippet for */
       
101022   const char *zStartMark,     /* Markup to appear before each match */
       
101023   const char *zEndMark,       /* Markup to appear after each match */
       
101024   const char *zEllipsis       /* Ellipsis mark */
       
101025 ){
       
101026   int i, j;
       
101027   struct snippetMatch *aMatch;
       
101028   int nMatch;
       
101029   int nDesired;
       
101030   StringBuffer sb;
       
101031   int tailCol;
       
101032   int tailOffset;
       
101033   int iCol;
       
101034   int nDoc;
       
101035   const char *zDoc;
       
101036   int iStart, iEnd;
       
101037   int tailEllipsis = 0;
       
101038   int iMatch;
       
101039   
       
101040 
       
101041   sqlite3_free(pCursor->snippet.zSnippet);
       
101042   pCursor->snippet.zSnippet = 0;
       
101043   aMatch = pCursor->snippet.aMatch;
       
101044   nMatch = pCursor->snippet.nMatch;
       
101045   initStringBuffer(&sb);
       
101046 
       
101047   for(i=0; i<nMatch; i++){
       
101048     aMatch[i].snStatus = SNIPPET_IGNORE;
       
101049   }
       
101050   nDesired = 0;
       
101051   for(i=0; i<FTS3_ROTOR_SZ; i++){
       
101052     for(j=0; j<nMatch; j++){
       
101053       if( aMatch[j].iTerm==i ){
       
101054         aMatch[j].snStatus = SNIPPET_DESIRED;
       
101055         nDesired++;
       
101056         break;
       
101057       }
       
101058     }
       
101059   }
       
101060 
       
101061   iMatch = 0;
       
101062   tailCol = -1;
       
101063   tailOffset = 0;
       
101064   for(i=0; i<nMatch && nDesired>0; i++){
       
101065     if( aMatch[i].snStatus!=SNIPPET_DESIRED ) continue;
       
101066     nDesired--;
       
101067     iCol = aMatch[i].iCol;
       
101068     zDoc = (const char*)sqlite3_column_text(pCursor->pStmt, iCol+1);
       
101069     nDoc = sqlite3_column_bytes(pCursor->pStmt, iCol+1);
       
101070     iStart = aMatch[i].iStart - 40;
       
101071     iStart = wordBoundary(iStart, zDoc, nDoc, aMatch, nMatch, iCol);
       
101072     if( iStart<=10 ){
       
101073       iStart = 0;
       
101074     }
       
101075     if( iCol==tailCol && iStart<=tailOffset+20 ){
       
101076       iStart = tailOffset;
       
101077     }
       
101078     if( (iCol!=tailCol && tailCol>=0) || iStart!=tailOffset ){
       
101079       trimWhiteSpace(&sb);
       
101080       appendWhiteSpace(&sb);
       
101081       append(&sb, zEllipsis);
       
101082       appendWhiteSpace(&sb);
       
101083     }
       
101084     iEnd = aMatch[i].iStart + aMatch[i].nByte + 40;
       
101085     iEnd = wordBoundary(iEnd, zDoc, nDoc, aMatch, nMatch, iCol);
       
101086     if( iEnd>=nDoc-10 ){
       
101087       iEnd = nDoc;
       
101088       tailEllipsis = 0;
       
101089     }else{
       
101090       tailEllipsis = 1;
       
101091     }
       
101092     while( iMatch<nMatch && aMatch[iMatch].iCol<iCol ){ iMatch++; }
       
101093     while( iStart<iEnd ){
       
101094       while( iMatch<nMatch && aMatch[iMatch].iStart<iStart
       
101095              && aMatch[iMatch].iCol<=iCol ){
       
101096         iMatch++;
       
101097       }
       
101098       if( iMatch<nMatch && aMatch[iMatch].iStart<iEnd
       
101099              && aMatch[iMatch].iCol==iCol ){
       
101100         nappend(&sb, &zDoc[iStart], aMatch[iMatch].iStart - iStart);
       
101101         iStart = aMatch[iMatch].iStart;
       
101102         append(&sb, zStartMark);
       
101103         nappend(&sb, &zDoc[iStart], aMatch[iMatch].nByte);
       
101104         append(&sb, zEndMark);
       
101105         iStart += aMatch[iMatch].nByte;
       
101106         for(j=iMatch+1; j<nMatch; j++){
       
101107           if( aMatch[j].iTerm==aMatch[iMatch].iTerm
       
101108               && aMatch[j].snStatus==SNIPPET_DESIRED ){
       
101109             nDesired--;
       
101110             aMatch[j].snStatus = SNIPPET_IGNORE;
       
101111           }
       
101112         }
       
101113       }else{
       
101114         nappend(&sb, &zDoc[iStart], iEnd - iStart);
       
101115         iStart = iEnd;
       
101116       }
       
101117     }
       
101118     tailCol = iCol;
       
101119     tailOffset = iEnd;
       
101120   }
       
101121   trimWhiteSpace(&sb);
       
101122   if( tailEllipsis ){
       
101123     appendWhiteSpace(&sb);
       
101124     append(&sb, zEllipsis);
       
101125   }
       
101126   pCursor->snippet.zSnippet = stringBufferData(&sb);
       
101127   pCursor->snippet.nSnippet = stringBufferLength(&sb);
       
101128 }
       
101129 
       
101130 
       
101131 /*
       
101132 ** Close the cursor.  For additional information see the documentation
       
101133 ** on the xClose method of the virtual table interface.
       
101134 */
       
101135 static int fulltextClose(sqlite3_vtab_cursor *pCursor){
       
101136   fulltext_cursor *c = (fulltext_cursor *) pCursor;
       
101137   FTSTRACE(("FTS3 Close %p\n", c));
       
101138   sqlite3_finalize(c->pStmt);
       
101139   sqlite3Fts3ExprFree(c->pExpr);
       
101140   snippetClear(&c->snippet);
       
101141   if( c->result.nData!=0 ){
       
101142     dlrDestroy(&c->reader);
       
101143   }
       
101144   dataBufferDestroy(&c->result);
       
101145   sqlite3_free(c);
       
101146   return SQLITE_OK;
       
101147 }
       
101148 
       
101149 static int fulltextNext(sqlite3_vtab_cursor *pCursor){
       
101150   fulltext_cursor *c = (fulltext_cursor *) pCursor;
       
101151   int rc;
       
101152 
       
101153   FTSTRACE(("FTS3 Next %p\n", pCursor));
       
101154   snippetClear(&c->snippet);
       
101155   if( c->iCursorType < QUERY_FULLTEXT ){
       
101156     /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */
       
101157     rc = sqlite3_step(c->pStmt);
       
101158     switch( rc ){
       
101159       case SQLITE_ROW:
       
101160         c->eof = 0;
       
101161         return SQLITE_OK;
       
101162       case SQLITE_DONE:
       
101163         c->eof = 1;
       
101164         return SQLITE_OK;
       
101165       default:
       
101166         c->eof = 1;
       
101167         return rc;
       
101168     }
       
101169   } else {  /* full-text query */
       
101170     rc = sqlite3_reset(c->pStmt);
       
101171     if( rc!=SQLITE_OK ) return rc;
       
101172 
       
101173     if( c->result.nData==0 || dlrAtEnd(&c->reader) ){
       
101174       c->eof = 1;
       
101175       return SQLITE_OK;
       
101176     }
       
101177     rc = sqlite3_bind_int64(c->pStmt, 1, dlrDocid(&c->reader));
       
101178     dlrStep(&c->reader);
       
101179     if( rc!=SQLITE_OK ) return rc;
       
101180     /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */
       
101181     rc = sqlite3_step(c->pStmt);
       
101182     if( rc==SQLITE_ROW ){   /* the case we expect */
       
101183       c->eof = 0;
       
101184       return SQLITE_OK;
       
101185     }
       
101186     /* an error occurred; abort */
       
101187     return rc==SQLITE_DONE ? SQLITE_ERROR : rc;
       
101188   }
       
101189 }
       
101190 
       
101191 
       
101192 /* TODO(shess) If we pushed LeafReader to the top of the file, or to
       
101193 ** another file, term_select() could be pushed above
       
101194 ** docListOfTerm().
       
101195 */
       
101196 static int termSelect(fulltext_vtab *v, int iColumn,
       
101197                       const char *pTerm, int nTerm, int isPrefix,
       
101198                       DocListType iType, DataBuffer *out);
       
101199 
       
101200 /* 
       
101201 ** Return a DocList corresponding to the phrase *pPhrase.
       
101202 **
       
101203 ** The resulting DL_DOCIDS doclist is stored in pResult, which is
       
101204 ** overwritten.
       
101205 */
       
101206 static int docListOfPhrase(
       
101207   fulltext_vtab *pTab,   /* The full text index */
       
101208   Fts3Phrase *pPhrase,   /* Phrase to return a doclist corresponding to */
       
101209   DocListType eListType, /* Either DL_DOCIDS or DL_POSITIONS */
       
101210   DataBuffer *pResult    /* Write the result here */
       
101211 ){
       
101212   int ii;
       
101213   int rc = SQLITE_OK;
       
101214   int iCol = pPhrase->iColumn;
       
101215   DocListType eType = eListType;
       
101216   assert( eType==DL_POSITIONS || eType==DL_DOCIDS );
       
101217   if( pPhrase->nToken>1 ){
       
101218     eType = DL_POSITIONS;
       
101219   }
       
101220 
       
101221   /* This code should never be called with buffered updates. */
       
101222   assert( pTab->nPendingData<0 );
       
101223 
       
101224   for(ii=0; rc==SQLITE_OK && ii<pPhrase->nToken; ii++){
       
101225     DataBuffer tmp;
       
101226     struct PhraseToken *p = &pPhrase->aToken[ii];
       
101227     rc = termSelect(pTab, iCol, p->z, p->n, p->isPrefix, eType, &tmp);
       
101228     if( rc==SQLITE_OK ){
       
101229       if( ii==0 ){
       
101230         *pResult = tmp;
       
101231       }else{
       
101232         DataBuffer res = *pResult;
       
101233         dataBufferInit(pResult, 0);
       
101234         if( ii==(pPhrase->nToken-1) ){
       
101235           eType = eListType;
       
101236         }
       
101237         docListPhraseMerge(
       
101238           res.pData, res.nData, tmp.pData, tmp.nData, 0, 0, eType, pResult
       
101239         );
       
101240         dataBufferDestroy(&res);
       
101241         dataBufferDestroy(&tmp);
       
101242       }
       
101243     }
       
101244   }
       
101245 
       
101246   return rc;
       
101247 }
       
101248 
       
101249 /*
       
101250 ** Evaluate the full-text expression pExpr against fts3 table pTab. Write
       
101251 ** the results into pRes.
       
101252 */
       
101253 static int evalFts3Expr(
       
101254   fulltext_vtab *pTab,           /* Fts3 Virtual table object */
       
101255   Fts3Expr *pExpr,               /* Parsed fts3 expression */
       
101256   DataBuffer *pRes               /* OUT: Write results of the expression here */
       
101257 ){
       
101258   int rc = SQLITE_OK;
       
101259 
       
101260   /* Initialize the output buffer. If this is an empty query (pExpr==0), 
       
101261   ** this is all that needs to be done. Empty queries produce empty 
       
101262   ** result sets.
       
101263   */
       
101264   dataBufferInit(pRes, 0);
       
101265 
       
101266   if( pExpr ){
       
101267     if( pExpr->eType==FTSQUERY_PHRASE ){
       
101268       DocListType eType = DL_DOCIDS;
       
101269       if( pExpr->pParent && pExpr->pParent->eType==FTSQUERY_NEAR ){
       
101270         eType = DL_POSITIONS;
       
101271       }
       
101272       rc = docListOfPhrase(pTab, pExpr->pPhrase, eType, pRes);
       
101273     }else{
       
101274       DataBuffer lhs;
       
101275       DataBuffer rhs;
       
101276 
       
101277       dataBufferInit(&rhs, 0);
       
101278       if( SQLITE_OK==(rc = evalFts3Expr(pTab, pExpr->pLeft, &lhs)) 
       
101279        && SQLITE_OK==(rc = evalFts3Expr(pTab, pExpr->pRight, &rhs)) 
       
101280       ){
       
101281         switch( pExpr->eType ){
       
101282           case FTSQUERY_NEAR: {
       
101283             int nToken;
       
101284             Fts3Expr *pLeft;
       
101285             DocListType eType = DL_DOCIDS;
       
101286             if( pExpr->pParent && pExpr->pParent->eType==FTSQUERY_NEAR ){
       
101287               eType = DL_POSITIONS;
       
101288             }
       
101289             pLeft = pExpr->pLeft;
       
101290             while( pLeft->eType==FTSQUERY_NEAR ){ 
       
101291               pLeft=pLeft->pRight;
       
101292             }
       
101293             assert( pExpr->pRight->eType==FTSQUERY_PHRASE );
       
101294             assert( pLeft->eType==FTSQUERY_PHRASE );
       
101295             nToken = pLeft->pPhrase->nToken + pExpr->pRight->pPhrase->nToken;
       
101296             docListPhraseMerge(lhs.pData, lhs.nData, rhs.pData, rhs.nData, 
       
101297                 pExpr->nNear+1, nToken, eType, pRes
       
101298             );
       
101299             break;
       
101300           }
       
101301           case FTSQUERY_NOT: {
       
101302             docListExceptMerge(lhs.pData, lhs.nData, rhs.pData, rhs.nData,pRes);
       
101303             break;
       
101304           }
       
101305           case FTSQUERY_AND: {
       
101306             docListAndMerge(lhs.pData, lhs.nData, rhs.pData, rhs.nData, pRes);
       
101307             break;
       
101308           }
       
101309           case FTSQUERY_OR: {
       
101310             docListOrMerge(lhs.pData, lhs.nData, rhs.pData, rhs.nData, pRes);
       
101311             break;
       
101312           }
       
101313         }
       
101314       }
       
101315       dataBufferDestroy(&lhs);
       
101316       dataBufferDestroy(&rhs);
       
101317     }
       
101318   }
       
101319 
       
101320   return rc;
       
101321 }
       
101322 
       
101323 /* TODO(shess) Refactor the code to remove this forward decl. */
       
101324 static int flushPendingTerms(fulltext_vtab *v);
       
101325 
       
101326 /* Perform a full-text query using the search expression in
       
101327 ** zInput[0..nInput-1].  Return a list of matching documents
       
101328 ** in pResult.
       
101329 **
       
101330 ** Queries must match column iColumn.  Or if iColumn>=nColumn
       
101331 ** they are allowed to match against any column.
       
101332 */
       
101333 static int fulltextQuery(
       
101334   fulltext_vtab *v,      /* The full text index */
       
101335   int iColumn,           /* Match against this column by default */
       
101336   const char *zInput,    /* The query string */
       
101337   int nInput,            /* Number of bytes in zInput[] */
       
101338   DataBuffer *pResult,   /* Write the result doclist here */
       
101339   Fts3Expr **ppExpr        /* Put parsed query string here */
       
101340 ){
       
101341   int rc;
       
101342 
       
101343   /* TODO(shess) Instead of flushing pendingTerms, we could query for
       
101344   ** the relevant term and merge the doclist into what we receive from
       
101345   ** the database.  Wait and see if this is a common issue, first.
       
101346   **
       
101347   ** A good reason not to flush is to not generate update-related
       
101348   ** error codes from here.
       
101349   */
       
101350 
       
101351   /* Flush any buffered updates before executing the query. */
       
101352   rc = flushPendingTerms(v);
       
101353   if( rc!=SQLITE_OK ){
       
101354     return rc;
       
101355   }
       
101356 
       
101357   /* Parse the query passed to the MATCH operator. */
       
101358   rc = sqlite3Fts3ExprParse(v->pTokenizer, 
       
101359       v->azColumn, v->nColumn, iColumn, zInput, nInput, ppExpr
       
101360   );
       
101361   if( rc!=SQLITE_OK ){
       
101362     assert( 0==(*ppExpr) );
       
101363     return rc;
       
101364   }
       
101365 
       
101366   return evalFts3Expr(v, *ppExpr, pResult);
       
101367 }
       
101368 
       
101369 /*
       
101370 ** This is the xFilter interface for the virtual table.  See
       
101371 ** the virtual table xFilter method documentation for additional
       
101372 ** information.
       
101373 **
       
101374 ** If idxNum==QUERY_GENERIC then do a full table scan against
       
101375 ** the %_content table.
       
101376 **
       
101377 ** If idxNum==QUERY_DOCID then do a docid lookup for a single entry
       
101378 ** in the %_content table.
       
101379 **
       
101380 ** If idxNum>=QUERY_FULLTEXT then use the full text index.  The
       
101381 ** column on the left-hand side of the MATCH operator is column
       
101382 ** number idxNum-QUERY_FULLTEXT, 0 indexed.  argv[0] is the right-hand
       
101383 ** side of the MATCH operator.
       
101384 */
       
101385 /* TODO(shess) Upgrade the cursor initialization and destruction to
       
101386 ** account for fulltextFilter() being called multiple times on the
       
101387 ** same cursor.  The current solution is very fragile.  Apply fix to
       
101388 ** fts3 as appropriate.
       
101389 */
       
101390 static int fulltextFilter(
       
101391   sqlite3_vtab_cursor *pCursor,     /* The cursor used for this query */
       
101392   int idxNum, const char *idxStr,   /* Which indexing scheme to use */
       
101393   int argc, sqlite3_value **argv    /* Arguments for the indexing scheme */
       
101394 ){
       
101395   fulltext_cursor *c = (fulltext_cursor *) pCursor;
       
101396   fulltext_vtab *v = cursor_vtab(c);
       
101397   int rc;
       
101398 
       
101399   FTSTRACE(("FTS3 Filter %p\n",pCursor));
       
101400 
       
101401   /* If the cursor has a statement that was not prepared according to
       
101402   ** idxNum, clear it.  I believe all calls to fulltextFilter with a
       
101403   ** given cursor will have the same idxNum , but in this case it's
       
101404   ** easy to be safe.
       
101405   */
       
101406   if( c->pStmt && c->iCursorType!=idxNum ){
       
101407     sqlite3_finalize(c->pStmt);
       
101408     c->pStmt = NULL;
       
101409   }
       
101410 
       
101411   /* Get a fresh statement appropriate to idxNum. */
       
101412   /* TODO(shess): Add a prepared-statement cache in the vt structure.
       
101413   ** The cache must handle multiple open cursors.  Easier to cache the
       
101414   ** statement variants at the vt to reduce malloc/realloc/free here.
       
101415   ** Or we could have a StringBuffer variant which allowed stack
       
101416   ** construction for small values.
       
101417   */
       
101418   if( !c->pStmt ){
       
101419     StringBuffer sb;
       
101420     initStringBuffer(&sb);
       
101421     append(&sb, "SELECT docid, ");
       
101422     appendList(&sb, v->nColumn, v->azContentColumn);
       
101423     append(&sb, " FROM %_content");
       
101424     if( idxNum!=QUERY_GENERIC ) append(&sb, " WHERE docid = ?");
       
101425     rc = sql_prepare(v->db, v->zDb, v->zName, &c->pStmt,
       
101426                      stringBufferData(&sb));
       
101427     stringBufferDestroy(&sb);
       
101428     if( rc!=SQLITE_OK ) return rc;
       
101429     c->iCursorType = idxNum;
       
101430   }else{
       
101431     sqlite3_reset(c->pStmt);
       
101432     assert( c->iCursorType==idxNum );
       
101433   }
       
101434 
       
101435   switch( idxNum ){
       
101436     case QUERY_GENERIC:
       
101437       break;
       
101438 
       
101439     case QUERY_DOCID:
       
101440       rc = sqlite3_bind_int64(c->pStmt, 1, sqlite3_value_int64(argv[0]));
       
101441       if( rc!=SQLITE_OK ) return rc;
       
101442       break;
       
101443 
       
101444     default:   /* full-text search */
       
101445     {
       
101446       int iCol = idxNum-QUERY_FULLTEXT;
       
101447       const char *zQuery = (const char *)sqlite3_value_text(argv[0]);
       
101448       assert( idxNum<=QUERY_FULLTEXT+v->nColumn);
       
101449       assert( argc==1 );
       
101450       if( c->result.nData!=0 ){
       
101451         /* This case happens if the same cursor is used repeatedly. */
       
101452         dlrDestroy(&c->reader);
       
101453         dataBufferReset(&c->result);
       
101454       }else{
       
101455         dataBufferInit(&c->result, 0);
       
101456       }
       
101457       rc = fulltextQuery(v, iCol, zQuery, -1, &c->result, &c->pExpr);
       
101458       if( rc!=SQLITE_OK ) return rc;
       
101459       if( c->result.nData!=0 ){
       
101460         dlrInit(&c->reader, DL_DOCIDS, c->result.pData, c->result.nData);
       
101461       }
       
101462       break;
       
101463     }
       
101464   }
       
101465 
       
101466   return fulltextNext(pCursor);
       
101467 }
       
101468 
       
101469 /* This is the xEof method of the virtual table.  The SQLite core
       
101470 ** calls this routine to find out if it has reached the end of
       
101471 ** a query's results set.
       
101472 */
       
101473 static int fulltextEof(sqlite3_vtab_cursor *pCursor){
       
101474   fulltext_cursor *c = (fulltext_cursor *) pCursor;
       
101475   return c->eof;
       
101476 }
       
101477 
       
101478 /* This is the xColumn method of the virtual table.  The SQLite
       
101479 ** core calls this method during a query when it needs the value
       
101480 ** of a column from the virtual table.  This method needs to use
       
101481 ** one of the sqlite3_result_*() routines to store the requested
       
101482 ** value back in the pContext.
       
101483 */
       
101484 static int fulltextColumn(sqlite3_vtab_cursor *pCursor,
       
101485                           sqlite3_context *pContext, int idxCol){
       
101486   fulltext_cursor *c = (fulltext_cursor *) pCursor;
       
101487   fulltext_vtab *v = cursor_vtab(c);
       
101488 
       
101489   if( idxCol<v->nColumn ){
       
101490     sqlite3_value *pVal = sqlite3_column_value(c->pStmt, idxCol+1);
       
101491     sqlite3_result_value(pContext, pVal);
       
101492   }else if( idxCol==v->nColumn ){
       
101493     /* The extra column whose name is the same as the table.
       
101494     ** Return a blob which is a pointer to the cursor
       
101495     */
       
101496     sqlite3_result_blob(pContext, &c, sizeof(c), SQLITE_TRANSIENT);
       
101497   }else if( idxCol==v->nColumn+1 ){
       
101498     /* The docid column, which is an alias for rowid. */
       
101499     sqlite3_value *pVal = sqlite3_column_value(c->pStmt, 0);
       
101500     sqlite3_result_value(pContext, pVal);
       
101501   }
       
101502   return SQLITE_OK;
       
101503 }
       
101504 
       
101505 /* This is the xRowid method.  The SQLite core calls this routine to
       
101506 ** retrieve the rowid for the current row of the result set.  fts3
       
101507 ** exposes %_content.docid as the rowid for the virtual table.  The
       
101508 ** rowid should be written to *pRowid.
       
101509 */
       
101510 static int fulltextRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
       
101511   fulltext_cursor *c = (fulltext_cursor *) pCursor;
       
101512 
       
101513   *pRowid = sqlite3_column_int64(c->pStmt, 0);
       
101514   return SQLITE_OK;
       
101515 }
       
101516 
       
101517 /* Add all terms in [zText] to pendingTerms table.  If [iColumn] > 0,
       
101518 ** we also store positions and offsets in the hash table using that
       
101519 ** column number.
       
101520 */
       
101521 static int buildTerms(fulltext_vtab *v, sqlite_int64 iDocid,
       
101522                       const char *zText, int iColumn){
       
101523   sqlite3_tokenizer *pTokenizer = v->pTokenizer;
       
101524   sqlite3_tokenizer_cursor *pCursor;
       
101525   const char *pToken;
       
101526   int nTokenBytes;
       
101527   int iStartOffset, iEndOffset, iPosition;
       
101528   int rc;
       
101529 
       
101530   rc = pTokenizer->pModule->xOpen(pTokenizer, zText, -1, &pCursor);
       
101531   if( rc!=SQLITE_OK ) return rc;
       
101532 
       
101533   pCursor->pTokenizer = pTokenizer;
       
101534   while( SQLITE_OK==(rc=pTokenizer->pModule->xNext(pCursor,
       
101535                                                    &pToken, &nTokenBytes,
       
101536                                                    &iStartOffset, &iEndOffset,
       
101537                                                    &iPosition)) ){
       
101538     DLCollector *p;
       
101539     int nData;                   /* Size of doclist before our update. */
       
101540 
       
101541     /* Positions can't be negative; we use -1 as a terminator
       
101542      * internally.  Token can't be NULL or empty. */
       
101543     if( iPosition<0 || pToken == NULL || nTokenBytes == 0 ){
       
101544       rc = SQLITE_ERROR;
       
101545       break;
       
101546     }
       
101547 
       
101548     p = fts3HashFind(&v->pendingTerms, pToken, nTokenBytes);
       
101549     if( p==NULL ){
       
101550       nData = 0;
       
101551       p = dlcNew(iDocid, DL_DEFAULT);
       
101552       fts3HashInsert(&v->pendingTerms, pToken, nTokenBytes, p);
       
101553 
       
101554       /* Overhead for our hash table entry, the key, and the value. */
       
101555       v->nPendingData += sizeof(struct fts3HashElem)+sizeof(*p)+nTokenBytes;
       
101556     }else{
       
101557       nData = p->b.nData;
       
101558       if( p->dlw.iPrevDocid!=iDocid ) dlcNext(p, iDocid);
       
101559     }
       
101560     if( iColumn>=0 ){
       
101561       dlcAddPos(p, iColumn, iPosition, iStartOffset, iEndOffset);
       
101562     }
       
101563 
       
101564     /* Accumulate data added by dlcNew or dlcNext, and dlcAddPos. */
       
101565     v->nPendingData += p->b.nData-nData;
       
101566   }
       
101567 
       
101568   /* TODO(shess) Check return?  Should this be able to cause errors at
       
101569   ** this point?  Actually, same question about sqlite3_finalize(),
       
101570   ** though one could argue that failure there means that the data is
       
101571   ** not durable.  *ponder*
       
101572   */
       
101573   pTokenizer->pModule->xClose(pCursor);
       
101574   if( SQLITE_DONE == rc ) return SQLITE_OK;
       
101575   return rc;
       
101576 }
       
101577 
       
101578 /* Add doclists for all terms in [pValues] to pendingTerms table. */
       
101579 static int insertTerms(fulltext_vtab *v, sqlite_int64 iDocid,
       
101580                        sqlite3_value **pValues){
       
101581   int i;
       
101582   for(i = 0; i < v->nColumn ; ++i){
       
101583     char *zText = (char*)sqlite3_value_text(pValues[i]);
       
101584     int rc = buildTerms(v, iDocid, zText, i);
       
101585     if( rc!=SQLITE_OK ) return rc;
       
101586   }
       
101587   return SQLITE_OK;
       
101588 }
       
101589 
       
101590 /* Add empty doclists for all terms in the given row's content to
       
101591 ** pendingTerms.
       
101592 */
       
101593 static int deleteTerms(fulltext_vtab *v, sqlite_int64 iDocid){
       
101594   const char **pValues;
       
101595   int i, rc;
       
101596 
       
101597   /* TODO(shess) Should we allow such tables at all? */
       
101598   if( DL_DEFAULT==DL_DOCIDS ) return SQLITE_ERROR;
       
101599 
       
101600   rc = content_select(v, iDocid, &pValues);
       
101601   if( rc!=SQLITE_OK ) return rc;
       
101602 
       
101603   for(i = 0 ; i < v->nColumn; ++i) {
       
101604     rc = buildTerms(v, iDocid, pValues[i], -1);
       
101605     if( rc!=SQLITE_OK ) break;
       
101606   }
       
101607 
       
101608   freeStringArray(v->nColumn, pValues);
       
101609   return SQLITE_OK;
       
101610 }
       
101611 
       
101612 /* TODO(shess) Refactor the code to remove this forward decl. */
       
101613 static int initPendingTerms(fulltext_vtab *v, sqlite_int64 iDocid);
       
101614 
       
101615 /* Insert a row into the %_content table; set *piDocid to be the ID of the
       
101616 ** new row.  Add doclists for terms to pendingTerms.
       
101617 */
       
101618 static int index_insert(fulltext_vtab *v, sqlite3_value *pRequestDocid,
       
101619                         sqlite3_value **pValues, sqlite_int64 *piDocid){
       
101620   int rc;
       
101621 
       
101622   rc = content_insert(v, pRequestDocid, pValues);  /* execute an SQL INSERT */
       
101623   if( rc!=SQLITE_OK ) return rc;
       
101624 
       
101625   /* docid column is an alias for rowid. */
       
101626   *piDocid = sqlite3_last_insert_rowid(v->db);
       
101627   rc = initPendingTerms(v, *piDocid);
       
101628   if( rc!=SQLITE_OK ) return rc;
       
101629 
       
101630   return insertTerms(v, *piDocid, pValues);
       
101631 }
       
101632 
       
101633 /* Delete a row from the %_content table; add empty doclists for terms
       
101634 ** to pendingTerms.
       
101635 */
       
101636 static int index_delete(fulltext_vtab *v, sqlite_int64 iRow){
       
101637   int rc = initPendingTerms(v, iRow);
       
101638   if( rc!=SQLITE_OK ) return rc;
       
101639 
       
101640   rc = deleteTerms(v, iRow);
       
101641   if( rc!=SQLITE_OK ) return rc;
       
101642 
       
101643   return content_delete(v, iRow);  /* execute an SQL DELETE */
       
101644 }
       
101645 
       
101646 /* Update a row in the %_content table; add delete doclists to
       
101647 ** pendingTerms for old terms not in the new data, add insert doclists
       
101648 ** to pendingTerms for terms in the new data.
       
101649 */
       
101650 static int index_update(fulltext_vtab *v, sqlite_int64 iRow,
       
101651                         sqlite3_value **pValues){
       
101652   int rc = initPendingTerms(v, iRow);
       
101653   if( rc!=SQLITE_OK ) return rc;
       
101654 
       
101655   /* Generate an empty doclist for each term that previously appeared in this
       
101656    * row. */
       
101657   rc = deleteTerms(v, iRow);
       
101658   if( rc!=SQLITE_OK ) return rc;
       
101659 
       
101660   rc = content_update(v, pValues, iRow);  /* execute an SQL UPDATE */
       
101661   if( rc!=SQLITE_OK ) return rc;
       
101662 
       
101663   /* Now add positions for terms which appear in the updated row. */
       
101664   return insertTerms(v, iRow, pValues);
       
101665 }
       
101666 
       
101667 /*******************************************************************/
       
101668 /* InteriorWriter is used to collect terms and block references into
       
101669 ** interior nodes in %_segments.  See commentary at top of file for
       
101670 ** format.
       
101671 */
       
101672 
       
101673 /* How large interior nodes can grow. */
       
101674 #define INTERIOR_MAX 2048
       
101675 
       
101676 /* Minimum number of terms per interior node (except the root). This
       
101677 ** prevents large terms from making the tree too skinny - must be >0
       
101678 ** so that the tree always makes progress.  Note that the min tree
       
101679 ** fanout will be INTERIOR_MIN_TERMS+1.
       
101680 */
       
101681 #define INTERIOR_MIN_TERMS 7
       
101682 #if INTERIOR_MIN_TERMS<1
       
101683 # error INTERIOR_MIN_TERMS must be greater than 0.
       
101684 #endif
       
101685 
       
101686 /* ROOT_MAX controls how much data is stored inline in the segment
       
101687 ** directory.
       
101688 */
       
101689 /* TODO(shess) Push ROOT_MAX down to whoever is writing things.  It's
       
101690 ** only here so that interiorWriterRootInfo() and leafWriterRootInfo()
       
101691 ** can both see it, but if the caller passed it in, we wouldn't even
       
101692 ** need a define.
       
101693 */
       
101694 #define ROOT_MAX 1024
       
101695 #if ROOT_MAX<VARINT_MAX*2
       
101696 # error ROOT_MAX must have enough space for a header.
       
101697 #endif
       
101698 
       
101699 /* InteriorBlock stores a linked-list of interior blocks while a lower
       
101700 ** layer is being constructed.
       
101701 */
       
101702 typedef struct InteriorBlock {
       
101703   DataBuffer term;           /* Leftmost term in block's subtree. */
       
101704   DataBuffer data;           /* Accumulated data for the block. */
       
101705   struct InteriorBlock *next;
       
101706 } InteriorBlock;
       
101707 
       
101708 static InteriorBlock *interiorBlockNew(int iHeight, sqlite_int64 iChildBlock,
       
101709                                        const char *pTerm, int nTerm){
       
101710   InteriorBlock *block = sqlite3_malloc(sizeof(InteriorBlock));
       
101711   char c[VARINT_MAX+VARINT_MAX];
       
101712   int n;
       
101713 
       
101714   if( block ){
       
101715     memset(block, 0, sizeof(*block));
       
101716     dataBufferInit(&block->term, 0);
       
101717     dataBufferReplace(&block->term, pTerm, nTerm);
       
101718 
       
101719     n = fts3PutVarint(c, iHeight);
       
101720     n += fts3PutVarint(c+n, iChildBlock);
       
101721     dataBufferInit(&block->data, INTERIOR_MAX);
       
101722     dataBufferReplace(&block->data, c, n);
       
101723   }
       
101724   return block;
       
101725 }
       
101726 
       
101727 #ifndef NDEBUG
       
101728 /* Verify that the data is readable as an interior node. */
       
101729 static void interiorBlockValidate(InteriorBlock *pBlock){
       
101730   const char *pData = pBlock->data.pData;
       
101731   int nData = pBlock->data.nData;
       
101732   int n, iDummy;
       
101733   sqlite_int64 iBlockid;
       
101734 
       
101735   assert( nData>0 );
       
101736   assert( pData!=0 );
       
101737   assert( pData+nData>pData );
       
101738 
       
101739   /* Must lead with height of node as a varint(n), n>0 */
       
101740   n = fts3GetVarint32(pData, &iDummy);
       
101741   assert( n>0 );
       
101742   assert( iDummy>0 );
       
101743   assert( n<nData );
       
101744   pData += n;
       
101745   nData -= n;
       
101746 
       
101747   /* Must contain iBlockid. */
       
101748   n = fts3GetVarint(pData, &iBlockid);
       
101749   assert( n>0 );
       
101750   assert( n<=nData );
       
101751   pData += n;
       
101752   nData -= n;
       
101753 
       
101754   /* Zero or more terms of positive length */
       
101755   if( nData!=0 ){
       
101756     /* First term is not delta-encoded. */
       
101757     n = fts3GetVarint32(pData, &iDummy);
       
101758     assert( n>0 );
       
101759     assert( iDummy>0 );
       
101760     assert( n+iDummy>0);
       
101761     assert( n+iDummy<=nData );
       
101762     pData += n+iDummy;
       
101763     nData -= n+iDummy;
       
101764 
       
101765     /* Following terms delta-encoded. */
       
101766     while( nData!=0 ){
       
101767       /* Length of shared prefix. */
       
101768       n = fts3GetVarint32(pData, &iDummy);
       
101769       assert( n>0 );
       
101770       assert( iDummy>=0 );
       
101771       assert( n<nData );
       
101772       pData += n;
       
101773       nData -= n;
       
101774 
       
101775       /* Length and data of distinct suffix. */
       
101776       n = fts3GetVarint32(pData, &iDummy);
       
101777       assert( n>0 );
       
101778       assert( iDummy>0 );
       
101779       assert( n+iDummy>0);
       
101780       assert( n+iDummy<=nData );
       
101781       pData += n+iDummy;
       
101782       nData -= n+iDummy;
       
101783     }
       
101784   }
       
101785 }
       
101786 #define ASSERT_VALID_INTERIOR_BLOCK(x) interiorBlockValidate(x)
       
101787 #else
       
101788 #define ASSERT_VALID_INTERIOR_BLOCK(x) assert( 1 )
       
101789 #endif
       
101790 
       
101791 typedef struct InteriorWriter {
       
101792   int iHeight;                   /* from 0 at leaves. */
       
101793   InteriorBlock *first, *last;
       
101794   struct InteriorWriter *parentWriter;
       
101795 
       
101796   DataBuffer term;               /* Last term written to block "last". */
       
101797   sqlite_int64 iOpeningChildBlock; /* First child block in block "last". */
       
101798 #ifndef NDEBUG
       
101799   sqlite_int64 iLastChildBlock;  /* for consistency checks. */
       
101800 #endif
       
101801 } InteriorWriter;
       
101802 
       
101803 /* Initialize an interior node where pTerm[nTerm] marks the leftmost
       
101804 ** term in the tree.  iChildBlock is the leftmost child block at the
       
101805 ** next level down the tree.
       
101806 */
       
101807 static void interiorWriterInit(int iHeight, const char *pTerm, int nTerm,
       
101808                                sqlite_int64 iChildBlock,
       
101809                                InteriorWriter *pWriter){
       
101810   InteriorBlock *block;
       
101811   assert( iHeight>0 );
       
101812   CLEAR(pWriter);
       
101813 
       
101814   pWriter->iHeight = iHeight;
       
101815   pWriter->iOpeningChildBlock = iChildBlock;
       
101816 #ifndef NDEBUG
       
101817   pWriter->iLastChildBlock = iChildBlock;
       
101818 #endif
       
101819   block = interiorBlockNew(iHeight, iChildBlock, pTerm, nTerm);
       
101820   pWriter->last = pWriter->first = block;
       
101821   ASSERT_VALID_INTERIOR_BLOCK(pWriter->last);
       
101822   dataBufferInit(&pWriter->term, 0);
       
101823 }
       
101824 
       
101825 /* Append the child node rooted at iChildBlock to the interior node,
       
101826 ** with pTerm[nTerm] as the leftmost term in iChildBlock's subtree.
       
101827 */
       
101828 static void interiorWriterAppend(InteriorWriter *pWriter,
       
101829                                  const char *pTerm, int nTerm,
       
101830                                  sqlite_int64 iChildBlock){
       
101831   char c[VARINT_MAX+VARINT_MAX];
       
101832   int n, nPrefix = 0;
       
101833 
       
101834   ASSERT_VALID_INTERIOR_BLOCK(pWriter->last);
       
101835 
       
101836   /* The first term written into an interior node is actually
       
101837   ** associated with the second child added (the first child was added
       
101838   ** in interiorWriterInit, or in the if clause at the bottom of this
       
101839   ** function).  That term gets encoded straight up, with nPrefix left
       
101840   ** at 0.
       
101841   */
       
101842   if( pWriter->term.nData==0 ){
       
101843     n = fts3PutVarint(c, nTerm);
       
101844   }else{
       
101845     while( nPrefix<pWriter->term.nData &&
       
101846            pTerm[nPrefix]==pWriter->term.pData[nPrefix] ){
       
101847       nPrefix++;
       
101848     }
       
101849 
       
101850     n = fts3PutVarint(c, nPrefix);
       
101851     n += fts3PutVarint(c+n, nTerm-nPrefix);
       
101852   }
       
101853 
       
101854 #ifndef NDEBUG
       
101855   pWriter->iLastChildBlock++;
       
101856 #endif
       
101857   assert( pWriter->iLastChildBlock==iChildBlock );
       
101858 
       
101859   /* Overflow to a new block if the new term makes the current block
       
101860   ** too big, and the current block already has enough terms.
       
101861   */
       
101862   if( pWriter->last->data.nData+n+nTerm-nPrefix>INTERIOR_MAX &&
       
101863       iChildBlock-pWriter->iOpeningChildBlock>INTERIOR_MIN_TERMS ){
       
101864     pWriter->last->next = interiorBlockNew(pWriter->iHeight, iChildBlock,
       
101865                                            pTerm, nTerm);
       
101866     pWriter->last = pWriter->last->next;
       
101867     pWriter->iOpeningChildBlock = iChildBlock;
       
101868     dataBufferReset(&pWriter->term);
       
101869   }else{
       
101870     dataBufferAppend2(&pWriter->last->data, c, n,
       
101871                       pTerm+nPrefix, nTerm-nPrefix);
       
101872     dataBufferReplace(&pWriter->term, pTerm, nTerm);
       
101873   }
       
101874   ASSERT_VALID_INTERIOR_BLOCK(pWriter->last);
       
101875 }
       
101876 
       
101877 /* Free the space used by pWriter, including the linked-list of
       
101878 ** InteriorBlocks, and parentWriter, if present.
       
101879 */
       
101880 static int interiorWriterDestroy(InteriorWriter *pWriter){
       
101881   InteriorBlock *block = pWriter->first;
       
101882 
       
101883   while( block!=NULL ){
       
101884     InteriorBlock *b = block;
       
101885     block = block->next;
       
101886     dataBufferDestroy(&b->term);
       
101887     dataBufferDestroy(&b->data);
       
101888     sqlite3_free(b);
       
101889   }
       
101890   if( pWriter->parentWriter!=NULL ){
       
101891     interiorWriterDestroy(pWriter->parentWriter);
       
101892     sqlite3_free(pWriter->parentWriter);
       
101893   }
       
101894   dataBufferDestroy(&pWriter->term);
       
101895   SCRAMBLE(pWriter);
       
101896   return SQLITE_OK;
       
101897 }
       
101898 
       
101899 /* If pWriter can fit entirely in ROOT_MAX, return it as the root info
       
101900 ** directly, leaving *piEndBlockid unchanged.  Otherwise, flush
       
101901 ** pWriter to %_segments, building a new layer of interior nodes, and
       
101902 ** recursively ask for their root into.
       
101903 */
       
101904 static int interiorWriterRootInfo(fulltext_vtab *v, InteriorWriter *pWriter,
       
101905                                   char **ppRootInfo, int *pnRootInfo,
       
101906                                   sqlite_int64 *piEndBlockid){
       
101907   InteriorBlock *block = pWriter->first;
       
101908   sqlite_int64 iBlockid = 0;
       
101909   int rc;
       
101910 
       
101911   /* If we can fit the segment inline */
       
101912   if( block==pWriter->last && block->data.nData<ROOT_MAX ){
       
101913     *ppRootInfo = block->data.pData;
       
101914     *pnRootInfo = block->data.nData;
       
101915     return SQLITE_OK;
       
101916   }
       
101917 
       
101918   /* Flush the first block to %_segments, and create a new level of
       
101919   ** interior node.
       
101920   */
       
101921   ASSERT_VALID_INTERIOR_BLOCK(block);
       
101922   rc = block_insert(v, block->data.pData, block->data.nData, &iBlockid);
       
101923   if( rc!=SQLITE_OK ) return rc;
       
101924   *piEndBlockid = iBlockid;
       
101925 
       
101926   pWriter->parentWriter = sqlite3_malloc(sizeof(*pWriter->parentWriter));
       
101927   interiorWriterInit(pWriter->iHeight+1,
       
101928                      block->term.pData, block->term.nData,
       
101929                      iBlockid, pWriter->parentWriter);
       
101930 
       
101931   /* Flush additional blocks and append to the higher interior
       
101932   ** node.
       
101933   */
       
101934   for(block=block->next; block!=NULL; block=block->next){
       
101935     ASSERT_VALID_INTERIOR_BLOCK(block);
       
101936     rc = block_insert(v, block->data.pData, block->data.nData, &iBlockid);
       
101937     if( rc!=SQLITE_OK ) return rc;
       
101938     *piEndBlockid = iBlockid;
       
101939 
       
101940     interiorWriterAppend(pWriter->parentWriter,
       
101941                          block->term.pData, block->term.nData, iBlockid);
       
101942   }
       
101943 
       
101944   /* Parent node gets the chance to be the root. */
       
101945   return interiorWriterRootInfo(v, pWriter->parentWriter,
       
101946                                 ppRootInfo, pnRootInfo, piEndBlockid);
       
101947 }
       
101948 
       
101949 /****************************************************************/
       
101950 /* InteriorReader is used to read off the data from an interior node
       
101951 ** (see comment at top of file for the format).
       
101952 */
       
101953 typedef struct InteriorReader {
       
101954   const char *pData;
       
101955   int nData;
       
101956 
       
101957   DataBuffer term;          /* previous term, for decoding term delta. */
       
101958 
       
101959   sqlite_int64 iBlockid;
       
101960 } InteriorReader;
       
101961 
       
101962 static void interiorReaderDestroy(InteriorReader *pReader){
       
101963   dataBufferDestroy(&pReader->term);
       
101964   SCRAMBLE(pReader);
       
101965 }
       
101966 
       
101967 /* TODO(shess) The assertions are great, but what if we're in NDEBUG
       
101968 ** and the blob is empty or otherwise contains suspect data?
       
101969 */
       
101970 static void interiorReaderInit(const char *pData, int nData,
       
101971                                InteriorReader *pReader){
       
101972   int n, nTerm;
       
101973 
       
101974   /* Require at least the leading flag byte */
       
101975   assert( nData>0 );
       
101976   assert( pData[0]!='\0' );
       
101977 
       
101978   CLEAR(pReader);
       
101979 
       
101980   /* Decode the base blockid, and set the cursor to the first term. */
       
101981   n = fts3GetVarint(pData+1, &pReader->iBlockid);
       
101982   assert( 1+n<=nData );
       
101983   pReader->pData = pData+1+n;
       
101984   pReader->nData = nData-(1+n);
       
101985 
       
101986   /* A single-child interior node (such as when a leaf node was too
       
101987   ** large for the segment directory) won't have any terms.
       
101988   ** Otherwise, decode the first term.
       
101989   */
       
101990   if( pReader->nData==0 ){
       
101991     dataBufferInit(&pReader->term, 0);
       
101992   }else{
       
101993     n = fts3GetVarint32(pReader->pData, &nTerm);
       
101994     dataBufferInit(&pReader->term, nTerm);
       
101995     dataBufferReplace(&pReader->term, pReader->pData+n, nTerm);
       
101996     assert( n+nTerm<=pReader->nData );
       
101997     pReader->pData += n+nTerm;
       
101998     pReader->nData -= n+nTerm;
       
101999   }
       
102000 }
       
102001 
       
102002 static int interiorReaderAtEnd(InteriorReader *pReader){
       
102003   return pReader->term.nData==0;
       
102004 }
       
102005 
       
102006 static sqlite_int64 interiorReaderCurrentBlockid(InteriorReader *pReader){
       
102007   return pReader->iBlockid;
       
102008 }
       
102009 
       
102010 static int interiorReaderTermBytes(InteriorReader *pReader){
       
102011   assert( !interiorReaderAtEnd(pReader) );
       
102012   return pReader->term.nData;
       
102013 }
       
102014 static const char *interiorReaderTerm(InteriorReader *pReader){
       
102015   assert( !interiorReaderAtEnd(pReader) );
       
102016   return pReader->term.pData;
       
102017 }
       
102018 
       
102019 /* Step forward to the next term in the node. */
       
102020 static void interiorReaderStep(InteriorReader *pReader){
       
102021   assert( !interiorReaderAtEnd(pReader) );
       
102022 
       
102023   /* If the last term has been read, signal eof, else construct the
       
102024   ** next term.
       
102025   */
       
102026   if( pReader->nData==0 ){
       
102027     dataBufferReset(&pReader->term);
       
102028   }else{
       
102029     int n, nPrefix, nSuffix;
       
102030 
       
102031     n = fts3GetVarint32(pReader->pData, &nPrefix);
       
102032     n += fts3GetVarint32(pReader->pData+n, &nSuffix);
       
102033 
       
102034     /* Truncate the current term and append suffix data. */
       
102035     pReader->term.nData = nPrefix;
       
102036     dataBufferAppend(&pReader->term, pReader->pData+n, nSuffix);
       
102037 
       
102038     assert( n+nSuffix<=pReader->nData );
       
102039     pReader->pData += n+nSuffix;
       
102040     pReader->nData -= n+nSuffix;
       
102041   }
       
102042   pReader->iBlockid++;
       
102043 }
       
102044 
       
102045 /* Compare the current term to pTerm[nTerm], returning strcmp-style
       
102046 ** results.  If isPrefix, equality means equal through nTerm bytes.
       
102047 */
       
102048 static int interiorReaderTermCmp(InteriorReader *pReader,
       
102049                                  const char *pTerm, int nTerm, int isPrefix){
       
102050   const char *pReaderTerm = interiorReaderTerm(pReader);
       
102051   int nReaderTerm = interiorReaderTermBytes(pReader);
       
102052   int c, n = nReaderTerm<nTerm ? nReaderTerm : nTerm;
       
102053 
       
102054   if( n==0 ){
       
102055     if( nReaderTerm>0 ) return -1;
       
102056     if( nTerm>0 ) return 1;
       
102057     return 0;
       
102058   }
       
102059 
       
102060   c = memcmp(pReaderTerm, pTerm, n);
       
102061   if( c!=0 ) return c;
       
102062   if( isPrefix && n==nTerm ) return 0;
       
102063   return nReaderTerm - nTerm;
       
102064 }
       
102065 
       
102066 /****************************************************************/
       
102067 /* LeafWriter is used to collect terms and associated doclist data
       
102068 ** into leaf blocks in %_segments (see top of file for format info).
       
102069 ** Expected usage is:
       
102070 **
       
102071 ** LeafWriter writer;
       
102072 ** leafWriterInit(0, 0, &writer);
       
102073 ** while( sorted_terms_left_to_process ){
       
102074 **   // data is doclist data for that term.
       
102075 **   rc = leafWriterStep(v, &writer, pTerm, nTerm, pData, nData);
       
102076 **   if( rc!=SQLITE_OK ) goto err;
       
102077 ** }
       
102078 ** rc = leafWriterFinalize(v, &writer);
       
102079 **err:
       
102080 ** leafWriterDestroy(&writer);
       
102081 ** return rc;
       
102082 **
       
102083 ** leafWriterStep() may write a collected leaf out to %_segments.
       
102084 ** leafWriterFinalize() finishes writing any buffered data and stores
       
102085 ** a root node in %_segdir.  leafWriterDestroy() frees all buffers and
       
102086 ** InteriorWriters allocated as part of writing this segment.
       
102087 **
       
102088 ** TODO(shess) Document leafWriterStepMerge().
       
102089 */
       
102090 
       
102091 /* Put terms with data this big in their own block. */
       
102092 #define STANDALONE_MIN 1024
       
102093 
       
102094 /* Keep leaf blocks below this size. */
       
102095 #define LEAF_MAX 2048
       
102096 
       
102097 typedef struct LeafWriter {
       
102098   int iLevel;
       
102099   int idx;
       
102100   sqlite_int64 iStartBlockid;     /* needed to create the root info */
       
102101   sqlite_int64 iEndBlockid;       /* when we're done writing. */
       
102102 
       
102103   DataBuffer term;                /* previous encoded term */
       
102104   DataBuffer data;                /* encoding buffer */
       
102105 
       
102106   /* bytes of first term in the current node which distinguishes that
       
102107   ** term from the last term of the previous node.
       
102108   */
       
102109   int nTermDistinct;
       
102110 
       
102111   InteriorWriter parentWriter;    /* if we overflow */
       
102112   int has_parent;
       
102113 } LeafWriter;
       
102114 
       
102115 static void leafWriterInit(int iLevel, int idx, LeafWriter *pWriter){
       
102116   CLEAR(pWriter);
       
102117   pWriter->iLevel = iLevel;
       
102118   pWriter->idx = idx;
       
102119 
       
102120   dataBufferInit(&pWriter->term, 32);
       
102121 
       
102122   /* Start out with a reasonably sized block, though it can grow. */
       
102123   dataBufferInit(&pWriter->data, LEAF_MAX);
       
102124 }
       
102125 
       
102126 #ifndef NDEBUG
       
102127 /* Verify that the data is readable as a leaf node. */
       
102128 static void leafNodeValidate(const char *pData, int nData){
       
102129   int n, iDummy;
       
102130 
       
102131   if( nData==0 ) return;
       
102132   assert( nData>0 );
       
102133   assert( pData!=0 );
       
102134   assert( pData+nData>pData );
       
102135 
       
102136   /* Must lead with a varint(0) */
       
102137   n = fts3GetVarint32(pData, &iDummy);
       
102138   assert( iDummy==0 );
       
102139   assert( n>0 );
       
102140   assert( n<nData );
       
102141   pData += n;
       
102142   nData -= n;
       
102143 
       
102144   /* Leading term length and data must fit in buffer. */
       
102145   n = fts3GetVarint32(pData, &iDummy);
       
102146   assert( n>0 );
       
102147   assert( iDummy>0 );
       
102148   assert( n+iDummy>0 );
       
102149   assert( n+iDummy<nData );
       
102150   pData += n+iDummy;
       
102151   nData -= n+iDummy;
       
102152 
       
102153   /* Leading term's doclist length and data must fit. */
       
102154   n = fts3GetVarint32(pData, &iDummy);
       
102155   assert( n>0 );
       
102156   assert( iDummy>0 );
       
102157   assert( n+iDummy>0 );
       
102158   assert( n+iDummy<=nData );
       
102159   ASSERT_VALID_DOCLIST(DL_DEFAULT, pData+n, iDummy, NULL);
       
102160   pData += n+iDummy;
       
102161   nData -= n+iDummy;
       
102162 
       
102163   /* Verify that trailing terms and doclists also are readable. */
       
102164   while( nData!=0 ){
       
102165     n = fts3GetVarint32(pData, &iDummy);
       
102166     assert( n>0 );
       
102167     assert( iDummy>=0 );
       
102168     assert( n<nData );
       
102169     pData += n;
       
102170     nData -= n;
       
102171     n = fts3GetVarint32(pData, &iDummy);
       
102172     assert( n>0 );
       
102173     assert( iDummy>0 );
       
102174     assert( n+iDummy>0 );
       
102175     assert( n+iDummy<nData );
       
102176     pData += n+iDummy;
       
102177     nData -= n+iDummy;
       
102178 
       
102179     n = fts3GetVarint32(pData, &iDummy);
       
102180     assert( n>0 );
       
102181     assert( iDummy>0 );
       
102182     assert( n+iDummy>0 );
       
102183     assert( n+iDummy<=nData );
       
102184     ASSERT_VALID_DOCLIST(DL_DEFAULT, pData+n, iDummy, NULL);
       
102185     pData += n+iDummy;
       
102186     nData -= n+iDummy;
       
102187   }
       
102188 }
       
102189 #define ASSERT_VALID_LEAF_NODE(p, n) leafNodeValidate(p, n)
       
102190 #else
       
102191 #define ASSERT_VALID_LEAF_NODE(p, n) assert( 1 )
       
102192 #endif
       
102193 
       
102194 /* Flush the current leaf node to %_segments, and adding the resulting
       
102195 ** blockid and the starting term to the interior node which will
       
102196 ** contain it.
       
102197 */
       
102198 static int leafWriterInternalFlush(fulltext_vtab *v, LeafWriter *pWriter,
       
102199                                    int iData, int nData){
       
102200   sqlite_int64 iBlockid = 0;
       
102201   const char *pStartingTerm;
       
102202   int nStartingTerm, rc, n;
       
102203 
       
102204   /* Must have the leading varint(0) flag, plus at least some
       
102205   ** valid-looking data.
       
102206   */
       
102207   assert( nData>2 );
       
102208   assert( iData>=0 );
       
102209   assert( iData+nData<=pWriter->data.nData );
       
102210   ASSERT_VALID_LEAF_NODE(pWriter->data.pData+iData, nData);
       
102211 
       
102212   rc = block_insert(v, pWriter->data.pData+iData, nData, &iBlockid);
       
102213   if( rc!=SQLITE_OK ) return rc;
       
102214   assert( iBlockid!=0 );
       
102215 
       
102216   /* Reconstruct the first term in the leaf for purposes of building
       
102217   ** the interior node.
       
102218   */
       
102219   n = fts3GetVarint32(pWriter->data.pData+iData+1, &nStartingTerm);
       
102220   pStartingTerm = pWriter->data.pData+iData+1+n;
       
102221   assert( pWriter->data.nData>iData+1+n+nStartingTerm );
       
102222   assert( pWriter->nTermDistinct>0 );
       
102223   assert( pWriter->nTermDistinct<=nStartingTerm );
       
102224   nStartingTerm = pWriter->nTermDistinct;
       
102225 
       
102226   if( pWriter->has_parent ){
       
102227     interiorWriterAppend(&pWriter->parentWriter,
       
102228                          pStartingTerm, nStartingTerm, iBlockid);
       
102229   }else{
       
102230     interiorWriterInit(1, pStartingTerm, nStartingTerm, iBlockid,
       
102231                        &pWriter->parentWriter);
       
102232     pWriter->has_parent = 1;
       
102233   }
       
102234 
       
102235   /* Track the span of this segment's leaf nodes. */
       
102236   if( pWriter->iEndBlockid==0 ){
       
102237     pWriter->iEndBlockid = pWriter->iStartBlockid = iBlockid;
       
102238   }else{
       
102239     pWriter->iEndBlockid++;
       
102240     assert( iBlockid==pWriter->iEndBlockid );
       
102241   }
       
102242 
       
102243   return SQLITE_OK;
       
102244 }
       
102245 static int leafWriterFlush(fulltext_vtab *v, LeafWriter *pWriter){
       
102246   int rc = leafWriterInternalFlush(v, pWriter, 0, pWriter->data.nData);
       
102247   if( rc!=SQLITE_OK ) return rc;
       
102248 
       
102249   /* Re-initialize the output buffer. */
       
102250   dataBufferReset(&pWriter->data);
       
102251 
       
102252   return SQLITE_OK;
       
102253 }
       
102254 
       
102255 /* Fetch the root info for the segment.  If the entire leaf fits
       
102256 ** within ROOT_MAX, then it will be returned directly, otherwise it
       
102257 ** will be flushed and the root info will be returned from the
       
102258 ** interior node.  *piEndBlockid is set to the blockid of the last
       
102259 ** interior or leaf node written to disk (0 if none are written at
       
102260 ** all).
       
102261 */
       
102262 static int leafWriterRootInfo(fulltext_vtab *v, LeafWriter *pWriter,
       
102263                               char **ppRootInfo, int *pnRootInfo,
       
102264                               sqlite_int64 *piEndBlockid){
       
102265   /* we can fit the segment entirely inline */
       
102266   if( !pWriter->has_parent && pWriter->data.nData<ROOT_MAX ){
       
102267     *ppRootInfo = pWriter->data.pData;
       
102268     *pnRootInfo = pWriter->data.nData;
       
102269     *piEndBlockid = 0;
       
102270     return SQLITE_OK;
       
102271   }
       
102272 
       
102273   /* Flush remaining leaf data. */
       
102274   if( pWriter->data.nData>0 ){
       
102275     int rc = leafWriterFlush(v, pWriter);
       
102276     if( rc!=SQLITE_OK ) return rc;
       
102277   }
       
102278 
       
102279   /* We must have flushed a leaf at some point. */
       
102280   assert( pWriter->has_parent );
       
102281 
       
102282   /* Tenatively set the end leaf blockid as the end blockid.  If the
       
102283   ** interior node can be returned inline, this will be the final
       
102284   ** blockid, otherwise it will be overwritten by
       
102285   ** interiorWriterRootInfo().
       
102286   */
       
102287   *piEndBlockid = pWriter->iEndBlockid;
       
102288 
       
102289   return interiorWriterRootInfo(v, &pWriter->parentWriter,
       
102290                                 ppRootInfo, pnRootInfo, piEndBlockid);
       
102291 }
       
102292 
       
102293 /* Collect the rootInfo data and store it into the segment directory.
       
102294 ** This has the effect of flushing the segment's leaf data to
       
102295 ** %_segments, and also flushing any interior nodes to %_segments.
       
102296 */
       
102297 static int leafWriterFinalize(fulltext_vtab *v, LeafWriter *pWriter){
       
102298   sqlite_int64 iEndBlockid;
       
102299   char *pRootInfo;
       
102300   int rc, nRootInfo;
       
102301 
       
102302   rc = leafWriterRootInfo(v, pWriter, &pRootInfo, &nRootInfo, &iEndBlockid);
       
102303   if( rc!=SQLITE_OK ) return rc;
       
102304 
       
102305   /* Don't bother storing an entirely empty segment. */
       
102306   if( iEndBlockid==0 && nRootInfo==0 ) return SQLITE_OK;
       
102307 
       
102308   return segdir_set(v, pWriter->iLevel, pWriter->idx,
       
102309                     pWriter->iStartBlockid, pWriter->iEndBlockid,
       
102310                     iEndBlockid, pRootInfo, nRootInfo);
       
102311 }
       
102312 
       
102313 static void leafWriterDestroy(LeafWriter *pWriter){
       
102314   if( pWriter->has_parent ) interiorWriterDestroy(&pWriter->parentWriter);
       
102315   dataBufferDestroy(&pWriter->term);
       
102316   dataBufferDestroy(&pWriter->data);
       
102317 }
       
102318 
       
102319 /* Encode a term into the leafWriter, delta-encoding as appropriate.
       
102320 ** Returns the length of the new term which distinguishes it from the
       
102321 ** previous term, which can be used to set nTermDistinct when a node
       
102322 ** boundary is crossed.
       
102323 */
       
102324 static int leafWriterEncodeTerm(LeafWriter *pWriter,
       
102325                                 const char *pTerm, int nTerm){
       
102326   char c[VARINT_MAX+VARINT_MAX];
       
102327   int n, nPrefix = 0;
       
102328 
       
102329   assert( nTerm>0 );
       
102330   while( nPrefix<pWriter->term.nData &&
       
102331          pTerm[nPrefix]==pWriter->term.pData[nPrefix] ){
       
102332     nPrefix++;
       
102333     /* Failing this implies that the terms weren't in order. */
       
102334     assert( nPrefix<nTerm );
       
102335   }
       
102336 
       
102337   if( pWriter->data.nData==0 ){
       
102338     /* Encode the node header and leading term as:
       
102339     **  varint(0)
       
102340     **  varint(nTerm)
       
102341     **  char pTerm[nTerm]
       
102342     */
       
102343     n = fts3PutVarint(c, '\0');
       
102344     n += fts3PutVarint(c+n, nTerm);
       
102345     dataBufferAppend2(&pWriter->data, c, n, pTerm, nTerm);
       
102346   }else{
       
102347     /* Delta-encode the term as:
       
102348     **  varint(nPrefix)
       
102349     **  varint(nSuffix)
       
102350     **  char pTermSuffix[nSuffix]
       
102351     */
       
102352     n = fts3PutVarint(c, nPrefix);
       
102353     n += fts3PutVarint(c+n, nTerm-nPrefix);
       
102354     dataBufferAppend2(&pWriter->data, c, n, pTerm+nPrefix, nTerm-nPrefix);
       
102355   }
       
102356   dataBufferReplace(&pWriter->term, pTerm, nTerm);
       
102357 
       
102358   return nPrefix+1;
       
102359 }
       
102360 
       
102361 /* Used to avoid a memmove when a large amount of doclist data is in
       
102362 ** the buffer.  This constructs a node and term header before
       
102363 ** iDoclistData and flushes the resulting complete node using
       
102364 ** leafWriterInternalFlush().
       
102365 */
       
102366 static int leafWriterInlineFlush(fulltext_vtab *v, LeafWriter *pWriter,
       
102367                                  const char *pTerm, int nTerm,
       
102368                                  int iDoclistData){
       
102369   char c[VARINT_MAX+VARINT_MAX];
       
102370   int iData, n = fts3PutVarint(c, 0);
       
102371   n += fts3PutVarint(c+n, nTerm);
       
102372 
       
102373   /* There should always be room for the header.  Even if pTerm shared
       
102374   ** a substantial prefix with the previous term, the entire prefix
       
102375   ** could be constructed from earlier data in the doclist, so there
       
102376   ** should be room.
       
102377   */
       
102378   assert( iDoclistData>=n+nTerm );
       
102379 
       
102380   iData = iDoclistData-(n+nTerm);
       
102381   memcpy(pWriter->data.pData+iData, c, n);
       
102382   memcpy(pWriter->data.pData+iData+n, pTerm, nTerm);
       
102383 
       
102384   return leafWriterInternalFlush(v, pWriter, iData, pWriter->data.nData-iData);
       
102385 }
       
102386 
       
102387 /* Push pTerm[nTerm] along with the doclist data to the leaf layer of
       
102388 ** %_segments.
       
102389 */
       
102390 static int leafWriterStepMerge(fulltext_vtab *v, LeafWriter *pWriter,
       
102391                                const char *pTerm, int nTerm,
       
102392                                DLReader *pReaders, int nReaders){
       
102393   char c[VARINT_MAX+VARINT_MAX];
       
102394   int iTermData = pWriter->data.nData, iDoclistData;
       
102395   int i, nData, n, nActualData, nActual, rc, nTermDistinct;
       
102396 
       
102397   ASSERT_VALID_LEAF_NODE(pWriter->data.pData, pWriter->data.nData);
       
102398   nTermDistinct = leafWriterEncodeTerm(pWriter, pTerm, nTerm);
       
102399 
       
102400   /* Remember nTermDistinct if opening a new node. */
       
102401   if( iTermData==0 ) pWriter->nTermDistinct = nTermDistinct;
       
102402 
       
102403   iDoclistData = pWriter->data.nData;
       
102404 
       
102405   /* Estimate the length of the merged doclist so we can leave space
       
102406   ** to encode it.
       
102407   */
       
102408   for(i=0, nData=0; i<nReaders; i++){
       
102409     nData += dlrAllDataBytes(&pReaders[i]);
       
102410   }
       
102411   n = fts3PutVarint(c, nData);
       
102412   dataBufferAppend(&pWriter->data, c, n);
       
102413 
       
102414   docListMerge(&pWriter->data, pReaders, nReaders);
       
102415   ASSERT_VALID_DOCLIST(DL_DEFAULT,
       
102416                        pWriter->data.pData+iDoclistData+n,
       
102417                        pWriter->data.nData-iDoclistData-n, NULL);
       
102418 
       
102419   /* The actual amount of doclist data at this point could be smaller
       
102420   ** than the length we encoded.  Additionally, the space required to
       
102421   ** encode this length could be smaller.  For small doclists, this is
       
102422   ** not a big deal, we can just use memmove() to adjust things.
       
102423   */
       
102424   nActualData = pWriter->data.nData-(iDoclistData+n);
       
102425   nActual = fts3PutVarint(c, nActualData);
       
102426   assert( nActualData<=nData );
       
102427   assert( nActual<=n );
       
102428 
       
102429   /* If the new doclist is big enough for force a standalone leaf
       
102430   ** node, we can immediately flush it inline without doing the
       
102431   ** memmove().
       
102432   */
       
102433   /* TODO(shess) This test matches leafWriterStep(), which does this
       
102434   ** test before it knows the cost to varint-encode the term and
       
102435   ** doclist lengths.  At some point, change to
       
102436   ** pWriter->data.nData-iTermData>STANDALONE_MIN.
       
102437   */
       
102438   if( nTerm+nActualData>STANDALONE_MIN ){
       
102439     /* Push leaf node from before this term. */
       
102440     if( iTermData>0 ){
       
102441       rc = leafWriterInternalFlush(v, pWriter, 0, iTermData);
       
102442       if( rc!=SQLITE_OK ) return rc;
       
102443 
       
102444       pWriter->nTermDistinct = nTermDistinct;
       
102445     }
       
102446 
       
102447     /* Fix the encoded doclist length. */
       
102448     iDoclistData += n - nActual;
       
102449     memcpy(pWriter->data.pData+iDoclistData, c, nActual);
       
102450 
       
102451     /* Push the standalone leaf node. */
       
102452     rc = leafWriterInlineFlush(v, pWriter, pTerm, nTerm, iDoclistData);
       
102453     if( rc!=SQLITE_OK ) return rc;
       
102454 
       
102455     /* Leave the node empty. */
       
102456     dataBufferReset(&pWriter->data);
       
102457 
       
102458     return rc;
       
102459   }
       
102460 
       
102461   /* At this point, we know that the doclist was small, so do the
       
102462   ** memmove if indicated.
       
102463   */
       
102464   if( nActual<n ){
       
102465     memmove(pWriter->data.pData+iDoclistData+nActual,
       
102466             pWriter->data.pData+iDoclistData+n,
       
102467             pWriter->data.nData-(iDoclistData+n));
       
102468     pWriter->data.nData -= n-nActual;
       
102469   }
       
102470 
       
102471   /* Replace written length with actual length. */
       
102472   memcpy(pWriter->data.pData+iDoclistData, c, nActual);
       
102473 
       
102474   /* If the node is too large, break things up. */
       
102475   /* TODO(shess) This test matches leafWriterStep(), which does this
       
102476   ** test before it knows the cost to varint-encode the term and
       
102477   ** doclist lengths.  At some point, change to
       
102478   ** pWriter->data.nData>LEAF_MAX.
       
102479   */
       
102480   if( iTermData+nTerm+nActualData>LEAF_MAX ){
       
102481     /* Flush out the leading data as a node */
       
102482     rc = leafWriterInternalFlush(v, pWriter, 0, iTermData);
       
102483     if( rc!=SQLITE_OK ) return rc;
       
102484 
       
102485     pWriter->nTermDistinct = nTermDistinct;
       
102486 
       
102487     /* Rebuild header using the current term */
       
102488     n = fts3PutVarint(pWriter->data.pData, 0);
       
102489     n += fts3PutVarint(pWriter->data.pData+n, nTerm);
       
102490     memcpy(pWriter->data.pData+n, pTerm, nTerm);
       
102491     n += nTerm;
       
102492 
       
102493     /* There should always be room, because the previous encoding
       
102494     ** included all data necessary to construct the term.
       
102495     */
       
102496     assert( n<iDoclistData );
       
102497     /* So long as STANDALONE_MIN is half or less of LEAF_MAX, the
       
102498     ** following memcpy() is safe (as opposed to needing a memmove).
       
102499     */
       
102500     assert( 2*STANDALONE_MIN<=LEAF_MAX );
       
102501     assert( n+pWriter->data.nData-iDoclistData<iDoclistData );
       
102502     memcpy(pWriter->data.pData+n,
       
102503            pWriter->data.pData+iDoclistData,
       
102504            pWriter->data.nData-iDoclistData);
       
102505     pWriter->data.nData -= iDoclistData-n;
       
102506   }
       
102507   ASSERT_VALID_LEAF_NODE(pWriter->data.pData, pWriter->data.nData);
       
102508 
       
102509   return SQLITE_OK;
       
102510 }
       
102511 
       
102512 /* Push pTerm[nTerm] along with the doclist data to the leaf layer of
       
102513 ** %_segments.
       
102514 */
       
102515 /* TODO(shess) Revise writeZeroSegment() so that doclists are
       
102516 ** constructed directly in pWriter->data.
       
102517 */
       
102518 static int leafWriterStep(fulltext_vtab *v, LeafWriter *pWriter,
       
102519                           const char *pTerm, int nTerm,
       
102520                           const char *pData, int nData){
       
102521   int rc;
       
102522   DLReader reader;
       
102523 
       
102524   dlrInit(&reader, DL_DEFAULT, pData, nData);
       
102525   rc = leafWriterStepMerge(v, pWriter, pTerm, nTerm, &reader, 1);
       
102526   dlrDestroy(&reader);
       
102527 
       
102528   return rc;
       
102529 }
       
102530 
       
102531 
       
102532 /****************************************************************/
       
102533 /* LeafReader is used to iterate over an individual leaf node. */
       
102534 typedef struct LeafReader {
       
102535   DataBuffer term;          /* copy of current term. */
       
102536 
       
102537   const char *pData;        /* data for current term. */
       
102538   int nData;
       
102539 } LeafReader;
       
102540 
       
102541 static void leafReaderDestroy(LeafReader *pReader){
       
102542   dataBufferDestroy(&pReader->term);
       
102543   SCRAMBLE(pReader);
       
102544 }
       
102545 
       
102546 static int leafReaderAtEnd(LeafReader *pReader){
       
102547   return pReader->nData<=0;
       
102548 }
       
102549 
       
102550 /* Access the current term. */
       
102551 static int leafReaderTermBytes(LeafReader *pReader){
       
102552   return pReader->term.nData;
       
102553 }
       
102554 static const char *leafReaderTerm(LeafReader *pReader){
       
102555   assert( pReader->term.nData>0 );
       
102556   return pReader->term.pData;
       
102557 }
       
102558 
       
102559 /* Access the doclist data for the current term. */
       
102560 static int leafReaderDataBytes(LeafReader *pReader){
       
102561   int nData;
       
102562   assert( pReader->term.nData>0 );
       
102563   fts3GetVarint32(pReader->pData, &nData);
       
102564   return nData;
       
102565 }
       
102566 static const char *leafReaderData(LeafReader *pReader){
       
102567   int n, nData;
       
102568   assert( pReader->term.nData>0 );
       
102569   n = fts3GetVarint32(pReader->pData, &nData);
       
102570   return pReader->pData+n;
       
102571 }
       
102572 
       
102573 static void leafReaderInit(const char *pData, int nData,
       
102574                            LeafReader *pReader){
       
102575   int nTerm, n;
       
102576 
       
102577   assert( nData>0 );
       
102578   assert( pData[0]=='\0' );
       
102579 
       
102580   CLEAR(pReader);
       
102581 
       
102582   /* Read the first term, skipping the header byte. */
       
102583   n = fts3GetVarint32(pData+1, &nTerm);
       
102584   dataBufferInit(&pReader->term, nTerm);
       
102585   dataBufferReplace(&pReader->term, pData+1+n, nTerm);
       
102586 
       
102587   /* Position after the first term. */
       
102588   assert( 1+n+nTerm<nData );
       
102589   pReader->pData = pData+1+n+nTerm;
       
102590   pReader->nData = nData-1-n-nTerm;
       
102591 }
       
102592 
       
102593 /* Step the reader forward to the next term. */
       
102594 static void leafReaderStep(LeafReader *pReader){
       
102595   int n, nData, nPrefix, nSuffix;
       
102596   assert( !leafReaderAtEnd(pReader) );
       
102597 
       
102598   /* Skip previous entry's data block. */
       
102599   n = fts3GetVarint32(pReader->pData, &nData);
       
102600   assert( n+nData<=pReader->nData );
       
102601   pReader->pData += n+nData;
       
102602   pReader->nData -= n+nData;
       
102603 
       
102604   if( !leafReaderAtEnd(pReader) ){
       
102605     /* Construct the new term using a prefix from the old term plus a
       
102606     ** suffix from the leaf data.
       
102607     */
       
102608     n = fts3GetVarint32(pReader->pData, &nPrefix);
       
102609     n += fts3GetVarint32(pReader->pData+n, &nSuffix);
       
102610     assert( n+nSuffix<pReader->nData );
       
102611     pReader->term.nData = nPrefix;
       
102612     dataBufferAppend(&pReader->term, pReader->pData+n, nSuffix);
       
102613 
       
102614     pReader->pData += n+nSuffix;
       
102615     pReader->nData -= n+nSuffix;
       
102616   }
       
102617 }
       
102618 
       
102619 /* strcmp-style comparison of pReader's current term against pTerm.
       
102620 ** If isPrefix, equality means equal through nTerm bytes.
       
102621 */
       
102622 static int leafReaderTermCmp(LeafReader *pReader,
       
102623                              const char *pTerm, int nTerm, int isPrefix){
       
102624   int c, n = pReader->term.nData<nTerm ? pReader->term.nData : nTerm;
       
102625   if( n==0 ){
       
102626     if( pReader->term.nData>0 ) return -1;
       
102627     if(nTerm>0 ) return 1;
       
102628     return 0;
       
102629   }
       
102630 
       
102631   c = memcmp(pReader->term.pData, pTerm, n);
       
102632   if( c!=0 ) return c;
       
102633   if( isPrefix && n==nTerm ) return 0;
       
102634   return pReader->term.nData - nTerm;
       
102635 }
       
102636 
       
102637 
       
102638 /****************************************************************/
       
102639 /* LeavesReader wraps LeafReader to allow iterating over the entire
       
102640 ** leaf layer of the tree.
       
102641 */
       
102642 typedef struct LeavesReader {
       
102643   int idx;                  /* Index within the segment. */
       
102644 
       
102645   sqlite3_stmt *pStmt;      /* Statement we're streaming leaves from. */
       
102646   int eof;                  /* we've seen SQLITE_DONE from pStmt. */
       
102647 
       
102648   LeafReader leafReader;    /* reader for the current leaf. */
       
102649   DataBuffer rootData;      /* root data for inline. */
       
102650 } LeavesReader;
       
102651 
       
102652 /* Access the current term. */
       
102653 static int leavesReaderTermBytes(LeavesReader *pReader){
       
102654   assert( !pReader->eof );
       
102655   return leafReaderTermBytes(&pReader->leafReader);
       
102656 }
       
102657 static const char *leavesReaderTerm(LeavesReader *pReader){
       
102658   assert( !pReader->eof );
       
102659   return leafReaderTerm(&pReader->leafReader);
       
102660 }
       
102661 
       
102662 /* Access the doclist data for the current term. */
       
102663 static int leavesReaderDataBytes(LeavesReader *pReader){
       
102664   assert( !pReader->eof );
       
102665   return leafReaderDataBytes(&pReader->leafReader);
       
102666 }
       
102667 static const char *leavesReaderData(LeavesReader *pReader){
       
102668   assert( !pReader->eof );
       
102669   return leafReaderData(&pReader->leafReader);
       
102670 }
       
102671 
       
102672 static int leavesReaderAtEnd(LeavesReader *pReader){
       
102673   return pReader->eof;
       
102674 }
       
102675 
       
102676 /* loadSegmentLeaves() may not read all the way to SQLITE_DONE, thus
       
102677 ** leaving the statement handle open, which locks the table.
       
102678 */
       
102679 /* TODO(shess) This "solution" is not satisfactory.  Really, there
       
102680 ** should be check-in function for all statement handles which
       
102681 ** arranges to call sqlite3_reset().  This most likely will require
       
102682 ** modification to control flow all over the place, though, so for now
       
102683 ** just punt.
       
102684 **
       
102685 ** Note the the current system assumes that segment merges will run to
       
102686 ** completion, which is why this particular probably hasn't arisen in
       
102687 ** this case.  Probably a brittle assumption.
       
102688 */
       
102689 static int leavesReaderReset(LeavesReader *pReader){
       
102690   return sqlite3_reset(pReader->pStmt);
       
102691 }
       
102692 
       
102693 static void leavesReaderDestroy(LeavesReader *pReader){
       
102694   /* If idx is -1, that means we're using a non-cached statement
       
102695   ** handle in the optimize() case, so we need to release it.
       
102696   */
       
102697   if( pReader->pStmt!=NULL && pReader->idx==-1 ){
       
102698     sqlite3_finalize(pReader->pStmt);
       
102699   }
       
102700   leafReaderDestroy(&pReader->leafReader);
       
102701   dataBufferDestroy(&pReader->rootData);
       
102702   SCRAMBLE(pReader);
       
102703 }
       
102704 
       
102705 /* Initialize pReader with the given root data (if iStartBlockid==0
       
102706 ** the leaf data was entirely contained in the root), or from the
       
102707 ** stream of blocks between iStartBlockid and iEndBlockid, inclusive.
       
102708 */
       
102709 static int leavesReaderInit(fulltext_vtab *v,
       
102710                             int idx,
       
102711                             sqlite_int64 iStartBlockid,
       
102712                             sqlite_int64 iEndBlockid,
       
102713                             const char *pRootData, int nRootData,
       
102714                             LeavesReader *pReader){
       
102715   CLEAR(pReader);
       
102716   pReader->idx = idx;
       
102717 
       
102718   dataBufferInit(&pReader->rootData, 0);
       
102719   if( iStartBlockid==0 ){
       
102720     /* Entire leaf level fit in root data. */
       
102721     dataBufferReplace(&pReader->rootData, pRootData, nRootData);
       
102722     leafReaderInit(pReader->rootData.pData, pReader->rootData.nData,
       
102723                    &pReader->leafReader);
       
102724   }else{
       
102725     sqlite3_stmt *s;
       
102726     int rc = sql_get_leaf_statement(v, idx, &s);
       
102727     if( rc!=SQLITE_OK ) return rc;
       
102728 
       
102729     rc = sqlite3_bind_int64(s, 1, iStartBlockid);
       
102730     if( rc!=SQLITE_OK ) return rc;
       
102731 
       
102732     rc = sqlite3_bind_int64(s, 2, iEndBlockid);
       
102733     if( rc!=SQLITE_OK ) return rc;
       
102734 
       
102735     rc = sqlite3_step(s);
       
102736     if( rc==SQLITE_DONE ){
       
102737       pReader->eof = 1;
       
102738       return SQLITE_OK;
       
102739     }
       
102740     if( rc!=SQLITE_ROW ) return rc;
       
102741 
       
102742     pReader->pStmt = s;
       
102743     leafReaderInit(sqlite3_column_blob(pReader->pStmt, 0),
       
102744                    sqlite3_column_bytes(pReader->pStmt, 0),
       
102745                    &pReader->leafReader);
       
102746   }
       
102747   return SQLITE_OK;
       
102748 }
       
102749 
       
102750 /* Step the current leaf forward to the next term.  If we reach the
       
102751 ** end of the current leaf, step forward to the next leaf block.
       
102752 */
       
102753 static int leavesReaderStep(fulltext_vtab *v, LeavesReader *pReader){
       
102754   assert( !leavesReaderAtEnd(pReader) );
       
102755   leafReaderStep(&pReader->leafReader);
       
102756 
       
102757   if( leafReaderAtEnd(&pReader->leafReader) ){
       
102758     int rc;
       
102759     if( pReader->rootData.pData ){
       
102760       pReader->eof = 1;
       
102761       return SQLITE_OK;
       
102762     }
       
102763     rc = sqlite3_step(pReader->pStmt);
       
102764     if( rc!=SQLITE_ROW ){
       
102765       pReader->eof = 1;
       
102766       return rc==SQLITE_DONE ? SQLITE_OK : rc;
       
102767     }
       
102768     leafReaderDestroy(&pReader->leafReader);
       
102769     leafReaderInit(sqlite3_column_blob(pReader->pStmt, 0),
       
102770                    sqlite3_column_bytes(pReader->pStmt, 0),
       
102771                    &pReader->leafReader);
       
102772   }
       
102773   return SQLITE_OK;
       
102774 }
       
102775 
       
102776 /* Order LeavesReaders by their term, ignoring idx.  Readers at eof
       
102777 ** always sort to the end.
       
102778 */
       
102779 static int leavesReaderTermCmp(LeavesReader *lr1, LeavesReader *lr2){
       
102780   if( leavesReaderAtEnd(lr1) ){
       
102781     if( leavesReaderAtEnd(lr2) ) return 0;
       
102782     return 1;
       
102783   }
       
102784   if( leavesReaderAtEnd(lr2) ) return -1;
       
102785 
       
102786   return leafReaderTermCmp(&lr1->leafReader,
       
102787                            leavesReaderTerm(lr2), leavesReaderTermBytes(lr2),
       
102788                            0);
       
102789 }
       
102790 
       
102791 /* Similar to leavesReaderTermCmp(), with additional ordering by idx
       
102792 ** so that older segments sort before newer segments.
       
102793 */
       
102794 static int leavesReaderCmp(LeavesReader *lr1, LeavesReader *lr2){
       
102795   int c = leavesReaderTermCmp(lr1, lr2);
       
102796   if( c!=0 ) return c;
       
102797   return lr1->idx-lr2->idx;
       
102798 }
       
102799 
       
102800 /* Assume that pLr[1]..pLr[nLr] are sorted.  Bubble pLr[0] into its
       
102801 ** sorted position.
       
102802 */
       
102803 static void leavesReaderReorder(LeavesReader *pLr, int nLr){
       
102804   while( nLr>1 && leavesReaderCmp(pLr, pLr+1)>0 ){
       
102805     LeavesReader tmp = pLr[0];
       
102806     pLr[0] = pLr[1];
       
102807     pLr[1] = tmp;
       
102808     nLr--;
       
102809     pLr++;
       
102810   }
       
102811 }
       
102812 
       
102813 /* Initializes pReaders with the segments from level iLevel, returning
       
102814 ** the number of segments in *piReaders.  Leaves pReaders in sorted
       
102815 ** order.
       
102816 */
       
102817 static int leavesReadersInit(fulltext_vtab *v, int iLevel,
       
102818                              LeavesReader *pReaders, int *piReaders){
       
102819   sqlite3_stmt *s;
       
102820   int i, rc = sql_get_statement(v, SEGDIR_SELECT_LEVEL_STMT, &s);
       
102821   if( rc!=SQLITE_OK ) return rc;
       
102822 
       
102823   rc = sqlite3_bind_int(s, 1, iLevel);
       
102824   if( rc!=SQLITE_OK ) return rc;
       
102825 
       
102826   i = 0;
       
102827   while( (rc = sqlite3_step(s))==SQLITE_ROW ){
       
102828     sqlite_int64 iStart = sqlite3_column_int64(s, 0);
       
102829     sqlite_int64 iEnd = sqlite3_column_int64(s, 1);
       
102830     const char *pRootData = sqlite3_column_blob(s, 2);
       
102831     int nRootData = sqlite3_column_bytes(s, 2);
       
102832 
       
102833     assert( i<MERGE_COUNT );
       
102834     rc = leavesReaderInit(v, i, iStart, iEnd, pRootData, nRootData,
       
102835                           &pReaders[i]);
       
102836     if( rc!=SQLITE_OK ) break;
       
102837 
       
102838     i++;
       
102839   }
       
102840   if( rc!=SQLITE_DONE ){
       
102841     while( i-->0 ){
       
102842       leavesReaderDestroy(&pReaders[i]);
       
102843     }
       
102844     return rc;
       
102845   }
       
102846 
       
102847   *piReaders = i;
       
102848 
       
102849   /* Leave our results sorted by term, then age. */
       
102850   while( i-- ){
       
102851     leavesReaderReorder(pReaders+i, *piReaders-i);
       
102852   }
       
102853   return SQLITE_OK;
       
102854 }
       
102855 
       
102856 /* Merge doclists from pReaders[nReaders] into a single doclist, which
       
102857 ** is written to pWriter.  Assumes pReaders is ordered oldest to
       
102858 ** newest.
       
102859 */
       
102860 /* TODO(shess) Consider putting this inline in segmentMerge(). */
       
102861 static int leavesReadersMerge(fulltext_vtab *v,
       
102862                               LeavesReader *pReaders, int nReaders,
       
102863                               LeafWriter *pWriter){
       
102864   DLReader dlReaders[MERGE_COUNT];
       
102865   const char *pTerm = leavesReaderTerm(pReaders);
       
102866   int i, nTerm = leavesReaderTermBytes(pReaders);
       
102867 
       
102868   assert( nReaders<=MERGE_COUNT );
       
102869 
       
102870   for(i=0; i<nReaders; i++){
       
102871     dlrInit(&dlReaders[i], DL_DEFAULT,
       
102872             leavesReaderData(pReaders+i),
       
102873             leavesReaderDataBytes(pReaders+i));
       
102874   }
       
102875 
       
102876   return leafWriterStepMerge(v, pWriter, pTerm, nTerm, dlReaders, nReaders);
       
102877 }
       
102878 
       
102879 /* Forward ref due to mutual recursion with segdirNextIndex(). */
       
102880 static int segmentMerge(fulltext_vtab *v, int iLevel);
       
102881 
       
102882 /* Put the next available index at iLevel into *pidx.  If iLevel
       
102883 ** already has MERGE_COUNT segments, they are merged to a higher
       
102884 ** level to make room.
       
102885 */
       
102886 static int segdirNextIndex(fulltext_vtab *v, int iLevel, int *pidx){
       
102887   int rc = segdir_max_index(v, iLevel, pidx);
       
102888   if( rc==SQLITE_DONE ){              /* No segments at iLevel. */
       
102889     *pidx = 0;
       
102890   }else if( rc==SQLITE_ROW ){
       
102891     if( *pidx==(MERGE_COUNT-1) ){
       
102892       rc = segmentMerge(v, iLevel);
       
102893       if( rc!=SQLITE_OK ) return rc;
       
102894       *pidx = 0;
       
102895     }else{
       
102896       (*pidx)++;
       
102897     }
       
102898   }else{
       
102899     return rc;
       
102900   }
       
102901   return SQLITE_OK;
       
102902 }
       
102903 
       
102904 /* Merge MERGE_COUNT segments at iLevel into a new segment at
       
102905 ** iLevel+1.  If iLevel+1 is already full of segments, those will be
       
102906 ** merged to make room.
       
102907 */
       
102908 static int segmentMerge(fulltext_vtab *v, int iLevel){
       
102909   LeafWriter writer;
       
102910   LeavesReader lrs[MERGE_COUNT];
       
102911   int i, rc, idx = 0;
       
102912 
       
102913   /* Determine the next available segment index at the next level,
       
102914   ** merging as necessary.
       
102915   */
       
102916   rc = segdirNextIndex(v, iLevel+1, &idx);
       
102917   if( rc!=SQLITE_OK ) return rc;
       
102918 
       
102919   /* TODO(shess) This assumes that we'll always see exactly
       
102920   ** MERGE_COUNT segments to merge at a given level.  That will be
       
102921   ** broken if we allow the developer to request preemptive or
       
102922   ** deferred merging.
       
102923   */
       
102924   memset(&lrs, '\0', sizeof(lrs));
       
102925   rc = leavesReadersInit(v, iLevel, lrs, &i);
       
102926   if( rc!=SQLITE_OK ) return rc;
       
102927   assert( i==MERGE_COUNT );
       
102928 
       
102929   leafWriterInit(iLevel+1, idx, &writer);
       
102930 
       
102931   /* Since leavesReaderReorder() pushes readers at eof to the end,
       
102932   ** when the first reader is empty, all will be empty.
       
102933   */
       
102934   while( !leavesReaderAtEnd(lrs) ){
       
102935     /* Figure out how many readers share their next term. */
       
102936     for(i=1; i<MERGE_COUNT && !leavesReaderAtEnd(lrs+i); i++){
       
102937       if( 0!=leavesReaderTermCmp(lrs, lrs+i) ) break;
       
102938     }
       
102939 
       
102940     rc = leavesReadersMerge(v, lrs, i, &writer);
       
102941     if( rc!=SQLITE_OK ) goto err;
       
102942 
       
102943     /* Step forward those that were merged. */
       
102944     while( i-->0 ){
       
102945       rc = leavesReaderStep(v, lrs+i);
       
102946       if( rc!=SQLITE_OK ) goto err;
       
102947 
       
102948       /* Reorder by term, then by age. */
       
102949       leavesReaderReorder(lrs+i, MERGE_COUNT-i);
       
102950     }
       
102951   }
       
102952 
       
102953   for(i=0; i<MERGE_COUNT; i++){
       
102954     leavesReaderDestroy(&lrs[i]);
       
102955   }
       
102956 
       
102957   rc = leafWriterFinalize(v, &writer);
       
102958   leafWriterDestroy(&writer);
       
102959   if( rc!=SQLITE_OK ) return rc;
       
102960 
       
102961   /* Delete the merged segment data. */
       
102962   return segdir_delete(v, iLevel);
       
102963 
       
102964  err:
       
102965   for(i=0; i<MERGE_COUNT; i++){
       
102966     leavesReaderDestroy(&lrs[i]);
       
102967   }
       
102968   leafWriterDestroy(&writer);
       
102969   return rc;
       
102970 }
       
102971 
       
102972 /* Accumulate the union of *acc and *pData into *acc. */
       
102973 static void docListAccumulateUnion(DataBuffer *acc,
       
102974                                    const char *pData, int nData) {
       
102975   DataBuffer tmp = *acc;
       
102976   dataBufferInit(acc, tmp.nData+nData);
       
102977   docListUnion(tmp.pData, tmp.nData, pData, nData, acc);
       
102978   dataBufferDestroy(&tmp);
       
102979 }
       
102980 
       
102981 /* TODO(shess) It might be interesting to explore different merge
       
102982 ** strategies, here.  For instance, since this is a sorted merge, we
       
102983 ** could easily merge many doclists in parallel.  With some
       
102984 ** comprehension of the storage format, we could merge all of the
       
102985 ** doclists within a leaf node directly from the leaf node's storage.
       
102986 ** It may be worthwhile to merge smaller doclists before larger
       
102987 ** doclists, since they can be traversed more quickly - but the
       
102988 ** results may have less overlap, making them more expensive in a
       
102989 ** different way.
       
102990 */
       
102991 
       
102992 /* Scan pReader for pTerm/nTerm, and merge the term's doclist over
       
102993 ** *out (any doclists with duplicate docids overwrite those in *out).
       
102994 ** Internal function for loadSegmentLeaf().
       
102995 */
       
102996 static int loadSegmentLeavesInt(fulltext_vtab *v, LeavesReader *pReader,
       
102997                                 const char *pTerm, int nTerm, int isPrefix,
       
102998                                 DataBuffer *out){
       
102999   /* doclist data is accumulated into pBuffers similar to how one does
       
103000   ** increment in binary arithmetic.  If index 0 is empty, the data is
       
103001   ** stored there.  If there is data there, it is merged and the
       
103002   ** results carried into position 1, with further merge-and-carry
       
103003   ** until an empty position is found.
       
103004   */
       
103005   DataBuffer *pBuffers = NULL;
       
103006   int nBuffers = 0, nMaxBuffers = 0, rc;
       
103007 
       
103008   assert( nTerm>0 );
       
103009 
       
103010   for(rc=SQLITE_OK; rc==SQLITE_OK && !leavesReaderAtEnd(pReader);
       
103011       rc=leavesReaderStep(v, pReader)){
       
103012     /* TODO(shess) Really want leavesReaderTermCmp(), but that name is
       
103013     ** already taken to compare the terms of two LeavesReaders.  Think
       
103014     ** on a better name.  [Meanwhile, break encapsulation rather than
       
103015     ** use a confusing name.]
       
103016     */
       
103017     int c = leafReaderTermCmp(&pReader->leafReader, pTerm, nTerm, isPrefix);
       
103018     if( c>0 ) break;      /* Past any possible matches. */
       
103019     if( c==0 ){
       
103020       const char *pData = leavesReaderData(pReader);
       
103021       int iBuffer, nData = leavesReaderDataBytes(pReader);
       
103022 
       
103023       /* Find the first empty buffer. */
       
103024       for(iBuffer=0; iBuffer<nBuffers; ++iBuffer){
       
103025         if( 0==pBuffers[iBuffer].nData ) break;
       
103026       }
       
103027 
       
103028       /* Out of buffers, add an empty one. */
       
103029       if( iBuffer==nBuffers ){
       
103030         if( nBuffers==nMaxBuffers ){
       
103031           DataBuffer *p;
       
103032           nMaxBuffers += 20;
       
103033 
       
103034           /* Manual realloc so we can handle NULL appropriately. */
       
103035           p = sqlite3_malloc(nMaxBuffers*sizeof(*pBuffers));
       
103036           if( p==NULL ){
       
103037             rc = SQLITE_NOMEM;
       
103038             break;
       
103039           }
       
103040 
       
103041           if( nBuffers>0 ){
       
103042             assert(pBuffers!=NULL);
       
103043             memcpy(p, pBuffers, nBuffers*sizeof(*pBuffers));
       
103044             sqlite3_free(pBuffers);
       
103045           }
       
103046           pBuffers = p;
       
103047         }
       
103048         dataBufferInit(&(pBuffers[nBuffers]), 0);
       
103049         nBuffers++;
       
103050       }
       
103051 
       
103052       /* At this point, must have an empty at iBuffer. */
       
103053       assert(iBuffer<nBuffers && pBuffers[iBuffer].nData==0);
       
103054 
       
103055       /* If empty was first buffer, no need for merge logic. */
       
103056       if( iBuffer==0 ){
       
103057         dataBufferReplace(&(pBuffers[0]), pData, nData);
       
103058       }else{
       
103059         /* pAcc is the empty buffer the merged data will end up in. */
       
103060         DataBuffer *pAcc = &(pBuffers[iBuffer]);
       
103061         DataBuffer *p = &(pBuffers[0]);
       
103062 
       
103063         /* Handle position 0 specially to avoid need to prime pAcc
       
103064         ** with pData/nData.
       
103065         */
       
103066         dataBufferSwap(p, pAcc);
       
103067         docListAccumulateUnion(pAcc, pData, nData);
       
103068 
       
103069         /* Accumulate remaining doclists into pAcc. */
       
103070         for(++p; p<pAcc; ++p){
       
103071           docListAccumulateUnion(pAcc, p->pData, p->nData);
       
103072 
       
103073           /* dataBufferReset() could allow a large doclist to blow up
       
103074           ** our memory requirements.
       
103075           */
       
103076           if( p->nCapacity<1024 ){
       
103077             dataBufferReset(p);
       
103078           }else{
       
103079             dataBufferDestroy(p);
       
103080             dataBufferInit(p, 0);
       
103081           }
       
103082         }
       
103083       }
       
103084     }
       
103085   }
       
103086 
       
103087   /* Union all the doclists together into *out. */
       
103088   /* TODO(shess) What if *out is big?  Sigh. */
       
103089   if( rc==SQLITE_OK && nBuffers>0 ){
       
103090     int iBuffer;
       
103091     for(iBuffer=0; iBuffer<nBuffers; ++iBuffer){
       
103092       if( pBuffers[iBuffer].nData>0 ){
       
103093         if( out->nData==0 ){
       
103094           dataBufferSwap(out, &(pBuffers[iBuffer]));
       
103095         }else{
       
103096           docListAccumulateUnion(out, pBuffers[iBuffer].pData,
       
103097                                  pBuffers[iBuffer].nData);
       
103098         }
       
103099       }
       
103100     }
       
103101   }
       
103102 
       
103103   while( nBuffers-- ){
       
103104     dataBufferDestroy(&(pBuffers[nBuffers]));
       
103105   }
       
103106   if( pBuffers!=NULL ) sqlite3_free(pBuffers);
       
103107 
       
103108   return rc;
       
103109 }
       
103110 
       
103111 /* Call loadSegmentLeavesInt() with pData/nData as input. */
       
103112 static int loadSegmentLeaf(fulltext_vtab *v, const char *pData, int nData,
       
103113                            const char *pTerm, int nTerm, int isPrefix,
       
103114                            DataBuffer *out){
       
103115   LeavesReader reader;
       
103116   int rc;
       
103117 
       
103118   assert( nData>1 );
       
103119   assert( *pData=='\0' );
       
103120   rc = leavesReaderInit(v, 0, 0, 0, pData, nData, &reader);
       
103121   if( rc!=SQLITE_OK ) return rc;
       
103122 
       
103123   rc = loadSegmentLeavesInt(v, &reader, pTerm, nTerm, isPrefix, out);
       
103124   leavesReaderReset(&reader);
       
103125   leavesReaderDestroy(&reader);
       
103126   return rc;
       
103127 }
       
103128 
       
103129 /* Call loadSegmentLeavesInt() with the leaf nodes from iStartLeaf to
       
103130 ** iEndLeaf (inclusive) as input, and merge the resulting doclist into
       
103131 ** out.
       
103132 */
       
103133 static int loadSegmentLeaves(fulltext_vtab *v,
       
103134                              sqlite_int64 iStartLeaf, sqlite_int64 iEndLeaf,
       
103135                              const char *pTerm, int nTerm, int isPrefix,
       
103136                              DataBuffer *out){
       
103137   int rc;
       
103138   LeavesReader reader;
       
103139 
       
103140   assert( iStartLeaf<=iEndLeaf );
       
103141   rc = leavesReaderInit(v, 0, iStartLeaf, iEndLeaf, NULL, 0, &reader);
       
103142   if( rc!=SQLITE_OK ) return rc;
       
103143 
       
103144   rc = loadSegmentLeavesInt(v, &reader, pTerm, nTerm, isPrefix, out);
       
103145   leavesReaderReset(&reader);
       
103146   leavesReaderDestroy(&reader);
       
103147   return rc;
       
103148 }
       
103149 
       
103150 /* Taking pData/nData as an interior node, find the sequence of child
       
103151 ** nodes which could include pTerm/nTerm/isPrefix.  Note that the
       
103152 ** interior node terms logically come between the blocks, so there is
       
103153 ** one more blockid than there are terms (that block contains terms >=
       
103154 ** the last interior-node term).
       
103155 */
       
103156 /* TODO(shess) The calling code may already know that the end child is
       
103157 ** not worth calculating, because the end may be in a later sibling
       
103158 ** node.  Consider whether breaking symmetry is worthwhile.  I suspect
       
103159 ** it is not worthwhile.
       
103160 */
       
103161 static void getChildrenContaining(const char *pData, int nData,
       
103162                                   const char *pTerm, int nTerm, int isPrefix,
       
103163                                   sqlite_int64 *piStartChild,
       
103164                                   sqlite_int64 *piEndChild){
       
103165   InteriorReader reader;
       
103166 
       
103167   assert( nData>1 );
       
103168   assert( *pData!='\0' );
       
103169   interiorReaderInit(pData, nData, &reader);
       
103170 
       
103171   /* Scan for the first child which could contain pTerm/nTerm. */
       
103172   while( !interiorReaderAtEnd(&reader) ){
       
103173     if( interiorReaderTermCmp(&reader, pTerm, nTerm, 0)>0 ) break;
       
103174     interiorReaderStep(&reader);
       
103175   }
       
103176   *piStartChild = interiorReaderCurrentBlockid(&reader);
       
103177 
       
103178   /* Keep scanning to find a term greater than our term, using prefix
       
103179   ** comparison if indicated.  If isPrefix is false, this will be the
       
103180   ** same blockid as the starting block.
       
103181   */
       
103182   while( !interiorReaderAtEnd(&reader) ){
       
103183     if( interiorReaderTermCmp(&reader, pTerm, nTerm, isPrefix)>0 ) break;
       
103184     interiorReaderStep(&reader);
       
103185   }
       
103186   *piEndChild = interiorReaderCurrentBlockid(&reader);
       
103187 
       
103188   interiorReaderDestroy(&reader);
       
103189 
       
103190   /* Children must ascend, and if !prefix, both must be the same. */
       
103191   assert( *piEndChild>=*piStartChild );
       
103192   assert( isPrefix || *piStartChild==*piEndChild );
       
103193 }
       
103194 
       
103195 /* Read block at iBlockid and pass it with other params to
       
103196 ** getChildrenContaining().
       
103197 */
       
103198 static int loadAndGetChildrenContaining(
       
103199   fulltext_vtab *v,
       
103200   sqlite_int64 iBlockid,
       
103201   const char *pTerm, int nTerm, int isPrefix,
       
103202   sqlite_int64 *piStartChild, sqlite_int64 *piEndChild
       
103203 ){
       
103204   sqlite3_stmt *s = NULL;
       
103205   int rc;
       
103206 
       
103207   assert( iBlockid!=0 );
       
103208   assert( pTerm!=NULL );
       
103209   assert( nTerm!=0 );        /* TODO(shess) Why not allow this? */
       
103210   assert( piStartChild!=NULL );
       
103211   assert( piEndChild!=NULL );
       
103212 
       
103213   rc = sql_get_statement(v, BLOCK_SELECT_STMT, &s);
       
103214   if( rc!=SQLITE_OK ) return rc;
       
103215 
       
103216   rc = sqlite3_bind_int64(s, 1, iBlockid);
       
103217   if( rc!=SQLITE_OK ) return rc;
       
103218 
       
103219   rc = sqlite3_step(s);
       
103220   if( rc==SQLITE_DONE ) return SQLITE_ERROR;
       
103221   if( rc!=SQLITE_ROW ) return rc;
       
103222 
       
103223   getChildrenContaining(sqlite3_column_blob(s, 0), sqlite3_column_bytes(s, 0),
       
103224                         pTerm, nTerm, isPrefix, piStartChild, piEndChild);
       
103225 
       
103226   /* We expect only one row.  We must execute another sqlite3_step()
       
103227    * to complete the iteration; otherwise the table will remain
       
103228    * locked. */
       
103229   rc = sqlite3_step(s);
       
103230   if( rc==SQLITE_ROW ) return SQLITE_ERROR;
       
103231   if( rc!=SQLITE_DONE ) return rc;
       
103232 
       
103233   return SQLITE_OK;
       
103234 }
       
103235 
       
103236 /* Traverse the tree represented by pData[nData] looking for
       
103237 ** pTerm[nTerm], placing its doclist into *out.  This is internal to
       
103238 ** loadSegment() to make error-handling cleaner.
       
103239 */
       
103240 static int loadSegmentInt(fulltext_vtab *v, const char *pData, int nData,
       
103241                           sqlite_int64 iLeavesEnd,
       
103242                           const char *pTerm, int nTerm, int isPrefix,
       
103243                           DataBuffer *out){
       
103244   /* Special case where root is a leaf. */
       
103245   if( *pData=='\0' ){
       
103246     return loadSegmentLeaf(v, pData, nData, pTerm, nTerm, isPrefix, out);
       
103247   }else{
       
103248     int rc;
       
103249     sqlite_int64 iStartChild, iEndChild;
       
103250 
       
103251     /* Process pData as an interior node, then loop down the tree
       
103252     ** until we find the set of leaf nodes to scan for the term.
       
103253     */
       
103254     getChildrenContaining(pData, nData, pTerm, nTerm, isPrefix,
       
103255                           &iStartChild, &iEndChild);
       
103256     while( iStartChild>iLeavesEnd ){
       
103257       sqlite_int64 iNextStart, iNextEnd;
       
103258       rc = loadAndGetChildrenContaining(v, iStartChild, pTerm, nTerm, isPrefix,
       
103259                                         &iNextStart, &iNextEnd);
       
103260       if( rc!=SQLITE_OK ) return rc;
       
103261 
       
103262       /* If we've branched, follow the end branch, too. */
       
103263       if( iStartChild!=iEndChild ){
       
103264         sqlite_int64 iDummy;
       
103265         rc = loadAndGetChildrenContaining(v, iEndChild, pTerm, nTerm, isPrefix,
       
103266                                           &iDummy, &iNextEnd);
       
103267         if( rc!=SQLITE_OK ) return rc;
       
103268       }
       
103269 
       
103270       assert( iNextStart<=iNextEnd );
       
103271       iStartChild = iNextStart;
       
103272       iEndChild = iNextEnd;
       
103273     }
       
103274     assert( iStartChild<=iLeavesEnd );
       
103275     assert( iEndChild<=iLeavesEnd );
       
103276 
       
103277     /* Scan through the leaf segments for doclists. */
       
103278     return loadSegmentLeaves(v, iStartChild, iEndChild,
       
103279                              pTerm, nTerm, isPrefix, out);
       
103280   }
       
103281 }
       
103282 
       
103283 /* Call loadSegmentInt() to collect the doclist for pTerm/nTerm, then
       
103284 ** merge its doclist over *out (any duplicate doclists read from the
       
103285 ** segment rooted at pData will overwrite those in *out).
       
103286 */
       
103287 /* TODO(shess) Consider changing this to determine the depth of the
       
103288 ** leaves using either the first characters of interior nodes (when
       
103289 ** ==1, we're one level above the leaves), or the first character of
       
103290 ** the root (which will describe the height of the tree directly).
       
103291 ** Either feels somewhat tricky to me.
       
103292 */
       
103293 /* TODO(shess) The current merge is likely to be slow for large
       
103294 ** doclists (though it should process from newest/smallest to
       
103295 ** oldest/largest, so it may not be that bad).  It might be useful to
       
103296 ** modify things to allow for N-way merging.  This could either be
       
103297 ** within a segment, with pairwise merges across segments, or across
       
103298 ** all segments at once.
       
103299 */
       
103300 static int loadSegment(fulltext_vtab *v, const char *pData, int nData,
       
103301                        sqlite_int64 iLeavesEnd,
       
103302                        const char *pTerm, int nTerm, int isPrefix,
       
103303                        DataBuffer *out){
       
103304   DataBuffer result;
       
103305   int rc;
       
103306 
       
103307   assert( nData>1 );
       
103308 
       
103309   /* This code should never be called with buffered updates. */
       
103310   assert( v->nPendingData<0 );
       
103311 
       
103312   dataBufferInit(&result, 0);
       
103313   rc = loadSegmentInt(v, pData, nData, iLeavesEnd,
       
103314                       pTerm, nTerm, isPrefix, &result);
       
103315   if( rc==SQLITE_OK && result.nData>0 ){
       
103316     if( out->nData==0 ){
       
103317       DataBuffer tmp = *out;
       
103318       *out = result;
       
103319       result = tmp;
       
103320     }else{
       
103321       DataBuffer merged;
       
103322       DLReader readers[2];
       
103323 
       
103324       dlrInit(&readers[0], DL_DEFAULT, out->pData, out->nData);
       
103325       dlrInit(&readers[1], DL_DEFAULT, result.pData, result.nData);
       
103326       dataBufferInit(&merged, out->nData+result.nData);
       
103327       docListMerge(&merged, readers, 2);
       
103328       dataBufferDestroy(out);
       
103329       *out = merged;
       
103330       dlrDestroy(&readers[0]);
       
103331       dlrDestroy(&readers[1]);
       
103332     }
       
103333   }
       
103334   dataBufferDestroy(&result);
       
103335   return rc;
       
103336 }
       
103337 
       
103338 /* Scan the database and merge together the posting lists for the term
       
103339 ** into *out.
       
103340 */
       
103341 static int termSelect(
       
103342   fulltext_vtab *v, 
       
103343   int iColumn,
       
103344   const char *pTerm, int nTerm,             /* Term to query for */
       
103345   int isPrefix,                             /* True for a prefix search */
       
103346   DocListType iType, 
       
103347   DataBuffer *out                           /* Write results here */
       
103348 ){
       
103349   DataBuffer doclist;
       
103350   sqlite3_stmt *s;
       
103351   int rc = sql_get_statement(v, SEGDIR_SELECT_ALL_STMT, &s);
       
103352   if( rc!=SQLITE_OK ) return rc;
       
103353 
       
103354   /* This code should never be called with buffered updates. */
       
103355   assert( v->nPendingData<0 );
       
103356 
       
103357   dataBufferInit(&doclist, 0);
       
103358   dataBufferInit(out, 0);
       
103359 
       
103360   /* Traverse the segments from oldest to newest so that newer doclist
       
103361   ** elements for given docids overwrite older elements.
       
103362   */
       
103363   while( (rc = sqlite3_step(s))==SQLITE_ROW ){
       
103364     const char *pData = sqlite3_column_blob(s, 2);
       
103365     const int nData = sqlite3_column_bytes(s, 2);
       
103366     const sqlite_int64 iLeavesEnd = sqlite3_column_int64(s, 1);
       
103367     rc = loadSegment(v, pData, nData, iLeavesEnd, pTerm, nTerm, isPrefix,
       
103368                      &doclist);
       
103369     if( rc!=SQLITE_OK ) goto err;
       
103370   }
       
103371   if( rc==SQLITE_DONE ){
       
103372     if( doclist.nData!=0 ){
       
103373       /* TODO(shess) The old term_select_all() code applied the column
       
103374       ** restrict as we merged segments, leading to smaller buffers.
       
103375       ** This is probably worthwhile to bring back, once the new storage
       
103376       ** system is checked in.
       
103377       */
       
103378       if( iColumn==v->nColumn) iColumn = -1;
       
103379       docListTrim(DL_DEFAULT, doclist.pData, doclist.nData,
       
103380                   iColumn, iType, out);
       
103381     }
       
103382     rc = SQLITE_OK;
       
103383   }
       
103384 
       
103385  err:
       
103386   dataBufferDestroy(&doclist);
       
103387   return rc;
       
103388 }
       
103389 
       
103390 /****************************************************************/
       
103391 /* Used to hold hashtable data for sorting. */
       
103392 typedef struct TermData {
       
103393   const char *pTerm;
       
103394   int nTerm;
       
103395   DLCollector *pCollector;
       
103396 } TermData;
       
103397 
       
103398 /* Orders TermData elements in strcmp fashion ( <0 for less-than, 0
       
103399 ** for equal, >0 for greater-than).
       
103400 */
       
103401 static int termDataCmp(const void *av, const void *bv){
       
103402   const TermData *a = (const TermData *)av;
       
103403   const TermData *b = (const TermData *)bv;
       
103404   int n = a->nTerm<b->nTerm ? a->nTerm : b->nTerm;
       
103405   int c = memcmp(a->pTerm, b->pTerm, n);
       
103406   if( c!=0 ) return c;
       
103407   return a->nTerm-b->nTerm;
       
103408 }
       
103409 
       
103410 /* Order pTerms data by term, then write a new level 0 segment using
       
103411 ** LeafWriter.
       
103412 */
       
103413 static int writeZeroSegment(fulltext_vtab *v, fts3Hash *pTerms){
       
103414   fts3HashElem *e;
       
103415   int idx, rc, i, n;
       
103416   TermData *pData;
       
103417   LeafWriter writer;
       
103418   DataBuffer dl;
       
103419 
       
103420   /* Determine the next index at level 0, merging as necessary. */
       
103421   rc = segdirNextIndex(v, 0, &idx);
       
103422   if( rc!=SQLITE_OK ) return rc;
       
103423 
       
103424   n = fts3HashCount(pTerms);
       
103425   pData = sqlite3_malloc(n*sizeof(TermData));
       
103426 
       
103427   for(i = 0, e = fts3HashFirst(pTerms); e; i++, e = fts3HashNext(e)){
       
103428     assert( i<n );
       
103429     pData[i].pTerm = fts3HashKey(e);
       
103430     pData[i].nTerm = fts3HashKeysize(e);
       
103431     pData[i].pCollector = fts3HashData(e);
       
103432   }
       
103433   assert( i==n );
       
103434 
       
103435   /* TODO(shess) Should we allow user-defined collation sequences,
       
103436   ** here?  I think we only need that once we support prefix searches.
       
103437   */
       
103438   if( n>1 ) qsort(pData, n, sizeof(*pData), termDataCmp);
       
103439 
       
103440   /* TODO(shess) Refactor so that we can write directly to the segment
       
103441   ** DataBuffer, as happens for segment merges.
       
103442   */
       
103443   leafWriterInit(0, idx, &writer);
       
103444   dataBufferInit(&dl, 0);
       
103445   for(i=0; i<n; i++){
       
103446     dataBufferReset(&dl);
       
103447     dlcAddDoclist(pData[i].pCollector, &dl);
       
103448     rc = leafWriterStep(v, &writer,
       
103449                         pData[i].pTerm, pData[i].nTerm, dl.pData, dl.nData);
       
103450     if( rc!=SQLITE_OK ) goto err;
       
103451   }
       
103452   rc = leafWriterFinalize(v, &writer);
       
103453 
       
103454  err:
       
103455   dataBufferDestroy(&dl);
       
103456   sqlite3_free(pData);
       
103457   leafWriterDestroy(&writer);
       
103458   return rc;
       
103459 }
       
103460 
       
103461 /* If pendingTerms has data, free it. */
       
103462 static int clearPendingTerms(fulltext_vtab *v){
       
103463   if( v->nPendingData>=0 ){
       
103464     fts3HashElem *e;
       
103465     for(e=fts3HashFirst(&v->pendingTerms); e; e=fts3HashNext(e)){
       
103466       dlcDelete(fts3HashData(e));
       
103467     }
       
103468     fts3HashClear(&v->pendingTerms);
       
103469     v->nPendingData = -1;
       
103470   }
       
103471   return SQLITE_OK;
       
103472 }
       
103473 
       
103474 /* If pendingTerms has data, flush it to a level-zero segment, and
       
103475 ** free it.
       
103476 */
       
103477 static int flushPendingTerms(fulltext_vtab *v){
       
103478   if( v->nPendingData>=0 ){
       
103479     int rc = writeZeroSegment(v, &v->pendingTerms);
       
103480     if( rc==SQLITE_OK ) clearPendingTerms(v);
       
103481     return rc;
       
103482   }
       
103483   return SQLITE_OK;
       
103484 }
       
103485 
       
103486 /* If pendingTerms is "too big", or docid is out of order, flush it.
       
103487 ** Regardless, be certain that pendingTerms is initialized for use.
       
103488 */
       
103489 static int initPendingTerms(fulltext_vtab *v, sqlite_int64 iDocid){
       
103490   /* TODO(shess) Explore whether partially flushing the buffer on
       
103491   ** forced-flush would provide better performance.  I suspect that if
       
103492   ** we ordered the doclists by size and flushed the largest until the
       
103493   ** buffer was half empty, that would let the less frequent terms
       
103494   ** generate longer doclists.
       
103495   */
       
103496   if( iDocid<=v->iPrevDocid || v->nPendingData>kPendingThreshold ){
       
103497     int rc = flushPendingTerms(v);
       
103498     if( rc!=SQLITE_OK ) return rc;
       
103499   }
       
103500   if( v->nPendingData<0 ){
       
103501     fts3HashInit(&v->pendingTerms, FTS3_HASH_STRING, 1);
       
103502     v->nPendingData = 0;
       
103503   }
       
103504   v->iPrevDocid = iDocid;
       
103505   return SQLITE_OK;
       
103506 }
       
103507 
       
103508 /* This function implements the xUpdate callback; it is the top-level entry
       
103509  * point for inserting, deleting or updating a row in a full-text table. */
       
103510 static int fulltextUpdate(sqlite3_vtab *pVtab, int nArg, sqlite3_value **ppArg,
       
103511                           sqlite_int64 *pRowid){
       
103512   fulltext_vtab *v = (fulltext_vtab *) pVtab;
       
103513   int rc;
       
103514 
       
103515   FTSTRACE(("FTS3 Update %p\n", pVtab));
       
103516 
       
103517   if( nArg<2 ){
       
103518     rc = index_delete(v, sqlite3_value_int64(ppArg[0]));
       
103519     if( rc==SQLITE_OK ){
       
103520       /* If we just deleted the last row in the table, clear out the
       
103521       ** index data.
       
103522       */
       
103523       rc = content_exists(v);
       
103524       if( rc==SQLITE_ROW ){
       
103525         rc = SQLITE_OK;
       
103526       }else if( rc==SQLITE_DONE ){
       
103527         /* Clear the pending terms so we don't flush a useless level-0
       
103528         ** segment when the transaction closes.
       
103529         */
       
103530         rc = clearPendingTerms(v);
       
103531         if( rc==SQLITE_OK ){
       
103532           rc = segdir_delete_all(v);
       
103533         }
       
103534       }
       
103535     }
       
103536   } else if( sqlite3_value_type(ppArg[0]) != SQLITE_NULL ){
       
103537     /* An update:
       
103538      * ppArg[0] = old rowid
       
103539      * ppArg[1] = new rowid
       
103540      * ppArg[2..2+v->nColumn-1] = values
       
103541      * ppArg[2+v->nColumn] = value for magic column (we ignore this)
       
103542      * ppArg[2+v->nColumn+1] = value for docid
       
103543      */
       
103544     sqlite_int64 rowid = sqlite3_value_int64(ppArg[0]);
       
103545     if( sqlite3_value_type(ppArg[1]) != SQLITE_INTEGER ||
       
103546         sqlite3_value_int64(ppArg[1]) != rowid ){
       
103547       rc = SQLITE_ERROR;  /* we don't allow changing the rowid */
       
103548     }else if( sqlite3_value_type(ppArg[2+v->nColumn+1]) != SQLITE_INTEGER ||
       
103549               sqlite3_value_int64(ppArg[2+v->nColumn+1]) != rowid ){
       
103550       rc = SQLITE_ERROR;  /* we don't allow changing the docid */
       
103551     }else{
       
103552       assert( nArg==2+v->nColumn+2);
       
103553       rc = index_update(v, rowid, &ppArg[2]);
       
103554     }
       
103555   } else {
       
103556     /* An insert:
       
103557      * ppArg[1] = requested rowid
       
103558      * ppArg[2..2+v->nColumn-1] = values
       
103559      * ppArg[2+v->nColumn] = value for magic column (we ignore this)
       
103560      * ppArg[2+v->nColumn+1] = value for docid
       
103561      */
       
103562     sqlite3_value *pRequestDocid = ppArg[2+v->nColumn+1];
       
103563     assert( nArg==2+v->nColumn+2);
       
103564     if( SQLITE_NULL != sqlite3_value_type(pRequestDocid) &&
       
103565         SQLITE_NULL != sqlite3_value_type(ppArg[1]) ){
       
103566       /* TODO(shess) Consider allowing this to work if the values are
       
103567       ** identical.  I'm inclined to discourage that usage, though,
       
103568       ** given that both rowid and docid are special columns.  Better
       
103569       ** would be to define one or the other as the default winner,
       
103570       ** but should it be fts3-centric (docid) or SQLite-centric
       
103571       ** (rowid)?
       
103572       */
       
103573       rc = SQLITE_ERROR;
       
103574     }else{
       
103575       if( SQLITE_NULL == sqlite3_value_type(pRequestDocid) ){
       
103576         pRequestDocid = ppArg[1];
       
103577       }
       
103578       rc = index_insert(v, pRequestDocid, &ppArg[2], pRowid);
       
103579     }
       
103580   }
       
103581 
       
103582   return rc;
       
103583 }
       
103584 
       
103585 static int fulltextSync(sqlite3_vtab *pVtab){
       
103586   FTSTRACE(("FTS3 xSync()\n"));
       
103587   return flushPendingTerms((fulltext_vtab *)pVtab);
       
103588 }
       
103589 
       
103590 static int fulltextBegin(sqlite3_vtab *pVtab){
       
103591   fulltext_vtab *v = (fulltext_vtab *) pVtab;
       
103592   FTSTRACE(("FTS3 xBegin()\n"));
       
103593 
       
103594   /* Any buffered updates should have been cleared by the previous
       
103595   ** transaction.
       
103596   */
       
103597   assert( v->nPendingData<0 );
       
103598   return clearPendingTerms(v);
       
103599 }
       
103600 
       
103601 static int fulltextCommit(sqlite3_vtab *pVtab){
       
103602   fulltext_vtab *v = (fulltext_vtab *) pVtab;
       
103603   FTSTRACE(("FTS3 xCommit()\n"));
       
103604 
       
103605   /* Buffered updates should have been cleared by fulltextSync(). */
       
103606   assert( v->nPendingData<0 );
       
103607   return clearPendingTerms(v);
       
103608 }
       
103609 
       
103610 static int fulltextRollback(sqlite3_vtab *pVtab){
       
103611   FTSTRACE(("FTS3 xRollback()\n"));
       
103612   return clearPendingTerms((fulltext_vtab *)pVtab);
       
103613 }
       
103614 
       
103615 /*
       
103616 ** Implementation of the snippet() function for FTS3
       
103617 */
       
103618 static void snippetFunc(
       
103619   sqlite3_context *pContext,
       
103620   int argc,
       
103621   sqlite3_value **argv
       
103622 ){
       
103623   fulltext_cursor *pCursor;
       
103624   if( argc<1 ) return;
       
103625   if( sqlite3_value_type(argv[0])!=SQLITE_BLOB ||
       
103626       sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){
       
103627     sqlite3_result_error(pContext, "illegal first argument to html_snippet",-1);
       
103628   }else{
       
103629     const char *zStart = "<b>";
       
103630     const char *zEnd = "</b>";
       
103631     const char *zEllipsis = "<b>...</b>";
       
103632     memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor));
       
103633     if( argc>=2 ){
       
103634       zStart = (const char*)sqlite3_value_text(argv[1]);
       
103635       if( argc>=3 ){
       
103636         zEnd = (const char*)sqlite3_value_text(argv[2]);
       
103637         if( argc>=4 ){
       
103638           zEllipsis = (const char*)sqlite3_value_text(argv[3]);
       
103639         }
       
103640       }
       
103641     }
       
103642     snippetAllOffsets(pCursor);
       
103643     snippetText(pCursor, zStart, zEnd, zEllipsis);
       
103644     sqlite3_result_text(pContext, pCursor->snippet.zSnippet,
       
103645                         pCursor->snippet.nSnippet, SQLITE_STATIC);
       
103646   }
       
103647 }
       
103648 
       
103649 /*
       
103650 ** Implementation of the offsets() function for FTS3
       
103651 */
       
103652 static void snippetOffsetsFunc(
       
103653   sqlite3_context *pContext,
       
103654   int argc,
       
103655   sqlite3_value **argv
       
103656 ){
       
103657   fulltext_cursor *pCursor;
       
103658   if( argc<1 ) return;
       
103659   if( sqlite3_value_type(argv[0])!=SQLITE_BLOB ||
       
103660       sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){
       
103661     sqlite3_result_error(pContext, "illegal first argument to offsets",-1);
       
103662   }else{
       
103663     memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor));
       
103664     snippetAllOffsets(pCursor);
       
103665     snippetOffsetText(&pCursor->snippet);
       
103666     sqlite3_result_text(pContext,
       
103667                         pCursor->snippet.zOffset, pCursor->snippet.nOffset,
       
103668                         SQLITE_STATIC);
       
103669   }
       
103670 }
       
103671 
       
103672 /* OptLeavesReader is nearly identical to LeavesReader, except that
       
103673 ** where LeavesReader is geared towards the merging of complete
       
103674 ** segment levels (with exactly MERGE_COUNT segments), OptLeavesReader
       
103675 ** is geared towards implementation of the optimize() function, and
       
103676 ** can merge all segments simultaneously.  This version may be
       
103677 ** somewhat less efficient than LeavesReader because it merges into an
       
103678 ** accumulator rather than doing an N-way merge, but since segment
       
103679 ** size grows exponentially (so segment count logrithmically) this is
       
103680 ** probably not an immediate problem.
       
103681 */
       
103682 /* TODO(shess): Prove that assertion, or extend the merge code to
       
103683 ** merge tree fashion (like the prefix-searching code does).
       
103684 */
       
103685 /* TODO(shess): OptLeavesReader and LeavesReader could probably be
       
103686 ** merged with little or no loss of performance for LeavesReader.  The
       
103687 ** merged code would need to handle >MERGE_COUNT segments, and would
       
103688 ** also need to be able to optionally optimize away deletes.
       
103689 */
       
103690 typedef struct OptLeavesReader {
       
103691   /* Segment number, to order readers by age. */
       
103692   int segment;
       
103693   LeavesReader reader;
       
103694 } OptLeavesReader;
       
103695 
       
103696 static int optLeavesReaderAtEnd(OptLeavesReader *pReader){
       
103697   return leavesReaderAtEnd(&pReader->reader);
       
103698 }
       
103699 static int optLeavesReaderTermBytes(OptLeavesReader *pReader){
       
103700   return leavesReaderTermBytes(&pReader->reader);
       
103701 }
       
103702 static const char *optLeavesReaderData(OptLeavesReader *pReader){
       
103703   return leavesReaderData(&pReader->reader);
       
103704 }
       
103705 static int optLeavesReaderDataBytes(OptLeavesReader *pReader){
       
103706   return leavesReaderDataBytes(&pReader->reader);
       
103707 }
       
103708 static const char *optLeavesReaderTerm(OptLeavesReader *pReader){
       
103709   return leavesReaderTerm(&pReader->reader);
       
103710 }
       
103711 static int optLeavesReaderStep(fulltext_vtab *v, OptLeavesReader *pReader){
       
103712   return leavesReaderStep(v, &pReader->reader);
       
103713 }
       
103714 static int optLeavesReaderTermCmp(OptLeavesReader *lr1, OptLeavesReader *lr2){
       
103715   return leavesReaderTermCmp(&lr1->reader, &lr2->reader);
       
103716 }
       
103717 /* Order by term ascending, segment ascending (oldest to newest), with
       
103718 ** exhausted readers to the end.
       
103719 */
       
103720 static int optLeavesReaderCmp(OptLeavesReader *lr1, OptLeavesReader *lr2){
       
103721   int c = optLeavesReaderTermCmp(lr1, lr2);
       
103722   if( c!=0 ) return c;
       
103723   return lr1->segment-lr2->segment;
       
103724 }
       
103725 /* Bubble pLr[0] to appropriate place in pLr[1..nLr-1].  Assumes that
       
103726 ** pLr[1..nLr-1] is already sorted.
       
103727 */
       
103728 static void optLeavesReaderReorder(OptLeavesReader *pLr, int nLr){
       
103729   while( nLr>1 && optLeavesReaderCmp(pLr, pLr+1)>0 ){
       
103730     OptLeavesReader tmp = pLr[0];
       
103731     pLr[0] = pLr[1];
       
103732     pLr[1] = tmp;
       
103733     nLr--;
       
103734     pLr++;
       
103735   }
       
103736 }
       
103737 
       
103738 /* optimize() helper function.  Put the readers in order and iterate
       
103739 ** through them, merging doclists for matching terms into pWriter.
       
103740 ** Returns SQLITE_OK on success, or the SQLite error code which
       
103741 ** prevented success.
       
103742 */
       
103743 static int optimizeInternal(fulltext_vtab *v,
       
103744                             OptLeavesReader *readers, int nReaders,
       
103745                             LeafWriter *pWriter){
       
103746   int i, rc = SQLITE_OK;
       
103747   DataBuffer doclist, merged, tmp;
       
103748 
       
103749   /* Order the readers. */
       
103750   i = nReaders;
       
103751   while( i-- > 0 ){
       
103752     optLeavesReaderReorder(&readers[i], nReaders-i);
       
103753   }
       
103754 
       
103755   dataBufferInit(&doclist, LEAF_MAX);
       
103756   dataBufferInit(&merged, LEAF_MAX);
       
103757 
       
103758   /* Exhausted readers bubble to the end, so when the first reader is
       
103759   ** at eof, all are at eof.
       
103760   */
       
103761   while( !optLeavesReaderAtEnd(&readers[0]) ){
       
103762 
       
103763     /* Figure out how many readers share the next term. */
       
103764     for(i=1; i<nReaders && !optLeavesReaderAtEnd(&readers[i]); i++){
       
103765       if( 0!=optLeavesReaderTermCmp(&readers[0], &readers[i]) ) break;
       
103766     }
       
103767 
       
103768     /* Special-case for no merge. */
       
103769     if( i==1 ){
       
103770       /* Trim deletions from the doclist. */
       
103771       dataBufferReset(&merged);
       
103772       docListTrim(DL_DEFAULT,
       
103773                   optLeavesReaderData(&readers[0]),
       
103774                   optLeavesReaderDataBytes(&readers[0]),
       
103775                   -1, DL_DEFAULT, &merged);
       
103776     }else{
       
103777       DLReader dlReaders[MERGE_COUNT];
       
103778       int iReader, nReaders;
       
103779 
       
103780       /* Prime the pipeline with the first reader's doclist.  After
       
103781       ** one pass index 0 will reference the accumulated doclist.
       
103782       */
       
103783       dlrInit(&dlReaders[0], DL_DEFAULT,
       
103784               optLeavesReaderData(&readers[0]),
       
103785               optLeavesReaderDataBytes(&readers[0]));
       
103786       iReader = 1;
       
103787 
       
103788       assert( iReader<i );  /* Must execute the loop at least once. */
       
103789       while( iReader<i ){
       
103790         /* Merge 16 inputs per pass. */
       
103791         for( nReaders=1; iReader<i && nReaders<MERGE_COUNT;
       
103792              iReader++, nReaders++ ){
       
103793           dlrInit(&dlReaders[nReaders], DL_DEFAULT,
       
103794                   optLeavesReaderData(&readers[iReader]),
       
103795                   optLeavesReaderDataBytes(&readers[iReader]));
       
103796         }
       
103797 
       
103798         /* Merge doclists and swap result into accumulator. */
       
103799         dataBufferReset(&merged);
       
103800         docListMerge(&merged, dlReaders, nReaders);
       
103801         tmp = merged;
       
103802         merged = doclist;
       
103803         doclist = tmp;
       
103804 
       
103805         while( nReaders-- > 0 ){
       
103806           dlrDestroy(&dlReaders[nReaders]);
       
103807         }
       
103808 
       
103809         /* Accumulated doclist to reader 0 for next pass. */
       
103810         dlrInit(&dlReaders[0], DL_DEFAULT, doclist.pData, doclist.nData);
       
103811       }
       
103812 
       
103813       /* Destroy reader that was left in the pipeline. */
       
103814       dlrDestroy(&dlReaders[0]);
       
103815 
       
103816       /* Trim deletions from the doclist. */
       
103817       dataBufferReset(&merged);
       
103818       docListTrim(DL_DEFAULT, doclist.pData, doclist.nData,
       
103819                   -1, DL_DEFAULT, &merged);
       
103820     }
       
103821 
       
103822     /* Only pass doclists with hits (skip if all hits deleted). */
       
103823     if( merged.nData>0 ){
       
103824       rc = leafWriterStep(v, pWriter,
       
103825                           optLeavesReaderTerm(&readers[0]),
       
103826                           optLeavesReaderTermBytes(&readers[0]),
       
103827                           merged.pData, merged.nData);
       
103828       if( rc!=SQLITE_OK ) goto err;
       
103829     }
       
103830 
       
103831     /* Step merged readers to next term and reorder. */
       
103832     while( i-- > 0 ){
       
103833       rc = optLeavesReaderStep(v, &readers[i]);
       
103834       if( rc!=SQLITE_OK ) goto err;
       
103835 
       
103836       optLeavesReaderReorder(&readers[i], nReaders-i);
       
103837     }
       
103838   }
       
103839 
       
103840  err:
       
103841   dataBufferDestroy(&doclist);
       
103842   dataBufferDestroy(&merged);
       
103843   return rc;
       
103844 }
       
103845 
       
103846 /* Implement optimize() function for FTS3.  optimize(t) merges all
       
103847 ** segments in the fts index into a single segment.  't' is the magic
       
103848 ** table-named column.
       
103849 */
       
103850 static void optimizeFunc(sqlite3_context *pContext,
       
103851                          int argc, sqlite3_value **argv){
       
103852   fulltext_cursor *pCursor;
       
103853   if( argc>1 ){
       
103854     sqlite3_result_error(pContext, "excess arguments to optimize()",-1);
       
103855   }else if( sqlite3_value_type(argv[0])!=SQLITE_BLOB ||
       
103856             sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){
       
103857     sqlite3_result_error(pContext, "illegal first argument to optimize",-1);
       
103858   }else{
       
103859     fulltext_vtab *v;
       
103860     int i, rc, iMaxLevel;
       
103861     OptLeavesReader *readers;
       
103862     int nReaders;
       
103863     LeafWriter writer;
       
103864     sqlite3_stmt *s;
       
103865 
       
103866     memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor));
       
103867     v = cursor_vtab(pCursor);
       
103868 
       
103869     /* Flush any buffered updates before optimizing. */
       
103870     rc = flushPendingTerms(v);
       
103871     if( rc!=SQLITE_OK ) goto err;
       
103872 
       
103873     rc = segdir_count(v, &nReaders, &iMaxLevel);
       
103874     if( rc!=SQLITE_OK ) goto err;
       
103875     if( nReaders==0 || nReaders==1 ){
       
103876       sqlite3_result_text(pContext, "Index already optimal", -1,
       
103877                           SQLITE_STATIC);
       
103878       return;
       
103879     }
       
103880 
       
103881     rc = sql_get_statement(v, SEGDIR_SELECT_ALL_STMT, &s);
       
103882     if( rc!=SQLITE_OK ) goto err;
       
103883 
       
103884     readers = sqlite3_malloc(nReaders*sizeof(readers[0]));
       
103885     if( readers==NULL ) goto err;
       
103886 
       
103887     /* Note that there will already be a segment at this position
       
103888     ** until we call segdir_delete() on iMaxLevel.
       
103889     */
       
103890     leafWriterInit(iMaxLevel, 0, &writer);
       
103891 
       
103892     i = 0;
       
103893     while( (rc = sqlite3_step(s))==SQLITE_ROW ){
       
103894       sqlite_int64 iStart = sqlite3_column_int64(s, 0);
       
103895       sqlite_int64 iEnd = sqlite3_column_int64(s, 1);
       
103896       const char *pRootData = sqlite3_column_blob(s, 2);
       
103897       int nRootData = sqlite3_column_bytes(s, 2);
       
103898 
       
103899       assert( i<nReaders );
       
103900       rc = leavesReaderInit(v, -1, iStart, iEnd, pRootData, nRootData,
       
103901                             &readers[i].reader);
       
103902       if( rc!=SQLITE_OK ) break;
       
103903 
       
103904       readers[i].segment = i;
       
103905       i++;
       
103906     }
       
103907 
       
103908     /* If we managed to successfully read them all, optimize them. */
       
103909     if( rc==SQLITE_DONE ){
       
103910       assert( i==nReaders );
       
103911       rc = optimizeInternal(v, readers, nReaders, &writer);
       
103912     }
       
103913 
       
103914     while( i-- > 0 ){
       
103915       leavesReaderDestroy(&readers[i].reader);
       
103916     }
       
103917     sqlite3_free(readers);
       
103918 
       
103919     /* If we've successfully gotten to here, delete the old segments
       
103920     ** and flush the interior structure of the new segment.
       
103921     */
       
103922     if( rc==SQLITE_OK ){
       
103923       for( i=0; i<=iMaxLevel; i++ ){
       
103924         rc = segdir_delete(v, i);
       
103925         if( rc!=SQLITE_OK ) break;
       
103926       }
       
103927 
       
103928       if( rc==SQLITE_OK ) rc = leafWriterFinalize(v, &writer);
       
103929     }
       
103930 
       
103931     leafWriterDestroy(&writer);
       
103932 
       
103933     if( rc!=SQLITE_OK ) goto err;
       
103934 
       
103935     sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
       
103936     return;
       
103937 
       
103938     /* TODO(shess): Error-handling needs to be improved along the
       
103939     ** lines of the dump_ functions.
       
103940     */
       
103941  err:
       
103942     {
       
103943       char buf[512];
       
103944       sqlite3_snprintf(sizeof(buf), buf, "Error in optimize: %s",
       
103945                        sqlite3_errmsg(sqlite3_context_db_handle(pContext)));
       
103946       sqlite3_result_error(pContext, buf, -1);
       
103947     }
       
103948   }
       
103949 }
       
103950 
       
103951 #ifdef SQLITE_TEST
       
103952 /* Generate an error of the form "<prefix>: <msg>".  If msg is NULL,
       
103953 ** pull the error from the context's db handle.
       
103954 */
       
103955 static void generateError(sqlite3_context *pContext,
       
103956                           const char *prefix, const char *msg){
       
103957   char buf[512];
       
103958   if( msg==NULL ) msg = sqlite3_errmsg(sqlite3_context_db_handle(pContext));
       
103959   sqlite3_snprintf(sizeof(buf), buf, "%s: %s", prefix, msg);
       
103960   sqlite3_result_error(pContext, buf, -1);
       
103961 }
       
103962 
       
103963 /* Helper function to collect the set of terms in the segment into
       
103964 ** pTerms.  The segment is defined by the leaf nodes between
       
103965 ** iStartBlockid and iEndBlockid, inclusive, or by the contents of
       
103966 ** pRootData if iStartBlockid is 0 (in which case the entire segment
       
103967 ** fit in a leaf).
       
103968 */
       
103969 static int collectSegmentTerms(fulltext_vtab *v, sqlite3_stmt *s,
       
103970                                fts3Hash *pTerms){
       
103971   const sqlite_int64 iStartBlockid = sqlite3_column_int64(s, 0);
       
103972   const sqlite_int64 iEndBlockid = sqlite3_column_int64(s, 1);
       
103973   const char *pRootData = sqlite3_column_blob(s, 2);
       
103974   const int nRootData = sqlite3_column_bytes(s, 2);
       
103975   LeavesReader reader;
       
103976   int rc = leavesReaderInit(v, 0, iStartBlockid, iEndBlockid,
       
103977                             pRootData, nRootData, &reader);
       
103978   if( rc!=SQLITE_OK ) return rc;
       
103979 
       
103980   while( rc==SQLITE_OK && !leavesReaderAtEnd(&reader) ){
       
103981     const char *pTerm = leavesReaderTerm(&reader);
       
103982     const int nTerm = leavesReaderTermBytes(&reader);
       
103983     void *oldValue = sqlite3Fts3HashFind(pTerms, pTerm, nTerm);
       
103984     void *newValue = (void *)((char *)oldValue+1);
       
103985 
       
103986     /* From the comment before sqlite3Fts3HashInsert in fts3_hash.c,
       
103987     ** the data value passed is returned in case of malloc failure.
       
103988     */
       
103989     if( newValue==sqlite3Fts3HashInsert(pTerms, pTerm, nTerm, newValue) ){
       
103990       rc = SQLITE_NOMEM;
       
103991     }else{
       
103992       rc = leavesReaderStep(v, &reader);
       
103993     }
       
103994   }
       
103995 
       
103996   leavesReaderDestroy(&reader);
       
103997   return rc;
       
103998 }
       
103999 
       
104000 /* Helper function to build the result string for dump_terms(). */
       
104001 static int generateTermsResult(sqlite3_context *pContext, fts3Hash *pTerms){
       
104002   int iTerm, nTerms, nResultBytes, iByte;
       
104003   char *result;
       
104004   TermData *pData;
       
104005   fts3HashElem *e;
       
104006 
       
104007   /* Iterate pTerms to generate an array of terms in pData for
       
104008   ** sorting.
       
104009   */
       
104010   nTerms = fts3HashCount(pTerms);
       
104011   assert( nTerms>0 );
       
104012   pData = sqlite3_malloc(nTerms*sizeof(TermData));
       
104013   if( pData==NULL ) return SQLITE_NOMEM;
       
104014 
       
104015   nResultBytes = 0;
       
104016   for(iTerm = 0, e = fts3HashFirst(pTerms); e; iTerm++, e = fts3HashNext(e)){
       
104017     nResultBytes += fts3HashKeysize(e)+1;   /* Term plus trailing space */
       
104018     assert( iTerm<nTerms );
       
104019     pData[iTerm].pTerm = fts3HashKey(e);
       
104020     pData[iTerm].nTerm = fts3HashKeysize(e);
       
104021     pData[iTerm].pCollector = fts3HashData(e);  /* unused */
       
104022   }
       
104023   assert( iTerm==nTerms );
       
104024 
       
104025   assert( nResultBytes>0 );   /* nTerms>0, nResultsBytes must be, too. */
       
104026   result = sqlite3_malloc(nResultBytes);
       
104027   if( result==NULL ){
       
104028     sqlite3_free(pData);
       
104029     return SQLITE_NOMEM;
       
104030   }
       
104031 
       
104032   if( nTerms>1 ) qsort(pData, nTerms, sizeof(*pData), termDataCmp);
       
104033 
       
104034   /* Read the terms in order to build the result. */
       
104035   iByte = 0;
       
104036   for(iTerm=0; iTerm<nTerms; ++iTerm){
       
104037     memcpy(result+iByte, pData[iTerm].pTerm, pData[iTerm].nTerm);
       
104038     iByte += pData[iTerm].nTerm;
       
104039     result[iByte++] = ' ';
       
104040   }
       
104041   assert( iByte==nResultBytes );
       
104042   assert( result[nResultBytes-1]==' ' );
       
104043   result[nResultBytes-1] = '\0';
       
104044 
       
104045   /* Passes away ownership of result. */
       
104046   sqlite3_result_text(pContext, result, nResultBytes-1, sqlite3_free);
       
104047   sqlite3_free(pData);
       
104048   return SQLITE_OK;
       
104049 }
       
104050 
       
104051 /* Implements dump_terms() for use in inspecting the fts3 index from
       
104052 ** tests.  TEXT result containing the ordered list of terms joined by
       
104053 ** spaces.  dump_terms(t, level, idx) dumps the terms for the segment
       
104054 ** specified by level, idx (in %_segdir), while dump_terms(t) dumps
       
104055 ** all terms in the index.  In both cases t is the fts table's magic
       
104056 ** table-named column.
       
104057 */
       
104058 static void dumpTermsFunc(
       
104059   sqlite3_context *pContext,
       
104060   int argc, sqlite3_value **argv
       
104061 ){
       
104062   fulltext_cursor *pCursor;
       
104063   if( argc!=3 && argc!=1 ){
       
104064     generateError(pContext, "dump_terms", "incorrect arguments");
       
104065   }else if( sqlite3_value_type(argv[0])!=SQLITE_BLOB ||
       
104066             sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){
       
104067     generateError(pContext, "dump_terms", "illegal first argument");
       
104068   }else{
       
104069     fulltext_vtab *v;
       
104070     fts3Hash terms;
       
104071     sqlite3_stmt *s = NULL;
       
104072     int rc;
       
104073 
       
104074     memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor));
       
104075     v = cursor_vtab(pCursor);
       
104076 
       
104077     /* If passed only the cursor column, get all segments.  Otherwise
       
104078     ** get the segment described by the following two arguments.
       
104079     */
       
104080     if( argc==1 ){
       
104081       rc = sql_get_statement(v, SEGDIR_SELECT_ALL_STMT, &s);
       
104082     }else{
       
104083       rc = sql_get_statement(v, SEGDIR_SELECT_SEGMENT_STMT, &s);
       
104084       if( rc==SQLITE_OK ){
       
104085         rc = sqlite3_bind_int(s, 1, sqlite3_value_int(argv[1]));
       
104086         if( rc==SQLITE_OK ){
       
104087           rc = sqlite3_bind_int(s, 2, sqlite3_value_int(argv[2]));
       
104088         }
       
104089       }
       
104090     }
       
104091 
       
104092     if( rc!=SQLITE_OK ){
       
104093       generateError(pContext, "dump_terms", NULL);
       
104094       return;
       
104095     }
       
104096 
       
104097     /* Collect the terms for each segment. */
       
104098     sqlite3Fts3HashInit(&terms, FTS3_HASH_STRING, 1);
       
104099     while( (rc = sqlite3_step(s))==SQLITE_ROW ){
       
104100       rc = collectSegmentTerms(v, s, &terms);
       
104101       if( rc!=SQLITE_OK ) break;
       
104102     }
       
104103 
       
104104     if( rc!=SQLITE_DONE ){
       
104105       sqlite3_reset(s);
       
104106       generateError(pContext, "dump_terms", NULL);
       
104107     }else{
       
104108       const int nTerms = fts3HashCount(&terms);
       
104109       if( nTerms>0 ){
       
104110         rc = generateTermsResult(pContext, &terms);
       
104111         if( rc==SQLITE_NOMEM ){
       
104112           generateError(pContext, "dump_terms", "out of memory");
       
104113         }else{
       
104114           assert( rc==SQLITE_OK );
       
104115         }
       
104116       }else if( argc==3 ){
       
104117         /* The specific segment asked for could not be found. */
       
104118         generateError(pContext, "dump_terms", "segment not found");
       
104119       }else{
       
104120         /* No segments found. */
       
104121         /* TODO(shess): It should be impossible to reach this.  This
       
104122         ** case can only happen for an empty table, in which case
       
104123         ** SQLite has no rows to call this function on.
       
104124         */
       
104125         sqlite3_result_null(pContext);
       
104126       }
       
104127     }
       
104128     sqlite3Fts3HashClear(&terms);
       
104129   }
       
104130 }
       
104131 
       
104132 /* Expand the DL_DEFAULT doclist in pData into a text result in
       
104133 ** pContext.
       
104134 */
       
104135 static void createDoclistResult(sqlite3_context *pContext,
       
104136                                 const char *pData, int nData){
       
104137   DataBuffer dump;
       
104138   DLReader dlReader;
       
104139 
       
104140   assert( pData!=NULL && nData>0 );
       
104141 
       
104142   dataBufferInit(&dump, 0);
       
104143   dlrInit(&dlReader, DL_DEFAULT, pData, nData);
       
104144   for( ; !dlrAtEnd(&dlReader); dlrStep(&dlReader) ){
       
104145     char buf[256];
       
104146     PLReader plReader;
       
104147 
       
104148     plrInit(&plReader, &dlReader);
       
104149     if( DL_DEFAULT==DL_DOCIDS || plrAtEnd(&plReader) ){
       
104150       sqlite3_snprintf(sizeof(buf), buf, "[%lld] ", dlrDocid(&dlReader));
       
104151       dataBufferAppend(&dump, buf, strlen(buf));
       
104152     }else{
       
104153       int iColumn = plrColumn(&plReader);
       
104154 
       
104155       sqlite3_snprintf(sizeof(buf), buf, "[%lld %d[",
       
104156                        dlrDocid(&dlReader), iColumn);
       
104157       dataBufferAppend(&dump, buf, strlen(buf));
       
104158 
       
104159       for( ; !plrAtEnd(&plReader); plrStep(&plReader) ){
       
104160         if( plrColumn(&plReader)!=iColumn ){
       
104161           iColumn = plrColumn(&plReader);
       
104162           sqlite3_snprintf(sizeof(buf), buf, "] %d[", iColumn);
       
104163           assert( dump.nData>0 );
       
104164           dump.nData--;                     /* Overwrite trailing space. */
       
104165           assert( dump.pData[dump.nData]==' ');
       
104166           dataBufferAppend(&dump, buf, strlen(buf));
       
104167         }
       
104168         if( DL_DEFAULT==DL_POSITIONS_OFFSETS ){
       
104169           sqlite3_snprintf(sizeof(buf), buf, "%d,%d,%d ",
       
104170                            plrPosition(&plReader),
       
104171                            plrStartOffset(&plReader), plrEndOffset(&plReader));
       
104172         }else if( DL_DEFAULT==DL_POSITIONS ){
       
104173           sqlite3_snprintf(sizeof(buf), buf, "%d ", plrPosition(&plReader));
       
104174         }else{
       
104175           assert( NULL=="Unhandled DL_DEFAULT value");
       
104176         }
       
104177         dataBufferAppend(&dump, buf, strlen(buf));
       
104178       }
       
104179       plrDestroy(&plReader);
       
104180 
       
104181       assert( dump.nData>0 );
       
104182       dump.nData--;                     /* Overwrite trailing space. */
       
104183       assert( dump.pData[dump.nData]==' ');
       
104184       dataBufferAppend(&dump, "]] ", 3);
       
104185     }
       
104186   }
       
104187   dlrDestroy(&dlReader);
       
104188 
       
104189   assert( dump.nData>0 );
       
104190   dump.nData--;                     /* Overwrite trailing space. */
       
104191   assert( dump.pData[dump.nData]==' ');
       
104192   dump.pData[dump.nData] = '\0';
       
104193   assert( dump.nData>0 );
       
104194 
       
104195   /* Passes ownership of dump's buffer to pContext. */
       
104196   sqlite3_result_text(pContext, dump.pData, dump.nData, sqlite3_free);
       
104197   dump.pData = NULL;
       
104198   dump.nData = dump.nCapacity = 0;
       
104199 }
       
104200 
       
104201 /* Implements dump_doclist() for use in inspecting the fts3 index from
       
104202 ** tests.  TEXT result containing a string representation of the
       
104203 ** doclist for the indicated term.  dump_doclist(t, term, level, idx)
       
104204 ** dumps the doclist for term from the segment specified by level, idx
       
104205 ** (in %_segdir), while dump_doclist(t, term) dumps the logical
       
104206 ** doclist for the term across all segments.  The per-segment doclist
       
104207 ** can contain deletions, while the full-index doclist will not
       
104208 ** (deletions are omitted).
       
104209 **
       
104210 ** Result formats differ with the setting of DL_DEFAULTS.  Examples:
       
104211 **
       
104212 ** DL_DOCIDS: [1] [3] [7]
       
104213 ** DL_POSITIONS: [1 0[0 4] 1[17]] [3 1[5]]
       
104214 ** DL_POSITIONS_OFFSETS: [1 0[0,0,3 4,23,26] 1[17,102,105]] [3 1[5,20,23]]
       
104215 **
       
104216 ** In each case the number after the outer '[' is the docid.  In the
       
104217 ** latter two cases, the number before the inner '[' is the column
       
104218 ** associated with the values within.  For DL_POSITIONS the numbers
       
104219 ** within are the positions, for DL_POSITIONS_OFFSETS they are the
       
104220 ** position, the start offset, and the end offset.
       
104221 */
       
104222 static void dumpDoclistFunc(
       
104223   sqlite3_context *pContext,
       
104224   int argc, sqlite3_value **argv
       
104225 ){
       
104226   fulltext_cursor *pCursor;
       
104227   if( argc!=2 && argc!=4 ){
       
104228     generateError(pContext, "dump_doclist", "incorrect arguments");
       
104229   }else if( sqlite3_value_type(argv[0])!=SQLITE_BLOB ||
       
104230             sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){
       
104231     generateError(pContext, "dump_doclist", "illegal first argument");
       
104232   }else if( sqlite3_value_text(argv[1])==NULL ||
       
104233             sqlite3_value_text(argv[1])[0]=='\0' ){
       
104234     generateError(pContext, "dump_doclist", "empty second argument");
       
104235   }else{
       
104236     const char *pTerm = (const char *)sqlite3_value_text(argv[1]);
       
104237     const int nTerm = strlen(pTerm);
       
104238     fulltext_vtab *v;
       
104239     int rc;
       
104240     DataBuffer doclist;
       
104241 
       
104242     memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor));
       
104243     v = cursor_vtab(pCursor);
       
104244 
       
104245     dataBufferInit(&doclist, 0);
       
104246 
       
104247     /* termSelect() yields the same logical doclist that queries are
       
104248     ** run against.
       
104249     */
       
104250     if( argc==2 ){
       
104251       rc = termSelect(v, v->nColumn, pTerm, nTerm, 0, DL_DEFAULT, &doclist);
       
104252     }else{
       
104253       sqlite3_stmt *s = NULL;
       
104254 
       
104255       /* Get our specific segment's information. */
       
104256       rc = sql_get_statement(v, SEGDIR_SELECT_SEGMENT_STMT, &s);
       
104257       if( rc==SQLITE_OK ){
       
104258         rc = sqlite3_bind_int(s, 1, sqlite3_value_int(argv[2]));
       
104259         if( rc==SQLITE_OK ){
       
104260           rc = sqlite3_bind_int(s, 2, sqlite3_value_int(argv[3]));
       
104261         }
       
104262       }
       
104263 
       
104264       if( rc==SQLITE_OK ){
       
104265         rc = sqlite3_step(s);
       
104266 
       
104267         if( rc==SQLITE_DONE ){
       
104268           dataBufferDestroy(&doclist);
       
104269           generateError(pContext, "dump_doclist", "segment not found");
       
104270           return;
       
104271         }
       
104272 
       
104273         /* Found a segment, load it into doclist. */
       
104274         if( rc==SQLITE_ROW ){
       
104275           const sqlite_int64 iLeavesEnd = sqlite3_column_int64(s, 1);
       
104276           const char *pData = sqlite3_column_blob(s, 2);
       
104277           const int nData = sqlite3_column_bytes(s, 2);
       
104278 
       
104279           /* loadSegment() is used by termSelect() to load each
       
104280           ** segment's data.
       
104281           */
       
104282           rc = loadSegment(v, pData, nData, iLeavesEnd, pTerm, nTerm, 0,
       
104283                            &doclist);
       
104284           if( rc==SQLITE_OK ){
       
104285             rc = sqlite3_step(s);
       
104286 
       
104287             /* Should not have more than one matching segment. */
       
104288             if( rc!=SQLITE_DONE ){
       
104289               sqlite3_reset(s);
       
104290               dataBufferDestroy(&doclist);
       
104291               generateError(pContext, "dump_doclist", "invalid segdir");
       
104292               return;
       
104293             }
       
104294             rc = SQLITE_OK;
       
104295           }
       
104296         }
       
104297       }
       
104298 
       
104299       sqlite3_reset(s);
       
104300     }
       
104301 
       
104302     if( rc==SQLITE_OK ){
       
104303       if( doclist.nData>0 ){
       
104304         createDoclistResult(pContext, doclist.pData, doclist.nData);
       
104305       }else{
       
104306         /* TODO(shess): This can happen if the term is not present, or
       
104307         ** if all instances of the term have been deleted and this is
       
104308         ** an all-index dump.  It may be interesting to distinguish
       
104309         ** these cases.
       
104310         */
       
104311         sqlite3_result_text(pContext, "", 0, SQLITE_STATIC);
       
104312       }
       
104313     }else if( rc==SQLITE_NOMEM ){
       
104314       /* Handle out-of-memory cases specially because if they are
       
104315       ** generated in fts3 code they may not be reflected in the db
       
104316       ** handle.
       
104317       */
       
104318       /* TODO(shess): Handle this more comprehensively.
       
104319       ** sqlite3ErrStr() has what I need, but is internal.
       
104320       */
       
104321       generateError(pContext, "dump_doclist", "out of memory");
       
104322     }else{
       
104323       generateError(pContext, "dump_doclist", NULL);
       
104324     }
       
104325 
       
104326     dataBufferDestroy(&doclist);
       
104327   }
       
104328 }
       
104329 #endif
       
104330 
       
104331 /*
       
104332 ** This routine implements the xFindFunction method for the FTS3
       
104333 ** virtual table.
       
104334 */
       
104335 static int fulltextFindFunction(
       
104336   sqlite3_vtab *pVtab,
       
104337   int nArg,
       
104338   const char *zName,
       
104339   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
       
104340   void **ppArg
       
104341 ){
       
104342   if( strcmp(zName,"snippet")==0 ){
       
104343     *pxFunc = snippetFunc;
       
104344     return 1;
       
104345   }else if( strcmp(zName,"offsets")==0 ){
       
104346     *pxFunc = snippetOffsetsFunc;
       
104347     return 1;
       
104348   }else if( strcmp(zName,"optimize")==0 ){
       
104349     *pxFunc = optimizeFunc;
       
104350     return 1;
       
104351 #ifdef SQLITE_TEST
       
104352     /* NOTE(shess): These functions are present only for testing
       
104353     ** purposes.  No particular effort is made to optimize their
       
104354     ** execution or how they build their results.
       
104355     */
       
104356   }else if( strcmp(zName,"dump_terms")==0 ){
       
104357     /* fprintf(stderr, "Found dump_terms\n"); */
       
104358     *pxFunc = dumpTermsFunc;
       
104359     return 1;
       
104360   }else if( strcmp(zName,"dump_doclist")==0 ){
       
104361     /* fprintf(stderr, "Found dump_doclist\n"); */
       
104362     *pxFunc = dumpDoclistFunc;
       
104363     return 1;
       
104364 #endif
       
104365   }
       
104366   return 0;
       
104367 }
       
104368 
       
104369 /*
       
104370 ** Rename an fts3 table.
       
104371 */
       
104372 static int fulltextRename(
       
104373   sqlite3_vtab *pVtab,
       
104374   const char *zName
       
104375 ){
       
104376   fulltext_vtab *p = (fulltext_vtab *)pVtab;
       
104377   int rc = SQLITE_NOMEM;
       
104378   char *zSql = sqlite3_mprintf(
       
104379     "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';"
       
104380     "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';"
       
104381     "ALTER TABLE %Q.'%q_segdir'   RENAME TO '%q_segdir';"
       
104382     , p->zDb, p->zName, zName 
       
104383     , p->zDb, p->zName, zName 
       
104384     , p->zDb, p->zName, zName
       
104385   );
       
104386   if( zSql ){
       
104387     rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
       
104388     sqlite3_free(zSql);
       
104389   }
       
104390   return rc;
       
104391 }
       
104392 
       
104393 static const sqlite3_module fts3Module = {
       
104394   /* iVersion      */ 0,
       
104395   /* xCreate       */ fulltextCreate,
       
104396   /* xConnect      */ fulltextConnect,
       
104397   /* xBestIndex    */ fulltextBestIndex,
       
104398   /* xDisconnect   */ fulltextDisconnect,
       
104399   /* xDestroy      */ fulltextDestroy,
       
104400   /* xOpen         */ fulltextOpen,
       
104401   /* xClose        */ fulltextClose,
       
104402   /* xFilter       */ fulltextFilter,
       
104403   /* xNext         */ fulltextNext,
       
104404   /* xEof          */ fulltextEof,
       
104405   /* xColumn       */ fulltextColumn,
       
104406   /* xRowid        */ fulltextRowid,
       
104407   /* xUpdate       */ fulltextUpdate,
       
104408   /* xBegin        */ fulltextBegin,
       
104409   /* xSync         */ fulltextSync,
       
104410   /* xCommit       */ fulltextCommit,
       
104411   /* xRollback     */ fulltextRollback,
       
104412   /* xFindFunction */ fulltextFindFunction,
       
104413   /* xRename */       fulltextRename,
       
104414 };
       
104415 
       
104416 static void hashDestroy(void *p){
       
104417   fts3Hash *pHash = (fts3Hash *)p;
       
104418   sqlite3Fts3HashClear(pHash);
       
104419   sqlite3_free(pHash);
       
104420 }
       
104421 
       
104422 /*
       
104423 ** The fts3 built-in tokenizers - "simple" and "porter" - are implemented
       
104424 ** in files fts3_tokenizer1.c and fts3_porter.c respectively. The following
       
104425 ** two forward declarations are for functions declared in these files
       
104426 ** used to retrieve the respective implementations.
       
104427 **
       
104428 ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
       
104429 ** to by the argument to point a the "simple" tokenizer implementation.
       
104430 ** Function ...PorterTokenizerModule() sets *pModule to point to the
       
104431 ** porter tokenizer/stemmer implementation.
       
104432 */
       
104433 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
       
104434 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
       
104435 SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
       
104436 
       
104437 SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, fts3Hash *, const char *);
       
104438 
       
104439 /*
       
104440 ** Initialise the fts3 extension. If this extension is built as part
       
104441 ** of the sqlite library, then this function is called directly by
       
104442 ** SQLite. If fts3 is built as a dynamically loadable extension, this
       
104443 ** function is called by the sqlite3_extension_init() entry point.
       
104444 */
       
104445 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
       
104446   int rc = SQLITE_OK;
       
104447   fts3Hash *pHash = 0;
       
104448   const sqlite3_tokenizer_module *pSimple = 0;
       
104449   const sqlite3_tokenizer_module *pPorter = 0;
       
104450   const sqlite3_tokenizer_module *pIcu = 0;
       
104451 
       
104452   sqlite3Fts3SimpleTokenizerModule(&pSimple);
       
104453   sqlite3Fts3PorterTokenizerModule(&pPorter);
       
104454 #ifdef SQLITE_ENABLE_ICU
       
104455   sqlite3Fts3IcuTokenizerModule(&pIcu);
       
104456 #endif
       
104457 
       
104458   /* Allocate and initialise the hash-table used to store tokenizers. */
       
104459   pHash = sqlite3_malloc(sizeof(fts3Hash));
       
104460   if( !pHash ){
       
104461     rc = SQLITE_NOMEM;
       
104462   }else{
       
104463     sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
       
104464   }
       
104465 
       
104466   /* Load the built-in tokenizers into the hash table */
       
104467   if( rc==SQLITE_OK ){
       
104468     if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
       
104469      || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) 
       
104470      || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
       
104471     ){
       
104472       rc = SQLITE_NOMEM;
       
104473     }
       
104474   }
       
104475 
       
104476 #ifdef SQLITE_TEST
       
104477   sqlite3Fts3ExprInitTestInterface(db);
       
104478 #endif
       
104479 
       
104480   /* Create the virtual table wrapper around the hash-table and overload 
       
104481   ** the two scalar functions. If this is successful, register the
       
104482   ** module with sqlite.
       
104483   */
       
104484   if( SQLITE_OK==rc 
       
104485    && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
       
104486    && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
       
104487    && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", -1))
       
104488    && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", -1))
       
104489 #ifdef SQLITE_TEST
       
104490    && SQLITE_OK==(rc = sqlite3_overload_function(db, "dump_terms", -1))
       
104491    && SQLITE_OK==(rc = sqlite3_overload_function(db, "dump_doclist", -1))
       
104492 #endif
       
104493   ){
       
104494     return sqlite3_create_module_v2(
       
104495         db, "fts3", &fts3Module, (void *)pHash, hashDestroy
       
104496     );
       
104497   }
       
104498 
       
104499   /* An error has occurred. Delete the hash table and return the error code. */
       
104500   assert( rc!=SQLITE_OK );
       
104501   if( pHash ){
       
104502     sqlite3Fts3HashClear(pHash);
       
104503     sqlite3_free(pHash);
       
104504   }
       
104505   return rc;
       
104506 }
       
104507 
       
104508 #if !SQLITE_CORE
       
104509 SQLITE_API int sqlite3_extension_init(
       
104510   sqlite3 *db, 
       
104511   char **pzErrMsg,
       
104512   const sqlite3_api_routines *pApi
       
104513 ){
       
104514   SQLITE_EXTENSION_INIT2(pApi)
       
104515   return sqlite3Fts3Init(db);
       
104516 }
       
104517 #endif
       
104518 
       
104519 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
       
104520 
       
104521 /************** End of fts3.c ************************************************/
       
104522 /************** Begin file fts3_expr.c ***************************************/
       
104523 /*
       
104524 ** 2008 Nov 28
       
104525 **
       
104526 ** The author disclaims copyright to this source code.  In place of
       
104527 ** a legal notice, here is a blessing:
       
104528 **
       
104529 **    May you do good and not evil.
       
104530 **    May you find forgiveness for yourself and forgive others.
       
104531 **    May you share freely, never taking more than you give.
       
104532 **
       
104533 ******************************************************************************
       
104534 **
       
104535 ** This module contains code that implements a parser for fts3 query strings
       
104536 ** (the right-hand argument to the MATCH operator). Because the supported 
       
104537 ** syntax is relatively simple, the whole tokenizer/parser system is
       
104538 ** hand-coded. The public interface to this module is declared in source
       
104539 ** code file "fts3_expr.h".
       
104540 */
       
104541 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
       
104542 
       
104543 /*
       
104544 ** By default, this module parses the legacy syntax that has been 
       
104545 ** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
       
104546 ** is defined, then it uses the new syntax. The differences between
       
104547 ** the new and the old syntaxes are:
       
104548 **
       
104549 **  a) The new syntax supports parenthesis. The old does not.
       
104550 **
       
104551 **  b) The new syntax supports the AND and NOT operators. The old does not.
       
104552 **
       
104553 **  c) The old syntax supports the "-" token qualifier. This is not 
       
104554 **     supported by the new syntax (it is replaced by the NOT operator).
       
104555 **
       
104556 **  d) When using the old syntax, the OR operator has a greater precedence
       
104557 **     than an implicit AND. When using the new, both implicity and explicit
       
104558 **     AND operators have a higher precedence than OR.
       
104559 **
       
104560 ** If compiled with SQLITE_TEST defined, then this module exports the
       
104561 ** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
       
104562 ** to zero causes the module to use the old syntax. If it is set to 
       
104563 ** non-zero the new syntax is activated. This is so both syntaxes can
       
104564 ** be tested using a single build of testfixture.
       
104565 */
       
104566 #ifdef SQLITE_TEST
       
104567 SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
       
104568 #else
       
104569 # ifdef SQLITE_ENABLE_FTS3_PARENTHESIS 
       
104570 #  define sqlite3_fts3_enable_parentheses 1
       
104571 # else
       
104572 #  define sqlite3_fts3_enable_parentheses 0
       
104573 # endif
       
104574 #endif
       
104575 
       
104576 /*
       
104577 ** Default span for NEAR operators.
       
104578 */
       
104579 #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
       
104580 
       
104581 
       
104582 typedef struct ParseContext ParseContext;
       
104583 struct ParseContext {
       
104584   sqlite3_tokenizer *pTokenizer;      /* Tokenizer module */
       
104585   const char **azCol;                 /* Array of column names for fts3 table */
       
104586   int nCol;                           /* Number of entries in azCol[] */
       
104587   int iDefaultCol;                    /* Default column to query */
       
104588   sqlite3_context *pCtx;              /* Write error message here */
       
104589   int nNest;                          /* Number of nested brackets */
       
104590 };
       
104591 
       
104592 /*
       
104593 ** This function is equivalent to the standard isspace() function. 
       
104594 **
       
104595 ** The standard isspace() can be awkward to use safely, because although it
       
104596 ** is defined to accept an argument of type int, its behaviour when passed
       
104597 ** an integer that falls outside of the range of the unsigned char type
       
104598 ** is undefined (and sometimes, "undefined" means segfault). This wrapper
       
104599 ** is defined to accept an argument of type char, and always returns 0 for
       
104600 ** any values that fall outside of the range of the unsigned char type (i.e.
       
104601 ** negative values).
       
104602 */
       
104603 static int fts3isspace(char c){
       
104604   return (c&0x80)==0 ? isspace(c) : 0;
       
104605 }
       
104606 
       
104607 /*
       
104608 ** Extract the next token from buffer z (length n) using the tokenizer
       
104609 ** and other information (column names etc.) in pParse. Create an Fts3Expr
       
104610 ** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
       
104611 ** single token and set *ppExpr to point to it. If the end of the buffer is
       
104612 ** reached before a token is found, set *ppExpr to zero. It is the
       
104613 ** responsibility of the caller to eventually deallocate the allocated 
       
104614 ** Fts3Expr structure (if any) by passing it to sqlite3_free().
       
104615 **
       
104616 ** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
       
104617 ** fails.
       
104618 */
       
104619 static int getNextToken(
       
104620   ParseContext *pParse,                   /* fts3 query parse context */
       
104621   int iCol,                               /* Value for Fts3Phrase.iColumn */
       
104622   const char *z, int n,                   /* Input string */
       
104623   Fts3Expr **ppExpr,                      /* OUT: expression */
       
104624   int *pnConsumed                         /* OUT: Number of bytes consumed */
       
104625 ){
       
104626   sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
       
104627   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
       
104628   int rc;
       
104629   sqlite3_tokenizer_cursor *pCursor;
       
104630   Fts3Expr *pRet = 0;
       
104631   int nConsumed = 0;
       
104632 
       
104633   rc = pModule->xOpen(pTokenizer, z, n, &pCursor);
       
104634   if( rc==SQLITE_OK ){
       
104635     const char *zToken;
       
104636     int nToken, iStart, iEnd, iPosition;
       
104637     int nByte;                               /* total space to allocate */
       
104638 
       
104639     pCursor->pTokenizer = pTokenizer;
       
104640     rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
       
104641 
       
104642     if( rc==SQLITE_OK ){
       
104643       nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
       
104644       pRet = (Fts3Expr *)sqlite3_malloc(nByte);
       
104645       if( !pRet ){
       
104646         rc = SQLITE_NOMEM;
       
104647       }else{
       
104648         memset(pRet, 0, nByte);
       
104649         pRet->eType = FTSQUERY_PHRASE;
       
104650         pRet->pPhrase = (Fts3Phrase *)&pRet[1];
       
104651         pRet->pPhrase->nToken = 1;
       
104652         pRet->pPhrase->iColumn = iCol;
       
104653         pRet->pPhrase->aToken[0].n = nToken;
       
104654         pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
       
104655         memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
       
104656 
       
104657         if( iEnd<n && z[iEnd]=='*' ){
       
104658           pRet->pPhrase->aToken[0].isPrefix = 1;
       
104659           iEnd++;
       
104660         }
       
104661         if( !sqlite3_fts3_enable_parentheses && iStart>0 && z[iStart-1]=='-' ){
       
104662           pRet->pPhrase->isNot = 1;
       
104663         }
       
104664       }
       
104665       nConsumed = iEnd;
       
104666     }
       
104667 
       
104668     pModule->xClose(pCursor);
       
104669   }
       
104670   
       
104671   *pnConsumed = nConsumed;
       
104672   *ppExpr = pRet;
       
104673   return rc;
       
104674 }
       
104675 
       
104676 
       
104677 /*
       
104678 ** Enlarge a memory allocation.  If an out-of-memory allocation occurs,
       
104679 ** then free the old allocation.
       
104680 */
       
104681 void *fts3ReallocOrFree(void *pOrig, int nNew){
       
104682   void *pRet = sqlite3_realloc(pOrig, nNew);
       
104683   if( !pRet ){
       
104684     sqlite3_free(pOrig);
       
104685   }
       
104686   return pRet;
       
104687 }
       
104688 
       
104689 /*
       
104690 ** Buffer zInput, length nInput, contains the contents of a quoted string
       
104691 ** that appeared as part of an fts3 query expression. Neither quote character
       
104692 ** is included in the buffer. This function attempts to tokenize the entire
       
104693 ** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE 
       
104694 ** containing the results.
       
104695 **
       
104696 ** If successful, SQLITE_OK is returned and *ppExpr set to point at the
       
104697 ** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
       
104698 ** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
       
104699 ** to 0.
       
104700 */
       
104701 static int getNextString(
       
104702   ParseContext *pParse,                   /* fts3 query parse context */
       
104703   const char *zInput, int nInput,         /* Input string */
       
104704   Fts3Expr **ppExpr                       /* OUT: expression */
       
104705 ){
       
104706   sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
       
104707   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
       
104708   int rc;
       
104709   Fts3Expr *p = 0;
       
104710   sqlite3_tokenizer_cursor *pCursor = 0;
       
104711   char *zTemp = 0;
       
104712   int nTemp = 0;
       
104713 
       
104714   rc = pModule->xOpen(pTokenizer, zInput, nInput, &pCursor);
       
104715   if( rc==SQLITE_OK ){
       
104716     int ii;
       
104717     pCursor->pTokenizer = pTokenizer;
       
104718     for(ii=0; rc==SQLITE_OK; ii++){
       
104719       const char *zToken;
       
104720       int nToken, iBegin, iEnd, iPos;
       
104721       rc = pModule->xNext(pCursor, &zToken, &nToken, &iBegin, &iEnd, &iPos);
       
104722       if( rc==SQLITE_OK ){
       
104723         int nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
       
104724         p = fts3ReallocOrFree(p, nByte+ii*sizeof(struct PhraseToken));
       
104725         zTemp = fts3ReallocOrFree(zTemp, nTemp + nToken);
       
104726         if( !p || !zTemp ){
       
104727           goto no_mem;
       
104728         }
       
104729         if( ii==0 ){
       
104730           memset(p, 0, nByte);
       
104731           p->pPhrase = (Fts3Phrase *)&p[1];
       
104732         }
       
104733         p->pPhrase = (Fts3Phrase *)&p[1];
       
104734         p->pPhrase->nToken = ii+1;
       
104735         p->pPhrase->aToken[ii].n = nToken;
       
104736         memcpy(&zTemp[nTemp], zToken, nToken);
       
104737         nTemp += nToken;
       
104738         if( iEnd<nInput && zInput[iEnd]=='*' ){
       
104739           p->pPhrase->aToken[ii].isPrefix = 1;
       
104740         }else{
       
104741           p->pPhrase->aToken[ii].isPrefix = 0;
       
104742         }
       
104743       }
       
104744     }
       
104745 
       
104746     pModule->xClose(pCursor);
       
104747     pCursor = 0;
       
104748   }
       
104749 
       
104750   if( rc==SQLITE_DONE ){
       
104751     int jj;
       
104752     char *zNew;
       
104753     int nNew = 0;
       
104754     int nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
       
104755     nByte += (p?(p->pPhrase->nToken-1):0) * sizeof(struct PhraseToken);
       
104756     p = fts3ReallocOrFree(p, nByte + nTemp);
       
104757     if( !p ){
       
104758       goto no_mem;
       
104759     }
       
104760     if( zTemp ){
       
104761       zNew = &(((char *)p)[nByte]);
       
104762       memcpy(zNew, zTemp, nTemp);
       
104763     }else{
       
104764       memset(p, 0, nByte+nTemp);
       
104765     }
       
104766     p->pPhrase = (Fts3Phrase *)&p[1];
       
104767     for(jj=0; jj<p->pPhrase->nToken; jj++){
       
104768       p->pPhrase->aToken[jj].z = &zNew[nNew];
       
104769       nNew += p->pPhrase->aToken[jj].n;
       
104770     }
       
104771     sqlite3_free(zTemp);
       
104772     p->eType = FTSQUERY_PHRASE;
       
104773     p->pPhrase->iColumn = pParse->iDefaultCol;
       
104774     rc = SQLITE_OK;
       
104775   }
       
104776 
       
104777   *ppExpr = p;
       
104778   return rc;
       
104779 no_mem:
       
104780 
       
104781   if( pCursor ){
       
104782     pModule->xClose(pCursor);
       
104783   }
       
104784   sqlite3_free(zTemp);
       
104785   sqlite3_free(p);
       
104786   *ppExpr = 0;
       
104787   return SQLITE_NOMEM;
       
104788 }
       
104789 
       
104790 /*
       
104791 ** Function getNextNode(), which is called by fts3ExprParse(), may itself
       
104792 ** call fts3ExprParse(). So this forward declaration is required.
       
104793 */
       
104794 static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
       
104795 
       
104796 /*
       
104797 ** The output variable *ppExpr is populated with an allocated Fts3Expr 
       
104798 ** structure, or set to 0 if the end of the input buffer is reached.
       
104799 **
       
104800 ** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
       
104801 ** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
       
104802 ** If SQLITE_ERROR is returned, pContext is populated with an error message.
       
104803 */
       
104804 static int getNextNode(
       
104805   ParseContext *pParse,                   /* fts3 query parse context */
       
104806   const char *z, int n,                   /* Input string */
       
104807   Fts3Expr **ppExpr,                      /* OUT: expression */
       
104808   int *pnConsumed                         /* OUT: Number of bytes consumed */
       
104809 ){
       
104810   static const struct Fts3Keyword {
       
104811     char z[4];                            /* Keyword text */
       
104812     unsigned char n;                      /* Length of the keyword */
       
104813     unsigned char parenOnly;              /* Only valid in paren mode */
       
104814     unsigned char eType;                  /* Keyword code */
       
104815   } aKeyword[] = {
       
104816     { "OR" ,  2, 0, FTSQUERY_OR   },
       
104817     { "AND",  3, 1, FTSQUERY_AND  },
       
104818     { "NOT",  3, 1, FTSQUERY_NOT  },
       
104819     { "NEAR", 4, 0, FTSQUERY_NEAR }
       
104820   };
       
104821   int ii;
       
104822   int iCol;
       
104823   int iColLen;
       
104824   int rc;
       
104825   Fts3Expr *pRet = 0;
       
104826 
       
104827   const char *zInput = z;
       
104828   int nInput = n;
       
104829 
       
104830   /* Skip over any whitespace before checking for a keyword, an open or
       
104831   ** close bracket, or a quoted string. 
       
104832   */
       
104833   while( nInput>0 && fts3isspace(*zInput) ){
       
104834     nInput--;
       
104835     zInput++;
       
104836   }
       
104837   if( nInput==0 ){
       
104838     return SQLITE_DONE;
       
104839   }
       
104840 
       
104841   /* See if we are dealing with a keyword. */
       
104842   for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
       
104843     const struct Fts3Keyword *pKey = &aKeyword[ii];
       
104844 
       
104845     if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
       
104846       continue;
       
104847     }
       
104848 
       
104849     if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
       
104850       int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
       
104851       int nKey = pKey->n;
       
104852       char cNext;
       
104853 
       
104854       /* If this is a "NEAR" keyword, check for an explicit nearness. */
       
104855       if( pKey->eType==FTSQUERY_NEAR ){
       
104856         assert( nKey==4 );
       
104857         if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
       
104858           nNear = 0;
       
104859           for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
       
104860             nNear = nNear * 10 + (zInput[nKey] - '0');
       
104861           }
       
104862         }
       
104863       }
       
104864 
       
104865       /* At this point this is probably a keyword. But for that to be true,
       
104866       ** the next byte must contain either whitespace, an open or close
       
104867       ** parenthesis, a quote character, or EOF. 
       
104868       */
       
104869       cNext = zInput[nKey];
       
104870       if( fts3isspace(cNext) 
       
104871        || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
       
104872       ){
       
104873         pRet = (Fts3Expr *)sqlite3_malloc(sizeof(Fts3Expr));
       
104874         memset(pRet, 0, sizeof(Fts3Expr));
       
104875         pRet->eType = pKey->eType;
       
104876         pRet->nNear = nNear;
       
104877         *ppExpr = pRet;
       
104878         *pnConsumed = (zInput - z) + nKey;
       
104879         return SQLITE_OK;
       
104880       }
       
104881 
       
104882       /* Turns out that wasn't a keyword after all. This happens if the
       
104883       ** user has supplied a token such as "ORacle". Continue.
       
104884       */
       
104885     }
       
104886   }
       
104887 
       
104888   /* Check for an open bracket. */
       
104889   if( sqlite3_fts3_enable_parentheses ){
       
104890     if( *zInput=='(' ){
       
104891       int nConsumed;
       
104892       int rc;
       
104893       pParse->nNest++;
       
104894       rc = fts3ExprParse(pParse, &zInput[1], nInput-1, ppExpr, &nConsumed);
       
104895       if( rc==SQLITE_OK && !*ppExpr ){
       
104896         rc = SQLITE_DONE;
       
104897       }
       
104898       *pnConsumed = (zInput - z) + 1 + nConsumed;
       
104899       return rc;
       
104900     }
       
104901   
       
104902     /* Check for a close bracket. */
       
104903     if( *zInput==')' ){
       
104904       pParse->nNest--;
       
104905       *pnConsumed = (zInput - z) + 1;
       
104906       return SQLITE_DONE;
       
104907     }
       
104908   }
       
104909 
       
104910   /* See if we are dealing with a quoted phrase. If this is the case, then
       
104911   ** search for the closing quote and pass the whole string to getNextString()
       
104912   ** for processing. This is easy to do, as fts3 has no syntax for escaping
       
104913   ** a quote character embedded in a string.
       
104914   */
       
104915   if( *zInput=='"' ){
       
104916     for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
       
104917     *pnConsumed = (zInput - z) + ii + 1;
       
104918     if( ii==nInput ){
       
104919       return SQLITE_ERROR;
       
104920     }
       
104921     return getNextString(pParse, &zInput[1], ii-1, ppExpr);
       
104922   }
       
104923 
       
104924 
       
104925   /* If control flows to this point, this must be a regular token, or 
       
104926   ** the end of the input. Read a regular token using the sqlite3_tokenizer
       
104927   ** interface. Before doing so, figure out if there is an explicit
       
104928   ** column specifier for the token. 
       
104929   **
       
104930   ** TODO: Strangely, it is not possible to associate a column specifier
       
104931   ** with a quoted phrase, only with a single token. Not sure if this was
       
104932   ** an implementation artifact or an intentional decision when fts3 was
       
104933   ** first implemented. Whichever it was, this module duplicates the 
       
104934   ** limitation.
       
104935   */
       
104936   iCol = pParse->iDefaultCol;
       
104937   iColLen = 0;
       
104938   for(ii=0; ii<pParse->nCol; ii++){
       
104939     const char *zStr = pParse->azCol[ii];
       
104940     int nStr = strlen(zStr);
       
104941     if( nInput>nStr && zInput[nStr]==':' 
       
104942      && sqlite3_strnicmp(zStr, zInput, nStr)==0 
       
104943     ){
       
104944       iCol = ii;
       
104945       iColLen = ((zInput - z) + nStr + 1);
       
104946       break;
       
104947     }
       
104948   }
       
104949   rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
       
104950   *pnConsumed += iColLen;
       
104951   return rc;
       
104952 }
       
104953 
       
104954 /*
       
104955 ** The argument is an Fts3Expr structure for a binary operator (any type
       
104956 ** except an FTSQUERY_PHRASE). Return an integer value representing the
       
104957 ** precedence of the operator. Lower values have a higher precedence (i.e.
       
104958 ** group more tightly). For example, in the C language, the == operator
       
104959 ** groups more tightly than ||, and would therefore have a higher precedence.
       
104960 **
       
104961 ** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
       
104962 ** is defined), the order of the operators in precedence from highest to
       
104963 ** lowest is:
       
104964 **
       
104965 **   NEAR
       
104966 **   NOT
       
104967 **   AND (including implicit ANDs)
       
104968 **   OR
       
104969 **
       
104970 ** Note that when using the old query syntax, the OR operator has a higher
       
104971 ** precedence than the AND operator.
       
104972 */
       
104973 static int opPrecedence(Fts3Expr *p){
       
104974   assert( p->eType!=FTSQUERY_PHRASE );
       
104975   if( sqlite3_fts3_enable_parentheses ){
       
104976     return p->eType;
       
104977   }else if( p->eType==FTSQUERY_NEAR ){
       
104978     return 1;
       
104979   }else if( p->eType==FTSQUERY_OR ){
       
104980     return 2;
       
104981   }
       
104982   assert( p->eType==FTSQUERY_AND );
       
104983   return 3;
       
104984 }
       
104985 
       
104986 /*
       
104987 ** Argument ppHead contains a pointer to the current head of a query 
       
104988 ** expression tree being parsed. pPrev is the expression node most recently
       
104989 ** inserted into the tree. This function adds pNew, which is always a binary
       
104990 ** operator node, into the expression tree based on the relative precedence
       
104991 ** of pNew and the existing nodes of the tree. This may result in the head
       
104992 ** of the tree changing, in which case *ppHead is set to the new root node.
       
104993 */
       
104994 static void insertBinaryOperator(
       
104995   Fts3Expr **ppHead,       /* Pointer to the root node of a tree */
       
104996   Fts3Expr *pPrev,         /* Node most recently inserted into the tree */
       
104997   Fts3Expr *pNew           /* New binary node to insert into expression tree */
       
104998 ){
       
104999   Fts3Expr *pSplit = pPrev;
       
105000   while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
       
105001     pSplit = pSplit->pParent;
       
105002   }
       
105003 
       
105004   if( pSplit->pParent ){
       
105005     assert( pSplit->pParent->pRight==pSplit );
       
105006     pSplit->pParent->pRight = pNew;
       
105007     pNew->pParent = pSplit->pParent;
       
105008   }else{
       
105009     *ppHead = pNew;
       
105010   }
       
105011   pNew->pLeft = pSplit;
       
105012   pSplit->pParent = pNew;
       
105013 }
       
105014 
       
105015 /*
       
105016 ** Parse the fts3 query expression found in buffer z, length n. This function
       
105017 ** returns either when the end of the buffer is reached or an unmatched 
       
105018 ** closing bracket - ')' - is encountered.
       
105019 **
       
105020 ** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
       
105021 ** parsed form of the expression and *pnConsumed is set to the number of
       
105022 ** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
       
105023 ** (out of memory error) or SQLITE_ERROR (parse error) is returned.
       
105024 */
       
105025 static int fts3ExprParse(
       
105026   ParseContext *pParse,                   /* fts3 query parse context */
       
105027   const char *z, int n,                   /* Text of MATCH query */
       
105028   Fts3Expr **ppExpr,                      /* OUT: Parsed query structure */
       
105029   int *pnConsumed                         /* OUT: Number of bytes consumed */
       
105030 ){
       
105031   Fts3Expr *pRet = 0;
       
105032   Fts3Expr *pPrev = 0;
       
105033   Fts3Expr *pNotBranch = 0;               /* Only used in legacy parse mode */
       
105034   int nIn = n;
       
105035   const char *zIn = z;
       
105036   int rc = SQLITE_OK;
       
105037   int isRequirePhrase = 1;
       
105038 
       
105039   while( rc==SQLITE_OK ){
       
105040     Fts3Expr *p = 0;
       
105041     int nByte = 0;
       
105042     rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
       
105043     if( rc==SQLITE_OK ){
       
105044       int isPhrase;
       
105045 
       
105046       if( !sqlite3_fts3_enable_parentheses 
       
105047        && p->eType==FTSQUERY_PHRASE && p->pPhrase->isNot 
       
105048       ){
       
105049         /* Create an implicit NOT operator. */
       
105050         Fts3Expr *pNot = sqlite3_malloc(sizeof(Fts3Expr));
       
105051         if( !pNot ){
       
105052           sqlite3Fts3ExprFree(p);
       
105053           rc = SQLITE_NOMEM;
       
105054           goto exprparse_out;
       
105055         }
       
105056         memset(pNot, 0, sizeof(Fts3Expr));
       
105057         pNot->eType = FTSQUERY_NOT;
       
105058         pNot->pRight = p;
       
105059         if( pNotBranch ){
       
105060           pNot->pLeft = pNotBranch;
       
105061         }
       
105062         pNotBranch = pNot;
       
105063         p = pPrev;
       
105064       }else{
       
105065         int eType = p->eType;
       
105066         assert( eType!=FTSQUERY_PHRASE || !p->pPhrase->isNot );
       
105067         isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
       
105068 
       
105069         /* The isRequirePhrase variable is set to true if a phrase or
       
105070         ** an expression contained in parenthesis is required. If a
       
105071         ** binary operator (AND, OR, NOT or NEAR) is encounted when
       
105072         ** isRequirePhrase is set, this is a syntax error.
       
105073         */
       
105074         if( !isPhrase && isRequirePhrase ){
       
105075           sqlite3Fts3ExprFree(p);
       
105076           rc = SQLITE_ERROR;
       
105077           goto exprparse_out;
       
105078         }
       
105079   
       
105080         if( isPhrase && !isRequirePhrase ){
       
105081           /* Insert an implicit AND operator. */
       
105082           Fts3Expr *pAnd;
       
105083           assert( pRet && pPrev );
       
105084           pAnd = sqlite3_malloc(sizeof(Fts3Expr));
       
105085           if( !pAnd ){
       
105086             sqlite3Fts3ExprFree(p);
       
105087             rc = SQLITE_NOMEM;
       
105088             goto exprparse_out;
       
105089           }
       
105090           memset(pAnd, 0, sizeof(Fts3Expr));
       
105091           pAnd->eType = FTSQUERY_AND;
       
105092           insertBinaryOperator(&pRet, pPrev, pAnd);
       
105093           pPrev = pAnd;
       
105094         }
       
105095 
       
105096         /* This test catches attempts to make either operand of a NEAR
       
105097         ** operator something other than a phrase. For example, either of
       
105098         ** the following:
       
105099         **
       
105100         **    (bracketed expression) NEAR phrase
       
105101         **    phrase NEAR (bracketed expression)
       
105102         **
       
105103         ** Return an error in either case.
       
105104         */
       
105105         if( pPrev && (
       
105106             (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
       
105107          || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
       
105108         )){
       
105109           sqlite3Fts3ExprFree(p);
       
105110           rc = SQLITE_ERROR;
       
105111           goto exprparse_out;
       
105112         }
       
105113   
       
105114         if( isPhrase ){
       
105115           if( pRet ){
       
105116             assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
       
105117             pPrev->pRight = p;
       
105118             p->pParent = pPrev;
       
105119           }else{
       
105120             pRet = p;
       
105121           }
       
105122         }else{
       
105123           insertBinaryOperator(&pRet, pPrev, p);
       
105124         }
       
105125         isRequirePhrase = !isPhrase;
       
105126       }
       
105127       assert( nByte>0 );
       
105128     }
       
105129     assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
       
105130     nIn -= nByte;
       
105131     zIn += nByte;
       
105132     pPrev = p;
       
105133   }
       
105134 
       
105135   if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
       
105136     rc = SQLITE_ERROR;
       
105137   }
       
105138 
       
105139   if( rc==SQLITE_DONE ){
       
105140     rc = SQLITE_OK;
       
105141     if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
       
105142       if( !pRet ){
       
105143         rc = SQLITE_ERROR;
       
105144       }else{
       
105145         Fts3Expr *pIter = pNotBranch;
       
105146         while( pIter->pLeft ){
       
105147           pIter = pIter->pLeft;
       
105148         }
       
105149         pIter->pLeft = pRet;
       
105150         pRet = pNotBranch;
       
105151       }
       
105152     }
       
105153   }
       
105154   *pnConsumed = n - nIn;
       
105155 
       
105156 exprparse_out:
       
105157   if( rc!=SQLITE_OK ){
       
105158     sqlite3Fts3ExprFree(pRet);
       
105159     sqlite3Fts3ExprFree(pNotBranch);
       
105160     pRet = 0;
       
105161   }
       
105162   *ppExpr = pRet;
       
105163   return rc;
       
105164 }
       
105165 
       
105166 /*
       
105167 ** Parameters z and n contain a pointer to and length of a buffer containing
       
105168 ** an fts3 query expression, respectively. This function attempts to parse the
       
105169 ** query expression and create a tree of Fts3Expr structures representing the
       
105170 ** parsed expression. If successful, *ppExpr is set to point to the head
       
105171 ** of the parsed expression tree and SQLITE_OK is returned. If an error
       
105172 ** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
       
105173 ** error) is returned and *ppExpr is set to 0.
       
105174 **
       
105175 ** If parameter n is a negative number, then z is assumed to point to a
       
105176 ** nul-terminated string and the length is determined using strlen().
       
105177 **
       
105178 ** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
       
105179 ** use to normalize query tokens while parsing the expression. The azCol[]
       
105180 ** array, which is assumed to contain nCol entries, should contain the names
       
105181 ** of each column in the target fts3 table, in order from left to right. 
       
105182 ** Column names must be nul-terminated strings.
       
105183 **
       
105184 ** The iDefaultCol parameter should be passed the index of the table column
       
105185 ** that appears on the left-hand-side of the MATCH operator (the default
       
105186 ** column to match against for tokens for which a column name is not explicitly
       
105187 ** specified as part of the query string), or -1 if tokens may by default
       
105188 ** match any table column.
       
105189 */
       
105190 SQLITE_PRIVATE int sqlite3Fts3ExprParse(
       
105191   sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
       
105192   char **azCol,                       /* Array of column names for fts3 table */
       
105193   int nCol,                           /* Number of entries in azCol[] */
       
105194   int iDefaultCol,                    /* Default column to query */
       
105195   const char *z, int n,               /* Text of MATCH query */
       
105196   Fts3Expr **ppExpr                   /* OUT: Parsed query structure */
       
105197 ){
       
105198   int nParsed;
       
105199   int rc;
       
105200   ParseContext sParse;
       
105201   sParse.pTokenizer = pTokenizer;
       
105202   sParse.azCol = (const char **)azCol;
       
105203   sParse.nCol = nCol;
       
105204   sParse.iDefaultCol = iDefaultCol;
       
105205   sParse.nNest = 0;
       
105206   if( z==0 ){
       
105207     *ppExpr = 0;
       
105208     return SQLITE_OK;
       
105209   }
       
105210   if( n<0 ){
       
105211     n = strlen(z);
       
105212   }
       
105213   rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
       
105214 
       
105215   /* Check for mismatched parenthesis */
       
105216   if( rc==SQLITE_OK && sParse.nNest ){
       
105217     rc = SQLITE_ERROR;
       
105218     sqlite3Fts3ExprFree(*ppExpr);
       
105219     *ppExpr = 0;
       
105220   }
       
105221 
       
105222   return rc;
       
105223 }
       
105224 
       
105225 /*
       
105226 ** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
       
105227 */
       
105228 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *p){
       
105229   if( p ){
       
105230     sqlite3Fts3ExprFree(p->pLeft);
       
105231     sqlite3Fts3ExprFree(p->pRight);
       
105232     sqlite3_free(p);
       
105233   }
       
105234 }
       
105235 
       
105236 /****************************************************************************
       
105237 *****************************************************************************
       
105238 ** Everything after this point is just test code.
       
105239 */
       
105240 
       
105241 #ifdef SQLITE_TEST
       
105242 
       
105243 
       
105244 /*
       
105245 ** Function to query the hash-table of tokenizers (see README.tokenizers).
       
105246 */
       
105247 static int queryTestTokenizer(
       
105248   sqlite3 *db, 
       
105249   const char *zName,  
       
105250   const sqlite3_tokenizer_module **pp
       
105251 ){
       
105252   int rc;
       
105253   sqlite3_stmt *pStmt;
       
105254   const char zSql[] = "SELECT fts3_tokenizer(?)";
       
105255 
       
105256   *pp = 0;
       
105257   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
       
105258   if( rc!=SQLITE_OK ){
       
105259     return rc;
       
105260   }
       
105261 
       
105262   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
       
105263   if( SQLITE_ROW==sqlite3_step(pStmt) ){
       
105264     if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
       
105265       memcpy(pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
       
105266     }
       
105267   }
       
105268 
       
105269   return sqlite3_finalize(pStmt);
       
105270 }
       
105271 
       
105272 /*
       
105273 ** This function is part of the test interface for the query parser. It
       
105274 ** writes a text representation of the query expression pExpr into the
       
105275 ** buffer pointed to by argument zBuf. It is assumed that zBuf is large 
       
105276 ** enough to store the required text representation.
       
105277 */
       
105278 static void exprToString(Fts3Expr *pExpr, char *zBuf){
       
105279   switch( pExpr->eType ){
       
105280     case FTSQUERY_PHRASE: {
       
105281       Fts3Phrase *pPhrase = pExpr->pPhrase;
       
105282       int i;
       
105283       zBuf += sprintf(zBuf, "PHRASE %d %d", pPhrase->iColumn, pPhrase->isNot);
       
105284       for(i=0; i<pPhrase->nToken; i++){
       
105285         zBuf += sprintf(zBuf," %.*s",pPhrase->aToken[i].n,pPhrase->aToken[i].z);
       
105286         zBuf += sprintf(zBuf,"%s", (pPhrase->aToken[i].isPrefix?"+":""));
       
105287       }
       
105288       return;
       
105289     }
       
105290 
       
105291     case FTSQUERY_NEAR:
       
105292       zBuf += sprintf(zBuf, "NEAR/%d ", pExpr->nNear);
       
105293       break;
       
105294     case FTSQUERY_NOT:
       
105295       zBuf += sprintf(zBuf, "NOT ");
       
105296       break;
       
105297     case FTSQUERY_AND:
       
105298       zBuf += sprintf(zBuf, "AND ");
       
105299       break;
       
105300     case FTSQUERY_OR:
       
105301       zBuf += sprintf(zBuf, "OR ");
       
105302       break;
       
105303   }
       
105304 
       
105305   zBuf += sprintf(zBuf, "{");
       
105306   exprToString(pExpr->pLeft, zBuf);
       
105307   zBuf += strlen(zBuf);
       
105308   zBuf += sprintf(zBuf, "} ");
       
105309 
       
105310   zBuf += sprintf(zBuf, "{");
       
105311   exprToString(pExpr->pRight, zBuf);
       
105312   zBuf += strlen(zBuf);
       
105313   zBuf += sprintf(zBuf, "}");
       
105314 }
       
105315 
       
105316 /*
       
105317 ** This is the implementation of a scalar SQL function used to test the 
       
105318 ** expression parser. It should be called as follows:
       
105319 **
       
105320 **   fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
       
105321 **
       
105322 ** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
       
105323 ** to parse the query expression (see README.tokenizers). The second argument
       
105324 ** is the query expression to parse. Each subsequent argument is the name
       
105325 ** of a column of the fts3 table that the query expression may refer to.
       
105326 ** For example:
       
105327 **
       
105328 **   SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
       
105329 */
       
105330 static void fts3ExprTest(
       
105331   sqlite3_context *context,
       
105332   int argc,
       
105333   sqlite3_value **argv
       
105334 ){
       
105335   sqlite3_tokenizer_module const *pModule = 0;
       
105336   sqlite3_tokenizer *pTokenizer = 0;
       
105337   int rc;
       
105338   char **azCol = 0;
       
105339   const char *zExpr;
       
105340   int nExpr;
       
105341   int nCol;
       
105342   int ii;
       
105343   Fts3Expr *pExpr;
       
105344   sqlite3 *db = sqlite3_context_db_handle(context);
       
105345 
       
105346   if( argc<3 ){
       
105347     sqlite3_result_error(context, 
       
105348         "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
       
105349     );
       
105350     return;
       
105351   }
       
105352 
       
105353   rc = queryTestTokenizer(db,
       
105354                           (const char *)sqlite3_value_text(argv[0]), &pModule);
       
105355   if( rc==SQLITE_NOMEM ){
       
105356     sqlite3_result_error_nomem(context);
       
105357     goto exprtest_out;
       
105358   }else if( !pModule ){
       
105359     sqlite3_result_error(context, "No such tokenizer module", -1);
       
105360     goto exprtest_out;
       
105361   }
       
105362 
       
105363   rc = pModule->xCreate(0, 0, &pTokenizer);
       
105364   assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
       
105365   if( rc==SQLITE_NOMEM ){
       
105366     sqlite3_result_error_nomem(context);
       
105367     goto exprtest_out;
       
105368   }
       
105369   pTokenizer->pModule = pModule;
       
105370 
       
105371   zExpr = (const char *)sqlite3_value_text(argv[1]);
       
105372   nExpr = sqlite3_value_bytes(argv[1]);
       
105373   nCol = argc-2;
       
105374   azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
       
105375   if( !azCol ){
       
105376     sqlite3_result_error_nomem(context);
       
105377     goto exprtest_out;
       
105378   }
       
105379   for(ii=0; ii<nCol; ii++){
       
105380     azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
       
105381   }
       
105382 
       
105383   rc = sqlite3Fts3ExprParse(
       
105384       pTokenizer, azCol, nCol, nCol, zExpr, nExpr, &pExpr
       
105385   );
       
105386   if( rc==SQLITE_NOMEM ){
       
105387     sqlite3_result_error_nomem(context);
       
105388     goto exprtest_out;
       
105389   }else if( rc==SQLITE_OK ){
       
105390     char zBuf[4096];
       
105391     exprToString(pExpr, zBuf);
       
105392     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
       
105393     sqlite3Fts3ExprFree(pExpr);
       
105394   }else{
       
105395     sqlite3_result_error(context, "Error parsing expression", -1);
       
105396   }
       
105397 
       
105398 exprtest_out:
       
105399   if( pModule && pTokenizer ){
       
105400     rc = pModule->xDestroy(pTokenizer);
       
105401   }
       
105402   sqlite3_free(azCol);
       
105403 }
       
105404 
       
105405 /*
       
105406 ** Register the query expression parser test function fts3_exprtest() 
       
105407 ** with database connection db. 
       
105408 */
       
105409 SQLITE_PRIVATE void sqlite3Fts3ExprInitTestInterface(sqlite3* db){
       
105410   sqlite3_create_function(
       
105411       db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
       
105412   );
       
105413 }
       
105414 
       
105415 #endif
       
105416 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
       
105417 
       
105418 /************** End of fts3_expr.c *******************************************/
       
105419 /************** Begin file fts3_hash.c ***************************************/
       
105420 /*
       
105421 ** 2001 September 22
       
105422 **
       
105423 ** The author disclaims copyright to this source code.  In place of
       
105424 ** a legal notice, here is a blessing:
       
105425 **
       
105426 **    May you do good and not evil.
       
105427 **    May you find forgiveness for yourself and forgive others.
       
105428 **    May you share freely, never taking more than you give.
       
105429 **
       
105430 *************************************************************************
       
105431 ** This is the implementation of generic hash-tables used in SQLite.
       
105432 ** We've modified it slightly to serve as a standalone hash table
       
105433 ** implementation for the full-text indexing module.
       
105434 */
       
105435 
       
105436 /*
       
105437 ** The code in this file is only compiled if:
       
105438 **
       
105439 **     * The FTS3 module is being built as an extension
       
105440 **       (in which case SQLITE_CORE is not defined), or
       
105441 **
       
105442 **     * The FTS3 module is being built into the core of
       
105443 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
       
105444 */
       
105445 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
       
105446 
       
105447 
       
105448 
       
105449 /*
       
105450 ** Malloc and Free functions
       
105451 */
       
105452 static void *fts3HashMalloc(int n){
       
105453   void *p = sqlite3_malloc(n);
       
105454   if( p ){
       
105455     memset(p, 0, n);
       
105456   }
       
105457   return p;
       
105458 }
       
105459 static void fts3HashFree(void *p){
       
105460   sqlite3_free(p);
       
105461 }
       
105462 
       
105463 /* Turn bulk memory into a hash table object by initializing the
       
105464 ** fields of the Hash structure.
       
105465 **
       
105466 ** "pNew" is a pointer to the hash table that is to be initialized.
       
105467 ** keyClass is one of the constants 
       
105468 ** FTS3_HASH_BINARY or FTS3_HASH_STRING.  The value of keyClass 
       
105469 ** determines what kind of key the hash table will use.  "copyKey" is
       
105470 ** true if the hash table should make its own private copy of keys and
       
105471 ** false if it should just use the supplied pointer.
       
105472 */
       
105473 SQLITE_PRIVATE void sqlite3Fts3HashInit(fts3Hash *pNew, int keyClass, int copyKey){
       
105474   assert( pNew!=0 );
       
105475   assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
       
105476   pNew->keyClass = keyClass;
       
105477   pNew->copyKey = copyKey;
       
105478   pNew->first = 0;
       
105479   pNew->count = 0;
       
105480   pNew->htsize = 0;
       
105481   pNew->ht = 0;
       
105482 }
       
105483 
       
105484 /* Remove all entries from a hash table.  Reclaim all memory.
       
105485 ** Call this routine to delete a hash table or to reset a hash table
       
105486 ** to the empty state.
       
105487 */
       
105488 SQLITE_PRIVATE void sqlite3Fts3HashClear(fts3Hash *pH){
       
105489   fts3HashElem *elem;         /* For looping over all elements of the table */
       
105490 
       
105491   assert( pH!=0 );
       
105492   elem = pH->first;
       
105493   pH->first = 0;
       
105494   fts3HashFree(pH->ht);
       
105495   pH->ht = 0;
       
105496   pH->htsize = 0;
       
105497   while( elem ){
       
105498     fts3HashElem *next_elem = elem->next;
       
105499     if( pH->copyKey && elem->pKey ){
       
105500       fts3HashFree(elem->pKey);
       
105501     }
       
105502     fts3HashFree(elem);
       
105503     elem = next_elem;
       
105504   }
       
105505   pH->count = 0;
       
105506 }
       
105507 
       
105508 /*
       
105509 ** Hash and comparison functions when the mode is FTS3_HASH_STRING
       
105510 */
       
105511 static int fts3StrHash(const void *pKey, int nKey){
       
105512   const char *z = (const char *)pKey;
       
105513   int h = 0;
       
105514   if( nKey<=0 ) nKey = (int) strlen(z);
       
105515   while( nKey > 0  ){
       
105516     h = (h<<3) ^ h ^ *z++;
       
105517     nKey--;
       
105518   }
       
105519   return h & 0x7fffffff;
       
105520 }
       
105521 static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
       
105522   if( n1!=n2 ) return 1;
       
105523   return strncmp((const char*)pKey1,(const char*)pKey2,n1);
       
105524 }
       
105525 
       
105526 /*
       
105527 ** Hash and comparison functions when the mode is FTS3_HASH_BINARY
       
105528 */
       
105529 static int fts3BinHash(const void *pKey, int nKey){
       
105530   int h = 0;
       
105531   const char *z = (const char *)pKey;
       
105532   while( nKey-- > 0 ){
       
105533     h = (h<<3) ^ h ^ *(z++);
       
105534   }
       
105535   return h & 0x7fffffff;
       
105536 }
       
105537 static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
       
105538   if( n1!=n2 ) return 1;
       
105539   return memcmp(pKey1,pKey2,n1);
       
105540 }
       
105541 
       
105542 /*
       
105543 ** Return a pointer to the appropriate hash function given the key class.
       
105544 **
       
105545 ** The C syntax in this function definition may be unfamilar to some 
       
105546 ** programmers, so we provide the following additional explanation:
       
105547 **
       
105548 ** The name of the function is "ftsHashFunction".  The function takes a
       
105549 ** single parameter "keyClass".  The return value of ftsHashFunction()
       
105550 ** is a pointer to another function.  Specifically, the return value
       
105551 ** of ftsHashFunction() is a pointer to a function that takes two parameters
       
105552 ** with types "const void*" and "int" and returns an "int".
       
105553 */
       
105554 static int (*ftsHashFunction(int keyClass))(const void*,int){
       
105555   if( keyClass==FTS3_HASH_STRING ){
       
105556     return &fts3StrHash;
       
105557   }else{
       
105558     assert( keyClass==FTS3_HASH_BINARY );
       
105559     return &fts3BinHash;
       
105560   }
       
105561 }
       
105562 
       
105563 /*
       
105564 ** Return a pointer to the appropriate hash function given the key class.
       
105565 **
       
105566 ** For help in interpreted the obscure C code in the function definition,
       
105567 ** see the header comment on the previous function.
       
105568 */
       
105569 static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
       
105570   if( keyClass==FTS3_HASH_STRING ){
       
105571     return &fts3StrCompare;
       
105572   }else{
       
105573     assert( keyClass==FTS3_HASH_BINARY );
       
105574     return &fts3BinCompare;
       
105575   }
       
105576 }
       
105577 
       
105578 /* Link an element into the hash table
       
105579 */
       
105580 static void fts3HashInsertElement(
       
105581   fts3Hash *pH,            /* The complete hash table */
       
105582   struct _fts3ht *pEntry,  /* The entry into which pNew is inserted */
       
105583   fts3HashElem *pNew       /* The element to be inserted */
       
105584 ){
       
105585   fts3HashElem *pHead;     /* First element already in pEntry */
       
105586   pHead = pEntry->chain;
       
105587   if( pHead ){
       
105588     pNew->next = pHead;
       
105589     pNew->prev = pHead->prev;
       
105590     if( pHead->prev ){ pHead->prev->next = pNew; }
       
105591     else             { pH->first = pNew; }
       
105592     pHead->prev = pNew;
       
105593   }else{
       
105594     pNew->next = pH->first;
       
105595     if( pH->first ){ pH->first->prev = pNew; }
       
105596     pNew->prev = 0;
       
105597     pH->first = pNew;
       
105598   }
       
105599   pEntry->count++;
       
105600   pEntry->chain = pNew;
       
105601 }
       
105602 
       
105603 
       
105604 /* Resize the hash table so that it cantains "new_size" buckets.
       
105605 ** "new_size" must be a power of 2.  The hash table might fail 
       
105606 ** to resize if sqliteMalloc() fails.
       
105607 */
       
105608 static void fts3Rehash(fts3Hash *pH, int new_size){
       
105609   struct _fts3ht *new_ht;          /* The new hash table */
       
105610   fts3HashElem *elem, *next_elem;  /* For looping over existing elements */
       
105611   int (*xHash)(const void*,int);   /* The hash function */
       
105612 
       
105613   assert( (new_size & (new_size-1))==0 );
       
105614   new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
       
105615   if( new_ht==0 ) return;
       
105616   fts3HashFree(pH->ht);
       
105617   pH->ht = new_ht;
       
105618   pH->htsize = new_size;
       
105619   xHash = ftsHashFunction(pH->keyClass);
       
105620   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
       
105621     int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
       
105622     next_elem = elem->next;
       
105623     fts3HashInsertElement(pH, &new_ht[h], elem);
       
105624   }
       
105625 }
       
105626 
       
105627 /* This function (for internal use only) locates an element in an
       
105628 ** hash table that matches the given key.  The hash for this key has
       
105629 ** already been computed and is passed as the 4th parameter.
       
105630 */
       
105631 static fts3HashElem *fts3FindElementByHash(
       
105632   const fts3Hash *pH, /* The pH to be searched */
       
105633   const void *pKey,   /* The key we are searching for */
       
105634   int nKey,
       
105635   int h               /* The hash for this key. */
       
105636 ){
       
105637   fts3HashElem *elem;            /* Used to loop thru the element list */
       
105638   int count;                     /* Number of elements left to test */
       
105639   int (*xCompare)(const void*,int,const void*,int);  /* comparison function */
       
105640 
       
105641   if( pH->ht ){
       
105642     struct _fts3ht *pEntry = &pH->ht[h];
       
105643     elem = pEntry->chain;
       
105644     count = pEntry->count;
       
105645     xCompare = ftsCompareFunction(pH->keyClass);
       
105646     while( count-- && elem ){
       
105647       if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ 
       
105648         return elem;
       
105649       }
       
105650       elem = elem->next;
       
105651     }
       
105652   }
       
105653   return 0;
       
105654 }
       
105655 
       
105656 /* Remove a single entry from the hash table given a pointer to that
       
105657 ** element and a hash on the element's key.
       
105658 */
       
105659 static void fts3RemoveElementByHash(
       
105660   fts3Hash *pH,         /* The pH containing "elem" */
       
105661   fts3HashElem* elem,   /* The element to be removed from the pH */
       
105662   int h                 /* Hash value for the element */
       
105663 ){
       
105664   struct _fts3ht *pEntry;
       
105665   if( elem->prev ){
       
105666     elem->prev->next = elem->next; 
       
105667   }else{
       
105668     pH->first = elem->next;
       
105669   }
       
105670   if( elem->next ){
       
105671     elem->next->prev = elem->prev;
       
105672   }
       
105673   pEntry = &pH->ht[h];
       
105674   if( pEntry->chain==elem ){
       
105675     pEntry->chain = elem->next;
       
105676   }
       
105677   pEntry->count--;
       
105678   if( pEntry->count<=0 ){
       
105679     pEntry->chain = 0;
       
105680   }
       
105681   if( pH->copyKey && elem->pKey ){
       
105682     fts3HashFree(elem->pKey);
       
105683   }
       
105684   fts3HashFree( elem );
       
105685   pH->count--;
       
105686   if( pH->count<=0 ){
       
105687     assert( pH->first==0 );
       
105688     assert( pH->count==0 );
       
105689     fts3HashClear(pH);
       
105690   }
       
105691 }
       
105692 
       
105693 /* Attempt to locate an element of the hash table pH with a key
       
105694 ** that matches pKey,nKey.  Return the data for this element if it is
       
105695 ** found, or NULL if there is no match.
       
105696 */
       
105697 SQLITE_PRIVATE void *sqlite3Fts3HashFind(const fts3Hash *pH, const void *pKey, int nKey){
       
105698   int h;                 /* A hash on key */
       
105699   fts3HashElem *elem;    /* The element that matches key */
       
105700   int (*xHash)(const void*,int);  /* The hash function */
       
105701 
       
105702   if( pH==0 || pH->ht==0 ) return 0;
       
105703   xHash = ftsHashFunction(pH->keyClass);
       
105704   assert( xHash!=0 );
       
105705   h = (*xHash)(pKey,nKey);
       
105706   assert( (pH->htsize & (pH->htsize-1))==0 );
       
105707   elem = fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
       
105708   return elem ? elem->data : 0;
       
105709 }
       
105710 
       
105711 /* Insert an element into the hash table pH.  The key is pKey,nKey
       
105712 ** and the data is "data".
       
105713 **
       
105714 ** If no element exists with a matching key, then a new
       
105715 ** element is created.  A copy of the key is made if the copyKey
       
105716 ** flag is set.  NULL is returned.
       
105717 **
       
105718 ** If another element already exists with the same key, then the
       
105719 ** new data replaces the old data and the old data is returned.
       
105720 ** The key is not copied in this instance.  If a malloc fails, then
       
105721 ** the new data is returned and the hash table is unchanged.
       
105722 **
       
105723 ** If the "data" parameter to this function is NULL, then the
       
105724 ** element corresponding to "key" is removed from the hash table.
       
105725 */
       
105726 SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
       
105727   fts3Hash *pH,        /* The hash table to insert into */
       
105728   const void *pKey,    /* The key */
       
105729   int nKey,            /* Number of bytes in the key */
       
105730   void *data           /* The data */
       
105731 ){
       
105732   int hraw;                 /* Raw hash value of the key */
       
105733   int h;                    /* the hash of the key modulo hash table size */
       
105734   fts3HashElem *elem;       /* Used to loop thru the element list */
       
105735   fts3HashElem *new_elem;   /* New element added to the pH */
       
105736   int (*xHash)(const void*,int);  /* The hash function */
       
105737 
       
105738   assert( pH!=0 );
       
105739   xHash = ftsHashFunction(pH->keyClass);
       
105740   assert( xHash!=0 );
       
105741   hraw = (*xHash)(pKey, nKey);
       
105742   assert( (pH->htsize & (pH->htsize-1))==0 );
       
105743   h = hraw & (pH->htsize-1);
       
105744   elem = fts3FindElementByHash(pH,pKey,nKey,h);
       
105745   if( elem ){
       
105746     void *old_data = elem->data;
       
105747     if( data==0 ){
       
105748       fts3RemoveElementByHash(pH,elem,h);
       
105749     }else{
       
105750       elem->data = data;
       
105751     }
       
105752     return old_data;
       
105753   }
       
105754   if( data==0 ) return 0;
       
105755   if( pH->htsize==0 ){
       
105756     fts3Rehash(pH,8);
       
105757     if( pH->htsize==0 ){
       
105758       pH->count = 0;
       
105759       return data;
       
105760     }
       
105761   }
       
105762   new_elem = (fts3HashElem*)fts3HashMalloc( sizeof(fts3HashElem) );
       
105763   if( new_elem==0 ) return data;
       
105764   if( pH->copyKey && pKey!=0 ){
       
105765     new_elem->pKey = fts3HashMalloc( nKey );
       
105766     if( new_elem->pKey==0 ){
       
105767       fts3HashFree(new_elem);
       
105768       return data;
       
105769     }
       
105770     memcpy((void*)new_elem->pKey, pKey, nKey);
       
105771   }else{
       
105772     new_elem->pKey = (void*)pKey;
       
105773   }
       
105774   new_elem->nKey = nKey;
       
105775   pH->count++;
       
105776   if( pH->count > pH->htsize ){
       
105777     fts3Rehash(pH,pH->htsize*2);
       
105778   }
       
105779   assert( pH->htsize>0 );
       
105780   assert( (pH->htsize & (pH->htsize-1))==0 );
       
105781   h = hraw & (pH->htsize-1);
       
105782   fts3HashInsertElement(pH, &pH->ht[h], new_elem);
       
105783   new_elem->data = data;
       
105784   return 0;
       
105785 }
       
105786 
       
105787 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
       
105788 
       
105789 /************** End of fts3_hash.c *******************************************/
       
105790 /************** Begin file fts3_porter.c *************************************/
       
105791 /*
       
105792 ** 2006 September 30
       
105793 **
       
105794 ** The author disclaims copyright to this source code.  In place of
       
105795 ** a legal notice, here is a blessing:
       
105796 **
       
105797 **    May you do good and not evil.
       
105798 **    May you find forgiveness for yourself and forgive others.
       
105799 **    May you share freely, never taking more than you give.
       
105800 **
       
105801 *************************************************************************
       
105802 ** Implementation of the full-text-search tokenizer that implements
       
105803 ** a Porter stemmer.
       
105804 */
       
105805 
       
105806 /*
       
105807 ** The code in this file is only compiled if:
       
105808 **
       
105809 **     * The FTS3 module is being built as an extension
       
105810 **       (in which case SQLITE_CORE is not defined), or
       
105811 **
       
105812 **     * The FTS3 module is being built into the core of
       
105813 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
       
105814 */
       
105815 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
       
105816 
       
105817 
       
105818 
       
105819 
       
105820 /*
       
105821 ** Class derived from sqlite3_tokenizer
       
105822 */
       
105823 typedef struct porter_tokenizer {
       
105824   sqlite3_tokenizer base;      /* Base class */
       
105825 } porter_tokenizer;
       
105826 
       
105827 /*
       
105828 ** Class derived from sqlit3_tokenizer_cursor
       
105829 */
       
105830 typedef struct porter_tokenizer_cursor {
       
105831   sqlite3_tokenizer_cursor base;
       
105832   const char *zInput;          /* input we are tokenizing */
       
105833   int nInput;                  /* size of the input */
       
105834   int iOffset;                 /* current position in zInput */
       
105835   int iToken;                  /* index of next token to be returned */
       
105836   char *zToken;                /* storage for current token */
       
105837   int nAllocated;              /* space allocated to zToken buffer */
       
105838 } porter_tokenizer_cursor;
       
105839 
       
105840 
       
105841 /* Forward declaration */
       
105842 static const sqlite3_tokenizer_module porterTokenizerModule;
       
105843 
       
105844 
       
105845 /*
       
105846 ** Create a new tokenizer instance.
       
105847 */
       
105848 static int porterCreate(
       
105849   int argc, const char * const *argv,
       
105850   sqlite3_tokenizer **ppTokenizer
       
105851 ){
       
105852   porter_tokenizer *t;
       
105853   t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
       
105854   if( t==NULL ) return SQLITE_NOMEM;
       
105855   memset(t, 0, sizeof(*t));
       
105856   *ppTokenizer = &t->base;
       
105857   return SQLITE_OK;
       
105858 }
       
105859 
       
105860 /*
       
105861 ** Destroy a tokenizer
       
105862 */
       
105863 static int porterDestroy(sqlite3_tokenizer *pTokenizer){
       
105864   sqlite3_free(pTokenizer);
       
105865   return SQLITE_OK;
       
105866 }
       
105867 
       
105868 /*
       
105869 ** Prepare to begin tokenizing a particular string.  The input
       
105870 ** string to be tokenized is zInput[0..nInput-1].  A cursor
       
105871 ** used to incrementally tokenize this string is returned in 
       
105872 ** *ppCursor.
       
105873 */
       
105874 static int porterOpen(
       
105875   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
       
105876   const char *zInput, int nInput,        /* String to be tokenized */
       
105877   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
       
105878 ){
       
105879   porter_tokenizer_cursor *c;
       
105880 
       
105881   c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
       
105882   if( c==NULL ) return SQLITE_NOMEM;
       
105883 
       
105884   c->zInput = zInput;
       
105885   if( zInput==0 ){
       
105886     c->nInput = 0;
       
105887   }else if( nInput<0 ){
       
105888     c->nInput = (int)strlen(zInput);
       
105889   }else{
       
105890     c->nInput = nInput;
       
105891   }
       
105892   c->iOffset = 0;                 /* start tokenizing at the beginning */
       
105893   c->iToken = 0;
       
105894   c->zToken = NULL;               /* no space allocated, yet. */
       
105895   c->nAllocated = 0;
       
105896 
       
105897   *ppCursor = &c->base;
       
105898   return SQLITE_OK;
       
105899 }
       
105900 
       
105901 /*
       
105902 ** Close a tokenization cursor previously opened by a call to
       
105903 ** porterOpen() above.
       
105904 */
       
105905 static int porterClose(sqlite3_tokenizer_cursor *pCursor){
       
105906   porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
       
105907   sqlite3_free(c->zToken);
       
105908   sqlite3_free(c);
       
105909   return SQLITE_OK;
       
105910 }
       
105911 /*
       
105912 ** Vowel or consonant
       
105913 */
       
105914 static const char cType[] = {
       
105915    0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
       
105916    1, 1, 1, 2, 1
       
105917 };
       
105918 
       
105919 /*
       
105920 ** isConsonant() and isVowel() determine if their first character in
       
105921 ** the string they point to is a consonant or a vowel, according
       
105922 ** to Porter ruls.  
       
105923 **
       
105924 ** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
       
105925 ** 'Y' is a consonant unless it follows another consonant,
       
105926 ** in which case it is a vowel.
       
105927 **
       
105928 ** In these routine, the letters are in reverse order.  So the 'y' rule
       
105929 ** is that 'y' is a consonant unless it is followed by another
       
105930 ** consonent.
       
105931 */
       
105932 static int isVowel(const char*);
       
105933 static int isConsonant(const char *z){
       
105934   int j;
       
105935   char x = *z;
       
105936   if( x==0 ) return 0;
       
105937   assert( x>='a' && x<='z' );
       
105938   j = cType[x-'a'];
       
105939   if( j<2 ) return j;
       
105940   return z[1]==0 || isVowel(z + 1);
       
105941 }
       
105942 static int isVowel(const char *z){
       
105943   int j;
       
105944   char x = *z;
       
105945   if( x==0 ) return 0;
       
105946   assert( x>='a' && x<='z' );
       
105947   j = cType[x-'a'];
       
105948   if( j<2 ) return 1-j;
       
105949   return isConsonant(z + 1);
       
105950 }
       
105951 
       
105952 /*
       
105953 ** Let any sequence of one or more vowels be represented by V and let
       
105954 ** C be sequence of one or more consonants.  Then every word can be
       
105955 ** represented as:
       
105956 **
       
105957 **           [C] (VC){m} [V]
       
105958 **
       
105959 ** In prose:  A word is an optional consonant followed by zero or
       
105960 ** vowel-consonant pairs followed by an optional vowel.  "m" is the
       
105961 ** number of vowel consonant pairs.  This routine computes the value
       
105962 ** of m for the first i bytes of a word.
       
105963 **
       
105964 ** Return true if the m-value for z is 1 or more.  In other words,
       
105965 ** return true if z contains at least one vowel that is followed
       
105966 ** by a consonant.
       
105967 **
       
105968 ** In this routine z[] is in reverse order.  So we are really looking
       
105969 ** for an instance of of a consonant followed by a vowel.
       
105970 */
       
105971 static int m_gt_0(const char *z){
       
105972   while( isVowel(z) ){ z++; }
       
105973   if( *z==0 ) return 0;
       
105974   while( isConsonant(z) ){ z++; }
       
105975   return *z!=0;
       
105976 }
       
105977 
       
105978 /* Like mgt0 above except we are looking for a value of m which is
       
105979 ** exactly 1
       
105980 */
       
105981 static int m_eq_1(const char *z){
       
105982   while( isVowel(z) ){ z++; }
       
105983   if( *z==0 ) return 0;
       
105984   while( isConsonant(z) ){ z++; }
       
105985   if( *z==0 ) return 0;
       
105986   while( isVowel(z) ){ z++; }
       
105987   if( *z==0 ) return 1;
       
105988   while( isConsonant(z) ){ z++; }
       
105989   return *z==0;
       
105990 }
       
105991 
       
105992 /* Like mgt0 above except we are looking for a value of m>1 instead
       
105993 ** or m>0
       
105994 */
       
105995 static int m_gt_1(const char *z){
       
105996   while( isVowel(z) ){ z++; }
       
105997   if( *z==0 ) return 0;
       
105998   while( isConsonant(z) ){ z++; }
       
105999   if( *z==0 ) return 0;
       
106000   while( isVowel(z) ){ z++; }
       
106001   if( *z==0 ) return 0;
       
106002   while( isConsonant(z) ){ z++; }
       
106003   return *z!=0;
       
106004 }
       
106005 
       
106006 /*
       
106007 ** Return TRUE if there is a vowel anywhere within z[0..n-1]
       
106008 */
       
106009 static int hasVowel(const char *z){
       
106010   while( isConsonant(z) ){ z++; }
       
106011   return *z!=0;
       
106012 }
       
106013 
       
106014 /*
       
106015 ** Return TRUE if the word ends in a double consonant.
       
106016 **
       
106017 ** The text is reversed here. So we are really looking at
       
106018 ** the first two characters of z[].
       
106019 */
       
106020 static int doubleConsonant(const char *z){
       
106021   return isConsonant(z) && z[0]==z[1] && isConsonant(z+1);
       
106022 }
       
106023 
       
106024 /*
       
106025 ** Return TRUE if the word ends with three letters which
       
106026 ** are consonant-vowel-consonent and where the final consonant
       
106027 ** is not 'w', 'x', or 'y'.
       
106028 **
       
106029 ** The word is reversed here.  So we are really checking the
       
106030 ** first three letters and the first one cannot be in [wxy].
       
106031 */
       
106032 static int star_oh(const char *z){
       
106033   return
       
106034     z[0]!=0 && isConsonant(z) &&
       
106035     z[0]!='w' && z[0]!='x' && z[0]!='y' &&
       
106036     z[1]!=0 && isVowel(z+1) &&
       
106037     z[2]!=0 && isConsonant(z+2);
       
106038 }
       
106039 
       
106040 /*
       
106041 ** If the word ends with zFrom and xCond() is true for the stem
       
106042 ** of the word that preceeds the zFrom ending, then change the 
       
106043 ** ending to zTo.
       
106044 **
       
106045 ** The input word *pz and zFrom are both in reverse order.  zTo
       
106046 ** is in normal order. 
       
106047 **
       
106048 ** Return TRUE if zFrom matches.  Return FALSE if zFrom does not
       
106049 ** match.  Not that TRUE is returned even if xCond() fails and
       
106050 ** no substitution occurs.
       
106051 */
       
106052 static int stem(
       
106053   char **pz,             /* The word being stemmed (Reversed) */
       
106054   const char *zFrom,     /* If the ending matches this... (Reversed) */
       
106055   const char *zTo,       /* ... change the ending to this (not reversed) */
       
106056   int (*xCond)(const char*)   /* Condition that must be true */
       
106057 ){
       
106058   char *z = *pz;
       
106059   while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
       
106060   if( *zFrom!=0 ) return 0;
       
106061   if( xCond && !xCond(z) ) return 1;
       
106062   while( *zTo ){
       
106063     *(--z) = *(zTo++);
       
106064   }
       
106065   *pz = z;
       
106066   return 1;
       
106067 }
       
106068 
       
106069 /*
       
106070 ** This is the fallback stemmer used when the porter stemmer is
       
106071 ** inappropriate.  The input word is copied into the output with
       
106072 ** US-ASCII case folding.  If the input word is too long (more
       
106073 ** than 20 bytes if it contains no digits or more than 6 bytes if
       
106074 ** it contains digits) then word is truncated to 20 or 6 bytes
       
106075 ** by taking 10 or 3 bytes from the beginning and end.
       
106076 */
       
106077 static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
       
106078   int i, mx, j;
       
106079   int hasDigit = 0;
       
106080   for(i=0; i<nIn; i++){
       
106081     int c = zIn[i];
       
106082     if( c>='A' && c<='Z' ){
       
106083       zOut[i] = c - 'A' + 'a';
       
106084     }else{
       
106085       if( c>='0' && c<='9' ) hasDigit = 1;
       
106086       zOut[i] = c;
       
106087     }
       
106088   }
       
106089   mx = hasDigit ? 3 : 10;
       
106090   if( nIn>mx*2 ){
       
106091     for(j=mx, i=nIn-mx; i<nIn; i++, j++){
       
106092       zOut[j] = zOut[i];
       
106093     }
       
106094     i = j;
       
106095   }
       
106096   zOut[i] = 0;
       
106097   *pnOut = i;
       
106098 }
       
106099 
       
106100 
       
106101 /*
       
106102 ** Stem the input word zIn[0..nIn-1].  Store the output in zOut.
       
106103 ** zOut is at least big enough to hold nIn bytes.  Write the actual
       
106104 ** size of the output word (exclusive of the '\0' terminator) into *pnOut.
       
106105 **
       
106106 ** Any upper-case characters in the US-ASCII character set ([A-Z])
       
106107 ** are converted to lower case.  Upper-case UTF characters are
       
106108 ** unchanged.
       
106109 **
       
106110 ** Words that are longer than about 20 bytes are stemmed by retaining
       
106111 ** a few bytes from the beginning and the end of the word.  If the
       
106112 ** word contains digits, 3 bytes are taken from the beginning and
       
106113 ** 3 bytes from the end.  For long words without digits, 10 bytes
       
106114 ** are taken from each end.  US-ASCII case folding still applies.
       
106115 ** 
       
106116 ** If the input word contains not digits but does characters not 
       
106117 ** in [a-zA-Z] then no stemming is attempted and this routine just 
       
106118 ** copies the input into the input into the output with US-ASCII
       
106119 ** case folding.
       
106120 **
       
106121 ** Stemming never increases the length of the word.  So there is
       
106122 ** no chance of overflowing the zOut buffer.
       
106123 */
       
106124 static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
       
106125   int i, j, c;
       
106126   char zReverse[28];
       
106127   char *z, *z2;
       
106128   if( nIn<3 || nIn>=sizeof(zReverse)-7 ){
       
106129     /* The word is too big or too small for the porter stemmer.
       
106130     ** Fallback to the copy stemmer */
       
106131     copy_stemmer(zIn, nIn, zOut, pnOut);
       
106132     return;
       
106133   }
       
106134   for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
       
106135     c = zIn[i];
       
106136     if( c>='A' && c<='Z' ){
       
106137       zReverse[j] = c + 'a' - 'A';
       
106138     }else if( c>='a' && c<='z' ){
       
106139       zReverse[j] = c;
       
106140     }else{
       
106141       /* The use of a character not in [a-zA-Z] means that we fallback
       
106142       ** to the copy stemmer */
       
106143       copy_stemmer(zIn, nIn, zOut, pnOut);
       
106144       return;
       
106145     }
       
106146   }
       
106147   memset(&zReverse[sizeof(zReverse)-5], 0, 5);
       
106148   z = &zReverse[j+1];
       
106149 
       
106150 
       
106151   /* Step 1a */
       
106152   if( z[0]=='s' ){
       
106153     if(
       
106154      !stem(&z, "sess", "ss", 0) &&
       
106155      !stem(&z, "sei", "i", 0)  &&
       
106156      !stem(&z, "ss", "ss", 0)
       
106157     ){
       
106158       z++;
       
106159     }
       
106160   }
       
106161 
       
106162   /* Step 1b */  
       
106163   z2 = z;
       
106164   if( stem(&z, "dee", "ee", m_gt_0) ){
       
106165     /* Do nothing.  The work was all in the test */
       
106166   }else if( 
       
106167      (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
       
106168       && z!=z2
       
106169   ){
       
106170      if( stem(&z, "ta", "ate", 0) ||
       
106171          stem(&z, "lb", "ble", 0) ||
       
106172          stem(&z, "zi", "ize", 0) ){
       
106173        /* Do nothing.  The work was all in the test */
       
106174      }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
       
106175        z++;
       
106176      }else if( m_eq_1(z) && star_oh(z) ){
       
106177        *(--z) = 'e';
       
106178      }
       
106179   }
       
106180 
       
106181   /* Step 1c */
       
106182   if( z[0]=='y' && hasVowel(z+1) ){
       
106183     z[0] = 'i';
       
106184   }
       
106185 
       
106186   /* Step 2 */
       
106187   switch( z[1] ){
       
106188    case 'a':
       
106189      stem(&z, "lanoita", "ate", m_gt_0) ||
       
106190      stem(&z, "lanoit", "tion", m_gt_0);
       
106191      break;
       
106192    case 'c':
       
106193      stem(&z, "icne", "ence", m_gt_0) ||
       
106194      stem(&z, "icna", "ance", m_gt_0);
       
106195      break;
       
106196    case 'e':
       
106197      stem(&z, "rezi", "ize", m_gt_0);
       
106198      break;
       
106199    case 'g':
       
106200      stem(&z, "igol", "log", m_gt_0);
       
106201      break;
       
106202    case 'l':
       
106203      stem(&z, "ilb", "ble", m_gt_0) ||
       
106204      stem(&z, "illa", "al", m_gt_0) ||
       
106205      stem(&z, "iltne", "ent", m_gt_0) ||
       
106206      stem(&z, "ile", "e", m_gt_0) ||
       
106207      stem(&z, "ilsuo", "ous", m_gt_0);
       
106208      break;
       
106209    case 'o':
       
106210      stem(&z, "noitazi", "ize", m_gt_0) ||
       
106211      stem(&z, "noita", "ate", m_gt_0) ||
       
106212      stem(&z, "rota", "ate", m_gt_0);
       
106213      break;
       
106214    case 's':
       
106215      stem(&z, "msila", "al", m_gt_0) ||
       
106216      stem(&z, "ssenevi", "ive", m_gt_0) ||
       
106217      stem(&z, "ssenluf", "ful", m_gt_0) ||
       
106218      stem(&z, "ssensuo", "ous", m_gt_0);
       
106219      break;
       
106220    case 't':
       
106221      stem(&z, "itila", "al", m_gt_0) ||
       
106222      stem(&z, "itivi", "ive", m_gt_0) ||
       
106223      stem(&z, "itilib", "ble", m_gt_0);
       
106224      break;
       
106225   }
       
106226 
       
106227   /* Step 3 */
       
106228   switch( z[0] ){
       
106229    case 'e':
       
106230      stem(&z, "etaci", "ic", m_gt_0) ||
       
106231      stem(&z, "evita", "", m_gt_0)   ||
       
106232      stem(&z, "ezila", "al", m_gt_0);
       
106233      break;
       
106234    case 'i':
       
106235      stem(&z, "itici", "ic", m_gt_0);
       
106236      break;
       
106237    case 'l':
       
106238      stem(&z, "laci", "ic", m_gt_0) ||
       
106239      stem(&z, "luf", "", m_gt_0);
       
106240      break;
       
106241    case 's':
       
106242      stem(&z, "ssen", "", m_gt_0);
       
106243      break;
       
106244   }
       
106245 
       
106246   /* Step 4 */
       
106247   switch( z[1] ){
       
106248    case 'a':
       
106249      if( z[0]=='l' && m_gt_1(z+2) ){
       
106250        z += 2;
       
106251      }
       
106252      break;
       
106253    case 'c':
       
106254      if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e')  && m_gt_1(z+4)  ){
       
106255        z += 4;
       
106256      }
       
106257      break;
       
106258    case 'e':
       
106259      if( z[0]=='r' && m_gt_1(z+2) ){
       
106260        z += 2;
       
106261      }
       
106262      break;
       
106263    case 'i':
       
106264      if( z[0]=='c' && m_gt_1(z+2) ){
       
106265        z += 2;
       
106266      }
       
106267      break;
       
106268    case 'l':
       
106269      if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
       
106270        z += 4;
       
106271      }
       
106272      break;
       
106273    case 'n':
       
106274      if( z[0]=='t' ){
       
106275        if( z[2]=='a' ){
       
106276          if( m_gt_1(z+3) ){
       
106277            z += 3;
       
106278          }
       
106279        }else if( z[2]=='e' ){
       
106280          stem(&z, "tneme", "", m_gt_1) ||
       
106281          stem(&z, "tnem", "", m_gt_1) ||
       
106282          stem(&z, "tne", "", m_gt_1);
       
106283        }
       
106284      }
       
106285      break;
       
106286    case 'o':
       
106287      if( z[0]=='u' ){
       
106288        if( m_gt_1(z+2) ){
       
106289          z += 2;
       
106290        }
       
106291      }else if( z[3]=='s' || z[3]=='t' ){
       
106292        stem(&z, "noi", "", m_gt_1);
       
106293      }
       
106294      break;
       
106295    case 's':
       
106296      if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
       
106297        z += 3;
       
106298      }
       
106299      break;
       
106300    case 't':
       
106301      stem(&z, "eta", "", m_gt_1) ||
       
106302      stem(&z, "iti", "", m_gt_1);
       
106303      break;
       
106304    case 'u':
       
106305      if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
       
106306        z += 3;
       
106307      }
       
106308      break;
       
106309    case 'v':
       
106310    case 'z':
       
106311      if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
       
106312        z += 3;
       
106313      }
       
106314      break;
       
106315   }
       
106316 
       
106317   /* Step 5a */
       
106318   if( z[0]=='e' ){
       
106319     if( m_gt_1(z+1) ){
       
106320       z++;
       
106321     }else if( m_eq_1(z+1) && !star_oh(z+1) ){
       
106322       z++;
       
106323     }
       
106324   }
       
106325 
       
106326   /* Step 5b */
       
106327   if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
       
106328     z++;
       
106329   }
       
106330 
       
106331   /* z[] is now the stemmed word in reverse order.  Flip it back
       
106332   ** around into forward order and return.
       
106333   */
       
106334   *pnOut = i = strlen(z);
       
106335   zOut[i] = 0;
       
106336   while( *z ){
       
106337     zOut[--i] = *(z++);
       
106338   }
       
106339 }
       
106340 
       
106341 /*
       
106342 ** Characters that can be part of a token.  We assume any character
       
106343 ** whose value is greater than 0x80 (any UTF character) can be
       
106344 ** part of a token.  In other words, delimiters all must have
       
106345 ** values of 0x7f or lower.
       
106346 */
       
106347 static const char porterIdChar[] = {
       
106348 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
       
106349     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
       
106350     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
       
106351     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
       
106352     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
       
106353     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
       
106354 };
       
106355 #define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
       
106356 
       
106357 /*
       
106358 ** Extract the next token from a tokenization cursor.  The cursor must
       
106359 ** have been opened by a prior call to porterOpen().
       
106360 */
       
106361 static int porterNext(
       
106362   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by porterOpen */
       
106363   const char **pzToken,               /* OUT: *pzToken is the token text */
       
106364   int *pnBytes,                       /* OUT: Number of bytes in token */
       
106365   int *piStartOffset,                 /* OUT: Starting offset of token */
       
106366   int *piEndOffset,                   /* OUT: Ending offset of token */
       
106367   int *piPosition                     /* OUT: Position integer of token */
       
106368 ){
       
106369   porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
       
106370   const char *z = c->zInput;
       
106371 
       
106372   while( c->iOffset<c->nInput ){
       
106373     int iStartOffset, ch;
       
106374 
       
106375     /* Scan past delimiter characters */
       
106376     while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
       
106377       c->iOffset++;
       
106378     }
       
106379 
       
106380     /* Count non-delimiter characters. */
       
106381     iStartOffset = c->iOffset;
       
106382     while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
       
106383       c->iOffset++;
       
106384     }
       
106385 
       
106386     if( c->iOffset>iStartOffset ){
       
106387       int n = c->iOffset-iStartOffset;
       
106388       if( n>c->nAllocated ){
       
106389         c->nAllocated = n+20;
       
106390         c->zToken = sqlite3_realloc(c->zToken, c->nAllocated);
       
106391         if( c->zToken==NULL ) return SQLITE_NOMEM;
       
106392       }
       
106393       porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
       
106394       *pzToken = c->zToken;
       
106395       *piStartOffset = iStartOffset;
       
106396       *piEndOffset = c->iOffset;
       
106397       *piPosition = c->iToken++;
       
106398       return SQLITE_OK;
       
106399     }
       
106400   }
       
106401   return SQLITE_DONE;
       
106402 }
       
106403 
       
106404 /*
       
106405 ** The set of routines that implement the porter-stemmer tokenizer
       
106406 */
       
106407 static const sqlite3_tokenizer_module porterTokenizerModule = {
       
106408   0,
       
106409   porterCreate,
       
106410   porterDestroy,
       
106411   porterOpen,
       
106412   porterClose,
       
106413   porterNext,
       
106414 };
       
106415 
       
106416 /*
       
106417 ** Allocate a new porter tokenizer.  Return a pointer to the new
       
106418 ** tokenizer in *ppModule
       
106419 */
       
106420 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
       
106421   sqlite3_tokenizer_module const**ppModule
       
106422 ){
       
106423   *ppModule = &porterTokenizerModule;
       
106424 }
       
106425 
       
106426 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
       
106427 
       
106428 /************** End of fts3_porter.c *****************************************/
       
106429 /************** Begin file fts3_tokenizer.c **********************************/
       
106430 /*
       
106431 ** 2007 June 22
       
106432 **
       
106433 ** The author disclaims copyright to this source code.  In place of
       
106434 ** a legal notice, here is a blessing:
       
106435 **
       
106436 **    May you do good and not evil.
       
106437 **    May you find forgiveness for yourself and forgive others.
       
106438 **    May you share freely, never taking more than you give.
       
106439 **
       
106440 ******************************************************************************
       
106441 **
       
106442 ** This is part of an SQLite module implementing full-text search.
       
106443 ** This particular file implements the generic tokenizer interface.
       
106444 */
       
106445 
       
106446 /*
       
106447 ** The code in this file is only compiled if:
       
106448 **
       
106449 **     * The FTS3 module is being built as an extension
       
106450 **       (in which case SQLITE_CORE is not defined), or
       
106451 **
       
106452 **     * The FTS3 module is being built into the core of
       
106453 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
       
106454 */
       
106455 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
       
106456 
       
106457 #ifndef SQLITE_CORE
       
106458   SQLITE_EXTENSION_INIT1
       
106459 #endif
       
106460 
       
106461 
       
106462 /*
       
106463 ** Implementation of the SQL scalar function for accessing the underlying 
       
106464 ** hash table. This function may be called as follows:
       
106465 **
       
106466 **   SELECT <function-name>(<key-name>);
       
106467 **   SELECT <function-name>(<key-name>, <pointer>);
       
106468 **
       
106469 ** where <function-name> is the name passed as the second argument
       
106470 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
       
106471 **
       
106472 ** If the <pointer> argument is specified, it must be a blob value
       
106473 ** containing a pointer to be stored as the hash data corresponding
       
106474 ** to the string <key-name>. If <pointer> is not specified, then
       
106475 ** the string <key-name> must already exist in the has table. Otherwise,
       
106476 ** an error is returned.
       
106477 **
       
106478 ** Whether or not the <pointer> argument is specified, the value returned
       
106479 ** is a blob containing the pointer stored as the hash data corresponding
       
106480 ** to string <key-name> (after the hash-table is updated, if applicable).
       
106481 */
       
106482 static void scalarFunc(
       
106483   sqlite3_context *context,
       
106484   int argc,
       
106485   sqlite3_value **argv
       
106486 ){
       
106487   fts3Hash *pHash;
       
106488   void *pPtr = 0;
       
106489   const unsigned char *zName;
       
106490   int nName;
       
106491 
       
106492   assert( argc==1 || argc==2 );
       
106493 
       
106494   pHash = (fts3Hash *)sqlite3_user_data(context);
       
106495 
       
106496   zName = sqlite3_value_text(argv[0]);
       
106497   nName = sqlite3_value_bytes(argv[0])+1;
       
106498 
       
106499   if( argc==2 ){
       
106500     void *pOld;
       
106501     int n = sqlite3_value_bytes(argv[1]);
       
106502     if( n!=sizeof(pPtr) ){
       
106503       sqlite3_result_error(context, "argument type mismatch", -1);
       
106504       return;
       
106505     }
       
106506     pPtr = *(void **)sqlite3_value_blob(argv[1]);
       
106507     pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
       
106508     if( pOld==pPtr ){
       
106509       sqlite3_result_error(context, "out of memory", -1);
       
106510       return;
       
106511     }
       
106512   }else{
       
106513     pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
       
106514     if( !pPtr ){
       
106515       char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
       
106516       sqlite3_result_error(context, zErr, -1);
       
106517       sqlite3_free(zErr);
       
106518       return;
       
106519     }
       
106520   }
       
106521 
       
106522   sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
       
106523 }
       
106524 
       
106525 #ifdef SQLITE_TEST
       
106526 
       
106527 
       
106528 /*
       
106529 ** Implementation of a special SQL scalar function for testing tokenizers 
       
106530 ** designed to be used in concert with the Tcl testing framework. This
       
106531 ** function must be called with two arguments:
       
106532 **
       
106533 **   SELECT <function-name>(<key-name>, <input-string>);
       
106534 **   SELECT <function-name>(<key-name>, <pointer>);
       
106535 **
       
106536 ** where <function-name> is the name passed as the second argument
       
106537 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
       
106538 ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
       
106539 **
       
106540 ** The return value is a string that may be interpreted as a Tcl
       
106541 ** list. For each token in the <input-string>, three elements are
       
106542 ** added to the returned list. The first is the token position, the 
       
106543 ** second is the token text (folded, stemmed, etc.) and the third is the
       
106544 ** substring of <input-string> associated with the token. For example, 
       
106545 ** using the built-in "simple" tokenizer:
       
106546 **
       
106547 **   SELECT fts_tokenizer_test('simple', 'I don't see how');
       
106548 **
       
106549 ** will return the string:
       
106550 **
       
106551 **   "{0 i I 1 dont don't 2 see see 3 how how}"
       
106552 **   
       
106553 */
       
106554 static void testFunc(
       
106555   sqlite3_context *context,
       
106556   int argc,
       
106557   sqlite3_value **argv
       
106558 ){
       
106559   fts3Hash *pHash;
       
106560   sqlite3_tokenizer_module *p;
       
106561   sqlite3_tokenizer *pTokenizer = 0;
       
106562   sqlite3_tokenizer_cursor *pCsr = 0;
       
106563 
       
106564   const char *zErr = 0;
       
106565 
       
106566   const char *zName;
       
106567   int nName;
       
106568   const char *zInput;
       
106569   int nInput;
       
106570 
       
106571   const char *zArg = 0;
       
106572 
       
106573   const char *zToken;
       
106574   int nToken;
       
106575   int iStart;
       
106576   int iEnd;
       
106577   int iPos;
       
106578 
       
106579   Tcl_Obj *pRet;
       
106580 
       
106581   assert( argc==2 || argc==3 );
       
106582 
       
106583   nName = sqlite3_value_bytes(argv[0]);
       
106584   zName = (const char *)sqlite3_value_text(argv[0]);
       
106585   nInput = sqlite3_value_bytes(argv[argc-1]);
       
106586   zInput = (const char *)sqlite3_value_text(argv[argc-1]);
       
106587 
       
106588   if( argc==3 ){
       
106589     zArg = (const char *)sqlite3_value_text(argv[1]);
       
106590   }
       
106591 
       
106592   pHash = (fts3Hash *)sqlite3_user_data(context);
       
106593   p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
       
106594 
       
106595   if( !p ){
       
106596     char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
       
106597     sqlite3_result_error(context, zErr, -1);
       
106598     sqlite3_free(zErr);
       
106599     return;
       
106600   }
       
106601 
       
106602   pRet = Tcl_NewObj();
       
106603   Tcl_IncrRefCount(pRet);
       
106604 
       
106605   if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){
       
106606     zErr = "error in xCreate()";
       
106607     goto finish;
       
106608   }
       
106609   pTokenizer->pModule = p;
       
106610   if( SQLITE_OK!=p->xOpen(pTokenizer, zInput, nInput, &pCsr) ){
       
106611     zErr = "error in xOpen()";
       
106612     goto finish;
       
106613   }
       
106614   pCsr->pTokenizer = pTokenizer;
       
106615 
       
106616   while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
       
106617     Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
       
106618     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
       
106619     zToken = &zInput[iStart];
       
106620     nToken = iEnd-iStart;
       
106621     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
       
106622   }
       
106623 
       
106624   if( SQLITE_OK!=p->xClose(pCsr) ){
       
106625     zErr = "error in xClose()";
       
106626     goto finish;
       
106627   }
       
106628   if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
       
106629     zErr = "error in xDestroy()";
       
106630     goto finish;
       
106631   }
       
106632 
       
106633 finish:
       
106634   if( zErr ){
       
106635     sqlite3_result_error(context, zErr, -1);
       
106636   }else{
       
106637     sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
       
106638   }
       
106639   Tcl_DecrRefCount(pRet);
       
106640 }
       
106641 
       
106642 static
       
106643 int registerTokenizer(
       
106644   sqlite3 *db, 
       
106645   char *zName, 
       
106646   const sqlite3_tokenizer_module *p
       
106647 ){
       
106648   int rc;
       
106649   sqlite3_stmt *pStmt;
       
106650   const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
       
106651 
       
106652   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
       
106653   if( rc!=SQLITE_OK ){
       
106654     return rc;
       
106655   }
       
106656 
       
106657   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
       
106658   sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
       
106659   sqlite3_step(pStmt);
       
106660 
       
106661   return sqlite3_finalize(pStmt);
       
106662 }
       
106663 
       
106664 static
       
106665 int queryTokenizer(
       
106666   sqlite3 *db, 
       
106667   char *zName,  
       
106668   const sqlite3_tokenizer_module **pp
       
106669 ){
       
106670   int rc;
       
106671   sqlite3_stmt *pStmt;
       
106672   const char zSql[] = "SELECT fts3_tokenizer(?)";
       
106673 
       
106674   *pp = 0;
       
106675   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
       
106676   if( rc!=SQLITE_OK ){
       
106677     return rc;
       
106678   }
       
106679 
       
106680   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
       
106681   if( SQLITE_ROW==sqlite3_step(pStmt) ){
       
106682     if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
       
106683       memcpy(pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
       
106684     }
       
106685   }
       
106686 
       
106687   return sqlite3_finalize(pStmt);
       
106688 }
       
106689 
       
106690 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
       
106691 
       
106692 /*
       
106693 ** Implementation of the scalar function fts3_tokenizer_internal_test().
       
106694 ** This function is used for testing only, it is not included in the
       
106695 ** build unless SQLITE_TEST is defined.
       
106696 **
       
106697 ** The purpose of this is to test that the fts3_tokenizer() function
       
106698 ** can be used as designed by the C-code in the queryTokenizer and
       
106699 ** registerTokenizer() functions above. These two functions are repeated
       
106700 ** in the README.tokenizer file as an example, so it is important to
       
106701 ** test them.
       
106702 **
       
106703 ** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
       
106704 ** function with no arguments. An assert() will fail if a problem is
       
106705 ** detected. i.e.:
       
106706 **
       
106707 **     SELECT fts3_tokenizer_internal_test();
       
106708 **
       
106709 */
       
106710 static void intTestFunc(
       
106711   sqlite3_context *context,
       
106712   int argc,
       
106713   sqlite3_value **argv
       
106714 ){
       
106715   int rc;
       
106716   const sqlite3_tokenizer_module *p1;
       
106717   const sqlite3_tokenizer_module *p2;
       
106718   sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
       
106719 
       
106720   /* Test the query function */
       
106721   sqlite3Fts3SimpleTokenizerModule(&p1);
       
106722   rc = queryTokenizer(db, "simple", &p2);
       
106723   assert( rc==SQLITE_OK );
       
106724   assert( p1==p2 );
       
106725   rc = queryTokenizer(db, "nosuchtokenizer", &p2);
       
106726   assert( rc==SQLITE_ERROR );
       
106727   assert( p2==0 );
       
106728   assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
       
106729 
       
106730   /* Test the storage function */
       
106731   rc = registerTokenizer(db, "nosuchtokenizer", p1);
       
106732   assert( rc==SQLITE_OK );
       
106733   rc = queryTokenizer(db, "nosuchtokenizer", &p2);
       
106734   assert( rc==SQLITE_OK );
       
106735   assert( p2==p1 );
       
106736 
       
106737   sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
       
106738 }
       
106739 
       
106740 #endif
       
106741 
       
106742 /*
       
106743 ** Set up SQL objects in database db used to access the contents of
       
106744 ** the hash table pointed to by argument pHash. The hash table must
       
106745 ** been initialised to use string keys, and to take a private copy 
       
106746 ** of the key when a value is inserted. i.e. by a call similar to:
       
106747 **
       
106748 **    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
       
106749 **
       
106750 ** This function adds a scalar function (see header comment above
       
106751 ** scalarFunc() in this file for details) and, if ENABLE_TABLE is
       
106752 ** defined at compilation time, a temporary virtual table (see header 
       
106753 ** comment above struct HashTableVtab) to the database schema. Both 
       
106754 ** provide read/write access to the contents of *pHash.
       
106755 **
       
106756 ** The third argument to this function, zName, is used as the name
       
106757 ** of both the scalar and, if created, the virtual table.
       
106758 */
       
106759 SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
       
106760   sqlite3 *db, 
       
106761   fts3Hash *pHash, 
       
106762   const char *zName
       
106763 ){
       
106764   int rc = SQLITE_OK;
       
106765   void *p = (void *)pHash;
       
106766   const int any = SQLITE_ANY;
       
106767   char *zTest = 0;
       
106768   char *zTest2 = 0;
       
106769 
       
106770 #ifdef SQLITE_TEST
       
106771   void *pdb = (void *)db;
       
106772   zTest = sqlite3_mprintf("%s_test", zName);
       
106773   zTest2 = sqlite3_mprintf("%s_internal_test", zName);
       
106774   if( !zTest || !zTest2 ){
       
106775     rc = SQLITE_NOMEM;
       
106776   }
       
106777 #endif
       
106778 
       
106779   if( rc!=SQLITE_OK
       
106780    || (rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0))
       
106781    || (rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0))
       
106782 #ifdef SQLITE_TEST
       
106783    || (rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0))
       
106784    || (rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0))
       
106785    || (rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0))
       
106786 #endif
       
106787   );
       
106788 
       
106789   sqlite3_free(zTest);
       
106790   sqlite3_free(zTest2);
       
106791   return rc;
       
106792 }
       
106793 
       
106794 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
       
106795 
       
106796 /************** End of fts3_tokenizer.c **************************************/
       
106797 /************** Begin file fts3_tokenizer1.c *********************************/
       
106798 /*
       
106799 ** 2006 Oct 10
       
106800 **
       
106801 ** The author disclaims copyright to this source code.  In place of
       
106802 ** a legal notice, here is a blessing:
       
106803 **
       
106804 **    May you do good and not evil.
       
106805 **    May you find forgiveness for yourself and forgive others.
       
106806 **    May you share freely, never taking more than you give.
       
106807 **
       
106808 ******************************************************************************
       
106809 **
       
106810 ** Implementation of the "simple" full-text-search tokenizer.
       
106811 */
       
106812 
       
106813 /*
       
106814 ** The code in this file is only compiled if:
       
106815 **
       
106816 **     * The FTS3 module is being built as an extension
       
106817 **       (in which case SQLITE_CORE is not defined), or
       
106818 **
       
106819 **     * The FTS3 module is being built into the core of
       
106820 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
       
106821 */
       
106822 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
       
106823 
       
106824 
       
106825 
       
106826 
       
106827 typedef struct simple_tokenizer {
       
106828   sqlite3_tokenizer base;
       
106829   char delim[128];             /* flag ASCII delimiters */
       
106830 } simple_tokenizer;
       
106831 
       
106832 typedef struct simple_tokenizer_cursor {
       
106833   sqlite3_tokenizer_cursor base;
       
106834   const char *pInput;          /* input we are tokenizing */
       
106835   int nBytes;                  /* size of the input */
       
106836   int iOffset;                 /* current position in pInput */
       
106837   int iToken;                  /* index of next token to be returned */
       
106838   char *pToken;                /* storage for current token */
       
106839   int nTokenAllocated;         /* space allocated to zToken buffer */
       
106840 } simple_tokenizer_cursor;
       
106841 
       
106842 
       
106843 /* Forward declaration */
       
106844 static const sqlite3_tokenizer_module simpleTokenizerModule;
       
106845 
       
106846 static int simpleDelim(simple_tokenizer *t, unsigned char c){
       
106847   return c<0x80 && t->delim[c];
       
106848 }
       
106849 
       
106850 /*
       
106851 ** Create a new tokenizer instance.
       
106852 */
       
106853 static int simpleCreate(
       
106854   int argc, const char * const *argv,
       
106855   sqlite3_tokenizer **ppTokenizer
       
106856 ){
       
106857   simple_tokenizer *t;
       
106858 
       
106859   t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
       
106860   if( t==NULL ) return SQLITE_NOMEM;
       
106861   memset(t, 0, sizeof(*t));
       
106862 
       
106863   /* TODO(shess) Delimiters need to remain the same from run to run,
       
106864   ** else we need to reindex.  One solution would be a meta-table to
       
106865   ** track such information in the database, then we'd only want this
       
106866   ** information on the initial create.
       
106867   */
       
106868   if( argc>1 ){
       
106869     int i, n = strlen(argv[1]);
       
106870     for(i=0; i<n; i++){
       
106871       unsigned char ch = argv[1][i];
       
106872       /* We explicitly don't support UTF-8 delimiters for now. */
       
106873       if( ch>=0x80 ){
       
106874         sqlite3_free(t);
       
106875         return SQLITE_ERROR;
       
106876       }
       
106877       t->delim[ch] = 1;
       
106878     }
       
106879   } else {
       
106880     /* Mark non-alphanumeric ASCII characters as delimiters */
       
106881     int i;
       
106882     for(i=1; i<0x80; i++){
       
106883       t->delim[i] = !isalnum(i);
       
106884     }
       
106885   }
       
106886 
       
106887   *ppTokenizer = &t->base;
       
106888   return SQLITE_OK;
       
106889 }
       
106890 
       
106891 /*
       
106892 ** Destroy a tokenizer
       
106893 */
       
106894 static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
       
106895   sqlite3_free(pTokenizer);
       
106896   return SQLITE_OK;
       
106897 }
       
106898 
       
106899 /*
       
106900 ** Prepare to begin tokenizing a particular string.  The input
       
106901 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
       
106902 ** used to incrementally tokenize this string is returned in 
       
106903 ** *ppCursor.
       
106904 */
       
106905 static int simpleOpen(
       
106906   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
       
106907   const char *pInput, int nBytes,        /* String to be tokenized */
       
106908   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
       
106909 ){
       
106910   simple_tokenizer_cursor *c;
       
106911 
       
106912   c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
       
106913   if( c==NULL ) return SQLITE_NOMEM;
       
106914 
       
106915   c->pInput = pInput;
       
106916   if( pInput==0 ){
       
106917     c->nBytes = 0;
       
106918   }else if( nBytes<0 ){
       
106919     c->nBytes = (int)strlen(pInput);
       
106920   }else{
       
106921     c->nBytes = nBytes;
       
106922   }
       
106923   c->iOffset = 0;                 /* start tokenizing at the beginning */
       
106924   c->iToken = 0;
       
106925   c->pToken = NULL;               /* no space allocated, yet. */
       
106926   c->nTokenAllocated = 0;
       
106927 
       
106928   *ppCursor = &c->base;
       
106929   return SQLITE_OK;
       
106930 }
       
106931 
       
106932 /*
       
106933 ** Close a tokenization cursor previously opened by a call to
       
106934 ** simpleOpen() above.
       
106935 */
       
106936 static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
       
106937   simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
       
106938   sqlite3_free(c->pToken);
       
106939   sqlite3_free(c);
       
106940   return SQLITE_OK;
       
106941 }
       
106942 
       
106943 /*
       
106944 ** Extract the next token from a tokenization cursor.  The cursor must
       
106945 ** have been opened by a prior call to simpleOpen().
       
106946 */
       
106947 static int simpleNext(
       
106948   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
       
106949   const char **ppToken,               /* OUT: *ppToken is the token text */
       
106950   int *pnBytes,                       /* OUT: Number of bytes in token */
       
106951   int *piStartOffset,                 /* OUT: Starting offset of token */
       
106952   int *piEndOffset,                   /* OUT: Ending offset of token */
       
106953   int *piPosition                     /* OUT: Position integer of token */
       
106954 ){
       
106955   simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
       
106956   simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
       
106957   unsigned char *p = (unsigned char *)c->pInput;
       
106958 
       
106959   while( c->iOffset<c->nBytes ){
       
106960     int iStartOffset;
       
106961 
       
106962     /* Scan past delimiter characters */
       
106963     while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
       
106964       c->iOffset++;
       
106965     }
       
106966 
       
106967     /* Count non-delimiter characters. */
       
106968     iStartOffset = c->iOffset;
       
106969     while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
       
106970       c->iOffset++;
       
106971     }
       
106972 
       
106973     if( c->iOffset>iStartOffset ){
       
106974       int i, n = c->iOffset-iStartOffset;
       
106975       if( n>c->nTokenAllocated ){
       
106976         c->nTokenAllocated = n+20;
       
106977         c->pToken = sqlite3_realloc(c->pToken, c->nTokenAllocated);
       
106978         if( c->pToken==NULL ) return SQLITE_NOMEM;
       
106979       }
       
106980       for(i=0; i<n; i++){
       
106981         /* TODO(shess) This needs expansion to handle UTF-8
       
106982         ** case-insensitivity.
       
106983         */
       
106984         unsigned char ch = p[iStartOffset+i];
       
106985         c->pToken[i] = ch<0x80 ? tolower(ch) : ch;
       
106986       }
       
106987       *ppToken = c->pToken;
       
106988       *pnBytes = n;
       
106989       *piStartOffset = iStartOffset;
       
106990       *piEndOffset = c->iOffset;
       
106991       *piPosition = c->iToken++;
       
106992 
       
106993       return SQLITE_OK;
       
106994     }
       
106995   }
       
106996   return SQLITE_DONE;
       
106997 }
       
106998 
       
106999 /*
       
107000 ** The set of routines that implement the simple tokenizer
       
107001 */
       
107002 static const sqlite3_tokenizer_module simpleTokenizerModule = {
       
107003   0,
       
107004   simpleCreate,
       
107005   simpleDestroy,
       
107006   simpleOpen,
       
107007   simpleClose,
       
107008   simpleNext,
       
107009 };
       
107010 
       
107011 /*
       
107012 ** Allocate a new simple tokenizer.  Return a pointer to the new
       
107013 ** tokenizer in *ppModule
       
107014 */
       
107015 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
       
107016   sqlite3_tokenizer_module const**ppModule
       
107017 ){
       
107018   *ppModule = &simpleTokenizerModule;
       
107019 }
       
107020 
       
107021 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
       
107022 
       
107023 /************** End of fts3_tokenizer1.c *************************************/
       
107024 /************** Begin file rtree.c *******************************************/
       
107025 /*
       
107026 ** 2001 September 15
       
107027 **
       
107028 ** The author disclaims copyright to this source code.  In place of
       
107029 ** a legal notice, here is a blessing:
       
107030 **
       
107031 **    May you do good and not evil.
       
107032 **    May you find forgiveness for yourself and forgive others.
       
107033 **    May you share freely, never taking more than you give.
       
107034 **
       
107035 *************************************************************************
       
107036 ** This file contains code for implementations of the r-tree and r*-tree
       
107037 ** algorithms packaged as an SQLite virtual table module.
       
107038 **
       
107039 ** $Id: rtree.c,v 1.14 2009/08/06 18:36:47 danielk1977 Exp $
       
107040 */
       
107041 
       
107042 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
       
107043 
       
107044 /*
       
107045 ** This file contains an implementation of a couple of different variants
       
107046 ** of the r-tree algorithm. See the README file for further details. The 
       
107047 ** same data-structure is used for all, but the algorithms for insert and
       
107048 ** delete operations vary. The variants used are selected at compile time 
       
107049 ** by defining the following symbols:
       
107050 */
       
107051 
       
107052 /* Either, both or none of the following may be set to activate 
       
107053 ** r*tree variant algorithms.
       
107054 */
       
107055 #define VARIANT_RSTARTREE_CHOOSESUBTREE 0
       
107056 #define VARIANT_RSTARTREE_REINSERT      1
       
107057 
       
107058 /* 
       
107059 ** Exactly one of the following must be set to 1.
       
107060 */
       
107061 #define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0
       
107062 #define VARIANT_GUTTMAN_LINEAR_SPLIT    0
       
107063 #define VARIANT_RSTARTREE_SPLIT         1
       
107064 
       
107065 #define VARIANT_GUTTMAN_SPLIT \
       
107066         (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT)
       
107067 
       
107068 #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
       
107069   #define PickNext QuadraticPickNext
       
107070   #define PickSeeds QuadraticPickSeeds
       
107071   #define AssignCells splitNodeGuttman
       
107072 #endif
       
107073 #if VARIANT_GUTTMAN_LINEAR_SPLIT
       
107074   #define PickNext LinearPickNext
       
107075   #define PickSeeds LinearPickSeeds
       
107076   #define AssignCells splitNodeGuttman
       
107077 #endif
       
107078 #if VARIANT_RSTARTREE_SPLIT
       
107079   #define AssignCells splitNodeStartree
       
107080 #endif
       
107081 
       
107082 
       
107083 #ifndef SQLITE_CORE
       
107084   SQLITE_EXTENSION_INIT1
       
107085 #else
       
107086 #endif
       
107087 
       
107088 
       
107089 #ifndef SQLITE_AMALGAMATION
       
107090 typedef sqlite3_int64 i64;
       
107091 typedef unsigned char u8;
       
107092 typedef unsigned int u32;
       
107093 #endif
       
107094 
       
107095 typedef struct Rtree Rtree;
       
107096 typedef struct RtreeCursor RtreeCursor;
       
107097 typedef struct RtreeNode RtreeNode;
       
107098 typedef struct RtreeCell RtreeCell;
       
107099 typedef struct RtreeConstraint RtreeConstraint;
       
107100 typedef union RtreeCoord RtreeCoord;
       
107101 
       
107102 /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
       
107103 #define RTREE_MAX_DIMENSIONS 5
       
107104 
       
107105 /* Size of hash table Rtree.aHash. This hash table is not expected to
       
107106 ** ever contain very many entries, so a fixed number of buckets is 
       
107107 ** used.
       
107108 */
       
107109 #define HASHSIZE 128
       
107110 
       
107111 /* 
       
107112 ** An rtree virtual-table object.
       
107113 */
       
107114 struct Rtree {
       
107115   sqlite3_vtab base;
       
107116   sqlite3 *db;                /* Host database connection */
       
107117   int iNodeSize;              /* Size in bytes of each node in the node table */
       
107118   int nDim;                   /* Number of dimensions */
       
107119   int nBytesPerCell;          /* Bytes consumed per cell */
       
107120   int iDepth;                 /* Current depth of the r-tree structure */
       
107121   char *zDb;                  /* Name of database containing r-tree table */
       
107122   char *zName;                /* Name of r-tree table */ 
       
107123   RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */ 
       
107124   int nBusy;                  /* Current number of users of this structure */
       
107125 
       
107126   /* List of nodes removed during a CondenseTree operation. List is
       
107127   ** linked together via the pointer normally used for hash chains -
       
107128   ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree 
       
107129   ** headed by the node (leaf nodes have RtreeNode.iNode==0).
       
107130   */
       
107131   RtreeNode *pDeleted;
       
107132   int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
       
107133 
       
107134   /* Statements to read/write/delete a record from xxx_node */
       
107135   sqlite3_stmt *pReadNode;
       
107136   sqlite3_stmt *pWriteNode;
       
107137   sqlite3_stmt *pDeleteNode;
       
107138 
       
107139   /* Statements to read/write/delete a record from xxx_rowid */
       
107140   sqlite3_stmt *pReadRowid;
       
107141   sqlite3_stmt *pWriteRowid;
       
107142   sqlite3_stmt *pDeleteRowid;
       
107143 
       
107144   /* Statements to read/write/delete a record from xxx_parent */
       
107145   sqlite3_stmt *pReadParent;
       
107146   sqlite3_stmt *pWriteParent;
       
107147   sqlite3_stmt *pDeleteParent;
       
107148 
       
107149   int eCoordType;
       
107150 };
       
107151 
       
107152 /* Possible values for eCoordType: */
       
107153 #define RTREE_COORD_REAL32 0
       
107154 #define RTREE_COORD_INT32  1
       
107155 
       
107156 /*
       
107157 ** The minimum number of cells allowed for a node is a third of the 
       
107158 ** maximum. In Gutman's notation:
       
107159 **
       
107160 **     m = M/3
       
107161 **
       
107162 ** If an R*-tree "Reinsert" operation is required, the same number of
       
107163 ** cells are removed from the overfull node and reinserted into the tree.
       
107164 */
       
107165 #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
       
107166 #define RTREE_REINSERT(p) RTREE_MINCELLS(p)
       
107167 #define RTREE_MAXCELLS 51
       
107168 
       
107169 /* 
       
107170 ** An rtree cursor object.
       
107171 */
       
107172 struct RtreeCursor {
       
107173   sqlite3_vtab_cursor base;
       
107174   RtreeNode *pNode;                 /* Node cursor is currently pointing at */
       
107175   int iCell;                        /* Index of current cell in pNode */
       
107176   int iStrategy;                    /* Copy of idxNum search parameter */
       
107177   int nConstraint;                  /* Number of entries in aConstraint */
       
107178   RtreeConstraint *aConstraint;     /* Search constraints. */
       
107179 };
       
107180 
       
107181 union RtreeCoord {
       
107182   float f;
       
107183   int i;
       
107184 };
       
107185 
       
107186 /*
       
107187 ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
       
107188 ** formatted as a double. This macro assumes that local variable pRtree points
       
107189 ** to the Rtree structure associated with the RtreeCoord.
       
107190 */
       
107191 #define DCOORD(coord) (                           \
       
107192   (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
       
107193     ((double)coord.f) :                           \
       
107194     ((double)coord.i)                             \
       
107195 )
       
107196 
       
107197 /*
       
107198 ** A search constraint.
       
107199 */
       
107200 struct RtreeConstraint {
       
107201   int iCoord;                       /* Index of constrained coordinate */
       
107202   int op;                           /* Constraining operation */
       
107203   double rValue;                    /* Constraint value. */
       
107204 };
       
107205 
       
107206 /* Possible values for RtreeConstraint.op */
       
107207 #define RTREE_EQ 0x41
       
107208 #define RTREE_LE 0x42
       
107209 #define RTREE_LT 0x43
       
107210 #define RTREE_GE 0x44
       
107211 #define RTREE_GT 0x45
       
107212 
       
107213 /* 
       
107214 ** An rtree structure node.
       
107215 **
       
107216 ** Data format (RtreeNode.zData):
       
107217 **
       
107218 **   1. If the node is the root node (node 1), then the first 2 bytes
       
107219 **      of the node contain the tree depth as a big-endian integer.
       
107220 **      For non-root nodes, the first 2 bytes are left unused.
       
107221 **
       
107222 **   2. The next 2 bytes contain the number of entries currently 
       
107223 **      stored in the node.
       
107224 **
       
107225 **   3. The remainder of the node contains the node entries. Each entry
       
107226 **      consists of a single 8-byte integer followed by an even number
       
107227 **      of 4-byte coordinates. For leaf nodes the integer is the rowid
       
107228 **      of a record. For internal nodes it is the node number of a
       
107229 **      child page.
       
107230 */
       
107231 struct RtreeNode {
       
107232   RtreeNode *pParent;               /* Parent node */
       
107233   i64 iNode;
       
107234   int nRef;
       
107235   int isDirty;
       
107236   u8 *zData;
       
107237   RtreeNode *pNext;                 /* Next node in this hash chain */
       
107238 };
       
107239 #define NCELL(pNode) readInt16(&(pNode)->zData[2])
       
107240 
       
107241 /* 
       
107242 ** Structure to store a deserialized rtree record.
       
107243 */
       
107244 struct RtreeCell {
       
107245   i64 iRowid;
       
107246   RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
       
107247 };
       
107248 
       
107249 #ifndef MAX
       
107250 # define MAX(x,y) ((x) < (y) ? (y) : (x))
       
107251 #endif
       
107252 #ifndef MIN
       
107253 # define MIN(x,y) ((x) > (y) ? (y) : (x))
       
107254 #endif
       
107255 
       
107256 /*
       
107257 ** Functions to deserialize a 16 bit integer, 32 bit real number and
       
107258 ** 64 bit integer. The deserialized value is returned.
       
107259 */
       
107260 static int readInt16(u8 *p){
       
107261   return (p[0]<<8) + p[1];
       
107262 }
       
107263 static void readCoord(u8 *p, RtreeCoord *pCoord){
       
107264   u32 i = (
       
107265     (((u32)p[0]) << 24) + 
       
107266     (((u32)p[1]) << 16) + 
       
107267     (((u32)p[2]) <<  8) + 
       
107268     (((u32)p[3]) <<  0)
       
107269   );
       
107270   *(u32 *)pCoord = i;
       
107271 }
       
107272 static i64 readInt64(u8 *p){
       
107273   return (
       
107274     (((i64)p[0]) << 56) + 
       
107275     (((i64)p[1]) << 48) + 
       
107276     (((i64)p[2]) << 40) + 
       
107277     (((i64)p[3]) << 32) + 
       
107278     (((i64)p[4]) << 24) + 
       
107279     (((i64)p[5]) << 16) + 
       
107280     (((i64)p[6]) <<  8) + 
       
107281     (((i64)p[7]) <<  0)
       
107282   );
       
107283 }
       
107284 
       
107285 /*
       
107286 ** Functions to serialize a 16 bit integer, 32 bit real number and
       
107287 ** 64 bit integer. The value returned is the number of bytes written
       
107288 ** to the argument buffer (always 2, 4 and 8 respectively).
       
107289 */
       
107290 static int writeInt16(u8 *p, int i){
       
107291   p[0] = (i>> 8)&0xFF;
       
107292   p[1] = (i>> 0)&0xFF;
       
107293   return 2;
       
107294 }
       
107295 static int writeCoord(u8 *p, RtreeCoord *pCoord){
       
107296   u32 i;
       
107297   assert( sizeof(RtreeCoord)==4 );
       
107298   assert( sizeof(u32)==4 );
       
107299   i = *(u32 *)pCoord;
       
107300   p[0] = (i>>24)&0xFF;
       
107301   p[1] = (i>>16)&0xFF;
       
107302   p[2] = (i>> 8)&0xFF;
       
107303   p[3] = (i>> 0)&0xFF;
       
107304   return 4;
       
107305 }
       
107306 static int writeInt64(u8 *p, i64 i){
       
107307   p[0] = (i>>56)&0xFF;
       
107308   p[1] = (i>>48)&0xFF;
       
107309   p[2] = (i>>40)&0xFF;
       
107310   p[3] = (i>>32)&0xFF;
       
107311   p[4] = (i>>24)&0xFF;
       
107312   p[5] = (i>>16)&0xFF;
       
107313   p[6] = (i>> 8)&0xFF;
       
107314   p[7] = (i>> 0)&0xFF;
       
107315   return 8;
       
107316 }
       
107317 
       
107318 /*
       
107319 ** Increment the reference count of node p.
       
107320 */
       
107321 static void nodeReference(RtreeNode *p){
       
107322   if( p ){
       
107323     p->nRef++;
       
107324   }
       
107325 }
       
107326 
       
107327 /*
       
107328 ** Clear the content of node p (set all bytes to 0x00).
       
107329 */
       
107330 static void nodeZero(Rtree *pRtree, RtreeNode *p){
       
107331   if( p ){
       
107332     memset(&p->zData[2], 0, pRtree->iNodeSize-2);
       
107333     p->isDirty = 1;
       
107334   }
       
107335 }
       
107336 
       
107337 /*
       
107338 ** Given a node number iNode, return the corresponding key to use
       
107339 ** in the Rtree.aHash table.
       
107340 */
       
107341 static int nodeHash(i64 iNode){
       
107342   return (
       
107343     (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^ 
       
107344     (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0)
       
107345   ) % HASHSIZE;
       
107346 }
       
107347 
       
107348 /*
       
107349 ** Search the node hash table for node iNode. If found, return a pointer
       
107350 ** to it. Otherwise, return 0.
       
107351 */
       
107352 static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
       
107353   RtreeNode *p;
       
107354   assert( iNode!=0 );
       
107355   for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
       
107356   return p;
       
107357 }
       
107358 
       
107359 /*
       
107360 ** Add node pNode to the node hash table.
       
107361 */
       
107362 static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
       
107363   if( pNode ){
       
107364     int iHash;
       
107365     assert( pNode->pNext==0 );
       
107366     iHash = nodeHash(pNode->iNode);
       
107367     pNode->pNext = pRtree->aHash[iHash];
       
107368     pRtree->aHash[iHash] = pNode;
       
107369   }
       
107370 }
       
107371 
       
107372 /*
       
107373 ** Remove node pNode from the node hash table.
       
107374 */
       
107375 static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
       
107376   RtreeNode **pp;
       
107377   if( pNode->iNode!=0 ){
       
107378     pp = &pRtree->aHash[nodeHash(pNode->iNode)];
       
107379     for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
       
107380     *pp = pNode->pNext;
       
107381     pNode->pNext = 0;
       
107382   }
       
107383 }
       
107384 
       
107385 /*
       
107386 ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
       
107387 ** indicating that node has not yet been assigned a node number. It is
       
107388 ** assigned a node number when nodeWrite() is called to write the
       
107389 ** node contents out to the database.
       
107390 */
       
107391 static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent, int zero){
       
107392   RtreeNode *pNode;
       
107393   pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
       
107394   if( pNode ){
       
107395     memset(pNode, 0, sizeof(RtreeNode) + (zero?pRtree->iNodeSize:0));
       
107396     pNode->zData = (u8 *)&pNode[1];
       
107397     pNode->nRef = 1;
       
107398     pNode->pParent = pParent;
       
107399     pNode->isDirty = 1;
       
107400     nodeReference(pParent);
       
107401   }
       
107402   return pNode;
       
107403 }
       
107404 
       
107405 /*
       
107406 ** Obtain a reference to an r-tree node.
       
107407 */
       
107408 static int
       
107409 nodeAcquire(
       
107410   Rtree *pRtree,             /* R-tree structure */
       
107411   i64 iNode,                 /* Node number to load */
       
107412   RtreeNode *pParent,        /* Either the parent node or NULL */
       
107413   RtreeNode **ppNode         /* OUT: Acquired node */
       
107414 ){
       
107415   int rc;
       
107416   RtreeNode *pNode;
       
107417 
       
107418   /* Check if the requested node is already in the hash table. If so,
       
107419   ** increase its reference count and return it.
       
107420   */
       
107421   if( (pNode = nodeHashLookup(pRtree, iNode)) ){
       
107422     assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
       
107423     if( pParent && !pNode->pParent ){
       
107424       nodeReference(pParent);
       
107425       pNode->pParent = pParent;
       
107426     }
       
107427     pNode->nRef++;
       
107428     *ppNode = pNode;
       
107429     return SQLITE_OK;
       
107430   }
       
107431 
       
107432   pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
       
107433   if( !pNode ){
       
107434     *ppNode = 0;
       
107435     return SQLITE_NOMEM;
       
107436   }
       
107437   pNode->pParent = pParent;
       
107438   pNode->zData = (u8 *)&pNode[1];
       
107439   pNode->nRef = 1;
       
107440   pNode->iNode = iNode;
       
107441   pNode->isDirty = 0;
       
107442   pNode->pNext = 0;
       
107443 
       
107444   sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
       
107445   rc = sqlite3_step(pRtree->pReadNode);
       
107446   if( rc==SQLITE_ROW ){
       
107447     const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
       
107448     memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
       
107449     nodeReference(pParent);
       
107450   }else{
       
107451     sqlite3_free(pNode);
       
107452     pNode = 0;
       
107453   }
       
107454 
       
107455   *ppNode = pNode;
       
107456   rc = sqlite3_reset(pRtree->pReadNode);
       
107457 
       
107458   if( rc==SQLITE_OK && iNode==1 ){
       
107459     pRtree->iDepth = readInt16(pNode->zData);
       
107460   }
       
107461 
       
107462   assert( (rc==SQLITE_OK && pNode) || (pNode==0 && rc!=SQLITE_OK) );
       
107463   nodeHashInsert(pRtree, pNode);
       
107464 
       
107465   return rc;
       
107466 }
       
107467 
       
107468 /*
       
107469 ** Overwrite cell iCell of node pNode with the contents of pCell.
       
107470 */
       
107471 static void nodeOverwriteCell(
       
107472   Rtree *pRtree, 
       
107473   RtreeNode *pNode,  
       
107474   RtreeCell *pCell, 
       
107475   int iCell
       
107476 ){
       
107477   int ii;
       
107478   u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
       
107479   p += writeInt64(p, pCell->iRowid);
       
107480   for(ii=0; ii<(pRtree->nDim*2); ii++){
       
107481     p += writeCoord(p, &pCell->aCoord[ii]);
       
107482   }
       
107483   pNode->isDirty = 1;
       
107484 }
       
107485 
       
107486 /*
       
107487 ** Remove cell the cell with index iCell from node pNode.
       
107488 */
       
107489 static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
       
107490   u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
       
107491   u8 *pSrc = &pDst[pRtree->nBytesPerCell];
       
107492   int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
       
107493   memmove(pDst, pSrc, nByte);
       
107494   writeInt16(&pNode->zData[2], NCELL(pNode)-1);
       
107495   pNode->isDirty = 1;
       
107496 }
       
107497 
       
107498 /*
       
107499 ** Insert the contents of cell pCell into node pNode. If the insert
       
107500 ** is successful, return SQLITE_OK.
       
107501 **
       
107502 ** If there is not enough free space in pNode, return SQLITE_FULL.
       
107503 */
       
107504 static int
       
107505 nodeInsertCell(
       
107506   Rtree *pRtree, 
       
107507   RtreeNode *pNode, 
       
107508   RtreeCell *pCell 
       
107509 ){
       
107510   int nCell;                    /* Current number of cells in pNode */
       
107511   int nMaxCell;                 /* Maximum number of cells for pNode */
       
107512 
       
107513   nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
       
107514   nCell = NCELL(pNode);
       
107515 
       
107516   assert(nCell<=nMaxCell);
       
107517 
       
107518   if( nCell<nMaxCell ){
       
107519     nodeOverwriteCell(pRtree, pNode, pCell, nCell);
       
107520     writeInt16(&pNode->zData[2], nCell+1);
       
107521     pNode->isDirty = 1;
       
107522   }
       
107523 
       
107524   return (nCell==nMaxCell);
       
107525 }
       
107526 
       
107527 /*
       
107528 ** If the node is dirty, write it out to the database.
       
107529 */
       
107530 static int
       
107531 nodeWrite(Rtree *pRtree, RtreeNode *pNode){
       
107532   int rc = SQLITE_OK;
       
107533   if( pNode->isDirty ){
       
107534     sqlite3_stmt *p = pRtree->pWriteNode;
       
107535     if( pNode->iNode ){
       
107536       sqlite3_bind_int64(p, 1, pNode->iNode);
       
107537     }else{
       
107538       sqlite3_bind_null(p, 1);
       
107539     }
       
107540     sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
       
107541     sqlite3_step(p);
       
107542     pNode->isDirty = 0;
       
107543     rc = sqlite3_reset(p);
       
107544     if( pNode->iNode==0 && rc==SQLITE_OK ){
       
107545       pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
       
107546       nodeHashInsert(pRtree, pNode);
       
107547     }
       
107548   }
       
107549   return rc;
       
107550 }
       
107551 
       
107552 /*
       
107553 ** Release a reference to a node. If the node is dirty and the reference
       
107554 ** count drops to zero, the node data is written to the database.
       
107555 */
       
107556 static int
       
107557 nodeRelease(Rtree *pRtree, RtreeNode *pNode){
       
107558   int rc = SQLITE_OK;
       
107559   if( pNode ){
       
107560     assert( pNode->nRef>0 );
       
107561     pNode->nRef--;
       
107562     if( pNode->nRef==0 ){
       
107563       if( pNode->iNode==1 ){
       
107564         pRtree->iDepth = -1;
       
107565       }
       
107566       if( pNode->pParent ){
       
107567         rc = nodeRelease(pRtree, pNode->pParent);
       
107568       }
       
107569       if( rc==SQLITE_OK ){
       
107570         rc = nodeWrite(pRtree, pNode);
       
107571       }
       
107572       nodeHashDelete(pRtree, pNode);
       
107573       sqlite3_free(pNode);
       
107574     }
       
107575   }
       
107576   return rc;
       
107577 }
       
107578 
       
107579 /*
       
107580 ** Return the 64-bit integer value associated with cell iCell of
       
107581 ** node pNode. If pNode is a leaf node, this is a rowid. If it is
       
107582 ** an internal node, then the 64-bit integer is a child page number.
       
107583 */
       
107584 static i64 nodeGetRowid(
       
107585   Rtree *pRtree, 
       
107586   RtreeNode *pNode, 
       
107587   int iCell
       
107588 ){
       
107589   assert( iCell<NCELL(pNode) );
       
107590   return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
       
107591 }
       
107592 
       
107593 /*
       
107594 ** Return coordinate iCoord from cell iCell in node pNode.
       
107595 */
       
107596 static void nodeGetCoord(
       
107597   Rtree *pRtree, 
       
107598   RtreeNode *pNode, 
       
107599   int iCell,
       
107600   int iCoord,
       
107601   RtreeCoord *pCoord           /* Space to write result to */
       
107602 ){
       
107603   readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
       
107604 }
       
107605 
       
107606 /*
       
107607 ** Deserialize cell iCell of node pNode. Populate the structure pointed
       
107608 ** to by pCell with the results.
       
107609 */
       
107610 static void nodeGetCell(
       
107611   Rtree *pRtree, 
       
107612   RtreeNode *pNode, 
       
107613   int iCell,
       
107614   RtreeCell *pCell
       
107615 ){
       
107616   int ii;
       
107617   pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
       
107618   for(ii=0; ii<pRtree->nDim*2; ii++){
       
107619     nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]);
       
107620   }
       
107621 }
       
107622 
       
107623 
       
107624 /* Forward declaration for the function that does the work of
       
107625 ** the virtual table module xCreate() and xConnect() methods.
       
107626 */
       
107627 static int rtreeInit(
       
107628   sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
       
107629 );
       
107630 
       
107631 /* 
       
107632 ** Rtree virtual table module xCreate method.
       
107633 */
       
107634 static int rtreeCreate(
       
107635   sqlite3 *db,
       
107636   void *pAux,
       
107637   int argc, const char *const*argv,
       
107638   sqlite3_vtab **ppVtab,
       
107639   char **pzErr
       
107640 ){
       
107641   return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
       
107642 }
       
107643 
       
107644 /* 
       
107645 ** Rtree virtual table module xConnect method.
       
107646 */
       
107647 static int rtreeConnect(
       
107648   sqlite3 *db,
       
107649   void *pAux,
       
107650   int argc, const char *const*argv,
       
107651   sqlite3_vtab **ppVtab,
       
107652   char **pzErr
       
107653 ){
       
107654   return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
       
107655 }
       
107656 
       
107657 /*
       
107658 ** Increment the r-tree reference count.
       
107659 */
       
107660 static void rtreeReference(Rtree *pRtree){
       
107661   pRtree->nBusy++;
       
107662 }
       
107663 
       
107664 /*
       
107665 ** Decrement the r-tree reference count. When the reference count reaches
       
107666 ** zero the structure is deleted.
       
107667 */
       
107668 static void rtreeRelease(Rtree *pRtree){
       
107669   pRtree->nBusy--;
       
107670   if( pRtree->nBusy==0 ){
       
107671     sqlite3_finalize(pRtree->pReadNode);
       
107672     sqlite3_finalize(pRtree->pWriteNode);
       
107673     sqlite3_finalize(pRtree->pDeleteNode);
       
107674     sqlite3_finalize(pRtree->pReadRowid);
       
107675     sqlite3_finalize(pRtree->pWriteRowid);
       
107676     sqlite3_finalize(pRtree->pDeleteRowid);
       
107677     sqlite3_finalize(pRtree->pReadParent);
       
107678     sqlite3_finalize(pRtree->pWriteParent);
       
107679     sqlite3_finalize(pRtree->pDeleteParent);
       
107680     sqlite3_free(pRtree);
       
107681   }
       
107682 }
       
107683 
       
107684 /* 
       
107685 ** Rtree virtual table module xDisconnect method.
       
107686 */
       
107687 static int rtreeDisconnect(sqlite3_vtab *pVtab){
       
107688   rtreeRelease((Rtree *)pVtab);
       
107689   return SQLITE_OK;
       
107690 }
       
107691 
       
107692 /* 
       
107693 ** Rtree virtual table module xDestroy method.
       
107694 */
       
107695 static int rtreeDestroy(sqlite3_vtab *pVtab){
       
107696   Rtree *pRtree = (Rtree *)pVtab;
       
107697   int rc;
       
107698   char *zCreate = sqlite3_mprintf(
       
107699     "DROP TABLE '%q'.'%q_node';"
       
107700     "DROP TABLE '%q'.'%q_rowid';"
       
107701     "DROP TABLE '%q'.'%q_parent';",
       
107702     pRtree->zDb, pRtree->zName, 
       
107703     pRtree->zDb, pRtree->zName,
       
107704     pRtree->zDb, pRtree->zName
       
107705   );
       
107706   if( !zCreate ){
       
107707     rc = SQLITE_NOMEM;
       
107708   }else{
       
107709     rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
       
107710     sqlite3_free(zCreate);
       
107711   }
       
107712   if( rc==SQLITE_OK ){
       
107713     rtreeRelease(pRtree);
       
107714   }
       
107715 
       
107716   return rc;
       
107717 }
       
107718 
       
107719 /* 
       
107720 ** Rtree virtual table module xOpen method.
       
107721 */
       
107722 static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
       
107723   int rc = SQLITE_NOMEM;
       
107724   RtreeCursor *pCsr;
       
107725 
       
107726   pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
       
107727   if( pCsr ){
       
107728     memset(pCsr, 0, sizeof(RtreeCursor));
       
107729     pCsr->base.pVtab = pVTab;
       
107730     rc = SQLITE_OK;
       
107731   }
       
107732   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
       
107733 
       
107734   return rc;
       
107735 }
       
107736 
       
107737 /* 
       
107738 ** Rtree virtual table module xClose method.
       
107739 */
       
107740 static int rtreeClose(sqlite3_vtab_cursor *cur){
       
107741   Rtree *pRtree = (Rtree *)(cur->pVtab);
       
107742   int rc;
       
107743   RtreeCursor *pCsr = (RtreeCursor *)cur;
       
107744   sqlite3_free(pCsr->aConstraint);
       
107745   rc = nodeRelease(pRtree, pCsr->pNode);
       
107746   sqlite3_free(pCsr);
       
107747   return rc;
       
107748 }
       
107749 
       
107750 /*
       
107751 ** Rtree virtual table module xEof method.
       
107752 **
       
107753 ** Return non-zero if the cursor does not currently point to a valid 
       
107754 ** record (i.e if the scan has finished), or zero otherwise.
       
107755 */
       
107756 static int rtreeEof(sqlite3_vtab_cursor *cur){
       
107757   RtreeCursor *pCsr = (RtreeCursor *)cur;
       
107758   return (pCsr->pNode==0);
       
107759 }
       
107760 
       
107761 /* 
       
107762 ** Cursor pCursor currently points to a cell in a non-leaf page.
       
107763 ** Return true if the sub-tree headed by the cell is filtered
       
107764 ** (excluded) by the constraints in the pCursor->aConstraint[] 
       
107765 ** array, or false otherwise.
       
107766 */
       
107767 static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor){
       
107768   RtreeCell cell;
       
107769   int ii;
       
107770   int bRes = 0;
       
107771 
       
107772   nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
       
107773   for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
       
107774     RtreeConstraint *p = &pCursor->aConstraint[ii];
       
107775     double cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
       
107776     double cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
       
107777 
       
107778     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
       
107779         || p->op==RTREE_GT || p->op==RTREE_EQ
       
107780     );
       
107781 
       
107782     switch( p->op ){
       
107783       case RTREE_LE: case RTREE_LT: bRes = p->rValue<cell_min; break;
       
107784       case RTREE_GE: case RTREE_GT: bRes = p->rValue>cell_max; break;
       
107785       case RTREE_EQ: 
       
107786         bRes = (p->rValue>cell_max || p->rValue<cell_min);
       
107787         break;
       
107788     }
       
107789   }
       
107790 
       
107791   return bRes;
       
107792 }
       
107793 
       
107794 /* 
       
107795 ** Return true if the cell that cursor pCursor currently points to
       
107796 ** would be filtered (excluded) by the constraints in the 
       
107797 ** pCursor->aConstraint[] array, or false otherwise.
       
107798 **
       
107799 ** This function assumes that the cell is part of a leaf node.
       
107800 */
       
107801 static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor){
       
107802   RtreeCell cell;
       
107803   int ii;
       
107804 
       
107805   nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
       
107806   for(ii=0; ii<pCursor->nConstraint; ii++){
       
107807     RtreeConstraint *p = &pCursor->aConstraint[ii];
       
107808     double coord = DCOORD(cell.aCoord[p->iCoord]);
       
107809     int res;
       
107810     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
       
107811         || p->op==RTREE_GT || p->op==RTREE_EQ
       
107812     );
       
107813     switch( p->op ){
       
107814       case RTREE_LE: res = (coord<=p->rValue); break;
       
107815       case RTREE_LT: res = (coord<p->rValue);  break;
       
107816       case RTREE_GE: res = (coord>=p->rValue); break;
       
107817       case RTREE_GT: res = (coord>p->rValue);  break;
       
107818       case RTREE_EQ: res = (coord==p->rValue); break;
       
107819     }
       
107820 
       
107821     if( !res ) return 1;
       
107822   }
       
107823 
       
107824   return 0;
       
107825 }
       
107826 
       
107827 /*
       
107828 ** Cursor pCursor currently points at a node that heads a sub-tree of
       
107829 ** height iHeight (if iHeight==0, then the node is a leaf). Descend
       
107830 ** to point to the left-most cell of the sub-tree that matches the 
       
107831 ** configured constraints.
       
107832 */
       
107833 static int descendToCell(
       
107834   Rtree *pRtree, 
       
107835   RtreeCursor *pCursor, 
       
107836   int iHeight,
       
107837   int *pEof                 /* OUT: Set to true if cannot descend */
       
107838 ){
       
107839   int isEof;
       
107840   int rc;
       
107841   int ii;
       
107842   RtreeNode *pChild;
       
107843   sqlite3_int64 iRowid;
       
107844 
       
107845   RtreeNode *pSavedNode = pCursor->pNode;
       
107846   int iSavedCell = pCursor->iCell;
       
107847 
       
107848   assert( iHeight>=0 );
       
107849 
       
107850   if( iHeight==0 ){
       
107851     isEof = testRtreeEntry(pRtree, pCursor);
       
107852   }else{
       
107853     isEof = testRtreeCell(pRtree, pCursor);
       
107854   }
       
107855   if( isEof || iHeight==0 ){
       
107856     *pEof = isEof;
       
107857     return SQLITE_OK;
       
107858   }
       
107859 
       
107860   iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell);
       
107861   rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild);
       
107862   if( rc!=SQLITE_OK ){
       
107863     return rc;
       
107864   }
       
107865 
       
107866   nodeRelease(pRtree, pCursor->pNode);
       
107867   pCursor->pNode = pChild;
       
107868   isEof = 1;
       
107869   for(ii=0; isEof && ii<NCELL(pChild); ii++){
       
107870     pCursor->iCell = ii;
       
107871     rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof);
       
107872     if( rc!=SQLITE_OK ){
       
107873       return rc;
       
107874     }
       
107875   }
       
107876 
       
107877   if( isEof ){
       
107878     assert( pCursor->pNode==pChild );
       
107879     nodeReference(pSavedNode);
       
107880     nodeRelease(pRtree, pChild);
       
107881     pCursor->pNode = pSavedNode;
       
107882     pCursor->iCell = iSavedCell;
       
107883   }
       
107884 
       
107885   *pEof = isEof;
       
107886   return SQLITE_OK;
       
107887 }
       
107888 
       
107889 /*
       
107890 ** One of the cells in node pNode is guaranteed to have a 64-bit 
       
107891 ** integer value equal to iRowid. Return the index of this cell.
       
107892 */
       
107893 static int nodeRowidIndex(Rtree *pRtree, RtreeNode *pNode, i64 iRowid){
       
107894   int ii;
       
107895   for(ii=0; nodeGetRowid(pRtree, pNode, ii)!=iRowid; ii++){
       
107896     assert( ii<(NCELL(pNode)-1) );
       
107897   }
       
107898   return ii;
       
107899 }
       
107900 
       
107901 /*
       
107902 ** Return the index of the cell containing a pointer to node pNode
       
107903 ** in its parent. If pNode is the root node, return -1.
       
107904 */
       
107905 static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode){
       
107906   RtreeNode *pParent = pNode->pParent;
       
107907   if( pParent ){
       
107908     return nodeRowidIndex(pRtree, pParent, pNode->iNode);
       
107909   }
       
107910   return -1;
       
107911 }
       
107912 
       
107913 /* 
       
107914 ** Rtree virtual table module xNext method.
       
107915 */
       
107916 static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
       
107917   Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab);
       
107918   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
       
107919   int rc = SQLITE_OK;
       
107920 
       
107921   if( pCsr->iStrategy==1 ){
       
107922     /* This "scan" is a direct lookup by rowid. There is no next entry. */
       
107923     nodeRelease(pRtree, pCsr->pNode);
       
107924     pCsr->pNode = 0;
       
107925   }
       
107926 
       
107927   else if( pCsr->pNode ){
       
107928     /* Move to the next entry that matches the configured constraints. */
       
107929     int iHeight = 0;
       
107930     while( pCsr->pNode ){
       
107931       RtreeNode *pNode = pCsr->pNode;
       
107932       int nCell = NCELL(pNode);
       
107933       for(pCsr->iCell++; pCsr->iCell<nCell; pCsr->iCell++){
       
107934         int isEof;
       
107935         rc = descendToCell(pRtree, pCsr, iHeight, &isEof);
       
107936         if( rc!=SQLITE_OK || !isEof ){
       
107937           return rc;
       
107938         }
       
107939       }
       
107940       pCsr->pNode = pNode->pParent;
       
107941       pCsr->iCell = nodeParentIndex(pRtree, pNode);
       
107942       nodeReference(pCsr->pNode);
       
107943       nodeRelease(pRtree, pNode);
       
107944       iHeight++;
       
107945     }
       
107946   }
       
107947 
       
107948   return rc;
       
107949 }
       
107950 
       
107951 /* 
       
107952 ** Rtree virtual table module xRowid method.
       
107953 */
       
107954 static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
       
107955   Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
       
107956   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
       
107957 
       
107958   assert(pCsr->pNode);
       
107959   *pRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
       
107960 
       
107961   return SQLITE_OK;
       
107962 }
       
107963 
       
107964 /* 
       
107965 ** Rtree virtual table module xColumn method.
       
107966 */
       
107967 static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
       
107968   Rtree *pRtree = (Rtree *)cur->pVtab;
       
107969   RtreeCursor *pCsr = (RtreeCursor *)cur;
       
107970 
       
107971   if( i==0 ){
       
107972     i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
       
107973     sqlite3_result_int64(ctx, iRowid);
       
107974   }else{
       
107975     RtreeCoord c;
       
107976     nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
       
107977     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
       
107978       sqlite3_result_double(ctx, c.f);
       
107979     }else{
       
107980       assert( pRtree->eCoordType==RTREE_COORD_INT32 );
       
107981       sqlite3_result_int(ctx, c.i);
       
107982     }
       
107983   }
       
107984 
       
107985   return SQLITE_OK;
       
107986 }
       
107987 
       
107988 /* 
       
107989 ** Use nodeAcquire() to obtain the leaf node containing the record with 
       
107990 ** rowid iRowid. If successful, set *ppLeaf to point to the node and
       
107991 ** return SQLITE_OK. If there is no such record in the table, set
       
107992 ** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
       
107993 ** to zero and return an SQLite error code.
       
107994 */
       
107995 static int findLeafNode(Rtree *pRtree, i64 iRowid, RtreeNode **ppLeaf){
       
107996   int rc;
       
107997   *ppLeaf = 0;
       
107998   sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
       
107999   if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
       
108000     i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
       
108001     rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
       
108002     sqlite3_reset(pRtree->pReadRowid);
       
108003   }else{
       
108004     rc = sqlite3_reset(pRtree->pReadRowid);
       
108005   }
       
108006   return rc;
       
108007 }
       
108008 
       
108009 
       
108010 /* 
       
108011 ** Rtree virtual table module xFilter method.
       
108012 */
       
108013 static int rtreeFilter(
       
108014   sqlite3_vtab_cursor *pVtabCursor, 
       
108015   int idxNum, const char *idxStr,
       
108016   int argc, sqlite3_value **argv
       
108017 ){
       
108018   Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
       
108019   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
       
108020 
       
108021   RtreeNode *pRoot = 0;
       
108022   int ii;
       
108023   int rc = SQLITE_OK;
       
108024 
       
108025   rtreeReference(pRtree);
       
108026 
       
108027   sqlite3_free(pCsr->aConstraint);
       
108028   pCsr->aConstraint = 0;
       
108029   pCsr->iStrategy = idxNum;
       
108030 
       
108031   if( idxNum==1 ){
       
108032     /* Special case - lookup by rowid. */
       
108033     RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
       
108034     i64 iRowid = sqlite3_value_int64(argv[0]);
       
108035     rc = findLeafNode(pRtree, iRowid, &pLeaf);
       
108036     pCsr->pNode = pLeaf; 
       
108037     if( pLeaf && rc==SQLITE_OK ){
       
108038       pCsr->iCell = nodeRowidIndex(pRtree, pLeaf, iRowid);
       
108039     }
       
108040   }else{
       
108041     /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array 
       
108042     ** with the configured constraints. 
       
108043     */
       
108044     if( argc>0 ){
       
108045       pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
       
108046       pCsr->nConstraint = argc;
       
108047       if( !pCsr->aConstraint ){
       
108048         rc = SQLITE_NOMEM;
       
108049       }else{
       
108050         assert( (idxStr==0 && argc==0) || strlen(idxStr)==argc*2 );
       
108051         for(ii=0; ii<argc; ii++){
       
108052           RtreeConstraint *p = &pCsr->aConstraint[ii];
       
108053           p->op = idxStr[ii*2];
       
108054           p->iCoord = idxStr[ii*2+1]-'a';
       
108055           p->rValue = sqlite3_value_double(argv[ii]);
       
108056         }
       
108057       }
       
108058     }
       
108059   
       
108060     if( rc==SQLITE_OK ){
       
108061       pCsr->pNode = 0;
       
108062       rc = nodeAcquire(pRtree, 1, 0, &pRoot);
       
108063     }
       
108064     if( rc==SQLITE_OK ){
       
108065       int isEof = 1;
       
108066       int nCell = NCELL(pRoot);
       
108067       pCsr->pNode = pRoot;
       
108068       for(pCsr->iCell=0; rc==SQLITE_OK && pCsr->iCell<nCell; pCsr->iCell++){
       
108069         assert( pCsr->pNode==pRoot );
       
108070         rc = descendToCell(pRtree, pCsr, pRtree->iDepth, &isEof);
       
108071         if( !isEof ){
       
108072           break;
       
108073         }
       
108074       }
       
108075       if( rc==SQLITE_OK && isEof ){
       
108076         assert( pCsr->pNode==pRoot );
       
108077         nodeRelease(pRtree, pRoot);
       
108078         pCsr->pNode = 0;
       
108079       }
       
108080       assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCell<NCELL(pCsr->pNode) );
       
108081     }
       
108082   }
       
108083 
       
108084   rtreeRelease(pRtree);
       
108085   return rc;
       
108086 }
       
108087 
       
108088 /*
       
108089 ** Rtree virtual table module xBestIndex method. There are three
       
108090 ** table scan strategies to choose from (in order from most to 
       
108091 ** least desirable):
       
108092 **
       
108093 **   idxNum     idxStr        Strategy
       
108094 **   ------------------------------------------------
       
108095 **     1        Unused        Direct lookup by rowid.
       
108096 **     2        See below     R-tree query.
       
108097 **     3        Unused        Full table scan.
       
108098 **   ------------------------------------------------
       
108099 **
       
108100 ** If strategy 1 or 3 is used, then idxStr is not meaningful. If strategy
       
108101 ** 2 is used, idxStr is formatted to contain 2 bytes for each 
       
108102 ** constraint used. The first two bytes of idxStr correspond to 
       
108103 ** the constraint in sqlite3_index_info.aConstraintUsage[] with
       
108104 ** (argvIndex==1) etc.
       
108105 **
       
108106 ** The first of each pair of bytes in idxStr identifies the constraint
       
108107 ** operator as follows:
       
108108 **
       
108109 **   Operator    Byte Value
       
108110 **   ----------------------
       
108111 **      =        0x41 ('A')
       
108112 **     <=        0x42 ('B')
       
108113 **      <        0x43 ('C')
       
108114 **     >=        0x44 ('D')
       
108115 **      >        0x45 ('E')
       
108116 **   ----------------------
       
108117 **
       
108118 ** The second of each pair of bytes identifies the coordinate column
       
108119 ** to which the constraint applies. The leftmost coordinate column
       
108120 ** is 'a', the second from the left 'b' etc.
       
108121 */
       
108122 static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
       
108123   int rc = SQLITE_OK;
       
108124   int ii, cCol;
       
108125 
       
108126   int iIdx = 0;
       
108127   char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
       
108128   memset(zIdxStr, 0, sizeof(zIdxStr));
       
108129 
       
108130   assert( pIdxInfo->idxStr==0 );
       
108131   for(ii=0; ii<pIdxInfo->nConstraint; ii++){
       
108132     struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
       
108133 
       
108134     if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
       
108135       /* We have an equality constraint on the rowid. Use strategy 1. */
       
108136       int jj;
       
108137       for(jj=0; jj<ii; jj++){
       
108138         pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
       
108139         pIdxInfo->aConstraintUsage[jj].omit = 0;
       
108140       }
       
108141       pIdxInfo->idxNum = 1;
       
108142       pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
       
108143       pIdxInfo->aConstraintUsage[jj].omit = 1;
       
108144 
       
108145       /* This strategy involves a two rowid lookups on an B-Tree structures
       
108146       ** and then a linear search of an R-Tree node. This should be 
       
108147       ** considered almost as quick as a direct rowid lookup (for which 
       
108148       ** sqlite uses an internal cost of 0.0).
       
108149       */ 
       
108150       pIdxInfo->estimatedCost = 10.0;
       
108151       return SQLITE_OK;
       
108152     }
       
108153 
       
108154     if( p->usable && p->iColumn>0 ){
       
108155       u8 op = 0;
       
108156       switch( p->op ){
       
108157         case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
       
108158         case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
       
108159         case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
       
108160         case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
       
108161         case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
       
108162       }
       
108163       if( op ){
       
108164         /* Make sure this particular constraint has not been used before.
       
108165         ** If it has been used before, ignore it.
       
108166         **
       
108167         ** A <= or < can be used if there is a prior >= or >.
       
108168         ** A >= or > can be used if there is a prior < or <=.
       
108169         ** A <= or < is disqualified if there is a prior <=, <, or ==.
       
108170         ** A >= or > is disqualified if there is a prior >=, >, or ==.
       
108171         ** A == is disqualifed if there is any prior constraint.
       
108172         */
       
108173         int j, opmsk;
       
108174         static const unsigned char compatible[] = { 0, 0, 1, 1, 2, 2 };
       
108175         assert( compatible[RTREE_EQ & 7]==0 );
       
108176         assert( compatible[RTREE_LT & 7]==1 );
       
108177         assert( compatible[RTREE_LE & 7]==1 );
       
108178         assert( compatible[RTREE_GT & 7]==2 );
       
108179         assert( compatible[RTREE_GE & 7]==2 );
       
108180         cCol = p->iColumn - 1 + 'a';
       
108181         opmsk = compatible[op & 7];
       
108182         for(j=0; j<iIdx; j+=2){
       
108183           if( zIdxStr[j+1]==cCol && (compatible[zIdxStr[j] & 7] & opmsk)!=0 ){
       
108184             op = 0;
       
108185             break;
       
108186           }
       
108187         }
       
108188       }
       
108189       if( op ){
       
108190         assert( iIdx<sizeof(zIdxStr)-1 );
       
108191         zIdxStr[iIdx++] = op;
       
108192         zIdxStr[iIdx++] = cCol;
       
108193         pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
       
108194         pIdxInfo->aConstraintUsage[ii].omit = 1;
       
108195       }
       
108196     }
       
108197   }
       
108198 
       
108199   pIdxInfo->idxNum = 2;
       
108200   pIdxInfo->needToFreeIdxStr = 1;
       
108201   if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
       
108202     return SQLITE_NOMEM;
       
108203   }
       
108204   assert( iIdx>=0 );
       
108205   pIdxInfo->estimatedCost = (2000000.0 / (double)(iIdx + 1));
       
108206   return rc;
       
108207 }
       
108208 
       
108209 /*
       
108210 ** Return the N-dimensional volumn of the cell stored in *p.
       
108211 */
       
108212 static float cellArea(Rtree *pRtree, RtreeCell *p){
       
108213   float area = 1.0;
       
108214   int ii;
       
108215   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       
108216     area = area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
       
108217   }
       
108218   return area;
       
108219 }
       
108220 
       
108221 /*
       
108222 ** Return the margin length of cell p. The margin length is the sum
       
108223 ** of the objects size in each dimension.
       
108224 */
       
108225 static float cellMargin(Rtree *pRtree, RtreeCell *p){
       
108226   float margin = 0.0;
       
108227   int ii;
       
108228   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       
108229     margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
       
108230   }
       
108231   return margin;
       
108232 }
       
108233 
       
108234 /*
       
108235 ** Store the union of cells p1 and p2 in p1.
       
108236 */
       
108237 static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
       
108238   int ii;
       
108239   if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
       
108240     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       
108241       p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
       
108242       p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
       
108243     }
       
108244   }else{
       
108245     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       
108246       p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
       
108247       p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
       
108248     }
       
108249   }
       
108250 }
       
108251 
       
108252 /*
       
108253 ** Return true if the area covered by p2 is a subset of the area covered
       
108254 ** by p1. False otherwise.
       
108255 */
       
108256 static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
       
108257   int ii;
       
108258   int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
       
108259   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       
108260     RtreeCoord *a1 = &p1->aCoord[ii];
       
108261     RtreeCoord *a2 = &p2->aCoord[ii];
       
108262     if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f)) 
       
108263      || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i)) 
       
108264     ){
       
108265       return 0;
       
108266     }
       
108267   }
       
108268   return 1;
       
108269 }
       
108270 
       
108271 /*
       
108272 ** Return the amount cell p would grow by if it were unioned with pCell.
       
108273 */
       
108274 static float cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
       
108275   float area;
       
108276   RtreeCell cell;
       
108277   memcpy(&cell, p, sizeof(RtreeCell));
       
108278   area = cellArea(pRtree, &cell);
       
108279   cellUnion(pRtree, &cell, pCell);
       
108280   return (cellArea(pRtree, &cell)-area);
       
108281 }
       
108282 
       
108283 #if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
       
108284 static float cellOverlap(
       
108285   Rtree *pRtree, 
       
108286   RtreeCell *p, 
       
108287   RtreeCell *aCell, 
       
108288   int nCell, 
       
108289   int iExclude
       
108290 ){
       
108291   int ii;
       
108292   float overlap = 0.0;
       
108293   for(ii=0; ii<nCell; ii++){
       
108294     if( ii!=iExclude ){
       
108295       int jj;
       
108296       float o = 1.0;
       
108297       for(jj=0; jj<(pRtree->nDim*2); jj+=2){
       
108298         double x1;
       
108299         double x2;
       
108300 
       
108301         x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
       
108302         x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
       
108303 
       
108304         if( x2<x1 ){
       
108305           o = 0.0;
       
108306           break;
       
108307         }else{
       
108308           o = o * (x2-x1);
       
108309         }
       
108310       }
       
108311       overlap += o;
       
108312     }
       
108313   }
       
108314   return overlap;
       
108315 }
       
108316 #endif
       
108317 
       
108318 #if VARIANT_RSTARTREE_CHOOSESUBTREE
       
108319 static float cellOverlapEnlargement(
       
108320   Rtree *pRtree, 
       
108321   RtreeCell *p, 
       
108322   RtreeCell *pInsert, 
       
108323   RtreeCell *aCell, 
       
108324   int nCell, 
       
108325   int iExclude
       
108326 ){
       
108327   float before;
       
108328   float after;
       
108329   before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
       
108330   cellUnion(pRtree, p, pInsert);
       
108331   after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
       
108332   return after-before;
       
108333 }
       
108334 #endif
       
108335 
       
108336 
       
108337 /*
       
108338 ** This function implements the ChooseLeaf algorithm from Gutman[84].
       
108339 ** ChooseSubTree in r*tree terminology.
       
108340 */
       
108341 static int ChooseLeaf(
       
108342   Rtree *pRtree,               /* Rtree table */
       
108343   RtreeCell *pCell,            /* Cell to insert into rtree */
       
108344   int iHeight,                 /* Height of sub-tree rooted at pCell */
       
108345   RtreeNode **ppLeaf           /* OUT: Selected leaf page */
       
108346 ){
       
108347   int rc;
       
108348   int ii;
       
108349   RtreeNode *pNode;
       
108350   rc = nodeAcquire(pRtree, 1, 0, &pNode);
       
108351 
       
108352   for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
       
108353     int iCell;
       
108354     sqlite3_int64 iBest;
       
108355 
       
108356     float fMinGrowth;
       
108357     float fMinArea;
       
108358     float fMinOverlap;
       
108359 
       
108360     int nCell = NCELL(pNode);
       
108361     RtreeCell cell;
       
108362     RtreeNode *pChild;
       
108363 
       
108364     RtreeCell *aCell = 0;
       
108365 
       
108366 #if VARIANT_RSTARTREE_CHOOSESUBTREE
       
108367     if( ii==(pRtree->iDepth-1) ){
       
108368       int jj;
       
108369       aCell = sqlite3_malloc(sizeof(RtreeCell)*nCell);
       
108370       if( !aCell ){
       
108371         rc = SQLITE_NOMEM;
       
108372         nodeRelease(pRtree, pNode);
       
108373         pNode = 0;
       
108374         continue;
       
108375       }
       
108376       for(jj=0; jj<nCell; jj++){
       
108377         nodeGetCell(pRtree, pNode, jj, &aCell[jj]);
       
108378       }
       
108379     }
       
108380 #endif
       
108381 
       
108382     /* Select the child node which will be enlarged the least if pCell
       
108383     ** is inserted into it. Resolve ties by choosing the entry with
       
108384     ** the smallest area.
       
108385     */
       
108386     for(iCell=0; iCell<nCell; iCell++){
       
108387       float growth;
       
108388       float area;
       
108389       float overlap = 0.0;
       
108390       nodeGetCell(pRtree, pNode, iCell, &cell);
       
108391       growth = cellGrowth(pRtree, &cell, pCell);
       
108392       area = cellArea(pRtree, &cell);
       
108393 #if VARIANT_RSTARTREE_CHOOSESUBTREE
       
108394       if( ii==(pRtree->iDepth-1) ){
       
108395         overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
       
108396       }
       
108397 #endif
       
108398       if( (iCell==0) 
       
108399        || (overlap<fMinOverlap) 
       
108400        || (overlap==fMinOverlap && growth<fMinGrowth)
       
108401        || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
       
108402       ){
       
108403         fMinOverlap = overlap;
       
108404         fMinGrowth = growth;
       
108405         fMinArea = area;
       
108406         iBest = cell.iRowid;
       
108407       }
       
108408     }
       
108409 
       
108410     sqlite3_free(aCell);
       
108411     rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
       
108412     nodeRelease(pRtree, pNode);
       
108413     pNode = pChild;
       
108414   }
       
108415 
       
108416   *ppLeaf = pNode;
       
108417   return rc;
       
108418 }
       
108419 
       
108420 /*
       
108421 ** A cell with the same content as pCell has just been inserted into
       
108422 ** the node pNode. This function updates the bounding box cells in
       
108423 ** all ancestor elements.
       
108424 */
       
108425 static void AdjustTree(
       
108426   Rtree *pRtree,                    /* Rtree table */
       
108427   RtreeNode *pNode,                 /* Adjust ancestry of this node. */
       
108428   RtreeCell *pCell                  /* This cell was just inserted */
       
108429 ){
       
108430   RtreeNode *p = pNode;
       
108431   while( p->pParent ){
       
108432     RtreeCell cell;
       
108433     RtreeNode *pParent = p->pParent;
       
108434     int iCell = nodeParentIndex(pRtree, p);
       
108435 
       
108436     nodeGetCell(pRtree, pParent, iCell, &cell);
       
108437     if( !cellContains(pRtree, &cell, pCell) ){
       
108438       cellUnion(pRtree, &cell, pCell);
       
108439       nodeOverwriteCell(pRtree, pParent, &cell, iCell);
       
108440     }
       
108441  
       
108442     p = pParent;
       
108443   }
       
108444 }
       
108445 
       
108446 /*
       
108447 ** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
       
108448 */
       
108449 static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
       
108450   sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
       
108451   sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
       
108452   sqlite3_step(pRtree->pWriteRowid);
       
108453   return sqlite3_reset(pRtree->pWriteRowid);
       
108454 }
       
108455 
       
108456 /*
       
108457 ** Write mapping (iNode->iPar) to the <rtree>_parent table.
       
108458 */
       
108459 static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
       
108460   sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
       
108461   sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
       
108462   sqlite3_step(pRtree->pWriteParent);
       
108463   return sqlite3_reset(pRtree->pWriteParent);
       
108464 }
       
108465 
       
108466 static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
       
108467 
       
108468 #if VARIANT_GUTTMAN_LINEAR_SPLIT
       
108469 /*
       
108470 ** Implementation of the linear variant of the PickNext() function from
       
108471 ** Guttman[84].
       
108472 */
       
108473 static RtreeCell *LinearPickNext(
       
108474   Rtree *pRtree,
       
108475   RtreeCell *aCell, 
       
108476   int nCell, 
       
108477   RtreeCell *pLeftBox, 
       
108478   RtreeCell *pRightBox,
       
108479   int *aiUsed
       
108480 ){
       
108481   int ii;
       
108482   for(ii=0; aiUsed[ii]; ii++);
       
108483   aiUsed[ii] = 1;
       
108484   return &aCell[ii];
       
108485 }
       
108486 
       
108487 /*
       
108488 ** Implementation of the linear variant of the PickSeeds() function from
       
108489 ** Guttman[84].
       
108490 */
       
108491 static void LinearPickSeeds(
       
108492   Rtree *pRtree,
       
108493   RtreeCell *aCell, 
       
108494   int nCell, 
       
108495   int *piLeftSeed, 
       
108496   int *piRightSeed
       
108497 ){
       
108498   int i;
       
108499   int iLeftSeed = 0;
       
108500   int iRightSeed = 1;
       
108501   float maxNormalInnerWidth = 0.0;
       
108502 
       
108503   /* Pick two "seed" cells from the array of cells. The algorithm used
       
108504   ** here is the LinearPickSeeds algorithm from Gutman[1984]. The 
       
108505   ** indices of the two seed cells in the array are stored in local
       
108506   ** variables iLeftSeek and iRightSeed.
       
108507   */
       
108508   for(i=0; i<pRtree->nDim; i++){
       
108509     float x1 = DCOORD(aCell[0].aCoord[i*2]);
       
108510     float x2 = DCOORD(aCell[0].aCoord[i*2+1]);
       
108511     float x3 = x1;
       
108512     float x4 = x2;
       
108513     int jj;
       
108514 
       
108515     int iCellLeft = 0;
       
108516     int iCellRight = 0;
       
108517 
       
108518     for(jj=1; jj<nCell; jj++){
       
108519       float left = DCOORD(aCell[jj].aCoord[i*2]);
       
108520       float right = DCOORD(aCell[jj].aCoord[i*2+1]);
       
108521 
       
108522       if( left<x1 ) x1 = left;
       
108523       if( right>x4 ) x4 = right;
       
108524       if( left>x3 ){
       
108525         x3 = left;
       
108526         iCellRight = jj;
       
108527       }
       
108528       if( right<x2 ){
       
108529         x2 = right;
       
108530         iCellLeft = jj;
       
108531       }
       
108532     }
       
108533 
       
108534     if( x4!=x1 ){
       
108535       float normalwidth = (x3 - x2) / (x4 - x1);
       
108536       if( normalwidth>maxNormalInnerWidth ){
       
108537         iLeftSeed = iCellLeft;
       
108538         iRightSeed = iCellRight;
       
108539       }
       
108540     }
       
108541   }
       
108542 
       
108543   *piLeftSeed = iLeftSeed;
       
108544   *piRightSeed = iRightSeed;
       
108545 }
       
108546 #endif /* VARIANT_GUTTMAN_LINEAR_SPLIT */
       
108547 
       
108548 #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
       
108549 /*
       
108550 ** Implementation of the quadratic variant of the PickNext() function from
       
108551 ** Guttman[84].
       
108552 */
       
108553 static RtreeCell *QuadraticPickNext(
       
108554   Rtree *pRtree,
       
108555   RtreeCell *aCell, 
       
108556   int nCell, 
       
108557   RtreeCell *pLeftBox, 
       
108558   RtreeCell *pRightBox,
       
108559   int *aiUsed
       
108560 ){
       
108561   #define FABS(a) ((a)<0.0?-1.0*(a):(a))
       
108562 
       
108563   int iSelect = -1;
       
108564   float fDiff;
       
108565   int ii;
       
108566   for(ii=0; ii<nCell; ii++){
       
108567     if( aiUsed[ii]==0 ){
       
108568       float left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
       
108569       float right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
       
108570       float diff = FABS(right-left);
       
108571       if( iSelect<0 || diff>fDiff ){
       
108572         fDiff = diff;
       
108573         iSelect = ii;
       
108574       }
       
108575     }
       
108576   }
       
108577   aiUsed[iSelect] = 1;
       
108578   return &aCell[iSelect];
       
108579 }
       
108580 
       
108581 /*
       
108582 ** Implementation of the quadratic variant of the PickSeeds() function from
       
108583 ** Guttman[84].
       
108584 */
       
108585 static void QuadraticPickSeeds(
       
108586   Rtree *pRtree,
       
108587   RtreeCell *aCell, 
       
108588   int nCell, 
       
108589   int *piLeftSeed, 
       
108590   int *piRightSeed
       
108591 ){
       
108592   int ii;
       
108593   int jj;
       
108594 
       
108595   int iLeftSeed = 0;
       
108596   int iRightSeed = 1;
       
108597   float fWaste = 0.0;
       
108598 
       
108599   for(ii=0; ii<nCell; ii++){
       
108600     for(jj=ii+1; jj<nCell; jj++){
       
108601       float right = cellArea(pRtree, &aCell[jj]);
       
108602       float growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
       
108603       float waste = growth - right;
       
108604 
       
108605       if( waste>fWaste ){
       
108606         iLeftSeed = ii;
       
108607         iRightSeed = jj;
       
108608         fWaste = waste;
       
108609       }
       
108610     }
       
108611   }
       
108612 
       
108613   *piLeftSeed = iLeftSeed;
       
108614   *piRightSeed = iRightSeed;
       
108615 }
       
108616 #endif /* VARIANT_GUTTMAN_QUADRATIC_SPLIT */
       
108617 
       
108618 /*
       
108619 ** Arguments aIdx, aDistance and aSpare all point to arrays of size
       
108620 ** nIdx. The aIdx array contains the set of integers from 0 to 
       
108621 ** (nIdx-1) in no particular order. This function sorts the values
       
108622 ** in aIdx according to the indexed values in aDistance. For
       
108623 ** example, assuming the inputs:
       
108624 **
       
108625 **   aIdx      = { 0,   1,   2,   3 }
       
108626 **   aDistance = { 5.0, 2.0, 7.0, 6.0 }
       
108627 **
       
108628 ** this function sets the aIdx array to contain:
       
108629 **
       
108630 **   aIdx      = { 0,   1,   2,   3 }
       
108631 **
       
108632 ** The aSpare array is used as temporary working space by the
       
108633 ** sorting algorithm.
       
108634 */
       
108635 static void SortByDistance(
       
108636   int *aIdx, 
       
108637   int nIdx, 
       
108638   float *aDistance, 
       
108639   int *aSpare
       
108640 ){
       
108641   if( nIdx>1 ){
       
108642     int iLeft = 0;
       
108643     int iRight = 0;
       
108644 
       
108645     int nLeft = nIdx/2;
       
108646     int nRight = nIdx-nLeft;
       
108647     int *aLeft = aIdx;
       
108648     int *aRight = &aIdx[nLeft];
       
108649 
       
108650     SortByDistance(aLeft, nLeft, aDistance, aSpare);
       
108651     SortByDistance(aRight, nRight, aDistance, aSpare);
       
108652 
       
108653     memcpy(aSpare, aLeft, sizeof(int)*nLeft);
       
108654     aLeft = aSpare;
       
108655 
       
108656     while( iLeft<nLeft || iRight<nRight ){
       
108657       if( iLeft==nLeft ){
       
108658         aIdx[iLeft+iRight] = aRight[iRight];
       
108659         iRight++;
       
108660       }else if( iRight==nRight ){
       
108661         aIdx[iLeft+iRight] = aLeft[iLeft];
       
108662         iLeft++;
       
108663       }else{
       
108664         float fLeft = aDistance[aLeft[iLeft]];
       
108665         float fRight = aDistance[aRight[iRight]];
       
108666         if( fLeft<fRight ){
       
108667           aIdx[iLeft+iRight] = aLeft[iLeft];
       
108668           iLeft++;
       
108669         }else{
       
108670           aIdx[iLeft+iRight] = aRight[iRight];
       
108671           iRight++;
       
108672         }
       
108673       }
       
108674     }
       
108675 
       
108676 #if 0
       
108677     /* Check that the sort worked */
       
108678     {
       
108679       int jj;
       
108680       for(jj=1; jj<nIdx; jj++){
       
108681         float left = aDistance[aIdx[jj-1]];
       
108682         float right = aDistance[aIdx[jj]];
       
108683         assert( left<=right );
       
108684       }
       
108685     }
       
108686 #endif
       
108687   }
       
108688 }
       
108689 
       
108690 /*
       
108691 ** Arguments aIdx, aCell and aSpare all point to arrays of size
       
108692 ** nIdx. The aIdx array contains the set of integers from 0 to 
       
108693 ** (nIdx-1) in no particular order. This function sorts the values
       
108694 ** in aIdx according to dimension iDim of the cells in aCell. The
       
108695 ** minimum value of dimension iDim is considered first, the
       
108696 ** maximum used to break ties.
       
108697 **
       
108698 ** The aSpare array is used as temporary working space by the
       
108699 ** sorting algorithm.
       
108700 */
       
108701 static void SortByDimension(
       
108702   Rtree *pRtree,
       
108703   int *aIdx, 
       
108704   int nIdx, 
       
108705   int iDim, 
       
108706   RtreeCell *aCell, 
       
108707   int *aSpare
       
108708 ){
       
108709   if( nIdx>1 ){
       
108710 
       
108711     int iLeft = 0;
       
108712     int iRight = 0;
       
108713 
       
108714     int nLeft = nIdx/2;
       
108715     int nRight = nIdx-nLeft;
       
108716     int *aLeft = aIdx;
       
108717     int *aRight = &aIdx[nLeft];
       
108718 
       
108719     SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
       
108720     SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
       
108721 
       
108722     memcpy(aSpare, aLeft, sizeof(int)*nLeft);
       
108723     aLeft = aSpare;
       
108724     while( iLeft<nLeft || iRight<nRight ){
       
108725       double xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
       
108726       double xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
       
108727       double xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
       
108728       double xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
       
108729       if( (iLeft!=nLeft) && ((iRight==nRight)
       
108730        || (xleft1<xright1)
       
108731        || (xleft1==xright1 && xleft2<xright2)
       
108732       )){
       
108733         aIdx[iLeft+iRight] = aLeft[iLeft];
       
108734         iLeft++;
       
108735       }else{
       
108736         aIdx[iLeft+iRight] = aRight[iRight];
       
108737         iRight++;
       
108738       }
       
108739     }
       
108740 
       
108741 #if 0
       
108742     /* Check that the sort worked */
       
108743     {
       
108744       int jj;
       
108745       for(jj=1; jj<nIdx; jj++){
       
108746         float xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
       
108747         float xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
       
108748         float xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
       
108749         float xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
       
108750         assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
       
108751       }
       
108752     }
       
108753 #endif
       
108754   }
       
108755 }
       
108756 
       
108757 #if VARIANT_RSTARTREE_SPLIT
       
108758 /*
       
108759 ** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
       
108760 */
       
108761 static int splitNodeStartree(
       
108762   Rtree *pRtree,
       
108763   RtreeCell *aCell,
       
108764   int nCell,
       
108765   RtreeNode *pLeft,
       
108766   RtreeNode *pRight,
       
108767   RtreeCell *pBboxLeft,
       
108768   RtreeCell *pBboxRight
       
108769 ){
       
108770   int **aaSorted;
       
108771   int *aSpare;
       
108772   int ii;
       
108773 
       
108774   int iBestDim;
       
108775   int iBestSplit;
       
108776   float fBestMargin;
       
108777 
       
108778   int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
       
108779 
       
108780   aaSorted = (int **)sqlite3_malloc(nByte);
       
108781   if( !aaSorted ){
       
108782     return SQLITE_NOMEM;
       
108783   }
       
108784 
       
108785   aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
       
108786   memset(aaSorted, 0, nByte);
       
108787   for(ii=0; ii<pRtree->nDim; ii++){
       
108788     int jj;
       
108789     aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
       
108790     for(jj=0; jj<nCell; jj++){
       
108791       aaSorted[ii][jj] = jj;
       
108792     }
       
108793     SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
       
108794   }
       
108795 
       
108796   for(ii=0; ii<pRtree->nDim; ii++){
       
108797     float margin = 0.0;
       
108798     float fBestOverlap;
       
108799     float fBestArea;
       
108800     int iBestLeft;
       
108801     int nLeft;
       
108802 
       
108803     for(
       
108804       nLeft=RTREE_MINCELLS(pRtree); 
       
108805       nLeft<=(nCell-RTREE_MINCELLS(pRtree)); 
       
108806       nLeft++
       
108807     ){
       
108808       RtreeCell left;
       
108809       RtreeCell right;
       
108810       int kk;
       
108811       float overlap;
       
108812       float area;
       
108813 
       
108814       memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
       
108815       memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
       
108816       for(kk=1; kk<(nCell-1); kk++){
       
108817         if( kk<nLeft ){
       
108818           cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
       
108819         }else{
       
108820           cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
       
108821         }
       
108822       }
       
108823       margin += cellMargin(pRtree, &left);
       
108824       margin += cellMargin(pRtree, &right);
       
108825       overlap = cellOverlap(pRtree, &left, &right, 1, -1);
       
108826       area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
       
108827       if( (nLeft==RTREE_MINCELLS(pRtree))
       
108828        || (overlap<fBestOverlap)
       
108829        || (overlap==fBestOverlap && area<fBestArea)
       
108830       ){
       
108831         iBestLeft = nLeft;
       
108832         fBestOverlap = overlap;
       
108833         fBestArea = area;
       
108834       }
       
108835     }
       
108836 
       
108837     if( ii==0 || margin<fBestMargin ){
       
108838       iBestDim = ii;
       
108839       fBestMargin = margin;
       
108840       iBestSplit = iBestLeft;
       
108841     }
       
108842   }
       
108843 
       
108844   memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
       
108845   memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
       
108846   for(ii=0; ii<nCell; ii++){
       
108847     RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
       
108848     RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
       
108849     RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
       
108850     nodeInsertCell(pRtree, pTarget, pCell);
       
108851     cellUnion(pRtree, pBbox, pCell);
       
108852   }
       
108853 
       
108854   sqlite3_free(aaSorted);
       
108855   return SQLITE_OK;
       
108856 }
       
108857 #endif
       
108858 
       
108859 #if VARIANT_GUTTMAN_SPLIT
       
108860 /*
       
108861 ** Implementation of the regular R-tree SplitNode from Guttman[1984].
       
108862 */
       
108863 static int splitNodeGuttman(
       
108864   Rtree *pRtree,
       
108865   RtreeCell *aCell,
       
108866   int nCell,
       
108867   RtreeNode *pLeft,
       
108868   RtreeNode *pRight,
       
108869   RtreeCell *pBboxLeft,
       
108870   RtreeCell *pBboxRight
       
108871 ){
       
108872   int iLeftSeed = 0;
       
108873   int iRightSeed = 1;
       
108874   int *aiUsed;
       
108875   int i;
       
108876 
       
108877   aiUsed = sqlite3_malloc(sizeof(int)*nCell);
       
108878   if( !aiUsed ){
       
108879     return SQLITE_NOMEM;
       
108880   }
       
108881   memset(aiUsed, 0, sizeof(int)*nCell);
       
108882 
       
108883   PickSeeds(pRtree, aCell, nCell, &iLeftSeed, &iRightSeed);
       
108884 
       
108885   memcpy(pBboxLeft, &aCell[iLeftSeed], sizeof(RtreeCell));
       
108886   memcpy(pBboxRight, &aCell[iRightSeed], sizeof(RtreeCell));
       
108887   nodeInsertCell(pRtree, pLeft, &aCell[iLeftSeed]);
       
108888   nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
       
108889   aiUsed[iLeftSeed] = 1;
       
108890   aiUsed[iRightSeed] = 1;
       
108891 
       
108892   for(i=nCell-2; i>0; i--){
       
108893     RtreeCell *pNext;
       
108894     pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
       
108895     float diff =  
       
108896       cellGrowth(pRtree, pBboxLeft, pNext) - 
       
108897       cellGrowth(pRtree, pBboxRight, pNext)
       
108898     ;
       
108899     if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
       
108900      || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
       
108901     ){
       
108902       nodeInsertCell(pRtree, pRight, pNext);
       
108903       cellUnion(pRtree, pBboxRight, pNext);
       
108904     }else{
       
108905       nodeInsertCell(pRtree, pLeft, pNext);
       
108906       cellUnion(pRtree, pBboxLeft, pNext);
       
108907     }
       
108908   }
       
108909 
       
108910   sqlite3_free(aiUsed);
       
108911   return SQLITE_OK;
       
108912 }
       
108913 #endif
       
108914 
       
108915 static int updateMapping(
       
108916   Rtree *pRtree, 
       
108917   i64 iRowid, 
       
108918   RtreeNode *pNode, 
       
108919   int iHeight
       
108920 ){
       
108921   int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
       
108922   xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
       
108923   if( iHeight>0 ){
       
108924     RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
       
108925     if( pChild ){
       
108926       nodeRelease(pRtree, pChild->pParent);
       
108927       nodeReference(pNode);
       
108928       pChild->pParent = pNode;
       
108929     }
       
108930   }
       
108931   return xSetMapping(pRtree, iRowid, pNode->iNode);
       
108932 }
       
108933 
       
108934 static int SplitNode(
       
108935   Rtree *pRtree,
       
108936   RtreeNode *pNode,
       
108937   RtreeCell *pCell,
       
108938   int iHeight
       
108939 ){
       
108940   int i;
       
108941   int newCellIsRight = 0;
       
108942 
       
108943   int rc = SQLITE_OK;
       
108944   int nCell = NCELL(pNode);
       
108945   RtreeCell *aCell;
       
108946   int *aiUsed;
       
108947 
       
108948   RtreeNode *pLeft = 0;
       
108949   RtreeNode *pRight = 0;
       
108950 
       
108951   RtreeCell leftbbox;
       
108952   RtreeCell rightbbox;
       
108953 
       
108954   /* Allocate an array and populate it with a copy of pCell and 
       
108955   ** all cells from node pLeft. Then zero the original node.
       
108956   */
       
108957   aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
       
108958   if( !aCell ){
       
108959     rc = SQLITE_NOMEM;
       
108960     goto splitnode_out;
       
108961   }
       
108962   aiUsed = (int *)&aCell[nCell+1];
       
108963   memset(aiUsed, 0, sizeof(int)*(nCell+1));
       
108964   for(i=0; i<nCell; i++){
       
108965     nodeGetCell(pRtree, pNode, i, &aCell[i]);
       
108966   }
       
108967   nodeZero(pRtree, pNode);
       
108968   memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
       
108969   nCell++;
       
108970 
       
108971   if( pNode->iNode==1 ){
       
108972     pRight = nodeNew(pRtree, pNode, 1);
       
108973     pLeft = nodeNew(pRtree, pNode, 1);
       
108974     pRtree->iDepth++;
       
108975     pNode->isDirty = 1;
       
108976     writeInt16(pNode->zData, pRtree->iDepth);
       
108977   }else{
       
108978     pLeft = pNode;
       
108979     pRight = nodeNew(pRtree, pLeft->pParent, 1);
       
108980     nodeReference(pLeft);
       
108981   }
       
108982 
       
108983   if( !pLeft || !pRight ){
       
108984     rc = SQLITE_NOMEM;
       
108985     goto splitnode_out;
       
108986   }
       
108987 
       
108988   memset(pLeft->zData, 0, pRtree->iNodeSize);
       
108989   memset(pRight->zData, 0, pRtree->iNodeSize);
       
108990 
       
108991   rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox);
       
108992   if( rc!=SQLITE_OK ){
       
108993     goto splitnode_out;
       
108994   }
       
108995 
       
108996   /* Ensure both child nodes have node numbers assigned to them. */
       
108997   if( (0==pRight->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pRight)))
       
108998    || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
       
108999   ){
       
109000     goto splitnode_out;
       
109001   }
       
109002 
       
109003   rightbbox.iRowid = pRight->iNode;
       
109004   leftbbox.iRowid = pLeft->iNode;
       
109005 
       
109006   if( pNode->iNode==1 ){
       
109007     rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
       
109008     if( rc!=SQLITE_OK ){
       
109009       goto splitnode_out;
       
109010     }
       
109011   }else{
       
109012     RtreeNode *pParent = pLeft->pParent;
       
109013     int iCell = nodeParentIndex(pRtree, pLeft);
       
109014     nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
       
109015     AdjustTree(pRtree, pParent, &leftbbox);
       
109016   }
       
109017   if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
       
109018     goto splitnode_out;
       
109019   }
       
109020 
       
109021   for(i=0; i<NCELL(pRight); i++){
       
109022     i64 iRowid = nodeGetRowid(pRtree, pRight, i);
       
109023     rc = updateMapping(pRtree, iRowid, pRight, iHeight);
       
109024     if( iRowid==pCell->iRowid ){
       
109025       newCellIsRight = 1;
       
109026     }
       
109027     if( rc!=SQLITE_OK ){
       
109028       goto splitnode_out;
       
109029     }
       
109030   }
       
109031   if( pNode->iNode==1 ){
       
109032     for(i=0; i<NCELL(pLeft); i++){
       
109033       i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
       
109034       rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
       
109035       if( rc!=SQLITE_OK ){
       
109036         goto splitnode_out;
       
109037       }
       
109038     }
       
109039   }else if( newCellIsRight==0 ){
       
109040     rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
       
109041   }
       
109042 
       
109043   if( rc==SQLITE_OK ){
       
109044     rc = nodeRelease(pRtree, pRight);
       
109045     pRight = 0;
       
109046   }
       
109047   if( rc==SQLITE_OK ){
       
109048     rc = nodeRelease(pRtree, pLeft);
       
109049     pLeft = 0;
       
109050   }
       
109051 
       
109052 splitnode_out:
       
109053   nodeRelease(pRtree, pRight);
       
109054   nodeRelease(pRtree, pLeft);
       
109055   sqlite3_free(aCell);
       
109056   return rc;
       
109057 }
       
109058 
       
109059 static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
       
109060   int rc = SQLITE_OK;
       
109061   if( pLeaf->iNode!=1 && pLeaf->pParent==0 ){
       
109062     sqlite3_bind_int64(pRtree->pReadParent, 1, pLeaf->iNode);
       
109063     if( sqlite3_step(pRtree->pReadParent)==SQLITE_ROW ){
       
109064       i64 iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
       
109065       rc = nodeAcquire(pRtree, iNode, 0, &pLeaf->pParent);
       
109066     }else{
       
109067       rc = SQLITE_ERROR;
       
109068     }
       
109069     sqlite3_reset(pRtree->pReadParent);
       
109070     if( rc==SQLITE_OK ){
       
109071       rc = fixLeafParent(pRtree, pLeaf->pParent);
       
109072     }
       
109073   }
       
109074   return rc;
       
109075 }
       
109076 
       
109077 static int deleteCell(Rtree *, RtreeNode *, int, int);
       
109078 
       
109079 static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
       
109080   int rc;
       
109081   RtreeNode *pParent;
       
109082   int iCell;
       
109083 
       
109084   assert( pNode->nRef==1 );
       
109085 
       
109086   /* Remove the entry in the parent cell. */
       
109087   iCell = nodeParentIndex(pRtree, pNode);
       
109088   pParent = pNode->pParent;
       
109089   pNode->pParent = 0;
       
109090   if( SQLITE_OK!=(rc = deleteCell(pRtree, pParent, iCell, iHeight+1)) 
       
109091    || SQLITE_OK!=(rc = nodeRelease(pRtree, pParent))
       
109092   ){
       
109093     return rc;
       
109094   }
       
109095 
       
109096   /* Remove the xxx_node entry. */
       
109097   sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
       
109098   sqlite3_step(pRtree->pDeleteNode);
       
109099   if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
       
109100     return rc;
       
109101   }
       
109102 
       
109103   /* Remove the xxx_parent entry. */
       
109104   sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
       
109105   sqlite3_step(pRtree->pDeleteParent);
       
109106   if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
       
109107     return rc;
       
109108   }
       
109109   
       
109110   /* Remove the node from the in-memory hash table and link it into
       
109111   ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
       
109112   */
       
109113   nodeHashDelete(pRtree, pNode);
       
109114   pNode->iNode = iHeight;
       
109115   pNode->pNext = pRtree->pDeleted;
       
109116   pNode->nRef++;
       
109117   pRtree->pDeleted = pNode;
       
109118 
       
109119   return SQLITE_OK;
       
109120 }
       
109121 
       
109122 static void fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
       
109123   RtreeNode *pParent = pNode->pParent;
       
109124   if( pParent ){
       
109125     int ii; 
       
109126     int nCell = NCELL(pNode);
       
109127     RtreeCell box;                            /* Bounding box for pNode */
       
109128     nodeGetCell(pRtree, pNode, 0, &box);
       
109129     for(ii=1; ii<nCell; ii++){
       
109130       RtreeCell cell;
       
109131       nodeGetCell(pRtree, pNode, ii, &cell);
       
109132       cellUnion(pRtree, &box, &cell);
       
109133     }
       
109134     box.iRowid = pNode->iNode;
       
109135     ii = nodeParentIndex(pRtree, pNode);
       
109136     nodeOverwriteCell(pRtree, pParent, &box, ii);
       
109137     fixBoundingBox(pRtree, pParent);
       
109138   }
       
109139 }
       
109140 
       
109141 /*
       
109142 ** Delete the cell at index iCell of node pNode. After removing the
       
109143 ** cell, adjust the r-tree data structure if required.
       
109144 */
       
109145 static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
       
109146   int rc;
       
109147 
       
109148   if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
       
109149     return rc;
       
109150   }
       
109151 
       
109152   /* Remove the cell from the node. This call just moves bytes around
       
109153   ** the in-memory node image, so it cannot fail.
       
109154   */
       
109155   nodeDeleteCell(pRtree, pNode, iCell);
       
109156 
       
109157   /* If the node is not the tree root and now has less than the minimum
       
109158   ** number of cells, remove it from the tree. Otherwise, update the
       
109159   ** cell in the parent node so that it tightly contains the updated
       
109160   ** node.
       
109161   */
       
109162   if( pNode->iNode!=1 ){
       
109163     RtreeNode *pParent = pNode->pParent;
       
109164     if( (pParent->iNode!=1 || NCELL(pParent)!=1) 
       
109165      && (NCELL(pNode)<RTREE_MINCELLS(pRtree))
       
109166     ){
       
109167       rc = removeNode(pRtree, pNode, iHeight);
       
109168     }else{
       
109169       fixBoundingBox(pRtree, pNode);
       
109170     }
       
109171   }
       
109172 
       
109173   return rc;
       
109174 }
       
109175 
       
109176 static int Reinsert(
       
109177   Rtree *pRtree, 
       
109178   RtreeNode *pNode, 
       
109179   RtreeCell *pCell, 
       
109180   int iHeight
       
109181 ){
       
109182   int *aOrder;
       
109183   int *aSpare;
       
109184   RtreeCell *aCell;
       
109185   float *aDistance;
       
109186   int nCell;
       
109187   float aCenterCoord[RTREE_MAX_DIMENSIONS];
       
109188   int iDim;
       
109189   int ii;
       
109190   int rc = SQLITE_OK;
       
109191 
       
109192   memset(aCenterCoord, 0, sizeof(float)*RTREE_MAX_DIMENSIONS);
       
109193 
       
109194   nCell = NCELL(pNode)+1;
       
109195 
       
109196   /* Allocate the buffers used by this operation. The allocation is
       
109197   ** relinquished before this function returns.
       
109198   */
       
109199   aCell = (RtreeCell *)sqlite3_malloc(nCell * (
       
109200     sizeof(RtreeCell) +         /* aCell array */
       
109201     sizeof(int)       +         /* aOrder array */
       
109202     sizeof(int)       +         /* aSpare array */
       
109203     sizeof(float)               /* aDistance array */
       
109204   ));
       
109205   if( !aCell ){
       
109206     return SQLITE_NOMEM;
       
109207   }
       
109208   aOrder    = (int *)&aCell[nCell];
       
109209   aSpare    = (int *)&aOrder[nCell];
       
109210   aDistance = (float *)&aSpare[nCell];
       
109211 
       
109212   for(ii=0; ii<nCell; ii++){
       
109213     if( ii==(nCell-1) ){
       
109214       memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
       
109215     }else{
       
109216       nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
       
109217     }
       
109218     aOrder[ii] = ii;
       
109219     for(iDim=0; iDim<pRtree->nDim; iDim++){
       
109220       aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
       
109221       aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
       
109222     }
       
109223   }
       
109224   for(iDim=0; iDim<pRtree->nDim; iDim++){
       
109225     aCenterCoord[iDim] = aCenterCoord[iDim]/((float)nCell*2.0);
       
109226   }
       
109227 
       
109228   for(ii=0; ii<nCell; ii++){
       
109229     aDistance[ii] = 0.0;
       
109230     for(iDim=0; iDim<pRtree->nDim; iDim++){
       
109231       float coord = DCOORD(aCell[ii].aCoord[iDim*2+1]) - 
       
109232           DCOORD(aCell[ii].aCoord[iDim*2]);
       
109233       aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
       
109234     }
       
109235   }
       
109236 
       
109237   SortByDistance(aOrder, nCell, aDistance, aSpare);
       
109238   nodeZero(pRtree, pNode);
       
109239 
       
109240   for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
       
109241     RtreeCell *p = &aCell[aOrder[ii]];
       
109242     nodeInsertCell(pRtree, pNode, p);
       
109243     if( p->iRowid==pCell->iRowid ){
       
109244       if( iHeight==0 ){
       
109245         rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
       
109246       }else{
       
109247         rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
       
109248       }
       
109249     }
       
109250   }
       
109251   if( rc==SQLITE_OK ){
       
109252     fixBoundingBox(pRtree, pNode);
       
109253   }
       
109254   for(; rc==SQLITE_OK && ii<nCell; ii++){
       
109255     /* Find a node to store this cell in. pNode->iNode currently contains
       
109256     ** the height of the sub-tree headed by the cell.
       
109257     */
       
109258     RtreeNode *pInsert;
       
109259     RtreeCell *p = &aCell[aOrder[ii]];
       
109260     rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
       
109261     if( rc==SQLITE_OK ){
       
109262       int rc2;
       
109263       rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
       
109264       rc2 = nodeRelease(pRtree, pInsert);
       
109265       if( rc==SQLITE_OK ){
       
109266         rc = rc2;
       
109267       }
       
109268     }
       
109269   }
       
109270 
       
109271   sqlite3_free(aCell);
       
109272   return rc;
       
109273 }
       
109274 
       
109275 /*
       
109276 ** Insert cell pCell into node pNode. Node pNode is the head of a 
       
109277 ** subtree iHeight high (leaf nodes have iHeight==0).
       
109278 */
       
109279 static int rtreeInsertCell(
       
109280   Rtree *pRtree,
       
109281   RtreeNode *pNode,
       
109282   RtreeCell *pCell,
       
109283   int iHeight
       
109284 ){
       
109285   int rc = SQLITE_OK;
       
109286   if( iHeight>0 ){
       
109287     RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
       
109288     if( pChild ){
       
109289       nodeRelease(pRtree, pChild->pParent);
       
109290       nodeReference(pNode);
       
109291       pChild->pParent = pNode;
       
109292     }
       
109293   }
       
109294   if( nodeInsertCell(pRtree, pNode, pCell) ){
       
109295 #if VARIANT_RSTARTREE_REINSERT
       
109296     if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
       
109297       rc = SplitNode(pRtree, pNode, pCell, iHeight);
       
109298     }else{
       
109299       pRtree->iReinsertHeight = iHeight;
       
109300       rc = Reinsert(pRtree, pNode, pCell, iHeight);
       
109301     }
       
109302 #else
       
109303     rc = SplitNode(pRtree, pNode, pCell, iHeight);
       
109304 #endif
       
109305   }else{
       
109306     AdjustTree(pRtree, pNode, pCell);
       
109307     if( iHeight==0 ){
       
109308       rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
       
109309     }else{
       
109310       rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
       
109311     }
       
109312   }
       
109313   return rc;
       
109314 }
       
109315 
       
109316 static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
       
109317   int ii;
       
109318   int rc = SQLITE_OK;
       
109319   int nCell = NCELL(pNode);
       
109320 
       
109321   for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
       
109322     RtreeNode *pInsert;
       
109323     RtreeCell cell;
       
109324     nodeGetCell(pRtree, pNode, ii, &cell);
       
109325 
       
109326     /* Find a node to store this cell in. pNode->iNode currently contains
       
109327     ** the height of the sub-tree headed by the cell.
       
109328     */
       
109329     rc = ChooseLeaf(pRtree, &cell, pNode->iNode, &pInsert);
       
109330     if( rc==SQLITE_OK ){
       
109331       int rc2;
       
109332       rc = rtreeInsertCell(pRtree, pInsert, &cell, pNode->iNode);
       
109333       rc2 = nodeRelease(pRtree, pInsert);
       
109334       if( rc==SQLITE_OK ){
       
109335         rc = rc2;
       
109336       }
       
109337     }
       
109338   }
       
109339   return rc;
       
109340 }
       
109341 
       
109342 /*
       
109343 ** Select a currently unused rowid for a new r-tree record.
       
109344 */
       
109345 static int newRowid(Rtree *pRtree, i64 *piRowid){
       
109346   int rc;
       
109347   sqlite3_bind_null(pRtree->pWriteRowid, 1);
       
109348   sqlite3_bind_null(pRtree->pWriteRowid, 2);
       
109349   sqlite3_step(pRtree->pWriteRowid);
       
109350   rc = sqlite3_reset(pRtree->pWriteRowid);
       
109351   *piRowid = sqlite3_last_insert_rowid(pRtree->db);
       
109352   return rc;
       
109353 }
       
109354 
       
109355 #ifndef NDEBUG
       
109356 static int hashIsEmpty(Rtree *pRtree){
       
109357   int ii;
       
109358   for(ii=0; ii<HASHSIZE; ii++){
       
109359     assert( !pRtree->aHash[ii] );
       
109360   }
       
109361   return 1;
       
109362 }
       
109363 #endif
       
109364 
       
109365 /*
       
109366 ** The xUpdate method for rtree module virtual tables.
       
109367 */
       
109368 static int rtreeUpdate(
       
109369   sqlite3_vtab *pVtab, 
       
109370   int nData, 
       
109371   sqlite3_value **azData, 
       
109372   sqlite_int64 *pRowid
       
109373 ){
       
109374   Rtree *pRtree = (Rtree *)pVtab;
       
109375   int rc = SQLITE_OK;
       
109376 
       
109377   rtreeReference(pRtree);
       
109378 
       
109379   assert(nData>=1);
       
109380   assert(hashIsEmpty(pRtree));
       
109381 
       
109382   /* If azData[0] is not an SQL NULL value, it is the rowid of a
       
109383   ** record to delete from the r-tree table. The following block does
       
109384   ** just that.
       
109385   */
       
109386   if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
       
109387     i64 iDelete;                /* The rowid to delete */
       
109388     RtreeNode *pLeaf;           /* Leaf node containing record iDelete */
       
109389     int iCell;                  /* Index of iDelete cell in pLeaf */
       
109390     RtreeNode *pRoot;
       
109391 
       
109392     /* Obtain a reference to the root node to initialise Rtree.iDepth */
       
109393     rc = nodeAcquire(pRtree, 1, 0, &pRoot);
       
109394 
       
109395     /* Obtain a reference to the leaf node that contains the entry 
       
109396     ** about to be deleted. 
       
109397     */
       
109398     if( rc==SQLITE_OK ){
       
109399       iDelete = sqlite3_value_int64(azData[0]);
       
109400       rc = findLeafNode(pRtree, iDelete, &pLeaf);
       
109401     }
       
109402 
       
109403     /* Delete the cell in question from the leaf node. */
       
109404     if( rc==SQLITE_OK ){
       
109405       int rc2;
       
109406       iCell = nodeRowidIndex(pRtree, pLeaf, iDelete);
       
109407       rc = deleteCell(pRtree, pLeaf, iCell, 0);
       
109408       rc2 = nodeRelease(pRtree, pLeaf);
       
109409       if( rc==SQLITE_OK ){
       
109410         rc = rc2;
       
109411       }
       
109412     }
       
109413 
       
109414     /* Delete the corresponding entry in the <rtree>_rowid table. */
       
109415     if( rc==SQLITE_OK ){
       
109416       sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
       
109417       sqlite3_step(pRtree->pDeleteRowid);
       
109418       rc = sqlite3_reset(pRtree->pDeleteRowid);
       
109419     }
       
109420 
       
109421     /* Check if the root node now has exactly one child. If so, remove
       
109422     ** it, schedule the contents of the child for reinsertion and 
       
109423     ** reduce the tree height by one.
       
109424     **
       
109425     ** This is equivalent to copying the contents of the child into
       
109426     ** the root node (the operation that Gutman's paper says to perform 
       
109427     ** in this scenario).
       
109428     */
       
109429     if( rc==SQLITE_OK && pRtree->iDepth>0 ){
       
109430       if( rc==SQLITE_OK && NCELL(pRoot)==1 ){
       
109431         RtreeNode *pChild;
       
109432         i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
       
109433         rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
       
109434         if( rc==SQLITE_OK ){
       
109435           rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
       
109436         }
       
109437         if( rc==SQLITE_OK ){
       
109438           pRtree->iDepth--;
       
109439           writeInt16(pRoot->zData, pRtree->iDepth);
       
109440           pRoot->isDirty = 1;
       
109441         }
       
109442       }
       
109443     }
       
109444 
       
109445     /* Re-insert the contents of any underfull nodes removed from the tree. */
       
109446     for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
       
109447       if( rc==SQLITE_OK ){
       
109448         rc = reinsertNodeContent(pRtree, pLeaf);
       
109449       }
       
109450       pRtree->pDeleted = pLeaf->pNext;
       
109451       sqlite3_free(pLeaf);
       
109452     }
       
109453 
       
109454     /* Release the reference to the root node. */
       
109455     if( rc==SQLITE_OK ){
       
109456       rc = nodeRelease(pRtree, pRoot);
       
109457     }else{
       
109458       nodeRelease(pRtree, pRoot);
       
109459     }
       
109460   }
       
109461 
       
109462   /* If the azData[] array contains more than one element, elements
       
109463   ** (azData[2]..azData[argc-1]) contain a new record to insert into
       
109464   ** the r-tree structure.
       
109465   */
       
109466   if( rc==SQLITE_OK && nData>1 ){
       
109467     /* Insert a new record into the r-tree */
       
109468     RtreeCell cell;
       
109469     int ii;
       
109470     RtreeNode *pLeaf;
       
109471 
       
109472     /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
       
109473     assert( nData==(pRtree->nDim*2 + 3) );
       
109474     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
       
109475       for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       
109476         cell.aCoord[ii].f = (float)sqlite3_value_double(azData[ii+3]);
       
109477         cell.aCoord[ii+1].f = (float)sqlite3_value_double(azData[ii+4]);
       
109478         if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
       
109479           rc = SQLITE_CONSTRAINT;
       
109480           goto constraint;
       
109481         }
       
109482       }
       
109483     }else{
       
109484       for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       
109485         cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
       
109486         cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
       
109487         if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
       
109488           rc = SQLITE_CONSTRAINT;
       
109489           goto constraint;
       
109490         }
       
109491       }
       
109492     }
       
109493 
       
109494     /* Figure out the rowid of the new row. */
       
109495     if( sqlite3_value_type(azData[2])==SQLITE_NULL ){
       
109496       rc = newRowid(pRtree, &cell.iRowid);
       
109497     }else{
       
109498       cell.iRowid = sqlite3_value_int64(azData[2]);
       
109499       sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
       
109500       if( SQLITE_ROW==sqlite3_step(pRtree->pReadRowid) ){
       
109501         sqlite3_reset(pRtree->pReadRowid);
       
109502         rc = SQLITE_CONSTRAINT;
       
109503         goto constraint;
       
109504       }
       
109505       rc = sqlite3_reset(pRtree->pReadRowid);
       
109506     }
       
109507 
       
109508     if( rc==SQLITE_OK ){
       
109509       rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
       
109510     }
       
109511     if( rc==SQLITE_OK ){
       
109512       int rc2;
       
109513       pRtree->iReinsertHeight = -1;
       
109514       rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
       
109515       rc2 = nodeRelease(pRtree, pLeaf);
       
109516       if( rc==SQLITE_OK ){
       
109517         rc = rc2;
       
109518       }
       
109519     }
       
109520   }
       
109521 
       
109522 constraint:
       
109523   rtreeRelease(pRtree);
       
109524   return rc;
       
109525 }
       
109526 
       
109527 /*
       
109528 ** The xRename method for rtree module virtual tables.
       
109529 */
       
109530 static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
       
109531   Rtree *pRtree = (Rtree *)pVtab;
       
109532   int rc = SQLITE_NOMEM;
       
109533   char *zSql = sqlite3_mprintf(
       
109534     "ALTER TABLE %Q.'%q_node'   RENAME TO \"%w_node\";"
       
109535     "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
       
109536     "ALTER TABLE %Q.'%q_rowid'  RENAME TO \"%w_rowid\";"
       
109537     , pRtree->zDb, pRtree->zName, zNewName 
       
109538     , pRtree->zDb, pRtree->zName, zNewName 
       
109539     , pRtree->zDb, pRtree->zName, zNewName
       
109540   );
       
109541   if( zSql ){
       
109542     rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
       
109543     sqlite3_free(zSql);
       
109544   }
       
109545   return rc;
       
109546 }
       
109547 
       
109548 static sqlite3_module rtreeModule = {
       
109549   0,                         /* iVersion */
       
109550   rtreeCreate,                /* xCreate - create a table */
       
109551   rtreeConnect,               /* xConnect - connect to an existing table */
       
109552   rtreeBestIndex,             /* xBestIndex - Determine search strategy */
       
109553   rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
       
109554   rtreeDestroy,               /* xDestroy - Drop a table */
       
109555   rtreeOpen,                  /* xOpen - open a cursor */
       
109556   rtreeClose,                 /* xClose - close a cursor */
       
109557   rtreeFilter,                /* xFilter - configure scan constraints */
       
109558   rtreeNext,                  /* xNext - advance a cursor */
       
109559   rtreeEof,                   /* xEof */
       
109560   rtreeColumn,                /* xColumn - read data */
       
109561   rtreeRowid,                 /* xRowid - read data */
       
109562   rtreeUpdate,                /* xUpdate - write data */
       
109563   0,                          /* xBegin - begin transaction */
       
109564   0,                          /* xSync - sync transaction */
       
109565   0,                          /* xCommit - commit transaction */
       
109566   0,                          /* xRollback - rollback transaction */
       
109567   0,                          /* xFindFunction - function overloading */
       
109568   rtreeRename                 /* xRename - rename the table */
       
109569 };
       
109570 
       
109571 static int rtreeSqlInit(
       
109572   Rtree *pRtree, 
       
109573   sqlite3 *db, 
       
109574   const char *zDb, 
       
109575   const char *zPrefix, 
       
109576   int isCreate
       
109577 ){
       
109578   int rc = SQLITE_OK;
       
109579 
       
109580   #define N_STATEMENT 9
       
109581   static const char *azSql[N_STATEMENT] = {
       
109582     /* Read and write the xxx_node table */
       
109583     "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
       
109584     "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
       
109585     "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
       
109586 
       
109587     /* Read and write the xxx_rowid table */
       
109588     "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
       
109589     "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
       
109590     "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
       
109591 
       
109592     /* Read and write the xxx_parent table */
       
109593     "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
       
109594     "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
       
109595     "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
       
109596   };
       
109597   sqlite3_stmt **appStmt[N_STATEMENT];
       
109598   int i;
       
109599 
       
109600   pRtree->db = db;
       
109601 
       
109602   if( isCreate ){
       
109603     char *zCreate = sqlite3_mprintf(
       
109604 "CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
       
109605 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
       
109606 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY, parentnode INTEGER);"
       
109607 "INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
       
109608       zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
       
109609     );
       
109610     if( !zCreate ){
       
109611       return SQLITE_NOMEM;
       
109612     }
       
109613     rc = sqlite3_exec(db, zCreate, 0, 0, 0);
       
109614     sqlite3_free(zCreate);
       
109615     if( rc!=SQLITE_OK ){
       
109616       return rc;
       
109617     }
       
109618   }
       
109619 
       
109620   appStmt[0] = &pRtree->pReadNode;
       
109621   appStmt[1] = &pRtree->pWriteNode;
       
109622   appStmt[2] = &pRtree->pDeleteNode;
       
109623   appStmt[3] = &pRtree->pReadRowid;
       
109624   appStmt[4] = &pRtree->pWriteRowid;
       
109625   appStmt[5] = &pRtree->pDeleteRowid;
       
109626   appStmt[6] = &pRtree->pReadParent;
       
109627   appStmt[7] = &pRtree->pWriteParent;
       
109628   appStmt[8] = &pRtree->pDeleteParent;
       
109629 
       
109630   for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
       
109631     char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
       
109632     if( zSql ){
       
109633       rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0); 
       
109634     }else{
       
109635       rc = SQLITE_NOMEM;
       
109636     }
       
109637     sqlite3_free(zSql);
       
109638   }
       
109639 
       
109640   return rc;
       
109641 }
       
109642 
       
109643 /*
       
109644 ** This routine queries database handle db for the page-size used by
       
109645 ** database zDb. If successful, the page-size in bytes is written to
       
109646 ** *piPageSize and SQLITE_OK returned. Otherwise, and an SQLite error 
       
109647 ** code is returned.
       
109648 */
       
109649 static int getPageSize(sqlite3 *db, const char *zDb, int *piPageSize){
       
109650   int rc = SQLITE_NOMEM;
       
109651   char *zSql;
       
109652   sqlite3_stmt *pStmt = 0;
       
109653 
       
109654   zSql = sqlite3_mprintf("PRAGMA %Q.page_size", zDb);
       
109655   if( !zSql ){
       
109656     return SQLITE_NOMEM;
       
109657   }
       
109658 
       
109659   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
       
109660   sqlite3_free(zSql);
       
109661   if( rc!=SQLITE_OK ){
       
109662     return rc;
       
109663   }
       
109664 
       
109665   if( SQLITE_ROW==sqlite3_step(pStmt) ){
       
109666     *piPageSize = sqlite3_column_int(pStmt, 0);
       
109667   }
       
109668   return sqlite3_finalize(pStmt);
       
109669 }
       
109670 
       
109671 /* 
       
109672 ** This function is the implementation of both the xConnect and xCreate
       
109673 ** methods of the r-tree virtual table.
       
109674 **
       
109675 **   argv[0]   -> module name
       
109676 **   argv[1]   -> database name
       
109677 **   argv[2]   -> table name
       
109678 **   argv[...] -> column names...
       
109679 */
       
109680 static int rtreeInit(
       
109681   sqlite3 *db,                        /* Database connection */
       
109682   void *pAux,                         /* One of the RTREE_COORD_* constants */
       
109683   int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
       
109684   sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
       
109685   char **pzErr,                       /* OUT: Error message, if any */
       
109686   int isCreate                        /* True for xCreate, false for xConnect */
       
109687 ){
       
109688   int rc = SQLITE_OK;
       
109689   int iPageSize = 0;
       
109690   Rtree *pRtree;
       
109691   int nDb;              /* Length of string argv[1] */
       
109692   int nName;            /* Length of string argv[2] */
       
109693   int eCoordType = (int)pAux;
       
109694 
       
109695   const char *aErrMsg[] = {
       
109696     0,                                                    /* 0 */
       
109697     "Wrong number of columns for an rtree table",         /* 1 */
       
109698     "Too few columns for an rtree table",                 /* 2 */
       
109699     "Too many columns for an rtree table"                 /* 3 */
       
109700   };
       
109701 
       
109702   int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
       
109703   if( aErrMsg[iErr] ){
       
109704     *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
       
109705     return SQLITE_ERROR;
       
109706   }
       
109707 
       
109708   rc = getPageSize(db, argv[1], &iPageSize);
       
109709   if( rc!=SQLITE_OK ){
       
109710     return rc;
       
109711   }
       
109712 
       
109713   /* Allocate the sqlite3_vtab structure */
       
109714   nDb = strlen(argv[1]);
       
109715   nName = strlen(argv[2]);
       
109716   pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
       
109717   if( !pRtree ){
       
109718     return SQLITE_NOMEM;
       
109719   }
       
109720   memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
       
109721   pRtree->nBusy = 1;
       
109722   pRtree->base.pModule = &rtreeModule;
       
109723   pRtree->zDb = (char *)&pRtree[1];
       
109724   pRtree->zName = &pRtree->zDb[nDb+1];
       
109725   pRtree->nDim = (argc-4)/2;
       
109726   pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
       
109727   pRtree->eCoordType = eCoordType;
       
109728   memcpy(pRtree->zDb, argv[1], nDb);
       
109729   memcpy(pRtree->zName, argv[2], nName);
       
109730 
       
109731   /* Figure out the node size to use. By default, use 64 bytes less than
       
109732   ** the database page-size. This ensures that each node is stored on
       
109733   ** a single database page.
       
109734   **
       
109735   ** If the databasd page-size is so large that more than RTREE_MAXCELLS
       
109736   ** entries would fit in a single node, use a smaller node-size.
       
109737   */
       
109738   pRtree->iNodeSize = iPageSize-64;
       
109739   if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
       
109740     pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
       
109741   }
       
109742 
       
109743   /* Create/Connect to the underlying relational database schema. If
       
109744   ** that is successful, call sqlite3_declare_vtab() to configure
       
109745   ** the r-tree table schema.
       
109746   */
       
109747   if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
       
109748     *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
       
109749   }else{
       
109750     char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
       
109751     char *zTmp;
       
109752     int ii;
       
109753     for(ii=4; zSql && ii<argc; ii++){
       
109754       zTmp = zSql;
       
109755       zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
       
109756       sqlite3_free(zTmp);
       
109757     }
       
109758     if( zSql ){
       
109759       zTmp = zSql;
       
109760       zSql = sqlite3_mprintf("%s);", zTmp);
       
109761       sqlite3_free(zTmp);
       
109762     }
       
109763     if( !zSql ){
       
109764       rc = SQLITE_NOMEM;
       
109765     }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
       
109766       *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
       
109767     }
       
109768     sqlite3_free(zSql);
       
109769   }
       
109770 
       
109771   if( rc==SQLITE_OK ){
       
109772     *ppVtab = (sqlite3_vtab *)pRtree;
       
109773   }else{
       
109774     rtreeRelease(pRtree);
       
109775   }
       
109776   return rc;
       
109777 }
       
109778 
       
109779 
       
109780 /*
       
109781 ** Implementation of a scalar function that decodes r-tree nodes to
       
109782 ** human readable strings. This can be used for debugging and analysis.
       
109783 **
       
109784 ** The scalar function takes two arguments, a blob of data containing
       
109785 ** an r-tree node, and the number of dimensions the r-tree indexes.
       
109786 ** For a two-dimensional r-tree structure called "rt", to deserialize
       
109787 ** all nodes, a statement like:
       
109788 **
       
109789 **   SELECT rtreenode(2, data) FROM rt_node;
       
109790 **
       
109791 ** The human readable string takes the form of a Tcl list with one
       
109792 ** entry for each cell in the r-tree node. Each entry is itself a
       
109793 ** list, containing the 8-byte rowid/pageno followed by the 
       
109794 ** <num-dimension>*2 coordinates.
       
109795 */
       
109796 static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
       
109797   char *zText = 0;
       
109798   RtreeNode node;
       
109799   Rtree tree;
       
109800   int ii;
       
109801 
       
109802   memset(&node, 0, sizeof(RtreeNode));
       
109803   memset(&tree, 0, sizeof(Rtree));
       
109804   tree.nDim = sqlite3_value_int(apArg[0]);
       
109805   tree.nBytesPerCell = 8 + 8 * tree.nDim;
       
109806   node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
       
109807 
       
109808   for(ii=0; ii<NCELL(&node); ii++){
       
109809     char zCell[512];
       
109810     int nCell = 0;
       
109811     RtreeCell cell;
       
109812     int jj;
       
109813 
       
109814     nodeGetCell(&tree, &node, ii, &cell);
       
109815     sqlite3_snprintf(512-nCell,&zCell[nCell],"%d", cell.iRowid);
       
109816     nCell = strlen(zCell);
       
109817     for(jj=0; jj<tree.nDim*2; jj++){
       
109818       sqlite3_snprintf(512-nCell,&zCell[nCell]," %f",(double)cell.aCoord[jj].f);
       
109819       nCell = strlen(zCell);
       
109820     }
       
109821 
       
109822     if( zText ){
       
109823       char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
       
109824       sqlite3_free(zText);
       
109825       zText = zTextNew;
       
109826     }else{
       
109827       zText = sqlite3_mprintf("{%s}", zCell);
       
109828     }
       
109829   }
       
109830   
       
109831   sqlite3_result_text(ctx, zText, -1, sqlite3_free);
       
109832 }
       
109833 
       
109834 static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
       
109835   if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB 
       
109836    || sqlite3_value_bytes(apArg[0])<2
       
109837   ){
       
109838     sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1); 
       
109839   }else{
       
109840     u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
       
109841     sqlite3_result_int(ctx, readInt16(zBlob));
       
109842   }
       
109843 }
       
109844 
       
109845 /*
       
109846 ** Register the r-tree module with database handle db. This creates the
       
109847 ** virtual table module "rtree" and the debugging/analysis scalar 
       
109848 ** function "rtreenode".
       
109849 */
       
109850 SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
       
109851   int rc = SQLITE_OK;
       
109852 
       
109853   if( rc==SQLITE_OK ){
       
109854     int utf8 = SQLITE_UTF8;
       
109855     rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
       
109856   }
       
109857   if( rc==SQLITE_OK ){
       
109858     int utf8 = SQLITE_UTF8;
       
109859     rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
       
109860   }
       
109861   if( rc==SQLITE_OK ){
       
109862     void *c = (void *)RTREE_COORD_REAL32;
       
109863     rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
       
109864   }
       
109865   if( rc==SQLITE_OK ){
       
109866     void *c = (void *)RTREE_COORD_INT32;
       
109867     rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
       
109868   }
       
109869 
       
109870   return rc;
       
109871 }
       
109872 
       
109873 #if !SQLITE_CORE
       
109874 SQLITE_API int sqlite3_extension_init(
       
109875   sqlite3 *db,
       
109876   char **pzErrMsg,
       
109877   const sqlite3_api_routines *pApi
       
109878 ){
       
109879   SQLITE_EXTENSION_INIT2(pApi)
       
109880   return sqlite3RtreeInit(db);
       
109881 }
       
109882 #endif
       
109883 
       
109884 #endif
       
109885 
       
109886 /************** End of rtree.c ***********************************************/
       
109887 /************** Begin file icu.c *********************************************/
       
109888 /*
       
109889 ** 2007 May 6
       
109890 **
       
109891 ** The author disclaims copyright to this source code.  In place of
       
109892 ** a legal notice, here is a blessing:
       
109893 **
       
109894 **    May you do good and not evil.
       
109895 **    May you find forgiveness for yourself and forgive others.
       
109896 **    May you share freely, never taking more than you give.
       
109897 **
       
109898 *************************************************************************
       
109899 ** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
       
109900 **
       
109901 ** This file implements an integration between the ICU library 
       
109902 ** ("International Components for Unicode", an open-source library 
       
109903 ** for handling unicode data) and SQLite. The integration uses 
       
109904 ** ICU to provide the following to SQLite:
       
109905 **
       
109906 **   * An implementation of the SQL regexp() function (and hence REGEXP
       
109907 **     operator) using the ICU uregex_XX() APIs.
       
109908 **
       
109909 **   * Implementations of the SQL scalar upper() and lower() functions
       
109910 **     for case mapping.
       
109911 **
       
109912 **   * Integration of ICU and SQLite collation seqences.
       
109913 **
       
109914 **   * An implementation of the LIKE operator that uses ICU to 
       
109915 **     provide case-independent matching.
       
109916 */
       
109917 
       
109918 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
       
109919 
       
109920 /* Include ICU headers */
       
109921 #include <unicode/utypes.h>
       
109922 #include <unicode/uregex.h>
       
109923 #include <unicode/ustring.h>
       
109924 #include <unicode/ucol.h>
       
109925 
       
109926 
       
109927 #ifndef SQLITE_CORE
       
109928   SQLITE_EXTENSION_INIT1
       
109929 #else
       
109930 #endif
       
109931 
       
109932 /*
       
109933 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
       
109934 ** operator.
       
109935 */
       
109936 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
       
109937 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
       
109938 #endif
       
109939 
       
109940 /*
       
109941 ** Version of sqlite3_free() that is always a function, never a macro.
       
109942 */
       
109943 static void xFree(void *p){
       
109944   sqlite3_free(p);
       
109945 }
       
109946 
       
109947 /*
       
109948 ** Compare two UTF-8 strings for equality where the first string is
       
109949 ** a "LIKE" expression. Return true (1) if they are the same and 
       
109950 ** false (0) if they are different.
       
109951 */
       
109952 static int icuLikeCompare(
       
109953   const uint8_t *zPattern,   /* LIKE pattern */
       
109954   const uint8_t *zString,    /* The UTF-8 string to compare against */
       
109955   const UChar32 uEsc         /* The escape character */
       
109956 ){
       
109957   static const int MATCH_ONE = (UChar32)'_';
       
109958   static const int MATCH_ALL = (UChar32)'%';
       
109959 
       
109960   int iPattern = 0;       /* Current byte index in zPattern */
       
109961   int iString = 0;        /* Current byte index in zString */
       
109962 
       
109963   int prevEscape = 0;     /* True if the previous character was uEsc */
       
109964 
       
109965   while( zPattern[iPattern]!=0 ){
       
109966 
       
109967     /* Read (and consume) the next character from the input pattern. */
       
109968     UChar32 uPattern;
       
109969     U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
       
109970     assert(uPattern!=0);
       
109971 
       
109972     /* There are now 4 possibilities:
       
109973     **
       
109974     **     1. uPattern is an unescaped match-all character "%",
       
109975     **     2. uPattern is an unescaped match-one character "_",
       
109976     **     3. uPattern is an unescaped escape character, or
       
109977     **     4. uPattern is to be handled as an ordinary character
       
109978     */
       
109979     if( !prevEscape && uPattern==MATCH_ALL ){
       
109980       /* Case 1. */
       
109981       uint8_t c;
       
109982 
       
109983       /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
       
109984       ** MATCH_ALL. For each MATCH_ONE, skip one character in the 
       
109985       ** test string.
       
109986       */
       
109987       while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
       
109988         if( c==MATCH_ONE ){
       
109989           if( zString[iString]==0 ) return 0;
       
109990           U8_FWD_1_UNSAFE(zString, iString);
       
109991         }
       
109992         iPattern++;
       
109993       }
       
109994 
       
109995       if( zPattern[iPattern]==0 ) return 1;
       
109996 
       
109997       while( zString[iString] ){
       
109998         if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
       
109999           return 1;
       
110000         }
       
110001         U8_FWD_1_UNSAFE(zString, iString);
       
110002       }
       
110003       return 0;
       
110004 
       
110005     }else if( !prevEscape && uPattern==MATCH_ONE ){
       
110006       /* Case 2. */
       
110007       if( zString[iString]==0 ) return 0;
       
110008       U8_FWD_1_UNSAFE(zString, iString);
       
110009 
       
110010     }else if( !prevEscape && uPattern==uEsc){
       
110011       /* Case 3. */
       
110012       prevEscape = 1;
       
110013 
       
110014     }else{
       
110015       /* Case 4. */
       
110016       UChar32 uString;
       
110017       U8_NEXT_UNSAFE(zString, iString, uString);
       
110018       uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
       
110019       uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
       
110020       if( uString!=uPattern ){
       
110021         return 0;
       
110022       }
       
110023       prevEscape = 0;
       
110024     }
       
110025   }
       
110026 
       
110027   return zString[iString]==0;
       
110028 }
       
110029 
       
110030 /*
       
110031 ** Implementation of the like() SQL function.  This function implements
       
110032 ** the build-in LIKE operator.  The first argument to the function is the
       
110033 ** pattern and the second argument is the string.  So, the SQL statements:
       
110034 **
       
110035 **       A LIKE B
       
110036 **
       
110037 ** is implemented as like(B, A). If there is an escape character E, 
       
110038 **
       
110039 **       A LIKE B ESCAPE E
       
110040 **
       
110041 ** is mapped to like(B, A, E).
       
110042 */
       
110043 static void icuLikeFunc(
       
110044   sqlite3_context *context, 
       
110045   int argc, 
       
110046   sqlite3_value **argv
       
110047 ){
       
110048   const unsigned char *zA = sqlite3_value_text(argv[0]);
       
110049   const unsigned char *zB = sqlite3_value_text(argv[1]);
       
110050   UChar32 uEsc = 0;
       
110051 
       
110052   /* Limit the length of the LIKE or GLOB pattern to avoid problems
       
110053   ** of deep recursion and N*N behavior in patternCompare().
       
110054   */
       
110055   if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
       
110056     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
       
110057     return;
       
110058   }
       
110059 
       
110060 
       
110061   if( argc==3 ){
       
110062     /* The escape character string must consist of a single UTF-8 character.
       
110063     ** Otherwise, return an error.
       
110064     */
       
110065     int nE= sqlite3_value_bytes(argv[2]);
       
110066     const unsigned char *zE = sqlite3_value_text(argv[2]);
       
110067     int i = 0;
       
110068     if( zE==0 ) return;
       
110069     U8_NEXT(zE, i, nE, uEsc);
       
110070     if( i!=nE){
       
110071       sqlite3_result_error(context, 
       
110072           "ESCAPE expression must be a single character", -1);
       
110073       return;
       
110074     }
       
110075   }
       
110076 
       
110077   if( zA && zB ){
       
110078     sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
       
110079   }
       
110080 }
       
110081 
       
110082 /*
       
110083 ** This function is called when an ICU function called from within
       
110084 ** the implementation of an SQL scalar function returns an error.
       
110085 **
       
110086 ** The scalar function context passed as the first argument is 
       
110087 ** loaded with an error message based on the following two args.
       
110088 */
       
110089 static void icuFunctionError(
       
110090   sqlite3_context *pCtx,       /* SQLite scalar function context */
       
110091   const char *zName,           /* Name of ICU function that failed */
       
110092   UErrorCode e                 /* Error code returned by ICU function */
       
110093 ){
       
110094   char zBuf[128];
       
110095   sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
       
110096   zBuf[127] = '\0';
       
110097   sqlite3_result_error(pCtx, zBuf, -1);
       
110098 }
       
110099 
       
110100 /*
       
110101 ** Function to delete compiled regexp objects. Registered as
       
110102 ** a destructor function with sqlite3_set_auxdata().
       
110103 */
       
110104 static void icuRegexpDelete(void *p){
       
110105   URegularExpression *pExpr = (URegularExpression *)p;
       
110106   uregex_close(pExpr);
       
110107 }
       
110108 
       
110109 /*
       
110110 ** Implementation of SQLite REGEXP operator. This scalar function takes
       
110111 ** two arguments. The first is a regular expression pattern to compile
       
110112 ** the second is a string to match against that pattern. If either 
       
110113 ** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
       
110114 ** is 1 if the string matches the pattern, or 0 otherwise.
       
110115 **
       
110116 ** SQLite maps the regexp() function to the regexp() operator such
       
110117 ** that the following two are equivalent:
       
110118 **
       
110119 **     zString REGEXP zPattern
       
110120 **     regexp(zPattern, zString)
       
110121 **
       
110122 ** Uses the following ICU regexp APIs:
       
110123 **
       
110124 **     uregex_open()
       
110125 **     uregex_matches()
       
110126 **     uregex_close()
       
110127 */
       
110128 static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
       
110129   UErrorCode status = U_ZERO_ERROR;
       
110130   URegularExpression *pExpr;
       
110131   UBool res;
       
110132   const UChar *zString = sqlite3_value_text16(apArg[1]);
       
110133 
       
110134   /* If the left hand side of the regexp operator is NULL, 
       
110135   ** then the result is also NULL. 
       
110136   */
       
110137   if( !zString ){
       
110138     return;
       
110139   }
       
110140 
       
110141   pExpr = sqlite3_get_auxdata(p, 0);
       
110142   if( !pExpr ){
       
110143     const UChar *zPattern = sqlite3_value_text16(apArg[0]);
       
110144     if( !zPattern ){
       
110145       return;
       
110146     }
       
110147     pExpr = uregex_open(zPattern, -1, 0, 0, &status);
       
110148 
       
110149     if( U_SUCCESS(status) ){
       
110150       sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
       
110151     }else{
       
110152       assert(!pExpr);
       
110153       icuFunctionError(p, "uregex_open", status);
       
110154       return;
       
110155     }
       
110156   }
       
110157 
       
110158   /* Configure the text that the regular expression operates on. */
       
110159   uregex_setText(pExpr, zString, -1, &status);
       
110160   if( !U_SUCCESS(status) ){
       
110161     icuFunctionError(p, "uregex_setText", status);
       
110162     return;
       
110163   }
       
110164 
       
110165   /* Attempt the match */
       
110166   res = uregex_matches(pExpr, 0, &status);
       
110167   if( !U_SUCCESS(status) ){
       
110168     icuFunctionError(p, "uregex_matches", status);
       
110169     return;
       
110170   }
       
110171 
       
110172   /* Set the text that the regular expression operates on to a NULL
       
110173   ** pointer. This is not really necessary, but it is tidier than 
       
110174   ** leaving the regular expression object configured with an invalid
       
110175   ** pointer after this function returns.
       
110176   */
       
110177   uregex_setText(pExpr, 0, 0, &status);
       
110178 
       
110179   /* Return 1 or 0. */
       
110180   sqlite3_result_int(p, res ? 1 : 0);
       
110181 }
       
110182 
       
110183 /*
       
110184 ** Implementations of scalar functions for case mapping - upper() and 
       
110185 ** lower(). Function upper() converts its input to upper-case (ABC).
       
110186 ** Function lower() converts to lower-case (abc).
       
110187 **
       
110188 ** ICU provides two types of case mapping, "general" case mapping and
       
110189 ** "language specific". Refer to ICU documentation for the differences
       
110190 ** between the two.
       
110191 **
       
110192 ** To utilise "general" case mapping, the upper() or lower() scalar 
       
110193 ** functions are invoked with one argument:
       
110194 **
       
110195 **     upper('ABC') -> 'abc'
       
110196 **     lower('abc') -> 'ABC'
       
110197 **
       
110198 ** To access ICU "language specific" case mapping, upper() or lower()
       
110199 ** should be invoked with two arguments. The second argument is the name
       
110200 ** of the locale to use. Passing an empty string ("") or SQL NULL value
       
110201 ** as the second argument is the same as invoking the 1 argument version
       
110202 ** of upper() or lower().
       
110203 **
       
110204 **     lower('I', 'en_us') -> 'i'
       
110205 **     lower('I', 'tr_tr') -> 'ı' (small dotless i)
       
110206 **
       
110207 ** http://www.icu-project.org/userguide/posix.html#case_mappings
       
110208 */
       
110209 static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
       
110210   const UChar *zInput;
       
110211   UChar *zOutput;
       
110212   int nInput;
       
110213   int nOutput;
       
110214 
       
110215   UErrorCode status = U_ZERO_ERROR;
       
110216   const char *zLocale = 0;
       
110217 
       
110218   assert(nArg==1 || nArg==2);
       
110219   if( nArg==2 ){
       
110220     zLocale = (const char *)sqlite3_value_text(apArg[1]);
       
110221   }
       
110222 
       
110223   zInput = sqlite3_value_text16(apArg[0]);
       
110224   if( !zInput ){
       
110225     return;
       
110226   }
       
110227   nInput = sqlite3_value_bytes16(apArg[0]);
       
110228 
       
110229   nOutput = nInput * 2 + 2;
       
110230   zOutput = sqlite3_malloc(nOutput);
       
110231   if( !zOutput ){
       
110232     return;
       
110233   }
       
110234 
       
110235   if( sqlite3_user_data(p) ){
       
110236     u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
       
110237   }else{
       
110238     u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
       
110239   }
       
110240 
       
110241   if( !U_SUCCESS(status) ){
       
110242     icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
       
110243     return;
       
110244   }
       
110245 
       
110246   sqlite3_result_text16(p, zOutput, -1, xFree);
       
110247 }
       
110248 
       
110249 /*
       
110250 ** Collation sequence destructor function. The pCtx argument points to
       
110251 ** a UCollator structure previously allocated using ucol_open().
       
110252 */
       
110253 static void icuCollationDel(void *pCtx){
       
110254   UCollator *p = (UCollator *)pCtx;
       
110255   ucol_close(p);
       
110256 }
       
110257 
       
110258 /*
       
110259 ** Collation sequence comparison function. The pCtx argument points to
       
110260 ** a UCollator structure previously allocated using ucol_open().
       
110261 */
       
110262 static int icuCollationColl(
       
110263   void *pCtx,
       
110264   int nLeft,
       
110265   const void *zLeft,
       
110266   int nRight,
       
110267   const void *zRight
       
110268 ){
       
110269   UCollationResult res;
       
110270   UCollator *p = (UCollator *)pCtx;
       
110271   res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
       
110272   switch( res ){
       
110273     case UCOL_LESS:    return -1;
       
110274     case UCOL_GREATER: return +1;
       
110275     case UCOL_EQUAL:   return 0;
       
110276   }
       
110277   assert(!"Unexpected return value from ucol_strcoll()");
       
110278   return 0;
       
110279 }
       
110280 
       
110281 /*
       
110282 ** Implementation of the scalar function icu_load_collation().
       
110283 **
       
110284 ** This scalar function is used to add ICU collation based collation 
       
110285 ** types to an SQLite database connection. It is intended to be called
       
110286 ** as follows:
       
110287 **
       
110288 **     SELECT icu_load_collation(<locale>, <collation-name>);
       
110289 **
       
110290 ** Where <locale> is a string containing an ICU locale identifier (i.e.
       
110291 ** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
       
110292 ** collation sequence to create.
       
110293 */
       
110294 static void icuLoadCollation(
       
110295   sqlite3_context *p, 
       
110296   int nArg, 
       
110297   sqlite3_value **apArg
       
110298 ){
       
110299   sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
       
110300   UErrorCode status = U_ZERO_ERROR;
       
110301   const char *zLocale;      /* Locale identifier - (eg. "jp_JP") */
       
110302   const char *zName;        /* SQL Collation sequence name (eg. "japanese") */
       
110303   UCollator *pUCollator;    /* ICU library collation object */
       
110304   int rc;                   /* Return code from sqlite3_create_collation_x() */
       
110305 
       
110306   assert(nArg==2);
       
110307   zLocale = (const char *)sqlite3_value_text(apArg[0]);
       
110308   zName = (const char *)sqlite3_value_text(apArg[1]);
       
110309 
       
110310   if( !zLocale || !zName ){
       
110311     return;
       
110312   }
       
110313 
       
110314   pUCollator = ucol_open(zLocale, &status);
       
110315   if( !U_SUCCESS(status) ){
       
110316     icuFunctionError(p, "ucol_open", status);
       
110317     return;
       
110318   }
       
110319   assert(p);
       
110320 
       
110321   rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator, 
       
110322       icuCollationColl, icuCollationDel
       
110323   );
       
110324   if( rc!=SQLITE_OK ){
       
110325     ucol_close(pUCollator);
       
110326     sqlite3_result_error(p, "Error registering collation function", -1);
       
110327   }
       
110328 }
       
110329 
       
110330 /*
       
110331 ** Register the ICU extension functions with database db.
       
110332 */
       
110333 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
       
110334   struct IcuScalar {
       
110335     const char *zName;                        /* Function name */
       
110336     int nArg;                                 /* Number of arguments */
       
110337     int enc;                                  /* Optimal text encoding */
       
110338     void *pContext;                           /* sqlite3_user_data() context */
       
110339     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
       
110340   } scalars[] = {
       
110341     {"regexp",-1, SQLITE_ANY,          0, icuRegexpFunc},
       
110342 
       
110343     {"lower",  1, SQLITE_UTF16,        0, icuCaseFunc16},
       
110344     {"lower",  2, SQLITE_UTF16,        0, icuCaseFunc16},
       
110345     {"upper",  1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
       
110346     {"upper",  2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
       
110347 
       
110348     {"lower",  1, SQLITE_UTF8,         0, icuCaseFunc16},
       
110349     {"lower",  2, SQLITE_UTF8,         0, icuCaseFunc16},
       
110350     {"upper",  1, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
       
110351     {"upper",  2, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
       
110352 
       
110353     {"like",   2, SQLITE_UTF8,         0, icuLikeFunc},
       
110354     {"like",   3, SQLITE_UTF8,         0, icuLikeFunc},
       
110355 
       
110356     {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
       
110357   };
       
110358 
       
110359   int rc = SQLITE_OK;
       
110360   int i;
       
110361 
       
110362   for(i=0; rc==SQLITE_OK && i<(sizeof(scalars)/sizeof(struct IcuScalar)); i++){
       
110363     struct IcuScalar *p = &scalars[i];
       
110364     rc = sqlite3_create_function(
       
110365         db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
       
110366     );
       
110367   }
       
110368 
       
110369   return rc;
       
110370 }
       
110371 
       
110372 #if !SQLITE_CORE
       
110373 SQLITE_API int sqlite3_extension_init(
       
110374   sqlite3 *db, 
       
110375   char **pzErrMsg,
       
110376   const sqlite3_api_routines *pApi
       
110377 ){
       
110378   SQLITE_EXTENSION_INIT2(pApi)
       
110379   return sqlite3IcuInit(db);
       
110380 }
       
110381 #endif
       
110382 
       
110383 #endif
       
110384 
       
110385 /************** End of icu.c *************************************************/
       
110386 /************** Begin file fts3_icu.c ****************************************/
       
110387 /*
       
110388 ** 2007 June 22
       
110389 **
       
110390 ** The author disclaims copyright to this source code.  In place of
       
110391 ** a legal notice, here is a blessing:
       
110392 **
       
110393 **    May you do good and not evil.
       
110394 **    May you find forgiveness for yourself and forgive others.
       
110395 **    May you share freely, never taking more than you give.
       
110396 **
       
110397 *************************************************************************
       
110398 ** This file implements a tokenizer for fts3 based on the ICU library.
       
110399 ** 
       
110400 ** $Id: fts3_icu.c,v 1.3 2008/09/01 18:34:20 danielk1977 Exp $
       
110401 */
       
110402 
       
110403 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
       
110404 #ifdef SQLITE_ENABLE_ICU
       
110405 
       
110406 
       
110407 #include <unicode/ubrk.h>
       
110408 #include <unicode/utf16.h>
       
110409 
       
110410 typedef struct IcuTokenizer IcuTokenizer;
       
110411 typedef struct IcuCursor IcuCursor;
       
110412 
       
110413 struct IcuTokenizer {
       
110414   sqlite3_tokenizer base;
       
110415   char *zLocale;
       
110416 };
       
110417 
       
110418 struct IcuCursor {
       
110419   sqlite3_tokenizer_cursor base;
       
110420 
       
110421   UBreakIterator *pIter;      /* ICU break-iterator object */
       
110422   int nChar;                  /* Number of UChar elements in pInput */
       
110423   UChar *aChar;               /* Copy of input using utf-16 encoding */
       
110424   int *aOffset;               /* Offsets of each character in utf-8 input */
       
110425 
       
110426   int nBuffer;
       
110427   char *zBuffer;
       
110428 
       
110429   int iToken;
       
110430 };
       
110431 
       
110432 /*
       
110433 ** Create a new tokenizer instance.
       
110434 */
       
110435 static int icuCreate(
       
110436   int argc,                            /* Number of entries in argv[] */
       
110437   const char * const *argv,            /* Tokenizer creation arguments */
       
110438   sqlite3_tokenizer **ppTokenizer      /* OUT: Created tokenizer */
       
110439 ){
       
110440   IcuTokenizer *p;
       
110441   int n = 0;
       
110442 
       
110443   if( argc>0 ){
       
110444     n = strlen(argv[0])+1;
       
110445   }
       
110446   p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
       
110447   if( !p ){
       
110448     return SQLITE_NOMEM;
       
110449   }
       
110450   memset(p, 0, sizeof(IcuTokenizer));
       
110451 
       
110452   if( n ){
       
110453     p->zLocale = (char *)&p[1];
       
110454     memcpy(p->zLocale, argv[0], n);
       
110455   }
       
110456 
       
110457   *ppTokenizer = (sqlite3_tokenizer *)p;
       
110458 
       
110459   return SQLITE_OK;
       
110460 }
       
110461 
       
110462 /*
       
110463 ** Destroy a tokenizer
       
110464 */
       
110465 static int icuDestroy(sqlite3_tokenizer *pTokenizer){
       
110466   IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
       
110467   sqlite3_free(p);
       
110468   return SQLITE_OK;
       
110469 }
       
110470 
       
110471 /*
       
110472 ** Prepare to begin tokenizing a particular string.  The input
       
110473 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
       
110474 ** used to incrementally tokenize this string is returned in 
       
110475 ** *ppCursor.
       
110476 */
       
110477 static int icuOpen(
       
110478   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
       
110479   const char *zInput,                    /* Input string */
       
110480   int nInput,                            /* Length of zInput in bytes */
       
110481   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
       
110482 ){
       
110483   IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
       
110484   IcuCursor *pCsr;
       
110485 
       
110486   const int32_t opt = U_FOLD_CASE_DEFAULT;
       
110487   UErrorCode status = U_ZERO_ERROR;
       
110488   int nChar;
       
110489 
       
110490   UChar32 c;
       
110491   int iInput = 0;
       
110492   int iOut = 0;
       
110493 
       
110494   *ppCursor = 0;
       
110495 
       
110496   if( nInput<0 ){
       
110497     nInput = strlen(zInput);
       
110498   }
       
110499   nChar = nInput+1;
       
110500   pCsr = (IcuCursor *)sqlite3_malloc(
       
110501       sizeof(IcuCursor) +                /* IcuCursor */
       
110502       nChar * sizeof(UChar) +            /* IcuCursor.aChar[] */
       
110503       (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
       
110504   );
       
110505   if( !pCsr ){
       
110506     return SQLITE_NOMEM;
       
110507   }
       
110508   memset(pCsr, 0, sizeof(IcuCursor));
       
110509   pCsr->aChar = (UChar *)&pCsr[1];
       
110510   pCsr->aOffset = (int *)&pCsr->aChar[nChar];
       
110511 
       
110512   pCsr->aOffset[iOut] = iInput;
       
110513   U8_NEXT(zInput, iInput, nInput, c); 
       
110514   while( c>0 ){
       
110515     int isError = 0;
       
110516     c = u_foldCase(c, opt);
       
110517     U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
       
110518     if( isError ){
       
110519       sqlite3_free(pCsr);
       
110520       return SQLITE_ERROR;
       
110521     }
       
110522     pCsr->aOffset[iOut] = iInput;
       
110523 
       
110524     if( iInput<nInput ){
       
110525       U8_NEXT(zInput, iInput, nInput, c);
       
110526     }else{
       
110527       c = 0;
       
110528     }
       
110529   }
       
110530 
       
110531   pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
       
110532   if( !U_SUCCESS(status) ){
       
110533     sqlite3_free(pCsr);
       
110534     return SQLITE_ERROR;
       
110535   }
       
110536   pCsr->nChar = iOut;
       
110537 
       
110538   ubrk_first(pCsr->pIter);
       
110539   *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
       
110540   return SQLITE_OK;
       
110541 }
       
110542 
       
110543 /*
       
110544 ** Close a tokenization cursor previously opened by a call to icuOpen().
       
110545 */
       
110546 static int icuClose(sqlite3_tokenizer_cursor *pCursor){
       
110547   IcuCursor *pCsr = (IcuCursor *)pCursor;
       
110548   ubrk_close(pCsr->pIter);
       
110549   sqlite3_free(pCsr->zBuffer);
       
110550   sqlite3_free(pCsr);
       
110551   return SQLITE_OK;
       
110552 }
       
110553 
       
110554 /*
       
110555 ** Extract the next token from a tokenization cursor.
       
110556 */
       
110557 static int icuNext(
       
110558   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
       
110559   const char **ppToken,               /* OUT: *ppToken is the token text */
       
110560   int *pnBytes,                       /* OUT: Number of bytes in token */
       
110561   int *piStartOffset,                 /* OUT: Starting offset of token */
       
110562   int *piEndOffset,                   /* OUT: Ending offset of token */
       
110563   int *piPosition                     /* OUT: Position integer of token */
       
110564 ){
       
110565   IcuCursor *pCsr = (IcuCursor *)pCursor;
       
110566 
       
110567   int iStart = 0;
       
110568   int iEnd = 0;
       
110569   int nByte = 0;
       
110570 
       
110571   while( iStart==iEnd ){
       
110572     UChar32 c;
       
110573 
       
110574     iStart = ubrk_current(pCsr->pIter);
       
110575     iEnd = ubrk_next(pCsr->pIter);
       
110576     if( iEnd==UBRK_DONE ){
       
110577       return SQLITE_DONE;
       
110578     }
       
110579 
       
110580     while( iStart<iEnd ){
       
110581       int iWhite = iStart;
       
110582       U8_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
       
110583       if( u_isspace(c) ){
       
110584         iStart = iWhite;
       
110585       }else{
       
110586         break;
       
110587       }
       
110588     }
       
110589     assert(iStart<=iEnd);
       
110590   }
       
110591 
       
110592   do {
       
110593     UErrorCode status = U_ZERO_ERROR;
       
110594     if( nByte ){
       
110595       char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
       
110596       if( !zNew ){
       
110597         return SQLITE_NOMEM;
       
110598       }
       
110599       pCsr->zBuffer = zNew;
       
110600       pCsr->nBuffer = nByte;
       
110601     }
       
110602 
       
110603     u_strToUTF8(
       
110604         pCsr->zBuffer, pCsr->nBuffer, &nByte,    /* Output vars */
       
110605         &pCsr->aChar[iStart], iEnd-iStart,       /* Input vars */
       
110606         &status                                  /* Output success/failure */
       
110607     );
       
110608   } while( nByte>pCsr->nBuffer );
       
110609 
       
110610   *ppToken = pCsr->zBuffer;
       
110611   *pnBytes = nByte;
       
110612   *piStartOffset = pCsr->aOffset[iStart];
       
110613   *piEndOffset = pCsr->aOffset[iEnd];
       
110614   *piPosition = pCsr->iToken++;
       
110615 
       
110616   return SQLITE_OK;
       
110617 }
       
110618 
       
110619 /*
       
110620 ** The set of routines that implement the simple tokenizer
       
110621 */
       
110622 static const sqlite3_tokenizer_module icuTokenizerModule = {
       
110623   0,                           /* iVersion */
       
110624   icuCreate,                   /* xCreate  */
       
110625   icuDestroy,                  /* xCreate  */
       
110626   icuOpen,                     /* xOpen    */
       
110627   icuClose,                    /* xClose   */
       
110628   icuNext,                     /* xNext    */
       
110629 };
       
110630 
       
110631 /*
       
110632 ** Set *ppModule to point at the implementation of the ICU tokenizer.
       
110633 */
       
110634 SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
       
110635   sqlite3_tokenizer_module const**ppModule
       
110636 ){
       
110637   *ppModule = &icuTokenizerModule;
       
110638 }
       
110639 
       
110640 #endif /* defined(SQLITE_ENABLE_ICU) */
       
110641 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
       
110642 
       
110643 /************** End of fts3_icu.c ********************************************/